1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bio.h> 35 #include <sys/endian.h> 36 #include <sys/kernel.h> 37 #include <sys/kobj.h> 38 #include <sys/limits.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/systm.h> 43 #include <sys/taskqueue.h> 44 #include <geom/geom.h> 45 #include "geom/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 u_int64_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 263 static struct intel_raid_map * 264 intel_get_map(struct intel_raid_vol *mvol, int i) 265 { 266 struct intel_raid_map *mmap; 267 268 if (i > (mvol->migr_state ? 1 : 0)) 269 return (NULL); 270 mmap = &mvol->map[0]; 271 for (; i > 0; i--) { 272 mmap = (struct intel_raid_map *) 273 &mmap->disk_idx[mmap->total_disks]; 274 } 275 return ((struct intel_raid_map *)mmap); 276 } 277 278 static struct intel_raid_vol * 279 intel_get_volume(struct intel_raid_conf *meta, int i) 280 { 281 struct intel_raid_vol *mvol; 282 struct intel_raid_map *mmap; 283 284 if (i > 1) 285 return (NULL); 286 mvol = (struct intel_raid_vol *)&meta->disk[meta->total_disks]; 287 for (; i > 0; i--) { 288 mmap = intel_get_map(mvol, mvol->migr_state ? 1 : 0); 289 mvol = (struct intel_raid_vol *) 290 &mmap->disk_idx[mmap->total_disks]; 291 } 292 return (mvol); 293 } 294 295 static off_t 296 intel_get_map_offset(struct intel_raid_map *mmap) 297 { 298 off_t offset = (off_t)mmap->offset_hi << 32; 299 300 offset += mmap->offset; 301 return (offset); 302 } 303 304 static void 305 intel_set_map_offset(struct intel_raid_map *mmap, off_t offset) 306 { 307 308 mmap->offset = offset & 0xffffffff; 309 mmap->offset_hi = offset >> 32; 310 } 311 312 static off_t 313 intel_get_map_disk_sectors(struct intel_raid_map *mmap) 314 { 315 off_t disk_sectors = (off_t)mmap->disk_sectors_hi << 32; 316 317 disk_sectors += mmap->disk_sectors; 318 return (disk_sectors); 319 } 320 321 static void 322 intel_set_map_disk_sectors(struct intel_raid_map *mmap, off_t disk_sectors) 323 { 324 325 mmap->disk_sectors = disk_sectors & 0xffffffff; 326 mmap->disk_sectors_hi = disk_sectors >> 32; 327 } 328 329 static void 330 intel_set_map_stripe_count(struct intel_raid_map *mmap, off_t stripe_count) 331 { 332 333 mmap->stripe_count = stripe_count & 0xffffffff; 334 mmap->stripe_count_hi = stripe_count >> 32; 335 } 336 337 static off_t 338 intel_get_disk_sectors(struct intel_raid_disk *disk) 339 { 340 off_t sectors = (off_t)disk->sectors_hi << 32; 341 342 sectors += disk->sectors; 343 return (sectors); 344 } 345 346 static void 347 intel_set_disk_sectors(struct intel_raid_disk *disk, off_t sectors) 348 { 349 350 disk->sectors = sectors & 0xffffffff; 351 disk->sectors_hi = sectors >> 32; 352 } 353 354 static off_t 355 intel_get_vol_curr_migr_unit(struct intel_raid_vol *vol) 356 { 357 off_t curr_migr_unit = (off_t)vol->curr_migr_unit_hi << 32; 358 359 curr_migr_unit += vol->curr_migr_unit; 360 return (curr_migr_unit); 361 } 362 363 static void 364 intel_set_vol_curr_migr_unit(struct intel_raid_vol *vol, off_t curr_migr_unit) 365 { 366 367 vol->curr_migr_unit = curr_migr_unit & 0xffffffff; 368 vol->curr_migr_unit_hi = curr_migr_unit >> 32; 369 } 370 371 static char * 372 intel_status2str(int status) 373 { 374 375 switch (status) { 376 case INTEL_S_READY: 377 return ("READY"); 378 case INTEL_S_UNINITIALIZED: 379 return ("UNINITIALIZED"); 380 case INTEL_S_DEGRADED: 381 return ("DEGRADED"); 382 case INTEL_S_FAILURE: 383 return ("FAILURE"); 384 default: 385 return ("UNKNOWN"); 386 } 387 } 388 389 static char * 390 intel_type2str(int type) 391 { 392 393 switch (type) { 394 case INTEL_T_RAID0: 395 return ("RAID0"); 396 case INTEL_T_RAID1: 397 return ("RAID1"); 398 case INTEL_T_RAID5: 399 return ("RAID5"); 400 default: 401 return ("UNKNOWN"); 402 } 403 } 404 405 static char * 406 intel_cngst2str(int cng_state) 407 { 408 409 switch (cng_state) { 410 case INTEL_CNGST_UPDATED: 411 return ("UPDATED"); 412 case INTEL_CNGST_NEEDS_UPDATE: 413 return ("NEEDS_UPDATE"); 414 case INTEL_CNGST_MASTER_MISSING: 415 return ("MASTER_MISSING"); 416 default: 417 return ("UNKNOWN"); 418 } 419 } 420 421 static char * 422 intel_mt2str(int type) 423 { 424 425 switch (type) { 426 case INTEL_MT_INIT: 427 return ("INIT"); 428 case INTEL_MT_REBUILD: 429 return ("REBUILD"); 430 case INTEL_MT_VERIFY: 431 return ("VERIFY"); 432 case INTEL_MT_GEN_MIGR: 433 return ("GEN_MIGR"); 434 case INTEL_MT_STATE_CHANGE: 435 return ("STATE_CHANGE"); 436 case INTEL_MT_REPAIR: 437 return ("REPAIR"); 438 default: 439 return ("UNKNOWN"); 440 } 441 } 442 443 static void 444 g_raid_md_intel_print(struct intel_raid_conf *meta) 445 { 446 struct intel_raid_vol *mvol; 447 struct intel_raid_map *mmap; 448 int i, j, k; 449 450 if (g_raid_debug < 1) 451 return; 452 453 printf("********* ATA Intel MatrixRAID Metadata *********\n"); 454 printf("intel_id <%.24s>\n", meta->intel_id); 455 printf("version <%.6s>\n", meta->version); 456 printf("checksum 0x%08x\n", meta->checksum); 457 printf("config_size 0x%08x\n", meta->config_size); 458 printf("config_id 0x%08x\n", meta->config_id); 459 printf("generation 0x%08x\n", meta->generation); 460 printf("error_log_size %d\n", meta->error_log_size); 461 printf("attributes 0x%b\n", meta->attributes, 462 "\020" 463 "\001RAID0" 464 "\002RAID1" 465 "\003RAID10" 466 "\004RAID1E" 467 "\005RAID15" 468 "\006RAIDCNG" 469 "\007EXT_STRIP" 470 "\032NVM_CACHE" 471 "\0332TB_DISK" 472 "\034BBM" 473 "\035NVM_CACHE" 474 "\0362TB" 475 "\037PM" 476 "\040CHECKSUM"); 477 printf("total_disks %u\n", meta->total_disks); 478 printf("total_volumes %u\n", meta->total_volumes); 479 printf("error_log_pos %u\n", meta->error_log_pos); 480 printf("cache_size %u\n", meta->cache_size); 481 printf("orig_config_id 0x%08x\n", meta->orig_config_id); 482 printf("pwr_cycle_count %u\n", meta->pwr_cycle_count); 483 printf("bbm_log_size %u\n", meta->bbm_log_size); 484 printf("Flags: S - Spare, A - Assigned, F - Failed, O - Online, D - Disabled\n"); 485 printf("DISK# serial disk_sectors disk_sectors_hi disk_id flags owner\n"); 486 for (i = 0; i < meta->total_disks; i++ ) { 487 printf(" %d <%.16s> %u %u 0x%08x 0x%b %08x\n", i, 488 meta->disk[i].serial, meta->disk[i].sectors, 489 meta->disk[i].sectors_hi, meta->disk[i].id, 490 meta->disk[i].flags, "\20\01S\02A\03F\04O\05D", 491 meta->disk[i].owner_cfg_num); 492 } 493 for (i = 0; i < meta->total_volumes; i++) { 494 mvol = intel_get_volume(meta, i); 495 printf(" ****** Volume %d ******\n", i); 496 printf(" name %.16s\n", mvol->name); 497 printf(" total_sectors %ju\n", mvol->total_sectors); 498 printf(" state 0x%b\n", mvol->state, 499 "\020" 500 "\001BOOTABLE" 501 "\002BOOT_DEVICE" 502 "\003READ_COALESCING" 503 "\004WRITE_COALESCING" 504 "\005LAST_SHUTDOWN_DIRTY" 505 "\006HIDDEN_AT_BOOT" 506 "\007CURRENTLY_HIDDEN" 507 "\010VERIFY_AND_FIX" 508 "\011MAP_STATE_UNINIT" 509 "\012NO_AUTO_RECOVERY" 510 "\013CLONE_N_GO" 511 "\014CLONE_MAN_SYNC" 512 "\015CNG_MASTER_DISK_NUM"); 513 printf(" reserved %u\n", mvol->reserved); 514 printf(" migr_priority %u\n", mvol->migr_priority); 515 printf(" num_sub_vols %u\n", mvol->num_sub_vols); 516 printf(" tid %u\n", mvol->tid); 517 printf(" cng_master_disk %u\n", mvol->cng_master_disk); 518 printf(" cache_policy %u\n", mvol->cache_policy); 519 printf(" cng_state %u (%s)\n", mvol->cng_state, 520 intel_cngst2str(mvol->cng_state)); 521 printf(" cng_sub_state %u\n", mvol->cng_sub_state); 522 printf(" curr_migr_unit %u\n", mvol->curr_migr_unit); 523 printf(" curr_migr_unit_hi %u\n", mvol->curr_migr_unit_hi); 524 printf(" checkpoint_id %u\n", mvol->checkpoint_id); 525 printf(" migr_state %u\n", mvol->migr_state); 526 printf(" migr_type %u (%s)\n", mvol->migr_type, 527 intel_mt2str(mvol->migr_type)); 528 printf(" dirty %u\n", mvol->dirty); 529 printf(" fs_state %u\n", mvol->fs_state); 530 printf(" verify_errors %u\n", mvol->verify_errors); 531 printf(" bad_blocks %u\n", mvol->bad_blocks); 532 533 for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) { 534 printf(" *** Map %d ***\n", j); 535 mmap = intel_get_map(mvol, j); 536 printf(" offset %u\n", mmap->offset); 537 printf(" offset_hi %u\n", mmap->offset_hi); 538 printf(" disk_sectors %u\n", mmap->disk_sectors); 539 printf(" disk_sectors_hi %u\n", mmap->disk_sectors_hi); 540 printf(" stripe_count %u\n", mmap->stripe_count); 541 printf(" stripe_count_hi %u\n", mmap->stripe_count_hi); 542 printf(" strip_sectors %u\n", mmap->strip_sectors); 543 printf(" status %u (%s)\n", mmap->status, 544 intel_status2str(mmap->status)); 545 printf(" type %u (%s)\n", mmap->type, 546 intel_type2str(mmap->type)); 547 printf(" total_disks %u\n", mmap->total_disks); 548 printf(" total_domains %u\n", mmap->total_domains); 549 printf(" failed_disk_num %u\n", mmap->failed_disk_num); 550 printf(" ddf %u\n", mmap->ddf); 551 printf(" disk_idx "); 552 for (k = 0; k < mmap->total_disks; k++) 553 printf(" 0x%08x", mmap->disk_idx[k]); 554 printf("\n"); 555 } 556 } 557 printf("=================================================\n"); 558 } 559 560 static struct intel_raid_conf * 561 intel_meta_copy(struct intel_raid_conf *meta) 562 { 563 struct intel_raid_conf *nmeta; 564 565 nmeta = malloc(meta->config_size, M_MD_INTEL, M_WAITOK); 566 memcpy(nmeta, meta, meta->config_size); 567 return (nmeta); 568 } 569 570 static int 571 intel_meta_find_disk(struct intel_raid_conf *meta, char *serial) 572 { 573 int pos; 574 575 for (pos = 0; pos < meta->total_disks; pos++) { 576 if (strncmp(meta->disk[pos].serial, 577 serial, INTEL_SERIAL_LEN) == 0) 578 return (pos); 579 } 580 return (-1); 581 } 582 583 static struct intel_raid_conf * 584 intel_meta_read(struct g_consumer *cp) 585 { 586 struct g_provider *pp; 587 struct intel_raid_conf *meta; 588 struct intel_raid_vol *mvol; 589 struct intel_raid_map *mmap, *mmap1; 590 char *buf; 591 int error, i, j, k, left, size; 592 uint32_t checksum, *ptr; 593 594 pp = cp->provider; 595 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[24]; 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 1714 if (*nargs < 4) { 1715 gctl_error(req, "Invalid number of arguments."); 1716 return (-1); 1717 } 1718 volname = gctl_get_asciiparam(req, "arg1"); 1719 if (volname == NULL) { 1720 gctl_error(req, "No volume name."); 1721 return (-2); 1722 } 1723 levelname = gctl_get_asciiparam(req, "arg2"); 1724 if (levelname == NULL) { 1725 gctl_error(req, "No RAID level."); 1726 return (-3); 1727 } 1728 if (strcasecmp(levelname, "RAID5") == 0) 1729 levelname = "RAID5-LA"; 1730 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1731 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1732 return (-4); 1733 } 1734 numdisks = *nargs - 3; 1735 force = gctl_get_paraml(req, "force", sizeof(*force)); 1736 if (!g_raid_md_intel_supported(level, qual, numdisks, 1737 force ? *force : 0)) { 1738 gctl_error(req, "Unsupported RAID level " 1739 "(0x%02x/0x%02x), or number of disks (%d).", 1740 level, qual, numdisks); 1741 return (-5); 1742 } 1743 1744 /* Search for disks, connect them and probe. */ 1745 size = 0x7fffffffffffffffllu; 1746 sectorsize = 0; 1747 for (i = 0; i < numdisks; i++) { 1748 snprintf(arg, sizeof(arg), "arg%d", i + 3); 1749 diskname = gctl_get_asciiparam(req, arg); 1750 if (diskname == NULL) { 1751 gctl_error(req, "No disk name (%s).", arg); 1752 error = -6; 1753 break; 1754 } 1755 if (strcmp(diskname, "NONE") == 0) { 1756 cp = NULL; 1757 pp = NULL; 1758 } else { 1759 g_topology_lock(); 1760 cp = g_raid_open_consumer(sc, diskname); 1761 if (cp == NULL) { 1762 gctl_error(req, "Can't open disk '%s'.", 1763 diskname); 1764 g_topology_unlock(); 1765 error = -7; 1766 break; 1767 } 1768 pp = cp->provider; 1769 } 1770 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 1771 pd->pd_disk_pos = i; 1772 disk = g_raid_create_disk(sc); 1773 disk->d_md_data = (void *)pd; 1774 disk->d_consumer = cp; 1775 if (cp == NULL) { 1776 strcpy(&pd->pd_disk_meta.serial[0], "NONE"); 1777 pd->pd_disk_meta.id = 0xffffffff; 1778 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED; 1779 continue; 1780 } 1781 cp->private = disk; 1782 g_topology_unlock(); 1783 1784 error = g_raid_md_get_label(cp, 1785 &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN); 1786 if (error != 0) { 1787 gctl_error(req, 1788 "Can't get serial for provider '%s'.", 1789 diskname); 1790 error = -8; 1791 break; 1792 } 1793 1794 g_raid_get_disk_info(disk); 1795 1796 intel_set_disk_sectors(&pd->pd_disk_meta, 1797 pp->mediasize / pp->sectorsize); 1798 if (size > pp->mediasize) 1799 size = pp->mediasize; 1800 if (sectorsize < pp->sectorsize) 1801 sectorsize = pp->sectorsize; 1802 pd->pd_disk_meta.id = 0; 1803 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED | INTEL_F_ONLINE; 1804 } 1805 if (error != 0) 1806 return (error); 1807 1808 if (sectorsize <= 0) { 1809 gctl_error(req, "Can't get sector size."); 1810 return (-8); 1811 } 1812 1813 /* Reserve some space for metadata. */ 1814 size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize; 1815 1816 /* Handle size argument. */ 1817 len = sizeof(*sizearg); 1818 sizearg = gctl_get_param(req, "size", &len); 1819 if (sizearg != NULL && len == sizeof(*sizearg) && 1820 *sizearg > 0) { 1821 if (*sizearg > size) { 1822 gctl_error(req, "Size too big %lld > %lld.", 1823 (long long)*sizearg, (long long)size); 1824 return (-9); 1825 } 1826 size = *sizearg; 1827 } 1828 1829 /* Handle strip argument. */ 1830 strip = 131072; 1831 len = sizeof(*striparg); 1832 striparg = gctl_get_param(req, "strip", &len); 1833 if (striparg != NULL && len == sizeof(*striparg) && 1834 *striparg > 0) { 1835 if (*striparg < sectorsize) { 1836 gctl_error(req, "Strip size too small."); 1837 return (-10); 1838 } 1839 if (*striparg % sectorsize != 0) { 1840 gctl_error(req, "Incorrect strip size."); 1841 return (-11); 1842 } 1843 if (strip > 65535 * sectorsize) { 1844 gctl_error(req, "Strip size too big."); 1845 return (-12); 1846 } 1847 strip = *striparg; 1848 } 1849 1850 /* Round size down to strip or sector. */ 1851 if (level == G_RAID_VOLUME_RL_RAID1) 1852 size -= (size % sectorsize); 1853 else if (level == G_RAID_VOLUME_RL_RAID1E && 1854 (numdisks & 1) != 0) 1855 size -= (size % (2 * strip)); 1856 else 1857 size -= (size % strip); 1858 if (size <= 0) { 1859 gctl_error(req, "Size too small."); 1860 return (-13); 1861 } 1862 1863 /* We have all we need, create things: volume, ... */ 1864 mdi->mdio_started = 1; 1865 vol = g_raid_create_volume(sc, volname, -1); 1866 pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO); 1867 pv->pv_volume_pos = 0; 1868 vol->v_md_data = pv; 1869 vol->v_raid_level = level; 1870 vol->v_raid_level_qualifier = qual; 1871 vol->v_strip_size = strip; 1872 vol->v_disks_count = numdisks; 1873 if (level == G_RAID_VOLUME_RL_RAID0) 1874 vol->v_mediasize = size * numdisks; 1875 else if (level == G_RAID_VOLUME_RL_RAID1) 1876 vol->v_mediasize = size; 1877 else if (level == G_RAID_VOLUME_RL_RAID5) 1878 vol->v_mediasize = size * (numdisks - 1); 1879 else { /* RAID1E */ 1880 vol->v_mediasize = ((size * numdisks) / strip / 2) * 1881 strip; 1882 } 1883 vol->v_sectorsize = sectorsize; 1884 g_raid_start_volume(vol); 1885 1886 /* , and subdisks. */ 1887 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1888 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 1889 sd = &vol->v_subdisks[pd->pd_disk_pos]; 1890 sd->sd_disk = disk; 1891 sd->sd_offset = 0; 1892 sd->sd_size = size; 1893 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1894 if (sd->sd_disk->d_consumer != NULL) { 1895 g_raid_change_disk_state(disk, 1896 G_RAID_DISK_S_ACTIVE); 1897 if (level == G_RAID_VOLUME_RL_RAID5) 1898 g_raid_change_subdisk_state(sd, 1899 G_RAID_SUBDISK_S_UNINITIALIZED); 1900 else 1901 g_raid_change_subdisk_state(sd, 1902 G_RAID_SUBDISK_S_ACTIVE); 1903 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1904 G_RAID_EVENT_SUBDISK); 1905 } else { 1906 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1907 } 1908 } 1909 1910 /* Write metadata based on created entities. */ 1911 G_RAID_DEBUG1(0, sc, "Array started."); 1912 g_raid_md_write_intel(md, NULL, NULL, NULL); 1913 1914 /* Pickup any STALE/SPARE disks to refill array if needed. */ 1915 g_raid_md_intel_refill(sc); 1916 1917 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1918 G_RAID_EVENT_VOLUME); 1919 return (0); 1920 } 1921 if (strcmp(verb, "add") == 0) { 1922 1923 if (*nargs != 3) { 1924 gctl_error(req, "Invalid number of arguments."); 1925 return (-1); 1926 } 1927 volname = gctl_get_asciiparam(req, "arg1"); 1928 if (volname == NULL) { 1929 gctl_error(req, "No volume name."); 1930 return (-2); 1931 } 1932 levelname = gctl_get_asciiparam(req, "arg2"); 1933 if (levelname == NULL) { 1934 gctl_error(req, "No RAID level."); 1935 return (-3); 1936 } 1937 if (strcasecmp(levelname, "RAID5") == 0) 1938 levelname = "RAID5-LA"; 1939 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1940 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1941 return (-4); 1942 } 1943 1944 /* Look for existing volumes. */ 1945 i = 0; 1946 vol1 = NULL; 1947 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1948 vol1 = vol; 1949 i++; 1950 } 1951 if (i > 1) { 1952 gctl_error(req, "Maximum two volumes supported."); 1953 return (-6); 1954 } 1955 if (vol1 == NULL) { 1956 gctl_error(req, "At least one volume must exist."); 1957 return (-7); 1958 } 1959 1960 numdisks = vol1->v_disks_count; 1961 force = gctl_get_paraml(req, "force", sizeof(*force)); 1962 if (!g_raid_md_intel_supported(level, qual, numdisks, 1963 force ? *force : 0)) { 1964 gctl_error(req, "Unsupported RAID level " 1965 "(0x%02x/0x%02x), or number of disks (%d).", 1966 level, qual, numdisks); 1967 return (-5); 1968 } 1969 1970 /* Collect info about present disks. */ 1971 size = 0x7fffffffffffffffllu; 1972 sectorsize = 512; 1973 for (i = 0; i < numdisks; i++) { 1974 disk = vol1->v_subdisks[i].sd_disk; 1975 pd = (struct g_raid_md_intel_perdisk *) 1976 disk->d_md_data; 1977 disk_sectors = 1978 intel_get_disk_sectors(&pd->pd_disk_meta); 1979 1980 if (disk_sectors * 512 < size) 1981 size = disk_sectors * 512; 1982 if (disk->d_consumer != NULL && 1983 disk->d_consumer->provider != NULL && 1984 disk->d_consumer->provider->sectorsize > 1985 sectorsize) { 1986 sectorsize = 1987 disk->d_consumer->provider->sectorsize; 1988 } 1989 } 1990 1991 /* Reserve some space for metadata. */ 1992 size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize; 1993 1994 /* Decide insert before or after. */ 1995 sd = &vol1->v_subdisks[0]; 1996 if (sd->sd_offset > 1997 size - (sd->sd_offset + sd->sd_size)) { 1998 off = 0; 1999 size = sd->sd_offset; 2000 } else { 2001 off = sd->sd_offset + sd->sd_size; 2002 size = size - (sd->sd_offset + sd->sd_size); 2003 } 2004 2005 /* Handle strip argument. */ 2006 strip = 131072; 2007 len = sizeof(*striparg); 2008 striparg = gctl_get_param(req, "strip", &len); 2009 if (striparg != NULL && len == sizeof(*striparg) && 2010 *striparg > 0) { 2011 if (*striparg < sectorsize) { 2012 gctl_error(req, "Strip size too small."); 2013 return (-10); 2014 } 2015 if (*striparg % sectorsize != 0) { 2016 gctl_error(req, "Incorrect strip size."); 2017 return (-11); 2018 } 2019 if (strip > 65535 * sectorsize) { 2020 gctl_error(req, "Strip size too big."); 2021 return (-12); 2022 } 2023 strip = *striparg; 2024 } 2025 2026 /* Round offset up to strip. */ 2027 if (off % strip != 0) { 2028 size -= strip - off % strip; 2029 off += strip - off % strip; 2030 } 2031 2032 /* Handle size argument. */ 2033 len = sizeof(*sizearg); 2034 sizearg = gctl_get_param(req, "size", &len); 2035 if (sizearg != NULL && len == sizeof(*sizearg) && 2036 *sizearg > 0) { 2037 if (*sizearg > size) { 2038 gctl_error(req, "Size too big %lld > %lld.", 2039 (long long)*sizearg, (long long)size); 2040 return (-9); 2041 } 2042 size = *sizearg; 2043 } 2044 2045 /* Round size down to strip or sector. */ 2046 if (level == G_RAID_VOLUME_RL_RAID1) 2047 size -= (size % sectorsize); 2048 else 2049 size -= (size % strip); 2050 if (size <= 0) { 2051 gctl_error(req, "Size too small."); 2052 return (-13); 2053 } 2054 if (size > 0xffffffffllu * sectorsize) { 2055 gctl_error(req, "Size too big."); 2056 return (-14); 2057 } 2058 2059 /* We have all we need, create things: volume, ... */ 2060 vol = g_raid_create_volume(sc, volname, -1); 2061 pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO); 2062 pv->pv_volume_pos = i; 2063 vol->v_md_data = pv; 2064 vol->v_raid_level = level; 2065 vol->v_raid_level_qualifier = qual; 2066 vol->v_strip_size = strip; 2067 vol->v_disks_count = numdisks; 2068 if (level == G_RAID_VOLUME_RL_RAID0) 2069 vol->v_mediasize = size * numdisks; 2070 else if (level == G_RAID_VOLUME_RL_RAID1) 2071 vol->v_mediasize = size; 2072 else if (level == G_RAID_VOLUME_RL_RAID5) 2073 vol->v_mediasize = size * (numdisks - 1); 2074 else { /* RAID1E */ 2075 vol->v_mediasize = ((size * numdisks) / strip / 2) * 2076 strip; 2077 } 2078 vol->v_sectorsize = sectorsize; 2079 g_raid_start_volume(vol); 2080 2081 /* , and subdisks. */ 2082 for (i = 0; i < numdisks; i++) { 2083 disk = vol1->v_subdisks[i].sd_disk; 2084 sd = &vol->v_subdisks[i]; 2085 sd->sd_disk = disk; 2086 sd->sd_offset = off; 2087 sd->sd_size = size; 2088 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 2089 if (disk->d_state == G_RAID_DISK_S_ACTIVE) { 2090 if (level == G_RAID_VOLUME_RL_RAID5) 2091 g_raid_change_subdisk_state(sd, 2092 G_RAID_SUBDISK_S_UNINITIALIZED); 2093 else 2094 g_raid_change_subdisk_state(sd, 2095 G_RAID_SUBDISK_S_ACTIVE); 2096 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 2097 G_RAID_EVENT_SUBDISK); 2098 } 2099 } 2100 2101 /* Write metadata based on created entities. */ 2102 g_raid_md_write_intel(md, NULL, NULL, NULL); 2103 2104 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 2105 G_RAID_EVENT_VOLUME); 2106 return (0); 2107 } 2108 if (strcmp(verb, "delete") == 0) { 2109 2110 nodename = gctl_get_asciiparam(req, "arg0"); 2111 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0) 2112 nodename = NULL; 2113 2114 /* Full node destruction. */ 2115 if (*nargs == 1 && nodename != NULL) { 2116 /* Check if some volume is still open. */ 2117 force = gctl_get_paraml(req, "force", sizeof(*force)); 2118 if (force != NULL && *force == 0 && 2119 g_raid_nopens(sc) != 0) { 2120 gctl_error(req, "Some volume is still open."); 2121 return (-4); 2122 } 2123 2124 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2125 if (disk->d_consumer) 2126 intel_meta_erase(disk->d_consumer); 2127 } 2128 g_raid_destroy_node(sc, 0); 2129 return (0); 2130 } 2131 2132 /* Destroy specified volume. If it was last - all node. */ 2133 if (*nargs > 2) { 2134 gctl_error(req, "Invalid number of arguments."); 2135 return (-1); 2136 } 2137 volname = gctl_get_asciiparam(req, 2138 nodename != NULL ? "arg1" : "arg0"); 2139 if (volname == NULL) { 2140 gctl_error(req, "No volume name."); 2141 return (-2); 2142 } 2143 2144 /* Search for volume. */ 2145 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 2146 if (strcmp(vol->v_name, volname) == 0) 2147 break; 2148 pp = vol->v_provider; 2149 if (pp == NULL) 2150 continue; 2151 if (strcmp(pp->name, volname) == 0) 2152 break; 2153 if (strncmp(pp->name, "raid/", 5) == 0 && 2154 strcmp(pp->name + 5, volname) == 0) 2155 break; 2156 } 2157 if (vol == NULL) { 2158 i = strtol(volname, &tmp, 10); 2159 if (verb != volname && tmp[0] == 0) { 2160 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 2161 if (vol->v_global_id == i) 2162 break; 2163 } 2164 } 2165 } 2166 if (vol == NULL) { 2167 gctl_error(req, "Volume '%s' not found.", volname); 2168 return (-3); 2169 } 2170 2171 /* Check if volume is still open. */ 2172 force = gctl_get_paraml(req, "force", sizeof(*force)); 2173 if (force != NULL && *force == 0 && 2174 vol->v_provider_open != 0) { 2175 gctl_error(req, "Volume is still open."); 2176 return (-4); 2177 } 2178 2179 /* Destroy volume and potentially node. */ 2180 i = 0; 2181 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next) 2182 i++; 2183 if (i >= 2) { 2184 g_raid_destroy_volume(vol); 2185 g_raid_md_write_intel(md, NULL, NULL, NULL); 2186 } else { 2187 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2188 if (disk->d_consumer) 2189 intel_meta_erase(disk->d_consumer); 2190 } 2191 g_raid_destroy_node(sc, 0); 2192 } 2193 return (0); 2194 } 2195 if (strcmp(verb, "remove") == 0 || 2196 strcmp(verb, "fail") == 0) { 2197 if (*nargs < 2) { 2198 gctl_error(req, "Invalid number of arguments."); 2199 return (-1); 2200 } 2201 for (i = 1; i < *nargs; i++) { 2202 snprintf(arg, sizeof(arg), "arg%d", i); 2203 diskname = gctl_get_asciiparam(req, arg); 2204 if (diskname == NULL) { 2205 gctl_error(req, "No disk name (%s).", arg); 2206 error = -2; 2207 break; 2208 } 2209 if (strncmp(diskname, "/dev/", 5) == 0) 2210 diskname += 5; 2211 2212 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2213 if (disk->d_consumer != NULL && 2214 disk->d_consumer->provider != NULL && 2215 strcmp(disk->d_consumer->provider->name, 2216 diskname) == 0) 2217 break; 2218 } 2219 if (disk == NULL) { 2220 gctl_error(req, "Disk '%s' not found.", 2221 diskname); 2222 error = -3; 2223 break; 2224 } 2225 2226 if (strcmp(verb, "fail") == 0) { 2227 g_raid_md_fail_disk_intel(md, NULL, disk); 2228 continue; 2229 } 2230 2231 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2232 2233 /* Erase metadata on deleting disk. */ 2234 intel_meta_erase(disk->d_consumer); 2235 2236 /* If disk was assigned, just update statuses. */ 2237 if (pd->pd_disk_pos >= 0) { 2238 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 2239 g_raid_kill_consumer(sc, disk->d_consumer); 2240 disk->d_consumer = NULL; 2241 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 2242 g_raid_change_subdisk_state(sd, 2243 G_RAID_SUBDISK_S_NONE); 2244 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 2245 G_RAID_EVENT_SUBDISK); 2246 } 2247 } else { 2248 /* Otherwise -- delete. */ 2249 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 2250 g_raid_destroy_disk(disk); 2251 } 2252 } 2253 2254 /* Write updated metadata to remaining disks. */ 2255 g_raid_md_write_intel(md, NULL, NULL, NULL); 2256 2257 /* Check if anything left except placeholders. */ 2258 if (g_raid_ndisks(sc, -1) == 2259 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 2260 g_raid_destroy_node(sc, 0); 2261 else 2262 g_raid_md_intel_refill(sc); 2263 return (error); 2264 } 2265 if (strcmp(verb, "insert") == 0) { 2266 if (*nargs < 2) { 2267 gctl_error(req, "Invalid number of arguments."); 2268 return (-1); 2269 } 2270 update = 0; 2271 for (i = 1; i < *nargs; i++) { 2272 /* Get disk name. */ 2273 snprintf(arg, sizeof(arg), "arg%d", i); 2274 diskname = gctl_get_asciiparam(req, arg); 2275 if (diskname == NULL) { 2276 gctl_error(req, "No disk name (%s).", arg); 2277 error = -3; 2278 break; 2279 } 2280 2281 /* Try to find provider with specified name. */ 2282 g_topology_lock(); 2283 cp = g_raid_open_consumer(sc, diskname); 2284 if (cp == NULL) { 2285 gctl_error(req, "Can't open disk '%s'.", 2286 diskname); 2287 g_topology_unlock(); 2288 error = -4; 2289 break; 2290 } 2291 pp = cp->provider; 2292 g_topology_unlock(); 2293 2294 /* Read disk serial. */ 2295 error = g_raid_md_get_label(cp, 2296 &serial[0], INTEL_SERIAL_LEN); 2297 if (error != 0) { 2298 gctl_error(req, 2299 "Can't get serial for provider '%s'.", 2300 diskname); 2301 g_raid_kill_consumer(sc, cp); 2302 error = -7; 2303 break; 2304 } 2305 2306 pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO); 2307 pd->pd_disk_pos = -1; 2308 2309 disk = g_raid_create_disk(sc); 2310 disk->d_consumer = cp; 2311 disk->d_md_data = (void *)pd; 2312 cp->private = disk; 2313 2314 g_raid_get_disk_info(disk); 2315 2316 memcpy(&pd->pd_disk_meta.serial[0], &serial[0], 2317 INTEL_SERIAL_LEN); 2318 intel_set_disk_sectors(&pd->pd_disk_meta, 2319 pp->mediasize / pp->sectorsize); 2320 pd->pd_disk_meta.id = 0; 2321 pd->pd_disk_meta.flags = INTEL_F_SPARE; 2322 2323 /* Welcome the "new" disk. */ 2324 update += g_raid_md_intel_start_disk(disk); 2325 if (disk->d_state == G_RAID_DISK_S_SPARE) { 2326 intel_meta_write_spare(cp, &pd->pd_disk_meta); 2327 g_raid_destroy_disk(disk); 2328 } else if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 2329 gctl_error(req, "Disk '%s' doesn't fit.", 2330 diskname); 2331 g_raid_destroy_disk(disk); 2332 error = -8; 2333 break; 2334 } 2335 } 2336 2337 /* Write new metadata if we changed something. */ 2338 if (update) 2339 g_raid_md_write_intel(md, NULL, NULL, NULL); 2340 return (error); 2341 } 2342 return (-100); 2343 } 2344 2345 static int 2346 g_raid_md_write_intel(struct g_raid_md_object *md, struct g_raid_volume *tvol, 2347 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 2348 { 2349 struct g_raid_softc *sc; 2350 struct g_raid_volume *vol; 2351 struct g_raid_subdisk *sd; 2352 struct g_raid_disk *disk; 2353 struct g_raid_md_intel_object *mdi; 2354 struct g_raid_md_intel_pervolume *pv; 2355 struct g_raid_md_intel_perdisk *pd; 2356 struct intel_raid_conf *meta; 2357 struct intel_raid_vol *mvol; 2358 struct intel_raid_map *mmap0, *mmap1; 2359 off_t sectorsize = 512, pos; 2360 const char *version, *cv; 2361 int vi, sdi, numdisks, len, state, stale; 2362 2363 sc = md->mdo_softc; 2364 mdi = (struct g_raid_md_intel_object *)md; 2365 2366 if (sc->sc_stopping == G_RAID_DESTROY_HARD) 2367 return (0); 2368 2369 /* Bump generation. Newly written metadata may differ from previous. */ 2370 mdi->mdio_generation++; 2371 2372 /* Count number of disks. */ 2373 numdisks = 0; 2374 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2375 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2376 if (pd->pd_disk_pos < 0) 2377 continue; 2378 numdisks++; 2379 if (disk->d_state == G_RAID_DISK_S_ACTIVE) { 2380 pd->pd_disk_meta.flags = 2381 INTEL_F_ONLINE | INTEL_F_ASSIGNED; 2382 } else if (disk->d_state == G_RAID_DISK_S_FAILED) { 2383 pd->pd_disk_meta.flags = INTEL_F_FAILED | 2384 INTEL_F_ASSIGNED; 2385 } else if (disk->d_state == G_RAID_DISK_S_DISABLED) { 2386 pd->pd_disk_meta.flags = INTEL_F_FAILED | 2387 INTEL_F_ASSIGNED | INTEL_F_DISABLED; 2388 } else { 2389 if (!(pd->pd_disk_meta.flags & INTEL_F_DISABLED)) 2390 pd->pd_disk_meta.flags = INTEL_F_ASSIGNED; 2391 if (pd->pd_disk_meta.id != 0xffffffff) { 2392 pd->pd_disk_meta.id = 0xffffffff; 2393 len = strlen(pd->pd_disk_meta.serial); 2394 len = min(len, INTEL_SERIAL_LEN - 3); 2395 strcpy(pd->pd_disk_meta.serial + len, ":0"); 2396 } 2397 } 2398 } 2399 2400 /* Fill anchor and disks. */ 2401 meta = malloc(INTEL_MAX_MD_SIZE(numdisks), 2402 M_MD_INTEL, M_WAITOK | M_ZERO); 2403 memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1); 2404 meta->config_size = INTEL_MAX_MD_SIZE(numdisks); 2405 meta->config_id = mdi->mdio_config_id; 2406 meta->orig_config_id = mdi->mdio_orig_config_id; 2407 meta->generation = mdi->mdio_generation; 2408 meta->attributes = INTEL_ATTR_CHECKSUM; 2409 meta->total_disks = numdisks; 2410 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2411 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2412 if (pd->pd_disk_pos < 0) 2413 continue; 2414 meta->disk[pd->pd_disk_pos] = pd->pd_disk_meta; 2415 if (pd->pd_disk_meta.sectors_hi != 0) 2416 meta->attributes |= INTEL_ATTR_2TB_DISK; 2417 } 2418 2419 /* Fill volumes and maps. */ 2420 vi = 0; 2421 version = INTEL_VERSION_1000; 2422 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 2423 pv = vol->v_md_data; 2424 if (vol->v_stopping) 2425 continue; 2426 mvol = intel_get_volume(meta, vi); 2427 2428 /* New metadata may have different volumes order. */ 2429 pv->pv_volume_pos = vi; 2430 2431 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2432 sd = &vol->v_subdisks[sdi]; 2433 if (sd->sd_disk != NULL) 2434 break; 2435 } 2436 if (sdi >= vol->v_disks_count) 2437 panic("No any filled subdisk in volume"); 2438 if (vol->v_mediasize >= 0x20000000000llu) 2439 meta->attributes |= INTEL_ATTR_2TB; 2440 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) 2441 meta->attributes |= INTEL_ATTR_RAID0; 2442 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2443 meta->attributes |= INTEL_ATTR_RAID1; 2444 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 2445 meta->attributes |= INTEL_ATTR_RAID5; 2446 else if ((vol->v_disks_count & 1) == 0) 2447 meta->attributes |= INTEL_ATTR_RAID10; 2448 else 2449 meta->attributes |= INTEL_ATTR_RAID1E; 2450 if (pv->pv_cng) 2451 meta->attributes |= INTEL_ATTR_RAIDCNG; 2452 if (vol->v_strip_size > 131072) 2453 meta->attributes |= INTEL_ATTR_EXT_STRIP; 2454 2455 if (pv->pv_cng) 2456 cv = INTEL_VERSION_1206; 2457 else if (vol->v_disks_count > 4) 2458 cv = INTEL_VERSION_1204; 2459 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 2460 cv = INTEL_VERSION_1202; 2461 else if (vol->v_disks_count > 2) 2462 cv = INTEL_VERSION_1201; 2463 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2464 cv = INTEL_VERSION_1100; 2465 else 2466 cv = INTEL_VERSION_1000; 2467 if (strcmp(cv, version) > 0) 2468 version = cv; 2469 2470 strlcpy(&mvol->name[0], vol->v_name, sizeof(mvol->name)); 2471 mvol->total_sectors = vol->v_mediasize / sectorsize; 2472 mvol->state = (INTEL_ST_READ_COALESCING | 2473 INTEL_ST_WRITE_COALESCING); 2474 mvol->tid = vol->v_global_id + 1; 2475 if (pv->pv_cng) { 2476 mvol->state |= INTEL_ST_CLONE_N_GO; 2477 if (pv->pv_cng_man_sync) 2478 mvol->state |= INTEL_ST_CLONE_MAN_SYNC; 2479 mvol->cng_master_disk = pv->pv_cng_master_disk; 2480 if (vol->v_subdisks[pv->pv_cng_master_disk].sd_state == 2481 G_RAID_SUBDISK_S_NONE) 2482 mvol->cng_state = INTEL_CNGST_MASTER_MISSING; 2483 else if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL) 2484 mvol->cng_state = INTEL_CNGST_NEEDS_UPDATE; 2485 else 2486 mvol->cng_state = INTEL_CNGST_UPDATED; 2487 } 2488 2489 /* Check for any recovery in progress. */ 2490 state = G_RAID_SUBDISK_S_ACTIVE; 2491 pos = 0x7fffffffffffffffllu; 2492 stale = 0; 2493 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2494 sd = &vol->v_subdisks[sdi]; 2495 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) 2496 state = G_RAID_SUBDISK_S_REBUILD; 2497 else if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC && 2498 state != G_RAID_SUBDISK_S_REBUILD) 2499 state = G_RAID_SUBDISK_S_RESYNC; 2500 else if (sd->sd_state == G_RAID_SUBDISK_S_STALE) 2501 stale = 1; 2502 if ((sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2503 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) && 2504 sd->sd_rebuild_pos < pos) 2505 pos = sd->sd_rebuild_pos; 2506 } 2507 if (state == G_RAID_SUBDISK_S_REBUILD) { 2508 mvol->migr_state = 1; 2509 mvol->migr_type = INTEL_MT_REBUILD; 2510 } else if (state == G_RAID_SUBDISK_S_RESYNC) { 2511 mvol->migr_state = 1; 2512 /* mvol->migr_type = INTEL_MT_REPAIR; */ 2513 mvol->migr_type = INTEL_MT_VERIFY; 2514 mvol->state |= INTEL_ST_VERIFY_AND_FIX; 2515 } else 2516 mvol->migr_state = 0; 2517 mvol->dirty = (vol->v_dirty || stale); 2518 2519 mmap0 = intel_get_map(mvol, 0); 2520 2521 /* Write map / common part of two maps. */ 2522 intel_set_map_offset(mmap0, sd->sd_offset / sectorsize); 2523 intel_set_map_disk_sectors(mmap0, sd->sd_size / sectorsize); 2524 mmap0->strip_sectors = vol->v_strip_size / sectorsize; 2525 if (vol->v_state == G_RAID_VOLUME_S_BROKEN) 2526 mmap0->status = INTEL_S_FAILURE; 2527 else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED) 2528 mmap0->status = INTEL_S_DEGRADED; 2529 else if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED) 2530 == g_raid_nsubdisks(vol, -1)) 2531 mmap0->status = INTEL_S_UNINITIALIZED; 2532 else 2533 mmap0->status = INTEL_S_READY; 2534 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) 2535 mmap0->type = INTEL_T_RAID0; 2536 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 2537 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 2538 mmap0->type = INTEL_T_RAID1; 2539 else 2540 mmap0->type = INTEL_T_RAID5; 2541 mmap0->total_disks = vol->v_disks_count; 2542 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 2543 mmap0->total_domains = vol->v_disks_count; 2544 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 2545 mmap0->total_domains = 2; 2546 else 2547 mmap0->total_domains = 1; 2548 intel_set_map_stripe_count(mmap0, 2549 sd->sd_size / vol->v_strip_size / mmap0->total_domains); 2550 mmap0->failed_disk_num = 0xff; 2551 mmap0->ddf = 1; 2552 2553 /* If there are two maps - copy common and update. */ 2554 if (mvol->migr_state) { 2555 intel_set_vol_curr_migr_unit(mvol, 2556 pos / vol->v_strip_size / mmap0->total_domains); 2557 mmap1 = intel_get_map(mvol, 1); 2558 memcpy(mmap1, mmap0, sizeof(struct intel_raid_map)); 2559 mmap0->status = INTEL_S_READY; 2560 } else 2561 mmap1 = NULL; 2562 2563 /* Write disk indexes and put rebuild flags. */ 2564 for (sdi = 0; sdi < vol->v_disks_count; sdi++) { 2565 sd = &vol->v_subdisks[sdi]; 2566 pd = (struct g_raid_md_intel_perdisk *) 2567 sd->sd_disk->d_md_data; 2568 mmap0->disk_idx[sdi] = pd->pd_disk_pos; 2569 if (mvol->migr_state) 2570 mmap1->disk_idx[sdi] = pd->pd_disk_pos; 2571 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2572 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) { 2573 mmap1->disk_idx[sdi] |= INTEL_DI_RBLD; 2574 } else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE && 2575 sd->sd_state != G_RAID_SUBDISK_S_STALE && 2576 sd->sd_state != G_RAID_SUBDISK_S_UNINITIALIZED) { 2577 mmap0->disk_idx[sdi] |= INTEL_DI_RBLD; 2578 if (mvol->migr_state) 2579 mmap1->disk_idx[sdi] |= INTEL_DI_RBLD; 2580 } 2581 if ((sd->sd_state == G_RAID_SUBDISK_S_NONE || 2582 sd->sd_state == G_RAID_SUBDISK_S_FAILED || 2583 sd->sd_state == G_RAID_SUBDISK_S_REBUILD) && 2584 mmap0->failed_disk_num == 0xff) { 2585 mmap0->failed_disk_num = sdi; 2586 if (mvol->migr_state) 2587 mmap1->failed_disk_num = sdi; 2588 } 2589 } 2590 vi++; 2591 } 2592 meta->total_volumes = vi; 2593 if (vi > 1 || meta->attributes & 2594 (INTEL_ATTR_EXT_STRIP | INTEL_ATTR_2TB_DISK | INTEL_ATTR_2TB)) 2595 version = INTEL_VERSION_1300; 2596 if (strcmp(version, INTEL_VERSION_1300) < 0) 2597 meta->attributes &= INTEL_ATTR_CHECKSUM; 2598 memcpy(&meta->version[0], version, sizeof(INTEL_VERSION_1000) - 1); 2599 2600 /* We are done. Print meta data and store them to disks. */ 2601 g_raid_md_intel_print(meta); 2602 if (mdi->mdio_meta != NULL) 2603 free(mdi->mdio_meta, M_MD_INTEL); 2604 mdi->mdio_meta = meta; 2605 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 2606 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2607 if (disk->d_state != G_RAID_DISK_S_ACTIVE) 2608 continue; 2609 if (pd->pd_meta != NULL) { 2610 free(pd->pd_meta, M_MD_INTEL); 2611 pd->pd_meta = NULL; 2612 } 2613 pd->pd_meta = intel_meta_copy(meta); 2614 intel_meta_write(disk->d_consumer, meta); 2615 } 2616 return (0); 2617 } 2618 2619 static int 2620 g_raid_md_fail_disk_intel(struct g_raid_md_object *md, 2621 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 2622 { 2623 struct g_raid_softc *sc; 2624 struct g_raid_md_intel_object *mdi; 2625 struct g_raid_md_intel_perdisk *pd; 2626 struct g_raid_subdisk *sd; 2627 2628 sc = md->mdo_softc; 2629 mdi = (struct g_raid_md_intel_object *)md; 2630 pd = (struct g_raid_md_intel_perdisk *)tdisk->d_md_data; 2631 2632 /* We can't fail disk that is not a part of array now. */ 2633 if (pd->pd_disk_pos < 0) 2634 return (-1); 2635 2636 /* 2637 * Mark disk as failed in metadata and try to write that metadata 2638 * to the disk itself to prevent it's later resurrection as STALE. 2639 */ 2640 mdi->mdio_meta->disk[pd->pd_disk_pos].flags = INTEL_F_FAILED; 2641 pd->pd_disk_meta.flags = INTEL_F_FAILED; 2642 g_raid_md_intel_print(mdi->mdio_meta); 2643 if (tdisk->d_consumer) 2644 intel_meta_write(tdisk->d_consumer, mdi->mdio_meta); 2645 2646 /* Change states. */ 2647 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 2648 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 2649 g_raid_change_subdisk_state(sd, 2650 G_RAID_SUBDISK_S_FAILED); 2651 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 2652 G_RAID_EVENT_SUBDISK); 2653 } 2654 2655 /* Write updated metadata to remaining disks. */ 2656 g_raid_md_write_intel(md, NULL, NULL, tdisk); 2657 2658 /* Check if anything left except placeholders. */ 2659 if (g_raid_ndisks(sc, -1) == 2660 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 2661 g_raid_destroy_node(sc, 0); 2662 else 2663 g_raid_md_intel_refill(sc); 2664 return (0); 2665 } 2666 2667 static int 2668 g_raid_md_free_disk_intel(struct g_raid_md_object *md, 2669 struct g_raid_disk *disk) 2670 { 2671 struct g_raid_md_intel_perdisk *pd; 2672 2673 pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data; 2674 if (pd->pd_meta != NULL) { 2675 free(pd->pd_meta, M_MD_INTEL); 2676 pd->pd_meta = NULL; 2677 } 2678 free(pd, M_MD_INTEL); 2679 disk->d_md_data = NULL; 2680 return (0); 2681 } 2682 2683 static int 2684 g_raid_md_free_volume_intel(struct g_raid_md_object *md, 2685 struct g_raid_volume *vol) 2686 { 2687 struct g_raid_md_intel_pervolume *pv; 2688 2689 pv = (struct g_raid_md_intel_pervolume *)vol->v_md_data; 2690 free(pv, M_MD_INTEL); 2691 vol->v_md_data = NULL; 2692 return (0); 2693 } 2694 2695 static int 2696 g_raid_md_free_intel(struct g_raid_md_object *md) 2697 { 2698 struct g_raid_md_intel_object *mdi; 2699 2700 mdi = (struct g_raid_md_intel_object *)md; 2701 if (!mdi->mdio_started) { 2702 mdi->mdio_started = 0; 2703 callout_stop(&mdi->mdio_start_co); 2704 G_RAID_DEBUG1(1, md->mdo_softc, 2705 "root_mount_rel %p", mdi->mdio_rootmount); 2706 root_mount_rel(mdi->mdio_rootmount); 2707 mdi->mdio_rootmount = NULL; 2708 } 2709 if (mdi->mdio_meta != NULL) { 2710 free(mdi->mdio_meta, M_MD_INTEL); 2711 mdi->mdio_meta = NULL; 2712 } 2713 return (0); 2714 } 2715 2716 G_RAID_MD_DECLARE(intel, "Intel"); 2717