svn commit: r218722 - in projects/graid/head: sbin/geom/class/raid
sys/conf sys/geom/raid sys/modules/geom/geom_raid
Alexander Motin
mav at FreeBSD.org
Tue Feb 15 22:01:40 UTC 2011
Author: mav
Date: Tue Feb 15 22:01:39 2011
New Revision: 218722
URL: http://svn.freebsd.org/changeset/base/218722
Log:
Add JMicron RAID BIOS metadata format module.
Added:
projects/graid/head/sys/geom/raid/md_jmicron.c (contents, props changed)
Modified:
projects/graid/head/sbin/geom/class/raid/graid.8
projects/graid/head/sys/conf/files
projects/graid/head/sys/modules/geom/geom_raid/Makefile
Modified: projects/graid/head/sbin/geom/class/raid/graid.8
==============================================================================
--- projects/graid/head/sbin/geom/class/raid/graid.8 Tue Feb 15 22:00:00 2011 (r218721)
+++ projects/graid/head/sbin/geom/class/raid/graid.8 Tue Feb 15 22:01:39 2011 (r218722)
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd February 13, 2011
+.Dd February 15, 2011
.Dt GRAID 8
.Os
.Sh NAME
@@ -207,6 +207,13 @@ Supports configurations: RAID0 (2+ disks
RAID5 (3+ disks), RAID10 (4 disks).
Configurations not supported by Intel RAID BIOS, but enforsable on your own
risk: RAID1 (3+ disks), RAID1E (3+ disks), RAID10 (6+ disks).
+.It JMicron
+The format used by JMicron RAID BIOS.
+Supports one volume per array.
+Supports configurations: RAID0 (2+ disks), RAID1 (2 disks),
+RAID10 (4 disks), CONCAT (2+ disks).
+Configurations not supported by JMicron RAID BIOS, but enforsable on your own
+risk: RAID1 (3+ disks), RAID1E (3+ disks), RAID10 (6+ disks), RAID5 (3+ disks).
.El
.Sh SUPPORTED RAID LEVELS
The GEOM RAID class follows a modular design, allowing different RAID levels
Modified: projects/graid/head/sys/conf/files
==============================================================================
--- projects/graid/head/sys/conf/files Tue Feb 15 22:00:00 2011 (r218721)
+++ projects/graid/head/sys/conf/files Tue Feb 15 22:01:39 2011 (r218722)
@@ -2090,6 +2090,7 @@ geom/raid/g_raid_ctl.c optional geom_ra
geom/raid/g_raid_md_if.m optional geom_raid
geom/raid/g_raid_tr_if.m optional geom_raid
geom/raid/md_intel.c optional geom_raid
+geom/raid/md_jmicron.c optional geom_raid
geom/raid/tr_concat.c optional geom_raid
geom/raid/tr_raid0.c optional geom_raid
geom/raid/tr_raid1.c optional geom_raid
Added: projects/graid/head/sys/geom/raid/md_jmicron.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/graid/head/sys/geom/raid/md_jmicron.c Tue Feb 15 22:01:39 2011 (r218722)
@@ -0,0 +1,1619 @@
+/*-
+ * Copyright (c) 2010 Alexander Motin <mav at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/bio.h>
+#include <sys/endian.h>
+#include <sys/kernel.h>
+#include <sys/kobj.h>
+#include <sys/limits.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/systm.h>
+#include <sys/taskqueue.h>
+#include <geom/geom.h>
+#include "geom/raid/g_raid.h"
+#include "g_raid_md_if.h"
+
+static MALLOC_DEFINE(M_MD_JMICRON, "md_jmicron_data", "GEOM_RAID JMicron metadata");
+
+#define JMICRON_MAX_DISKS 8
+#define JMICRON_MAX_SPARE 2
+
+struct jmicron_raid_conf {
+ u_int8_t signature[2];
+#define JMICRON_MAGIC "JM"
+
+ u_int16_t version;
+#define JMICRON_VERSION 0x0001
+
+ u_int16_t checksum;
+ u_int8_t filler_1[10];
+ u_int32_t disk_id;
+ u_int32_t offset;
+ u_int32_t disk_sectors_high;
+ u_int16_t disk_sectors_low;
+ u_int8_t filler_2[2];
+ u_int8_t name[16];
+ u_int8_t type;
+#define JMICRON_T_RAID0 0
+#define JMICRON_T_RAID1 1
+#define JMICRON_T_RAID01 2
+#define JMICRON_T_CONCAT 3
+#define JMICRON_T_RAID5 5
+
+ u_int8_t stripe_shift;
+ u_int16_t flags;
+#define JMICRON_F_READY 0x0001
+#define JMICRON_F_BOOTABLE 0x0002
+#define JMICRON_F_BADSEC 0x0004
+#define JMICRON_F_ACTIVE 0x0010
+#define JMICRON_F_UNSYNC 0x0020
+#define JMICRON_F_NEWEST 0x0040
+
+ u_int8_t filler_3[4];
+ u_int32_t spare[JMICRON_MAX_SPARE];
+ u_int32_t disks[JMICRON_MAX_DISKS];
+#define JMICRON_DISK_MASK 0xFFFFFFF0
+#define JMICRON_SEG_MASK 0x0000000F
+ u_int8_t filler_4[32];
+ u_int8_t filler_5[384];
+};
+
+struct g_raid_md_jmicron_perdisk {
+ struct jmicron_raid_conf *pd_meta;
+ int pd_disk_pos;
+ int pd_disk_id;
+};
+
+struct g_raid_md_jmicron_object {
+ struct g_raid_md_object mdio_base;
+ uint32_t mdio_config_id;
+ struct jmicron_raid_conf *mdio_meta;
+ struct callout mdio_start_co; /* STARTING state timer. */
+ int mdio_total_disks;
+ int mdio_disks_present;
+ int mdio_started;
+ int mdio_incomplete;
+ struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
+};
+
+static g_raid_md_create_t g_raid_md_create_jmicron;
+static g_raid_md_taste_t g_raid_md_taste_jmicron;
+static g_raid_md_event_t g_raid_md_event_jmicron;
+static g_raid_md_ctl_t g_raid_md_ctl_jmicron;
+static g_raid_md_write_t g_raid_md_write_jmicron;
+static g_raid_md_fail_disk_t g_raid_md_fail_disk_jmicron;
+static g_raid_md_free_disk_t g_raid_md_free_disk_jmicron;
+static g_raid_md_free_t g_raid_md_free_jmicron;
+
+static kobj_method_t g_raid_md_jmicron_methods[] = {
+ KOBJMETHOD(g_raid_md_create, g_raid_md_create_jmicron),
+ KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_jmicron),
+ KOBJMETHOD(g_raid_md_event, g_raid_md_event_jmicron),
+ KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_jmicron),
+ KOBJMETHOD(g_raid_md_write, g_raid_md_write_jmicron),
+ KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_jmicron),
+ KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_jmicron),
+ KOBJMETHOD(g_raid_md_free, g_raid_md_free_jmicron),
+ { 0, 0 }
+};
+
+static struct g_raid_md_class g_raid_md_jmicron_class = {
+ "JMicron",
+ g_raid_md_jmicron_methods,
+ sizeof(struct g_raid_md_jmicron_object),
+ .mdc_priority = 100
+};
+
+static void
+g_raid_md_jmicron_print(struct jmicron_raid_conf *meta)
+{
+ int k;
+
+ if (g_raid_debug < 1)
+ return;
+
+ printf("********* ATA JMicron RAID Metadata *********\n");
+ printf("signature <%c%c>\n", meta->signature[0], meta->signature[1]);
+ printf("version %04x\n", meta->version);
+ printf("checksum 0x%04x\n", meta->checksum);
+ printf("disk_id 0x%08x\n", meta->disk_id);
+ printf("offset 0x%08x\n", meta->offset);
+ printf("disk_sectors_high 0x%08x\n", meta->disk_sectors_high);
+ printf("disk_sectors_low 0x%04x\n", meta->disk_sectors_low);
+ printf("name <%.16s>\n", meta->name);
+ printf("type %d\n", meta->type);
+ printf("stripe_shift %d\n", meta->stripe_shift);
+ printf("flags %04x\n", meta->flags);
+ printf("spare ");
+ for (k = 0; k < 2; k++)
+ printf(" 0x%08x", meta->spare[k]);
+ printf("\n");
+ printf("disks ");
+ for (k = 0; k < JMICRON_MAX_DISKS; k++)
+ printf(" 0x%08x", meta->disks[k]);
+ printf("\n");
+ printf("=================================================\n");
+}
+
+static struct jmicron_raid_conf *
+jmicron_meta_copy(struct jmicron_raid_conf *meta)
+{
+ struct jmicron_raid_conf *nmeta;
+
+ nmeta = malloc(sizeof(*meta), M_MD_JMICRON, M_WAITOK);
+ memcpy(nmeta, meta, sizeof(*meta));
+ return (nmeta);
+}
+
+static int
+jmicron_meta_total_disks(struct jmicron_raid_conf *meta)
+{
+ int pos;
+
+ for (pos = 0; pos < JMICRON_MAX_DISKS; pos++) {
+ if (meta->disks[pos] == 0)
+ break;
+ }
+ return (pos);
+}
+
+static int
+jmicron_meta_total_spare(struct jmicron_raid_conf *meta)
+{
+ int pos, n;
+
+ n = 0;
+ for (pos = 0; pos < JMICRON_MAX_SPARE; pos++) {
+ if (meta->spare[pos] != 0)
+ n++;
+ }
+ return (n);
+}
+
+/*
+ * Generate fake Configuration ID based on disk IDs.
+ * Note: it will change after each disk set change.
+ */
+static uint32_t
+jmicron_meta_config_id(struct jmicron_raid_conf *meta)
+{
+ int pos;
+ uint32_t config_id;
+
+ config_id = 0;
+ for (pos = 0; pos < JMICRON_MAX_DISKS; pos++)
+ config_id += meta->disks[pos] << pos;
+ return (config_id);
+}
+
+static void
+jmicron_meta_get_name(struct jmicron_raid_conf *meta, char *buf)
+{
+ int i;
+
+ strncpy(buf, meta->name, 16);
+ buf[16] = 0;
+ for (i = 15; i >= 0; i--) {
+ if (buf[i] > 0x20)
+ break;
+ buf[i] = 0;
+ }
+}
+
+static void
+jmicron_meta_put_name(struct jmicron_raid_conf *meta, char *buf)
+{
+
+ memset(meta->name, 0x20, 16);
+ memcpy(meta->name, buf, MIN(strlen(meta->name), 16));
+}
+
+static int
+jmicron_meta_find_disk(struct jmicron_raid_conf *meta, uint32_t id)
+{
+ int pos;
+
+ id &= JMICRON_DISK_MASK;
+ for (pos = 0; pos < JMICRON_MAX_DISKS; pos++) {
+ if ((meta->disks[pos] & JMICRON_DISK_MASK) == id)
+ return (pos);
+ }
+ for (pos = 0; pos < 2; pos++) {
+ if ((meta->spare[pos] & JMICRON_DISK_MASK) == id)
+ return (-3);
+ }
+ return (-1);
+}
+
+static struct jmicron_raid_conf *
+jmicron_meta_read(struct g_consumer *cp)
+{
+ struct g_provider *pp;
+ struct jmicron_raid_conf *meta;
+ char *buf;
+ int error, i;
+ uint16_t checksum, *ptr;
+
+ pp = cp->provider;
+
+ /* Read the anchor sector. */
+ buf = g_read_data(cp,
+ pp->mediasize - pp->sectorsize, pp->sectorsize, &error);
+ if (buf == NULL) {
+ G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
+ pp->name, error);
+ return (NULL);
+ }
+ meta = (struct jmicron_raid_conf *)buf;
+
+ /* Check if this is an JMicron RAID struct */
+ if (strncmp(meta->signature, JMICRON_MAGIC, strlen(JMICRON_MAGIC))) {
+ G_RAID_DEBUG(1, "JMicron signature check failed on %s", pp->name);
+ g_free(buf);
+ return (NULL);
+ }
+ meta = malloc(sizeof(*meta), M_MD_JMICRON, M_WAITOK);
+ memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
+ g_free(buf);
+
+ /* Check metadata checksum. */
+ for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 64; i++)
+ checksum += *ptr++;
+ if (checksum != 0) {
+ G_RAID_DEBUG(1, "JMicron checksum check failed on %s", pp->name);
+ free(meta, M_MD_JMICRON);
+ return (NULL);
+ }
+
+ return (meta);
+}
+
+static int
+jmicron_meta_write(struct g_consumer *cp, struct jmicron_raid_conf *meta)
+{
+ struct g_provider *pp;
+ char *buf;
+ int error, i;
+ uint16_t checksum, *ptr;
+
+ pp = cp->provider;
+
+ /* Recalculate checksum for case if metadata were changed. */
+ meta->checksum = 0;
+ for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 64; i++)
+ checksum += *ptr++;
+ meta->checksum -= checksum;
+
+ /* Create and fill buffer. */
+ buf = malloc(pp->sectorsize, M_MD_JMICRON, M_WAITOK | M_ZERO);
+ memcpy(buf, meta, sizeof(*meta));
+
+ error = g_write_data(cp,
+ pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
+ if (error != 0) {
+ G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
+ pp->name, error);
+ }
+
+ free(buf, M_MD_JMICRON);
+ return (error);
+}
+
+static int
+jmicron_meta_erase(struct g_consumer *cp)
+{
+ struct g_provider *pp;
+ char *buf;
+ int error;
+
+ pp = cp->provider;
+ buf = malloc(pp->sectorsize, M_MD_JMICRON, M_WAITOK | M_ZERO);
+ error = g_write_data(cp,
+ pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
+ if (error != 0) {
+ G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
+ pp->name, error);
+ }
+ free(buf, M_MD_JMICRON);
+ return (error);
+}
+
+static struct g_raid_disk *
+g_raid_md_jmicron_get_disk(struct g_raid_softc *sc, int id)
+{
+ struct g_raid_disk *disk;
+ struct g_raid_md_jmicron_perdisk *pd;
+
+ TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
+ pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
+ if (pd->pd_disk_pos == id)
+ break;
+ }
+ return (disk);
+}
+
+static int
+g_raid_md_jmicron_supported(int level, int qual, int disks, int force)
+{
+
+ if (disks > 8)
+ return (0);
+ switch (level) {
+ case G_RAID_VOLUME_RL_RAID0:
+ if (disks < 1)
+ return (0);
+ if (!force && (disks < 2 || disks > 6))
+ return (0);
+ break;
+ case G_RAID_VOLUME_RL_RAID1:
+ if (disks < 1)
+ return (0);
+ if (!force && (disks != 2))
+ return (0);
+ break;
+ case G_RAID_VOLUME_RL_RAID1E:
+ if (disks < 2)
+ return (0);
+ if (!force && (disks != 4))
+ return (0);
+ break;
+ case G_RAID_VOLUME_RL_SINGLE:
+ if (disks != 1)
+ return (0);
+ break;
+ case G_RAID_VOLUME_RL_CONCAT:
+ if (disks < 2)
+ return (0);
+ break;
+ case G_RAID_VOLUME_RL_RAID5:
+ if (disks < 3)
+ return (0);
+ if (!force)
+ return (0);
+ break;
+ default:
+ return (0);
+ }
+ if (qual != G_RAID_VOLUME_RLQ_NONE)
+ return (0);
+ return (1);
+}
+
+static int
+g_raid_md_jmicron_start_disk(struct g_raid_disk *disk)
+{
+ struct g_raid_softc *sc;
+ struct g_raid_subdisk *sd, *tmpsd;
+ struct g_raid_disk *olddisk, *tmpdisk;
+ struct g_raid_md_object *md;
+ struct g_raid_md_jmicron_object *mdi;
+ struct g_raid_md_jmicron_perdisk *pd, *oldpd;
+ struct jmicron_raid_conf *meta;
+ int disk_pos, resurrection = 0;
+
+ sc = disk->d_softc;
+ md = sc->sc_md;
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ meta = mdi->mdio_meta;
+ pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
+ olddisk = NULL;
+
+ /* Find disk position in metadata by it's serial. */
+ disk_pos = jmicron_meta_find_disk(meta, pd->pd_disk_id);
+ if (disk_pos < 0) {
+ G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
+ /* If we are in the start process, that's all for now. */
+ if (!mdi->mdio_started)
+ goto nofit;
+ /*
+ * If we have already started - try to get use of the disk.
+ * Try to replace OFFLINE disks first, then FAILED.
+ */
+ TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
+ if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
+ tmpdisk->d_state != G_RAID_DISK_S_FAILED)
+ continue;
+#if 0
+ /* Make sure this disk is big enough. */
+ TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
+ if (sd->sd_offset + sd->sd_size + 4096 >
+ (off_t)pd->pd_disk_meta.sectors * 512) {
+ G_RAID_DEBUG1(1, sc,
+ "Disk too small (%llu < %llu)",
+ ((unsigned long long)
+ pd->pd_disk_meta.sectors) * 512,
+ (unsigned long long)
+ sd->sd_offset + sd->sd_size + 4096);
+ break;
+ }
+ }
+ if (sd != NULL)
+ continue;
+#endif
+ if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
+ olddisk = tmpdisk;
+ break;
+ } else if (olddisk == NULL)
+ olddisk = tmpdisk;
+ }
+ if (olddisk == NULL) {
+nofit:
+ if (disk_pos == -3 || pd->pd_disk_pos == -3) {
+ g_raid_change_disk_state(disk,
+ G_RAID_DISK_S_SPARE);
+ return (1);
+ } else {
+ g_raid_change_disk_state(disk,
+ G_RAID_DISK_S_STALE);
+ return (0);
+ }
+ }
+ oldpd = (struct g_raid_md_jmicron_perdisk *)olddisk->d_md_data;
+ disk_pos = oldpd->pd_disk_pos;
+ resurrection = 1;
+ }
+
+ if (olddisk == NULL) {
+ /* Find placeholder by position. */
+ olddisk = g_raid_md_jmicron_get_disk(sc, disk_pos);
+ if (olddisk == NULL)
+ panic("No disk at position %d!", disk_pos);
+ if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
+ G_RAID_DEBUG1(1, sc, "More then one disk for pos %d",
+ disk_pos);
+ g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
+ return (0);
+ }
+ oldpd = (struct g_raid_md_jmicron_perdisk *)olddisk->d_md_data;
+ }
+
+ /* Replace failed disk or placeholder with new disk. */
+ TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
+ TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
+ TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
+ sd->sd_disk = disk;
+ }
+ oldpd->pd_disk_pos = -2;
+ pd->pd_disk_pos = disk_pos;
+ /* Update global metadata just in case. */
+ meta->disks[disk_pos] = pd->pd_disk_id;
+
+ /* If it was placeholder -- destroy it. */
+ if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
+ g_raid_destroy_disk(olddisk);
+ } else {
+ /* Otherwise, make it STALE_FAILED. */
+ g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
+ }
+
+ /* Welcome the new disk. */
+ g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
+ TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
+
+ /*
+ * Different disks may have different sizes/offsets,
+ * especially in concat mode. Update.
+ */
+ if (pd->pd_meta != NULL && !resurrection) {
+ sd->sd_offset =
+ (off_t)pd->pd_meta->offset * 16 * 512; //ZZZ
+ sd->sd_size =
+ (((off_t)pd->pd_meta->disk_sectors_high << 16) +
+ pd->pd_meta->disk_sectors_low) * 512;
+ }
+
+ if (resurrection) {
+ /* Stale disk, almost same as new. */
+ g_raid_change_subdisk_state(sd,
+ G_RAID_SUBDISK_S_NEW);
+ } else if ((meta->flags & JMICRON_F_BADSEC) != 0 &&
+ (pd->pd_meta->flags & JMICRON_F_BADSEC) == 0) {
+ /* Cold-inserted or rebuilding disk. */
+ g_raid_change_subdisk_state(sd,
+ G_RAID_SUBDISK_S_NEW);
+ } else if (pd->pd_meta->flags & JMICRON_F_UNSYNC) {
+ /* Dirty or resyncing disk.. */
+ g_raid_change_subdisk_state(sd,
+ G_RAID_SUBDISK_S_STALE);
+ } else {
+ /* Up to date disk. */
+ g_raid_change_subdisk_state(sd,
+ G_RAID_SUBDISK_S_ACTIVE);
+ }
+ g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
+ G_RAID_EVENT_SUBDISK);
+ }
+
+ /* Update status of our need for spare. */
+ if (mdi->mdio_started) {
+ mdi->mdio_incomplete =
+ (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
+ mdi->mdio_total_disks);
+ }
+
+ return (resurrection);
+}
+
+static void
+g_disk_md_jmicron_retaste(void *arg, int pending)
+{
+
+ G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
+ g_retaste(&g_raid_class);
+ free(arg, M_MD_JMICRON);
+}
+
+static void
+g_raid_md_jmicron_refill(struct g_raid_softc *sc)
+{
+ struct g_raid_md_object *md;
+ struct g_raid_md_jmicron_object *mdi;
+ struct jmicron_raid_conf *meta;
+ struct g_raid_disk *disk;
+ struct task *task;
+ int update, na;
+
+ md = sc->sc_md;
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ meta = mdi->mdio_meta;
+ update = 0;
+ do {
+ /* Make sure we miss anything. */
+ na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
+ if (na == mdi->mdio_total_disks)
+ break;
+
+ G_RAID_DEBUG1(1, md->mdo_softc,
+ "Array is not complete (%d of %d), "
+ "trying to refill.", na, mdi->mdio_total_disks);
+
+ /* Try to get use some of STALE disks. */
+ TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
+ if (disk->d_state == G_RAID_DISK_S_STALE) {
+ update += g_raid_md_jmicron_start_disk(disk);
+ if (disk->d_state == G_RAID_DISK_S_ACTIVE)
+ break;
+ }
+ }
+ if (disk != NULL)
+ continue;
+
+ /* Try to get use some of SPARE disks. */
+ TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
+ if (disk->d_state == G_RAID_DISK_S_SPARE) {
+ update += g_raid_md_jmicron_start_disk(disk);
+ if (disk->d_state == G_RAID_DISK_S_ACTIVE)
+ break;
+ }
+ }
+ } while (disk != NULL);
+
+ /* Write new metadata if we changed something. */
+ if (update) {
+ g_raid_md_write_jmicron(md, NULL, NULL, NULL);
+ meta = mdi->mdio_meta;
+ }
+
+ /* Update status of our need for spare. */
+ mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
+ mdi->mdio_total_disks);
+
+ /* Request retaste hoping to find spare. */
+ if (mdi->mdio_incomplete) {
+ task = malloc(sizeof(struct task),
+ M_MD_JMICRON, M_WAITOK | M_ZERO);
+ TASK_INIT(task, 0, g_disk_md_jmicron_retaste, task);
+ taskqueue_enqueue(taskqueue_swi, task);
+ }
+}
+
+static void
+g_raid_md_jmicron_start(struct g_raid_softc *sc)
+{
+ struct g_raid_md_object *md;
+ struct g_raid_md_jmicron_object *mdi;
+ struct g_raid_md_jmicron_perdisk *pd;
+ struct jmicron_raid_conf *meta;
+ struct g_raid_volume *vol;
+ struct g_raid_subdisk *sd;
+ struct g_raid_disk *disk;
+ off_t size;
+ int j, disk_pos;
+ char buf[17];
+
+ md = sc->sc_md;
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ meta = mdi->mdio_meta;
+
+ /* Create volumes and subdisks. */
+ jmicron_meta_get_name(meta, buf);
+ vol = g_raid_create_volume(sc, buf);
+ size = ((off_t)meta->disk_sectors_high << 16) + meta->disk_sectors_low;
+ size *= 512; //ZZZ
+ if (meta->type == JMICRON_T_RAID0) {
+ vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
+ vol->v_mediasize = size * mdi->mdio_total_disks;
+ } else if (meta->type == JMICRON_T_RAID1) {
+ vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
+ vol->v_mediasize = size;
+ } else if (meta->type == JMICRON_T_RAID01) {
+ vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
+ vol->v_mediasize = size * mdi->mdio_total_disks / 2;
+ } else if (meta->type == JMICRON_T_CONCAT) {
+ vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
+ vol->v_mediasize = 0;
+ } else if (meta->type == JMICRON_T_RAID5) {
+ vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
+ vol->v_mediasize = size * (mdi->mdio_total_disks - 1);
+ } else {
+ vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
+ vol->v_mediasize = 0;
+ }
+ vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
+ vol->v_strip_size = 1024 << meta->stripe_shift; //ZZZ
+ vol->v_disks_count = mdi->mdio_total_disks;
+ vol->v_sectorsize = 512; //ZZZ
+ for (j = 0; j < vol->v_disks_count; j++) {
+ sd = &vol->v_subdisks[j];
+ sd->sd_offset = (off_t)meta->offset * 16 * 512; //ZZZ
+ sd->sd_size = size;
+ }
+ g_raid_start_volume(vol);
+
+ /* Create disk placeholders to store data for later writing. */
+ for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
+ pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
+ pd->pd_disk_pos = disk_pos;
+ pd->pd_disk_id = meta->disks[disk_pos];
+ disk = g_raid_create_disk(sc);
+ disk->d_md_data = (void *)pd;
+ disk->d_state = G_RAID_DISK_S_OFFLINE;
+ sd = &vol->v_subdisks[disk_pos];
+ sd->sd_disk = disk;
+ TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
+ }
+
+ /* Make all disks found till the moment take their places. */
+ do {
+ TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
+ if (disk->d_state == G_RAID_DISK_S_NONE) {
+ g_raid_md_jmicron_start_disk(disk);
+ break;
+ }
+ }
+ } while (disk != NULL);
+
+ mdi->mdio_started = 1;
+ G_RAID_DEBUG1(0, sc, "Array started.");
+ g_raid_md_write_jmicron(md, NULL, NULL, NULL);
+
+ /* Pickup any STALE/SPARE disks to refill array if needed. */
+ g_raid_md_jmicron_refill(sc);
+
+ g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
+
+ callout_stop(&mdi->mdio_start_co);
+ G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
+ root_mount_rel(mdi->mdio_rootmount);
+ mdi->mdio_rootmount = NULL;
+}
+
+static void
+g_raid_md_jmicron_new_disk(struct g_raid_disk *disk)
+{
+ struct g_raid_softc *sc;
+ struct g_raid_md_object *md;
+ struct g_raid_md_jmicron_object *mdi;
+ struct jmicron_raid_conf *pdmeta;
+ struct g_raid_md_jmicron_perdisk *pd;
+
+ sc = disk->d_softc;
+ md = sc->sc_md;
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
+ pdmeta = pd->pd_meta;
+
+ if (mdi->mdio_started) {
+ if (g_raid_md_jmicron_start_disk(disk))
+ g_raid_md_write_jmicron(md, NULL, NULL, NULL);
+ } else {
+ /* If we haven't started yet - check metadata freshness. */
+ if (mdi->mdio_meta == NULL) {
+ G_RAID_DEBUG1(1, sc, "Newer disk");
+ if (mdi->mdio_meta != NULL)
+ free(mdi->mdio_meta, M_MD_JMICRON);
+ mdi->mdio_meta = jmicron_meta_copy(pdmeta);
+ mdi->mdio_total_disks = jmicron_meta_total_disks(pdmeta);
+ mdi->mdio_disks_present = 1;
+ } else {
+ mdi->mdio_disks_present++;
+ G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d+%d up)",
+ mdi->mdio_disks_present,
+ mdi->mdio_total_disks,
+ jmicron_meta_total_spare(mdi->mdio_meta));
+ mdi->mdio_meta->flags |=
+ pdmeta->flags & JMICRON_F_BADSEC;
+ }
+ /* If we collected all needed disks - start array. */
+ if (mdi->mdio_disks_present == mdi->mdio_total_disks +
+ jmicron_meta_total_spare(mdi->mdio_meta))
+ g_raid_md_jmicron_start(sc);
+ }
+}
+
+static void
+g_raid_jmicron_go(void *arg)
+{
+ struct g_raid_softc *sc;
+ struct g_raid_md_object *md;
+ struct g_raid_md_jmicron_object *mdi;
+
+ sc = arg;
+ md = sc->sc_md;
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ sx_xlock(&sc->sc_lock);
+ if (!mdi->mdio_started) {
+ G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
+ g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
+ }
+ sx_xunlock(&sc->sc_lock);
+}
+
+static int
+g_raid_md_create_jmicron(struct g_raid_md_object *md, struct g_class *mp,
+ struct g_geom **gp)
+{
+ struct g_raid_softc *sc;
+ struct g_raid_md_jmicron_object *mdi;
+ char name[16];
+
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ mdi->mdio_config_id = arc4random();
+ snprintf(name, sizeof(name), "JMicron-%08x", mdi->mdio_config_id);
+ sc = g_raid_create_node(mp, name, md);
+ if (sc == NULL)
+ return (G_RAID_MD_TASTE_FAIL);
+ md->mdo_softc = sc;
+ *gp = sc->sc_geom;
+ return (G_RAID_MD_TASTE_NEW);
+}
+
+static int
+g_raid_md_taste_jmicron(struct g_raid_md_object *md, struct g_class *mp,
+ struct g_consumer *cp, struct g_geom **gp)
+{
+ struct g_consumer *rcp;
+ struct g_provider *pp;
+ struct g_raid_md_jmicron_object *mdi, *mdi1;
+ struct g_raid_softc *sc;
+ struct g_raid_disk *disk;
+ struct jmicron_raid_conf *meta;
+ struct g_raid_md_jmicron_perdisk *pd;
+ struct g_geom *geom;
+ int error, disk_pos, result, spare, len;
+ char name[16];
+ uint16_t vendor;
+
+ G_RAID_DEBUG(1, "Tasting JMicron on %s", cp->provider->name);
+ mdi = (struct g_raid_md_jmicron_object *)md;
+ pp = cp->provider;
+
+ /* Read metadata from device. */
+ meta = NULL;
+ spare = 0;
+ vendor = 0xffff;
+ disk_pos = 0;
+ if (g_access(cp, 1, 0, 0) != 0)
+ return (G_RAID_MD_TASTE_FAIL);
+ g_topology_unlock();
+ len = 2;
+ if (pp->geom->rank == 1)
+ g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
+ meta = jmicron_meta_read(cp);
+ g_topology_lock();
+ g_access(cp, -1, 0, 0);
+ if (meta == NULL) {
+ if (g_raid_aggressive_spare) {
+ if (vendor == 0x197b) {
+ G_RAID_DEBUG(1,
+ "No JMicron metadata, forcing spare.");
+ spare = 2;
+ goto search;
+ } else {
+ G_RAID_DEBUG(1,
+ "JMicron vendor mismatch 0x%04x != 0x197b",
+ vendor);
+ }
+ }
+ return (G_RAID_MD_TASTE_FAIL);
+ }
+
+ /* Check this disk position in obtained metadata. */
+ disk_pos = jmicron_meta_find_disk(meta, meta->disk_id);
+ if (disk_pos == -1) {
+ G_RAID_DEBUG(1, "JMicron disk_id %08x not found",
+ meta->disk_id);
+ goto fail1;
+ }
+
+ /* Metadata valid. Print it. */
+ g_raid_md_jmicron_print(meta);
+ G_RAID_DEBUG(1, "JMicron disk position %d", disk_pos);
+ spare = (disk_pos == -2) ? 1 : 0;
+
+search:
+ /* Search for matching node. */
+ sc = NULL;
+ mdi1 = NULL;
+ LIST_FOREACH(geom, &mp->geom, geom) {
+ sc = geom->softc;
+ if (sc == NULL)
+ continue;
+ if (sc->sc_stopping != 0)
+ continue;
+ if (sc->sc_md->mdo_class != md->mdo_class)
+ continue;
+ mdi1 = (struct g_raid_md_jmicron_object *)sc->sc_md;
+ if (spare == 2) {
+ if (mdi1->mdio_incomplete)
+ break;
+ } else {
+ if (mdi1->mdio_config_id ==
+ jmicron_meta_config_id(meta))
+ break;
+ }
+ }
+
+ /* Found matching node. */
+ if (geom != NULL) {
+ G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
+ result = G_RAID_MD_TASTE_EXISTING;
+
+ } else if (spare) { /* Not found needy node -- left for later. */
+ G_RAID_DEBUG(1, "Spare is not needed at this time");
+ goto fail1;
+
+ } else { /* Not found matching node -- create one. */
+ result = G_RAID_MD_TASTE_NEW;
+ mdi->mdio_config_id = jmicron_meta_config_id(meta);
+ snprintf(name, sizeof(name), "JMicron-%08x",
+ mdi->mdio_config_id);
+ sc = g_raid_create_node(mp, name, md);
+ md->mdo_softc = sc;
+ geom = sc->sc_geom;
+ callout_init(&mdi->mdio_start_co, 1);
+ callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
+ g_raid_jmicron_go, sc);
+ mdi->mdio_rootmount = root_mount_hold("GRAID-JMicron");
+ G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
+ }
+
+ rcp = g_new_consumer(geom);
+ g_attach(rcp, pp);
+ if (g_access(rcp, 1, 1, 1) != 0)
+ ; //goto fail1;
+
+ g_topology_unlock();
+ sx_xlock(&sc->sc_lock);
+
+ pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
+ pd->pd_meta = meta;
+ if (spare == 2) {
+// pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
+ pd->pd_disk_pos = -3;
+ pd->pd_disk_id = arc4random() & JMICRON_DISK_MASK;
+ } else {
+ pd->pd_disk_pos = -1;
+ pd->pd_disk_id = meta->disk_id;
+ }
+ disk = g_raid_create_disk(sc);
+ disk->d_md_data = (void *)pd;
+ disk->d_consumer = rcp;
+ rcp->private = disk;
+
+ /* Read kernel dumping information. */
+ disk->d_kd.offset = 0;
+ disk->d_kd.length = OFF_MAX;
+ len = sizeof(disk->d_kd);
+ error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
+ if (disk->d_kd.di.dumper == NULL)
+ G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
+ rcp->provider->name, error);
+
+ g_raid_md_jmicron_new_disk(disk);
+
+ sx_xunlock(&sc->sc_lock);
+ g_topology_lock();
+ *gp = geom;
+ return (result);
+fail1:
+ free(meta, M_MD_JMICRON);
+ return (G_RAID_MD_TASTE_FAIL);
+}
+
*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
More information about the svn-src-projects
mailing list