1 /*- 2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 3 * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/bio.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/systm.h> 41 #include <sys/taskqueue.h> 42 #include <geom/geom.h> 43 #include "geom/raid/g_raid.h" 44 #include "g_raid_md_if.h" 45 46 static MALLOC_DEFINE(M_MD_INTEL, "md_intel_data", "GEOM_RAID Intel metadata"); 47 48 struct intel_raid_map { 49 uint32_t offset; 50 uint32_t disk_sectors; 51 uint32_t stripe_count; 52 uint16_t strip_sectors; 53 uint8_t status; 54 #define INTEL_S_READY 0x00 55 #define INTEL_S_UNINITIALIZED 0x01 56 #define INTEL_S_DEGRADED 0x02 57 #define INTEL_S_FAILURE 0x03 58 59 uint8_t type; 60 #define INTEL_T_RAID0 0x00 61 #define INTEL_T_RAID1 0x01 62 #define INTEL_T_RAID5 0x05 63 64 uint8_t total_disks; 65 uint8_t total_domains; 66 uint8_t failed_disk_num; 67 uint8_t ddf; 68 uint32_t offset_hi; 69 uint32_t disk_sectors_hi; 70 uint32_t stripe_count_hi; 71 uint32_t filler_2[4]; 72 uint32_t disk_idx[1]; /* total_disks entries. */ 73 #define INTEL_DI_IDX 0x00ffffff 74 #define INTEL_DI_RBLD 0x01000000 75 } __packed; 76 77 struct intel_raid_vol { 78 uint8_t name[16]; 79 u_int64_t total_sectors __packed; 80 uint32_t state; 81 #define INTEL_ST_BOOTABLE 0x00000001 82 #define INTEL_ST_BOOT_DEVICE 0x00000002 83 #define INTEL_ST_READ_COALESCING 0x00000004 84 #define INTEL_ST_WRITE_COALESCING 0x00000008 85 #define INTEL_ST_LAST_SHUTDOWN_DIRTY 0x00000010 86 #define INTEL_ST_HIDDEN_AT_BOOT 0x00000020 87 #define INTEL_ST_CURRENTLY_HIDDEN 0x00000040 88 #define INTEL_ST_VERIFY_AND_FIX 0x00000080 89 #define INTEL_ST_MAP_STATE_UNINIT 0x00000100 90 #define INTEL_ST_NO_AUTO_RECOVERY 0x00000200 91 #define INTEL_ST_CLONE_N_GO 0x00000400 92 #define INTEL_ST_CLONE_MAN_SYNC 0x00000800 93 #define INTEL_ST_CNG_MASTER_DISK_NUM 0x00001000 94 uint32_t reserved; 95 uint8_t migr_priority; 96 uint8_t num_sub_vols; 97 uint8_t tid; 98 uint8_t cng_master_disk; 99 uint16_t cache_policy; 100 uint8_t cng_state; 101 uint8_t cng_sub_state; 102 uint32_t filler_0[10]; 103 104 uint32_t curr_migr_unit; 105 uint32_t checkpoint_id; 106 uint8_t migr_state; 107 uint8_t migr_type; 108 #define INTEL_MT_INIT 0 109 #define INTEL_MT_REBUILD 1 110 #define INTEL_MT_VERIFY 2 111 #define INTEL_MT_GEN_MIGR 3 112 #define INTEL_MT_STATE_CHANGE 4 113 #define INTEL_MT_REPAIR 5 114 uint8_t dirty; 115 uint8_t fs_state; 116 uint16_t verify_errors; 117 uint16_t bad_blocks; 118 uint32_t curr_migr_unit_hi; 119 uint32_t filler_1[3]; 120 struct intel_raid_map map[1]; /* 2 entries if migr_state != 0. */ 121 } __packed; 122 123 struct intel_raid_disk { 124 #define INTEL_SERIAL_LEN 16 125 uint8_t serial[INTEL_SERIAL_LEN]; 126 uint32_t sectors; 127 uint32_t id; 128 uint32_t flags; 129 #define INTEL_F_SPARE 0x01 130 #define INTEL_F_ASSIGNED 0x02 131 #define INTEL_F_FAILED 0x04 132 #define INTEL_F_ONLINE 0x08 133 uint32_t owner_cfg_num; 134 uint32_t sectors_hi; 135 uint32_t filler[3]; 136 } __packed; 137 138 struct intel_raid_conf { 139 uint8_t intel_id[24]; 140 #define INTEL_MAGIC "Intel Raid ISM Cfg Sig. " 141 142 uint8_t version[6]; 143 #define INTEL_VERSION_1000 "1.0.00" /* RAID0 */ 144 #define INTEL_VERSION_1100 "1.1.00" /* RAID1 */ 145 #define INTEL_VERSION_1200 "1.2.00" /* Many volumes */ 146 #define INTEL_VERSION_1201 "1.2.01" /* 3 or 4 disks */ 147 #define INTEL_VERSION_1202 "1.2.02" /* RAID5 */ 148 #define INTEL_VERSION_1204 "1.2.04" /* 5 or 6 disks */ 149 #define INTEL_VERSION_1206 "1.2.06" /* CNG */ 150 #define INTEL_VERSION_1300 "1.3.00" /* Attributes */ 151 152 uint8_t dummy_0[2]; 153 uint32_t checksum; 154 uint32_t config_size; 155 uint32_t config_id; 156 uint32_t generation; 157 uint32_t error_log_size; 158 uint32_t attributes; 159 #define INTEL_ATTR_RAID0 0x00000001 160 #define INTEL_ATTR_RAID1 0x00000002 161 #define INTEL_ATTR_RAID10 0x00000004 162 #define INTEL_ATTR_RAID1E 0x00000008 163 #define INTEL_ATTR_RAID5 0x00000010 164 #define INTEL_ATTR_RAIDCNG 0x00000020 165 #define INTEL_ATTR_2TB 0x20000000 166 #define INTEL_ATTR_PM 0x40000000 167 #define INTEL_ATTR_CHECKSUM 0x80000000 168 169 uint8_t total_disks; 170 uint8_t total_volumes; 171 uint8_t dummy_2[2]; 172 uint32_t filler_0[39]; 173 struct intel_raid_disk disk[1]; /* total_disks entries. */ 174 /* Here goes total_volumes of struct intel_raid_vol. */ 175 } __packed; 176 177 #define INTEL_MAX_MD_SIZE(ndisks) \ 178 (sizeof(struct intel_raid_conf) + \ 179 sizeof(struct intel_raid_disk) * (ndisks - 1) + \ 180 sizeof(struct intel_raid_vol) * 2 + \ 181 sizeof(struct intel_raid_map) * 2 + \ 182 sizeof(uint32_t) * (ndisks - 1) * 4) 183 184 struct g_raid_md_intel_perdisk { 185 struct intel_raid_conf *pd_meta; 186 int pd_disk_pos; 187 struct intel_raid_disk pd_disk_meta; 188 }; 189 190 struct g_raid_md_intel_object { 191 struct g_raid_md_object mdio_base; 192 uint32_t mdio_config_id; 193 uint32_t mdio_generation; 194 struct intel_raid_conf *mdio_meta; 195 struct callout mdio_start_co; /* STARTING state timer. */ 196 int mdio_disks_present; 197 int mdio_started; 198 int mdio_incomplete; 199 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */ 200 }; 201 202 static g_raid_md_create_t g_raid_md_create_intel; 203 static g_raid_md_taste_t g_raid_md_taste_intel; 204 static g_raid_md_event_t g_raid_md_event_intel; 205 static g_raid_md_ctl_t g_raid_md_ctl_intel; 206 static g_raid_md_write_t g_raid_md_write_intel; 207 static g_raid_md_fail_disk_t g_raid_md_fail_disk_intel; 208 static g_raid_md_free_disk_t g_raid_md_free_disk_intel; 209 static g_raid_md_free_t g_raid_md_free_intel; 210 211 static kobj_method_t g_raid_md_intel_methods[] = { 212 KOBJMETHOD(g_raid_md_create, g_raid_md_create_intel), 213 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_intel), 214 KOBJMETHOD(g_raid_md_event, g_raid_md_event_intel), 215 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_intel), 216 KOBJMETHOD(g_raid_md_write, g_raid_md_write_intel), 217 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_intel), 218 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_intel), 219 KOBJMETHOD(g_raid_md_free, g_raid_md_free_intel), 220 { 0, 0 } 221 }; 222 223 static struct g_raid_md_class g_raid_md_intel_class = { 224 "Intel", 225 g_raid_md_intel_methods, 226 sizeof(struct g_raid_md_intel_object), 227 .mdc_enable = 1, 228 .mdc_priority = 100 229 }; 230 231 232 static struct intel_raid_map * 233 intel_get_map(struct intel_raid_vol *mvol, int i) 234 { 235 struct intel_raid_map *mmap; 236 237 if (i > (mvol->migr_state ? 1 : 0)) 238 return (NULL); 239 mmap = &mvol->map[0]; 240 for (; i > 0; i--) { 241 mmap = (struct intel_raid_map *) 242 &mmap->disk_idx[mmap->total_disks]; 243 } 244 return ((struct intel_raid_map *)mmap); 245 } 246 247 static struct intel_raid_vol * 248 intel_get_volume(struct intel_raid_conf *meta, int i) 249 { 250 struct intel_raid_vol *mvol; 251 struct intel_raid_map *mmap; 252 253 if (i > 1) 254 return (NULL); 255 mvol = (struct intel_raid_vol *)&meta->disk[meta->total_disks]; 256 for (; i > 0; i--) { 257 mmap = intel_get_map(mvol, mvol->migr_state ? 1 : 0); 258 mvol = (struct intel_raid_vol *) 259 &mmap->disk_idx[mmap->total_disks]; 260 } 261 return (mvol); 262 } 263 264 static off_t 265 intel_get_map_offset(struct intel_raid_map *mmap) 266 { 267 off_t offset = (off_t)mmap->offset_hi << 32; 268 269 offset += mmap->offset; 270 return (offset); 271 } 272 273 static void 274 intel_set_map_offset(struct intel_raid_map *mmap, off_t offset) 275 { 276 277 mmap->offset = offset & 0xffffffff; 278 mmap->offset_hi = offset >> 32; 279 } 280 281 static off_t 282 intel_get_map_disk_sectors(struct intel_raid_map *mmap) 283 { 284 off_t disk_sectors = (off_t)mmap->disk_sectors_hi << 32; 285 286 disk_sectors += mmap->disk_sectors; 287 return (disk_sectors); 288 } 289 290 static void 291 intel_set_map_disk_sectors(struct intel_raid_map *mmap, off_t disk_sectors) 292 { 293 294 mmap->disk_sectors = disk_sectors & 0xffffffff; 295 mmap->disk_sectors_hi = disk_sectors >> 32; 296 } 297 298 static void 299 intel_set_map_stripe_count(struct intel_raid_map *mmap, off_t stripe_count) 300 { 301 302 mmap->stripe_count = stripe_count & 0xffffffff; 303 mmap->stripe_count_hi = stripe_count >> 32; 304 } 305 306 static off_t 307 intel_get_disk_sectors(struct intel_raid_disk *disk) 308 { 309 off_t sectors = (off_t)disk->sectors_hi << 32; 310 311 sectors += disk->sectors; 312 return (sectors); 313 } 314 315 static void 316 intel_set_disk_sectors(struct intel_raid_disk *disk, off_t sectors) 317 { 318 319 disk->sectors = sectors & 0xffffffff; 320 disk->sectors_hi = sectors >> 32; 321 } 322 323 static off_t 324 intel_get_vol_curr_migr_unit(struct intel_raid_vol *vol) 325 { 326 off_t curr_migr_unit = (off_t)vol->curr_migr_unit_hi << 32; 327 328 curr_migr_unit += vol->curr_migr_unit; 329 return (curr_migr_unit); 330 } 331 332 static void 333 intel_set_vol_curr_migr_unit(struct intel_raid_vol *vol, off_t curr_migr_unit) 334 { 335 336 vol->curr_migr_unit = curr_migr_unit & 0xffffffff; 337 vol->curr_migr_unit_hi = curr_migr_unit >> 32; 338 } 339 340 static void 341 g_raid_md_intel_print(struct intel_raid_conf *meta) 342 { 343 struct intel_raid_vol *mvol; 344 struct intel_raid_map *mmap; 345 int i, j, k; 346 347 if (g_raid_debug < 1) 348 return; 349 350 printf("********* ATA Intel MatrixRAID Metadata *********\n"); 351 printf("intel_id <%.24s>\n", meta->intel_id); 352 printf("version <%.6s>\n", meta->version); 353 printf("checksum 0x%08x\n", meta->checksum); 354 printf("config_size 0x%08x\n", meta->config_size); 355 printf("config_id 0x%08x\n", meta->config_id); 356 printf("generation 0x%08x\n", meta->generation); 357 printf("attributes 0x%08x\n", meta->attributes); 358 printf("total_disks %u\n", meta->total_disks); 359 printf("total_volumes %u\n", meta->total_volumes); 360 printf("DISK# serial disk_sectors disk_sectors_hi disk_id flags\n"); 361 for (i = 0; i < meta->total_disks; i++ ) { 362 printf(" %d <%.16s> %u %u 0x%08x 0x%08x\n", i, 363 meta->disk[i].serial, meta->disk[i].sectors, 364 meta->disk[i].sectors_hi, 365 meta->disk[i].id, meta->disk[i].flags); 366 } 367 for (i = 0; i < meta->total_volumes; i++) { 368 mvol = intel_get_volume(meta, i); 369 printf(" ****** Volume %d ******\n", i); 370 printf(" name %.16s\n", mvol->name); 371 printf(" total_sectors %ju\n", mvol->total_sectors); 372 printf(" state %u\n", mvol->state); 373 printf(" reserved %u\n", mvol->reserved); 374 printf(" curr_migr_unit %u\n", mvol->curr_migr_unit); 375 printf(" curr_migr_unit_hi %u\n", mvol->curr_migr_unit_hi); 376 printf(" checkpoint_id %u\n", mvol->checkpoint_id); 377 printf(" migr_state %u\n", mvol->migr_state); 378 printf(" migr_type %u\n", mvol->migr_type); 379 printf(" dirty %u\n", mvol->dirty); 380 381 for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) { 382 printf(" *** Map %d ***\n", j); 383 mmap = intel_get_map(mvol, j); 384 printf(" offset %u\n", mmap->offset); 385 printf(" offset_hi %u\n", mmap->offset_hi); 386 printf(" disk_sectors %u\n", mmap->disk_sectors); 387 printf(" disk_sectors_hi %u\n", mmap->disk_sectors_hi); 388 printf(" stripe_count %u\n", mmap->stripe_count); 389 printf(" stripe_count_hi %u\n", mmap->stripe_count_hi); 390 printf(" strip_sectors %u\n", mmap->strip_sectors); 391 printf(" status %u\n", mmap->status); 392 printf(" type %u\n", mmap->type); 393 printf(" total_disks %u\n", mmap->total_disks); 394 printf(" total_domains %u\n", mmap->total_domains); 395 printf(" failed_disk_num %u\n", mmap->failed_disk_num); 396 printf(" ddf %u\n", mmap->ddf); 397 printf(" disk_idx "); 398 for (k = 0; k < mmap->total_disks; k++) 399 printf(" 0x%08x", mmap->disk_idx[k]); 400 printf("\n"); 401 } 402 } 403 printf("=================================================\n"); 404 } 405 406 static struct intel_raid_conf * 407 intel_meta_copy(struct intel_raid_conf *meta) 408 { 409 struct intel_raid_conf *nmeta; 410 411 nmeta = malloc(meta->config_size, M_MD_INTEL, M_WAITOK); 412 memcpy(nmeta, meta, meta->config_size); 413 return (nmeta); 414 } 415 416 static int 417 intel_meta_find_disk(struct intel_raid_conf *meta, char *serial) 418 { 419 int pos; 420 421 for (pos = 0; pos < meta->total_disks; pos++) { 422 if (strncmp(meta->disk[pos].serial, 423 serial, INTEL_SERIAL_LEN) == 0) 424 return (pos); 425 } 426 return (-1); 427 } 428 429 static struct intel_raid_conf * 430 intel_meta_read(struct g_consumer *cp) 431 { 432 struct g_provider *pp; 433 struct intel_raid_conf *meta; 434 struct intel_raid_vol *mvol; 435 struct intel_raid_map *mmap; 436 char *buf; 437 int error, i, j, k, left, size; 438 uint32_t checksum, *ptr; 439 440 pp = cp->provider; 441 442 /* Read the anchor sector. */ 443 buf = g_read_data(cp, 444 pp->mediasize - pp->sectorsize * 2, pp->sectorsize, &error); 445 if (buf == NULL) { 446 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 447 pp->name, error); 448 return (NULL); 449 } 450 meta = (struct intel_raid_conf *)buf; 451 452 /* Check if this is an Intel RAID struct */ 453 if (strncmp(meta->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) { 454 G_RAID_DEBUG(1, "Intel signature check failed on %s", pp->name); 455 g_free(buf); 456 return (NULL); 457 } 458 if (meta->config_size > 65536 || 459 meta->config_size < sizeof(struct intel_raid_conf)) { 460 G_RAID_DEBUG(1, "Intel metadata size looks wrong: %d", 461 meta->config_size); 462 g_free(buf); 463 return (NULL); 464 } 465 size = meta->config_size; 466 meta = malloc(size, M_MD_INTEL, M_WAITOK); 467 memcpy(meta, buf, min(size, pp->sectorsize)); 468 g_free(buf); 469 470 /* Read all the rest, if needed. */ 471 if (meta->config_size > pp->sectorsize) { 472 left = (meta->config_size - 1) / pp->sectorsize; 473 buf = g_read_data(cp, 474 pp->mediasize - pp->sectorsize * (2 + left), 475 pp->sectorsize * left, &error); 476 if (buf == NULL) { 477 G_RAID_DEBUG(1, "Cannot read remaining metadata" 478 " part from %s (error=%d).", 479 pp->name, error); 480 free(meta, M_MD_INTEL); 481 return (NULL); 482 } 483 memcpy(((char *)meta) + pp->sectorsize, buf, 484 pp->sectorsize * left); 485 g_free(buf); 486 } 487 488 /* Check metadata checksum. */ 489 for (checksum = 0, ptr = (uint32_t *)meta, i = 0; 490 i < (meta->config_size / sizeof(uint32_t)); i++) { 491 checksum += *ptr++; 492 } 493 checksum -= meta->checksum; 494 if (checksum != meta->checksum) { 495 G_RAID_DEBUG(1, "Intel checksum check failed on %s", pp->name); 496 free(meta, M_MD_INTEL); 497 return (NULL); 498 } 499 500 /* Validate metadata size. */ 501 size = sizeof(struct intel_raid_conf) + 502 sizeof(struct intel_raid_disk) * (meta->total_disks - 1) + 503 sizeof(struct intel_raid_vol) * meta->total_volumes; 504 if (size > meta->config_size) { 505 badsize: 506 G_RAID_DEBUG(1, "Intel metadata size incorrect %d < %d", 507 meta->config_size, size); 508 free(meta, M_MD_INTEL); 509 return (NULL); 510 } 511 for (i = 0; i < meta->total_volumes; i++) { 512 mvol = intel_get_volume(meta, i); 513 mmap = intel_get_map(mvol, 0); 514 size += 4 * (mmap->total_disks - 1); 515 if (size > meta->config_size) 516 goto badsize; 517 if (mvol->migr_state) { 518 size += sizeof(struct intel_raid_map); 519 if (size > meta->config_size) 520 goto badsize; 521 mmap = intel_get_map(mvol, 1); 522 size += 4 * (mmap->total_disks - 1); 523 if (size > meta->config_size) 524 goto badsize; 525 } 526 } 527 528 /* Validate disk indexes. */ 529 for (i = 0; i < meta->total_volumes; i++) { 530 mvol = intel_get_volume(meta, i); 531 for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) { 532 mmap = intel_get_map(mvol, j); 533 for (k = 0; k < mmap->total_disks; k++) { 534 if ((mmap->disk_idx[k] & INTEL_DI_IDX) > 535 meta->total_disks) { 536 G_RAID_DEBUG(1, "Intel metadata disk" 537 " index %d too big (>%d)", 538 mmap->disk_idx[k] & INTEL_DI_IDX, 539 meta->total_disks); 540 free(meta, M_MD_INTEL); 541 return (NULL); 542 } 543 } 544 } 545 } 546 547 /* Validate migration types. */ 548 for (i = 0; i < meta->total_volumes; i++) { 549 mvol = intel_get_volume(meta, i); 550 if (mvol->migr_state && 551 mvol->migr_type != INTEL_MT_INIT && 552 mvol->migr_type != INTEL_MT_REBUILD && 553 mvol->migr_type != INTEL_MT_VERIFY && 554 mvol->migr_type != INTEL_MT_REPAIR) { 555 G_RAID_DEBUG(1, "Intel metadata has unsupported" 556 " migration type %d", mvol->migr_type); 557 free(meta, M_MD_INTEL); 558 return (NULL); 559 } 560 } 561 562 return (meta); 563 } 564 565 static int 566 intel_meta_write(struct g_consumer *cp, struct intel_raid_conf *meta) 567 { 568 struct g_provider *pp; 569 char *buf; 570 int error, i, sectors; 571 uint32_t checksum, *ptr; 572 573 pp = cp->provider; 574 575 /* Recalculate checksum for case if metadata were changed. */ 576 meta->checksum = 0; 577 for (checksum = 0, ptr = (uint32_t *)meta, i = 0; 578 i < (meta->config_size / sizeof(uint32_t)); i++) { 579 checksum += *ptr++; 580 } 581 meta->checksum = checksum; 582 583 /* Create and fill buffer. */ 584 sectors = (meta->config_size + pp->sectorsize - 1) / pp->sectorsize; 585 buf = malloc(sectors * pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO); 586 if (sectors > 1) { 587 memcpy(buf, ((char *)meta) + pp->sectorsize, 588 (sectors - 1) * pp->sectorsize); 589 } 590 memcpy(buf + (sectors - 1) * pp->sectorsize, meta, pp->sectorsize); 591 592 error = g_write_data(cp, 593 pp->mediasize - pp->sectorsize * (1 + sectors), 594 buf, pp->sectorsize * sectors); 595 if (error != 0) { 596 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 597 pp->name, error); 598 } 599 600 free(buf, M_MD_INTEL); 601 return (error); 602 } 603 604 static int 605 intel_meta_erase(struct g_consumer *cp) 606 { 607 struct g_provider *pp; 608 char *buf; 609 int error; 610 611 pp = cp->provider; 612 buf = malloc(pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO); 613 error = g_write_data(cp, 614 pp->mediasize - 2 * pp->sectorsize, 615 buf, pp->sectorsize); 616 if (error != 0) { 617 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 618 pp->name, error); 619 } 620 free(buf, M_MD_INTEL); 621 return (error); 622 } 623 624 static int 625 intel_meta_write_spare(struct g_consumer *cp, struct intel_raid_disk *d) 626 { 627 struct intel_raid_conf *meta; 628 int error; 629 630 /* Fill anchor and single disk. */ 631 meta = malloc(INTEL_MAX_MD_SIZE(1), M_MD_INTEL, M_WAITOK | M_ZERO); 632 memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1); 633 memcpy(&meta->version[0], INTEL_VERSION_1000, 634 sizeof(INTEL_VERSION_1000) - 1); 635 meta->config_size = INTEL_MAX_MD_SIZE(1); 636 meta->config_id = arc4random(); 637 meta->generation = 1; 638 meta->total_disks = 1; 639 meta->disk[0] = *d; 640 error = intel_meta_write(cp, meta); 641 free(meta, M_MD_INTEL); 642 return (error); 643 } 644 645 static struct g_raid_disk * 646 g_raid_md_intel_get_disk(struct g_raid_softc *sc, int id) 647 { 648 struct g_raid_disk *disk; 649 struct g_raid_md_intel_perdisk *pd; 650 651 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 652 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 653 if (pd->pd_disk_pos == id) 654 break; 655 } 656 return (disk); 657 } 658 659 static int 660 g_raid_md_intel_supported(int level, int qual, int disks, int force) 661 { 662 663 switch (level) { 664 case G_RAID_VOLUME_RL_RAID0: 665 if (disks < 1) 666 return (0); 667 if (!force && (disks < 2 || disks > 6)) 668 return (0); 669 break; 670 case G_RAID_VOLUME_RL_RAID1: 671 if (disks < 1) 672 return (0); 673 if (!force && (disks != 2)) 674 return (0); 675 break; 676 case G_RAID_VOLUME_RL_RAID1E: 677 if (disks < 2) 678 return (0); 679 if (!force && (disks != 4)) 680 return (0); 681 break; 682 case G_RAID_VOLUME_RL_RAID5: 683 if (disks < 3) 684 return (0); 685 if (!force && disks > 6) 686 return (0); 687 if (qual != G_RAID_VOLUME_RLQ_R5LA) 688 return (0); 689 break; 690 default: 691 return (0); 692 } 693 if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE) 694 return (0); 695 return (1); 696 } 697 698 static struct g_raid_volume * 699 g_raid_md_intel_get_volume(struct g_raid_softc *sc, int id) 700 { 701 struct g_raid_volume *mvol; 702 703 TAILQ_FOREACH(mvol, &sc->sc_volumes, v_next) { 704 if ((intptr_t)(mvol->v_md_data) == id) 705 break; 706 } 707 return (mvol); 708 } 709 710 static int 711 g_raid_md_intel_start_disk(struct g_raid_disk *disk) 712 { 713 struct g_raid_softc *sc; 714 struct g_raid_subdisk *sd, *tmpsd; 715 struct g_raid_disk *olddisk, *tmpdisk; 716 struct g_raid_md_object *md; 717 struct g_raid_md_intel_object *mdi; 718 struct g_raid_md_intel_perdisk *pd, *oldpd; 719 struct intel_raid_conf *meta; 720 struct intel_raid_vol *mvol; 721 struct intel_raid_map *mmap0, *mmap1; 722 int disk_pos, resurrection = 0; 723 724 sc = disk->d_softc; 725 md = sc->sc_md; 726 mdi = (struct g_raid_md_intel_object *)md; 727 meta = mdi->mdio_meta; 728 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 729 olddisk = NULL; 730 731 /* Find disk position in metadata by it's serial. */ 732 disk_pos = intel_meta_find_disk(meta, pd->pd_disk_meta.serial); 733 if (disk_pos < 0) { 734 G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk"); 735 /* Failed stale disk is useless for us. */ 736 if (pd->pd_disk_meta.flags & INTEL_F_FAILED) { 737 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED); 738 return (0); 739 } 740 /* If we are in the start process, that's all for now. */ 741 if (!mdi->mdio_started) 742 goto nofit; 743 /* 744 * If we have already started - try to get use of the disk. 745 * Try to replace OFFLINE disks first, then FAILED. 746 */ 747 TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) { 748 if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE && 749 tmpdisk->d_state != G_RAID_DISK_S_FAILED) 750 continue; 751 /* Make sure this disk is big enough. */ 752 TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) { 753 off_t disk_sectors = 754 intel_get_disk_sectors(&pd->pd_disk_meta); 755 756 if (sd->sd_offset + sd->sd_size + 4096 > 757 disk_sectors * 512) { 758 G_RAID_DEBUG1(1, sc, 759 "Disk too small (%llu < %llu)", 760 (unsigned long long) 761 disk_sectors * 512, 762 (unsigned long long) 763 sd->sd_offset + sd->sd_size + 4096); 764 break; 765 } 766 } 767 if (sd != NULL) 768 continue; 769 if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) { 770 olddisk = tmpdisk; 771 break; 772 } else if (olddisk == NULL) 773 olddisk = tmpdisk; 774 } 775 if (olddisk == NULL) { 776 nofit: 777 if (pd->pd_disk_meta.flags & INTEL_F_SPARE) { 778 g_raid_change_disk_state(disk, 779 G_RAID_DISK_S_SPARE); 780 return (1); 781 } else { 782 g_raid_change_disk_state(disk, 783 G_RAID_DISK_S_STALE); 784 return (0); 785 } 786 } 787 oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data; 788 disk_pos = oldpd->pd_disk_pos; 789 resurrection = 1; 790 } 791 792 if (olddisk == NULL) { 793 /* Find placeholder by position. */ 794 olddisk = g_raid_md_intel_get_disk(sc, disk_pos); 795 if (olddisk == NULL) 796 panic("No disk at position %d!", disk_pos); 797 if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) { 798 G_RAID_DEBUG1(1, sc, "More then one disk for pos %d", 799 disk_pos); 800 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE); 801 return (0); 802 } 803 oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data; 804 } 805 806 /* Replace failed disk or placeholder with new disk. */ 807 TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) { 808 TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next); 809 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 810 sd->sd_disk = disk; 811 } 812 oldpd->pd_disk_pos = -2; 813 pd->pd_disk_pos = disk_pos; 814 815 /* If it was placeholder -- destroy it. */ 816 if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) { 817 g_raid_destroy_disk(olddisk); 818 } else { 819 /* Otherwise, make it STALE_FAILED. */ 820 g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED); 821 /* Update global metadata just in case. */ 822 memcpy(&meta->disk[disk_pos], &pd->pd_disk_meta, 823 sizeof(struct intel_raid_disk)); 824 } 825 826 /* Welcome the new disk. */ 827 if (resurrection) 828 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 829 else if (meta->disk[disk_pos].flags & INTEL_F_FAILED) 830 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 831 else if (meta->disk[disk_pos].flags & INTEL_F_SPARE) 832 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE); 833 else 834 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 835 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 836 mvol = intel_get_volume(meta, 837 (uintptr_t)(sd->sd_volume->v_md_data)); 838 mmap0 = intel_get_map(mvol, 0); 839 if (mvol->migr_state) 840 mmap1 = intel_get_map(mvol, 1); 841 else 842 mmap1 = mmap0; 843 844 if (resurrection) { 845 /* Stale disk, almost same as new. */ 846 g_raid_change_subdisk_state(sd, 847 G_RAID_SUBDISK_S_NEW); 848 } else if (meta->disk[disk_pos].flags & INTEL_F_FAILED) { 849 /* Failed disk, almost useless. */ 850 g_raid_change_subdisk_state(sd, 851 G_RAID_SUBDISK_S_FAILED); 852 } else if (mvol->migr_state == 0) { 853 if (mmap0->status == INTEL_S_UNINITIALIZED) { 854 /* Freshly created uninitialized volume. */ 855 g_raid_change_subdisk_state(sd, 856 G_RAID_SUBDISK_S_UNINITIALIZED); 857 } else if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) { 858 /* Freshly inserted disk. */ 859 g_raid_change_subdisk_state(sd, 860 G_RAID_SUBDISK_S_NEW); 861 } else if (mvol->dirty) { 862 /* Dirty volume (unclean shutdown). */ 863 g_raid_change_subdisk_state(sd, 864 G_RAID_SUBDISK_S_STALE); 865 } else { 866 /* Up to date disk. */ 867 g_raid_change_subdisk_state(sd, 868 G_RAID_SUBDISK_S_ACTIVE); 869 } 870 } else if (mvol->migr_type == INTEL_MT_INIT || 871 mvol->migr_type == INTEL_MT_REBUILD) { 872 if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) { 873 /* Freshly inserted disk. */ 874 g_raid_change_subdisk_state(sd, 875 G_RAID_SUBDISK_S_NEW); 876 } else if (mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) { 877 /* Rebuilding disk. */ 878 g_raid_change_subdisk_state(sd, 879 G_RAID_SUBDISK_S_REBUILD); 880 if (mvol->dirty) { 881 sd->sd_rebuild_pos = 0; 882 } else { 883 sd->sd_rebuild_pos = 884 intel_get_vol_curr_migr_unit(mvol) * 885 sd->sd_volume->v_strip_size * 886 mmap0->total_domains; 887 } 888 } else if (mvol->dirty) { 889 /* Dirty volume (unclean shutdown). */ 890 g_raid_change_subdisk_state(sd, 891 G_RAID_SUBDISK_S_STALE); 892 } else { 893 /* Up to date disk. */ 894 g_raid_change_subdisk_state(sd, 895 G_RAID_SUBDISK_S_ACTIVE); 896 } 897 } else if (mvol->migr_type == INTEL_MT_VERIFY || 898 mvol->migr_type == INTEL_MT_REPAIR) { 899 if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) { 900 /* Freshly inserted disk. */ 901 g_raid_change_subdisk_state(sd, 902 G_RAID_SUBDISK_S_NEW); 903 } else if (mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) { 904 /* Resyncing disk. */ 905 g_raid_change_subdisk_state(sd, 906 G_RAID_SUBDISK_S_RESYNC); 907 if (mvol->dirty) { 908 sd->sd_rebuild_pos = 0; 909 } else { 910 sd->sd_rebuild_pos = 911 intel_get_vol_curr_migr_unit(mvol) * 912 sd->sd_volume->v_strip_size * 913 mmap0->total_domains; 914 } 915 } else if (mvol->dirty) { 916 /* Dirty volume (unclean shutdown). */ 917 g_raid_change_subdisk_state(sd, 918 G_RAID_SUBDISK_S_STALE); 919 } else { 920 /* Up to date disk. */ 921 g_raid_change_subdisk_state(sd, 922 G_RAID_SUBDISK_S_ACTIVE); 923 } 924 } 925 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 926 G_RAID_EVENT_SUBDISK); 927 } 928 929 /* Update status of our need for spare. */ 930 if (mdi->mdio_started) { 931 mdi->mdio_incomplete = 932 (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 933 meta->total_disks); 934 } 935 936 return (resurrection); 937 } 938 939 static void 940 g_disk_md_intel_retaste(void *arg, int pending) 941 { 942 943 G_RAID_DEBUG(1, "Array is not complete, trying to retaste."); 944 g_retaste(&g_raid_class); 945 free(arg, M_MD_INTEL); 946 } 947 948 static void 949 g_raid_md_intel_refill(struct g_raid_softc *sc) 950 { 951 struct g_raid_md_object *md; 952 struct g_raid_md_intel_object *mdi; 953 struct intel_raid_conf *meta; 954 struct g_raid_disk *disk; 955 struct task *task; 956 int update, na; 957 958 md = sc->sc_md; 959 mdi = (struct g_raid_md_intel_object *)md; 960 meta = mdi->mdio_meta; 961 update = 0; 962 do { 963 /* Make sure we miss anything. */ 964 na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE); 965 if (na == meta->total_disks) 966 break; 967 968 G_RAID_DEBUG1(1, md->mdo_softc, 969 "Array is not complete (%d of %d), " 970 "trying to refill.", na, meta->total_disks); 971 972 /* Try to get use some of STALE disks. */ 973 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 974 if (disk->d_state == G_RAID_DISK_S_STALE) { 975 update += g_raid_md_intel_start_disk(disk); 976 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 977 break; 978 } 979 } 980 if (disk != NULL) 981 continue; 982 983 /* Try to get use some of SPARE disks. */ 984 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 985 if (disk->d_state == G_RAID_DISK_S_SPARE) { 986 update += g_raid_md_intel_start_disk(disk); 987 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 988 break; 989 } 990 } 991 } while (disk != NULL); 992 993 /* Write new metadata if we changed something. */ 994 if (update) { 995 g_raid_md_write_intel(md, NULL, NULL, NULL); 996 meta = mdi->mdio_meta; 997 } 998 999 /* Update status of our need for spare. */ 1000 mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 1001 meta->total_disks); 1002 1003 /* Request retaste hoping to find spare. */ 1004 if (mdi->mdio_incomplete) { 1005 task = malloc(sizeof(struct task), 1006 M_MD_INTEL, M_WAITOK | M_ZERO); 1007 TASK_INIT(task, 0, g_disk_md_intel_retaste, task); 1008 taskqueue_enqueue(taskqueue_swi, task); 1009 } 1010 } 1011 1012 static void 1013 g_raid_md_intel_start(struct g_raid_softc *sc) 1014 { 1015 struct g_raid_md_object *md; 1016 struct g_raid_md_intel_object *mdi; 1017 struct g_raid_md_intel_perdisk *pd; 1018 struct intel_raid_conf *meta; 1019 struct intel_raid_vol *mvol; 1020 struct intel_raid_map *mmap; 1021 struct g_raid_volume *vol; 1022 struct g_raid_subdisk *sd; 1023 struct g_raid_disk *disk; 1024 int i, j, disk_pos; 1025 1026 md = sc->sc_md; 1027 mdi = (struct g_raid_md_intel_object *)md; 1028 meta = mdi->mdio_meta; 1029 1030 /* Create volumes and subdisks. */ 1031 for (i = 0; i < meta->total_volumes; i++) { 1032 mvol = intel_get_volume(meta, i); 1033 mmap = intel_get_map(mvol, 0); 1034 vol = g_raid_create_volume(sc, mvol->name, -1); 1035 vol->v_md_data = (void *)(intptr_t)i; 1036 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 1037 if (mmap->type == INTEL_T_RAID0) 1038 vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 1039 else if (mmap->type == INTEL_T_RAID1 && 1040 mmap->total_domains >= 2 && 1041 mmap->total_domains <= mmap->total_disks) { 1042 /* Assume total_domains is correct. */ 1043 if (mmap->total_domains == mmap->total_disks) 1044 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 1045 else 1046 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 1047 } else if (mmap->type == INTEL_T_RAID1) { 1048 /* total_domains looks wrong. */ 1049 if (mmap->total_disks <= 2) 1050 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 1051 else 1052 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 1053 } else if (mmap->type == INTEL_T_RAID5) { 1054 vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 1055 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA; 1056 } else 1057 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 1058 vol->v_strip_size = (u_int)mmap->strip_sectors * 512; //ZZZ 1059 vol->v_disks_count = mmap->total_disks; 1060 vol->v_mediasize = (off_t)mvol->total_sectors * 512; //ZZZ 1061 vol->v_sectorsize = 512; //ZZZ 1062 for (j = 0; j < vol->v_disks_count; j++) { 1063 sd = &vol->v_subdisks[j]; 1064 sd->sd_offset = intel_get_map_offset(mmap) * 512; //ZZZ 1065 sd->sd_size = intel_get_map_disk_sectors(mmap) * 512; //ZZZ 1066 } 1067 g_raid_start_volume(vol); 1068 } 1069 1070 /* Create disk placeholders to store data for later writing. */ 1071 for (disk_pos = 0; disk_pos < meta->total_disks; disk_pos++) { 1072 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 1073 pd->pd_disk_pos = disk_pos; 1074 pd->pd_disk_meta = meta->disk[disk_pos]; 1075 disk = g_raid_create_disk(sc); 1076 disk->d_md_data = (void *)pd; 1077 disk->d_state = G_RAID_DISK_S_OFFLINE; 1078 for (i = 0; i < meta->total_volumes; i++) { 1079 mvol = intel_get_volume(meta, i); 1080 mmap = intel_get_map(mvol, 0); 1081 for (j = 0; j < mmap->total_disks; j++) { 1082 if ((mmap->disk_idx[j] & INTEL_DI_IDX) == disk_pos) 1083 break; 1084 } 1085 if (j == mmap->total_disks) 1086 continue; 1087 vol = g_raid_md_intel_get_volume(sc, i); 1088 sd = &vol->v_subdisks[j]; 1089 sd->sd_disk = disk; 1090 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1091 } 1092 } 1093 1094 /* Make all disks found till the moment take their places. */ 1095 do { 1096 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1097 if (disk->d_state == G_RAID_DISK_S_NONE) { 1098 g_raid_md_intel_start_disk(disk); 1099 break; 1100 } 1101 } 1102 } while (disk != NULL); 1103 1104 mdi->mdio_started = 1; 1105 G_RAID_DEBUG1(0, sc, "Array started."); 1106 g_raid_md_write_intel(md, NULL, NULL, NULL); 1107 1108 /* Pickup any STALE/SPARE disks to refill array if needed. */ 1109 g_raid_md_intel_refill(sc); 1110 1111 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1112 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1113 G_RAID_EVENT_VOLUME); 1114 } 1115 1116 callout_stop(&mdi->mdio_start_co); 1117 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount); 1118 root_mount_rel(mdi->mdio_rootmount); 1119 mdi->mdio_rootmount = NULL; 1120 } 1121 1122 static void 1123 g_raid_md_intel_new_disk(struct g_raid_disk *disk) 1124 { 1125 struct g_raid_softc *sc; 1126 struct g_raid_md_object *md; 1127 struct g_raid_md_intel_object *mdi; 1128 struct intel_raid_conf *pdmeta; 1129 struct g_raid_md_intel_perdisk *pd; 1130 1131 sc = disk->d_softc; 1132 md = sc->sc_md; 1133 mdi = (struct g_raid_md_intel_object *)md; 1134 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 1135 pdmeta = pd->pd_meta; 1136 1137 if (mdi->mdio_started) { 1138 if (g_raid_md_intel_start_disk(disk)) 1139 g_raid_md_write_intel(md, NULL, NULL, NULL); 1140 } else { 1141 /* If we haven't started yet - check metadata freshness. */ 1142 if (mdi->mdio_meta == NULL || 1143 ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) { 1144 G_RAID_DEBUG1(1, sc, "Newer disk"); 1145 if (mdi->mdio_meta != NULL) 1146 free(mdi->mdio_meta, M_MD_INTEL); 1147 mdi->mdio_meta = intel_meta_copy(pdmeta); 1148 mdi->mdio_generation = mdi->mdio_meta->generation; 1149 mdi->mdio_disks_present = 1; 1150 } else if (pdmeta->generation == mdi->mdio_generation) { 1151 mdi->mdio_disks_present++; 1152 G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 1153 mdi->mdio_disks_present, 1154 mdi->mdio_meta->total_disks); 1155 } else { 1156 G_RAID_DEBUG1(1, sc, "Older disk"); 1157 } 1158 /* If we collected all needed disks - start array. */ 1159 if (mdi->mdio_disks_present == mdi->mdio_meta->total_disks) 1160 g_raid_md_intel_start(sc); 1161 } 1162 } 1163 1164 static void 1165 g_raid_intel_go(void *arg) 1166 { 1167 struct g_raid_softc *sc; 1168 struct g_raid_md_object *md; 1169 struct g_raid_md_intel_object *mdi; 1170 1171 sc = arg; 1172 md = sc->sc_md; 1173 mdi = (struct g_raid_md_intel_object *)md; 1174 if (!mdi->mdio_started) { 1175 G_RAID_DEBUG1(0, sc, "Force array start due to timeout."); 1176 g_raid_event_send(sc, G_RAID_NODE_E_START, 0); 1177 } 1178 } 1179 1180 static int 1181 g_raid_md_create_intel(struct g_raid_md_object *md, struct g_class *mp, 1182 struct g_geom **gp) 1183 { 1184 struct g_raid_softc *sc; 1185 struct g_raid_md_intel_object *mdi; 1186 char name[16]; 1187 1188 mdi = (struct g_raid_md_intel_object *)md; 1189 mdi->mdio_config_id = arc4random(); 1190 mdi->mdio_generation = 0; 1191 snprintf(name, sizeof(name), "Intel-%08x", mdi->mdio_config_id); 1192 sc = g_raid_create_node(mp, name, md); 1193 if (sc == NULL) 1194 return (G_RAID_MD_TASTE_FAIL); 1195 md->mdo_softc = sc; 1196 *gp = sc->sc_geom; 1197 return (G_RAID_MD_TASTE_NEW); 1198 } 1199 1200 /* 1201 * Return the last N characters of the serial label. The Linux and 1202 * ataraid(7) code always uses the last 16 characters of the label to 1203 * store into the Intel meta format. Generalize this to N characters 1204 * since that's easy. Labels can be up to 20 characters for SATA drives 1205 * and up 251 characters for SAS drives. Since intel controllers don't 1206 * support SAS drives, just stick with the SATA limits for stack friendliness. 1207 */ 1208 static int 1209 g_raid_md_get_label(struct g_consumer *cp, char *serial, int serlen) 1210 { 1211 char serial_buffer[24]; 1212 int len, error; 1213 1214 len = sizeof(serial_buffer); 1215 error = g_io_getattr("GEOM::ident", cp, &len, serial_buffer); 1216 if (error != 0) 1217 return (error); 1218 len = strlen(serial_buffer); 1219 if (len > serlen) 1220 len -= serlen; 1221 else 1222 len = 0; 1223 strncpy(serial, serial_buffer + len, serlen); 1224 return (0); 1225 } 1226 1227 static int 1228 g_raid_md_taste_intel(struct g_raid_md_object *md, struct g_class *mp, 1229 struct g_consumer *cp, struct g_geom **gp) 1230 { 1231 struct g_consumer *rcp; 1232 struct g_provider *pp; 1233 struct g_raid_md_intel_object *mdi, *mdi1; 1234 struct g_raid_softc *sc; 1235 struct g_raid_disk *disk; 1236 struct intel_raid_conf *meta; 1237 struct g_raid_md_intel_perdisk *pd; 1238 struct g_geom *geom; 1239 int error, disk_pos, result, spare, len; 1240 char serial[INTEL_SERIAL_LEN]; 1241 char name[16]; 1242 uint16_t vendor; 1243 1244 G_RAID_DEBUG(1, "Tasting Intel on %s", cp->provider->name); 1245 mdi = (struct g_raid_md_intel_object *)md; 1246 pp = cp->provider; 1247 1248 /* Read metadata from device. */ 1249 meta = NULL; 1250 vendor = 0xffff; 1251 disk_pos = 0; 1252 if (g_access(cp, 1, 0, 0) != 0) 1253 return (G_RAID_MD_TASTE_FAIL); 1254 g_topology_unlock(); 1255 error = g_raid_md_get_label(cp, serial, sizeof(serial)); 1256 if (error != 0) { 1257 G_RAID_DEBUG(1, "Cannot get serial number from %s (error=%d).", 1258 pp->name, error); 1259 goto fail2; 1260 } 1261 len = 2; 1262 if (pp->geom->rank == 1) 1263 g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 1264 meta = intel_meta_read(cp); 1265 g_topology_lock(); 1266 g_access(cp, -1, 0, 0); 1267 if (meta == NULL) { 1268 if (g_raid_aggressive_spare) { 1269 if (vendor != 0x8086) { 1270 G_RAID_DEBUG(1, 1271 "Intel vendor mismatch 0x%04x != 0x8086", 1272 vendor); 1273 } else { 1274 G_RAID_DEBUG(1, 1275 "No Intel metadata, forcing spare."); 1276 spare = 2; 1277 goto search; 1278 } 1279 } 1280 return (G_RAID_MD_TASTE_FAIL); 1281 } 1282 1283 /* Check this disk position in obtained metadata. */ 1284 disk_pos = intel_meta_find_disk(meta, serial); 1285 if (disk_pos < 0) { 1286 G_RAID_DEBUG(1, "Intel serial '%s' not found", serial); 1287 goto fail1; 1288 } 1289 if (intel_get_disk_sectors(&meta->disk[disk_pos]) != 1290 (pp->mediasize / pp->sectorsize)) { 1291 G_RAID_DEBUG(1, "Intel size mismatch %ju != %ju", 1292 intel_get_disk_sectors(&meta->disk[disk_pos]), 1293 (off_t)(pp->mediasize / pp->sectorsize)); 1294 goto fail1; 1295 } 1296 1297 /* Metadata valid. Print it. */ 1298 g_raid_md_intel_print(meta); 1299 G_RAID_DEBUG(1, "Intel disk position %d", disk_pos); 1300 spare = meta->disk[disk_pos].flags & INTEL_F_SPARE; 1301 1302 search: 1303 /* Search for matching node. */ 1304 sc = NULL; 1305 mdi1 = NULL; 1306 LIST_FOREACH(geom, &mp->geom, geom) { 1307 sc = geom->softc; 1308 if (sc == NULL) 1309 continue; 1310 if (sc->sc_stopping != 0) 1311 continue; 1312 if (sc->sc_md->mdo_class != md->mdo_class) 1313 continue; 1314 mdi1 = (struct g_raid_md_intel_object *)sc->sc_md; 1315 if (spare) { 1316 if (mdi1->mdio_incomplete) 1317 break; 1318 } else { 1319 if (mdi1->mdio_config_id == meta->config_id) 1320 break; 1321 } 1322 } 1323 1324 /* Found matching node. */ 1325 if (geom != NULL) { 1326 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name); 1327 result = G_RAID_MD_TASTE_EXISTING; 1328 1329 } else if (spare) { /* Not found needy node -- left for later. */ 1330 G_RAID_DEBUG(1, "Spare is not needed at this time"); 1331 goto fail1; 1332 1333 } else { /* Not found matching node -- create one. */ 1334 result = G_RAID_MD_TASTE_NEW; 1335 mdi->mdio_config_id = meta->config_id; 1336 snprintf(name, sizeof(name), "Intel-%08x", meta->config_id); 1337 sc = g_raid_create_node(mp, name, md); 1338 md->mdo_softc = sc; 1339 geom = sc->sc_geom; 1340 callout_init(&mdi->mdio_start_co, 1); 1341 callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz, 1342 g_raid_intel_go, sc); 1343 mdi->mdio_rootmount = root_mount_hold("GRAID-Intel"); 1344 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount); 1345 } 1346 1347 rcp = g_new_consumer(geom); 1348 g_attach(rcp, pp); 1349 if (g_access(rcp, 1, 1, 1) != 0) 1350 ; //goto fail1; 1351 1352 g_topology_unlock(); 1353 sx_xlock(&sc->sc_lock); 1354 1355 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 1356 pd->pd_meta = meta; 1357 pd->pd_disk_pos = -1; 1358 if (spare == 2) { 1359 memcpy(&pd->pd_disk_meta.serial[0], serial, INTEL_SERIAL_LEN); 1360 intel_set_disk_sectors(&pd->pd_disk_meta, 1361 pp->mediasize / pp->sectorsize); 1362 pd->pd_disk_meta.id = 0; 1363 pd->pd_disk_meta.flags = INTEL_F_SPARE; 1364 } else { 1365 pd->pd_disk_meta = meta->disk[disk_pos]; 1366 } 1367 disk = g_raid_create_disk(sc); 1368 disk->d_md_data = (void *)pd; 1369 disk->d_consumer = rcp; 1370 rcp->private = disk; 1371 1372 g_raid_get_disk_info(disk); 1373 1374 g_raid_md_intel_new_disk(disk); 1375 1376 sx_xunlock(&sc->sc_lock); 1377 g_topology_lock(); 1378 *gp = geom; 1379 return (result); 1380 fail2: 1381 g_topology_lock(); 1382 g_access(cp, -1, 0, 0); 1383 fail1: 1384 free(meta, M_MD_INTEL); 1385 return (G_RAID_MD_TASTE_FAIL); 1386 } 1387 1388 static int 1389 g_raid_md_event_intel(struct g_raid_md_object *md, 1390 struct g_raid_disk *disk, u_int event) 1391 { 1392 struct g_raid_softc *sc; 1393 struct g_raid_subdisk *sd; 1394 struct g_raid_md_intel_object *mdi; 1395 struct g_raid_md_intel_perdisk *pd; 1396 1397 sc = md->mdo_softc; 1398 mdi = (struct g_raid_md_intel_object *)md; 1399 if (disk == NULL) { 1400 switch (event) { 1401 case G_RAID_NODE_E_START: 1402 if (!mdi->mdio_started) 1403 g_raid_md_intel_start(sc); 1404 return (0); 1405 } 1406 return (-1); 1407 } 1408 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 1409 switch (event) { 1410 case G_RAID_DISK_E_DISCONNECTED: 1411 /* If disk was assigned, just update statuses. */ 1412 if (pd->pd_disk_pos >= 0) { 1413 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1414 if (disk->d_consumer) { 1415 g_raid_kill_consumer(sc, disk->d_consumer); 1416 disk->d_consumer = NULL; 1417 } 1418 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1419 g_raid_change_subdisk_state(sd, 1420 G_RAID_SUBDISK_S_NONE); 1421 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1422 G_RAID_EVENT_SUBDISK); 1423 } 1424 } else { 1425 /* Otherwise -- delete. */ 1426 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1427 g_raid_destroy_disk(disk); 1428 } 1429 1430 /* Write updated metadata to all disks. */ 1431 g_raid_md_write_intel(md, NULL, NULL, NULL); 1432 1433 /* Check if anything left except placeholders. */ 1434 if (g_raid_ndisks(sc, -1) == 1435 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1436 g_raid_destroy_node(sc, 0); 1437 else 1438 g_raid_md_intel_refill(sc); 1439 return (0); 1440 } 1441 return (-2); 1442 } 1443 1444 static int 1445 g_raid_md_ctl_intel(struct g_raid_md_object *md, 1446 struct gctl_req *req) 1447 { 1448 struct g_raid_softc *sc; 1449 struct g_raid_volume *vol, *vol1; 1450 struct g_raid_subdisk *sd; 1451 struct g_raid_disk *disk; 1452 struct g_raid_md_intel_object *mdi; 1453 struct g_raid_md_intel_perdisk *pd; 1454 struct g_consumer *cp; 1455 struct g_provider *pp; 1456 char arg[16], serial[INTEL_SERIAL_LEN]; 1457 const char *nodename, *verb, *volname, *levelname, *diskname; 1458 char *tmp; 1459 int *nargs, *force; 1460 off_t off, size, sectorsize, strip, disk_sectors; 1461 intmax_t *sizearg, *striparg; 1462 int numdisks, i, len, level, qual, update; 1463 int error; 1464 1465 sc = md->mdo_softc; 1466 mdi = (struct g_raid_md_intel_object *)md; 1467 verb = gctl_get_param(req, "verb", NULL); 1468 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1469 error = 0; 1470 if (strcmp(verb, "label") == 0) { 1471 1472 if (*nargs < 4) { 1473 gctl_error(req, "Invalid number of arguments."); 1474 return (-1); 1475 } 1476 volname = gctl_get_asciiparam(req, "arg1"); 1477 if (volname == NULL) { 1478 gctl_error(req, "No volume name."); 1479 return (-2); 1480 } 1481 levelname = gctl_get_asciiparam(req, "arg2"); 1482 if (levelname == NULL) { 1483 gctl_error(req, "No RAID level."); 1484 return (-3); 1485 } 1486 if (strcasecmp(levelname, "RAID5") == 0) 1487 levelname = "RAID5-LA"; 1488 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1489 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1490 return (-4); 1491 } 1492 numdisks = *nargs - 3; 1493 force = gctl_get_paraml(req, "force", sizeof(*force)); 1494 if (!g_raid_md_intel_supported(level, qual, numdisks, 1495 force ? *force : 0)) { 1496 gctl_error(req, "Unsupported RAID level " 1497 "(0x%02x/0x%02x), or number of disks (%d).", 1498 level, qual, numdisks); 1499 return (-5); 1500 } 1501 1502 /* Search for disks, connect them and probe. */ 1503 size = 0x7fffffffffffffffllu; 1504 sectorsize = 0; 1505 for (i = 0; i < numdisks; i++) { 1506 snprintf(arg, sizeof(arg), "arg%d", i + 3); 1507 diskname = gctl_get_asciiparam(req, arg); 1508 if (diskname == NULL) { 1509 gctl_error(req, "No disk name (%s).", arg); 1510 error = -6; 1511 break; 1512 } 1513 if (strcmp(diskname, "NONE") == 0) { 1514 cp = NULL; 1515 pp = NULL; 1516 } else { 1517 g_topology_lock(); 1518 cp = g_raid_open_consumer(sc, diskname); 1519 if (cp == NULL) { 1520 gctl_error(req, "Can't open disk '%s'.", 1521 diskname); 1522 g_topology_unlock(); 1523 error = -7; 1524 break; 1525 } 1526 pp = cp->provider; 1527 } 1528 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 1529 pd->pd_disk_pos = i; 1530 disk = g_raid_create_disk(sc); 1531 disk->d_md_data = (void *)pd; 1532 disk->d_consumer = cp; 1533 if (cp == NULL) { 1534 strcpy(&pd->pd_disk_meta.serial[0], "NONE"); 1535 pd->pd_disk_meta.id = 0xffffffff; 1536 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED; 1537 continue; 1538 } 1539 cp->private = disk; 1540 g_topology_unlock(); 1541 1542 error = g_raid_md_get_label(cp, 1543 &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN); 1544 if (error != 0) { 1545 gctl_error(req, 1546 "Can't get serial for provider '%s'.", 1547 diskname); 1548 error = -8; 1549 break; 1550 } 1551 1552 g_raid_get_disk_info(disk); 1553 1554 intel_set_disk_sectors(&pd->pd_disk_meta, 1555 pp->mediasize / pp->sectorsize); 1556 if (size > pp->mediasize) 1557 size = pp->mediasize; 1558 if (sectorsize < pp->sectorsize) 1559 sectorsize = pp->sectorsize; 1560 pd->pd_disk_meta.id = 0; 1561 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED | INTEL_F_ONLINE; 1562 } 1563 if (error != 0) 1564 return (error); 1565 1566 if (sectorsize <= 0) { 1567 gctl_error(req, "Can't get sector size."); 1568 return (-8); 1569 } 1570 1571 /* Reserve some space for metadata. */ 1572 size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize; 1573 1574 /* Handle size argument. */ 1575 len = sizeof(*sizearg); 1576 sizearg = gctl_get_param(req, "size", &len); 1577 if (sizearg != NULL && len == sizeof(*sizearg) && 1578 *sizearg > 0) { 1579 if (*sizearg > size) { 1580 gctl_error(req, "Size too big %lld > %lld.", 1581 (long long)*sizearg, (long long)size); 1582 return (-9); 1583 } 1584 size = *sizearg; 1585 } 1586 1587 /* Handle strip argument. */ 1588 strip = 131072; 1589 len = sizeof(*striparg); 1590 striparg = gctl_get_param(req, "strip", &len); 1591 if (striparg != NULL && len == sizeof(*striparg) && 1592 *striparg > 0) { 1593 if (*striparg < sectorsize) { 1594 gctl_error(req, "Strip size too small."); 1595 return (-10); 1596 } 1597 if (*striparg % sectorsize != 0) { 1598 gctl_error(req, "Incorrect strip size."); 1599 return (-11); 1600 } 1601 if (strip > 65535 * sectorsize) { 1602 gctl_error(req, "Strip size too big."); 1603 return (-12); 1604 } 1605 strip = *striparg; 1606 } 1607 1608 /* Round size down to strip or sector. */ 1609 if (level == G_RAID_VOLUME_RL_RAID1) 1610 size -= (size % sectorsize); 1611 else if (level == G_RAID_VOLUME_RL_RAID1E && 1612 (numdisks & 1) != 0) 1613 size -= (size % (2 * strip)); 1614 else 1615 size -= (size % strip); 1616 if (size <= 0) { 1617 gctl_error(req, "Size too small."); 1618 return (-13); 1619 } 1620 1621 /* We have all we need, create things: volume, ... */ 1622 mdi->mdio_started = 1; 1623 vol = g_raid_create_volume(sc, volname, -1); 1624 vol->v_md_data = (void *)(intptr_t)0; 1625 vol->v_raid_level = level; 1626 vol->v_raid_level_qualifier = qual; 1627 vol->v_strip_size = strip; 1628 vol->v_disks_count = numdisks; 1629 if (level == G_RAID_VOLUME_RL_RAID0) 1630 vol->v_mediasize = size * numdisks; 1631 else if (level == G_RAID_VOLUME_RL_RAID1) 1632 vol->v_mediasize = size; 1633 else if (level == G_RAID_VOLUME_RL_RAID5) 1634 vol->v_mediasize = size * (numdisks - 1); 1635 else { /* RAID1E */ 1636 vol->v_mediasize = ((size * numdisks) / strip / 2) * 1637 strip; 1638 } 1639 vol->v_sectorsize = sectorsize; 1640 g_raid_start_volume(vol); 1641 1642 /* , and subdisks. */ 1643 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1644 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 1645 sd = &vol->v_subdisks[pd->pd_disk_pos]; 1646 sd->sd_disk = disk; 1647 sd->sd_offset = 0; 1648 sd->sd_size = size; 1649 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1650 if (sd->sd_disk->d_consumer != NULL) { 1651 g_raid_change_disk_state(disk, 1652 G_RAID_DISK_S_ACTIVE); 1653 if (level == G_RAID_VOLUME_RL_RAID5) 1654 g_raid_change_subdisk_state(sd, 1655 G_RAID_SUBDISK_S_UNINITIALIZED); 1656 else 1657 g_raid_change_subdisk_state(sd, 1658 G_RAID_SUBDISK_S_ACTIVE); 1659 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1660 G_RAID_EVENT_SUBDISK); 1661 } else { 1662 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1663 } 1664 } 1665 1666 /* Write metadata based on created entities. */ 1667 G_RAID_DEBUG1(0, sc, "Array started."); 1668 g_raid_md_write_intel(md, NULL, NULL, NULL); 1669 1670 /* Pickup any STALE/SPARE disks to refill array if needed. */ 1671 g_raid_md_intel_refill(sc); 1672 1673 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1674 G_RAID_EVENT_VOLUME); 1675 return (0); 1676 } 1677 if (strcmp(verb, "add") == 0) { 1678 1679 if (*nargs != 3) { 1680 gctl_error(req, "Invalid number of arguments."); 1681 return (-1); 1682 } 1683 volname = gctl_get_asciiparam(req, "arg1"); 1684 if (volname == NULL) { 1685 gctl_error(req, "No volume name."); 1686 return (-2); 1687 } 1688 levelname = gctl_get_asciiparam(req, "arg2"); 1689 if (levelname == NULL) { 1690 gctl_error(req, "No RAID level."); 1691 return (-3); 1692 } 1693 if (strcasecmp(levelname, "RAID5") == 0) 1694 levelname = "RAID5-LA"; 1695 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1696 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1697 return (-4); 1698 } 1699 1700 /* Look for existing volumes. */ 1701 i = 0; 1702 vol1 = NULL; 1703 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1704 vol1 = vol; 1705 i++; 1706 } 1707 if (i > 1) { 1708 gctl_error(req, "Maximum two volumes supported."); 1709 return (-6); 1710 } 1711 if (vol1 == NULL) { 1712 gctl_error(req, "At least one volume must exist."); 1713 return (-7); 1714 } 1715 1716 numdisks = vol1->v_disks_count; 1717 force = gctl_get_paraml(req, "force", sizeof(*force)); 1718 if (!g_raid_md_intel_supported(level, qual, numdisks, 1719 force ? *force : 0)) { 1720 gctl_error(req, "Unsupported RAID level " 1721 "(0x%02x/0x%02x), or number of disks (%d).", 1722 level, qual, numdisks); 1723 return (-5); 1724 } 1725 1726 /* Collect info about present disks. */ 1727 size = 0x7fffffffffffffffllu; 1728 sectorsize = 512; 1729 for (i = 0; i < numdisks; i++) { 1730 disk = vol1->v_subdisks[i].sd_disk; 1731 pd = (struct g_raid_md_intel_perdisk *) 1732 disk->d_md_data; 1733 disk_sectors = 1734 intel_get_disk_sectors(&pd->pd_disk_meta); 1735 1736 if (disk_sectors * 512 < size) 1737 size = disk_sectors * 512; 1738 if (disk->d_consumer != NULL && 1739 disk->d_consumer->provider != NULL && 1740 disk->d_consumer->provider->sectorsize > 1741 sectorsize) { 1742 sectorsize = 1743 disk->d_consumer->provider->sectorsize; 1744 } 1745 } 1746 1747 /* Reserve some space for metadata. */ 1748 size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize; 1749 1750 /* Decide insert before or after. */ 1751 sd = &vol1->v_subdisks[0]; 1752 if (sd->sd_offset > 1753 size - (sd->sd_offset + sd->sd_size)) { 1754 off = 0; 1755 size = sd->sd_offset; 1756 } else { 1757 off = sd->sd_offset + sd->sd_size; 1758 size = size - (sd->sd_offset + sd->sd_size); 1759 } 1760 1761 /* Handle strip argument. */ 1762 strip = 131072; 1763 len = sizeof(*striparg); 1764 striparg = gctl_get_param(req, "strip", &len); 1765 if (striparg != NULL && len == sizeof(*striparg) && 1766 *striparg > 0) { 1767 if (*striparg < sectorsize) { 1768 gctl_error(req, "Strip size too small."); 1769 return (-10); 1770 } 1771 if (*striparg % sectorsize != 0) { 1772 gctl_error(req, "Incorrect strip size."); 1773 return (-11); 1774 } 1775 if (strip > 65535 * sectorsize) { 1776 gctl_error(req, "Strip size too big."); 1777 return (-12); 1778 } 1779 strip = *striparg; 1780 } 1781 1782 /* Round offset up to strip. */ 1783 if (off % strip != 0) { 1784 size -= strip - off % strip; 1785 off += strip - off % strip; 1786 } 1787 1788 /* Handle size argument. */ 1789 len = sizeof(*sizearg); 1790 sizearg = gctl_get_param(req, "size", &len); 1791 if (sizearg != NULL && len == sizeof(*sizearg) && 1792 *sizearg > 0) { 1793 if (*sizearg > size) { 1794 gctl_error(req, "Size too big %lld > %lld.", 1795 (long long)*sizearg, (long long)size); 1796 return (-9); 1797 } 1798 size = *sizearg; 1799 } 1800 1801 /* Round size down to strip or sector. */ 1802 if (level == G_RAID_VOLUME_RL_RAID1) 1803 size -= (size % sectorsize); 1804 else 1805 size -= (size % strip); 1806 if (size <= 0) { 1807 gctl_error(req, "Size too small."); 1808 return (-13); 1809 } 1810 if (size > 0xffffffffllu * sectorsize) { 1811 gctl_error(req, "Size too big."); 1812 return (-14); 1813 } 1814 1815 /* We have all we need, create things: volume, ... */ 1816 vol = g_raid_create_volume(sc, volname, -1); 1817 vol->v_md_data = (void *)(intptr_t)i; 1818 vol->v_raid_level = level; 1819 vol->v_raid_level_qualifier = qual; 1820 vol->v_strip_size = strip; 1821 vol->v_disks_count = numdisks; 1822 if (level == G_RAID_VOLUME_RL_RAID0) 1823 vol->v_mediasize = size * numdisks; 1824 else if (level == G_RAID_VOLUME_RL_RAID1) 1825 vol->v_mediasize = size; 1826 else if (level == G_RAID_VOLUME_RL_RAID5) 1827 vol->v_mediasize = size * (numdisks - 1); 1828 else { /* RAID1E */ 1829 vol->v_mediasize = ((size * numdisks) / strip / 2) * 1830 strip; 1831 } 1832 vol->v_sectorsize = sectorsize; 1833 g_raid_start_volume(vol); 1834 1835 /* , and subdisks. */ 1836 for (i = 0; i < numdisks; i++) { 1837 disk = vol1->v_subdisks[i].sd_disk; 1838 sd = &vol->v_subdisks[i]; 1839 sd->sd_disk = disk; 1840 sd->sd_offset = off; 1841 sd->sd_size = size; 1842 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1843 if (disk->d_state == G_RAID_DISK_S_ACTIVE) { 1844 if (level == G_RAID_VOLUME_RL_RAID5) 1845 g_raid_change_subdisk_state(sd, 1846 G_RAID_SUBDISK_S_UNINITIALIZED); 1847 else 1848 g_raid_change_subdisk_state(sd, 1849 G_RAID_SUBDISK_S_ACTIVE); 1850 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1851 G_RAID_EVENT_SUBDISK); 1852 } 1853 } 1854 1855 /* Write metadata based on created entities. */ 1856 g_raid_md_write_intel(md, NULL, NULL, NULL); 1857 1858 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1859 G_RAID_EVENT_VOLUME); 1860 return (0); 1861 } 1862 if (strcmp(verb, "delete") == 0) { 1863 1864 nodename = gctl_get_asciiparam(req, "arg0"); 1865 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0) 1866 nodename = NULL; 1867 1868 /* Full node destruction. */ 1869 if (*nargs == 1 && nodename != NULL) { 1870 /* Check if some volume is still open. */ 1871 force = gctl_get_paraml(req, "force", sizeof(*force)); 1872 if (force != NULL && *force == 0 && 1873 g_raid_nopens(sc) != 0) { 1874 gctl_error(req, "Some volume is still open."); 1875 return (-4); 1876 } 1877 1878 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1879 if (disk->d_consumer) 1880 intel_meta_erase(disk->d_consumer); 1881 } 1882 g_raid_destroy_node(sc, 0); 1883 return (0); 1884 } 1885 1886 /* Destroy specified volume. If it was last - all node. */ 1887 if (*nargs > 2) { 1888 gctl_error(req, "Invalid number of arguments."); 1889 return (-1); 1890 } 1891 volname = gctl_get_asciiparam(req, 1892 nodename != NULL ? "arg1" : "arg0"); 1893 if (volname == NULL) { 1894 gctl_error(req, "No volume name."); 1895 return (-2); 1896 } 1897 1898 /* Search for volume. */ 1899 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1900 if (strcmp(vol->v_name, volname) == 0) 1901 break; 1902 pp = vol->v_provider; 1903 if (pp == NULL) 1904 continue; 1905 if (strcmp(pp->name, volname) == 0) 1906 break; 1907 if (strncmp(pp->name, "raid/", 5) == 0 && 1908 strcmp(pp->name + 5, volname) == 0) 1909 break; 1910 } 1911 if (vol == NULL) { 1912 i = strtol(volname, &tmp, 10); 1913 if (verb != volname && tmp[0] == 0) { 1914 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1915 if (vol->v_global_id == i) 1916 break; 1917 } 1918 } 1919 } 1920 if (vol == NULL) { 1921 gctl_error(req, "Volume '%s' not found.", volname); 1922 return (-3); 1923 } 1924 1925 /* Check if volume is still open. */ 1926 force = gctl_get_paraml(req, "force", sizeof(*force)); 1927 if (force != NULL && *force == 0 && 1928 vol->v_provider_open != 0) { 1929 gctl_error(req, "Volume is still open."); 1930 return (-4); 1931 } 1932 1933 /* Destroy volume and potentially node. */ 1934 i = 0; 1935 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next) 1936 i++; 1937 if (i >= 2) { 1938 g_raid_destroy_volume(vol); 1939 g_raid_md_write_intel(md, NULL, NULL, NULL); 1940 } else { 1941 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1942 if (disk->d_consumer) 1943 intel_meta_erase(disk->d_consumer); 1944 } 1945 g_raid_destroy_node(sc, 0); 1946 } 1947 return (0); 1948 } 1949 if (strcmp(verb, "remove") == 0 || 1950 strcmp(verb, "fail") == 0) { 1951 if (*nargs < 2) { 1952 gctl_error(req, "Invalid number of arguments."); 1953 return (-1); 1954 } 1955 for (i = 1; i < *nargs; i++) { 1956 snprintf(arg, sizeof(arg), "arg%d", i); 1957 diskname = gctl_get_asciiparam(req, arg); 1958 if (diskname == NULL) { 1959 gctl_error(req, "No disk name (%s).", arg); 1960 error = -2; 1961 break; 1962 } 1963 if (strncmp(diskname, "/dev/", 5) == 0) 1964 diskname += 5; 1965 1966 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1967 if (disk->d_consumer != NULL && 1968 disk->d_consumer->provider != NULL && 1969 strcmp(disk->d_consumer->provider->name, 1970 diskname) == 0) 1971 break; 1972 } 1973 if (disk == NULL) { 1974 gctl_error(req, "Disk '%s' not found.", 1975 diskname); 1976 error = -3; 1977 break; 1978 } 1979 1980 if (strcmp(verb, "fail") == 0) { 1981 g_raid_md_fail_disk_intel(md, NULL, disk); 1982 continue; 1983 } 1984 1985 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 1986 1987 /* Erase metadata on deleting disk. */ 1988 intel_meta_erase(disk->d_consumer); 1989 1990 /* If disk was assigned, just update statuses. */ 1991 if (pd->pd_disk_pos >= 0) { 1992 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1993 g_raid_kill_consumer(sc, disk->d_consumer); 1994 disk->d_consumer = NULL; 1995 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1996 g_raid_change_subdisk_state(sd, 1997 G_RAID_SUBDISK_S_NONE); 1998 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1999 G_RAID_EVENT_SUBDISK); 2000 } 2001 } else { 2002 /* Otherwise -- delete. */ 2003 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 2004 g_raid_destroy_disk(disk); 2005 } 2006 } 2007 2008 /* Write updated metadata to remaining disks. */ 2009 g_raid_md_write_intel(md, NULL, NULL, NULL); 2010 2011 /* Check if anything left except placeholders. */ 2012 if (g_raid_ndisks(sc, -1) == 2013 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 2014 g_raid_destroy_node(sc, 0); 2015 else 2016 g_raid_md_intel_refill(sc); 2017 return (error); 2018 } 2019 if (strcmp(verb, "insert") == 0) { 2020 if (*nargs < 2) { 2021 gctl_error(req, "Invalid number of arguments."); 2022 return (-1); 2023 } 2024 update = 0; 2025 for (i = 1; i < *nargs; i++) { 2026 /* Get disk name. */ 2027 snprintf(arg, sizeof(arg), "arg%d", i); 2028 diskname = gctl_get_asciiparam(req, arg); 2029 if (diskname == NULL) { 2030 gctl_error(req, "No disk name (%s).", arg); 2031 error = -3; 2032 break; 2033 } 2034 2035 /* Try to find provider with specified name. */ 2036 g_topology_lock(); 2037 cp = g_raid_open_consumer(sc, diskname); 2038 if (cp == NULL) { 2039 gctl_error(req, "Can't open disk '%s'.", 2040 diskname); 2041 g_topology_unlock(); 2042 error = -4; 2043 break; 2044 } 2045 pp = cp->provider; 2046 g_topology_unlock(); 2047 2048 /* Read disk serial. */ 2049 error = g_raid_md_get_label(cp, 2050 &serial[0], INTEL_SERIAL_LEN); 2051 if (error != 0) { 2052 gctl_error(req, 2053 "Can't get serial for provider '%s'.", 2054 diskname); 2055 g_raid_kill_consumer(sc, cp); 2056 error = -7; 2057 break; 2058 } 2059 2060 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 2061 pd->pd_disk_pos = -1; 2062 2063 disk = g_raid_create_disk(sc); 2064 disk->d_consumer = cp; 2065 disk->d_md_data = (void *)pd; 2066 cp->private = disk; 2067 2068 g_raid_get_disk_info(disk); 2069 2070 memcpy(&pd->pd_disk_meta.serial[0], &serial[0], 2071 INTEL_SERIAL_LEN); 2072 intel_set_disk_sectors(&pd->pd_disk_meta, 2073 pp->mediasize / pp->sectorsize); 2074 pd->pd_disk_meta.id = 0; 2075 pd->pd_disk_meta.flags = INTEL_F_SPARE; 2076 2077 /* Welcome the "new" disk. */ 2078 update += g_raid_md_intel_start_disk(disk); 2079 if (disk->d_state == G_RAID_DISK_S_SPARE) { 2080 intel_meta_write_spare(cp, &pd->pd_disk_meta); 2081 g_raid_destroy_disk(disk); 2082 } else if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 2083 gctl_error(req, "Disk '%s' doesn't fit.", 2084 diskname); 2085 g_raid_destroy_disk(disk); 2086 error = -8; 2087 break; 2088 } 2089 } 2090 2091 /* Write new metadata if we changed something. */ 2092 if (update) 2093 g_raid_md_write_intel(md, NULL, NULL, NULL); 2094 return (error); 2095 } 2096 return (-100); 2097 } 2098 2099 static int 2100 g_raid_md_write_intel(struct g_raid_md_object *md, struct g_raid_volume *tvol, 2101 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 2102 { 2103 struct g_raid_softc *sc; 2104 struct g_raid_volume *vol; 2105 struct g_raid_subdisk *sd; 2106 struct g_raid_disk *disk; 2107 struct g_raid_md_intel_object *mdi; 2108 struct g_raid_md_intel_perdisk *pd; 2109 struct intel_raid_conf *meta; 2110 struct intel_raid_vol *mvol; 2111 struct intel_raid_map *mmap0, *mmap1; 2112 off_t sectorsize = 512, pos; 2113 const char *version, *cv; 2114 int vi, sdi, numdisks, len, state, stale; 2115 2116 sc = md->mdo_softc; 2117 mdi = (struct g_raid_md_intel_object *)md; 2118 2119 if (sc->sc_stopping == G_RAID_DESTROY_HARD) 2120 return (0); 2121 2122 /* Bump generation. Newly written metadata may differ from previous. */ 2123 mdi->mdio_generation++; 2124 2125 /* Count number of disks. */ 2126 numdisks = 0; 2127 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2128 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2129 if (pd->pd_disk_pos < 0) 2130 continue; 2131 numdisks++; 2132 if (disk->d_state == G_RAID_DISK_S_ACTIVE) { 2133 pd->pd_disk_meta.flags = 2134 INTEL_F_ONLINE | INTEL_F_ASSIGNED; 2135 } else if (disk->d_state == G_RAID_DISK_S_FAILED) { 2136 pd->pd_disk_meta.flags = INTEL_F_FAILED | INTEL_F_ASSIGNED; 2137 } else { 2138 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED; 2139 if (pd->pd_disk_meta.id != 0xffffffff) { 2140 pd->pd_disk_meta.id = 0xffffffff; 2141 len = strlen(pd->pd_disk_meta.serial); 2142 len = min(len, INTEL_SERIAL_LEN - 3); 2143 strcpy(pd->pd_disk_meta.serial + len, ":0"); 2144 } 2145 } 2146 } 2147 2148 /* Fill anchor and disks. */ 2149 meta = malloc(INTEL_MAX_MD_SIZE(numdisks), 2150 M_MD_INTEL, M_WAITOK | M_ZERO); 2151 memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1); 2152 meta->config_size = INTEL_MAX_MD_SIZE(numdisks); 2153 meta->config_id = mdi->mdio_config_id; 2154 meta->generation = mdi->mdio_generation; 2155 meta->attributes = INTEL_ATTR_CHECKSUM; 2156 meta->total_disks = numdisks; 2157 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2158 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2159 if (pd->pd_disk_pos < 0) 2160 continue; 2161 meta->disk[pd->pd_disk_pos] = pd->pd_disk_meta; 2162 } 2163 2164 /* Fill volumes and maps. */ 2165 vi = 0; 2166 version = INTEL_VERSION_1000; 2167 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 2168 if (vol->v_stopping) 2169 continue; 2170 mvol = intel_get_volume(meta, vi); 2171 2172 /* New metadata may have different volumes order. */ 2173 vol->v_md_data = (void *)(intptr_t)vi; 2174 2175 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2176 sd = &vol->v_subdisks[sdi]; 2177 if (sd->sd_disk != NULL) 2178 break; 2179 } 2180 if (sdi >= vol->v_disks_count) 2181 panic("No any filled subdisk in volume"); 2182 if (vol->v_mediasize >= 0x20000000000llu) 2183 meta->attributes |= INTEL_ATTR_2TB; 2184 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) 2185 meta->attributes |= INTEL_ATTR_RAID0; 2186 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2187 meta->attributes |= INTEL_ATTR_RAID1; 2188 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 2189 meta->attributes |= INTEL_ATTR_RAID5; 2190 else 2191 meta->attributes |= INTEL_ATTR_RAID10; 2192 2193 if (meta->attributes & INTEL_ATTR_2TB) 2194 cv = INTEL_VERSION_1300; 2195 // else if (dev->status == DEV_CLONE_N_GO) 2196 // cv = INTEL_VERSION_1206; 2197 else if (vol->v_disks_count > 4) 2198 cv = INTEL_VERSION_1204; 2199 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 2200 cv = INTEL_VERSION_1202; 2201 else if (vol->v_disks_count > 2) 2202 cv = INTEL_VERSION_1201; 2203 else if (vi > 0) 2204 cv = INTEL_VERSION_1200; 2205 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2206 cv = INTEL_VERSION_1100; 2207 else 2208 cv = INTEL_VERSION_1000; 2209 if (strcmp(cv, version) > 0) 2210 version = cv; 2211 2212 strlcpy(&mvol->name[0], vol->v_name, sizeof(mvol->name)); 2213 mvol->total_sectors = vol->v_mediasize / sectorsize; 2214 2215 /* Check for any recovery in progress. */ 2216 state = G_RAID_SUBDISK_S_ACTIVE; 2217 pos = 0x7fffffffffffffffllu; 2218 stale = 0; 2219 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2220 sd = &vol->v_subdisks[sdi]; 2221 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) 2222 state = G_RAID_SUBDISK_S_REBUILD; 2223 else if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC && 2224 state != G_RAID_SUBDISK_S_REBUILD) 2225 state = G_RAID_SUBDISK_S_RESYNC; 2226 else if (sd->sd_state == G_RAID_SUBDISK_S_STALE) 2227 stale = 1; 2228 if ((sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2229 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) && 2230 sd->sd_rebuild_pos < pos) 2231 pos = sd->sd_rebuild_pos; 2232 } 2233 if (state == G_RAID_SUBDISK_S_REBUILD) { 2234 mvol->migr_state = 1; 2235 mvol->migr_type = INTEL_MT_REBUILD; 2236 } else if (state == G_RAID_SUBDISK_S_RESYNC) { 2237 mvol->migr_state = 1; 2238 /* mvol->migr_type = INTEL_MT_REPAIR; */ 2239 mvol->migr_type = INTEL_MT_VERIFY; 2240 mvol->state |= INTEL_ST_VERIFY_AND_FIX; 2241 } else 2242 mvol->migr_state = 0; 2243 mvol->dirty = (vol->v_dirty || stale); 2244 2245 mmap0 = intel_get_map(mvol, 0); 2246 2247 /* Write map / common part of two maps. */ 2248 intel_set_map_offset(mmap0, sd->sd_offset / sectorsize); 2249 intel_set_map_disk_sectors(mmap0, sd->sd_size / sectorsize); 2250 mmap0->strip_sectors = vol->v_strip_size / sectorsize; 2251 if (vol->v_state == G_RAID_VOLUME_S_BROKEN) 2252 mmap0->status = INTEL_S_FAILURE; 2253 else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED) 2254 mmap0->status = INTEL_S_DEGRADED; 2255 else if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED) 2256 == g_raid_nsubdisks(vol, -1)) 2257 mmap0->status = INTEL_S_UNINITIALIZED; 2258 else 2259 mmap0->status = INTEL_S_READY; 2260 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) 2261 mmap0->type = INTEL_T_RAID0; 2262 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 2263 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 2264 mmap0->type = INTEL_T_RAID1; 2265 else 2266 mmap0->type = INTEL_T_RAID5; 2267 mmap0->total_disks = vol->v_disks_count; 2268 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2269 mmap0->total_domains = vol->v_disks_count; 2270 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 2271 mmap0->total_domains = 2; 2272 else 2273 mmap0->total_domains = 1; 2274 intel_set_map_stripe_count(mmap0, 2275 sd->sd_size / vol->v_strip_size / mmap0->total_domains); 2276 mmap0->failed_disk_num = 0xff; 2277 mmap0->ddf = 1; 2278 2279 /* If there are two maps - copy common and update. */ 2280 if (mvol->migr_state) { 2281 intel_set_vol_curr_migr_unit(mvol, 2282 pos / vol->v_strip_size / mmap0->total_domains); 2283 mmap1 = intel_get_map(mvol, 1); 2284 memcpy(mmap1, mmap0, sizeof(struct intel_raid_map)); 2285 mmap0->status = INTEL_S_READY; 2286 } else 2287 mmap1 = NULL; 2288 2289 /* Write disk indexes and put rebuild flags. */ 2290 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2291 sd = &vol->v_subdisks[sdi]; 2292 pd = (struct g_raid_md_intel_perdisk *) 2293 sd->sd_disk->d_md_data; 2294 mmap0->disk_idx[sdi] = pd->pd_disk_pos; 2295 if (mvol->migr_state) 2296 mmap1->disk_idx[sdi] = pd->pd_disk_pos; 2297 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2298 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) { 2299 mmap1->disk_idx[sdi] |= INTEL_DI_RBLD; 2300 } else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE && 2301 sd->sd_state != G_RAID_SUBDISK_S_STALE && 2302 sd->sd_state != G_RAID_SUBDISK_S_UNINITIALIZED) { 2303 mmap0->disk_idx[sdi] |= INTEL_DI_RBLD; 2304 if (mvol->migr_state) 2305 mmap1->disk_idx[sdi] |= INTEL_DI_RBLD; 2306 } 2307 if ((sd->sd_state == G_RAID_SUBDISK_S_NONE || 2308 sd->sd_state == G_RAID_SUBDISK_S_FAILED) && 2309 mmap0->failed_disk_num == 0xff) { 2310 mmap0->failed_disk_num = sdi; 2311 if (mvol->migr_state) 2312 mmap1->failed_disk_num = sdi; 2313 } 2314 } 2315 vi++; 2316 } 2317 meta->total_volumes = vi; 2318 if (strcmp(version, INTEL_VERSION_1300) != 0) 2319 meta->attributes &= INTEL_ATTR_CHECKSUM; 2320 memcpy(&meta->version[0], version, sizeof(INTEL_VERSION_1000) - 1); 2321 2322 /* We are done. Print meta data and store them to disks. */ 2323 g_raid_md_intel_print(meta); 2324 if (mdi->mdio_meta != NULL) 2325 free(mdi->mdio_meta, M_MD_INTEL); 2326 mdi->mdio_meta = meta; 2327 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2328 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2329 if (disk->d_state != G_RAID_DISK_S_ACTIVE) 2330 continue; 2331 if (pd->pd_meta != NULL) { 2332 free(pd->pd_meta, M_MD_INTEL); 2333 pd->pd_meta = NULL; 2334 } 2335 pd->pd_meta = intel_meta_copy(meta); 2336 intel_meta_write(disk->d_consumer, meta); 2337 } 2338 return (0); 2339 } 2340 2341 static int 2342 g_raid_md_fail_disk_intel(struct g_raid_md_object *md, 2343 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 2344 { 2345 struct g_raid_softc *sc; 2346 struct g_raid_md_intel_object *mdi; 2347 struct g_raid_md_intel_perdisk *pd; 2348 struct g_raid_subdisk *sd; 2349 2350 sc = md->mdo_softc; 2351 mdi = (struct g_raid_md_intel_object *)md; 2352 pd = (struct g_raid_md_intel_perdisk *)tdisk->d_md_data; 2353 2354 /* We can't fail disk that is not a part of array now. */ 2355 if (pd->pd_disk_pos < 0) 2356 return (-1); 2357 2358 /* 2359 * Mark disk as failed in metadata and try to write that metadata 2360 * to the disk itself to prevent it's later resurrection as STALE. 2361 */ 2362 mdi->mdio_meta->disk[pd->pd_disk_pos].flags = INTEL_F_FAILED; 2363 pd->pd_disk_meta.flags = INTEL_F_FAILED; 2364 g_raid_md_intel_print(mdi->mdio_meta); 2365 if (tdisk->d_consumer) 2366 intel_meta_write(tdisk->d_consumer, mdi->mdio_meta); 2367 2368 /* Change states. */ 2369 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 2370 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 2371 g_raid_change_subdisk_state(sd, 2372 G_RAID_SUBDISK_S_FAILED); 2373 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 2374 G_RAID_EVENT_SUBDISK); 2375 } 2376 2377 /* Write updated metadata to remaining disks. */ 2378 g_raid_md_write_intel(md, NULL, NULL, tdisk); 2379 2380 /* Check if anything left except placeholders. */ 2381 if (g_raid_ndisks(sc, -1) == 2382 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 2383 g_raid_destroy_node(sc, 0); 2384 else 2385 g_raid_md_intel_refill(sc); 2386 return (0); 2387 } 2388 2389 static int 2390 g_raid_md_free_disk_intel(struct g_raid_md_object *md, 2391 struct g_raid_disk *disk) 2392 { 2393 struct g_raid_md_intel_perdisk *pd; 2394 2395 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2396 if (pd->pd_meta != NULL) { 2397 free(pd->pd_meta, M_MD_INTEL); 2398 pd->pd_meta = NULL; 2399 } 2400 free(pd, M_MD_INTEL); 2401 disk->d_md_data = NULL; 2402 return (0); 2403 } 2404 2405 static int 2406 g_raid_md_free_intel(struct g_raid_md_object *md) 2407 { 2408 struct g_raid_md_intel_object *mdi; 2409 2410 mdi = (struct g_raid_md_intel_object *)md; 2411 if (!mdi->mdio_started) { 2412 mdi->mdio_started = 0; 2413 callout_stop(&mdi->mdio_start_co); 2414 G_RAID_DEBUG1(1, md->mdo_softc, 2415 "root_mount_rel %p", mdi->mdio_rootmount); 2416 root_mount_rel(mdi->mdio_rootmount); 2417 mdi->mdio_rootmount = NULL; 2418 } 2419 if (mdi->mdio_meta != NULL) { 2420 free(mdi->mdio_meta, M_MD_INTEL); 2421 mdi->mdio_meta = NULL; 2422 } 2423 return (0); 2424 } 2425 2426 G_RAID_MD_DECLARE(intel, "Intel"); 2427