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