xref: /freebsd/sys/geom/raid/md_promise.c (revision fc1de96060db910b8aa5224d4d10b37baefeca1e)
189b17223SAlexander Motin /*-
289b17223SAlexander Motin  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
389b17223SAlexander Motin  * All rights reserved.
489b17223SAlexander Motin  *
589b17223SAlexander Motin  * Redistribution and use in source and binary forms, with or without
689b17223SAlexander Motin  * modification, are permitted provided that the following conditions
789b17223SAlexander Motin  * are met:
889b17223SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
989b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer.
1089b17223SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1189b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1289b17223SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1389b17223SAlexander Motin  *
1489b17223SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1589b17223SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1689b17223SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1789b17223SAlexander Motin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1889b17223SAlexander Motin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1989b17223SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2089b17223SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2189b17223SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2289b17223SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2389b17223SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2489b17223SAlexander Motin  * SUCH DAMAGE.
2589b17223SAlexander Motin  */
2689b17223SAlexander Motin 
2789b17223SAlexander Motin #include <sys/cdefs.h>
2889b17223SAlexander Motin __FBSDID("$FreeBSD$");
2989b17223SAlexander Motin 
3089b17223SAlexander Motin #include <sys/param.h>
3189b17223SAlexander Motin #include <sys/bio.h>
3289b17223SAlexander Motin #include <sys/endian.h>
3389b17223SAlexander Motin #include <sys/kernel.h>
3489b17223SAlexander Motin #include <sys/kobj.h>
3589b17223SAlexander Motin #include <sys/limits.h>
3689b17223SAlexander Motin #include <sys/lock.h>
3789b17223SAlexander Motin #include <sys/malloc.h>
3889b17223SAlexander Motin #include <sys/mutex.h>
3989b17223SAlexander Motin #include <sys/systm.h>
4089b17223SAlexander Motin #include <geom/geom.h>
4189b17223SAlexander Motin #include "geom/raid/g_raid.h"
4289b17223SAlexander Motin #include "g_raid_md_if.h"
4389b17223SAlexander Motin 
4489b17223SAlexander Motin static MALLOC_DEFINE(M_MD_PROMISE, "md_promise_data", "GEOM_RAID Promise metadata");
4589b17223SAlexander Motin 
4689b17223SAlexander Motin #define	PROMISE_MAX_DISKS	8
4789b17223SAlexander Motin #define	PROMISE_MAX_SUBDISKS	2
4889b17223SAlexander Motin #define	PROMISE_META_OFFSET	14
4989b17223SAlexander Motin 
5089b17223SAlexander Motin struct promise_raid_disk {
5189b17223SAlexander Motin 	uint8_t		flags;			/* Subdisk status. */
5289b17223SAlexander Motin #define PROMISE_F_VALID		0x01
5389b17223SAlexander Motin #define PROMISE_F_ONLINE	0x02
5489b17223SAlexander Motin #define PROMISE_F_ASSIGNED	0x04
5589b17223SAlexander Motin #define PROMISE_F_SPARE		0x08
5689b17223SAlexander Motin #define PROMISE_F_DUPLICATE	0x10
5789b17223SAlexander Motin #define PROMISE_F_REDIR		0x20
5889b17223SAlexander Motin #define PROMISE_F_DOWN		0x40
5989b17223SAlexander Motin #define PROMISE_F_READY		0x80
6089b17223SAlexander Motin 
6189b17223SAlexander Motin 	uint8_t		number;			/* Position in a volume. */
6289b17223SAlexander Motin 	uint8_t		channel;		/* ATA channel number. */
6389b17223SAlexander Motin 	uint8_t		device;			/* ATA device number. */
6489b17223SAlexander Motin 	uint64_t	id __packed;		/* Subdisk ID. */
6589b17223SAlexander Motin } __packed;
6689b17223SAlexander Motin 
6789b17223SAlexander Motin struct promise_raid_conf {
6889b17223SAlexander Motin 	char		promise_id[24];
6989b17223SAlexander Motin #define PROMISE_MAGIC		"Promise Technology, Inc."
7089b17223SAlexander Motin #define FREEBSD_MAGIC		"FreeBSD ATA driver RAID "
7189b17223SAlexander Motin 
7289b17223SAlexander Motin 	uint32_t	dummy_0;
7389b17223SAlexander Motin 	uint64_t	magic_0;
7489b17223SAlexander Motin #define PROMISE_MAGIC0(x)	(((uint64_t)(x.channel) << 48) | \
7589b17223SAlexander Motin 				((uint64_t)(x.device != 0) << 56))
7689b17223SAlexander Motin 	uint16_t	magic_1;
7789b17223SAlexander Motin 	uint32_t	magic_2;
7889b17223SAlexander Motin 	uint8_t		filler1[470];
7989b17223SAlexander Motin 
8089b17223SAlexander Motin 	uint32_t	integrity;
8189b17223SAlexander Motin #define PROMISE_I_VALID		0x00000080
8289b17223SAlexander Motin 
8389b17223SAlexander Motin 	struct promise_raid_disk	disk;	/* This subdisk info. */
8489b17223SAlexander Motin 	uint32_t	disk_offset;		/* Subdisk offset. */
8589b17223SAlexander Motin 	uint32_t	disk_sectors;		/* Subdisk size */
8689b17223SAlexander Motin 	uint32_t	rebuild_lba;		/* Rebuild position. */
8789b17223SAlexander Motin 	uint16_t	generation;		/* Generation number. */
8889b17223SAlexander Motin 	uint8_t		status;			/* Volume status. */
8989b17223SAlexander Motin #define PROMISE_S_VALID		0x01
9089b17223SAlexander Motin #define PROMISE_S_ONLINE	0x02
9189b17223SAlexander Motin #define PROMISE_S_INITED	0x04
9289b17223SAlexander Motin #define PROMISE_S_READY		0x08
9389b17223SAlexander Motin #define PROMISE_S_DEGRADED	0x10
9489b17223SAlexander Motin #define PROMISE_S_MARKED	0x20
9589b17223SAlexander Motin #define PROMISE_S_MIGRATING	0x40
9689b17223SAlexander Motin #define PROMISE_S_FUNCTIONAL	0x80
9789b17223SAlexander Motin 
9889b17223SAlexander Motin 	uint8_t		type;			/* Voluem type. */
9989b17223SAlexander Motin #define PROMISE_T_RAID0		0x00
10089b17223SAlexander Motin #define PROMISE_T_RAID1		0x01
10189b17223SAlexander Motin #define PROMISE_T_RAID3		0x02
10289b17223SAlexander Motin #define PROMISE_T_RAID5		0x04
10389b17223SAlexander Motin #define PROMISE_T_SPAN		0x08
10489b17223SAlexander Motin #define PROMISE_T_JBOD		0x10
10589b17223SAlexander Motin 
10689b17223SAlexander Motin 	uint8_t		total_disks;		/* Disks in this volume. */
10789b17223SAlexander Motin 	uint8_t		stripe_shift;		/* Strip size. */
10889b17223SAlexander Motin 	uint8_t		array_width;		/* Number of RAID0 stripes. */
10989b17223SAlexander Motin 	uint8_t		array_number;		/* Global volume number. */
11089b17223SAlexander Motin 	uint32_t	total_sectors;		/* Volume size. */
11189b17223SAlexander Motin 	uint16_t	cylinders;		/* Volume geometry: C. */
11289b17223SAlexander Motin 	uint8_t		heads;			/* Volume geometry: H. */
11389b17223SAlexander Motin 	uint8_t		sectors;		/* Volume geometry: S. */
11489b17223SAlexander Motin 	uint64_t	volume_id __packed;	/* Volume ID, */
11589b17223SAlexander Motin 	struct promise_raid_disk	disks[PROMISE_MAX_DISKS];
11689b17223SAlexander Motin 						/* Subdisks in this volume. */
11789b17223SAlexander Motin 	char		name[32];		/* Volume label. */
11889b17223SAlexander Motin 
11989b17223SAlexander Motin 	uint32_t	filler2[8];
12089b17223SAlexander Motin 	uint32_t	magic_3;	/* Something related to rebuild. */
12189b17223SAlexander Motin 	uint64_t	rebuild_lba64;	/* Per-volume rebuild position. */
12289b17223SAlexander Motin 	uint32_t	magic_4;
12389b17223SAlexander Motin 	uint32_t	magic_5;
124733a1f3fSAlexander Motin 	uint32_t	total_sectors_high;
125733a1f3fSAlexander Motin 	uint32_t	filler3[324];
12689b17223SAlexander Motin 	uint32_t	checksum;
12789b17223SAlexander Motin } __packed;
12889b17223SAlexander Motin 
12989b17223SAlexander Motin struct g_raid_md_promise_perdisk {
13089b17223SAlexander Motin 	int		 pd_updated;
13189b17223SAlexander Motin 	int		 pd_subdisks;
13289b17223SAlexander Motin 	struct promise_raid_conf	*pd_meta[PROMISE_MAX_SUBDISKS];
13389b17223SAlexander Motin };
13489b17223SAlexander Motin 
13589b17223SAlexander Motin struct g_raid_md_promise_pervolume {
13689b17223SAlexander Motin 	struct promise_raid_conf	*pv_meta;
13789b17223SAlexander Motin 	uint64_t			 pv_id;
13889b17223SAlexander Motin 	uint16_t			 pv_generation;
13989b17223SAlexander Motin 	int				 pv_disks_present;
14089b17223SAlexander Motin 	int				 pv_started;
14189b17223SAlexander Motin 	struct callout			 pv_start_co;	/* STARTING state timer. */
14289b17223SAlexander Motin };
14389b17223SAlexander Motin 
14489b17223SAlexander Motin static g_raid_md_create_t g_raid_md_create_promise;
14589b17223SAlexander Motin static g_raid_md_taste_t g_raid_md_taste_promise;
14689b17223SAlexander Motin static g_raid_md_event_t g_raid_md_event_promise;
14789b17223SAlexander Motin static g_raid_md_volume_event_t g_raid_md_volume_event_promise;
14889b17223SAlexander Motin static g_raid_md_ctl_t g_raid_md_ctl_promise;
14989b17223SAlexander Motin static g_raid_md_write_t g_raid_md_write_promise;
15089b17223SAlexander Motin static g_raid_md_fail_disk_t g_raid_md_fail_disk_promise;
15189b17223SAlexander Motin static g_raid_md_free_disk_t g_raid_md_free_disk_promise;
15289b17223SAlexander Motin static g_raid_md_free_volume_t g_raid_md_free_volume_promise;
15389b17223SAlexander Motin static g_raid_md_free_t g_raid_md_free_promise;
15489b17223SAlexander Motin 
15589b17223SAlexander Motin static kobj_method_t g_raid_md_promise_methods[] = {
15689b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_promise),
15789b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_promise),
15889b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_promise),
15989b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_volume_event,	g_raid_md_volume_event_promise),
16089b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_promise),
16189b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_promise),
16289b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_promise),
16389b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_promise),
16489b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_free_volume,	g_raid_md_free_volume_promise),
16589b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_promise),
16689b17223SAlexander Motin 	{ 0, 0 }
16789b17223SAlexander Motin };
16889b17223SAlexander Motin 
16989b17223SAlexander Motin static struct g_raid_md_class g_raid_md_promise_class = {
17089b17223SAlexander Motin 	"Promise",
17189b17223SAlexander Motin 	g_raid_md_promise_methods,
17289b17223SAlexander Motin 	sizeof(struct g_raid_md_object),
17389b17223SAlexander Motin 	.mdc_priority = 100
17489b17223SAlexander Motin };
17589b17223SAlexander Motin 
17689b17223SAlexander Motin 
17789b17223SAlexander Motin static void
17889b17223SAlexander Motin g_raid_md_promise_print(struct promise_raid_conf *meta)
17989b17223SAlexander Motin {
18089b17223SAlexander Motin 	int i;
18189b17223SAlexander Motin 
18289b17223SAlexander Motin 	if (g_raid_debug < 1)
18389b17223SAlexander Motin 		return;
18489b17223SAlexander Motin 
18589b17223SAlexander Motin 	printf("********* ATA Promise Metadata *********\n");
18689b17223SAlexander Motin 	printf("promise_id          <%.24s>\n", meta->promise_id);
18789b17223SAlexander Motin 	printf("disk                %02x %02x %02x %02x %016jx\n",
18889b17223SAlexander Motin 	    meta->disk.flags, meta->disk.number, meta->disk.channel,
18989b17223SAlexander Motin 	    meta->disk.device, meta->disk.id);
19089b17223SAlexander Motin 	printf("disk_offset         %u\n", meta->disk_offset);
19189b17223SAlexander Motin 	printf("disk_sectors        %u\n", meta->disk_sectors);
19289b17223SAlexander Motin 	printf("rebuild_lba         %u\n", meta->rebuild_lba);
19389b17223SAlexander Motin 	printf("generation          %u\n", meta->generation);
19489b17223SAlexander Motin 	printf("status              0x%02x\n", meta->status);
19589b17223SAlexander Motin 	printf("type                %u\n", meta->type);
19689b17223SAlexander Motin 	printf("total_disks         %u\n", meta->total_disks);
19789b17223SAlexander Motin 	printf("stripe_shift        %u\n", meta->stripe_shift);
19889b17223SAlexander Motin 	printf("array_width         %u\n", meta->array_width);
19989b17223SAlexander Motin 	printf("array_number        %u\n", meta->array_number);
20089b17223SAlexander Motin 	printf("total_sectors       %u\n", meta->total_sectors);
20189b17223SAlexander Motin 	printf("cylinders           %u\n", meta->cylinders);
20289b17223SAlexander Motin 	printf("heads               %u\n", meta->heads);
20389b17223SAlexander Motin 	printf("sectors             %u\n", meta->sectors);
20489b17223SAlexander Motin 	printf("volume_id           0x%016jx\n", meta->volume_id);
20589b17223SAlexander Motin 	printf("disks:\n");
20689b17223SAlexander Motin 	for (i = 0; i < PROMISE_MAX_DISKS; i++ ) {
20789b17223SAlexander Motin 		printf("                    %02x %02x %02x %02x %016jx\n",
20889b17223SAlexander Motin 		    meta->disks[i].flags, meta->disks[i].number,
20989b17223SAlexander Motin 		    meta->disks[i].channel, meta->disks[i].device,
21089b17223SAlexander Motin 		    meta->disks[i].id);
21189b17223SAlexander Motin 	}
21289b17223SAlexander Motin 	printf("name                <%.32s>\n", meta->name);
21389b17223SAlexander Motin 	printf("magic_3             0x%08x\n", meta->magic_3);
21489b17223SAlexander Motin 	printf("rebuild_lba64       %ju\n", meta->rebuild_lba64);
21589b17223SAlexander Motin 	printf("magic_4             0x%08x\n", meta->magic_4);
21689b17223SAlexander Motin 	printf("magic_5             0x%08x\n", meta->magic_5);
217733a1f3fSAlexander Motin 	printf("total_sectors_high  0x%08x\n", meta->total_sectors_high);
21889b17223SAlexander Motin 	printf("=================================================\n");
21989b17223SAlexander Motin }
22089b17223SAlexander Motin 
22189b17223SAlexander Motin static struct promise_raid_conf *
22289b17223SAlexander Motin promise_meta_copy(struct promise_raid_conf *meta)
22389b17223SAlexander Motin {
22489b17223SAlexander Motin 	struct promise_raid_conf *nmeta;
22589b17223SAlexander Motin 
22689b17223SAlexander Motin 	nmeta = malloc(sizeof(*nmeta), M_MD_PROMISE, M_WAITOK);
22789b17223SAlexander Motin 	memcpy(nmeta, meta, sizeof(*nmeta));
22889b17223SAlexander Motin 	return (nmeta);
22989b17223SAlexander Motin }
23089b17223SAlexander Motin 
23189b17223SAlexander Motin static int
23289b17223SAlexander Motin promise_meta_find_disk(struct promise_raid_conf *meta, uint64_t id)
23389b17223SAlexander Motin {
23489b17223SAlexander Motin 	int pos;
23589b17223SAlexander Motin 
23689b17223SAlexander Motin 	for (pos = 0; pos < meta->total_disks; pos++) {
23789b17223SAlexander Motin 		if (meta->disks[pos].id == id)
23889b17223SAlexander Motin 			return (pos);
23989b17223SAlexander Motin 	}
24089b17223SAlexander Motin 	return (-1);
24189b17223SAlexander Motin }
24289b17223SAlexander Motin 
24389b17223SAlexander Motin static int
24489b17223SAlexander Motin promise_meta_unused_range(struct promise_raid_conf **metaarr, int nsd,
24589b17223SAlexander Motin     uint32_t sectors, uint32_t *off, uint32_t *size)
24689b17223SAlexander Motin {
24789b17223SAlexander Motin 	uint32_t coff, csize;
24889b17223SAlexander Motin 	int i, j;
24989b17223SAlexander Motin 
25089b17223SAlexander Motin 	sectors -= 131072;
25189b17223SAlexander Motin 	*off = 0;
25289b17223SAlexander Motin 	*size = 0;
25389b17223SAlexander Motin 	coff = 0;
25489b17223SAlexander Motin 	csize = sectors;
25589b17223SAlexander Motin 	i = 0;
25689b17223SAlexander Motin 	while (1) {
25789b17223SAlexander Motin 		for (j = 0; j < nsd; j++) {
25889b17223SAlexander Motin 			if (metaarr[j]->disk_offset >= coff) {
25989b17223SAlexander Motin 				csize = MIN(csize,
26089b17223SAlexander Motin 				    metaarr[j]->disk_offset - coff);
26189b17223SAlexander Motin 			}
26289b17223SAlexander Motin 		}
26389b17223SAlexander Motin 		if (csize > *size) {
26489b17223SAlexander Motin 			*off = coff;
26589b17223SAlexander Motin 			*size = csize;
26689b17223SAlexander Motin 		}
26789b17223SAlexander Motin 		if (i >= nsd)
26889b17223SAlexander Motin 			break;
26989b17223SAlexander Motin 		coff = metaarr[i]->disk_offset + metaarr[i]->disk_sectors;
27089b17223SAlexander Motin 		csize = sectors - coff;
27189b17223SAlexander Motin 		i++;
27289b17223SAlexander Motin 	};
27389b17223SAlexander Motin 	return ((*size > 0) ? 1 : 0);
27489b17223SAlexander Motin }
27589b17223SAlexander Motin 
27689b17223SAlexander Motin static int
27789b17223SAlexander Motin promise_meta_translate_disk(struct g_raid_volume *vol, int md_disk_pos)
27889b17223SAlexander Motin {
27989b17223SAlexander Motin 	int disk_pos, width;
28089b17223SAlexander Motin 
28189b17223SAlexander Motin 	if (md_disk_pos >= 0 && vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
28289b17223SAlexander Motin 		width = vol->v_disks_count / 2;
28389b17223SAlexander Motin 		disk_pos = (md_disk_pos / width) +
28489b17223SAlexander Motin 		    (md_disk_pos % width) * width;
28589b17223SAlexander Motin 	} else
28689b17223SAlexander Motin 		disk_pos = md_disk_pos;
28789b17223SAlexander Motin 	return (disk_pos);
28889b17223SAlexander Motin }
28989b17223SAlexander Motin 
29089b17223SAlexander Motin static void
29189b17223SAlexander Motin promise_meta_get_name(struct promise_raid_conf *meta, char *buf)
29289b17223SAlexander Motin {
29389b17223SAlexander Motin 	int i;
29489b17223SAlexander Motin 
29589b17223SAlexander Motin 	strncpy(buf, meta->name, 32);
29689b17223SAlexander Motin 	buf[32] = 0;
29789b17223SAlexander Motin 	for (i = 31; i >= 0; i--) {
29889b17223SAlexander Motin 		if (buf[i] > 0x20)
29989b17223SAlexander Motin 			break;
30089b17223SAlexander Motin 		buf[i] = 0;
30189b17223SAlexander Motin 	}
30289b17223SAlexander Motin }
30389b17223SAlexander Motin 
30489b17223SAlexander Motin static void
30589b17223SAlexander Motin promise_meta_put_name(struct promise_raid_conf *meta, char *buf)
30689b17223SAlexander Motin {
30789b17223SAlexander Motin 
30889b17223SAlexander Motin 	memset(meta->name, 0x20, 32);
30989b17223SAlexander Motin 	memcpy(meta->name, buf, MIN(strlen(buf), 32));
31089b17223SAlexander Motin }
31189b17223SAlexander Motin 
31289b17223SAlexander Motin static int
31389b17223SAlexander Motin promise_meta_read(struct g_consumer *cp, struct promise_raid_conf **metaarr)
31489b17223SAlexander Motin {
31589b17223SAlexander Motin 	struct g_provider *pp;
31689b17223SAlexander Motin 	struct promise_raid_conf *meta;
31789b17223SAlexander Motin 	char *buf;
31889b17223SAlexander Motin 	int error, i, subdisks;
31989b17223SAlexander Motin 	uint32_t checksum, *ptr;
32089b17223SAlexander Motin 
32189b17223SAlexander Motin 	pp = cp->provider;
32289b17223SAlexander Motin 	subdisks = 0;
32389b17223SAlexander Motin next:
32489b17223SAlexander Motin 	/* Read metadata block. */
32589b17223SAlexander Motin 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize *
32689b17223SAlexander Motin 	    (63 - subdisks * PROMISE_META_OFFSET),
32789b17223SAlexander Motin 	    pp->sectorsize * 4, &error);
32889b17223SAlexander Motin 	if (buf == NULL) {
32989b17223SAlexander Motin 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
33089b17223SAlexander Motin 		    pp->name, error);
33189b17223SAlexander Motin 		return (subdisks);
33289b17223SAlexander Motin 	}
33389b17223SAlexander Motin 	meta = (struct promise_raid_conf *)buf;
33489b17223SAlexander Motin 
33589b17223SAlexander Motin 	/* Check if this is an Promise RAID struct */
33689b17223SAlexander Motin 	if (strncmp(meta->promise_id, PROMISE_MAGIC, strlen(PROMISE_MAGIC)) &&
33789b17223SAlexander Motin 	    strncmp(meta->promise_id, FREEBSD_MAGIC, strlen(FREEBSD_MAGIC))) {
33889b17223SAlexander Motin 		if (subdisks == 0)
33989b17223SAlexander Motin 			G_RAID_DEBUG(1,
34089b17223SAlexander Motin 			    "Promise signature check failed on %s", pp->name);
34189b17223SAlexander Motin 		g_free(buf);
34289b17223SAlexander Motin 		return (subdisks);
34389b17223SAlexander Motin 	}
34489b17223SAlexander Motin 	meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK);
34589b17223SAlexander Motin 	memcpy(meta, buf, MIN(sizeof(*meta), pp->sectorsize * 4));
34689b17223SAlexander Motin 	g_free(buf);
34789b17223SAlexander Motin 
34889b17223SAlexander Motin 	/* Check metadata checksum. */
34989b17223SAlexander Motin 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++)
35089b17223SAlexander Motin 		checksum += *ptr++;
35189b17223SAlexander Motin 	if (checksum != meta->checksum) {
35289b17223SAlexander Motin 		G_RAID_DEBUG(1, "Promise checksum check failed on %s", pp->name);
35389b17223SAlexander Motin 		free(meta, M_MD_PROMISE);
35489b17223SAlexander Motin 		return (subdisks);
35589b17223SAlexander Motin 	}
35689b17223SAlexander Motin 
35789b17223SAlexander Motin 	if ((meta->integrity & PROMISE_I_VALID) == 0) {
35889b17223SAlexander Motin 		G_RAID_DEBUG(1, "Promise metadata is invalid on %s", pp->name);
35989b17223SAlexander Motin 		free(meta, M_MD_PROMISE);
36089b17223SAlexander Motin 		return (subdisks);
36189b17223SAlexander Motin 	}
36289b17223SAlexander Motin 
36389b17223SAlexander Motin 	if (meta->total_disks > PROMISE_MAX_DISKS) {
36489b17223SAlexander Motin 		G_RAID_DEBUG(1, "Wrong number of disks on %s (%d)",
36589b17223SAlexander Motin 		    pp->name, meta->total_disks);
36689b17223SAlexander Motin 		free(meta, M_MD_PROMISE);
36789b17223SAlexander Motin 		return (subdisks);
36889b17223SAlexander Motin 	}
36989b17223SAlexander Motin 
37089b17223SAlexander Motin 	/* Save this part and look for next. */
37189b17223SAlexander Motin 	*metaarr = meta;
37289b17223SAlexander Motin 	metaarr++;
37389b17223SAlexander Motin 	subdisks++;
37489b17223SAlexander Motin 	if (subdisks < PROMISE_MAX_SUBDISKS)
37589b17223SAlexander Motin 		goto next;
37689b17223SAlexander Motin 
37789b17223SAlexander Motin 	return (subdisks);
37889b17223SAlexander Motin }
37989b17223SAlexander Motin 
38089b17223SAlexander Motin static int
38189b17223SAlexander Motin promise_meta_write(struct g_consumer *cp,
38289b17223SAlexander Motin     struct promise_raid_conf **metaarr, int nsd)
38389b17223SAlexander Motin {
38489b17223SAlexander Motin 	struct g_provider *pp;
38589b17223SAlexander Motin 	struct promise_raid_conf *meta;
38689b17223SAlexander Motin 	char *buf;
38789b17223SAlexander Motin 	int error, i, subdisk, fake;
38889b17223SAlexander Motin 	uint32_t checksum, *ptr, off, size;
38989b17223SAlexander Motin 
39089b17223SAlexander Motin 	pp = cp->provider;
39189b17223SAlexander Motin 	subdisk = 0;
39289b17223SAlexander Motin 	fake = 0;
39389b17223SAlexander Motin next:
39489b17223SAlexander Motin 	buf = malloc(pp->sectorsize * 4, M_MD_PROMISE, M_WAITOK | M_ZERO);
39589b17223SAlexander Motin 	meta = NULL;
39689b17223SAlexander Motin 	if (subdisk < nsd) {
39789b17223SAlexander Motin 		meta = metaarr[subdisk];
39889b17223SAlexander Motin 	} else if (!fake && promise_meta_unused_range(metaarr, nsd,
39989b17223SAlexander Motin 	    cp->provider->mediasize / cp->provider->sectorsize,
40089b17223SAlexander Motin 	    &off, &size)) {
40189b17223SAlexander Motin 		/* Optionally add record for unused space. */
40289b17223SAlexander Motin 		meta = (struct promise_raid_conf *)buf;
40363607675SAlexander Motin 		memcpy(&meta->promise_id[0], PROMISE_MAGIC,
40463607675SAlexander Motin 		    sizeof(PROMISE_MAGIC) - 1);
40589b17223SAlexander Motin 		meta->dummy_0 = 0x00020000;
40689b17223SAlexander Motin 		meta->integrity = PROMISE_I_VALID;
40789b17223SAlexander Motin 		meta->disk.flags = PROMISE_F_ONLINE | PROMISE_F_VALID;
40889b17223SAlexander Motin 		meta->disk.number = 0xff;
40989b17223SAlexander Motin 		arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0);
41089b17223SAlexander Motin 		meta->disk_offset = off;
41189b17223SAlexander Motin 		meta->disk_sectors = size;
41289b17223SAlexander Motin 		meta->rebuild_lba = UINT32_MAX;
41389b17223SAlexander Motin 		fake = 1;
41489b17223SAlexander Motin 	}
41589b17223SAlexander Motin 	if (meta != NULL) {
41689b17223SAlexander Motin 		/* Recalculate checksum for case if metadata were changed. */
41789b17223SAlexander Motin 		meta->checksum = 0;
41889b17223SAlexander Motin 		for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++)
41989b17223SAlexander Motin 			checksum += *ptr++;
42089b17223SAlexander Motin 		meta->checksum = checksum;
42189b17223SAlexander Motin 		memcpy(buf, meta, MIN(pp->sectorsize * 4, sizeof(*meta)));
42289b17223SAlexander Motin 	}
42389b17223SAlexander Motin 	error = g_write_data(cp, pp->mediasize - pp->sectorsize *
42489b17223SAlexander Motin 	    (63 - subdisk * PROMISE_META_OFFSET),
42589b17223SAlexander Motin 	    buf, pp->sectorsize * 4);
42689b17223SAlexander Motin 	if (error != 0) {
42789b17223SAlexander Motin 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
42889b17223SAlexander Motin 		    pp->name, error);
42989b17223SAlexander Motin 	}
43089b17223SAlexander Motin 	free(buf, M_MD_PROMISE);
43189b17223SAlexander Motin 
43289b17223SAlexander Motin 	subdisk++;
43389b17223SAlexander Motin 	if (subdisk < PROMISE_MAX_SUBDISKS)
43489b17223SAlexander Motin 		goto next;
43589b17223SAlexander Motin 
43689b17223SAlexander Motin 	return (error);
43789b17223SAlexander Motin }
43889b17223SAlexander Motin 
43989b17223SAlexander Motin static int
44089b17223SAlexander Motin promise_meta_erase(struct g_consumer *cp)
44189b17223SAlexander Motin {
44289b17223SAlexander Motin 	struct g_provider *pp;
44389b17223SAlexander Motin 	char *buf;
44489b17223SAlexander Motin 	int error, subdisk;
44589b17223SAlexander Motin 
44689b17223SAlexander Motin 	pp = cp->provider;
44789b17223SAlexander Motin 	buf = malloc(4 * pp->sectorsize, M_MD_PROMISE, M_WAITOK | M_ZERO);
44889b17223SAlexander Motin 	for (subdisk = 0; subdisk < PROMISE_MAX_SUBDISKS; subdisk++) {
44989b17223SAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize *
45089b17223SAlexander Motin 		    (63 - subdisk * PROMISE_META_OFFSET),
45189b17223SAlexander Motin 		    buf, 4 * pp->sectorsize);
45289b17223SAlexander Motin 		if (error != 0) {
45389b17223SAlexander Motin 			G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
45489b17223SAlexander Motin 			    pp->name, error);
45589b17223SAlexander Motin 		}
45689b17223SAlexander Motin 	}
45789b17223SAlexander Motin 	free(buf, M_MD_PROMISE);
45889b17223SAlexander Motin 	return (error);
45989b17223SAlexander Motin }
46089b17223SAlexander Motin 
46189b17223SAlexander Motin static int
46289b17223SAlexander Motin promise_meta_write_spare(struct g_consumer *cp)
46389b17223SAlexander Motin {
46489b17223SAlexander Motin 	struct promise_raid_conf *meta;
46589b17223SAlexander Motin 	int error;
46689b17223SAlexander Motin 
46789b17223SAlexander Motin 	meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO);
46863607675SAlexander Motin 	memcpy(&meta->promise_id[0], PROMISE_MAGIC, sizeof(PROMISE_MAGIC) - 1);
46989b17223SAlexander Motin 	meta->dummy_0 = 0x00020000;
47089b17223SAlexander Motin 	meta->integrity = PROMISE_I_VALID;
47189b17223SAlexander Motin 	meta->disk.flags = PROMISE_F_SPARE | PROMISE_F_ONLINE | PROMISE_F_VALID;
47289b17223SAlexander Motin 	meta->disk.number = 0xff;
47389b17223SAlexander Motin 	arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0);
47489b17223SAlexander Motin 	meta->disk_sectors = cp->provider->mediasize / cp->provider->sectorsize;
47589b17223SAlexander Motin 	meta->disk_sectors -= 131072;
47689b17223SAlexander Motin 	meta->rebuild_lba = UINT32_MAX;
47789b17223SAlexander Motin 	error = promise_meta_write(cp, &meta, 1);
47889b17223SAlexander Motin 	free(meta, M_MD_PROMISE);
47989b17223SAlexander Motin 	return (error);
48089b17223SAlexander Motin }
48189b17223SAlexander Motin 
48289b17223SAlexander Motin static struct g_raid_volume *
48389b17223SAlexander Motin g_raid_md_promise_get_volume(struct g_raid_softc *sc, uint64_t id)
48489b17223SAlexander Motin {
48589b17223SAlexander Motin 	struct g_raid_volume	*vol;
48689b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
48789b17223SAlexander Motin 
48889b17223SAlexander Motin 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
48989b17223SAlexander Motin 		pv = vol->v_md_data;
49089b17223SAlexander Motin 		if (pv->pv_id == id)
49189b17223SAlexander Motin 			break;
49289b17223SAlexander Motin 	}
49389b17223SAlexander Motin 	return (vol);
49489b17223SAlexander Motin }
49589b17223SAlexander Motin 
49689b17223SAlexander Motin static int
49789b17223SAlexander Motin g_raid_md_promise_purge_volumes(struct g_raid_softc *sc)
49889b17223SAlexander Motin {
49989b17223SAlexander Motin 	struct g_raid_volume	*vol, *tvol;
50089b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
50189b17223SAlexander Motin 	int i, res;
50289b17223SAlexander Motin 
50389b17223SAlexander Motin 	res = 0;
50489b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
50589b17223SAlexander Motin 		pv = vol->v_md_data;
50689b17223SAlexander Motin 		if (!pv->pv_started || vol->v_stopping)
50789b17223SAlexander Motin 			continue;
50889b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
50989b17223SAlexander Motin 			if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
51089b17223SAlexander Motin 				break;
51189b17223SAlexander Motin 		}
51289b17223SAlexander Motin 		if (i >= vol->v_disks_count) {
51389b17223SAlexander Motin 			g_raid_destroy_volume(vol);
51489b17223SAlexander Motin 			res = 1;
51589b17223SAlexander Motin 		}
51689b17223SAlexander Motin 	}
51789b17223SAlexander Motin 	return (res);
51889b17223SAlexander Motin }
51989b17223SAlexander Motin 
52089b17223SAlexander Motin static int
52189b17223SAlexander Motin g_raid_md_promise_purge_disks(struct g_raid_softc *sc)
52289b17223SAlexander Motin {
52389b17223SAlexander Motin 	struct g_raid_disk	*disk, *tdisk;
52489b17223SAlexander Motin 	struct g_raid_volume	*vol;
52589b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
52689b17223SAlexander Motin 	int i, j, res;
52789b17223SAlexander Motin 
52889b17223SAlexander Motin 	res = 0;
52989b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
53089b17223SAlexander Motin 		if (disk->d_state == G_RAID_DISK_S_SPARE)
53189b17223SAlexander Motin 			continue;
53289b17223SAlexander Motin 		pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
53389b17223SAlexander Motin 
53489b17223SAlexander Motin 		/* Scan for deleted volumes. */
53589b17223SAlexander Motin 		for (i = 0; i < pd->pd_subdisks; ) {
53689b17223SAlexander Motin 			vol = g_raid_md_promise_get_volume(sc,
53789b17223SAlexander Motin 			    pd->pd_meta[i]->volume_id);
53889b17223SAlexander Motin 			if (vol != NULL && !vol->v_stopping) {
53989b17223SAlexander Motin 				i++;
54089b17223SAlexander Motin 				continue;
54189b17223SAlexander Motin 			}
54289b17223SAlexander Motin 			free(pd->pd_meta[i], M_MD_PROMISE);
54389b17223SAlexander Motin 			for (j = i; j < pd->pd_subdisks - 1; j++)
54489b17223SAlexander Motin 				pd->pd_meta[j] = pd->pd_meta[j + 1];
54589b17223SAlexander Motin 			pd->pd_meta[PROMISE_MAX_SUBDISKS - 1] = NULL;
54689b17223SAlexander Motin 			pd->pd_subdisks--;
54789b17223SAlexander Motin 			pd->pd_updated = 1;
54889b17223SAlexander Motin 		}
54989b17223SAlexander Motin 
55089b17223SAlexander Motin 		/* If there is no metadata left - erase and delete disk. */
55189b17223SAlexander Motin 		if (pd->pd_subdisks == 0) {
55289b17223SAlexander Motin 			promise_meta_erase(disk->d_consumer);
55389b17223SAlexander Motin 			g_raid_destroy_disk(disk);
55489b17223SAlexander Motin 			res = 1;
55589b17223SAlexander Motin 		}
55689b17223SAlexander Motin 	}
55789b17223SAlexander Motin 	return (res);
55889b17223SAlexander Motin }
55989b17223SAlexander Motin 
56089b17223SAlexander Motin static int
56189b17223SAlexander Motin g_raid_md_promise_supported(int level, int qual, int disks, int force)
56289b17223SAlexander Motin {
56389b17223SAlexander Motin 
56489b17223SAlexander Motin 	if (disks > PROMISE_MAX_DISKS)
56589b17223SAlexander Motin 		return (0);
56689b17223SAlexander Motin 	switch (level) {
56789b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID0:
56889b17223SAlexander Motin 		if (disks < 1)
56989b17223SAlexander Motin 			return (0);
57089b17223SAlexander Motin 		if (!force && disks < 2)
57189b17223SAlexander Motin 			return (0);
57289b17223SAlexander Motin 		break;
57389b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1:
57489b17223SAlexander Motin 		if (disks < 1)
57589b17223SAlexander Motin 			return (0);
57689b17223SAlexander Motin 		if (!force && (disks != 2))
57789b17223SAlexander Motin 			return (0);
57889b17223SAlexander Motin 		break;
57989b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1E:
58089b17223SAlexander Motin 		if (disks < 2)
58189b17223SAlexander Motin 			return (0);
58289b17223SAlexander Motin 		if (disks % 2 != 0)
58389b17223SAlexander Motin 			return (0);
58489b17223SAlexander Motin 		if (!force && (disks != 4))
58589b17223SAlexander Motin 			return (0);
58689b17223SAlexander Motin 		break;
58789b17223SAlexander Motin 	case G_RAID_VOLUME_RL_SINGLE:
58889b17223SAlexander Motin 		if (disks != 1)
58989b17223SAlexander Motin 			return (0);
59089b17223SAlexander Motin 		break;
59189b17223SAlexander Motin 	case G_RAID_VOLUME_RL_CONCAT:
59289b17223SAlexander Motin 		if (disks < 2)
59389b17223SAlexander Motin 			return (0);
59489b17223SAlexander Motin 		break;
59589b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID5:
59689b17223SAlexander Motin 		if (disks < 3)
59789b17223SAlexander Motin 			return (0);
598*fc1de960SAlexander Motin 		if (qual != G_RAID_VOLUME_RLQ_R5LA)
599*fc1de960SAlexander Motin 			return (0);
60089b17223SAlexander Motin 		break;
60189b17223SAlexander Motin 	default:
60289b17223SAlexander Motin 		return (0);
60389b17223SAlexander Motin 	}
604*fc1de960SAlexander Motin 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
60589b17223SAlexander Motin 		return (0);
60689b17223SAlexander Motin 	return (1);
60789b17223SAlexander Motin }
60889b17223SAlexander Motin 
60989b17223SAlexander Motin static int
61089b17223SAlexander Motin g_raid_md_promise_start_disk(struct g_raid_disk *disk, int sdn,
61189b17223SAlexander Motin     struct g_raid_volume *vol)
61289b17223SAlexander Motin {
61389b17223SAlexander Motin 	struct g_raid_softc *sc;
61489b17223SAlexander Motin 	struct g_raid_subdisk *sd;
61589b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
61689b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
61789b17223SAlexander Motin 	struct promise_raid_conf *meta;
61889b17223SAlexander Motin 	off_t size;
61989b17223SAlexander Motin 	int disk_pos, md_disk_pos, i, resurrection = 0;
62089b17223SAlexander Motin 	uint32_t eoff, esize;
62189b17223SAlexander Motin 
62289b17223SAlexander Motin 	sc = disk->d_softc;
62389b17223SAlexander Motin 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
62489b17223SAlexander Motin 
62589b17223SAlexander Motin 	pv = vol->v_md_data;
62689b17223SAlexander Motin 	meta = pv->pv_meta;
62789b17223SAlexander Motin 
62889b17223SAlexander Motin 	if (sdn >= 0) {
62989b17223SAlexander Motin 		/* Find disk position in metadata by it's serial. */
63089b17223SAlexander Motin 		md_disk_pos = promise_meta_find_disk(meta, pd->pd_meta[sdn]->disk.id);
63189b17223SAlexander Motin 		/* For RAID0+1 we need to translate order. */
63289b17223SAlexander Motin 		disk_pos = promise_meta_translate_disk(vol, md_disk_pos);
63389b17223SAlexander Motin 	} else {
63489b17223SAlexander Motin 		md_disk_pos = -1;
63589b17223SAlexander Motin 		disk_pos = -1;
63689b17223SAlexander Motin 	}
63789b17223SAlexander Motin 	if (disk_pos < 0) {
63889b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "Disk %s is not part of the volume %s",
63989b17223SAlexander Motin 		    g_raid_get_diskname(disk), vol->v_name);
64089b17223SAlexander Motin 		/* Failed stale disk is useless for us. */
64189b17223SAlexander Motin 		if (sdn >= 0 &&
64289b17223SAlexander Motin 		    pd->pd_meta[sdn]->disk.flags & PROMISE_F_DOWN) {
64389b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
64489b17223SAlexander Motin 			return (0);
64589b17223SAlexander Motin 		}
64689b17223SAlexander Motin 		/* If we were given specific metadata subdisk - erase it. */
64789b17223SAlexander Motin 		if (sdn >= 0) {
64889b17223SAlexander Motin 			free(pd->pd_meta[sdn], M_MD_PROMISE);
64989b17223SAlexander Motin 			for (i = sdn; i < pd->pd_subdisks - 1; i++)
65089b17223SAlexander Motin 				pd->pd_meta[i] = pd->pd_meta[i + 1];
65189b17223SAlexander Motin 			pd->pd_meta[PROMISE_MAX_SUBDISKS - 1] = NULL;
65289b17223SAlexander Motin 			pd->pd_subdisks--;
65389b17223SAlexander Motin 		}
65489b17223SAlexander Motin 		/* If we are in the start process, that's all for now. */
65589b17223SAlexander Motin 		if (!pv->pv_started)
65689b17223SAlexander Motin 			goto nofit;
65789b17223SAlexander Motin 		/*
65889b17223SAlexander Motin 		 * If we have already started - try to get use of the disk.
65989b17223SAlexander Motin 		 * Try to replace OFFLINE disks first, then FAILED.
66089b17223SAlexander Motin 		 */
66189b17223SAlexander Motin 		promise_meta_unused_range(pd->pd_meta, pd->pd_subdisks,
66289b17223SAlexander Motin 		    disk->d_consumer->provider->mediasize /
66389b17223SAlexander Motin 		    disk->d_consumer->provider->sectorsize,
66489b17223SAlexander Motin 		    &eoff, &esize);
66589b17223SAlexander Motin 		if (esize == 0) {
66689b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "No free space on disk %s",
66789b17223SAlexander Motin 			    g_raid_get_diskname(disk));
66889b17223SAlexander Motin 			goto nofit;
66989b17223SAlexander Motin 		}
67089b17223SAlexander Motin 		size = INT64_MAX;
67189b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
67289b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
67389b17223SAlexander Motin 			if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
67489b17223SAlexander Motin 				size = sd->sd_size;
67589b17223SAlexander Motin 			if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
67689b17223SAlexander Motin 			    (disk_pos < 0 ||
67789b17223SAlexander Motin 			     vol->v_subdisks[i].sd_state < sd->sd_state))
67889b17223SAlexander Motin 				disk_pos = i;
67989b17223SAlexander Motin 		}
68089b17223SAlexander Motin 		if (disk_pos >= 0 &&
68189b17223SAlexander Motin 		    vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
68289b17223SAlexander Motin 		    (off_t)esize * 512 < size) {
68389b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Disk %s free space "
68489b17223SAlexander Motin 			    "is too small (%ju < %ju)",
68589b17223SAlexander Motin 			    g_raid_get_diskname(disk),
68689b17223SAlexander Motin 			    (off_t)esize * 512, size);
68789b17223SAlexander Motin 			disk_pos = -1;
68889b17223SAlexander Motin 		}
68989b17223SAlexander Motin 		if (disk_pos >= 0) {
69089b17223SAlexander Motin 			if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
69189b17223SAlexander Motin 				esize = size / 512;
69289b17223SAlexander Motin 			/* For RAID0+1 we need to translate order. */
69389b17223SAlexander Motin 			md_disk_pos = promise_meta_translate_disk(vol, disk_pos);
69489b17223SAlexander Motin 		} else {
69589b17223SAlexander Motin nofit:
69689b17223SAlexander Motin 			if (pd->pd_subdisks == 0) {
69789b17223SAlexander Motin 				g_raid_change_disk_state(disk,
69889b17223SAlexander Motin 				    G_RAID_DISK_S_SPARE);
69989b17223SAlexander Motin 			}
70089b17223SAlexander Motin 			return (0);
70189b17223SAlexander Motin 		}
70289b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
70389b17223SAlexander Motin 		    g_raid_get_diskname(disk), disk_pos, vol->v_name);
70489b17223SAlexander Motin 		resurrection = 1;
70589b17223SAlexander Motin 	}
70689b17223SAlexander Motin 
70789b17223SAlexander Motin 	sd = &vol->v_subdisks[disk_pos];
70889b17223SAlexander Motin 
70989b17223SAlexander Motin 	if (resurrection && sd->sd_disk != NULL) {
71089b17223SAlexander Motin 		g_raid_change_disk_state(sd->sd_disk,
71189b17223SAlexander Motin 		    G_RAID_DISK_S_STALE_FAILED);
71289b17223SAlexander Motin 		TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
71389b17223SAlexander Motin 		    sd, sd_next);
71489b17223SAlexander Motin 	}
71589b17223SAlexander Motin 	vol->v_subdisks[disk_pos].sd_disk = disk;
71689b17223SAlexander Motin 	TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
71789b17223SAlexander Motin 
71889b17223SAlexander Motin 	/* Welcome the new disk. */
71989b17223SAlexander Motin 	if (resurrection)
72089b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
72189b17223SAlexander Motin 	else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN)
72289b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
72389b17223SAlexander Motin 	else
72489b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
72589b17223SAlexander Motin 
72689b17223SAlexander Motin 	if (resurrection) {
72789b17223SAlexander Motin 		sd->sd_offset = (off_t)eoff * 512;
72889b17223SAlexander Motin 		sd->sd_size = (off_t)esize * 512;
72989b17223SAlexander Motin 	} else {
73089b17223SAlexander Motin 		sd->sd_offset = (off_t)pd->pd_meta[sdn]->disk_offset * 512;
73189b17223SAlexander Motin 		sd->sd_size = (off_t)pd->pd_meta[sdn]->disk_sectors * 512;
73289b17223SAlexander Motin 	}
73389b17223SAlexander Motin 
73489b17223SAlexander Motin 	if (resurrection) {
73589b17223SAlexander Motin 		/* Stale disk, almost same as new. */
73689b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
73789b17223SAlexander Motin 		    G_RAID_SUBDISK_S_NEW);
73889b17223SAlexander Motin 	} else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN) {
73989b17223SAlexander Motin 		/* Failed disk. */
74089b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
74189b17223SAlexander Motin 		    G_RAID_SUBDISK_S_FAILED);
74289b17223SAlexander Motin 	} else if (meta->disks[md_disk_pos].flags & PROMISE_F_REDIR) {
74389b17223SAlexander Motin 		/* Rebuilding disk. */
74489b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
74589b17223SAlexander Motin 		    G_RAID_SUBDISK_S_REBUILD);
74689b17223SAlexander Motin 		if (pd->pd_meta[sdn]->generation != meta->generation)
74789b17223SAlexander Motin 			sd->sd_rebuild_pos = 0;
74889b17223SAlexander Motin 		else {
74989b17223SAlexander Motin 			sd->sd_rebuild_pos =
75089b17223SAlexander Motin 			    (off_t)pd->pd_meta[sdn]->rebuild_lba * 512;
75189b17223SAlexander Motin 		}
75289b17223SAlexander Motin 	} else if (!(meta->disks[md_disk_pos].flags & PROMISE_F_ONLINE)) {
75389b17223SAlexander Motin 		/* Rebuilding disk. */
75489b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
75589b17223SAlexander Motin 		    G_RAID_SUBDISK_S_NEW);
75689b17223SAlexander Motin 	} else if (pd->pd_meta[sdn]->generation != meta->generation ||
75789b17223SAlexander Motin 	    (meta->status & PROMISE_S_MARKED)) {
75889b17223SAlexander Motin 		/* Stale disk or dirty volume (unclean shutdown). */
75989b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
76089b17223SAlexander Motin 		    G_RAID_SUBDISK_S_STALE);
76189b17223SAlexander Motin 	} else {
76289b17223SAlexander Motin 		/* Up to date disk. */
76389b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
76489b17223SAlexander Motin 		    G_RAID_SUBDISK_S_ACTIVE);
76589b17223SAlexander Motin 	}
76689b17223SAlexander Motin 	g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
76789b17223SAlexander Motin 	    G_RAID_EVENT_SUBDISK);
76889b17223SAlexander Motin 
76989b17223SAlexander Motin 	return (resurrection);
77089b17223SAlexander Motin }
77189b17223SAlexander Motin 
77289b17223SAlexander Motin static void
77389b17223SAlexander Motin g_raid_md_promise_refill(struct g_raid_softc *sc)
77489b17223SAlexander Motin {
77589b17223SAlexander Motin 	struct g_raid_volume *vol;
77689b17223SAlexander Motin 	struct g_raid_subdisk *sd;
77789b17223SAlexander Motin 	struct g_raid_disk *disk;
77889b17223SAlexander Motin 	struct g_raid_md_object *md;
77989b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
78089b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
78189b17223SAlexander Motin 	int update, updated, i, bad;
78289b17223SAlexander Motin 
78389b17223SAlexander Motin 	md = sc->sc_md;
78489b17223SAlexander Motin restart:
78589b17223SAlexander Motin 	updated = 0;
78689b17223SAlexander Motin 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
78789b17223SAlexander Motin 		pv = vol->v_md_data;
78889b17223SAlexander Motin 		if (!pv->pv_started || vol->v_stopping)
78989b17223SAlexander Motin 			continue;
79089b17223SAlexander Motin 
79189b17223SAlexander Motin 		/* Search for subdisk that needs replacement. */
79289b17223SAlexander Motin 		bad = 0;
79389b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
79489b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
79589b17223SAlexander Motin 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
79689b17223SAlexander Motin 			    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
79789b17223SAlexander Motin 			        bad = 1;
79889b17223SAlexander Motin 		}
79989b17223SAlexander Motin 		if (!bad)
80089b17223SAlexander Motin 			continue;
80189b17223SAlexander Motin 
80289b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
80389b17223SAlexander Motin 		    "trying to refill.", vol->v_name);
80489b17223SAlexander Motin 
80589b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
80689b17223SAlexander Motin 			/* Skip failed. */
80789b17223SAlexander Motin 			if (disk->d_state < G_RAID_DISK_S_SPARE)
80889b17223SAlexander Motin 				continue;
80989b17223SAlexander Motin 			/* Skip already used by this volume. */
81089b17223SAlexander Motin 			for (i = 0; i < vol->v_disks_count; i++) {
81189b17223SAlexander Motin 				sd = &vol->v_subdisks[i];
81289b17223SAlexander Motin 				if (sd->sd_disk == disk)
81389b17223SAlexander Motin 					break;
81489b17223SAlexander Motin 			}
81589b17223SAlexander Motin 			if (i < vol->v_disks_count)
81689b17223SAlexander Motin 				continue;
81789b17223SAlexander Motin 
81889b17223SAlexander Motin 			/* Try to use disk if it has empty extents. */
81989b17223SAlexander Motin 			pd = disk->d_md_data;
82089b17223SAlexander Motin 			if (pd->pd_subdisks < PROMISE_MAX_SUBDISKS) {
82189b17223SAlexander Motin 				update =
82289b17223SAlexander Motin 				    g_raid_md_promise_start_disk(disk, -1, vol);
82389b17223SAlexander Motin 			} else
82489b17223SAlexander Motin 				update = 0;
82589b17223SAlexander Motin 			if (update) {
82663607675SAlexander Motin 				updated = 1;
82789b17223SAlexander Motin 				g_raid_md_write_promise(md, vol, NULL, disk);
82889b17223SAlexander Motin 				break;
82989b17223SAlexander Motin 			}
83089b17223SAlexander Motin 		}
83189b17223SAlexander Motin 	}
83289b17223SAlexander Motin 	if (updated)
83389b17223SAlexander Motin 		goto restart;
83489b17223SAlexander Motin }
83589b17223SAlexander Motin 
83689b17223SAlexander Motin static void
83789b17223SAlexander Motin g_raid_md_promise_start(struct g_raid_volume *vol)
83889b17223SAlexander Motin {
83989b17223SAlexander Motin 	struct g_raid_softc *sc;
84089b17223SAlexander Motin 	struct g_raid_subdisk *sd;
84189b17223SAlexander Motin 	struct g_raid_disk *disk;
84289b17223SAlexander Motin 	struct g_raid_md_object *md;
84389b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
84489b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
84589b17223SAlexander Motin 	struct promise_raid_conf *meta;
84689b17223SAlexander Motin 	int i;
84789b17223SAlexander Motin 
84889b17223SAlexander Motin 	sc = vol->v_softc;
84989b17223SAlexander Motin 	md = sc->sc_md;
85089b17223SAlexander Motin 	pv = vol->v_md_data;
85189b17223SAlexander Motin 	meta = pv->pv_meta;
85289b17223SAlexander Motin 
853*fc1de960SAlexander Motin 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
85489b17223SAlexander Motin 	if (meta->type == PROMISE_T_RAID0)
85589b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
85689b17223SAlexander Motin 	else if (meta->type == PROMISE_T_RAID1) {
85789b17223SAlexander Motin 		if (meta->array_width == 1)
85889b17223SAlexander Motin 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
85989b17223SAlexander Motin 		else
86089b17223SAlexander Motin 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
86189b17223SAlexander Motin 	} else if (meta->type == PROMISE_T_RAID3)
86289b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID3;
863*fc1de960SAlexander Motin 	else if (meta->type == PROMISE_T_RAID5) {
86489b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
865*fc1de960SAlexander Motin 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
866*fc1de960SAlexander Motin 	} else if (meta->type == PROMISE_T_SPAN)
86789b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
86889b17223SAlexander Motin 	else if (meta->type == PROMISE_T_JBOD)
86989b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
87089b17223SAlexander Motin 	else
87189b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
87289b17223SAlexander Motin 	vol->v_strip_size = 512 << meta->stripe_shift; //ZZZ
87389b17223SAlexander Motin 	vol->v_disks_count = meta->total_disks;
87489b17223SAlexander Motin 	vol->v_mediasize = (off_t)meta->total_sectors * 512; //ZZZ
875733a1f3fSAlexander Motin 	if (meta->total_sectors_high < 256) /* If value looks sane. */
876733a1f3fSAlexander Motin 		vol->v_mediasize |=
877733a1f3fSAlexander Motin 		    ((off_t)meta->total_sectors_high << 32) * 512; //ZZZ
87889b17223SAlexander Motin 	vol->v_sectorsize = 512; //ZZZ
87989b17223SAlexander Motin 	for (i = 0; i < vol->v_disks_count; i++) {
88089b17223SAlexander Motin 		sd = &vol->v_subdisks[i];
88189b17223SAlexander Motin 		sd->sd_offset = (off_t)meta->disk_offset * 512; //ZZZ
88289b17223SAlexander Motin 		sd->sd_size = (off_t)meta->disk_sectors * 512; //ZZZ
88389b17223SAlexander Motin 	}
88489b17223SAlexander Motin 	g_raid_start_volume(vol);
88589b17223SAlexander Motin 
88689b17223SAlexander Motin 	/* Make all disks found till the moment take their places. */
88789b17223SAlexander Motin 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
88889b17223SAlexander Motin 		pd = disk->d_md_data;
88989b17223SAlexander Motin 		for (i = 0; i < pd->pd_subdisks; i++) {
89089b17223SAlexander Motin 			if (pd->pd_meta[i]->volume_id == meta->volume_id)
89189b17223SAlexander Motin 				g_raid_md_promise_start_disk(disk, i, vol);
89289b17223SAlexander Motin 		}
89389b17223SAlexander Motin 	}
89489b17223SAlexander Motin 
89589b17223SAlexander Motin 	pv->pv_started = 1;
89689b17223SAlexander Motin 	callout_stop(&pv->pv_start_co);
89789b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Volume started.");
89889b17223SAlexander Motin 	g_raid_md_write_promise(md, vol, NULL, NULL);
89989b17223SAlexander Motin 
90089b17223SAlexander Motin 	/* Pickup any STALE/SPARE disks to refill array if needed. */
90189b17223SAlexander Motin 	g_raid_md_promise_refill(sc);
90289b17223SAlexander Motin 
90389b17223SAlexander Motin 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
90489b17223SAlexander Motin }
90589b17223SAlexander Motin 
90689b17223SAlexander Motin static void
90789b17223SAlexander Motin g_raid_promise_go(void *arg)
90889b17223SAlexander Motin {
90989b17223SAlexander Motin 	struct g_raid_volume *vol;
91089b17223SAlexander Motin 	struct g_raid_softc *sc;
91189b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
91289b17223SAlexander Motin 
91389b17223SAlexander Motin 	vol = arg;
91489b17223SAlexander Motin 	pv = vol->v_md_data;
91589b17223SAlexander Motin 	sc = vol->v_softc;
91689b17223SAlexander Motin 	if (!pv->pv_started) {
91789b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
91889b17223SAlexander Motin 		g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
91989b17223SAlexander Motin 		    G_RAID_EVENT_VOLUME);
92089b17223SAlexander Motin 	}
92189b17223SAlexander Motin }
92289b17223SAlexander Motin 
92389b17223SAlexander Motin static void
92489b17223SAlexander Motin g_raid_md_promise_new_disk(struct g_raid_disk *disk)
92589b17223SAlexander Motin {
92689b17223SAlexander Motin 	struct g_raid_softc *sc;
92789b17223SAlexander Motin 	struct g_raid_md_object *md;
92889b17223SAlexander Motin 	struct promise_raid_conf *pdmeta;
92989b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
93089b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
93189b17223SAlexander Motin 	struct g_raid_volume *vol;
93289b17223SAlexander Motin 	int i;
93389b17223SAlexander Motin 	char buf[33];
93489b17223SAlexander Motin 
93589b17223SAlexander Motin 	sc = disk->d_softc;
93689b17223SAlexander Motin 	md = sc->sc_md;
93789b17223SAlexander Motin 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
93889b17223SAlexander Motin 
93989b17223SAlexander Motin 	if (pd->pd_subdisks == 0) {
94089b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
94189b17223SAlexander Motin 		g_raid_md_promise_refill(sc);
94289b17223SAlexander Motin 		return;
94389b17223SAlexander Motin 	}
94489b17223SAlexander Motin 
94589b17223SAlexander Motin 	for (i = 0; i < pd->pd_subdisks; i++) {
94689b17223SAlexander Motin 		pdmeta = pd->pd_meta[i];
94789b17223SAlexander Motin 
94889b17223SAlexander Motin 		/* Look for volume with matching ID. */
94989b17223SAlexander Motin 		vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id);
95089b17223SAlexander Motin 		if (vol == NULL) {
95189b17223SAlexander Motin 			promise_meta_get_name(pdmeta, buf);
95289b17223SAlexander Motin 			vol = g_raid_create_volume(sc, buf, pdmeta->array_number);
95389b17223SAlexander Motin 			pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO);
95489b17223SAlexander Motin 			pv->pv_id = pdmeta->volume_id;
95589b17223SAlexander Motin 			vol->v_md_data = pv;
95689b17223SAlexander Motin 			callout_init(&pv->pv_start_co, 1);
95789b17223SAlexander Motin 			callout_reset(&pv->pv_start_co,
95889b17223SAlexander Motin 			    g_raid_start_timeout * hz,
95989b17223SAlexander Motin 			    g_raid_promise_go, vol);
96089b17223SAlexander Motin 		} else
96189b17223SAlexander Motin 			pv = vol->v_md_data;
96289b17223SAlexander Motin 
96389b17223SAlexander Motin 		/* If we haven't started yet - check metadata freshness. */
96489b17223SAlexander Motin 		if (pv->pv_meta == NULL || !pv->pv_started) {
96589b17223SAlexander Motin 			if (pv->pv_meta == NULL ||
96689b17223SAlexander Motin 			    ((int16_t)(pdmeta->generation - pv->pv_generation)) > 0) {
96789b17223SAlexander Motin 				G_RAID_DEBUG1(1, sc, "Newer disk");
96889b17223SAlexander Motin 				if (pv->pv_meta != NULL)
96989b17223SAlexander Motin 					free(pv->pv_meta, M_MD_PROMISE);
97089b17223SAlexander Motin 				pv->pv_meta = promise_meta_copy(pdmeta);
97189b17223SAlexander Motin 				pv->pv_generation = pv->pv_meta->generation;
97289b17223SAlexander Motin 				pv->pv_disks_present = 1;
97389b17223SAlexander Motin 			} else if (pdmeta->generation == pv->pv_generation) {
97489b17223SAlexander Motin 				pv->pv_disks_present++;
97589b17223SAlexander Motin 				G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
97689b17223SAlexander Motin 				    pv->pv_disks_present,
97789b17223SAlexander Motin 				    pv->pv_meta->total_disks);
97889b17223SAlexander Motin 			} else {
97989b17223SAlexander Motin 				G_RAID_DEBUG1(1, sc, "Older disk");
98089b17223SAlexander Motin 			}
98189b17223SAlexander Motin 		}
98289b17223SAlexander Motin 	}
98389b17223SAlexander Motin 
98489b17223SAlexander Motin 	for (i = 0; i < pd->pd_subdisks; i++) {
98589b17223SAlexander Motin 		pdmeta = pd->pd_meta[i];
98689b17223SAlexander Motin 
98789b17223SAlexander Motin 		/* Look for volume with matching ID. */
98889b17223SAlexander Motin 		vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id);
98989b17223SAlexander Motin 		if (vol == NULL)
99089b17223SAlexander Motin 			continue;
99189b17223SAlexander Motin 		pv = vol->v_md_data;
99289b17223SAlexander Motin 
99389b17223SAlexander Motin 		if (pv->pv_started) {
99489b17223SAlexander Motin 			if (g_raid_md_promise_start_disk(disk, i, vol))
99589b17223SAlexander Motin 				g_raid_md_write_promise(md, vol, NULL, NULL);
99689b17223SAlexander Motin 		} else {
99789b17223SAlexander Motin 			/* If we collected all needed disks - start array. */
99889b17223SAlexander Motin 			if (pv->pv_disks_present == pv->pv_meta->total_disks)
99989b17223SAlexander Motin 				g_raid_md_promise_start(vol);
100089b17223SAlexander Motin 		}
100189b17223SAlexander Motin 	}
100289b17223SAlexander Motin }
100389b17223SAlexander Motin 
100489b17223SAlexander Motin static int
100589b17223SAlexander Motin g_raid_md_create_promise(struct g_raid_md_object *md, struct g_class *mp,
100689b17223SAlexander Motin     struct g_geom **gp)
100789b17223SAlexander Motin {
100889b17223SAlexander Motin 	struct g_geom *geom;
100989b17223SAlexander Motin 	struct g_raid_softc *sc;
101089b17223SAlexander Motin 
101189b17223SAlexander Motin 	/* Search for existing node. */
101289b17223SAlexander Motin 	LIST_FOREACH(geom, &mp->geom, geom) {
101389b17223SAlexander Motin 		sc = geom->softc;
101489b17223SAlexander Motin 		if (sc == NULL)
101589b17223SAlexander Motin 			continue;
101689b17223SAlexander Motin 		if (sc->sc_stopping != 0)
101789b17223SAlexander Motin 			continue;
101889b17223SAlexander Motin 		if (sc->sc_md->mdo_class != md->mdo_class)
101989b17223SAlexander Motin 			continue;
102089b17223SAlexander Motin 		break;
102189b17223SAlexander Motin 	}
102289b17223SAlexander Motin 	if (geom != NULL) {
102389b17223SAlexander Motin 		*gp = geom;
102489b17223SAlexander Motin 		return (G_RAID_MD_TASTE_EXISTING);
102589b17223SAlexander Motin 	}
102689b17223SAlexander Motin 
102789b17223SAlexander Motin 	/* Create new one if not found. */
102889b17223SAlexander Motin 	sc = g_raid_create_node(mp, "Promise", md);
102989b17223SAlexander Motin 	if (sc == NULL)
103089b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
103189b17223SAlexander Motin 	md->mdo_softc = sc;
103289b17223SAlexander Motin 	*gp = sc->sc_geom;
103389b17223SAlexander Motin 	return (G_RAID_MD_TASTE_NEW);
103489b17223SAlexander Motin }
103589b17223SAlexander Motin 
103689b17223SAlexander Motin static int
103789b17223SAlexander Motin g_raid_md_taste_promise(struct g_raid_md_object *md, struct g_class *mp,
103889b17223SAlexander Motin                               struct g_consumer *cp, struct g_geom **gp)
103989b17223SAlexander Motin {
104089b17223SAlexander Motin 	struct g_consumer *rcp;
104189b17223SAlexander Motin 	struct g_provider *pp;
104289b17223SAlexander Motin 	struct g_raid_softc *sc;
104389b17223SAlexander Motin 	struct g_raid_disk *disk;
104489b17223SAlexander Motin 	struct promise_raid_conf *meta, *metaarr[4];
104589b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
104689b17223SAlexander Motin 	struct g_geom *geom;
104789b17223SAlexander Motin 	int error, i, j, result, len, subdisks;
104889b17223SAlexander Motin 	char name[16];
104989b17223SAlexander Motin 	uint16_t vendor;
105089b17223SAlexander Motin 
105189b17223SAlexander Motin 	G_RAID_DEBUG(1, "Tasting Promise on %s", cp->provider->name);
105289b17223SAlexander Motin 	pp = cp->provider;
105389b17223SAlexander Motin 
105489b17223SAlexander Motin 	/* Read metadata from device. */
105589b17223SAlexander Motin 	meta = NULL;
105689b17223SAlexander Motin 	vendor = 0xffff;
105789b17223SAlexander Motin 	if (g_access(cp, 1, 0, 0) != 0)
105889b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
105989b17223SAlexander Motin 	g_topology_unlock();
106089b17223SAlexander Motin 	len = 2;
106189b17223SAlexander Motin 	if (pp->geom->rank == 1)
106289b17223SAlexander Motin 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
106389b17223SAlexander Motin 	subdisks = promise_meta_read(cp, metaarr);
106489b17223SAlexander Motin 	g_topology_lock();
106589b17223SAlexander Motin 	g_access(cp, -1, 0, 0);
106689b17223SAlexander Motin 	if (subdisks == 0) {
106789b17223SAlexander Motin 		if (g_raid_aggressive_spare) {
106889b17223SAlexander Motin 			if (vendor == 0x105a || vendor == 0x1002) {
106989b17223SAlexander Motin 				G_RAID_DEBUG(1,
107089b17223SAlexander Motin 				    "No Promise metadata, forcing spare.");
107189b17223SAlexander Motin 				goto search;
107289b17223SAlexander Motin 			} else {
107389b17223SAlexander Motin 				G_RAID_DEBUG(1,
107489b17223SAlexander Motin 				    "Promise/ATI vendor mismatch "
107589b17223SAlexander Motin 				    "0x%04x != 0x105a/0x1002",
107689b17223SAlexander Motin 				    vendor);
107789b17223SAlexander Motin 			}
107889b17223SAlexander Motin 		}
107989b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
108089b17223SAlexander Motin 	}
108189b17223SAlexander Motin 
108289b17223SAlexander Motin 	/* Metadata valid. Print it. */
108389b17223SAlexander Motin 	for (i = 0; i < subdisks; i++)
108489b17223SAlexander Motin 		g_raid_md_promise_print(metaarr[i]);
108589b17223SAlexander Motin 
108689b17223SAlexander Motin 	/* Purge meaningless (empty/spare) records. */
108789b17223SAlexander Motin 	for (i = 0; i < subdisks; ) {
108889b17223SAlexander Motin 		if (metaarr[i]->disk.flags & PROMISE_F_ASSIGNED) {
108989b17223SAlexander Motin 			i++;
109089b17223SAlexander Motin 			continue;
109189b17223SAlexander Motin 		}
109289b17223SAlexander Motin 		free(metaarr[i], M_MD_PROMISE);
109389b17223SAlexander Motin 		for (j = i; j < subdisks - 1; j++)
109489b17223SAlexander Motin 			metaarr[i] = metaarr[j + 1];
109589b17223SAlexander Motin 		metaarr[PROMISE_MAX_SUBDISKS - 1] = NULL;
109689b17223SAlexander Motin 		subdisks--;
109789b17223SAlexander Motin 	}
109889b17223SAlexander Motin 
109989b17223SAlexander Motin search:
110089b17223SAlexander Motin 	/* Search for matching node. */
110189b17223SAlexander Motin 	sc = NULL;
110289b17223SAlexander Motin 	LIST_FOREACH(geom, &mp->geom, geom) {
110389b17223SAlexander Motin 		sc = geom->softc;
110489b17223SAlexander Motin 		if (sc == NULL)
110589b17223SAlexander Motin 			continue;
110689b17223SAlexander Motin 		if (sc->sc_stopping != 0)
110789b17223SAlexander Motin 			continue;
110889b17223SAlexander Motin 		if (sc->sc_md->mdo_class != md->mdo_class)
110989b17223SAlexander Motin 			continue;
111089b17223SAlexander Motin 		break;
111189b17223SAlexander Motin 	}
111289b17223SAlexander Motin 
111389b17223SAlexander Motin 	/* Found matching node. */
111489b17223SAlexander Motin 	if (geom != NULL) {
111589b17223SAlexander Motin 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
111689b17223SAlexander Motin 		result = G_RAID_MD_TASTE_EXISTING;
111789b17223SAlexander Motin 
111889b17223SAlexander Motin 	} else { /* Not found matching node -- create one. */
111989b17223SAlexander Motin 		result = G_RAID_MD_TASTE_NEW;
112089b17223SAlexander Motin 		snprintf(name, sizeof(name), "Promise");
112189b17223SAlexander Motin 		sc = g_raid_create_node(mp, name, md);
112289b17223SAlexander Motin 		md->mdo_softc = sc;
112389b17223SAlexander Motin 		geom = sc->sc_geom;
112489b17223SAlexander Motin 	}
112589b17223SAlexander Motin 
112689b17223SAlexander Motin 	rcp = g_new_consumer(geom);
112789b17223SAlexander Motin 	g_attach(rcp, pp);
112889b17223SAlexander Motin 	if (g_access(rcp, 1, 1, 1) != 0)
112989b17223SAlexander Motin 		; //goto fail1;
113089b17223SAlexander Motin 
113189b17223SAlexander Motin 	g_topology_unlock();
113289b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
113389b17223SAlexander Motin 
113489b17223SAlexander Motin 	pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
113589b17223SAlexander Motin 	pd->pd_subdisks = subdisks;
113689b17223SAlexander Motin 	for (i = 0; i < subdisks; i++)
113789b17223SAlexander Motin 		pd->pd_meta[i] = metaarr[i];
113889b17223SAlexander Motin 	disk = g_raid_create_disk(sc);
113989b17223SAlexander Motin 	disk->d_md_data = (void *)pd;
114089b17223SAlexander Motin 	disk->d_consumer = rcp;
114189b17223SAlexander Motin 	rcp->private = disk;
114289b17223SAlexander Motin 
114389b17223SAlexander Motin 	/* Read kernel dumping information. */
114489b17223SAlexander Motin 	disk->d_kd.offset = 0;
114589b17223SAlexander Motin 	disk->d_kd.length = OFF_MAX;
114689b17223SAlexander Motin 	len = sizeof(disk->d_kd);
114789b17223SAlexander Motin 	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
114889b17223SAlexander Motin 	if (disk->d_kd.di.dumper == NULL)
114989b17223SAlexander Motin 		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
115089b17223SAlexander Motin 		    rcp->provider->name, error);
115189b17223SAlexander Motin 
115289b17223SAlexander Motin 	g_raid_md_promise_new_disk(disk);
115389b17223SAlexander Motin 
115489b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
115589b17223SAlexander Motin 	g_topology_lock();
115689b17223SAlexander Motin 	*gp = geom;
115789b17223SAlexander Motin 	return (result);
115889b17223SAlexander Motin }
115989b17223SAlexander Motin 
116089b17223SAlexander Motin static int
116189b17223SAlexander Motin g_raid_md_event_promise(struct g_raid_md_object *md,
116289b17223SAlexander Motin     struct g_raid_disk *disk, u_int event)
116389b17223SAlexander Motin {
116489b17223SAlexander Motin 	struct g_raid_softc *sc;
116589b17223SAlexander Motin 
116689b17223SAlexander Motin 	sc = md->mdo_softc;
116789b17223SAlexander Motin 	if (disk == NULL)
116889b17223SAlexander Motin 		return (-1);
116989b17223SAlexander Motin 	switch (event) {
117089b17223SAlexander Motin 	case G_RAID_DISK_E_DISCONNECTED:
117189b17223SAlexander Motin 		/* Delete disk. */
117289b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
117389b17223SAlexander Motin 		g_raid_destroy_disk(disk);
117489b17223SAlexander Motin 		g_raid_md_promise_purge_volumes(sc);
117589b17223SAlexander Motin 
117689b17223SAlexander Motin 		/* Write updated metadata to all disks. */
117789b17223SAlexander Motin 		g_raid_md_write_promise(md, NULL, NULL, NULL);
117889b17223SAlexander Motin 
117989b17223SAlexander Motin 		/* Check if anything left. */
118089b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) == 0)
118189b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
118289b17223SAlexander Motin 		else
118389b17223SAlexander Motin 			g_raid_md_promise_refill(sc);
118489b17223SAlexander Motin 		return (0);
118589b17223SAlexander Motin 	}
118689b17223SAlexander Motin 	return (-2);
118789b17223SAlexander Motin }
118889b17223SAlexander Motin 
118989b17223SAlexander Motin static int
119089b17223SAlexander Motin g_raid_md_volume_event_promise(struct g_raid_md_object *md,
119189b17223SAlexander Motin     struct g_raid_volume *vol, u_int event)
119289b17223SAlexander Motin {
119389b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
119489b17223SAlexander Motin 
119589b17223SAlexander Motin 	pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
119689b17223SAlexander Motin 	switch (event) {
119789b17223SAlexander Motin 	case G_RAID_VOLUME_E_STARTMD:
119889b17223SAlexander Motin 		if (!pv->pv_started)
119989b17223SAlexander Motin 			g_raid_md_promise_start(vol);
120089b17223SAlexander Motin 		return (0);
120189b17223SAlexander Motin 	}
120289b17223SAlexander Motin 	return (-2);
120389b17223SAlexander Motin }
120489b17223SAlexander Motin 
120589b17223SAlexander Motin static int
120689b17223SAlexander Motin g_raid_md_ctl_promise(struct g_raid_md_object *md,
120789b17223SAlexander Motin     struct gctl_req *req)
120889b17223SAlexander Motin {
120989b17223SAlexander Motin 	struct g_raid_softc *sc;
121089b17223SAlexander Motin 	struct g_raid_volume *vol, *vol1;
121189b17223SAlexander Motin 	struct g_raid_subdisk *sd;
121289b17223SAlexander Motin 	struct g_raid_disk *disk, *disks[PROMISE_MAX_DISKS];
121389b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
121489b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
121589b17223SAlexander Motin 	struct g_consumer *cp;
121689b17223SAlexander Motin 	struct g_provider *pp;
121789b17223SAlexander Motin 	char arg[16];
121889b17223SAlexander Motin 	const char *verb, *volname, *levelname, *diskname;
121989b17223SAlexander Motin 	char *tmp;
122089b17223SAlexander Motin 	int *nargs, *force;
122189b17223SAlexander Motin 	off_t size, sectorsize, strip;
122289b17223SAlexander Motin 	intmax_t *sizearg, *striparg;
122389b17223SAlexander Motin 	uint32_t offs[PROMISE_MAX_DISKS], esize;
122489b17223SAlexander Motin 	int numdisks, i, len, level, qual;
122589b17223SAlexander Motin 	int error;
122689b17223SAlexander Motin 
122789b17223SAlexander Motin 	sc = md->mdo_softc;
122889b17223SAlexander Motin 	verb = gctl_get_param(req, "verb", NULL);
122989b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
123089b17223SAlexander Motin 	error = 0;
123189b17223SAlexander Motin 	if (strcmp(verb, "label") == 0) {
123289b17223SAlexander Motin 
123389b17223SAlexander Motin 		if (*nargs < 4) {
123489b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
123589b17223SAlexander Motin 			return (-1);
123689b17223SAlexander Motin 		}
123789b17223SAlexander Motin 		volname = gctl_get_asciiparam(req, "arg1");
123889b17223SAlexander Motin 		if (volname == NULL) {
123989b17223SAlexander Motin 			gctl_error(req, "No volume name.");
124089b17223SAlexander Motin 			return (-2);
124189b17223SAlexander Motin 		}
124289b17223SAlexander Motin 		levelname = gctl_get_asciiparam(req, "arg2");
124389b17223SAlexander Motin 		if (levelname == NULL) {
124489b17223SAlexander Motin 			gctl_error(req, "No RAID level.");
124589b17223SAlexander Motin 			return (-3);
124689b17223SAlexander Motin 		}
1247*fc1de960SAlexander Motin 		if (strcasecmp(levelname, "RAID5") == 0)
1248*fc1de960SAlexander Motin 			levelname = "RAID5LA";
124989b17223SAlexander Motin 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
125089b17223SAlexander Motin 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
125189b17223SAlexander Motin 			return (-4);
125289b17223SAlexander Motin 		}
125389b17223SAlexander Motin 		numdisks = *nargs - 3;
125489b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
125589b17223SAlexander Motin 		if (!g_raid_md_promise_supported(level, qual, numdisks,
125689b17223SAlexander Motin 		    force ? *force : 0)) {
125789b17223SAlexander Motin 			gctl_error(req, "Unsupported RAID level "
125889b17223SAlexander Motin 			    "(0x%02x/0x%02x), or number of disks (%d).",
125989b17223SAlexander Motin 			    level, qual, numdisks);
126089b17223SAlexander Motin 			return (-5);
126189b17223SAlexander Motin 		}
126289b17223SAlexander Motin 
126389b17223SAlexander Motin 		/* Search for disks, connect them and probe. */
126489b17223SAlexander Motin 		size = INT64_MAX;
126589b17223SAlexander Motin 		sectorsize = 0;
126689b17223SAlexander Motin 		bzero(disks, sizeof(disks));
126789b17223SAlexander Motin 		bzero(offs, sizeof(offs));
126889b17223SAlexander Motin 		for (i = 0; i < numdisks; i++) {
126989b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
127089b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
127189b17223SAlexander Motin 			if (diskname == NULL) {
127289b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
127389b17223SAlexander Motin 				error = -6;
127489b17223SAlexander Motin 				break;
127589b17223SAlexander Motin 			}
127689b17223SAlexander Motin 			if (strcmp(diskname, "NONE") == 0)
127789b17223SAlexander Motin 				continue;
127889b17223SAlexander Motin 
127989b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
128089b17223SAlexander Motin 				if (disk->d_consumer != NULL &&
128189b17223SAlexander Motin 				    disk->d_consumer->provider != NULL &&
128289b17223SAlexander Motin 				    strcmp(disk->d_consumer->provider->name,
128389b17223SAlexander Motin 				     diskname) == 0)
128489b17223SAlexander Motin 					break;
128589b17223SAlexander Motin 			}
128689b17223SAlexander Motin 			if (disk != NULL) {
128789b17223SAlexander Motin 				if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
128889b17223SAlexander Motin 					gctl_error(req, "Disk '%s' is in a "
128989b17223SAlexander Motin 					    "wrong state (%s).", diskname,
129089b17223SAlexander Motin 					    g_raid_disk_state2str(disk->d_state));
129189b17223SAlexander Motin 					error = -7;
129289b17223SAlexander Motin 					break;
129389b17223SAlexander Motin 				}
129489b17223SAlexander Motin 				pd = disk->d_md_data;
129589b17223SAlexander Motin 				if (pd->pd_subdisks >= PROMISE_MAX_SUBDISKS) {
129689b17223SAlexander Motin 					gctl_error(req, "Disk '%s' already "
129789b17223SAlexander Motin 					    "used by %d volumes.",
129889b17223SAlexander Motin 					    diskname, pd->pd_subdisks);
129989b17223SAlexander Motin 					error = -7;
130089b17223SAlexander Motin 					break;
130189b17223SAlexander Motin 				}
130289b17223SAlexander Motin 				pp = disk->d_consumer->provider;
130389b17223SAlexander Motin 				disks[i] = disk;
130489b17223SAlexander Motin 				promise_meta_unused_range(pd->pd_meta,
130589b17223SAlexander Motin 				    pd->pd_subdisks,
130689b17223SAlexander Motin 				    pp->mediasize / pp->sectorsize,
130789b17223SAlexander Motin 				    &offs[i], &esize);
130889b17223SAlexander Motin 				size = MIN(size, (off_t)esize * pp->sectorsize);
130989b17223SAlexander Motin 				sectorsize = MAX(sectorsize, pp->sectorsize);
131089b17223SAlexander Motin 				continue;
131189b17223SAlexander Motin 			}
131289b17223SAlexander Motin 
131389b17223SAlexander Motin 			g_topology_lock();
131489b17223SAlexander Motin 			cp = g_raid_open_consumer(sc, diskname);
131589b17223SAlexander Motin 			if (cp == NULL) {
131689b17223SAlexander Motin 				gctl_error(req, "Can't open disk '%s'.",
131789b17223SAlexander Motin 				    diskname);
131889b17223SAlexander Motin 				g_topology_unlock();
131989b17223SAlexander Motin 				error = -8;
132089b17223SAlexander Motin 				break;
132189b17223SAlexander Motin 			}
132289b17223SAlexander Motin 			pp = cp->provider;
132389b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
132489b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
132589b17223SAlexander Motin 			disk->d_md_data = (void *)pd;
132689b17223SAlexander Motin 			disk->d_consumer = cp;
132789b17223SAlexander Motin 			disks[i] = disk;
132889b17223SAlexander Motin 			cp->private = disk;
132989b17223SAlexander Motin 			g_topology_unlock();
133089b17223SAlexander Motin 
1331733a1f3fSAlexander Motin 			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
1332733a1f3fSAlexander Motin 				gctl_error(req,
1333733a1f3fSAlexander Motin 				    "Disk '%s' is too big.", diskname);
1334733a1f3fSAlexander Motin 				error = -8;
1335733a1f3fSAlexander Motin 				break;
1336733a1f3fSAlexander Motin 			}
1337733a1f3fSAlexander Motin 
133889b17223SAlexander Motin 			/* Read kernel dumping information. */
133989b17223SAlexander Motin 			disk->d_kd.offset = 0;
134089b17223SAlexander Motin 			disk->d_kd.length = OFF_MAX;
134189b17223SAlexander Motin 			len = sizeof(disk->d_kd);
134289b17223SAlexander Motin 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
134389b17223SAlexander Motin 			if (disk->d_kd.di.dumper == NULL)
134489b17223SAlexander Motin 				G_RAID_DEBUG1(2, sc,
134589b17223SAlexander Motin 				    "Dumping not supported by %s.",
134689b17223SAlexander Motin 				    cp->provider->name);
134789b17223SAlexander Motin 
134889b17223SAlexander Motin 			/* Reserve some space for metadata. */
134989b17223SAlexander Motin 			size = MIN(size, pp->mediasize - 131072llu * pp->sectorsize);
135089b17223SAlexander Motin 			sectorsize = MAX(sectorsize, pp->sectorsize);
135189b17223SAlexander Motin 		}
135289b17223SAlexander Motin 		if (error != 0) {
135389b17223SAlexander Motin 			for (i = 0; i < numdisks; i++) {
135489b17223SAlexander Motin 				if (disks[i] != NULL &&
135589b17223SAlexander Motin 				    disks[i]->d_state == G_RAID_DISK_S_NONE)
135689b17223SAlexander Motin 					g_raid_destroy_disk(disks[i]);
135789b17223SAlexander Motin 			}
135889b17223SAlexander Motin 			return (error);
135989b17223SAlexander Motin 		}
136089b17223SAlexander Motin 
136114e2cd0aSAlexander Motin 		if (sectorsize <= 0) {
136214e2cd0aSAlexander Motin 			gctl_error(req, "Can't get sector size.");
136314e2cd0aSAlexander Motin 			return (-8);
136414e2cd0aSAlexander Motin 		}
136514e2cd0aSAlexander Motin 
136689b17223SAlexander Motin 		/* Handle size argument. */
136789b17223SAlexander Motin 		len = sizeof(*sizearg);
136889b17223SAlexander Motin 		sizearg = gctl_get_param(req, "size", &len);
136989b17223SAlexander Motin 		if (sizearg != NULL && len == sizeof(*sizearg) &&
137089b17223SAlexander Motin 		    *sizearg > 0) {
137189b17223SAlexander Motin 			if (*sizearg > size) {
137289b17223SAlexander Motin 				gctl_error(req, "Size too big %lld > %lld.",
137389b17223SAlexander Motin 				    (long long)*sizearg, (long long)size);
137489b17223SAlexander Motin 				return (-9);
137589b17223SAlexander Motin 			}
137689b17223SAlexander Motin 			size = *sizearg;
137789b17223SAlexander Motin 		}
137889b17223SAlexander Motin 
137989b17223SAlexander Motin 		/* Handle strip argument. */
138089b17223SAlexander Motin 		strip = 131072;
138189b17223SAlexander Motin 		len = sizeof(*striparg);
138289b17223SAlexander Motin 		striparg = gctl_get_param(req, "strip", &len);
138389b17223SAlexander Motin 		if (striparg != NULL && len == sizeof(*striparg) &&
138489b17223SAlexander Motin 		    *striparg > 0) {
138589b17223SAlexander Motin 			if (*striparg < sectorsize) {
138689b17223SAlexander Motin 				gctl_error(req, "Strip size too small.");
138789b17223SAlexander Motin 				return (-10);
138889b17223SAlexander Motin 			}
138989b17223SAlexander Motin 			if (*striparg % sectorsize != 0) {
139089b17223SAlexander Motin 				gctl_error(req, "Incorrect strip size.");
139189b17223SAlexander Motin 				return (-11);
139289b17223SAlexander Motin 			}
139389b17223SAlexander Motin 			strip = *striparg;
139489b17223SAlexander Motin 		}
139589b17223SAlexander Motin 
139689b17223SAlexander Motin 		/* Round size down to strip or sector. */
139789b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID1 ||
139889b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_SINGLE ||
139989b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_CONCAT)
140089b17223SAlexander Motin 			size -= (size % sectorsize);
140189b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
140289b17223SAlexander Motin 		    (numdisks & 1) != 0)
140389b17223SAlexander Motin 			size -= (size % (2 * strip));
140489b17223SAlexander Motin 		else
140589b17223SAlexander Motin 			size -= (size % strip);
140689b17223SAlexander Motin 		if (size <= 0) {
140789b17223SAlexander Motin 			gctl_error(req, "Size too small.");
140889b17223SAlexander Motin 			return (-13);
140989b17223SAlexander Motin 		}
141089b17223SAlexander Motin 		if (size > 0xffffffffllu * sectorsize) {
141189b17223SAlexander Motin 			gctl_error(req, "Size too big.");
141289b17223SAlexander Motin 			return (-14);
141389b17223SAlexander Motin 		}
141489b17223SAlexander Motin 
141589b17223SAlexander Motin 		/* We have all we need, create things: volume, ... */
141689b17223SAlexander Motin 		pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO);
141789b17223SAlexander Motin 		arc4rand(&pv->pv_id, sizeof(pv->pv_id), 0);
141889b17223SAlexander Motin 		pv->pv_generation = 0;
141989b17223SAlexander Motin 		pv->pv_started = 1;
142089b17223SAlexander Motin 		vol = g_raid_create_volume(sc, volname, -1);
142189b17223SAlexander Motin 		vol->v_md_data = pv;
142289b17223SAlexander Motin 		vol->v_raid_level = level;
1423*fc1de960SAlexander Motin 		vol->v_raid_level_qualifier = qual;
142489b17223SAlexander Motin 		vol->v_strip_size = strip;
142589b17223SAlexander Motin 		vol->v_disks_count = numdisks;
142689b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID0 ||
142789b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_CONCAT ||
142889b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_SINGLE)
142989b17223SAlexander Motin 			vol->v_mediasize = size * numdisks;
143089b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1)
143189b17223SAlexander Motin 			vol->v_mediasize = size;
143289b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID3 ||
143389b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_RAID5)
143489b17223SAlexander Motin 			vol->v_mediasize = size * (numdisks - 1);
143589b17223SAlexander Motin 		else { /* RAID1E */
143689b17223SAlexander Motin 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
143789b17223SAlexander Motin 			    strip;
143889b17223SAlexander Motin 		}
143989b17223SAlexander Motin 		vol->v_sectorsize = sectorsize;
144089b17223SAlexander Motin 		g_raid_start_volume(vol);
144189b17223SAlexander Motin 
144289b17223SAlexander Motin 		/* , and subdisks. */
144389b17223SAlexander Motin 		for (i = 0; i < numdisks; i++) {
144489b17223SAlexander Motin 			disk = disks[i];
144589b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
144689b17223SAlexander Motin 			sd->sd_disk = disk;
144789b17223SAlexander Motin 			sd->sd_offset = (off_t)offs[i] * 512;
144889b17223SAlexander Motin 			sd->sd_size = size;
144989b17223SAlexander Motin 			if (disk == NULL)
145089b17223SAlexander Motin 				continue;
145189b17223SAlexander Motin 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
145289b17223SAlexander Motin 			g_raid_change_disk_state(disk,
145389b17223SAlexander Motin 			    G_RAID_DISK_S_ACTIVE);
145489b17223SAlexander Motin 			g_raid_change_subdisk_state(sd,
145589b17223SAlexander Motin 			    G_RAID_SUBDISK_S_ACTIVE);
145689b17223SAlexander Motin 			g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
145789b17223SAlexander Motin 			    G_RAID_EVENT_SUBDISK);
145889b17223SAlexander Motin 		}
145989b17223SAlexander Motin 
146089b17223SAlexander Motin 		/* Write metadata based on created entities. */
146189b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Array started.");
146289b17223SAlexander Motin 		g_raid_md_write_promise(md, vol, NULL, NULL);
146389b17223SAlexander Motin 
146489b17223SAlexander Motin 		/* Pickup any STALE/SPARE disks to refill array if needed. */
146589b17223SAlexander Motin 		g_raid_md_promise_refill(sc);
146689b17223SAlexander Motin 
146789b17223SAlexander Motin 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
146889b17223SAlexander Motin 		    G_RAID_EVENT_VOLUME);
146989b17223SAlexander Motin 		return (0);
147089b17223SAlexander Motin 	}
147189b17223SAlexander Motin 	if (strcmp(verb, "add") == 0) {
147289b17223SAlexander Motin 
147389b17223SAlexander Motin 		gctl_error(req, "`add` command is not applicable, "
147489b17223SAlexander Motin 		    "use `label` instead.");
147589b17223SAlexander Motin 		return (-99);
147689b17223SAlexander Motin 	}
147789b17223SAlexander Motin 	if (strcmp(verb, "delete") == 0) {
147889b17223SAlexander Motin 
147989b17223SAlexander Motin 		/* Full node destruction. */
148089b17223SAlexander Motin 		if (*nargs == 1) {
148189b17223SAlexander Motin 			/* Check if some volume is still open. */
148289b17223SAlexander Motin 			force = gctl_get_paraml(req, "force", sizeof(*force));
148389b17223SAlexander Motin 			if (force != NULL && *force == 0 &&
148489b17223SAlexander Motin 			    g_raid_nopens(sc) != 0) {
148589b17223SAlexander Motin 				gctl_error(req, "Some volume is still open.");
148689b17223SAlexander Motin 				return (-4);
148789b17223SAlexander Motin 			}
148889b17223SAlexander Motin 
148989b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
149089b17223SAlexander Motin 				if (disk->d_consumer)
149189b17223SAlexander Motin 					promise_meta_erase(disk->d_consumer);
149289b17223SAlexander Motin 			}
149389b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
149489b17223SAlexander Motin 			return (0);
149589b17223SAlexander Motin 		}
149689b17223SAlexander Motin 
149789b17223SAlexander Motin 		/* Destroy specified volume. If it was last - all node. */
149889b17223SAlexander Motin 		if (*nargs != 2) {
149989b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
150089b17223SAlexander Motin 			return (-1);
150189b17223SAlexander Motin 		}
150289b17223SAlexander Motin 		volname = gctl_get_asciiparam(req, "arg1");
150389b17223SAlexander Motin 		if (volname == NULL) {
150489b17223SAlexander Motin 			gctl_error(req, "No volume name.");
150589b17223SAlexander Motin 			return (-2);
150689b17223SAlexander Motin 		}
150789b17223SAlexander Motin 
150889b17223SAlexander Motin 		/* Search for volume. */
150989b17223SAlexander Motin 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
151089b17223SAlexander Motin 			if (strcmp(vol->v_name, volname) == 0)
151189b17223SAlexander Motin 				break;
151289b17223SAlexander Motin 		}
151389b17223SAlexander Motin 		if (vol == NULL) {
151489b17223SAlexander Motin 			i = strtol(volname, &tmp, 10);
151589b17223SAlexander Motin 			if (verb != volname && tmp[0] == 0) {
151689b17223SAlexander Motin 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
151789b17223SAlexander Motin 					if (vol->v_global_id == i)
151889b17223SAlexander Motin 						break;
151989b17223SAlexander Motin 				}
152089b17223SAlexander Motin 			}
152189b17223SAlexander Motin 		}
152289b17223SAlexander Motin 		if (vol == NULL) {
152389b17223SAlexander Motin 			gctl_error(req, "Volume '%s' not found.", volname);
152489b17223SAlexander Motin 			return (-3);
152589b17223SAlexander Motin 		}
152689b17223SAlexander Motin 
152789b17223SAlexander Motin 		/* Check if volume is still open. */
152889b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
152989b17223SAlexander Motin 		if (force != NULL && *force == 0 &&
153089b17223SAlexander Motin 		    vol->v_provider_open != 0) {
153189b17223SAlexander Motin 			gctl_error(req, "Volume is still open.");
153289b17223SAlexander Motin 			return (-4);
153389b17223SAlexander Motin 		}
153489b17223SAlexander Motin 
153589b17223SAlexander Motin 		/* Destroy volume and potentially node. */
153689b17223SAlexander Motin 		i = 0;
153789b17223SAlexander Motin 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
153889b17223SAlexander Motin 			i++;
153989b17223SAlexander Motin 		if (i >= 2) {
154089b17223SAlexander Motin 			g_raid_destroy_volume(vol);
154189b17223SAlexander Motin 			g_raid_md_promise_purge_disks(sc);
154289b17223SAlexander Motin 			g_raid_md_write_promise(md, NULL, NULL, NULL);
154389b17223SAlexander Motin 		} else {
154489b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
154589b17223SAlexander Motin 				if (disk->d_consumer)
154689b17223SAlexander Motin 					promise_meta_erase(disk->d_consumer);
154789b17223SAlexander Motin 			}
154889b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
154989b17223SAlexander Motin 		}
155089b17223SAlexander Motin 		return (0);
155189b17223SAlexander Motin 	}
155289b17223SAlexander Motin 	if (strcmp(verb, "remove") == 0 ||
155389b17223SAlexander Motin 	    strcmp(verb, "fail") == 0) {
155489b17223SAlexander Motin 		if (*nargs < 2) {
155589b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
155689b17223SAlexander Motin 			return (-1);
155789b17223SAlexander Motin 		}
155889b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
155989b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
156089b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
156189b17223SAlexander Motin 			if (diskname == NULL) {
156289b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
156389b17223SAlexander Motin 				error = -2;
156489b17223SAlexander Motin 				break;
156589b17223SAlexander Motin 			}
156689b17223SAlexander Motin 			if (strncmp(diskname, "/dev/", 5) == 0)
156789b17223SAlexander Motin 				diskname += 5;
156889b17223SAlexander Motin 
156989b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
157089b17223SAlexander Motin 				if (disk->d_consumer != NULL &&
157189b17223SAlexander Motin 				    disk->d_consumer->provider != NULL &&
157289b17223SAlexander Motin 				    strcmp(disk->d_consumer->provider->name,
157389b17223SAlexander Motin 				     diskname) == 0)
157489b17223SAlexander Motin 					break;
157589b17223SAlexander Motin 			}
157689b17223SAlexander Motin 			if (disk == NULL) {
157789b17223SAlexander Motin 				gctl_error(req, "Disk '%s' not found.",
157889b17223SAlexander Motin 				    diskname);
157989b17223SAlexander Motin 				error = -3;
158089b17223SAlexander Motin 				break;
158189b17223SAlexander Motin 			}
158289b17223SAlexander Motin 
158389b17223SAlexander Motin 			if (strcmp(verb, "fail") == 0) {
158489b17223SAlexander Motin 				g_raid_md_fail_disk_promise(md, NULL, disk);
158589b17223SAlexander Motin 				continue;
158689b17223SAlexander Motin 			}
158789b17223SAlexander Motin 
158889b17223SAlexander Motin 			/* Erase metadata on deleting disk and destroy it. */
158989b17223SAlexander Motin 			promise_meta_erase(disk->d_consumer);
159089b17223SAlexander Motin 			g_raid_destroy_disk(disk);
159189b17223SAlexander Motin 		}
159289b17223SAlexander Motin 		g_raid_md_promise_purge_volumes(sc);
159389b17223SAlexander Motin 
159489b17223SAlexander Motin 		/* Write updated metadata to remaining disks. */
159589b17223SAlexander Motin 		g_raid_md_write_promise(md, NULL, NULL, NULL);
159689b17223SAlexander Motin 
159789b17223SAlexander Motin 		/* Check if anything left. */
159889b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) == 0)
159989b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
160089b17223SAlexander Motin 		else
160189b17223SAlexander Motin 			g_raid_md_promise_refill(sc);
160289b17223SAlexander Motin 		return (error);
160389b17223SAlexander Motin 	}
160489b17223SAlexander Motin 	if (strcmp(verb, "insert") == 0) {
160589b17223SAlexander Motin 		if (*nargs < 2) {
160689b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
160789b17223SAlexander Motin 			return (-1);
160889b17223SAlexander Motin 		}
160989b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
161089b17223SAlexander Motin 			/* Get disk name. */
161189b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
161289b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
161389b17223SAlexander Motin 			if (diskname == NULL) {
161489b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
161589b17223SAlexander Motin 				error = -3;
161689b17223SAlexander Motin 				break;
161789b17223SAlexander Motin 			}
161889b17223SAlexander Motin 
161989b17223SAlexander Motin 			/* Try to find provider with specified name. */
162089b17223SAlexander Motin 			g_topology_lock();
162189b17223SAlexander Motin 			cp = g_raid_open_consumer(sc, diskname);
162289b17223SAlexander Motin 			if (cp == NULL) {
162389b17223SAlexander Motin 				gctl_error(req, "Can't open disk '%s'.",
162489b17223SAlexander Motin 				    diskname);
162589b17223SAlexander Motin 				g_topology_unlock();
162689b17223SAlexander Motin 				error = -4;
162789b17223SAlexander Motin 				break;
162889b17223SAlexander Motin 			}
1629733a1f3fSAlexander Motin 			pp = cp->provider;
163089b17223SAlexander Motin 			g_topology_unlock();
163189b17223SAlexander Motin 
1632733a1f3fSAlexander Motin 			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
1633733a1f3fSAlexander Motin 				gctl_error(req,
1634733a1f3fSAlexander Motin 				    "Disk '%s' is too big.", diskname);
1635733a1f3fSAlexander Motin 				g_raid_kill_consumer(sc, cp);
1636733a1f3fSAlexander Motin 				error = -8;
1637733a1f3fSAlexander Motin 				break;
1638733a1f3fSAlexander Motin 			}
1639733a1f3fSAlexander Motin 
164089b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
164189b17223SAlexander Motin 
164289b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
164389b17223SAlexander Motin 			disk->d_consumer = cp;
164489b17223SAlexander Motin 			disk->d_md_data = (void *)pd;
164589b17223SAlexander Motin 			cp->private = disk;
164689b17223SAlexander Motin 
164789b17223SAlexander Motin 			/* Read kernel dumping information. */
164889b17223SAlexander Motin 			disk->d_kd.offset = 0;
164989b17223SAlexander Motin 			disk->d_kd.length = OFF_MAX;
165089b17223SAlexander Motin 			len = sizeof(disk->d_kd);
165189b17223SAlexander Motin 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
165289b17223SAlexander Motin 			if (disk->d_kd.di.dumper == NULL)
165389b17223SAlexander Motin 				G_RAID_DEBUG1(2, sc,
165489b17223SAlexander Motin 				    "Dumping not supported by %s.",
165589b17223SAlexander Motin 				    cp->provider->name);
165689b17223SAlexander Motin 
165789b17223SAlexander Motin 			/* Welcome the "new" disk. */
165889b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
165989b17223SAlexander Motin 			promise_meta_write_spare(cp);
166089b17223SAlexander Motin 			g_raid_md_promise_refill(sc);
166189b17223SAlexander Motin 		}
166289b17223SAlexander Motin 		return (error);
166389b17223SAlexander Motin 	}
166489b17223SAlexander Motin 	return (-100);
166589b17223SAlexander Motin }
166689b17223SAlexander Motin 
166789b17223SAlexander Motin static int
166889b17223SAlexander Motin g_raid_md_write_promise(struct g_raid_md_object *md, struct g_raid_volume *tvol,
166989b17223SAlexander Motin     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
167089b17223SAlexander Motin {
167189b17223SAlexander Motin 	struct g_raid_softc *sc;
167289b17223SAlexander Motin 	struct g_raid_volume *vol;
167389b17223SAlexander Motin 	struct g_raid_subdisk *sd;
167489b17223SAlexander Motin 	struct g_raid_disk *disk;
167589b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
167689b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
167789b17223SAlexander Motin 	struct promise_raid_conf *meta;
167889b17223SAlexander Motin 	off_t rebuild_lba64;
167989b17223SAlexander Motin 	int i, j, pos, rebuild;
168089b17223SAlexander Motin 
168189b17223SAlexander Motin 	sc = md->mdo_softc;
168289b17223SAlexander Motin 
168389b17223SAlexander Motin 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
168489b17223SAlexander Motin 		return (0);
168589b17223SAlexander Motin 
168689b17223SAlexander Motin 	/* Generate new per-volume metadata for affected volumes. */
168789b17223SAlexander Motin 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
168889b17223SAlexander Motin 		if (vol->v_stopping)
168989b17223SAlexander Motin 			continue;
169089b17223SAlexander Motin 
169189b17223SAlexander Motin 		/* Skip volumes not related to specified targets. */
169289b17223SAlexander Motin 		if (tvol != NULL && vol != tvol)
169389b17223SAlexander Motin 			continue;
169489b17223SAlexander Motin 		if (tsd != NULL && vol != tsd->sd_volume)
169589b17223SAlexander Motin 			continue;
169689b17223SAlexander Motin 		if (tdisk != NULL) {
169789b17223SAlexander Motin 			for (i = 0; i < vol->v_disks_count; i++) {
169889b17223SAlexander Motin 				if (vol->v_subdisks[i].sd_disk == tdisk)
169989b17223SAlexander Motin 					break;
170089b17223SAlexander Motin 			}
170189b17223SAlexander Motin 			if (i >= vol->v_disks_count)
170289b17223SAlexander Motin 				continue;
170389b17223SAlexander Motin 		}
170489b17223SAlexander Motin 
170589b17223SAlexander Motin 		pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
170689b17223SAlexander Motin 		pv->pv_generation++;
170789b17223SAlexander Motin 
170889b17223SAlexander Motin 		meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO);
170989b17223SAlexander Motin 		if (pv->pv_meta != NULL)
171089b17223SAlexander Motin 			memcpy(meta, pv->pv_meta, sizeof(*meta));
171163607675SAlexander Motin 		memcpy(meta->promise_id, PROMISE_MAGIC,
171263607675SAlexander Motin 		    sizeof(PROMISE_MAGIC) - 1);
171389b17223SAlexander Motin 		meta->dummy_0 = 0x00020000;
171489b17223SAlexander Motin 		meta->integrity = PROMISE_I_VALID;
171589b17223SAlexander Motin 
171689b17223SAlexander Motin 		meta->generation = pv->pv_generation;
171789b17223SAlexander Motin 		meta->status = PROMISE_S_VALID | PROMISE_S_ONLINE |
171889b17223SAlexander Motin 		    PROMISE_S_INITED | PROMISE_S_READY;
171989b17223SAlexander Motin 		if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
172089b17223SAlexander Motin 			meta->status |= PROMISE_S_DEGRADED;
172189b17223SAlexander Motin 		if (vol->v_dirty)
172289b17223SAlexander Motin 			meta->status |= PROMISE_S_MARKED; /* XXX: INVENTED! */
172389b17223SAlexander Motin 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0 ||
172489b17223SAlexander Motin 		    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE)
172589b17223SAlexander Motin 			meta->type = PROMISE_T_RAID0;
172689b17223SAlexander Motin 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
172789b17223SAlexander Motin 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
172889b17223SAlexander Motin 			meta->type = PROMISE_T_RAID1;
172989b17223SAlexander Motin 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3)
173089b17223SAlexander Motin 			meta->type = PROMISE_T_RAID3;
173189b17223SAlexander Motin 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
173289b17223SAlexander Motin 			meta->type = PROMISE_T_RAID5;
173389b17223SAlexander Motin 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT)
173489b17223SAlexander Motin 			meta->type = PROMISE_T_SPAN;
173589b17223SAlexander Motin 		else
173689b17223SAlexander Motin 			meta->type = PROMISE_T_JBOD;
173789b17223SAlexander Motin 		meta->total_disks = vol->v_disks_count;
173889b17223SAlexander Motin 		meta->stripe_shift = ffs(vol->v_strip_size / 1024);
173989b17223SAlexander Motin 		meta->array_width = vol->v_disks_count;
174089b17223SAlexander Motin 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
174189b17223SAlexander Motin 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
174289b17223SAlexander Motin 			meta->array_width /= 2;
174389b17223SAlexander Motin 		meta->array_number = vol->v_global_id;
174489b17223SAlexander Motin 		meta->total_sectors = vol->v_mediasize / vol->v_sectorsize;
1745733a1f3fSAlexander Motin 		meta->total_sectors_high =
1746733a1f3fSAlexander Motin 		    (vol->v_mediasize / vol->v_sectorsize) >> 32;
174789b17223SAlexander Motin 		meta->cylinders = meta->total_sectors / (255 * 63) - 1;
174889b17223SAlexander Motin 		meta->heads = 254;
174989b17223SAlexander Motin 		meta->sectors = 63;
175089b17223SAlexander Motin 		meta->volume_id = pv->pv_id;
175189b17223SAlexander Motin 		rebuild_lba64 = UINT64_MAX;
175289b17223SAlexander Motin 		rebuild = 0;
175389b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
175489b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
175589b17223SAlexander Motin 			/* For RAID0+1 we need to translate order. */
175689b17223SAlexander Motin 			pos = promise_meta_translate_disk(vol, i);
175789b17223SAlexander Motin 			meta->disks[pos].flags = PROMISE_F_VALID |
175889b17223SAlexander Motin 			    PROMISE_F_ASSIGNED;
175989b17223SAlexander Motin 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE) {
176089b17223SAlexander Motin 				meta->disks[pos].flags |= 0;
176189b17223SAlexander Motin 			} else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED) {
176289b17223SAlexander Motin 				meta->disks[pos].flags |=
176389b17223SAlexander Motin 				    PROMISE_F_DOWN | PROMISE_F_REDIR;
176489b17223SAlexander Motin 			} else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD) {
176589b17223SAlexander Motin 				meta->disks[pos].flags |=
176689b17223SAlexander Motin 				    PROMISE_F_ONLINE | PROMISE_F_REDIR;
176789b17223SAlexander Motin 				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) {
176889b17223SAlexander Motin 					rebuild_lba64 = MIN(rebuild_lba64,
176989b17223SAlexander Motin 					    sd->sd_rebuild_pos / 512);
177089b17223SAlexander Motin 				} else
177189b17223SAlexander Motin 					rebuild_lba64 = 0;
177289b17223SAlexander Motin 				rebuild = 1;
177389b17223SAlexander Motin 			} else {
177489b17223SAlexander Motin 				meta->disks[pos].flags |= PROMISE_F_ONLINE;
177589b17223SAlexander Motin 				if (sd->sd_state < G_RAID_SUBDISK_S_ACTIVE) {
177689b17223SAlexander Motin 					meta->status |= PROMISE_S_MARKED;
177789b17223SAlexander Motin 					if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
177889b17223SAlexander Motin 						rebuild_lba64 = MIN(rebuild_lba64,
177989b17223SAlexander Motin 						    sd->sd_rebuild_pos / 512);
178089b17223SAlexander Motin 					} else
178189b17223SAlexander Motin 						rebuild_lba64 = 0;
178289b17223SAlexander Motin 				}
178389b17223SAlexander Motin 			}
178489b17223SAlexander Motin 			if (pv->pv_meta != NULL) {
178589b17223SAlexander Motin 				meta->disks[pos].id = pv->pv_meta->disks[pos].id;
178689b17223SAlexander Motin 			} else {
178789b17223SAlexander Motin 				meta->disks[pos].number = i * 2;
178889b17223SAlexander Motin 				arc4rand(&meta->disks[pos].id,
178989b17223SAlexander Motin 				    sizeof(meta->disks[pos].id), 0);
179089b17223SAlexander Motin 			}
179189b17223SAlexander Motin 		}
179289b17223SAlexander Motin 		promise_meta_put_name(meta, vol->v_name);
179389b17223SAlexander Motin 
179489b17223SAlexander Motin 		/* Try to mimic AMD BIOS rebuild/resync behavior. */
179589b17223SAlexander Motin 		if (rebuild_lba64 != UINT64_MAX) {
179689b17223SAlexander Motin 			if (rebuild)
179789b17223SAlexander Motin 				meta->magic_3 = 0x03040010UL; /* Rebuild? */
179889b17223SAlexander Motin 			else
179989b17223SAlexander Motin 				meta->magic_3 = 0x03040008UL; /* Resync? */
180089b17223SAlexander Motin 			/* Translate from per-disk to per-volume LBA. */
180189b17223SAlexander Motin 			if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
180289b17223SAlexander Motin 			    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
180389b17223SAlexander Motin 				rebuild_lba64 *= meta->array_width;
180489b17223SAlexander Motin 			} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
180589b17223SAlexander Motin 			    vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) {
180689b17223SAlexander Motin 				rebuild_lba64 *= meta->array_width - 1;
180789b17223SAlexander Motin 			} else
180889b17223SAlexander Motin 				rebuild_lba64 = 0;
180989b17223SAlexander Motin 		} else
181089b17223SAlexander Motin 			meta->magic_3 = 0x03000000UL;
181189b17223SAlexander Motin 		meta->rebuild_lba64 = rebuild_lba64;
181289b17223SAlexander Motin 		meta->magic_4 = 0x04010101UL;
181389b17223SAlexander Motin 
181489b17223SAlexander Motin 		/* Replace per-volume metadata with new. */
181589b17223SAlexander Motin 		if (pv->pv_meta != NULL)
181689b17223SAlexander Motin 			free(pv->pv_meta, M_MD_PROMISE);
181789b17223SAlexander Motin 		pv->pv_meta = meta;
181889b17223SAlexander Motin 
181989b17223SAlexander Motin 		/* Copy new metadata to the disks, adding or replacing old. */
182089b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
182189b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
182289b17223SAlexander Motin 			disk = sd->sd_disk;
182389b17223SAlexander Motin 			if (disk == NULL)
182489b17223SAlexander Motin 				continue;
182589b17223SAlexander Motin 			/* For RAID0+1 we need to translate order. */
182689b17223SAlexander Motin 			pos = promise_meta_translate_disk(vol, i);
182789b17223SAlexander Motin 			pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
182889b17223SAlexander Motin 			for (j = 0; j < pd->pd_subdisks; j++) {
182989b17223SAlexander Motin 				if (pd->pd_meta[j]->volume_id == meta->volume_id)
183089b17223SAlexander Motin 					break;
183189b17223SAlexander Motin 			}
183289b17223SAlexander Motin 			if (j == pd->pd_subdisks)
183389b17223SAlexander Motin 				pd->pd_subdisks++;
183489b17223SAlexander Motin 			if (pd->pd_meta[j] != NULL)
183589b17223SAlexander Motin 				free(pd->pd_meta[j], M_MD_PROMISE);
183689b17223SAlexander Motin 			pd->pd_meta[j] = promise_meta_copy(meta);
183789b17223SAlexander Motin 			pd->pd_meta[j]->disk = meta->disks[pos];
183889b17223SAlexander Motin 			pd->pd_meta[j]->disk.number = pos;
183989b17223SAlexander Motin 			pd->pd_meta[j]->disk_offset = sd->sd_offset / 512;
184089b17223SAlexander Motin 			pd->pd_meta[j]->disk_sectors = sd->sd_size / 512;
184189b17223SAlexander Motin 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) {
184289b17223SAlexander Motin 				pd->pd_meta[j]->rebuild_lba =
184389b17223SAlexander Motin 				    sd->sd_rebuild_pos / 512;
184489b17223SAlexander Motin 			} else if (sd->sd_state < G_RAID_SUBDISK_S_REBUILD)
184589b17223SAlexander Motin 				pd->pd_meta[j]->rebuild_lba = 0;
184689b17223SAlexander Motin 			else
184789b17223SAlexander Motin 				pd->pd_meta[j]->rebuild_lba = UINT32_MAX;
184889b17223SAlexander Motin 			pd->pd_updated = 1;
184989b17223SAlexander Motin 		}
185089b17223SAlexander Motin 	}
185189b17223SAlexander Motin 
185289b17223SAlexander Motin 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
185389b17223SAlexander Motin 		pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
185489b17223SAlexander Motin 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
185589b17223SAlexander Motin 			continue;
185689b17223SAlexander Motin 		if (!pd->pd_updated)
185789b17223SAlexander Motin 			continue;
185889b17223SAlexander Motin 		G_RAID_DEBUG(1, "Writing Promise metadata to %s",
185989b17223SAlexander Motin 		    g_raid_get_diskname(disk));
186089b17223SAlexander Motin 		for (i = 0; i < pd->pd_subdisks; i++)
186189b17223SAlexander Motin 			g_raid_md_promise_print(pd->pd_meta[i]);
186289b17223SAlexander Motin 		promise_meta_write(disk->d_consumer,
186389b17223SAlexander Motin 		    pd->pd_meta, pd->pd_subdisks);
186489b17223SAlexander Motin 		pd->pd_updated = 0;
186589b17223SAlexander Motin 	}
186689b17223SAlexander Motin 
186789b17223SAlexander Motin 	return (0);
186889b17223SAlexander Motin }
186989b17223SAlexander Motin 
187089b17223SAlexander Motin static int
187189b17223SAlexander Motin g_raid_md_fail_disk_promise(struct g_raid_md_object *md,
187289b17223SAlexander Motin     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
187389b17223SAlexander Motin {
187489b17223SAlexander Motin 	struct g_raid_softc *sc;
187589b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
187689b17223SAlexander Motin 	struct g_raid_subdisk *sd;
187789b17223SAlexander Motin 	int i, pos;
187889b17223SAlexander Motin 
187989b17223SAlexander Motin 	sc = md->mdo_softc;
188089b17223SAlexander Motin 	pd = (struct g_raid_md_promise_perdisk *)tdisk->d_md_data;
188189b17223SAlexander Motin 
188289b17223SAlexander Motin 	/* We can't fail disk that is not a part of array now. */
188389b17223SAlexander Motin 	if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
188489b17223SAlexander Motin 		return (-1);
188589b17223SAlexander Motin 
188689b17223SAlexander Motin 	/*
188789b17223SAlexander Motin 	 * Mark disk as failed in metadata and try to write that metadata
188889b17223SAlexander Motin 	 * to the disk itself to prevent it's later resurrection as STALE.
188989b17223SAlexander Motin 	 */
189089b17223SAlexander Motin 	if (pd->pd_subdisks > 0 && tdisk->d_consumer != NULL)
189189b17223SAlexander Motin 		G_RAID_DEBUG(1, "Writing Promise metadata to %s",
189289b17223SAlexander Motin 		    g_raid_get_diskname(tdisk));
189389b17223SAlexander Motin 	for (i = 0; i < pd->pd_subdisks; i++) {
189489b17223SAlexander Motin 		pd->pd_meta[i]->disk.flags |=
189589b17223SAlexander Motin 		    PROMISE_F_DOWN | PROMISE_F_REDIR;
189689b17223SAlexander Motin 		pos = pd->pd_meta[i]->disk.number;
189789b17223SAlexander Motin 		if (pos >= 0 && pos < PROMISE_MAX_DISKS) {
189889b17223SAlexander Motin 			pd->pd_meta[i]->disks[pos].flags |=
189989b17223SAlexander Motin 			    PROMISE_F_DOWN | PROMISE_F_REDIR;
190089b17223SAlexander Motin 		}
190189b17223SAlexander Motin 		g_raid_md_promise_print(pd->pd_meta[i]);
190289b17223SAlexander Motin 	}
190389b17223SAlexander Motin 	if (tdisk->d_consumer != NULL)
190489b17223SAlexander Motin 		promise_meta_write(tdisk->d_consumer,
190589b17223SAlexander Motin 		    pd->pd_meta, pd->pd_subdisks);
190689b17223SAlexander Motin 
190789b17223SAlexander Motin 	/* Change states. */
190889b17223SAlexander Motin 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
190989b17223SAlexander Motin 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
191089b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
191189b17223SAlexander Motin 		    G_RAID_SUBDISK_S_FAILED);
191289b17223SAlexander Motin 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
191389b17223SAlexander Motin 		    G_RAID_EVENT_SUBDISK);
191489b17223SAlexander Motin 	}
191589b17223SAlexander Motin 
191689b17223SAlexander Motin 	/* Write updated metadata to remaining disks. */
191789b17223SAlexander Motin 	g_raid_md_write_promise(md, NULL, NULL, tdisk);
191889b17223SAlexander Motin 
191989b17223SAlexander Motin 	g_raid_md_promise_refill(sc);
192089b17223SAlexander Motin 	return (0);
192189b17223SAlexander Motin }
192289b17223SAlexander Motin 
192389b17223SAlexander Motin static int
192489b17223SAlexander Motin g_raid_md_free_disk_promise(struct g_raid_md_object *md,
192589b17223SAlexander Motin     struct g_raid_disk *disk)
192689b17223SAlexander Motin {
192789b17223SAlexander Motin 	struct g_raid_md_promise_perdisk *pd;
192889b17223SAlexander Motin 	int i;
192989b17223SAlexander Motin 
193089b17223SAlexander Motin 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
193189b17223SAlexander Motin 	for (i = 0; i < pd->pd_subdisks; i++) {
193289b17223SAlexander Motin 		if (pd->pd_meta[i] != NULL) {
193389b17223SAlexander Motin 			free(pd->pd_meta[i], M_MD_PROMISE);
193489b17223SAlexander Motin 			pd->pd_meta[i] = NULL;
193589b17223SAlexander Motin 		}
193689b17223SAlexander Motin 	}
193789b17223SAlexander Motin 	free(pd, M_MD_PROMISE);
193889b17223SAlexander Motin 	disk->d_md_data = NULL;
193989b17223SAlexander Motin 	return (0);
194089b17223SAlexander Motin }
194189b17223SAlexander Motin 
194289b17223SAlexander Motin static int
194389b17223SAlexander Motin g_raid_md_free_volume_promise(struct g_raid_md_object *md,
194489b17223SAlexander Motin     struct g_raid_volume *vol)
194589b17223SAlexander Motin {
194689b17223SAlexander Motin 	struct g_raid_md_promise_pervolume *pv;
194789b17223SAlexander Motin 
194889b17223SAlexander Motin 	pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
194989b17223SAlexander Motin 	if (pv && pv->pv_meta != NULL) {
195089b17223SAlexander Motin 		free(pv->pv_meta, M_MD_PROMISE);
195189b17223SAlexander Motin 		pv->pv_meta = NULL;
195289b17223SAlexander Motin 	}
195389b17223SAlexander Motin 	if (pv && !pv->pv_started) {
195489b17223SAlexander Motin 		pv->pv_started = 1;
195589b17223SAlexander Motin 		callout_stop(&pv->pv_start_co);
195689b17223SAlexander Motin 	}
195789b17223SAlexander Motin 	return (0);
195889b17223SAlexander Motin }
195989b17223SAlexander Motin 
196089b17223SAlexander Motin static int
196189b17223SAlexander Motin g_raid_md_free_promise(struct g_raid_md_object *md)
196289b17223SAlexander Motin {
196389b17223SAlexander Motin 
196489b17223SAlexander Motin 	return (0);
196589b17223SAlexander Motin }
196689b17223SAlexander Motin 
196789b17223SAlexander Motin G_RAID_MD_DECLARE(g_raid_md_promise);
1968