2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5 * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
43 #include <sys/taskqueue.h>
44 #include <geom/geom.h>
45 #include <geom/geom_dbg.h>
46 #include "geom/raid/g_raid.h"
47 #include "g_raid_md_if.h"
49 static MALLOC_DEFINE(M_MD_JMICRON, "md_jmicron_data", "GEOM_RAID JMicron metadata");
51 #define JMICRON_MAX_DISKS 8
52 #define JMICRON_MAX_SPARE 2
54 struct jmicron_raid_conf {
55 u_int8_t signature[2];
56 #define JMICRON_MAGIC "JM"
59 #define JMICRON_VERSION 0x0001
62 u_int8_t filler_1[10];
65 u_int32_t disk_sectors_high;
66 u_int16_t disk_sectors_low;
70 #define JMICRON_T_RAID0 0
71 #define JMICRON_T_RAID1 1
72 #define JMICRON_T_RAID01 2
73 #define JMICRON_T_CONCAT 3
74 #define JMICRON_T_RAID5 5
76 u_int8_t stripe_shift;
78 #define JMICRON_F_READY 0x0001
79 #define JMICRON_F_BOOTABLE 0x0002
80 #define JMICRON_F_BADSEC 0x0004
81 #define JMICRON_F_ACTIVE 0x0010
82 #define JMICRON_F_UNSYNC 0x0020
83 #define JMICRON_F_NEWEST 0x0040
86 u_int32_t spare[JMICRON_MAX_SPARE];
87 u_int32_t disks[JMICRON_MAX_DISKS];
88 #define JMICRON_DISK_MASK 0xFFFFFFF0
89 #define JMICRON_SEG_MASK 0x0000000F
90 u_int8_t filler_4[32];
91 u_int8_t filler_5[384];
94 struct g_raid_md_jmicron_perdisk {
95 struct jmicron_raid_conf *pd_meta;
101 struct g_raid_md_jmicron_object {
102 struct g_raid_md_object mdio_base;
103 uint32_t mdio_config_id;
104 struct jmicron_raid_conf *mdio_meta;
105 struct callout mdio_start_co; /* STARTING state timer. */
106 int mdio_total_disks;
107 int mdio_disks_present;
110 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
113 static g_raid_md_create_t g_raid_md_create_jmicron;
114 static g_raid_md_taste_t g_raid_md_taste_jmicron;
115 static g_raid_md_event_t g_raid_md_event_jmicron;
116 static g_raid_md_ctl_t g_raid_md_ctl_jmicron;
117 static g_raid_md_write_t g_raid_md_write_jmicron;
118 static g_raid_md_fail_disk_t g_raid_md_fail_disk_jmicron;
119 static g_raid_md_free_disk_t g_raid_md_free_disk_jmicron;
120 static g_raid_md_free_t g_raid_md_free_jmicron;
122 static kobj_method_t g_raid_md_jmicron_methods[] = {
123 KOBJMETHOD(g_raid_md_create, g_raid_md_create_jmicron),
124 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_jmicron),
125 KOBJMETHOD(g_raid_md_event, g_raid_md_event_jmicron),
126 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_jmicron),
127 KOBJMETHOD(g_raid_md_write, g_raid_md_write_jmicron),
128 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_jmicron),
129 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_jmicron),
130 KOBJMETHOD(g_raid_md_free, g_raid_md_free_jmicron),
134 static struct g_raid_md_class g_raid_md_jmicron_class = {
136 g_raid_md_jmicron_methods,
137 sizeof(struct g_raid_md_jmicron_object),
143 g_raid_md_jmicron_print(struct jmicron_raid_conf *meta)
147 if (g_raid_debug < 1)
150 printf("********* ATA JMicron RAID Metadata *********\n");
151 printf("signature <%c%c>\n", meta->signature[0], meta->signature[1]);
152 printf("version %04x\n", meta->version);
153 printf("checksum 0x%04x\n", meta->checksum);
154 printf("disk_id 0x%08x\n", meta->disk_id);
155 printf("offset 0x%08x\n", meta->offset);
156 printf("disk_sectors_high 0x%08x\n", meta->disk_sectors_high);
157 printf("disk_sectors_low 0x%04x\n", meta->disk_sectors_low);
158 printf("name <%.16s>\n", meta->name);
159 printf("type %d\n", meta->type);
160 printf("stripe_shift %d\n", meta->stripe_shift);
161 printf("flags %04x\n", meta->flags);
163 for (k = 0; k < JMICRON_MAX_SPARE; k++)
164 printf(" 0x%08x", meta->spare[k]);
167 for (k = 0; k < JMICRON_MAX_DISKS; k++)
168 printf(" 0x%08x", meta->disks[k]);
170 printf("=================================================\n");
173 static struct jmicron_raid_conf *
174 jmicron_meta_copy(struct jmicron_raid_conf *meta)
176 struct jmicron_raid_conf *nmeta;
178 nmeta = malloc(sizeof(*meta), M_MD_JMICRON, M_WAITOK);
179 memcpy(nmeta, meta, sizeof(*meta));
184 jmicron_meta_total_disks(struct jmicron_raid_conf *meta)
188 for (pos = 0; pos < JMICRON_MAX_DISKS; pos++) {
189 if (meta->disks[pos] == 0)
196 jmicron_meta_total_spare(struct jmicron_raid_conf *meta)
201 for (pos = 0; pos < JMICRON_MAX_SPARE; pos++) {
202 if (meta->spare[pos] != 0)
209 * Generate fake Configuration ID based on disk IDs.
210 * Note: it will change after each disk set change.
213 jmicron_meta_config_id(struct jmicron_raid_conf *meta)
219 for (pos = 0; pos < JMICRON_MAX_DISKS; pos++)
220 config_id += meta->disks[pos] << pos;
225 jmicron_meta_get_name(struct jmicron_raid_conf *meta, char *buf)
229 strncpy(buf, meta->name, 16);
231 for (i = 15; i >= 0; i--) {
239 jmicron_meta_put_name(struct jmicron_raid_conf *meta, char *buf)
242 memset(meta->name, 0x20, 16);
243 memcpy(meta->name, buf, MIN(strlen(buf), 16));
247 jmicron_meta_find_disk(struct jmicron_raid_conf *meta, uint32_t id)
251 id &= JMICRON_DISK_MASK;
252 for (pos = 0; pos < JMICRON_MAX_DISKS; pos++) {
253 if ((meta->disks[pos] & JMICRON_DISK_MASK) == id)
256 for (pos = 0; pos < JMICRON_MAX_SPARE; pos++) {
257 if ((meta->spare[pos] & JMICRON_DISK_MASK) == id)
263 static struct jmicron_raid_conf *
264 jmicron_meta_read(struct g_consumer *cp)
266 struct g_provider *pp;
267 struct jmicron_raid_conf *meta;
270 uint16_t checksum, *ptr;
274 /* Read the anchor sector. */
275 buf = g_read_data(cp,
276 pp->mediasize - pp->sectorsize, pp->sectorsize, &error);
278 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
282 meta = (struct jmicron_raid_conf *)buf;
284 /* Check if this is an JMicron RAID struct */
285 if (strncmp(meta->signature, JMICRON_MAGIC, strlen(JMICRON_MAGIC))) {
286 G_RAID_DEBUG(1, "JMicron signature check failed on %s", pp->name);
290 meta = malloc(sizeof(*meta), M_MD_JMICRON, M_WAITOK);
291 memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
294 /* Check metadata checksum. */
295 for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 64; i++)
298 G_RAID_DEBUG(1, "JMicron checksum check failed on %s", pp->name);
299 free(meta, M_MD_JMICRON);
307 jmicron_meta_write(struct g_consumer *cp, struct jmicron_raid_conf *meta)
309 struct g_provider *pp;
312 uint16_t checksum, *ptr;
316 /* Recalculate checksum for case if metadata were changed. */
318 for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 64; i++)
320 meta->checksum -= checksum;
322 /* Create and fill buffer. */
323 buf = malloc(pp->sectorsize, M_MD_JMICRON, M_WAITOK | M_ZERO);
324 memcpy(buf, meta, sizeof(*meta));
326 error = g_write_data(cp,
327 pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
329 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
333 free(buf, M_MD_JMICRON);
338 jmicron_meta_erase(struct g_consumer *cp)
340 struct g_provider *pp;
345 buf = malloc(pp->sectorsize, M_MD_JMICRON, M_WAITOK | M_ZERO);
346 error = g_write_data(cp,
347 pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
349 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
352 free(buf, M_MD_JMICRON);
356 static struct g_raid_disk *
357 g_raid_md_jmicron_get_disk(struct g_raid_softc *sc, int id)
359 struct g_raid_disk *disk;
360 struct g_raid_md_jmicron_perdisk *pd;
362 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
363 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
364 if (pd->pd_disk_pos == id)
371 g_raid_md_jmicron_supported(int level, int qual, int disks, int force)
377 case G_RAID_VOLUME_RL_RAID0:
380 if (!force && (disks < 2 || disks > 6))
383 case G_RAID_VOLUME_RL_RAID1:
386 if (!force && (disks != 2))
389 case G_RAID_VOLUME_RL_RAID1E:
392 if (!force && (disks != 4))
395 case G_RAID_VOLUME_RL_SINGLE:
401 case G_RAID_VOLUME_RL_CONCAT:
405 case G_RAID_VOLUME_RL_RAID5:
408 if (qual != G_RAID_VOLUME_RLQ_R5LA)
416 if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
422 g_raid_md_jmicron_start_disk(struct g_raid_disk *disk)
424 struct g_raid_softc *sc;
425 struct g_raid_subdisk *sd, *tmpsd;
426 struct g_raid_disk *olddisk, *tmpdisk;
427 struct g_raid_md_object *md;
428 struct g_raid_md_jmicron_object *mdi;
429 struct g_raid_md_jmicron_perdisk *pd, *oldpd;
430 struct jmicron_raid_conf *meta;
431 int disk_pos, resurrection = 0;
435 mdi = (struct g_raid_md_jmicron_object *)md;
436 meta = mdi->mdio_meta;
437 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
440 /* Find disk position in metadata by its serial. */
441 if (pd->pd_meta != NULL)
442 disk_pos = jmicron_meta_find_disk(meta, pd->pd_disk_id);
446 G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
447 /* If we are in the start process, that's all for now. */
448 if (!mdi->mdio_started)
451 * If we have already started - try to get use of the disk.
452 * Try to replace OFFLINE disks first, then FAILED.
454 TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
455 if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
456 tmpdisk->d_state != G_RAID_DISK_S_FAILED)
458 /* Make sure this disk is big enough. */
459 TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
460 if (sd->sd_offset + sd->sd_size + 512 >
463 "Disk too small (%ju < %ju)",
465 sd->sd_offset + sd->sd_size + 512);
471 if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
474 } else if (olddisk == NULL)
477 if (olddisk == NULL) {
479 if (disk_pos == -3 || pd->pd_disk_pos == -3) {
480 g_raid_change_disk_state(disk,
481 G_RAID_DISK_S_SPARE);
484 g_raid_change_disk_state(disk,
485 G_RAID_DISK_S_STALE);
489 oldpd = (struct g_raid_md_jmicron_perdisk *)olddisk->d_md_data;
490 disk_pos = oldpd->pd_disk_pos;
494 if (olddisk == NULL) {
495 /* Find placeholder by position. */
496 olddisk = g_raid_md_jmicron_get_disk(sc, disk_pos);
498 panic("No disk at position %d!", disk_pos);
499 if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
500 G_RAID_DEBUG1(1, sc, "More than one disk for pos %d",
502 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
505 oldpd = (struct g_raid_md_jmicron_perdisk *)olddisk->d_md_data;
508 /* Replace failed disk or placeholder with new disk. */
509 TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
510 TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
511 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
514 oldpd->pd_disk_pos = -2;
515 pd->pd_disk_pos = disk_pos;
516 /* Update global metadata just in case. */
517 meta->disks[disk_pos] = pd->pd_disk_id;
519 /* If it was placeholder -- destroy it. */
520 if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
521 g_raid_destroy_disk(olddisk);
523 /* Otherwise, make it STALE_FAILED. */
524 g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
527 /* Welcome the new disk. */
528 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
529 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
531 * Different disks may have different sizes/offsets,
532 * especially in concat mode. Update.
536 (off_t)pd->pd_meta->offset * 16 * 512; //ZZZ
538 (((off_t)pd->pd_meta->disk_sectors_high << 16) +
539 pd->pd_meta->disk_sectors_low) * 512;
543 /* Stale disk, almost same as new. */
544 g_raid_change_subdisk_state(sd,
545 G_RAID_SUBDISK_S_NEW);
546 } else if ((meta->flags & JMICRON_F_BADSEC) != 0 &&
547 (pd->pd_meta->flags & JMICRON_F_BADSEC) == 0) {
548 /* Cold-inserted or rebuilding disk. */
549 g_raid_change_subdisk_state(sd,
550 G_RAID_SUBDISK_S_NEW);
551 } else if (pd->pd_meta->flags & JMICRON_F_UNSYNC) {
552 /* Dirty or resyncing disk.. */
553 g_raid_change_subdisk_state(sd,
554 G_RAID_SUBDISK_S_STALE);
556 /* Up to date disk. */
557 g_raid_change_subdisk_state(sd,
558 G_RAID_SUBDISK_S_ACTIVE);
560 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
561 G_RAID_EVENT_SUBDISK);
564 /* Update status of our need for spare. */
565 if (mdi->mdio_started) {
566 mdi->mdio_incomplete =
567 (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
568 mdi->mdio_total_disks);
571 return (resurrection);
575 g_disk_md_jmicron_retaste(void *arg, int pending)
578 G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
579 g_retaste(&g_raid_class);
580 free(arg, M_MD_JMICRON);
584 g_raid_md_jmicron_refill(struct g_raid_softc *sc)
586 struct g_raid_md_object *md;
587 struct g_raid_md_jmicron_object *mdi;
588 struct g_raid_disk *disk;
593 mdi = (struct g_raid_md_jmicron_object *)md;
596 /* Make sure we miss anything. */
597 na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
598 if (na == mdi->mdio_total_disks)
601 G_RAID_DEBUG1(1, md->mdo_softc,
602 "Array is not complete (%d of %d), "
603 "trying to refill.", na, mdi->mdio_total_disks);
605 /* Try to get use some of STALE disks. */
606 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
607 if (disk->d_state == G_RAID_DISK_S_STALE) {
608 update += g_raid_md_jmicron_start_disk(disk);
609 if (disk->d_state == G_RAID_DISK_S_ACTIVE)
616 /* Try to get use some of SPARE disks. */
617 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
618 if (disk->d_state == G_RAID_DISK_S_SPARE) {
619 update += g_raid_md_jmicron_start_disk(disk);
620 if (disk->d_state == G_RAID_DISK_S_ACTIVE)
624 } while (disk != NULL);
626 /* Write new metadata if we changed something. */
628 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
630 /* Update status of our need for spare. */
631 mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
632 mdi->mdio_total_disks);
634 /* Request retaste hoping to find spare. */
635 if (mdi->mdio_incomplete) {
636 task = malloc(sizeof(struct task),
637 M_MD_JMICRON, M_WAITOK | M_ZERO);
638 TASK_INIT(task, 0, g_disk_md_jmicron_retaste, task);
639 taskqueue_enqueue(taskqueue_swi, task);
644 g_raid_md_jmicron_start(struct g_raid_softc *sc)
646 struct g_raid_md_object *md;
647 struct g_raid_md_jmicron_object *mdi;
648 struct g_raid_md_jmicron_perdisk *pd;
649 struct jmicron_raid_conf *meta;
650 struct g_raid_volume *vol;
651 struct g_raid_subdisk *sd;
652 struct g_raid_disk *disk;
658 mdi = (struct g_raid_md_jmicron_object *)md;
659 meta = mdi->mdio_meta;
661 /* Create volumes and subdisks. */
662 jmicron_meta_get_name(meta, buf);
663 vol = g_raid_create_volume(sc, buf, -1);
664 size = ((off_t)meta->disk_sectors_high << 16) + meta->disk_sectors_low;
666 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
667 if (meta->type == JMICRON_T_RAID0) {
668 vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
669 vol->v_mediasize = size * mdi->mdio_total_disks;
670 } else if (meta->type == JMICRON_T_RAID1) {
671 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
672 vol->v_mediasize = size;
673 } else if (meta->type == JMICRON_T_RAID01) {
674 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
675 vol->v_mediasize = size * mdi->mdio_total_disks / 2;
676 } else if (meta->type == JMICRON_T_CONCAT) {
677 if (mdi->mdio_total_disks == 1)
678 vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
680 vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
681 vol->v_mediasize = 0;
682 } else if (meta->type == JMICRON_T_RAID5) {
683 vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
684 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
685 vol->v_mediasize = size * (mdi->mdio_total_disks - 1);
687 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
688 vol->v_mediasize = 0;
690 vol->v_strip_size = 1024 << meta->stripe_shift; //ZZZ
691 vol->v_disks_count = mdi->mdio_total_disks;
692 vol->v_sectorsize = 512; //ZZZ
693 for (j = 0; j < vol->v_disks_count; j++) {
694 sd = &vol->v_subdisks[j];
695 sd->sd_offset = (off_t)meta->offset * 16 * 512; //ZZZ
698 g_raid_start_volume(vol);
700 /* Create disk placeholders to store data for later writing. */
701 for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
702 pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
703 pd->pd_disk_pos = disk_pos;
704 pd->pd_disk_id = meta->disks[disk_pos];
705 disk = g_raid_create_disk(sc);
706 disk->d_md_data = (void *)pd;
707 disk->d_state = G_RAID_DISK_S_OFFLINE;
708 sd = &vol->v_subdisks[disk_pos];
710 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
713 /* Make all disks found till the moment take their places. */
715 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
716 if (disk->d_state == G_RAID_DISK_S_NONE) {
717 g_raid_md_jmicron_start_disk(disk);
721 } while (disk != NULL);
723 mdi->mdio_started = 1;
724 G_RAID_DEBUG1(0, sc, "Array started.");
725 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
727 /* Pickup any STALE/SPARE disks to refill array if needed. */
728 g_raid_md_jmicron_refill(sc);
730 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
732 callout_stop(&mdi->mdio_start_co);
733 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
734 root_mount_rel(mdi->mdio_rootmount);
735 mdi->mdio_rootmount = NULL;
739 g_raid_md_jmicron_new_disk(struct g_raid_disk *disk)
741 struct g_raid_softc *sc;
742 struct g_raid_md_object *md;
743 struct g_raid_md_jmicron_object *mdi;
744 struct jmicron_raid_conf *pdmeta;
745 struct g_raid_md_jmicron_perdisk *pd;
749 mdi = (struct g_raid_md_jmicron_object *)md;
750 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
751 pdmeta = pd->pd_meta;
753 if (mdi->mdio_started) {
754 if (g_raid_md_jmicron_start_disk(disk))
755 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
758 * If we haven't started yet - update common metadata
759 * to get subdisks details, avoiding data from spare disks.
761 if (mdi->mdio_meta == NULL ||
762 jmicron_meta_find_disk(mdi->mdio_meta,
763 mdi->mdio_meta->disk_id) == -3) {
764 if (mdi->mdio_meta != NULL)
765 free(mdi->mdio_meta, M_MD_JMICRON);
766 mdi->mdio_meta = jmicron_meta_copy(pdmeta);
767 mdi->mdio_total_disks = jmicron_meta_total_disks(pdmeta);
769 mdi->mdio_meta->flags |= pdmeta->flags & JMICRON_F_BADSEC;
771 mdi->mdio_disks_present++;
772 G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d+%d up)",
773 mdi->mdio_disks_present,
774 mdi->mdio_total_disks,
775 jmicron_meta_total_spare(mdi->mdio_meta));
777 /* If we collected all needed disks - start array. */
778 if (mdi->mdio_disks_present == mdi->mdio_total_disks +
779 jmicron_meta_total_spare(mdi->mdio_meta))
780 g_raid_md_jmicron_start(sc);
785 g_raid_jmicron_go(void *arg)
787 struct g_raid_softc *sc;
788 struct g_raid_md_object *md;
789 struct g_raid_md_jmicron_object *mdi;
793 mdi = (struct g_raid_md_jmicron_object *)md;
794 if (!mdi->mdio_started) {
795 G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
796 g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
801 g_raid_md_create_jmicron(struct g_raid_md_object *md, struct g_class *mp,
804 struct g_raid_softc *sc;
805 struct g_raid_md_jmicron_object *mdi;
808 mdi = (struct g_raid_md_jmicron_object *)md;
809 mdi->mdio_config_id = arc4random();
810 snprintf(name, sizeof(name), "JMicron-%08x", mdi->mdio_config_id);
811 sc = g_raid_create_node(mp, name, md);
813 return (G_RAID_MD_TASTE_FAIL);
816 return (G_RAID_MD_TASTE_NEW);
820 g_raid_md_taste_jmicron(struct g_raid_md_object *md, struct g_class *mp,
821 struct g_consumer *cp, struct g_geom **gp)
823 struct g_consumer *rcp;
824 struct g_provider *pp;
825 struct g_raid_md_jmicron_object *mdi, *mdi1;
826 struct g_raid_softc *sc;
827 struct g_raid_disk *disk;
828 struct jmicron_raid_conf *meta;
829 struct g_raid_md_jmicron_perdisk *pd;
831 int disk_pos, result, spare, len;
835 G_RAID_DEBUG(1, "Tasting JMicron on %s", cp->provider->name);
836 mdi = (struct g_raid_md_jmicron_object *)md;
839 /* Read metadata from device. */
843 len = sizeof(vendor);
844 if (pp->geom->rank == 1)
845 g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
846 meta = jmicron_meta_read(cp);
849 if (g_raid_aggressive_spare) {
850 if (vendor == 0x197b) {
852 "No JMicron metadata, forcing spare.");
857 "JMicron vendor mismatch 0x%04x != 0x197b",
861 return (G_RAID_MD_TASTE_FAIL);
864 /* Check this disk position in obtained metadata. */
865 disk_pos = jmicron_meta_find_disk(meta, meta->disk_id);
866 if (disk_pos == -1) {
867 G_RAID_DEBUG(1, "JMicron disk_id %08x not found",
872 /* Metadata valid. Print it. */
873 g_raid_md_jmicron_print(meta);
874 G_RAID_DEBUG(1, "JMicron disk position %d", disk_pos);
875 spare = (disk_pos == -2) ? 1 : 0;
878 /* Search for matching node. */
881 LIST_FOREACH(geom, &mp->geom, geom) {
885 if (sc->sc_stopping != 0)
887 if (sc->sc_md->mdo_class != md->mdo_class)
889 mdi1 = (struct g_raid_md_jmicron_object *)sc->sc_md;
891 if (mdi1->mdio_incomplete)
894 if (mdi1->mdio_config_id ==
895 jmicron_meta_config_id(meta))
900 /* Found matching node. */
902 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
903 result = G_RAID_MD_TASTE_EXISTING;
905 } else if (spare) { /* Not found needy node -- left for later. */
906 G_RAID_DEBUG(1, "Spare is not needed at this time");
909 } else { /* Not found matching node -- create one. */
910 result = G_RAID_MD_TASTE_NEW;
911 mdi->mdio_config_id = jmicron_meta_config_id(meta);
912 snprintf(name, sizeof(name), "JMicron-%08x",
913 mdi->mdio_config_id);
914 sc = g_raid_create_node(mp, name, md);
917 callout_init(&mdi->mdio_start_co, 1);
918 callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
919 g_raid_jmicron_go, sc);
920 mdi->mdio_rootmount = root_mount_hold("GRAID-JMicron");
921 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
924 /* There is no return after this point, so we close passed consumer. */
925 g_access(cp, -1, 0, 0);
927 rcp = g_new_consumer(geom);
928 rcp->flags |= G_CF_DIRECT_RECEIVE;
930 if (g_access(rcp, 1, 1, 1) != 0)
934 sx_xlock(&sc->sc_lock);
936 pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
939 pd->pd_disk_pos = -3;
940 pd->pd_disk_id = arc4random() & JMICRON_DISK_MASK;
942 pd->pd_disk_pos = -1;
943 pd->pd_disk_id = meta->disk_id;
945 pd->pd_disk_size = pp->mediasize;
946 disk = g_raid_create_disk(sc);
947 disk->d_md_data = (void *)pd;
948 disk->d_consumer = rcp;
951 g_raid_get_disk_info(disk);
953 g_raid_md_jmicron_new_disk(disk);
955 sx_xunlock(&sc->sc_lock);
960 free(meta, M_MD_JMICRON);
961 return (G_RAID_MD_TASTE_FAIL);
965 g_raid_md_event_jmicron(struct g_raid_md_object *md,
966 struct g_raid_disk *disk, u_int event)
968 struct g_raid_softc *sc;
969 struct g_raid_subdisk *sd;
970 struct g_raid_md_jmicron_object *mdi;
971 struct g_raid_md_jmicron_perdisk *pd;
974 mdi = (struct g_raid_md_jmicron_object *)md;
977 case G_RAID_NODE_E_START:
978 if (!mdi->mdio_started)
979 g_raid_md_jmicron_start(sc);
984 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
986 case G_RAID_DISK_E_DISCONNECTED:
987 /* If disk was assigned, just update statuses. */
988 if (pd->pd_disk_pos >= 0) {
989 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
990 if (disk->d_consumer) {
991 g_raid_kill_consumer(sc, disk->d_consumer);
992 disk->d_consumer = NULL;
994 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
995 g_raid_change_subdisk_state(sd,
996 G_RAID_SUBDISK_S_NONE);
997 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
998 G_RAID_EVENT_SUBDISK);
1001 /* Otherwise -- delete. */
1002 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1003 g_raid_destroy_disk(disk);
1006 /* Write updated metadata to all disks. */
1007 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
1009 /* Check if anything left except placeholders. */
1010 if (g_raid_ndisks(sc, -1) ==
1011 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1012 g_raid_destroy_node(sc, 0);
1014 g_raid_md_jmicron_refill(sc);
1021 g_raid_md_ctl_jmicron(struct g_raid_md_object *md,
1022 struct gctl_req *req)
1024 struct g_raid_softc *sc;
1025 struct g_raid_volume *vol;
1026 struct g_raid_subdisk *sd;
1027 struct g_raid_disk *disk;
1028 struct g_raid_md_jmicron_object *mdi;
1029 struct g_raid_md_jmicron_perdisk *pd;
1030 struct g_consumer *cp;
1031 struct g_provider *pp;
1033 const char *verb, *volname, *levelname, *diskname;
1035 off_t size, sectorsize, strip;
1036 intmax_t *sizearg, *striparg;
1037 int numdisks, i, len, level, qual, update;
1041 mdi = (struct g_raid_md_jmicron_object *)md;
1042 verb = gctl_get_param(req, "verb", NULL);
1043 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1045 if (strcmp(verb, "label") == 0) {
1047 gctl_error(req, "Invalid number of arguments.");
1050 volname = gctl_get_asciiparam(req, "arg1");
1051 if (volname == NULL) {
1052 gctl_error(req, "No volume name.");
1055 levelname = gctl_get_asciiparam(req, "arg2");
1056 if (levelname == NULL) {
1057 gctl_error(req, "No RAID level.");
1060 if (strcasecmp(levelname, "RAID5") == 0)
1061 levelname = "RAID5-LA";
1062 if (g_raid_volume_str2level(levelname, &level, &qual)) {
1063 gctl_error(req, "Unknown RAID level '%s'.", levelname);
1066 numdisks = *nargs - 3;
1067 force = gctl_get_paraml(req, "force", sizeof(*force));
1068 if (!g_raid_md_jmicron_supported(level, qual, numdisks,
1069 force ? *force : 0)) {
1070 gctl_error(req, "Unsupported RAID level "
1071 "(0x%02x/0x%02x), or number of disks (%d).",
1072 level, qual, numdisks);
1076 /* Search for disks, connect them and probe. */
1077 size = 0x7fffffffffffffffllu;
1079 for (i = 0; i < numdisks; i++) {
1080 snprintf(arg, sizeof(arg), "arg%d", i + 3);
1081 diskname = gctl_get_asciiparam(req, arg);
1082 if (diskname == NULL) {
1083 gctl_error(req, "No disk name (%s).", arg);
1087 if (strcmp(diskname, "NONE") == 0) {
1092 cp = g_raid_open_consumer(sc, diskname);
1094 gctl_error(req, "Can't open '%s'.",
1096 g_topology_unlock();
1102 pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
1103 pd->pd_disk_pos = i;
1104 pd->pd_disk_id = arc4random() & JMICRON_DISK_MASK;
1105 disk = g_raid_create_disk(sc);
1106 disk->d_md_data = (void *)pd;
1107 disk->d_consumer = cp;
1111 g_topology_unlock();
1113 g_raid_get_disk_info(disk);
1115 pd->pd_disk_size = pp->mediasize;
1116 if (size > pp->mediasize)
1117 size = pp->mediasize;
1118 if (sectorsize < pp->sectorsize)
1119 sectorsize = pp->sectorsize;
1124 if (sectorsize <= 0) {
1125 gctl_error(req, "Can't get sector size.");
1129 /* Reserve space for metadata. */
1132 /* Handle size argument. */
1133 len = sizeof(*sizearg);
1134 sizearg = gctl_get_param(req, "size", &len);
1135 if (sizearg != NULL && len == sizeof(*sizearg) &&
1137 if (*sizearg > size) {
1138 gctl_error(req, "Size too big %lld > %lld.",
1139 (long long)*sizearg, (long long)size);
1145 /* Handle strip argument. */
1147 len = sizeof(*striparg);
1148 striparg = gctl_get_param(req, "strip", &len);
1149 if (striparg != NULL && len == sizeof(*striparg) &&
1151 if (*striparg < sectorsize) {
1152 gctl_error(req, "Strip size too small.");
1155 if (*striparg % sectorsize != 0) {
1156 gctl_error(req, "Incorrect strip size.");
1159 if (strip > 65535 * sectorsize) {
1160 gctl_error(req, "Strip size too big.");
1166 /* Round size down to strip or sector. */
1167 if (level == G_RAID_VOLUME_RL_RAID1)
1168 size -= (size % sectorsize);
1169 else if (level == G_RAID_VOLUME_RL_RAID1E &&
1170 (numdisks & 1) != 0)
1171 size -= (size % (2 * strip));
1173 size -= (size % strip);
1175 gctl_error(req, "Size too small.");
1178 if (size > 0xffffffffffffllu * sectorsize) {
1179 gctl_error(req, "Size too big.");
1183 /* We have all we need, create things: volume, ... */
1184 mdi->mdio_total_disks = numdisks;
1185 mdi->mdio_started = 1;
1186 vol = g_raid_create_volume(sc, volname, -1);
1187 vol->v_md_data = (void *)(intptr_t)0;
1188 vol->v_raid_level = level;
1189 vol->v_raid_level_qualifier = qual;
1190 vol->v_strip_size = strip;
1191 vol->v_disks_count = numdisks;
1192 if (level == G_RAID_VOLUME_RL_RAID0 ||
1193 level == G_RAID_VOLUME_RL_CONCAT ||
1194 level == G_RAID_VOLUME_RL_SINGLE)
1195 vol->v_mediasize = size * numdisks;
1196 else if (level == G_RAID_VOLUME_RL_RAID1)
1197 vol->v_mediasize = size;
1198 else if (level == G_RAID_VOLUME_RL_RAID5)
1199 vol->v_mediasize = size * (numdisks - 1);
1201 vol->v_mediasize = ((size * numdisks) / strip / 2) *
1204 vol->v_sectorsize = sectorsize;
1205 g_raid_start_volume(vol);
1207 /* , and subdisks. */
1208 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1209 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
1210 sd = &vol->v_subdisks[pd->pd_disk_pos];
1214 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1215 if (sd->sd_disk->d_consumer != NULL) {
1216 g_raid_change_disk_state(disk,
1217 G_RAID_DISK_S_ACTIVE);
1218 g_raid_change_subdisk_state(sd,
1219 G_RAID_SUBDISK_S_ACTIVE);
1220 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1221 G_RAID_EVENT_SUBDISK);
1223 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1227 /* Write metadata based on created entities. */
1228 G_RAID_DEBUG1(0, sc, "Array started.");
1229 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
1231 /* Pickup any STALE/SPARE disks to refill array if needed. */
1232 g_raid_md_jmicron_refill(sc);
1234 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1235 G_RAID_EVENT_VOLUME);
1238 if (strcmp(verb, "delete") == 0) {
1239 /* Check if some volume is still open. */
1240 force = gctl_get_paraml(req, "force", sizeof(*force));
1241 if (force != NULL && *force == 0 &&
1242 g_raid_nopens(sc) != 0) {
1243 gctl_error(req, "Some volume is still open.");
1247 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1248 if (disk->d_consumer)
1249 jmicron_meta_erase(disk->d_consumer);
1251 g_raid_destroy_node(sc, 0);
1254 if (strcmp(verb, "remove") == 0 ||
1255 strcmp(verb, "fail") == 0) {
1257 gctl_error(req, "Invalid number of arguments.");
1260 for (i = 1; i < *nargs; i++) {
1261 snprintf(arg, sizeof(arg), "arg%d", i);
1262 diskname = gctl_get_asciiparam(req, arg);
1263 if (diskname == NULL) {
1264 gctl_error(req, "No disk name (%s).", arg);
1268 if (strncmp(diskname, _PATH_DEV, 5) == 0)
1271 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1272 if (disk->d_consumer != NULL &&
1273 disk->d_consumer->provider != NULL &&
1274 strcmp(disk->d_consumer->provider->name,
1279 gctl_error(req, "Disk '%s' not found.",
1285 if (strcmp(verb, "fail") == 0) {
1286 g_raid_md_fail_disk_jmicron(md, NULL, disk);
1290 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
1292 /* Erase metadata on deleting disk. */
1293 jmicron_meta_erase(disk->d_consumer);
1295 /* If disk was assigned, just update statuses. */
1296 if (pd->pd_disk_pos >= 0) {
1297 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1298 g_raid_kill_consumer(sc, disk->d_consumer);
1299 disk->d_consumer = NULL;
1300 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1301 g_raid_change_subdisk_state(sd,
1302 G_RAID_SUBDISK_S_NONE);
1303 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1304 G_RAID_EVENT_SUBDISK);
1307 /* Otherwise -- delete. */
1308 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1309 g_raid_destroy_disk(disk);
1313 /* Write updated metadata to remaining disks. */
1314 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
1316 /* Check if anything left except placeholders. */
1317 if (g_raid_ndisks(sc, -1) ==
1318 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1319 g_raid_destroy_node(sc, 0);
1321 g_raid_md_jmicron_refill(sc);
1324 if (strcmp(verb, "insert") == 0) {
1326 gctl_error(req, "Invalid number of arguments.");
1330 for (i = 1; i < *nargs; i++) {
1331 /* Get disk name. */
1332 snprintf(arg, sizeof(arg), "arg%d", i);
1333 diskname = gctl_get_asciiparam(req, arg);
1334 if (diskname == NULL) {
1335 gctl_error(req, "No disk name (%s).", arg);
1340 /* Try to find provider with specified name. */
1342 cp = g_raid_open_consumer(sc, diskname);
1344 gctl_error(req, "Can't open disk '%s'.",
1346 g_topology_unlock();
1352 pd = malloc(sizeof(*pd), M_MD_JMICRON, M_WAITOK | M_ZERO);
1353 pd->pd_disk_pos = -3;
1354 pd->pd_disk_id = arc4random() & JMICRON_DISK_MASK;
1355 pd->pd_disk_size = pp->mediasize;
1357 disk = g_raid_create_disk(sc);
1358 disk->d_consumer = cp;
1359 disk->d_md_data = (void *)pd;
1361 g_topology_unlock();
1363 g_raid_get_disk_info(disk);
1365 /* Welcome the "new" disk. */
1366 update += g_raid_md_jmicron_start_disk(disk);
1367 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1368 disk->d_state != G_RAID_DISK_S_SPARE) {
1369 gctl_error(req, "Disk '%s' doesn't fit.",
1371 g_raid_destroy_disk(disk);
1377 /* Write new metadata if we changed something. */
1379 g_raid_md_write_jmicron(md, NULL, NULL, NULL);
1382 gctl_error(req, "Command '%s' is not supported.", verb);
1387 g_raid_md_write_jmicron(struct g_raid_md_object *md, struct g_raid_volume *tvol,
1388 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1390 struct g_raid_softc *sc;
1391 struct g_raid_volume *vol;
1392 struct g_raid_subdisk *sd;
1393 struct g_raid_disk *disk;
1394 struct g_raid_md_jmicron_object *mdi;
1395 struct g_raid_md_jmicron_perdisk *pd;
1396 struct jmicron_raid_conf *meta;
1400 mdi = (struct g_raid_md_jmicron_object *)md;
1402 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1405 /* There is only one volume. */
1406 vol = TAILQ_FIRST(&sc->sc_volumes);
1408 /* Fill global fields. */
1409 meta = malloc(sizeof(*meta), M_MD_JMICRON, M_WAITOK | M_ZERO);
1410 strncpy(meta->signature, JMICRON_MAGIC, 2);
1411 meta->version = JMICRON_VERSION;
1412 jmicron_meta_put_name(meta, vol->v_name);
1413 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
1414 meta->type = JMICRON_T_RAID0;
1415 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
1416 meta->type = JMICRON_T_RAID1;
1417 else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
1418 meta->type = JMICRON_T_RAID01;
1419 else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
1420 vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE)
1421 meta->type = JMICRON_T_CONCAT;
1423 meta->type = JMICRON_T_RAID5;
1424 meta->stripe_shift = fls(vol->v_strip_size / 2048);
1425 meta->flags = JMICRON_F_READY | JMICRON_F_BOOTABLE;
1426 for (i = 0; i < vol->v_disks_count; i++) {
1427 sd = &vol->v_subdisks[i];
1428 if (sd->sd_disk == NULL || sd->sd_disk->d_md_data == NULL)
1429 meta->disks[i] = 0xffffffff;
1431 pd = (struct g_raid_md_jmicron_perdisk *)
1432 sd->sd_disk->d_md_data;
1433 meta->disks[i] = pd->pd_disk_id;
1435 if (sd->sd_state < G_RAID_SUBDISK_S_STALE)
1436 meta->flags |= JMICRON_F_BADSEC;
1438 meta->flags |= JMICRON_F_UNSYNC;
1441 /* Put spares to their slots. */
1443 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1444 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
1445 if (disk->d_state != G_RAID_DISK_S_SPARE)
1447 meta->spare[spares] = pd->pd_disk_id;
1452 /* We are done. Print meta data and store them to disks. */
1453 if (mdi->mdio_meta != NULL)
1454 free(mdi->mdio_meta, M_MD_JMICRON);
1455 mdi->mdio_meta = meta;
1456 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1457 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
1458 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1459 disk->d_state != G_RAID_DISK_S_SPARE)
1461 if (pd->pd_meta != NULL) {
1462 free(pd->pd_meta, M_MD_JMICRON);
1465 pd->pd_meta = jmicron_meta_copy(meta);
1466 pd->pd_meta->disk_id = pd->pd_disk_id;
1467 if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) {
1468 pd->pd_meta->offset =
1469 (sd->sd_offset / 512) / 16;
1470 pd->pd_meta->disk_sectors_high =
1471 (sd->sd_size / 512) >> 16;
1472 pd->pd_meta->disk_sectors_low =
1473 (sd->sd_size / 512) & 0xffff;
1474 if (sd->sd_state < G_RAID_SUBDISK_S_STALE)
1475 pd->pd_meta->flags &= ~JMICRON_F_BADSEC;
1476 else if (sd->sd_state < G_RAID_SUBDISK_S_ACTIVE)
1477 pd->pd_meta->flags |= JMICRON_F_UNSYNC;
1479 G_RAID_DEBUG(1, "Writing JMicron metadata to %s",
1480 g_raid_get_diskname(disk));
1481 g_raid_md_jmicron_print(pd->pd_meta);
1482 jmicron_meta_write(disk->d_consumer, pd->pd_meta);
1488 g_raid_md_fail_disk_jmicron(struct g_raid_md_object *md,
1489 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1491 struct g_raid_softc *sc;
1492 struct g_raid_md_jmicron_perdisk *pd;
1493 struct g_raid_subdisk *sd;
1496 pd = (struct g_raid_md_jmicron_perdisk *)tdisk->d_md_data;
1498 /* We can't fail disk that is not a part of array now. */
1499 if (pd->pd_disk_pos < 0)
1502 if (tdisk->d_consumer)
1503 jmicron_meta_erase(tdisk->d_consumer);
1505 /* Change states. */
1506 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
1507 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
1508 g_raid_change_subdisk_state(sd,
1509 G_RAID_SUBDISK_S_FAILED);
1510 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
1511 G_RAID_EVENT_SUBDISK);
1514 /* Write updated metadata to remaining disks. */
1515 g_raid_md_write_jmicron(md, NULL, NULL, tdisk);
1517 /* Check if anything left except placeholders. */
1518 if (g_raid_ndisks(sc, -1) ==
1519 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1520 g_raid_destroy_node(sc, 0);
1522 g_raid_md_jmicron_refill(sc);
1527 g_raid_md_free_disk_jmicron(struct g_raid_md_object *md,
1528 struct g_raid_disk *disk)
1530 struct g_raid_md_jmicron_perdisk *pd;
1532 pd = (struct g_raid_md_jmicron_perdisk *)disk->d_md_data;
1533 if (pd->pd_meta != NULL) {
1534 free(pd->pd_meta, M_MD_JMICRON);
1537 free(pd, M_MD_JMICRON);
1538 disk->d_md_data = NULL;
1543 g_raid_md_free_jmicron(struct g_raid_md_object *md)
1545 struct g_raid_md_jmicron_object *mdi;
1547 mdi = (struct g_raid_md_jmicron_object *)md;
1548 if (!mdi->mdio_started) {
1549 mdi->mdio_started = 0;
1550 callout_stop(&mdi->mdio_start_co);
1551 G_RAID_DEBUG1(1, md->mdo_softc,
1552 "root_mount_rel %p", mdi->mdio_rootmount);
1553 root_mount_rel(mdi->mdio_rootmount);
1554 mdi->mdio_rootmount = NULL;
1556 if (mdi->mdio_meta != NULL) {
1557 free(mdi->mdio_meta, M_MD_JMICRON);
1558 mdi->mdio_meta = NULL;
1563 G_RAID_MD_DECLARE(jmicron, "JMicron");