md_sii.c revision 219974
1219974Smav/*- 2219974Smav * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org> 3219974Smav * All rights reserved. 4219974Smav * 5219974Smav * Redistribution and use in source and binary forms, with or without 6219974Smav * modification, are permitted provided that the following conditions 7219974Smav * are met: 8219974Smav * 1. Redistributions of source code must retain the above copyright 9219974Smav * notice, this list of conditions and the following disclaimer. 10219974Smav * 2. Redistributions in binary form must reproduce the above copyright 11219974Smav * notice, this list of conditions and the following disclaimer in the 12219974Smav * documentation and/or other materials provided with the distribution. 13219974Smav * 14219974Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15219974Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16219974Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17219974Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18219974Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19219974Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20219974Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21219974Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22219974Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23219974Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24219974Smav * SUCH DAMAGE. 25219974Smav */ 26219974Smav 27219974Smav#include <sys/cdefs.h> 28219974Smav__FBSDID("$FreeBSD: head/sys/geom/raid/md_sii.c 219974 2011-03-24 21:31:32Z mav $"); 29219974Smav 30219974Smav#include <sys/param.h> 31219974Smav#include <sys/bio.h> 32219974Smav#include <sys/endian.h> 33219974Smav#include <sys/kernel.h> 34219974Smav#include <sys/kobj.h> 35219974Smav#include <sys/limits.h> 36219974Smav#include <sys/lock.h> 37219974Smav#include <sys/malloc.h> 38219974Smav#include <sys/mutex.h> 39219974Smav#include <sys/systm.h> 40219974Smav#include <sys/taskqueue.h> 41219974Smav#include <geom/geom.h> 42219974Smav#include "geom/raid/g_raid.h" 43219974Smav#include "g_raid_md_if.h" 44219974Smav 45219974Smavstatic MALLOC_DEFINE(M_MD_SII, "md_sii_data", "GEOM_RAID SiI metadata"); 46219974Smav 47219974Smavstruct sii_raid_conf { 48219974Smav uint16_t ata_params_00_53[54]; 49219974Smav uint64_t total_sectors; /* 54 - 57 */ 50219974Smav uint16_t ata_params_58_81[72]; 51219974Smav uint16_t product_id; /* 130 */ 52219974Smav uint16_t vendor_id; /* 131 */ 53219974Smav uint16_t version_minor; /* 132 */ 54219974Smav uint16_t version_major; /* 133 */ 55219974Smav uint8_t timestamp[6]; /* 134 - 136 */ 56219974Smav uint16_t strip_sectors; /* 137 */ 57219974Smav uint16_t dummy_2; 58219974Smav uint8_t disk_number; /* 139 */ 59219974Smav uint8_t type; 60219974Smav#define SII_T_RAID0 0x00 61219974Smav#define SII_T_RAID1 0x01 62219974Smav#define SII_T_RAID01 0x02 63219974Smav#define SII_T_SPARE 0x03 64219974Smav#define SII_T_CONCAT 0x04 65219974Smav#define SII_T_RAID5 0x10 66219974Smav#define SII_T_RESERVED 0xfd 67219974Smav#define SII_T_JBOD 0xff 68219974Smav 69219974Smav uint8_t raid0_disks; /* 140 */ 70219974Smav uint8_t raid0_ident; 71219974Smav uint8_t raid1_disks; /* 141 */ 72219974Smav uint8_t raid1_ident; 73219974Smav uint64_t rebuild_lba; /* 142 - 145 */ 74219974Smav uint32_t generation; /* 146 - 147 */ 75219974Smav uint8_t disk_status; /* 148 */ 76219974Smav#define SII_S_CURRENT 0x01 77219974Smav#define SII_S_REBUILD 0x02 78219974Smav#define SII_S_DROPPED 0x03 79219974Smav#define SII_S_REMOVED 0x04 80219974Smav 81219974Smav uint8_t raid_status; 82219974Smav#define SII_S_ONLINE 0x01 83219974Smav#define SII_S_AVAILABLE 0x02 84219974Smav 85219974Smav uint8_t raid_location; /* 149 */ 86219974Smav uint8_t disk_location; 87219974Smav uint8_t auto_rebuild; /* 150 */ 88219974Smav#define SII_R_REBUILD 0x00 89219974Smav#define SII_R_NOREBUILD 0xff 90219974Smav 91219974Smav uint8_t dummy_3; 92219974Smav uint8_t name[16]; /* 151 - 158 */ 93219974Smav uint16_t checksum; /* 159 */ 94219974Smav uint16_t ata_params_160_255[96]; 95219974Smav} __packed; 96219974Smav 97219974Smavstruct g_raid_md_sii_perdisk { 98219974Smav struct sii_raid_conf *pd_meta; 99219974Smav int pd_disk_pos; 100219974Smav off_t pd_disk_size; 101219974Smav}; 102219974Smav 103219974Smavstruct g_raid_md_sii_object { 104219974Smav struct g_raid_md_object mdio_base; 105219974Smav uint8_t mdio_timestamp[6]; 106219974Smav uint8_t mdio_location; 107219974Smav uint32_t mdio_generation; 108219974Smav struct sii_raid_conf *mdio_meta; 109219974Smav struct callout mdio_start_co; /* STARTING state timer. */ 110219974Smav int mdio_total_disks; 111219974Smav int mdio_disks_present; 112219974Smav int mdio_started; 113219974Smav int mdio_incomplete; 114219974Smav struct root_hold_token *mdio_rootmount; /* Root mount delay token. */ 115219974Smav}; 116219974Smav 117219974Smavstatic g_raid_md_create_t g_raid_md_create_sii; 118219974Smavstatic g_raid_md_taste_t g_raid_md_taste_sii; 119219974Smavstatic g_raid_md_event_t g_raid_md_event_sii; 120219974Smavstatic g_raid_md_ctl_t g_raid_md_ctl_sii; 121219974Smavstatic g_raid_md_write_t g_raid_md_write_sii; 122219974Smavstatic g_raid_md_fail_disk_t g_raid_md_fail_disk_sii; 123219974Smavstatic g_raid_md_free_disk_t g_raid_md_free_disk_sii; 124219974Smavstatic g_raid_md_free_t g_raid_md_free_sii; 125219974Smav 126219974Smavstatic kobj_method_t g_raid_md_sii_methods[] = { 127219974Smav KOBJMETHOD(g_raid_md_create, g_raid_md_create_sii), 128219974Smav KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_sii), 129219974Smav KOBJMETHOD(g_raid_md_event, g_raid_md_event_sii), 130219974Smav KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_sii), 131219974Smav KOBJMETHOD(g_raid_md_write, g_raid_md_write_sii), 132219974Smav KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_sii), 133219974Smav KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_sii), 134219974Smav KOBJMETHOD(g_raid_md_free, g_raid_md_free_sii), 135219974Smav { 0, 0 } 136219974Smav}; 137219974Smav 138219974Smavstatic struct g_raid_md_class g_raid_md_sii_class = { 139219974Smav "SiI", 140219974Smav g_raid_md_sii_methods, 141219974Smav sizeof(struct g_raid_md_sii_object), 142219974Smav .mdc_priority = 100 143219974Smav}; 144219974Smav 145219974Smavstatic void 146219974Smavg_raid_md_sii_print(struct sii_raid_conf *meta) 147219974Smav{ 148219974Smav 149219974Smav if (g_raid_debug < 1) 150219974Smav return; 151219974Smav 152219974Smav printf("********* ATA SiI RAID Metadata *********\n"); 153219974Smav printf("total_sectors %llu\n", 154219974Smav (long long unsigned)meta->total_sectors); 155219974Smav printf("product_id 0x%04x\n", meta->product_id); 156219974Smav printf("vendor_id 0x%04x\n", meta->vendor_id); 157219974Smav printf("version_minor 0x%04x\n", meta->version_minor); 158219974Smav printf("version_major 0x%04x\n", meta->version_major); 159219974Smav printf("timestamp 0x%02x%02x%02x%02x%02x%02x\n", 160219974Smav meta->timestamp[5], meta->timestamp[4], meta->timestamp[3], 161219974Smav meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]); 162219974Smav printf("strip_sectors %d\n", meta->strip_sectors); 163219974Smav printf("disk_number %d\n", meta->disk_number); 164219974Smav printf("type 0x%02x\n", meta->type); 165219974Smav printf("raid0_disks %d\n", meta->raid0_disks); 166219974Smav printf("raid0_ident %d\n", meta->raid0_ident); 167219974Smav printf("raid1_disks %d\n", meta->raid1_disks); 168219974Smav printf("raid1_ident %d\n", meta->raid1_ident); 169219974Smav printf("rebuild_lba %llu\n", 170219974Smav (long long unsigned)meta->rebuild_lba); 171219974Smav printf("generation %d\n", meta->generation); 172219974Smav printf("disk_status %d\n", meta->disk_status); 173219974Smav printf("raid_status %d\n", meta->raid_status); 174219974Smav printf("raid_location %d\n", meta->raid_location); 175219974Smav printf("disk_location %d\n", meta->disk_location); 176219974Smav printf("auto_rebuild %d\n", meta->auto_rebuild); 177219974Smav printf("name <%.16s>\n", meta->name); 178219974Smav printf("checksum 0x%04x\n", meta->checksum); 179219974Smav printf("=================================================\n"); 180219974Smav} 181219974Smav 182219974Smavstatic struct sii_raid_conf * 183219974Smavsii_meta_copy(struct sii_raid_conf *meta) 184219974Smav{ 185219974Smav struct sii_raid_conf *nmeta; 186219974Smav 187219974Smav nmeta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK); 188219974Smav memcpy(nmeta, meta, sizeof(*meta)); 189219974Smav return (nmeta); 190219974Smav} 191219974Smav 192219974Smavstatic int 193219974Smavsii_meta_total_disks(struct sii_raid_conf *meta) 194219974Smav{ 195219974Smav 196219974Smav switch (meta->type) { 197219974Smav case SII_T_RAID0: 198219974Smav case SII_T_RAID5: 199219974Smav case SII_T_CONCAT: 200219974Smav return (meta->raid0_disks); 201219974Smav case SII_T_RAID1: 202219974Smav return (meta->raid1_disks); 203219974Smav case SII_T_RAID01: 204219974Smav return (meta->raid0_disks * meta->raid1_disks); 205219974Smav case SII_T_SPARE: 206219974Smav case SII_T_JBOD: 207219974Smav return (1); 208219974Smav } 209219974Smav return (0); 210219974Smav} 211219974Smav 212219974Smavstatic int 213219974Smavsii_meta_disk_pos(struct sii_raid_conf *meta, struct sii_raid_conf *pdmeta) 214219974Smav{ 215219974Smav 216219974Smav if (pdmeta->type == SII_T_SPARE) 217219974Smav return (-3); 218219974Smav 219219974Smav if (memcmp(&meta->timestamp, &pdmeta->timestamp, 6) != 0) 220219974Smav return (-1); 221219974Smav 222219974Smav switch (pdmeta->type) { 223219974Smav case SII_T_RAID0: 224219974Smav case SII_T_RAID1: 225219974Smav case SII_T_RAID5: 226219974Smav case SII_T_CONCAT: 227219974Smav return (pdmeta->disk_number); 228219974Smav case SII_T_RAID01: 229219974Smav return (pdmeta->raid1_ident * pdmeta->raid1_disks + 230219974Smav pdmeta->raid0_ident); 231219974Smav case SII_T_JBOD: 232219974Smav return (0); 233219974Smav } 234219974Smav return (-1); 235219974Smav} 236219974Smav 237219974Smavstatic void 238219974Smavsii_meta_get_name(struct sii_raid_conf *meta, char *buf) 239219974Smav{ 240219974Smav int i; 241219974Smav 242219974Smav strncpy(buf, meta->name, 16); 243219974Smav buf[16] = 0; 244219974Smav for (i = 15; i >= 0; i--) { 245219974Smav if (buf[i] > 0x20) 246219974Smav break; 247219974Smav buf[i] = 0; 248219974Smav } 249219974Smav} 250219974Smav 251219974Smavstatic void 252219974Smavsii_meta_put_name(struct sii_raid_conf *meta, char *buf) 253219974Smav{ 254219974Smav 255219974Smav memset(meta->name, 0x20, 16); 256219974Smav memcpy(meta->name, buf, MIN(strlen(buf), 16)); 257219974Smav} 258219974Smav 259219974Smavstatic struct sii_raid_conf * 260219974Smavsii_meta_read(struct g_consumer *cp) 261219974Smav{ 262219974Smav struct g_provider *pp; 263219974Smav struct sii_raid_conf *meta; 264219974Smav char *buf; 265219974Smav int error, i; 266219974Smav uint16_t checksum, *ptr; 267219974Smav 268219974Smav pp = cp->provider; 269219974Smav 270219974Smav /* Read the anchor sector. */ 271219974Smav buf = g_read_data(cp, 272219974Smav pp->mediasize - pp->sectorsize, pp->sectorsize, &error); 273219974Smav if (buf == NULL) { 274219974Smav G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 275219974Smav pp->name, error); 276219974Smav return (NULL); 277219974Smav } 278219974Smav meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK); 279219974Smav memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize)); 280219974Smav g_free(buf); 281219974Smav 282219974Smav /* Check vendor ID. */ 283219974Smav if (meta->vendor_id != 0x1095) { 284219974Smav G_RAID_DEBUG(1, "SiI vendor ID check failed on %s (0x%04x)", 285219974Smav pp->name, meta->vendor_id); 286219974Smav free(meta, M_MD_SII); 287219974Smav return (NULL); 288219974Smav } 289219974Smav 290219974Smav /* Check metadata major version. */ 291219974Smav if (meta->version_major != 2) { 292219974Smav G_RAID_DEBUG(1, "SiI version check failed on %s (%d.%d)", 293219974Smav pp->name, meta->version_major, meta->version_minor); 294219974Smav free(meta, M_MD_SII); 295219974Smav return (NULL); 296219974Smav } 297219974Smav 298219974Smav /* Check metadata checksum. */ 299219974Smav for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i <= 159; i++) 300219974Smav checksum += *ptr++; 301219974Smav if (checksum != 0) { 302219974Smav G_RAID_DEBUG(1, "SiI checksum check failed on %s", pp->name); 303219974Smav free(meta, M_MD_SII); 304219974Smav return (NULL); 305219974Smav } 306219974Smav 307219974Smav /* Check raid type. */ 308219974Smav if (meta->type != SII_T_RAID0 && meta->type != SII_T_RAID1 && 309219974Smav meta->type != SII_T_RAID01 && meta->type != SII_T_SPARE && 310219974Smav meta->type != SII_T_RAID5 && meta->type != SII_T_CONCAT && 311219974Smav meta->type != SII_T_JBOD) { 312219974Smav G_RAID_DEBUG(1, "SiI unknown RAID level on %s (0x%02x)", 313219974Smav pp->name, meta->type); 314219974Smav free(meta, M_MD_SII); 315219974Smav return (NULL); 316219974Smav } 317219974Smav 318219974Smav return (meta); 319219974Smav} 320219974Smav 321219974Smavstatic int 322219974Smavsii_meta_write(struct g_consumer *cp, struct sii_raid_conf *meta) 323219974Smav{ 324219974Smav struct g_provider *pp; 325219974Smav char *buf; 326219974Smav int error, i; 327219974Smav uint16_t checksum, *ptr; 328219974Smav 329219974Smav pp = cp->provider; 330219974Smav 331219974Smav /* Recalculate checksum for case if metadata were changed. */ 332219974Smav meta->checksum = 0; 333219974Smav for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 159; i++) 334219974Smav checksum += *ptr++; 335219974Smav meta->checksum -= checksum; 336219974Smav 337219974Smav /* Create and fill buffer. */ 338219974Smav buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO); 339219974Smav memcpy(buf, meta, sizeof(*meta)); 340219974Smav 341219974Smav /* Write 4 copies of metadata. */ 342219974Smav for (i = 0; i < 4; i++) { 343219974Smav error = g_write_data(cp, 344219974Smav pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)), 345219974Smav buf, pp->sectorsize); 346219974Smav if (error != 0) { 347219974Smav G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 348219974Smav pp->name, error); 349219974Smav break; 350219974Smav } 351219974Smav } 352219974Smav 353219974Smav free(buf, M_MD_SII); 354219974Smav return (error); 355219974Smav} 356219974Smav 357219974Smavstatic int 358219974Smavsii_meta_erase(struct g_consumer *cp) 359219974Smav{ 360219974Smav struct g_provider *pp; 361219974Smav char *buf; 362219974Smav int error, i; 363219974Smav 364219974Smav pp = cp->provider; 365219974Smav buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO); 366219974Smav /* Write 4 copies of metadata. */ 367219974Smav for (i = 0; i < 4; i++) { 368219974Smav error = g_write_data(cp, 369219974Smav pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)), 370219974Smav buf, pp->sectorsize); 371219974Smav if (error != 0) { 372219974Smav G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 373219974Smav pp->name, error); 374219974Smav } 375219974Smav } 376219974Smav free(buf, M_MD_SII); 377219974Smav return (error); 378219974Smav} 379219974Smav 380219974Smavstatic int 381219974Smavsii_meta_write_spare(struct g_consumer *cp) 382219974Smav{ 383219974Smav struct sii_raid_conf *meta; 384219974Smav int error; 385219974Smav 386219974Smav meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO); 387219974Smav meta->total_sectors = cp->provider->mediasize / 388219974Smav cp->provider->sectorsize - 0x800; 389219974Smav meta->vendor_id = 0x1095; 390219974Smav meta->version_minor = 0; 391219974Smav meta->version_major = 2; 392219974Smav meta->timestamp[0] = arc4random(); 393219974Smav meta->timestamp[1] = arc4random(); 394219974Smav meta->timestamp[2] = arc4random(); 395219974Smav meta->timestamp[3] = arc4random(); 396219974Smav meta->timestamp[4] = arc4random(); 397219974Smav meta->timestamp[5] = arc4random(); 398219974Smav meta->type = SII_T_SPARE; 399219974Smav meta->generation = 1; 400219974Smav meta->raid1_ident = 0xff; 401219974Smav meta->raid_location = arc4random(); 402219974Smav error = sii_meta_write(cp, meta); 403219974Smav free(meta, M_MD_SII); 404219974Smav return (error); 405219974Smav} 406219974Smav 407219974Smavstatic struct g_raid_disk * 408219974Smavg_raid_md_sii_get_disk(struct g_raid_softc *sc, int id) 409219974Smav{ 410219974Smav struct g_raid_disk *disk; 411219974Smav struct g_raid_md_sii_perdisk *pd; 412219974Smav 413219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 414219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 415219974Smav if (pd->pd_disk_pos == id) 416219974Smav break; 417219974Smav } 418219974Smav return (disk); 419219974Smav} 420219974Smav 421219974Smavstatic int 422219974Smavg_raid_md_sii_supported(int level, int qual, int disks, int force) 423219974Smav{ 424219974Smav 425219974Smav if (disks > 8) 426219974Smav return (0); 427219974Smav switch (level) { 428219974Smav case G_RAID_VOLUME_RL_RAID0: 429219974Smav if (disks < 1) 430219974Smav return (0); 431219974Smav if (!force && (disks < 2 || disks > 6)) 432219974Smav return (0); 433219974Smav break; 434219974Smav case G_RAID_VOLUME_RL_RAID1: 435219974Smav if (disks < 1) 436219974Smav return (0); 437219974Smav if (!force && (disks != 2)) 438219974Smav return (0); 439219974Smav break; 440219974Smav case G_RAID_VOLUME_RL_RAID1E: 441219974Smav if (disks < 2) 442219974Smav return (0); 443219974Smav if (disks % 2 != 0) 444219974Smav return (0); 445219974Smav if (!force && (disks < 4)) 446219974Smav return (0); 447219974Smav break; 448219974Smav case G_RAID_VOLUME_RL_SINGLE: 449219974Smav if (disks != 1) 450219974Smav return (0); 451219974Smav break; 452219974Smav case G_RAID_VOLUME_RL_CONCAT: 453219974Smav if (disks < 2) 454219974Smav return (0); 455219974Smav break; 456219974Smav case G_RAID_VOLUME_RL_RAID5: 457219974Smav if (disks < 3) 458219974Smav return (0); 459219974Smav break; 460219974Smav default: 461219974Smav return (0); 462219974Smav } 463219974Smav if (qual != G_RAID_VOLUME_RLQ_NONE) 464219974Smav return (0); 465219974Smav return (1); 466219974Smav} 467219974Smav 468219974Smavstatic int 469219974Smavg_raid_md_sii_start_disk(struct g_raid_disk *disk) 470219974Smav{ 471219974Smav struct g_raid_softc *sc; 472219974Smav struct g_raid_subdisk *sd, *tmpsd; 473219974Smav struct g_raid_disk *olddisk, *tmpdisk; 474219974Smav struct g_raid_md_object *md; 475219974Smav struct g_raid_md_sii_object *mdi; 476219974Smav struct g_raid_md_sii_perdisk *pd, *oldpd; 477219974Smav struct sii_raid_conf *meta; 478219974Smav int disk_pos, resurrection = 0; 479219974Smav 480219974Smav sc = disk->d_softc; 481219974Smav md = sc->sc_md; 482219974Smav mdi = (struct g_raid_md_sii_object *)md; 483219974Smav meta = mdi->mdio_meta; 484219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 485219974Smav olddisk = NULL; 486219974Smav 487219974Smav /* Find disk position in metadata by it's serial. */ 488219974Smav if (pd->pd_meta != NULL) 489219974Smav disk_pos = sii_meta_disk_pos(meta, pd->pd_meta); 490219974Smav else 491219974Smav disk_pos = -3; 492219974Smav if (disk_pos < 0) { 493219974Smav G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk"); 494219974Smav /* If we are in the start process, that's all for now. */ 495219974Smav if (!mdi->mdio_started) 496219974Smav goto nofit; 497219974Smav /* 498219974Smav * If we have already started - try to get use of the disk. 499219974Smav * Try to replace OFFLINE disks first, then FAILED. 500219974Smav */ 501219974Smav TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) { 502219974Smav if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE && 503219974Smav tmpdisk->d_state != G_RAID_DISK_S_FAILED) 504219974Smav continue; 505219974Smav /* Make sure this disk is big enough. */ 506219974Smav TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) { 507219974Smav if (sd->sd_offset + sd->sd_size + 512 > 508219974Smav pd->pd_disk_size) { 509219974Smav G_RAID_DEBUG1(1, sc, 510219974Smav "Disk too small (%ju < %ju)", 511219974Smav pd->pd_disk_size, 512219974Smav sd->sd_offset + sd->sd_size + 512); 513219974Smav break; 514219974Smav } 515219974Smav } 516219974Smav if (sd != NULL) 517219974Smav continue; 518219974Smav if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) { 519219974Smav olddisk = tmpdisk; 520219974Smav break; 521219974Smav } else if (olddisk == NULL) 522219974Smav olddisk = tmpdisk; 523219974Smav } 524219974Smav if (olddisk == NULL) { 525219974Smavnofit: 526219974Smav if (disk_pos == -3 || pd->pd_disk_pos == -3) { 527219974Smav g_raid_change_disk_state(disk, 528219974Smav G_RAID_DISK_S_SPARE); 529219974Smav return (1); 530219974Smav } else { 531219974Smav g_raid_change_disk_state(disk, 532219974Smav G_RAID_DISK_S_STALE); 533219974Smav return (0); 534219974Smav } 535219974Smav } 536219974Smav oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data; 537219974Smav disk_pos = oldpd->pd_disk_pos; 538219974Smav resurrection = 1; 539219974Smav } 540219974Smav 541219974Smav if (olddisk == NULL) { 542219974Smav /* Find placeholder by position. */ 543219974Smav olddisk = g_raid_md_sii_get_disk(sc, disk_pos); 544219974Smav if (olddisk == NULL) 545219974Smav panic("No disk at position %d!", disk_pos); 546219974Smav if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) { 547219974Smav G_RAID_DEBUG1(1, sc, "More then one disk for pos %d", 548219974Smav disk_pos); 549219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE); 550219974Smav return (0); 551219974Smav } 552219974Smav oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data; 553219974Smav } 554219974Smav 555219974Smav /* Replace failed disk or placeholder with new disk. */ 556219974Smav TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) { 557219974Smav TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next); 558219974Smav TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 559219974Smav sd->sd_disk = disk; 560219974Smav } 561219974Smav oldpd->pd_disk_pos = -2; 562219974Smav pd->pd_disk_pos = disk_pos; 563219974Smav 564219974Smav /* If it was placeholder -- destroy it. */ 565219974Smav if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) { 566219974Smav g_raid_destroy_disk(olddisk); 567219974Smav } else { 568219974Smav /* Otherwise, make it STALE_FAILED. */ 569219974Smav g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED); 570219974Smav } 571219974Smav 572219974Smav /* Welcome the new disk. */ 573219974Smav if (resurrection) 574219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 575219974Smav else if (pd->pd_meta->disk_status == SII_S_CURRENT || 576219974Smav pd->pd_meta->disk_status == SII_S_REBUILD) 577219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 578219974Smav else 579219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 580219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 581219974Smav 582219974Smav /* 583219974Smav * Different disks may have different sizes, 584219974Smav * in concat mode. Update from real disk size. 585219974Smav */ 586219974Smav if (meta->type == SII_T_CONCAT || meta->type == SII_T_JBOD) 587219974Smav sd->sd_size = pd->pd_disk_size - 0x800 * 512; 588219974Smav 589219974Smav if (resurrection) { 590219974Smav /* New or ex-spare disk. */ 591219974Smav g_raid_change_subdisk_state(sd, 592219974Smav G_RAID_SUBDISK_S_NEW); 593219974Smav } else if (pd->pd_meta->disk_status == SII_S_REBUILD) { 594219974Smav /* Rebuilding disk. */ 595219974Smav g_raid_change_subdisk_state(sd, 596219974Smav G_RAID_SUBDISK_S_REBUILD); 597219974Smav if (pd->pd_meta->generation == meta->generation) 598219974Smav sd->sd_rebuild_pos = pd->pd_meta->rebuild_lba * 512; 599219974Smav else 600219974Smav sd->sd_rebuild_pos = 0; 601219974Smav } else if (pd->pd_meta->disk_status == SII_S_CURRENT) { 602219974Smav if (pd->pd_meta->raid_status == SII_S_ONLINE || 603219974Smav pd->pd_meta->generation != meta->generation) { 604219974Smav /* Dirty or resyncing disk. */ 605219974Smav g_raid_change_subdisk_state(sd, 606219974Smav G_RAID_SUBDISK_S_STALE); 607219974Smav } else { 608219974Smav /* Up to date disk. */ 609219974Smav g_raid_change_subdisk_state(sd, 610219974Smav G_RAID_SUBDISK_S_ACTIVE); 611219974Smav } 612219974Smav } else { 613219974Smav g_raid_change_subdisk_state(sd, 614219974Smav G_RAID_SUBDISK_S_FAILED); 615219974Smav } 616219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 617219974Smav G_RAID_EVENT_SUBDISK); 618219974Smav } 619219974Smav 620219974Smav /* Update status of our need for spare. */ 621219974Smav if (mdi->mdio_started) { 622219974Smav mdi->mdio_incomplete = 623219974Smav (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 624219974Smav mdi->mdio_total_disks); 625219974Smav } 626219974Smav 627219974Smav return (resurrection); 628219974Smav} 629219974Smav 630219974Smavstatic void 631219974Smavg_disk_md_sii_retaste(void *arg, int pending) 632219974Smav{ 633219974Smav 634219974Smav G_RAID_DEBUG(1, "Array is not complete, trying to retaste."); 635219974Smav g_retaste(&g_raid_class); 636219974Smav free(arg, M_MD_SII); 637219974Smav} 638219974Smav 639219974Smavstatic void 640219974Smavg_raid_md_sii_refill(struct g_raid_softc *sc) 641219974Smav{ 642219974Smav struct g_raid_md_object *md; 643219974Smav struct g_raid_md_sii_object *mdi; 644219974Smav struct sii_raid_conf *meta; 645219974Smav struct g_raid_disk *disk; 646219974Smav struct task *task; 647219974Smav int update, na; 648219974Smav 649219974Smav md = sc->sc_md; 650219974Smav mdi = (struct g_raid_md_sii_object *)md; 651219974Smav meta = mdi->mdio_meta; 652219974Smav update = 0; 653219974Smav do { 654219974Smav /* Make sure we miss anything. */ 655219974Smav na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE); 656219974Smav if (na == mdi->mdio_total_disks) 657219974Smav break; 658219974Smav 659219974Smav G_RAID_DEBUG1(1, md->mdo_softc, 660219974Smav "Array is not complete (%d of %d), " 661219974Smav "trying to refill.", na, mdi->mdio_total_disks); 662219974Smav 663219974Smav /* Try to get use some of STALE disks. */ 664219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 665219974Smav if (disk->d_state == G_RAID_DISK_S_STALE) { 666219974Smav update += g_raid_md_sii_start_disk(disk); 667219974Smav if (disk->d_state == G_RAID_DISK_S_ACTIVE) 668219974Smav break; 669219974Smav } 670219974Smav } 671219974Smav if (disk != NULL) 672219974Smav continue; 673219974Smav 674219974Smav /* Try to get use some of SPARE disks. */ 675219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 676219974Smav if (disk->d_state == G_RAID_DISK_S_SPARE) { 677219974Smav update += g_raid_md_sii_start_disk(disk); 678219974Smav if (disk->d_state == G_RAID_DISK_S_ACTIVE) 679219974Smav break; 680219974Smav } 681219974Smav } 682219974Smav } while (disk != NULL); 683219974Smav 684219974Smav /* Write new metadata if we changed something. */ 685219974Smav if (update) { 686219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 687219974Smav meta = mdi->mdio_meta; 688219974Smav } 689219974Smav 690219974Smav /* Update status of our need for spare. */ 691219974Smav mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 692219974Smav mdi->mdio_total_disks); 693219974Smav 694219974Smav /* Request retaste hoping to find spare. */ 695219974Smav if (mdi->mdio_incomplete) { 696219974Smav task = malloc(sizeof(struct task), 697219974Smav M_MD_SII, M_WAITOK | M_ZERO); 698219974Smav TASK_INIT(task, 0, g_disk_md_sii_retaste, task); 699219974Smav taskqueue_enqueue(taskqueue_swi, task); 700219974Smav } 701219974Smav} 702219974Smav 703219974Smavstatic void 704219974Smavg_raid_md_sii_start(struct g_raid_softc *sc) 705219974Smav{ 706219974Smav struct g_raid_md_object *md; 707219974Smav struct g_raid_md_sii_object *mdi; 708219974Smav struct g_raid_md_sii_perdisk *pd; 709219974Smav struct sii_raid_conf *meta; 710219974Smav struct g_raid_volume *vol; 711219974Smav struct g_raid_subdisk *sd; 712219974Smav struct g_raid_disk *disk, *best; 713219974Smav off_t size; 714219974Smav int j, disk_pos; 715219974Smav uint32_t gendiff, bestgendiff; 716219974Smav char buf[17]; 717219974Smav 718219974Smav md = sc->sc_md; 719219974Smav mdi = (struct g_raid_md_sii_object *)md; 720219974Smav meta = mdi->mdio_meta; 721219974Smav 722219974Smav /* Create volumes and subdisks. */ 723219974Smav sii_meta_get_name(meta, buf); 724219974Smav vol = g_raid_create_volume(sc, buf, -1); 725219974Smav vol->v_mediasize = (off_t)meta->total_sectors * 512; 726219974Smav if (meta->type == SII_T_RAID0) { 727219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 728219974Smav size = vol->v_mediasize / mdi->mdio_total_disks; 729219974Smav } else if (meta->type == SII_T_RAID1) { 730219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 731219974Smav size = vol->v_mediasize; 732219974Smav } else if (meta->type == SII_T_RAID01) { 733219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 734219974Smav size = vol->v_mediasize / (mdi->mdio_total_disks / 2); 735219974Smav } else if (meta->type == SII_T_CONCAT) { 736219974Smav if (mdi->mdio_total_disks == 1) 737219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 738219974Smav else 739219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT; 740219974Smav size = 0; 741219974Smav } else if (meta->type == SII_T_RAID5) { 742219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 743219974Smav size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 744219974Smav } else if (meta->type == SII_T_JBOD) { 745219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 746219974Smav size = 0; 747219974Smav } else { 748219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 749219974Smav size = 0; 750219974Smav } 751219974Smav vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 752219974Smav vol->v_strip_size = meta->strip_sectors * 512; //ZZZ 753219974Smav vol->v_disks_count = mdi->mdio_total_disks; 754219974Smav vol->v_sectorsize = 512; //ZZZ 755219974Smav for (j = 0; j < vol->v_disks_count; j++) { 756219974Smav sd = &vol->v_subdisks[j]; 757219974Smav sd->sd_offset = 0; 758219974Smav sd->sd_size = size; 759219974Smav } 760219974Smav g_raid_start_volume(vol); 761219974Smav 762219974Smav /* Create disk placeholders to store data for later writing. */ 763219974Smav for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) { 764219974Smav pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 765219974Smav pd->pd_disk_pos = disk_pos; 766219974Smav disk = g_raid_create_disk(sc); 767219974Smav disk->d_md_data = (void *)pd; 768219974Smav disk->d_state = G_RAID_DISK_S_OFFLINE; 769219974Smav sd = &vol->v_subdisks[disk_pos]; 770219974Smav sd->sd_disk = disk; 771219974Smav TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 772219974Smav } 773219974Smav 774219974Smav /* 775219974Smav * Make all disks found till the moment take their places 776219974Smav * in order of their generation numbers. 777219974Smav */ 778219974Smav do { 779219974Smav best = NULL; 780219974Smav bestgendiff = 0xffffffff; 781219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 782219974Smav if (disk->d_state != G_RAID_DISK_S_NONE) 783219974Smav continue; 784219974Smav pd = disk->d_md_data; 785219974Smav if (pd->pd_meta == NULL) 786219974Smav gendiff = 0xfffffffe; 787219974Smav else 788219974Smav gendiff = meta->generation - 789219974Smav pd->pd_meta->generation; 790219974Smav if (gendiff < bestgendiff) { 791219974Smav best = disk; 792219974Smav bestgendiff = gendiff; 793219974Smav } 794219974Smav } 795219974Smav if (best != NULL) 796219974Smav g_raid_md_sii_start_disk(best); 797219974Smav } while (best != NULL); 798219974Smav 799219974Smav mdi->mdio_started = 1; 800219974Smav G_RAID_DEBUG1(0, sc, "Array started."); 801219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 802219974Smav 803219974Smav /* Pickup any STALE/SPARE disks to refill array if needed. */ 804219974Smav g_raid_md_sii_refill(sc); 805219974Smav 806219974Smav g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME); 807219974Smav 808219974Smav callout_stop(&mdi->mdio_start_co); 809219974Smav G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount); 810219974Smav root_mount_rel(mdi->mdio_rootmount); 811219974Smav mdi->mdio_rootmount = NULL; 812219974Smav} 813219974Smav 814219974Smavstatic void 815219974Smavg_raid_md_sii_new_disk(struct g_raid_disk *disk) 816219974Smav{ 817219974Smav struct g_raid_softc *sc; 818219974Smav struct g_raid_md_object *md; 819219974Smav struct g_raid_md_sii_object *mdi; 820219974Smav struct sii_raid_conf *pdmeta; 821219974Smav struct g_raid_md_sii_perdisk *pd; 822219974Smav 823219974Smav sc = disk->d_softc; 824219974Smav md = sc->sc_md; 825219974Smav mdi = (struct g_raid_md_sii_object *)md; 826219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 827219974Smav pdmeta = pd->pd_meta; 828219974Smav 829219974Smav if (mdi->mdio_started) { 830219974Smav if (g_raid_md_sii_start_disk(disk)) 831219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 832219974Smav } else { 833219974Smav if (mdi->mdio_meta == NULL || 834219974Smav ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) { 835219974Smav G_RAID_DEBUG1(1, sc, "Newer disk"); 836219974Smav if (mdi->mdio_meta != NULL) 837219974Smav free(mdi->mdio_meta, M_MD_SII); 838219974Smav mdi->mdio_meta = sii_meta_copy(pdmeta); 839219974Smav mdi->mdio_generation = mdi->mdio_meta->generation; 840219974Smav mdi->mdio_total_disks = sii_meta_total_disks(pdmeta); 841219974Smav mdi->mdio_disks_present = 1; 842219974Smav } else if (pdmeta->generation == mdi->mdio_generation) { 843219974Smav mdi->mdio_disks_present++; 844219974Smav G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 845219974Smav mdi->mdio_disks_present, 846219974Smav mdi->mdio_total_disks); 847219974Smav } else { 848219974Smav G_RAID_DEBUG1(1, sc, "Older disk"); 849219974Smav } 850219974Smav 851219974Smav /* If we collected all needed disks - start array. */ 852219974Smav if (mdi->mdio_disks_present == mdi->mdio_total_disks) 853219974Smav g_raid_md_sii_start(sc); 854219974Smav } 855219974Smav} 856219974Smav 857219974Smavstatic void 858219974Smavg_raid_sii_go(void *arg) 859219974Smav{ 860219974Smav struct g_raid_softc *sc; 861219974Smav struct g_raid_md_object *md; 862219974Smav struct g_raid_md_sii_object *mdi; 863219974Smav 864219974Smav sc = arg; 865219974Smav md = sc->sc_md; 866219974Smav mdi = (struct g_raid_md_sii_object *)md; 867219974Smav if (!mdi->mdio_started) { 868219974Smav G_RAID_DEBUG1(0, sc, "Force array start due to timeout."); 869219974Smav g_raid_event_send(sc, G_RAID_NODE_E_START, 0); 870219974Smav } 871219974Smav} 872219974Smav 873219974Smavstatic int 874219974Smavg_raid_md_create_sii(struct g_raid_md_object *md, struct g_class *mp, 875219974Smav struct g_geom **gp) 876219974Smav{ 877219974Smav struct g_raid_softc *sc; 878219974Smav struct g_raid_md_sii_object *mdi; 879219974Smav char name[32]; 880219974Smav 881219974Smav mdi = (struct g_raid_md_sii_object *)md; 882219974Smav mdi->mdio_timestamp[5] = arc4random(); 883219974Smav mdi->mdio_timestamp[4] = arc4random(); 884219974Smav mdi->mdio_timestamp[3] = arc4random(); 885219974Smav mdi->mdio_timestamp[2] = arc4random(); 886219974Smav mdi->mdio_timestamp[1] = arc4random(); 887219974Smav mdi->mdio_timestamp[0] = arc4random(); 888219974Smav mdi->mdio_location = arc4random(); 889219974Smav mdi->mdio_generation = 0; 890219974Smav snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x", 891219974Smav mdi->mdio_timestamp[5], mdi->mdio_timestamp[4], 892219974Smav mdi->mdio_timestamp[3], mdi->mdio_timestamp[2], 893219974Smav mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]); 894219974Smav sc = g_raid_create_node(mp, name, md); 895219974Smav if (sc == NULL) 896219974Smav return (G_RAID_MD_TASTE_FAIL); 897219974Smav md->mdo_softc = sc; 898219974Smav *gp = sc->sc_geom; 899219974Smav return (G_RAID_MD_TASTE_NEW); 900219974Smav} 901219974Smav 902219974Smavstatic int 903219974Smavg_raid_md_taste_sii(struct g_raid_md_object *md, struct g_class *mp, 904219974Smav struct g_consumer *cp, struct g_geom **gp) 905219974Smav{ 906219974Smav struct g_consumer *rcp; 907219974Smav struct g_provider *pp; 908219974Smav struct g_raid_md_sii_object *mdi, *mdi1; 909219974Smav struct g_raid_softc *sc; 910219974Smav struct g_raid_disk *disk; 911219974Smav struct sii_raid_conf *meta; 912219974Smav struct g_raid_md_sii_perdisk *pd; 913219974Smav struct g_geom *geom; 914219974Smav int error, disk_pos, result, spare, len; 915219974Smav char name[32]; 916219974Smav uint16_t vendor; 917219974Smav 918219974Smav G_RAID_DEBUG(1, "Tasting SiI on %s", cp->provider->name); 919219974Smav mdi = (struct g_raid_md_sii_object *)md; 920219974Smav pp = cp->provider; 921219974Smav 922219974Smav /* Read metadata from device. */ 923219974Smav meta = NULL; 924219974Smav spare = 0; 925219974Smav vendor = 0xffff; 926219974Smav disk_pos = 0; 927219974Smav if (g_access(cp, 1, 0, 0) != 0) 928219974Smav return (G_RAID_MD_TASTE_FAIL); 929219974Smav g_topology_unlock(); 930219974Smav len = 2; 931219974Smav if (pp->geom->rank == 1) 932219974Smav g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 933219974Smav meta = sii_meta_read(cp); 934219974Smav g_topology_lock(); 935219974Smav g_access(cp, -1, 0, 0); 936219974Smav if (meta == NULL) { 937219974Smav if (g_raid_aggressive_spare) { 938219974Smav if (vendor == 0x1095) { 939219974Smav G_RAID_DEBUG(1, 940219974Smav "No SiI metadata, forcing spare."); 941219974Smav spare = 2; 942219974Smav goto search; 943219974Smav } else { 944219974Smav G_RAID_DEBUG(1, 945219974Smav "SiI vendor mismatch 0x%04x != 0x1095", 946219974Smav vendor); 947219974Smav } 948219974Smav } 949219974Smav return (G_RAID_MD_TASTE_FAIL); 950219974Smav } 951219974Smav 952219974Smav /* Check this disk position in obtained metadata. */ 953219974Smav disk_pos = sii_meta_disk_pos(meta, meta); 954219974Smav if (disk_pos == -1) { 955219974Smav G_RAID_DEBUG(1, "SiI disk position not found"); 956219974Smav goto fail1; 957219974Smav } 958219974Smav 959219974Smav /* Metadata valid. Print it. */ 960219974Smav g_raid_md_sii_print(meta); 961219974Smav G_RAID_DEBUG(1, "SiI disk position %d", disk_pos); 962219974Smav spare = (meta->type == SII_T_SPARE) ? 1 : 0; 963219974Smav 964219974Smavsearch: 965219974Smav /* Search for matching node. */ 966219974Smav sc = NULL; 967219974Smav mdi1 = NULL; 968219974Smav LIST_FOREACH(geom, &mp->geom, geom) { 969219974Smav sc = geom->softc; 970219974Smav if (sc == NULL) 971219974Smav continue; 972219974Smav if (sc->sc_stopping != 0) 973219974Smav continue; 974219974Smav if (sc->sc_md->mdo_class != md->mdo_class) 975219974Smav continue; 976219974Smav mdi1 = (struct g_raid_md_sii_object *)sc->sc_md; 977219974Smav if (spare) { 978219974Smav if (mdi1->mdio_incomplete) 979219974Smav break; 980219974Smav } else { 981219974Smav if (mdi1->mdio_location == meta->raid_location && 982219974Smav memcmp(&mdi1->mdio_timestamp, 983219974Smav &meta->timestamp, 6) == 0) 984219974Smav break; 985219974Smav } 986219974Smav } 987219974Smav 988219974Smav /* Found matching node. */ 989219974Smav if (geom != NULL) { 990219974Smav G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name); 991219974Smav result = G_RAID_MD_TASTE_EXISTING; 992219974Smav 993219974Smav } else if (spare) { /* Not found needy node -- left for later. */ 994219974Smav G_RAID_DEBUG(1, "Spare is not needed at this time"); 995219974Smav goto fail1; 996219974Smav 997219974Smav } else { /* Not found matching node -- create one. */ 998219974Smav result = G_RAID_MD_TASTE_NEW; 999219974Smav memcpy(&mdi->mdio_timestamp, &meta->timestamp, 6); 1000219974Smav mdi->mdio_location = meta->raid_location; 1001219974Smav snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x", 1002219974Smav mdi->mdio_timestamp[5], mdi->mdio_timestamp[4], 1003219974Smav mdi->mdio_timestamp[3], mdi->mdio_timestamp[2], 1004219974Smav mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]); 1005219974Smav sc = g_raid_create_node(mp, name, md); 1006219974Smav md->mdo_softc = sc; 1007219974Smav geom = sc->sc_geom; 1008219974Smav callout_init(&mdi->mdio_start_co, 1); 1009219974Smav callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz, 1010219974Smav g_raid_sii_go, sc); 1011219974Smav mdi->mdio_rootmount = root_mount_hold("GRAID-SiI"); 1012219974Smav G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount); 1013219974Smav } 1014219974Smav 1015219974Smav rcp = g_new_consumer(geom); 1016219974Smav g_attach(rcp, pp); 1017219974Smav if (g_access(rcp, 1, 1, 1) != 0) 1018219974Smav ; //goto fail1; 1019219974Smav 1020219974Smav g_topology_unlock(); 1021219974Smav sx_xlock(&sc->sc_lock); 1022219974Smav 1023219974Smav pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1024219974Smav pd->pd_meta = meta; 1025219974Smav if (spare == 2) { 1026219974Smav pd->pd_disk_pos = -3; 1027219974Smav } else { 1028219974Smav pd->pd_disk_pos = -1; 1029219974Smav } 1030219974Smav pd->pd_disk_size = pp->mediasize; 1031219974Smav disk = g_raid_create_disk(sc); 1032219974Smav disk->d_md_data = (void *)pd; 1033219974Smav disk->d_consumer = rcp; 1034219974Smav rcp->private = disk; 1035219974Smav 1036219974Smav /* Read kernel dumping information. */ 1037219974Smav disk->d_kd.offset = 0; 1038219974Smav disk->d_kd.length = OFF_MAX; 1039219974Smav len = sizeof(disk->d_kd); 1040219974Smav error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd); 1041219974Smav if (disk->d_kd.di.dumper == NULL) 1042219974Smav G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.", 1043219974Smav rcp->provider->name, error); 1044219974Smav 1045219974Smav g_raid_md_sii_new_disk(disk); 1046219974Smav 1047219974Smav sx_xunlock(&sc->sc_lock); 1048219974Smav g_topology_lock(); 1049219974Smav *gp = geom; 1050219974Smav return (result); 1051219974Smavfail1: 1052219974Smav free(meta, M_MD_SII); 1053219974Smav return (G_RAID_MD_TASTE_FAIL); 1054219974Smav} 1055219974Smav 1056219974Smavstatic int 1057219974Smavg_raid_md_event_sii(struct g_raid_md_object *md, 1058219974Smav struct g_raid_disk *disk, u_int event) 1059219974Smav{ 1060219974Smav struct g_raid_softc *sc; 1061219974Smav struct g_raid_subdisk *sd; 1062219974Smav struct g_raid_md_sii_object *mdi; 1063219974Smav struct g_raid_md_sii_perdisk *pd; 1064219974Smav 1065219974Smav sc = md->mdo_softc; 1066219974Smav mdi = (struct g_raid_md_sii_object *)md; 1067219974Smav if (disk == NULL) { 1068219974Smav switch (event) { 1069219974Smav case G_RAID_NODE_E_START: 1070219974Smav if (!mdi->mdio_started) 1071219974Smav g_raid_md_sii_start(sc); 1072219974Smav return (0); 1073219974Smav } 1074219974Smav return (-1); 1075219974Smav } 1076219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1077219974Smav switch (event) { 1078219974Smav case G_RAID_DISK_E_DISCONNECTED: 1079219974Smav /* If disk was assigned, just update statuses. */ 1080219974Smav if (pd->pd_disk_pos >= 0) { 1081219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1082219974Smav if (disk->d_consumer) { 1083219974Smav g_raid_kill_consumer(sc, disk->d_consumer); 1084219974Smav disk->d_consumer = NULL; 1085219974Smav } 1086219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1087219974Smav g_raid_change_subdisk_state(sd, 1088219974Smav G_RAID_SUBDISK_S_NONE); 1089219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1090219974Smav G_RAID_EVENT_SUBDISK); 1091219974Smav } 1092219974Smav } else { 1093219974Smav /* Otherwise -- delete. */ 1094219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1095219974Smav g_raid_destroy_disk(disk); 1096219974Smav } 1097219974Smav 1098219974Smav /* Write updated metadata to all disks. */ 1099219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 1100219974Smav 1101219974Smav /* Check if anything left except placeholders. */ 1102219974Smav if (g_raid_ndisks(sc, -1) == 1103219974Smav g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1104219974Smav g_raid_destroy_node(sc, 0); 1105219974Smav else 1106219974Smav g_raid_md_sii_refill(sc); 1107219974Smav return (0); 1108219974Smav } 1109219974Smav return (-2); 1110219974Smav} 1111219974Smav 1112219974Smavstatic int 1113219974Smavg_raid_md_ctl_sii(struct g_raid_md_object *md, 1114219974Smav struct gctl_req *req) 1115219974Smav{ 1116219974Smav struct g_raid_softc *sc; 1117219974Smav struct g_raid_volume *vol; 1118219974Smav struct g_raid_subdisk *sd; 1119219974Smav struct g_raid_disk *disk; 1120219974Smav struct g_raid_md_sii_object *mdi; 1121219974Smav struct g_raid_md_sii_perdisk *pd; 1122219974Smav struct g_consumer *cp; 1123219974Smav struct g_provider *pp; 1124219974Smav char arg[16]; 1125219974Smav const char *verb, *volname, *levelname, *diskname; 1126219974Smav int *nargs, *force; 1127219974Smav off_t size, sectorsize, strip; 1128219974Smav intmax_t *sizearg, *striparg; 1129219974Smav int numdisks, i, len, level, qual, update; 1130219974Smav int error; 1131219974Smav 1132219974Smav sc = md->mdo_softc; 1133219974Smav mdi = (struct g_raid_md_sii_object *)md; 1134219974Smav verb = gctl_get_param(req, "verb", NULL); 1135219974Smav nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1136219974Smav error = 0; 1137219974Smav if (strcmp(verb, "label") == 0) { 1138219974Smav 1139219974Smav if (*nargs < 4) { 1140219974Smav gctl_error(req, "Invalid number of arguments."); 1141219974Smav return (-1); 1142219974Smav } 1143219974Smav volname = gctl_get_asciiparam(req, "arg1"); 1144219974Smav if (volname == NULL) { 1145219974Smav gctl_error(req, "No volume name."); 1146219974Smav return (-2); 1147219974Smav } 1148219974Smav levelname = gctl_get_asciiparam(req, "arg2"); 1149219974Smav if (levelname == NULL) { 1150219974Smav gctl_error(req, "No RAID level."); 1151219974Smav return (-3); 1152219974Smav } 1153219974Smav if (g_raid_volume_str2level(levelname, &level, &qual)) { 1154219974Smav gctl_error(req, "Unknown RAID level '%s'.", levelname); 1155219974Smav return (-4); 1156219974Smav } 1157219974Smav numdisks = *nargs - 3; 1158219974Smav force = gctl_get_paraml(req, "force", sizeof(*force)); 1159219974Smav if (!g_raid_md_sii_supported(level, qual, numdisks, 1160219974Smav force ? *force : 0)) { 1161219974Smav gctl_error(req, "Unsupported RAID level " 1162219974Smav "(0x%02x/0x%02x), or number of disks (%d).", 1163219974Smav level, qual, numdisks); 1164219974Smav return (-5); 1165219974Smav } 1166219974Smav 1167219974Smav /* Search for disks, connect them and probe. */ 1168219974Smav size = 0x7fffffffffffffffllu; 1169219974Smav sectorsize = 0; 1170219974Smav for (i = 0; i < numdisks; i++) { 1171219974Smav snprintf(arg, sizeof(arg), "arg%d", i + 3); 1172219974Smav diskname = gctl_get_asciiparam(req, arg); 1173219974Smav if (diskname == NULL) { 1174219974Smav gctl_error(req, "No disk name (%s).", arg); 1175219974Smav error = -6; 1176219974Smav break; 1177219974Smav } 1178219974Smav if (strcmp(diskname, "NONE") == 0) { 1179219974Smav cp = NULL; 1180219974Smav pp = NULL; 1181219974Smav } else { 1182219974Smav g_topology_lock(); 1183219974Smav cp = g_raid_open_consumer(sc, diskname); 1184219974Smav if (cp == NULL) { 1185219974Smav gctl_error(req, "Can't open '%s'.", 1186219974Smav diskname); 1187219974Smav g_topology_unlock(); 1188219974Smav error = -7; 1189219974Smav break; 1190219974Smav } 1191219974Smav pp = cp->provider; 1192219974Smav } 1193219974Smav pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1194219974Smav pd->pd_disk_pos = i; 1195219974Smav disk = g_raid_create_disk(sc); 1196219974Smav disk->d_md_data = (void *)pd; 1197219974Smav disk->d_consumer = cp; 1198219974Smav if (cp == NULL) 1199219974Smav continue; 1200219974Smav cp->private = disk; 1201219974Smav g_topology_unlock(); 1202219974Smav 1203219974Smav /* Read kernel dumping information. */ 1204219974Smav disk->d_kd.offset = 0; 1205219974Smav disk->d_kd.length = OFF_MAX; 1206219974Smav len = sizeof(disk->d_kd); 1207219974Smav g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd); 1208219974Smav if (disk->d_kd.di.dumper == NULL) 1209219974Smav G_RAID_DEBUG1(2, sc, 1210219974Smav "Dumping not supported by %s.", 1211219974Smav cp->provider->name); 1212219974Smav 1213219974Smav pd->pd_disk_size = pp->mediasize; 1214219974Smav if (size > pp->mediasize) 1215219974Smav size = pp->mediasize; 1216219974Smav if (sectorsize < pp->sectorsize) 1217219974Smav sectorsize = pp->sectorsize; 1218219974Smav } 1219219974Smav if (error != 0) 1220219974Smav return (error); 1221219974Smav 1222219974Smav /* Reserve space for metadata. */ 1223219974Smav size -= 0x800 * sectorsize; 1224219974Smav 1225219974Smav /* Handle size argument. */ 1226219974Smav len = sizeof(*sizearg); 1227219974Smav sizearg = gctl_get_param(req, "size", &len); 1228219974Smav if (sizearg != NULL && len == sizeof(*sizearg) && 1229219974Smav *sizearg > 0) { 1230219974Smav if (*sizearg > size) { 1231219974Smav gctl_error(req, "Size too big %lld > %lld.", 1232219974Smav (long long)*sizearg, (long long)size); 1233219974Smav return (-9); 1234219974Smav } 1235219974Smav size = *sizearg; 1236219974Smav } 1237219974Smav 1238219974Smav /* Handle strip argument. */ 1239219974Smav strip = 131072; 1240219974Smav len = sizeof(*striparg); 1241219974Smav striparg = gctl_get_param(req, "strip", &len); 1242219974Smav if (striparg != NULL && len == sizeof(*striparg) && 1243219974Smav *striparg > 0) { 1244219974Smav if (*striparg < sectorsize) { 1245219974Smav gctl_error(req, "Strip size too small."); 1246219974Smav return (-10); 1247219974Smav } 1248219974Smav if (*striparg % sectorsize != 0) { 1249219974Smav gctl_error(req, "Incorrect strip size."); 1250219974Smav return (-11); 1251219974Smav } 1252219974Smav if (strip > 65535 * sectorsize) { 1253219974Smav gctl_error(req, "Strip size too big."); 1254219974Smav return (-12); 1255219974Smav } 1256219974Smav strip = *striparg; 1257219974Smav } 1258219974Smav 1259219974Smav /* Round size down to strip or sector. */ 1260219974Smav if (level == G_RAID_VOLUME_RL_RAID1) 1261219974Smav size -= (size % sectorsize); 1262219974Smav else if (level == G_RAID_VOLUME_RL_RAID1E && 1263219974Smav (numdisks & 1) != 0) 1264219974Smav size -= (size % (2 * strip)); 1265219974Smav else 1266219974Smav size -= (size % strip); 1267219974Smav if (size <= 0) { 1268219974Smav gctl_error(req, "Size too small."); 1269219974Smav return (-13); 1270219974Smav } 1271219974Smav if (size > 0xffffffffffffllu * sectorsize) { 1272219974Smav gctl_error(req, "Size too big."); 1273219974Smav return (-14); 1274219974Smav } 1275219974Smav 1276219974Smav /* We have all we need, create things: volume, ... */ 1277219974Smav mdi->mdio_total_disks = numdisks; 1278219974Smav mdi->mdio_started = 1; 1279219974Smav vol = g_raid_create_volume(sc, volname, -1); 1280219974Smav vol->v_md_data = (void *)(intptr_t)0; 1281219974Smav vol->v_raid_level = level; 1282219974Smav vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 1283219974Smav vol->v_strip_size = strip; 1284219974Smav vol->v_disks_count = numdisks; 1285219974Smav if (level == G_RAID_VOLUME_RL_RAID0 || 1286219974Smav level == G_RAID_VOLUME_RL_CONCAT || 1287219974Smav level == G_RAID_VOLUME_RL_SINGLE) 1288219974Smav vol->v_mediasize = size * numdisks; 1289219974Smav else if (level == G_RAID_VOLUME_RL_RAID1) 1290219974Smav vol->v_mediasize = size; 1291219974Smav else if (level == G_RAID_VOLUME_RL_RAID5) 1292219974Smav vol->v_mediasize = size * (numdisks - 1); 1293219974Smav else { /* RAID1E */ 1294219974Smav vol->v_mediasize = ((size * numdisks) / strip / 2) * 1295219974Smav strip; 1296219974Smav } 1297219974Smav vol->v_sectorsize = sectorsize; 1298219974Smav g_raid_start_volume(vol); 1299219974Smav 1300219974Smav /* , and subdisks. */ 1301219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1302219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1303219974Smav sd = &vol->v_subdisks[pd->pd_disk_pos]; 1304219974Smav sd->sd_disk = disk; 1305219974Smav sd->sd_offset = 0; 1306219974Smav sd->sd_size = size; 1307219974Smav TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1308219974Smav if (sd->sd_disk->d_consumer != NULL) { 1309219974Smav g_raid_change_disk_state(disk, 1310219974Smav G_RAID_DISK_S_ACTIVE); 1311219974Smav g_raid_change_subdisk_state(sd, 1312219974Smav G_RAID_SUBDISK_S_ACTIVE); 1313219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1314219974Smav G_RAID_EVENT_SUBDISK); 1315219974Smav } else { 1316219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1317219974Smav } 1318219974Smav } 1319219974Smav 1320219974Smav /* Write metadata based on created entities. */ 1321219974Smav G_RAID_DEBUG1(0, sc, "Array started."); 1322219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 1323219974Smav 1324219974Smav /* Pickup any STALE/SPARE disks to refill array if needed. */ 1325219974Smav g_raid_md_sii_refill(sc); 1326219974Smav 1327219974Smav g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1328219974Smav G_RAID_EVENT_VOLUME); 1329219974Smav return (0); 1330219974Smav } 1331219974Smav if (strcmp(verb, "delete") == 0) { 1332219974Smav 1333219974Smav /* Check if some volume is still open. */ 1334219974Smav force = gctl_get_paraml(req, "force", sizeof(*force)); 1335219974Smav if (force != NULL && *force == 0 && 1336219974Smav g_raid_nopens(sc) != 0) { 1337219974Smav gctl_error(req, "Some volume is still open."); 1338219974Smav return (-4); 1339219974Smav } 1340219974Smav 1341219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1342219974Smav if (disk->d_consumer) 1343219974Smav sii_meta_erase(disk->d_consumer); 1344219974Smav } 1345219974Smav g_raid_destroy_node(sc, 0); 1346219974Smav return (0); 1347219974Smav } 1348219974Smav if (strcmp(verb, "remove") == 0 || 1349219974Smav strcmp(verb, "fail") == 0) { 1350219974Smav if (*nargs < 2) { 1351219974Smav gctl_error(req, "Invalid number of arguments."); 1352219974Smav return (-1); 1353219974Smav } 1354219974Smav for (i = 1; i < *nargs; i++) { 1355219974Smav snprintf(arg, sizeof(arg), "arg%d", i); 1356219974Smav diskname = gctl_get_asciiparam(req, arg); 1357219974Smav if (diskname == NULL) { 1358219974Smav gctl_error(req, "No disk name (%s).", arg); 1359219974Smav error = -2; 1360219974Smav break; 1361219974Smav } 1362219974Smav if (strncmp(diskname, "/dev/", 5) == 0) 1363219974Smav diskname += 5; 1364219974Smav 1365219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1366219974Smav if (disk->d_consumer != NULL && 1367219974Smav disk->d_consumer->provider != NULL && 1368219974Smav strcmp(disk->d_consumer->provider->name, 1369219974Smav diskname) == 0) 1370219974Smav break; 1371219974Smav } 1372219974Smav if (disk == NULL) { 1373219974Smav gctl_error(req, "Disk '%s' not found.", 1374219974Smav diskname); 1375219974Smav error = -3; 1376219974Smav break; 1377219974Smav } 1378219974Smav 1379219974Smav if (strcmp(verb, "fail") == 0) { 1380219974Smav g_raid_md_fail_disk_sii(md, NULL, disk); 1381219974Smav continue; 1382219974Smav } 1383219974Smav 1384219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1385219974Smav 1386219974Smav /* Erase metadata on deleting disk. */ 1387219974Smav sii_meta_erase(disk->d_consumer); 1388219974Smav 1389219974Smav /* If disk was assigned, just update statuses. */ 1390219974Smav if (pd->pd_disk_pos >= 0) { 1391219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1392219974Smav if (disk->d_consumer) { 1393219974Smav g_raid_kill_consumer(sc, disk->d_consumer); 1394219974Smav disk->d_consumer = NULL; 1395219974Smav } 1396219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1397219974Smav g_raid_change_subdisk_state(sd, 1398219974Smav G_RAID_SUBDISK_S_NONE); 1399219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1400219974Smav G_RAID_EVENT_SUBDISK); 1401219974Smav } 1402219974Smav } else { 1403219974Smav /* Otherwise -- delete. */ 1404219974Smav g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1405219974Smav g_raid_destroy_disk(disk); 1406219974Smav } 1407219974Smav } 1408219974Smav 1409219974Smav /* Write updated metadata to remaining disks. */ 1410219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 1411219974Smav 1412219974Smav /* Check if anything left except placeholders. */ 1413219974Smav if (g_raid_ndisks(sc, -1) == 1414219974Smav g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1415219974Smav g_raid_destroy_node(sc, 0); 1416219974Smav else 1417219974Smav g_raid_md_sii_refill(sc); 1418219974Smav return (error); 1419219974Smav } 1420219974Smav if (strcmp(verb, "insert") == 0) { 1421219974Smav if (*nargs < 2) { 1422219974Smav gctl_error(req, "Invalid number of arguments."); 1423219974Smav return (-1); 1424219974Smav } 1425219974Smav update = 0; 1426219974Smav for (i = 1; i < *nargs; i++) { 1427219974Smav /* Get disk name. */ 1428219974Smav snprintf(arg, sizeof(arg), "arg%d", i); 1429219974Smav diskname = gctl_get_asciiparam(req, arg); 1430219974Smav if (diskname == NULL) { 1431219974Smav gctl_error(req, "No disk name (%s).", arg); 1432219974Smav error = -3; 1433219974Smav break; 1434219974Smav } 1435219974Smav 1436219974Smav /* Try to find provider with specified name. */ 1437219974Smav g_topology_lock(); 1438219974Smav cp = g_raid_open_consumer(sc, diskname); 1439219974Smav if (cp == NULL) { 1440219974Smav gctl_error(req, "Can't open disk '%s'.", 1441219974Smav diskname); 1442219974Smav g_topology_unlock(); 1443219974Smav error = -4; 1444219974Smav break; 1445219974Smav } 1446219974Smav pp = cp->provider; 1447219974Smav 1448219974Smav pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1449219974Smav pd->pd_disk_pos = -3; 1450219974Smav pd->pd_disk_size = pp->mediasize; 1451219974Smav 1452219974Smav disk = g_raid_create_disk(sc); 1453219974Smav disk->d_consumer = cp; 1454219974Smav disk->d_consumer->private = disk; 1455219974Smav disk->d_md_data = (void *)pd; 1456219974Smav cp->private = disk; 1457219974Smav g_topology_unlock(); 1458219974Smav 1459219974Smav /* Read kernel dumping information. */ 1460219974Smav disk->d_kd.offset = 0; 1461219974Smav disk->d_kd.length = OFF_MAX; 1462219974Smav len = sizeof(disk->d_kd); 1463219974Smav g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd); 1464219974Smav if (disk->d_kd.di.dumper == NULL) 1465219974Smav G_RAID_DEBUG1(2, sc, 1466219974Smav "Dumping not supported by %s.", 1467219974Smav cp->provider->name); 1468219974Smav 1469219974Smav /* Welcome the "new" disk. */ 1470219974Smav update += g_raid_md_sii_start_disk(disk); 1471219974Smav if (disk->d_state == G_RAID_DISK_S_SPARE) { 1472219974Smav sii_meta_write_spare(cp); 1473219974Smav g_raid_destroy_disk(disk); 1474219974Smav } else if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 1475219974Smav gctl_error(req, "Disk '%s' doesn't fit.", 1476219974Smav diskname); 1477219974Smav g_raid_destroy_disk(disk); 1478219974Smav error = -8; 1479219974Smav break; 1480219974Smav } 1481219974Smav } 1482219974Smav 1483219974Smav /* Write new metadata if we changed something. */ 1484219974Smav if (update) 1485219974Smav g_raid_md_write_sii(md, NULL, NULL, NULL); 1486219974Smav return (error); 1487219974Smav } 1488219974Smav gctl_error(req, "Command '%s' is not supported.", verb); 1489219974Smav return (-100); 1490219974Smav} 1491219974Smav 1492219974Smavstatic int 1493219974Smavg_raid_md_write_sii(struct g_raid_md_object *md, struct g_raid_volume *tvol, 1494219974Smav struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 1495219974Smav{ 1496219974Smav struct g_raid_softc *sc; 1497219974Smav struct g_raid_volume *vol; 1498219974Smav struct g_raid_subdisk *sd; 1499219974Smav struct g_raid_disk *disk; 1500219974Smav struct g_raid_md_sii_object *mdi; 1501219974Smav struct g_raid_md_sii_perdisk *pd; 1502219974Smav struct sii_raid_conf *meta; 1503219974Smav int i; 1504219974Smav 1505219974Smav sc = md->mdo_softc; 1506219974Smav mdi = (struct g_raid_md_sii_object *)md; 1507219974Smav 1508219974Smav if (sc->sc_stopping == G_RAID_DESTROY_HARD) 1509219974Smav return (0); 1510219974Smav 1511219974Smav /* Bump generation. Newly written metadata may differ from previous. */ 1512219974Smav mdi->mdio_generation++; 1513219974Smav 1514219974Smav /* There is only one volume. */ 1515219974Smav vol = TAILQ_FIRST(&sc->sc_volumes); 1516219974Smav 1517219974Smav /* Fill global fields. */ 1518219974Smav meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO); 1519219974Smav if (mdi->mdio_meta) 1520219974Smav memcpy(meta, mdi->mdio_meta, sizeof(*meta)); 1521219974Smav meta->total_sectors = vol->v_mediasize / vol->v_sectorsize; 1522219974Smav meta->vendor_id = 0x1095; 1523219974Smav meta->version_minor = 0; 1524219974Smav meta->version_major = 2; 1525219974Smav memcpy(&meta->timestamp, &mdi->mdio_timestamp, 6); 1526219974Smav meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize; 1527219974Smav if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) { 1528219974Smav meta->type = SII_T_RAID0; 1529219974Smav meta->raid0_disks = vol->v_disks_count; 1530219974Smav meta->raid1_disks = 0xff; 1531219974Smav } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) { 1532219974Smav meta->type = SII_T_RAID1; 1533219974Smav meta->raid0_disks = 0xff; 1534219974Smav meta->raid1_disks = vol->v_disks_count; 1535219974Smav } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 1536219974Smav meta->type = SII_T_RAID01; 1537219974Smav meta->raid0_disks = vol->v_disks_count / 2; 1538219974Smav meta->raid1_disks = 2; 1539219974Smav } else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT || 1540219974Smav vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) { 1541219974Smav meta->type = SII_T_JBOD; 1542219974Smav meta->raid0_disks = vol->v_disks_count; 1543219974Smav meta->raid1_disks = 0xff; 1544219974Smav } else { 1545219974Smav meta->type = SII_T_RAID5; 1546219974Smav meta->raid0_disks = vol->v_disks_count; 1547219974Smav meta->raid1_disks = 0xff; 1548219974Smav } 1549219974Smav meta->generation = mdi->mdio_generation; 1550219974Smav meta->raid_status = vol->v_dirty ? SII_S_ONLINE : SII_S_AVAILABLE; 1551219974Smav for (i = 0; i < vol->v_disks_count; i++) { 1552219974Smav sd = &vol->v_subdisks[i]; 1553219974Smav if (sd->sd_state == G_RAID_SUBDISK_S_STALE || 1554219974Smav sd->sd_state == G_RAID_SUBDISK_S_RESYNC) 1555219974Smav meta->raid_status = SII_S_ONLINE; 1556219974Smav } 1557219974Smav meta->raid_location = mdi->mdio_location; 1558219974Smav sii_meta_put_name(meta, vol->v_name); 1559219974Smav 1560219974Smav /* We are done. Print meta data and store them to disks. */ 1561219974Smav if (mdi->mdio_meta != NULL) 1562219974Smav free(mdi->mdio_meta, M_MD_SII); 1563219974Smav mdi->mdio_meta = meta; 1564219974Smav i = 0; 1565219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1566219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1567219974Smav if (disk->d_state != G_RAID_DISK_S_ACTIVE) 1568219974Smav continue; 1569219974Smav if (pd->pd_meta != NULL) { 1570219974Smav free(pd->pd_meta, M_MD_SII); 1571219974Smav pd->pd_meta = NULL; 1572219974Smav } 1573219974Smav pd->pd_meta = sii_meta_copy(meta); 1574219974Smav if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) { 1575219974Smav if (sd->sd_state < G_RAID_SUBDISK_S_NEW) 1576219974Smav pd->pd_meta->disk_status = SII_S_DROPPED; 1577219974Smav else if (sd->sd_state < G_RAID_SUBDISK_S_STALE) { 1578219974Smav pd->pd_meta->disk_status = SII_S_REBUILD; 1579219974Smav pd->pd_meta->rebuild_lba = 1580219974Smav sd->sd_rebuild_pos / vol->v_sectorsize; 1581219974Smav } else 1582219974Smav pd->pd_meta->disk_status = SII_S_CURRENT; 1583219974Smav if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) { 1584219974Smav pd->pd_meta->disk_number = sd->sd_pos; 1585219974Smav pd->pd_meta->raid0_ident = 0xff; 1586219974Smav pd->pd_meta->raid1_ident = 0; 1587219974Smav } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 1588219974Smav pd->pd_meta->disk_number = sd->sd_pos / meta->raid1_disks; 1589219974Smav pd->pd_meta->raid0_ident = sd->sd_pos % meta->raid1_disks; 1590219974Smav pd->pd_meta->raid1_ident = sd->sd_pos / meta->raid1_disks; 1591219974Smav } else { 1592219974Smav pd->pd_meta->disk_number = sd->sd_pos; 1593219974Smav pd->pd_meta->raid0_ident = 0; 1594219974Smav pd->pd_meta->raid1_ident = 0xff; 1595219974Smav } 1596219974Smav } 1597219974Smav G_RAID_DEBUG(1, "Writing SiI metadata to %s", 1598219974Smav g_raid_get_diskname(disk)); 1599219974Smav g_raid_md_sii_print(pd->pd_meta); 1600219974Smav sii_meta_write(disk->d_consumer, pd->pd_meta); 1601219974Smav } 1602219974Smav return (0); 1603219974Smav} 1604219974Smav 1605219974Smavstatic int 1606219974Smavg_raid_md_fail_disk_sii(struct g_raid_md_object *md, 1607219974Smav struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 1608219974Smav{ 1609219974Smav struct g_raid_softc *sc; 1610219974Smav struct g_raid_md_sii_object *mdi; 1611219974Smav struct g_raid_md_sii_perdisk *pd; 1612219974Smav struct g_raid_subdisk *sd; 1613219974Smav 1614219974Smav sc = md->mdo_softc; 1615219974Smav mdi = (struct g_raid_md_sii_object *)md; 1616219974Smav pd = (struct g_raid_md_sii_perdisk *)tdisk->d_md_data; 1617219974Smav 1618219974Smav /* We can't fail disk that is not a part of array now. */ 1619219974Smav if (pd->pd_disk_pos < 0) 1620219974Smav return (-1); 1621219974Smav 1622219974Smav /* 1623219974Smav * Mark disk as failed in metadata and try to write that metadata 1624219974Smav * to the disk itself to prevent it's later resurrection as STALE. 1625219974Smav */ 1626219974Smav if (tdisk->d_consumer) { 1627219974Smav if (pd->pd_meta) { 1628219974Smav pd->pd_meta->disk_status = SII_S_REMOVED; 1629219974Smav sii_meta_write(tdisk->d_consumer, pd->pd_meta); 1630219974Smav } else 1631219974Smav sii_meta_erase(tdisk->d_consumer); 1632219974Smav } 1633219974Smav 1634219974Smav /* Change states. */ 1635219974Smav g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 1636219974Smav TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 1637219974Smav g_raid_change_subdisk_state(sd, 1638219974Smav G_RAID_SUBDISK_S_FAILED); 1639219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 1640219974Smav G_RAID_EVENT_SUBDISK); 1641219974Smav } 1642219974Smav 1643219974Smav /* Write updated metadata to remaining disks. */ 1644219974Smav g_raid_md_write_sii(md, NULL, NULL, tdisk); 1645219974Smav 1646219974Smav /* Check if anything left except placeholders. */ 1647219974Smav if (g_raid_ndisks(sc, -1) == 1648219974Smav g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1649219974Smav g_raid_destroy_node(sc, 0); 1650219974Smav else 1651219974Smav g_raid_md_sii_refill(sc); 1652219974Smav return (0); 1653219974Smav} 1654219974Smav 1655219974Smavstatic int 1656219974Smavg_raid_md_free_disk_sii(struct g_raid_md_object *md, 1657219974Smav struct g_raid_disk *disk) 1658219974Smav{ 1659219974Smav struct g_raid_md_sii_perdisk *pd; 1660219974Smav 1661219974Smav pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1662219974Smav if (pd->pd_meta != NULL) { 1663219974Smav free(pd->pd_meta, M_MD_SII); 1664219974Smav pd->pd_meta = NULL; 1665219974Smav } 1666219974Smav free(pd, M_MD_SII); 1667219974Smav disk->d_md_data = NULL; 1668219974Smav return (0); 1669219974Smav} 1670219974Smav 1671219974Smavstatic int 1672219974Smavg_raid_md_free_sii(struct g_raid_md_object *md) 1673219974Smav{ 1674219974Smav struct g_raid_md_sii_object *mdi; 1675219974Smav 1676219974Smav mdi = (struct g_raid_md_sii_object *)md; 1677219974Smav if (!mdi->mdio_started) { 1678219974Smav mdi->mdio_started = 0; 1679219974Smav callout_stop(&mdi->mdio_start_co); 1680219974Smav G_RAID_DEBUG1(1, md->mdo_softc, 1681219974Smav "root_mount_rel %p", mdi->mdio_rootmount); 1682219974Smav root_mount_rel(mdi->mdio_rootmount); 1683219974Smav mdi->mdio_rootmount = NULL; 1684219974Smav } 1685219974Smav if (mdi->mdio_meta != NULL) { 1686219974Smav free(mdi->mdio_meta, M_MD_SII); 1687219974Smav mdi->mdio_meta = NULL; 1688219974Smav } 1689219974Smav return (0); 1690219974Smav} 1691219974Smav 1692219974SmavG_RAID_MD_DECLARE(g_raid_md_sii); 1693