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