xref: /freebsd/sys/geom/raid/md_promise.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bio.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
43 #include <geom/geom.h>
44 #include "geom/raid/g_raid.h"
45 #include "g_raid_md_if.h"
46 
47 static MALLOC_DEFINE(M_MD_PROMISE, "md_promise_data", "GEOM_RAID Promise metadata");
48 
49 #define	PROMISE_MAX_DISKS	8
50 #define	PROMISE_MAX_SUBDISKS	2
51 #define	PROMISE_META_OFFSET	14
52 
53 struct promise_raid_disk {
54 	uint8_t		flags;			/* Subdisk status. */
55 #define PROMISE_F_VALID		0x01
56 #define PROMISE_F_ONLINE	0x02
57 #define PROMISE_F_ASSIGNED	0x04
58 #define PROMISE_F_SPARE		0x08
59 #define PROMISE_F_DUPLICATE	0x10
60 #define PROMISE_F_REDIR		0x20
61 #define PROMISE_F_DOWN		0x40
62 #define PROMISE_F_READY		0x80
63 
64 	uint8_t		number;			/* Position in a volume. */
65 	uint8_t		channel;		/* ATA channel number. */
66 	uint8_t		device;			/* ATA device number. */
67 	uint64_t	id __packed;		/* Subdisk ID. */
68 } __packed;
69 
70 struct promise_raid_conf {
71 	char		promise_id[24];
72 #define PROMISE_MAGIC		"Promise Technology, Inc."
73 #define FREEBSD_MAGIC		"FreeBSD ATA driver RAID "
74 
75 	uint32_t	dummy_0;
76 	uint64_t	magic_0;
77 #define PROMISE_MAGIC0(x)	(((uint64_t)(x.channel) << 48) | \
78 				((uint64_t)(x.device != 0) << 56))
79 	uint16_t	magic_1;
80 	uint32_t	magic_2;
81 	uint8_t		filler1[470];
82 
83 	uint32_t	integrity;
84 #define PROMISE_I_VALID		0x00000080
85 
86 	struct promise_raid_disk	disk;	/* This subdisk info. */
87 	uint32_t	disk_offset;		/* Subdisk offset. */
88 	uint32_t	disk_sectors;		/* Subdisk size */
89 	uint32_t	disk_rebuild;		/* Rebuild position. */
90 	uint16_t	generation;		/* Generation number. */
91 	uint8_t		status;			/* Volume status. */
92 #define PROMISE_S_VALID		0x01
93 #define PROMISE_S_ONLINE	0x02
94 #define PROMISE_S_INITED	0x04
95 #define PROMISE_S_READY		0x08
96 #define PROMISE_S_DEGRADED	0x10
97 #define PROMISE_S_MARKED	0x20
98 #define PROMISE_S_MIGRATING	0x40
99 #define PROMISE_S_FUNCTIONAL	0x80
100 
101 	uint8_t		type;			/* Voluem type. */
102 #define PROMISE_T_RAID0		0x00
103 #define PROMISE_T_RAID1		0x01
104 #define PROMISE_T_RAID3		0x02
105 #define PROMISE_T_RAID5		0x04
106 #define PROMISE_T_SPAN		0x08
107 #define PROMISE_T_JBOD		0x10
108 
109 	uint8_t		total_disks;		/* Disks in this volume. */
110 	uint8_t		stripe_shift;		/* Strip size. */
111 	uint8_t		array_width;		/* Number of RAID0 stripes. */
112 	uint8_t		array_number;		/* Global volume number. */
113 	uint32_t	total_sectors;		/* Volume size. */
114 	uint16_t	cylinders;		/* Volume geometry: C. */
115 	uint8_t		heads;			/* Volume geometry: H. */
116 	uint8_t		sectors;		/* Volume geometry: S. */
117 	uint64_t	volume_id __packed;	/* Volume ID, */
118 	struct promise_raid_disk	disks[PROMISE_MAX_DISKS];
119 						/* Subdisks in this volume. */
120 	char		name[32];		/* Volume label. */
121 
122 	uint32_t	filler2[8];
123 	uint32_t	magic_3;	/* Something related to rebuild. */
124 	uint64_t	rebuild_lba64;	/* Per-volume rebuild position. */
125 	uint32_t	magic_4;
126 	uint32_t	magic_5;
127 	uint32_t	total_sectors_high;
128 	uint8_t		magic_6;
129 	uint8_t		sector_size;
130 	uint16_t	magic_7;
131 	uint32_t	magic_8[31];
132 	uint32_t	backup_time;
133 	uint16_t	magic_9;
134 	uint32_t	disk_offset_high;
135 	uint32_t	disk_sectors_high;
136 	uint32_t	disk_rebuild_high;
137 	uint16_t	magic_10;
138 	uint32_t	magic_11[3];
139 	uint32_t	filler3[284];
140 	uint32_t	checksum;
141 } __packed;
142 
143 struct g_raid_md_promise_perdisk {
144 	int		 pd_updated;
145 	int		 pd_subdisks;
146 	struct promise_raid_conf	*pd_meta[PROMISE_MAX_SUBDISKS];
147 };
148 
149 struct g_raid_md_promise_pervolume {
150 	struct promise_raid_conf	*pv_meta;
151 	uint64_t			 pv_id;
152 	uint16_t			 pv_generation;
153 	int				 pv_disks_present;
154 	int				 pv_started;
155 	struct callout			 pv_start_co;	/* STARTING state timer. */
156 };
157 
158 static g_raid_md_create_t g_raid_md_create_promise;
159 static g_raid_md_taste_t g_raid_md_taste_promise;
160 static g_raid_md_event_t g_raid_md_event_promise;
161 static g_raid_md_volume_event_t g_raid_md_volume_event_promise;
162 static g_raid_md_ctl_t g_raid_md_ctl_promise;
163 static g_raid_md_write_t g_raid_md_write_promise;
164 static g_raid_md_fail_disk_t g_raid_md_fail_disk_promise;
165 static g_raid_md_free_disk_t g_raid_md_free_disk_promise;
166 static g_raid_md_free_volume_t g_raid_md_free_volume_promise;
167 static g_raid_md_free_t g_raid_md_free_promise;
168 
169 static kobj_method_t g_raid_md_promise_methods[] = {
170 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_promise),
171 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_promise),
172 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_promise),
173 	KOBJMETHOD(g_raid_md_volume_event,	g_raid_md_volume_event_promise),
174 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_promise),
175 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_promise),
176 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_promise),
177 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_promise),
178 	KOBJMETHOD(g_raid_md_free_volume,	g_raid_md_free_volume_promise),
179 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_promise),
180 	{ 0, 0 }
181 };
182 
183 static struct g_raid_md_class g_raid_md_promise_class = {
184 	"Promise",
185 	g_raid_md_promise_methods,
186 	sizeof(struct g_raid_md_object),
187 	.mdc_enable = 1,
188 	.mdc_priority = 100
189 };
190 
191 
192 static void
193 g_raid_md_promise_print(struct promise_raid_conf *meta)
194 {
195 	int i;
196 
197 	if (g_raid_debug < 1)
198 		return;
199 
200 	printf("********* ATA Promise Metadata *********\n");
201 	printf("promise_id          <%.24s>\n", meta->promise_id);
202 	printf("disk                %02x %02x %02x %02x %016jx\n",
203 	    meta->disk.flags, meta->disk.number, meta->disk.channel,
204 	    meta->disk.device, meta->disk.id);
205 	printf("disk_offset         %u\n", meta->disk_offset);
206 	printf("disk_sectors        %u\n", meta->disk_sectors);
207 	printf("disk_rebuild        %u\n", meta->disk_rebuild);
208 	printf("generation          %u\n", meta->generation);
209 	printf("status              0x%02x\n", meta->status);
210 	printf("type                %u\n", meta->type);
211 	printf("total_disks         %u\n", meta->total_disks);
212 	printf("stripe_shift        %u\n", meta->stripe_shift);
213 	printf("array_width         %u\n", meta->array_width);
214 	printf("array_number        %u\n", meta->array_number);
215 	printf("total_sectors       %u\n", meta->total_sectors);
216 	printf("cylinders           %u\n", meta->cylinders);
217 	printf("heads               %u\n", meta->heads);
218 	printf("sectors             %u\n", meta->sectors);
219 	printf("volume_id           0x%016jx\n", meta->volume_id);
220 	printf("disks:\n");
221 	for (i = 0; i < PROMISE_MAX_DISKS; i++ ) {
222 		printf("                    %02x %02x %02x %02x %016jx\n",
223 		    meta->disks[i].flags, meta->disks[i].number,
224 		    meta->disks[i].channel, meta->disks[i].device,
225 		    meta->disks[i].id);
226 	}
227 	printf("name                <%.32s>\n", meta->name);
228 	printf("magic_3             0x%08x\n", meta->magic_3);
229 	printf("rebuild_lba64       %ju\n", meta->rebuild_lba64);
230 	printf("magic_4             0x%08x\n", meta->magic_4);
231 	printf("magic_5             0x%08x\n", meta->magic_5);
232 	printf("total_sectors_high  0x%08x\n", meta->total_sectors_high);
233 	printf("sector_size         %u\n", meta->sector_size);
234 	printf("backup_time         %d\n", meta->backup_time);
235 	printf("disk_offset_high    0x%08x\n", meta->disk_offset_high);
236 	printf("disk_sectors_high   0x%08x\n", meta->disk_sectors_high);
237 	printf("disk_rebuild_high   0x%08x\n", meta->disk_rebuild_high);
238 	printf("=================================================\n");
239 }
240 
241 static struct promise_raid_conf *
242 promise_meta_copy(struct promise_raid_conf *meta)
243 {
244 	struct promise_raid_conf *nmeta;
245 
246 	nmeta = malloc(sizeof(*nmeta), M_MD_PROMISE, M_WAITOK);
247 	memcpy(nmeta, meta, sizeof(*nmeta));
248 	return (nmeta);
249 }
250 
251 static int
252 promise_meta_find_disk(struct promise_raid_conf *meta, uint64_t id)
253 {
254 	int pos;
255 
256 	for (pos = 0; pos < meta->total_disks; pos++) {
257 		if (meta->disks[pos].id == id)
258 			return (pos);
259 	}
260 	return (-1);
261 }
262 
263 static int
264 promise_meta_unused_range(struct promise_raid_conf **metaarr, int nsd,
265     off_t sectors, off_t *off, off_t *size)
266 {
267 	off_t coff, csize, tmp;
268 	int i, j;
269 
270 	sectors -= 131072;
271 	*off = 0;
272 	*size = 0;
273 	coff = 0;
274 	csize = sectors;
275 	i = 0;
276 	while (1) {
277 		for (j = 0; j < nsd; j++) {
278 			tmp = ((off_t)metaarr[j]->disk_offset_high << 32) +
279 			    metaarr[j]->disk_offset;
280 			if (tmp >= coff)
281 				csize = MIN(csize, tmp - coff);
282 		}
283 		if (csize > *size) {
284 			*off = coff;
285 			*size = csize;
286 		}
287 		if (i >= nsd)
288 			break;
289 		coff = ((off_t)metaarr[i]->disk_offset_high << 32) +
290 		     metaarr[i]->disk_offset +
291 		    ((off_t)metaarr[i]->disk_sectors_high << 32) +
292 		     metaarr[i]->disk_sectors;
293 		csize = sectors - coff;
294 		i++;
295 	}
296 	return ((*size > 0) ? 1 : 0);
297 }
298 
299 static int
300 promise_meta_translate_disk(struct g_raid_volume *vol, int md_disk_pos)
301 {
302 	int disk_pos, width;
303 
304 	if (md_disk_pos >= 0 && vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
305 		width = vol->v_disks_count / 2;
306 		disk_pos = (md_disk_pos / width) +
307 		    (md_disk_pos % width) * width;
308 	} else
309 		disk_pos = md_disk_pos;
310 	return (disk_pos);
311 }
312 
313 static void
314 promise_meta_get_name(struct promise_raid_conf *meta, char *buf)
315 {
316 	int i;
317 
318 	strncpy(buf, meta->name, 32);
319 	buf[32] = 0;
320 	for (i = 31; i >= 0; i--) {
321 		if (buf[i] > 0x20)
322 			break;
323 		buf[i] = 0;
324 	}
325 }
326 
327 static void
328 promise_meta_put_name(struct promise_raid_conf *meta, char *buf)
329 {
330 
331 	memset(meta->name, 0x20, 32);
332 	memcpy(meta->name, buf, MIN(strlen(buf), 32));
333 }
334 
335 static int
336 promise_meta_read(struct g_consumer *cp, struct promise_raid_conf **metaarr)
337 {
338 	struct g_provider *pp;
339 	struct promise_raid_conf *meta;
340 	char *buf;
341 	int error, i, subdisks;
342 	uint32_t checksum, *ptr;
343 
344 	pp = cp->provider;
345 	subdisks = 0;
346 
347 	if (pp->sectorsize * 4 > MAXPHYS) {
348 		G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
349 		return (subdisks);
350 	}
351 next:
352 	/* Read metadata block. */
353 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize *
354 	    (63 - subdisks * PROMISE_META_OFFSET),
355 	    pp->sectorsize * 4, &error);
356 	if (buf == NULL) {
357 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
358 		    pp->name, error);
359 		return (subdisks);
360 	}
361 	meta = (struct promise_raid_conf *)buf;
362 
363 	/* Check if this is an Promise RAID struct */
364 	if (strncmp(meta->promise_id, PROMISE_MAGIC, strlen(PROMISE_MAGIC)) &&
365 	    strncmp(meta->promise_id, FREEBSD_MAGIC, strlen(FREEBSD_MAGIC))) {
366 		if (subdisks == 0)
367 			G_RAID_DEBUG(1,
368 			    "Promise signature check failed on %s", pp->name);
369 		g_free(buf);
370 		return (subdisks);
371 	}
372 	meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK);
373 	memcpy(meta, buf, MIN(sizeof(*meta), pp->sectorsize * 4));
374 	g_free(buf);
375 
376 	/* Check metadata checksum. */
377 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++)
378 		checksum += *ptr++;
379 	if (checksum != meta->checksum) {
380 		G_RAID_DEBUG(1, "Promise checksum check failed on %s", pp->name);
381 		free(meta, M_MD_PROMISE);
382 		return (subdisks);
383 	}
384 
385 	if ((meta->integrity & PROMISE_I_VALID) == 0) {
386 		G_RAID_DEBUG(1, "Promise metadata is invalid on %s", pp->name);
387 		free(meta, M_MD_PROMISE);
388 		return (subdisks);
389 	}
390 
391 	if (meta->total_disks > PROMISE_MAX_DISKS) {
392 		G_RAID_DEBUG(1, "Wrong number of disks on %s (%d)",
393 		    pp->name, meta->total_disks);
394 		free(meta, M_MD_PROMISE);
395 		return (subdisks);
396 	}
397 
398 	/* Remove filler garbage from fields used in newer metadata. */
399 	if (meta->disk_offset_high == 0x8b8c8d8e &&
400 	    meta->disk_sectors_high == 0x8788898a &&
401 	    meta->disk_rebuild_high == 0x83848586) {
402 		meta->disk_offset_high = 0;
403 		meta->disk_sectors_high = 0;
404 		if (meta->disk_rebuild == UINT32_MAX)
405 			meta->disk_rebuild_high = UINT32_MAX;
406 		else
407 			meta->disk_rebuild_high = 0;
408 		if (meta->total_sectors_high == 0x15161718) {
409 			meta->total_sectors_high = 0;
410 			meta->backup_time = 0;
411 			if (meta->rebuild_lba64 == 0x2122232425262728)
412 				meta->rebuild_lba64 = UINT64_MAX;
413 		}
414 	}
415 	if (meta->sector_size < 1 || meta->sector_size > 8)
416 		meta->sector_size = 1;
417 
418 	/* Save this part and look for next. */
419 	*metaarr = meta;
420 	metaarr++;
421 	subdisks++;
422 	if (subdisks < PROMISE_MAX_SUBDISKS)
423 		goto next;
424 
425 	return (subdisks);
426 }
427 
428 static int
429 promise_meta_write(struct g_consumer *cp,
430     struct promise_raid_conf **metaarr, int nsd)
431 {
432 	struct g_provider *pp;
433 	struct promise_raid_conf *meta;
434 	char *buf;
435 	off_t off, size;
436 	int error, i, subdisk, fake;
437 	uint32_t checksum, *ptr;
438 
439 	pp = cp->provider;
440 	subdisk = 0;
441 	fake = 0;
442 next:
443 	buf = malloc(pp->sectorsize * 4, M_MD_PROMISE, M_WAITOK | M_ZERO);
444 	meta = NULL;
445 	if (subdisk < nsd) {
446 		meta = metaarr[subdisk];
447 	} else if (!fake && promise_meta_unused_range(metaarr, nsd,
448 	    cp->provider->mediasize / cp->provider->sectorsize,
449 	    &off, &size)) {
450 		/* Optionally add record for unused space. */
451 		meta = (struct promise_raid_conf *)buf;
452 		memcpy(&meta->promise_id[0], PROMISE_MAGIC,
453 		    sizeof(PROMISE_MAGIC) - 1);
454 		meta->dummy_0 = 0x00020000;
455 		meta->integrity = PROMISE_I_VALID;
456 		meta->disk.flags = PROMISE_F_ONLINE | PROMISE_F_VALID;
457 		meta->disk.number = 0xff;
458 		arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0);
459 		meta->disk_offset_high = off >> 32;
460 		meta->disk_offset = (uint32_t)off;
461 		meta->disk_sectors_high = size >> 32;
462 		meta->disk_sectors = (uint32_t)size;
463 		meta->disk_rebuild_high = UINT32_MAX;
464 		meta->disk_rebuild = UINT32_MAX;
465 		fake = 1;
466 	}
467 	if (meta != NULL) {
468 		/* Recalculate checksum for case if metadata were changed. */
469 		meta->checksum = 0;
470 		for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++)
471 			checksum += *ptr++;
472 		meta->checksum = checksum;
473 		memcpy(buf, meta, MIN(pp->sectorsize * 4, sizeof(*meta)));
474 	}
475 	error = g_write_data(cp, pp->mediasize - pp->sectorsize *
476 	    (63 - subdisk * PROMISE_META_OFFSET),
477 	    buf, pp->sectorsize * 4);
478 	if (error != 0) {
479 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
480 		    pp->name, error);
481 	}
482 	free(buf, M_MD_PROMISE);
483 
484 	subdisk++;
485 	if (subdisk < PROMISE_MAX_SUBDISKS)
486 		goto next;
487 
488 	return (error);
489 }
490 
491 static int
492 promise_meta_erase(struct g_consumer *cp)
493 {
494 	struct g_provider *pp;
495 	char *buf;
496 	int error, subdisk;
497 
498 	pp = cp->provider;
499 	buf = malloc(4 * pp->sectorsize, M_MD_PROMISE, M_WAITOK | M_ZERO);
500 	for (subdisk = 0; subdisk < PROMISE_MAX_SUBDISKS; subdisk++) {
501 		error = g_write_data(cp, pp->mediasize - pp->sectorsize *
502 		    (63 - subdisk * PROMISE_META_OFFSET),
503 		    buf, 4 * pp->sectorsize);
504 		if (error != 0) {
505 			G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
506 			    pp->name, error);
507 		}
508 	}
509 	free(buf, M_MD_PROMISE);
510 	return (error);
511 }
512 
513 static int
514 promise_meta_write_spare(struct g_consumer *cp)
515 {
516 	struct promise_raid_conf *meta;
517 	off_t tmp;
518 	int error;
519 
520 	meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO);
521 	memcpy(&meta->promise_id[0], PROMISE_MAGIC, sizeof(PROMISE_MAGIC) - 1);
522 	meta->dummy_0 = 0x00020000;
523 	meta->integrity = PROMISE_I_VALID;
524 	meta->disk.flags = PROMISE_F_SPARE | PROMISE_F_ONLINE | PROMISE_F_VALID;
525 	meta->disk.number = 0xff;
526 	arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0);
527 	tmp = cp->provider->mediasize / cp->provider->sectorsize - 131072;
528 	meta->disk_sectors_high = tmp >> 32;
529 	meta->disk_sectors = (uint32_t)tmp;
530 	meta->disk_rebuild_high = UINT32_MAX;
531 	meta->disk_rebuild = UINT32_MAX;
532 	error = promise_meta_write(cp, &meta, 1);
533 	free(meta, M_MD_PROMISE);
534 	return (error);
535 }
536 
537 static struct g_raid_volume *
538 g_raid_md_promise_get_volume(struct g_raid_softc *sc, uint64_t id)
539 {
540 	struct g_raid_volume	*vol;
541 	struct g_raid_md_promise_pervolume *pv;
542 
543 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
544 		pv = vol->v_md_data;
545 		if (pv->pv_id == id)
546 			break;
547 	}
548 	return (vol);
549 }
550 
551 static int
552 g_raid_md_promise_purge_volumes(struct g_raid_softc *sc)
553 {
554 	struct g_raid_volume	*vol, *tvol;
555 	struct g_raid_md_promise_pervolume *pv;
556 	int i, res;
557 
558 	res = 0;
559 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
560 		pv = vol->v_md_data;
561 		if (!pv->pv_started || vol->v_stopping)
562 			continue;
563 		for (i = 0; i < vol->v_disks_count; i++) {
564 			if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
565 				break;
566 		}
567 		if (i >= vol->v_disks_count) {
568 			g_raid_destroy_volume(vol);
569 			res = 1;
570 		}
571 	}
572 	return (res);
573 }
574 
575 static int
576 g_raid_md_promise_purge_disks(struct g_raid_softc *sc)
577 {
578 	struct g_raid_disk	*disk, *tdisk;
579 	struct g_raid_volume	*vol;
580 	struct g_raid_md_promise_perdisk *pd;
581 	int i, j, res;
582 
583 	res = 0;
584 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
585 		if (disk->d_state == G_RAID_DISK_S_SPARE)
586 			continue;
587 		pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
588 
589 		/* Scan for deleted volumes. */
590 		for (i = 0; i < pd->pd_subdisks; ) {
591 			vol = g_raid_md_promise_get_volume(sc,
592 			    pd->pd_meta[i]->volume_id);
593 			if (vol != NULL && !vol->v_stopping) {
594 				i++;
595 				continue;
596 			}
597 			free(pd->pd_meta[i], M_MD_PROMISE);
598 			for (j = i; j < pd->pd_subdisks - 1; j++)
599 				pd->pd_meta[j] = pd->pd_meta[j + 1];
600 			pd->pd_meta[pd->pd_subdisks - 1] = NULL;
601 			pd->pd_subdisks--;
602 			pd->pd_updated = 1;
603 		}
604 
605 		/* If there is no metadata left - erase and delete disk. */
606 		if (pd->pd_subdisks == 0) {
607 			promise_meta_erase(disk->d_consumer);
608 			g_raid_destroy_disk(disk);
609 			res = 1;
610 		}
611 	}
612 	return (res);
613 }
614 
615 static int
616 g_raid_md_promise_supported(int level, int qual, int disks, int force)
617 {
618 
619 	if (disks > PROMISE_MAX_DISKS)
620 		return (0);
621 	switch (level) {
622 	case G_RAID_VOLUME_RL_RAID0:
623 		if (disks < 1)
624 			return (0);
625 		if (!force && disks < 2)
626 			return (0);
627 		break;
628 	case G_RAID_VOLUME_RL_RAID1:
629 		if (disks < 1)
630 			return (0);
631 		if (!force && (disks != 2))
632 			return (0);
633 		break;
634 	case G_RAID_VOLUME_RL_RAID1E:
635 		if (disks < 2)
636 			return (0);
637 		if (disks % 2 != 0)
638 			return (0);
639 		if (!force && (disks != 4))
640 			return (0);
641 		break;
642 	case G_RAID_VOLUME_RL_SINGLE:
643 		if (disks != 1)
644 			return (0);
645 		break;
646 	case G_RAID_VOLUME_RL_CONCAT:
647 		if (disks < 2)
648 			return (0);
649 		break;
650 	case G_RAID_VOLUME_RL_RAID5:
651 		if (disks < 3)
652 			return (0);
653 		if (qual != G_RAID_VOLUME_RLQ_R5LA)
654 			return (0);
655 		break;
656 	default:
657 		return (0);
658 	}
659 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
660 		return (0);
661 	return (1);
662 }
663 
664 static int
665 g_raid_md_promise_start_disk(struct g_raid_disk *disk, int sdn,
666     struct g_raid_volume *vol)
667 {
668 	struct g_raid_softc *sc;
669 	struct g_raid_subdisk *sd;
670 	struct g_raid_md_promise_perdisk *pd;
671 	struct g_raid_md_promise_pervolume *pv;
672 	struct promise_raid_conf *meta;
673 	off_t eoff, esize, size;
674 	int disk_pos, md_disk_pos, i, resurrection = 0;
675 
676 	sc = disk->d_softc;
677 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
678 
679 	pv = vol->v_md_data;
680 	meta = pv->pv_meta;
681 
682 	if (sdn >= 0) {
683 		/* Find disk position in metadata by its serial. */
684 		md_disk_pos = promise_meta_find_disk(meta, pd->pd_meta[sdn]->disk.id);
685 		/* For RAID0+1 we need to translate order. */
686 		disk_pos = promise_meta_translate_disk(vol, md_disk_pos);
687 	} else {
688 		md_disk_pos = -1;
689 		disk_pos = -1;
690 	}
691 	if (disk_pos < 0) {
692 		G_RAID_DEBUG1(1, sc, "Disk %s is not part of the volume %s",
693 		    g_raid_get_diskname(disk), vol->v_name);
694 		/* Failed stale disk is useless for us. */
695 		if (sdn >= 0 &&
696 		    pd->pd_meta[sdn]->disk.flags & PROMISE_F_DOWN) {
697 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
698 			return (0);
699 		}
700 		/* If we were given specific metadata subdisk - erase it. */
701 		if (sdn >= 0) {
702 			free(pd->pd_meta[sdn], M_MD_PROMISE);
703 			for (i = sdn; i < pd->pd_subdisks - 1; i++)
704 				pd->pd_meta[i] = pd->pd_meta[i + 1];
705 			pd->pd_meta[pd->pd_subdisks - 1] = NULL;
706 			pd->pd_subdisks--;
707 		}
708 		/* If we are in the start process, that's all for now. */
709 		if (!pv->pv_started)
710 			goto nofit;
711 		/*
712 		 * If we have already started - try to get use of the disk.
713 		 * Try to replace OFFLINE disks first, then FAILED.
714 		 */
715 		promise_meta_unused_range(pd->pd_meta, pd->pd_subdisks,
716 		    disk->d_consumer->provider->mediasize /
717 		    disk->d_consumer->provider->sectorsize,
718 		    &eoff, &esize);
719 		if (esize == 0) {
720 			G_RAID_DEBUG1(1, sc, "No free space on disk %s",
721 			    g_raid_get_diskname(disk));
722 			goto nofit;
723 		}
724 		size = INT64_MAX;
725 		for (i = 0; i < vol->v_disks_count; i++) {
726 			sd = &vol->v_subdisks[i];
727 			if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
728 				size = sd->sd_size;
729 			if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
730 			    (disk_pos < 0 ||
731 			     vol->v_subdisks[i].sd_state < sd->sd_state))
732 				disk_pos = i;
733 		}
734 		if (disk_pos >= 0 &&
735 		    vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
736 		    (off_t)esize * 512 < size) {
737 			G_RAID_DEBUG1(1, sc, "Disk %s free space "
738 			    "is too small (%ju < %ju)",
739 			    g_raid_get_diskname(disk),
740 			    (off_t)esize * 512, size);
741 			disk_pos = -1;
742 		}
743 		if (disk_pos >= 0) {
744 			if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
745 				esize = size / 512;
746 			/* For RAID0+1 we need to translate order. */
747 			md_disk_pos = promise_meta_translate_disk(vol, disk_pos);
748 		} else {
749 nofit:
750 			if (pd->pd_subdisks == 0) {
751 				g_raid_change_disk_state(disk,
752 				    G_RAID_DISK_S_SPARE);
753 			}
754 			return (0);
755 		}
756 		G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
757 		    g_raid_get_diskname(disk), disk_pos, vol->v_name);
758 		resurrection = 1;
759 	}
760 
761 	sd = &vol->v_subdisks[disk_pos];
762 
763 	if (resurrection && sd->sd_disk != NULL) {
764 		g_raid_change_disk_state(sd->sd_disk,
765 		    G_RAID_DISK_S_STALE_FAILED);
766 		TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
767 		    sd, sd_next);
768 	}
769 	vol->v_subdisks[disk_pos].sd_disk = disk;
770 	TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
771 
772 	/* Welcome the new disk. */
773 	if (resurrection)
774 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
775 	else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN)
776 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
777 	else
778 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
779 
780 	if (resurrection) {
781 		sd->sd_offset = (off_t)eoff * 512;
782 		sd->sd_size = (off_t)esize * 512;
783 	} else {
784 		sd->sd_offset = (((off_t)pd->pd_meta[sdn]->disk_offset_high
785 		    << 32) + pd->pd_meta[sdn]->disk_offset) * 512;
786 		sd->sd_size = (((off_t)pd->pd_meta[sdn]->disk_sectors_high
787 		    << 32) + pd->pd_meta[sdn]->disk_sectors) * 512;
788 	}
789 
790 	if (resurrection) {
791 		/* Stale disk, almost same as new. */
792 		g_raid_change_subdisk_state(sd,
793 		    G_RAID_SUBDISK_S_NEW);
794 	} else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN) {
795 		/* Failed disk. */
796 		g_raid_change_subdisk_state(sd,
797 		    G_RAID_SUBDISK_S_FAILED);
798 	} else if (meta->disks[md_disk_pos].flags & PROMISE_F_REDIR) {
799 		/* Rebuilding disk. */
800 		g_raid_change_subdisk_state(sd,
801 		    G_RAID_SUBDISK_S_REBUILD);
802 		if (pd->pd_meta[sdn]->generation != meta->generation)
803 			sd->sd_rebuild_pos = 0;
804 		else {
805 			sd->sd_rebuild_pos =
806 			    (((off_t)pd->pd_meta[sdn]->disk_rebuild_high << 32) +
807 			     pd->pd_meta[sdn]->disk_rebuild) * 512;
808 		}
809 	} else if (!(meta->disks[md_disk_pos].flags & PROMISE_F_ONLINE)) {
810 		/* Rebuilding disk. */
811 		g_raid_change_subdisk_state(sd,
812 		    G_RAID_SUBDISK_S_NEW);
813 	} else if (pd->pd_meta[sdn]->generation != meta->generation ||
814 	    (meta->status & PROMISE_S_MARKED)) {
815 		/* Stale disk or dirty volume (unclean shutdown). */
816 		g_raid_change_subdisk_state(sd,
817 		    G_RAID_SUBDISK_S_STALE);
818 	} else {
819 		/* Up to date disk. */
820 		g_raid_change_subdisk_state(sd,
821 		    G_RAID_SUBDISK_S_ACTIVE);
822 	}
823 	g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
824 	    G_RAID_EVENT_SUBDISK);
825 
826 	return (resurrection);
827 }
828 
829 static void
830 g_raid_md_promise_refill(struct g_raid_softc *sc)
831 {
832 	struct g_raid_volume *vol;
833 	struct g_raid_subdisk *sd;
834 	struct g_raid_disk *disk;
835 	struct g_raid_md_object *md;
836 	struct g_raid_md_promise_perdisk *pd;
837 	struct g_raid_md_promise_pervolume *pv;
838 	int update, updated, i, bad;
839 
840 	md = sc->sc_md;
841 restart:
842 	updated = 0;
843 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
844 		pv = vol->v_md_data;
845 		if (!pv->pv_started || vol->v_stopping)
846 			continue;
847 
848 		/* Search for subdisk that needs replacement. */
849 		bad = 0;
850 		for (i = 0; i < vol->v_disks_count; i++) {
851 			sd = &vol->v_subdisks[i];
852 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
853 			    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
854 			        bad = 1;
855 		}
856 		if (!bad)
857 			continue;
858 
859 		G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
860 		    "trying to refill.", vol->v_name);
861 
862 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
863 			/* Skip failed. */
864 			if (disk->d_state < G_RAID_DISK_S_SPARE)
865 				continue;
866 			/* Skip already used by this volume. */
867 			for (i = 0; i < vol->v_disks_count; i++) {
868 				sd = &vol->v_subdisks[i];
869 				if (sd->sd_disk == disk)
870 					break;
871 			}
872 			if (i < vol->v_disks_count)
873 				continue;
874 
875 			/* Try to use disk if it has empty extents. */
876 			pd = disk->d_md_data;
877 			if (pd->pd_subdisks < PROMISE_MAX_SUBDISKS) {
878 				update =
879 				    g_raid_md_promise_start_disk(disk, -1, vol);
880 			} else
881 				update = 0;
882 			if (update) {
883 				updated = 1;
884 				g_raid_md_write_promise(md, vol, NULL, disk);
885 				break;
886 			}
887 		}
888 	}
889 	if (updated)
890 		goto restart;
891 }
892 
893 static void
894 g_raid_md_promise_start(struct g_raid_volume *vol)
895 {
896 	struct g_raid_softc *sc;
897 	struct g_raid_subdisk *sd;
898 	struct g_raid_disk *disk;
899 	struct g_raid_md_object *md;
900 	struct g_raid_md_promise_perdisk *pd;
901 	struct g_raid_md_promise_pervolume *pv;
902 	struct promise_raid_conf *meta;
903 	u_int i;
904 
905 	sc = vol->v_softc;
906 	md = sc->sc_md;
907 	pv = vol->v_md_data;
908 	meta = pv->pv_meta;
909 
910 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
911 	if (meta->type == PROMISE_T_RAID0)
912 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
913 	else if (meta->type == PROMISE_T_RAID1) {
914 		if (meta->array_width == 1)
915 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
916 		else
917 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
918 	} else if (meta->type == PROMISE_T_RAID3)
919 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID3;
920 	else if (meta->type == PROMISE_T_RAID5) {
921 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
922 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
923 	} else if (meta->type == PROMISE_T_SPAN)
924 		vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
925 	else if (meta->type == PROMISE_T_JBOD)
926 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
927 	else
928 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
929 	vol->v_strip_size = 512 << meta->stripe_shift; //ZZZ
930 	vol->v_disks_count = meta->total_disks;
931 	vol->v_mediasize = (off_t)meta->total_sectors * 512; //ZZZ
932 	if (meta->total_sectors_high < 256) /* If value looks sane. */
933 		vol->v_mediasize +=
934 		    ((off_t)meta->total_sectors_high << 32) * 512; //ZZZ
935 	vol->v_sectorsize = 512 * meta->sector_size;
936 	for (i = 0; i < vol->v_disks_count; i++) {
937 		sd = &vol->v_subdisks[i];
938 		sd->sd_offset = (((off_t)meta->disk_offset_high << 32) +
939 		    meta->disk_offset) * 512;
940 		sd->sd_size = (((off_t)meta->disk_sectors_high << 32) +
941 		    meta->disk_sectors) * 512;
942 	}
943 	g_raid_start_volume(vol);
944 
945 	/* Make all disks found till the moment take their places. */
946 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
947 		pd = disk->d_md_data;
948 		for (i = 0; i < pd->pd_subdisks; i++) {
949 			if (pd->pd_meta[i]->volume_id == meta->volume_id)
950 				g_raid_md_promise_start_disk(disk, i, vol);
951 		}
952 	}
953 
954 	pv->pv_started = 1;
955 	callout_stop(&pv->pv_start_co);
956 	G_RAID_DEBUG1(0, sc, "Volume started.");
957 	g_raid_md_write_promise(md, vol, NULL, NULL);
958 
959 	/* Pickup any STALE/SPARE disks to refill array if needed. */
960 	g_raid_md_promise_refill(sc);
961 
962 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
963 }
964 
965 static void
966 g_raid_promise_go(void *arg)
967 {
968 	struct g_raid_volume *vol;
969 	struct g_raid_softc *sc;
970 	struct g_raid_md_promise_pervolume *pv;
971 
972 	vol = arg;
973 	pv = vol->v_md_data;
974 	sc = vol->v_softc;
975 	if (!pv->pv_started) {
976 		G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
977 		g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
978 		    G_RAID_EVENT_VOLUME);
979 	}
980 }
981 
982 static void
983 g_raid_md_promise_new_disk(struct g_raid_disk *disk)
984 {
985 	struct g_raid_softc *sc;
986 	struct g_raid_md_object *md;
987 	struct promise_raid_conf *pdmeta;
988 	struct g_raid_md_promise_perdisk *pd;
989 	struct g_raid_md_promise_pervolume *pv;
990 	struct g_raid_volume *vol;
991 	int i;
992 	char buf[33];
993 
994 	sc = disk->d_softc;
995 	md = sc->sc_md;
996 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
997 
998 	if (pd->pd_subdisks == 0) {
999 		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
1000 		g_raid_md_promise_refill(sc);
1001 		return;
1002 	}
1003 
1004 	for (i = 0; i < pd->pd_subdisks; i++) {
1005 		pdmeta = pd->pd_meta[i];
1006 
1007 		/* Look for volume with matching ID. */
1008 		vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id);
1009 		if (vol == NULL) {
1010 			promise_meta_get_name(pdmeta, buf);
1011 			vol = g_raid_create_volume(sc, buf, pdmeta->array_number);
1012 			pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO);
1013 			pv->pv_id = pdmeta->volume_id;
1014 			vol->v_md_data = pv;
1015 			callout_init(&pv->pv_start_co, 1);
1016 			callout_reset(&pv->pv_start_co,
1017 			    g_raid_start_timeout * hz,
1018 			    g_raid_promise_go, vol);
1019 		} else
1020 			pv = vol->v_md_data;
1021 
1022 		/* If we haven't started yet - check metadata freshness. */
1023 		if (pv->pv_meta == NULL || !pv->pv_started) {
1024 			if (pv->pv_meta == NULL ||
1025 			    ((int16_t)(pdmeta->generation - pv->pv_generation)) > 0) {
1026 				G_RAID_DEBUG1(1, sc, "Newer disk");
1027 				if (pv->pv_meta != NULL)
1028 					free(pv->pv_meta, M_MD_PROMISE);
1029 				pv->pv_meta = promise_meta_copy(pdmeta);
1030 				pv->pv_generation = pv->pv_meta->generation;
1031 				pv->pv_disks_present = 1;
1032 			} else if (pdmeta->generation == pv->pv_generation) {
1033 				pv->pv_disks_present++;
1034 				G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
1035 				    pv->pv_disks_present,
1036 				    pv->pv_meta->total_disks);
1037 			} else {
1038 				G_RAID_DEBUG1(1, sc, "Older disk");
1039 			}
1040 		}
1041 	}
1042 
1043 	for (i = 0; i < pd->pd_subdisks; i++) {
1044 		pdmeta = pd->pd_meta[i];
1045 
1046 		/* Look for volume with matching ID. */
1047 		vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id);
1048 		if (vol == NULL)
1049 			continue;
1050 		pv = vol->v_md_data;
1051 
1052 		if (pv->pv_started) {
1053 			if (g_raid_md_promise_start_disk(disk, i, vol))
1054 				g_raid_md_write_promise(md, vol, NULL, NULL);
1055 		} else {
1056 			/* If we collected all needed disks - start array. */
1057 			if (pv->pv_disks_present == pv->pv_meta->total_disks)
1058 				g_raid_md_promise_start(vol);
1059 		}
1060 	}
1061 }
1062 
1063 static int
1064 g_raid_md_create_promise(struct g_raid_md_object *md, struct g_class *mp,
1065     struct g_geom **gp)
1066 {
1067 	struct g_geom *geom;
1068 	struct g_raid_softc *sc;
1069 
1070 	/* Search for existing node. */
1071 	LIST_FOREACH(geom, &mp->geom, geom) {
1072 		sc = geom->softc;
1073 		if (sc == NULL)
1074 			continue;
1075 		if (sc->sc_stopping != 0)
1076 			continue;
1077 		if (sc->sc_md->mdo_class != md->mdo_class)
1078 			continue;
1079 		break;
1080 	}
1081 	if (geom != NULL) {
1082 		*gp = geom;
1083 		return (G_RAID_MD_TASTE_EXISTING);
1084 	}
1085 
1086 	/* Create new one if not found. */
1087 	sc = g_raid_create_node(mp, "Promise", md);
1088 	if (sc == NULL)
1089 		return (G_RAID_MD_TASTE_FAIL);
1090 	md->mdo_softc = sc;
1091 	*gp = sc->sc_geom;
1092 	return (G_RAID_MD_TASTE_NEW);
1093 }
1094 
1095 static int
1096 g_raid_md_taste_promise(struct g_raid_md_object *md, struct g_class *mp,
1097                               struct g_consumer *cp, struct g_geom **gp)
1098 {
1099 	struct g_consumer *rcp;
1100 	struct g_provider *pp;
1101 	struct g_raid_softc *sc;
1102 	struct g_raid_disk *disk;
1103 	struct promise_raid_conf *metaarr[4];
1104 	struct g_raid_md_promise_perdisk *pd;
1105 	struct g_geom *geom;
1106 	int i, j, result, len, subdisks;
1107 	char name[16];
1108 	uint16_t vendor;
1109 
1110 	G_RAID_DEBUG(1, "Tasting Promise on %s", cp->provider->name);
1111 	pp = cp->provider;
1112 
1113 	/* Read metadata from device. */
1114 	g_topology_unlock();
1115 	vendor = 0xffff;
1116 	len = sizeof(vendor);
1117 	if (pp->geom->rank == 1)
1118 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
1119 	subdisks = promise_meta_read(cp, metaarr);
1120 	g_topology_lock();
1121 	if (subdisks == 0) {
1122 		if (g_raid_aggressive_spare) {
1123 			if (vendor == 0x105a || vendor == 0x1002) {
1124 				G_RAID_DEBUG(1,
1125 				    "No Promise metadata, forcing spare.");
1126 				goto search;
1127 			} else {
1128 				G_RAID_DEBUG(1,
1129 				    "Promise/ATI vendor mismatch "
1130 				    "0x%04x != 0x105a/0x1002",
1131 				    vendor);
1132 			}
1133 		}
1134 		return (G_RAID_MD_TASTE_FAIL);
1135 	}
1136 
1137 	/* Metadata valid. Print it. */
1138 	for (i = 0; i < subdisks; i++)
1139 		g_raid_md_promise_print(metaarr[i]);
1140 
1141 	/* Purge meaningless (empty/spare) records. */
1142 	for (i = 0; i < subdisks; ) {
1143 		if (metaarr[i]->disk.flags & PROMISE_F_ASSIGNED) {
1144 			i++;
1145 			continue;
1146 		}
1147 		free(metaarr[i], M_MD_PROMISE);
1148 		for (j = i; j < subdisks - 1; j++)
1149 			metaarr[i] = metaarr[j + 1];
1150 		metaarr[subdisks - 1] = NULL;
1151 		subdisks--;
1152 	}
1153 
1154 search:
1155 	/* Search for matching node. */
1156 	sc = NULL;
1157 	LIST_FOREACH(geom, &mp->geom, geom) {
1158 		sc = geom->softc;
1159 		if (sc == NULL)
1160 			continue;
1161 		if (sc->sc_stopping != 0)
1162 			continue;
1163 		if (sc->sc_md->mdo_class != md->mdo_class)
1164 			continue;
1165 		break;
1166 	}
1167 
1168 	/* Found matching node. */
1169 	if (geom != NULL) {
1170 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
1171 		result = G_RAID_MD_TASTE_EXISTING;
1172 
1173 	} else { /* Not found matching node -- create one. */
1174 		result = G_RAID_MD_TASTE_NEW;
1175 		snprintf(name, sizeof(name), "Promise");
1176 		sc = g_raid_create_node(mp, name, md);
1177 		md->mdo_softc = sc;
1178 		geom = sc->sc_geom;
1179 	}
1180 
1181 	/* There is no return after this point, so we close passed consumer. */
1182 	g_access(cp, -1, 0, 0);
1183 
1184 	rcp = g_new_consumer(geom);
1185 	rcp->flags |= G_CF_DIRECT_RECEIVE;
1186 	g_attach(rcp, pp);
1187 	if (g_access(rcp, 1, 1, 1) != 0)
1188 		; //goto fail1;
1189 
1190 	g_topology_unlock();
1191 	sx_xlock(&sc->sc_lock);
1192 
1193 	pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
1194 	pd->pd_subdisks = subdisks;
1195 	for (i = 0; i < subdisks; i++)
1196 		pd->pd_meta[i] = metaarr[i];
1197 	disk = g_raid_create_disk(sc);
1198 	disk->d_md_data = (void *)pd;
1199 	disk->d_consumer = rcp;
1200 	rcp->private = disk;
1201 
1202 	g_raid_get_disk_info(disk);
1203 
1204 	g_raid_md_promise_new_disk(disk);
1205 
1206 	sx_xunlock(&sc->sc_lock);
1207 	g_topology_lock();
1208 	*gp = geom;
1209 	return (result);
1210 }
1211 
1212 static int
1213 g_raid_md_event_promise(struct g_raid_md_object *md,
1214     struct g_raid_disk *disk, u_int event)
1215 {
1216 	struct g_raid_softc *sc;
1217 
1218 	sc = md->mdo_softc;
1219 	if (disk == NULL)
1220 		return (-1);
1221 	switch (event) {
1222 	case G_RAID_DISK_E_DISCONNECTED:
1223 		/* Delete disk. */
1224 		g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1225 		g_raid_destroy_disk(disk);
1226 		g_raid_md_promise_purge_volumes(sc);
1227 
1228 		/* Write updated metadata to all disks. */
1229 		g_raid_md_write_promise(md, NULL, NULL, NULL);
1230 
1231 		/* Check if anything left. */
1232 		if (g_raid_ndisks(sc, -1) == 0)
1233 			g_raid_destroy_node(sc, 0);
1234 		else
1235 			g_raid_md_promise_refill(sc);
1236 		return (0);
1237 	}
1238 	return (-2);
1239 }
1240 
1241 static int
1242 g_raid_md_volume_event_promise(struct g_raid_md_object *md,
1243     struct g_raid_volume *vol, u_int event)
1244 {
1245 	struct g_raid_md_promise_pervolume *pv;
1246 
1247 	pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
1248 	switch (event) {
1249 	case G_RAID_VOLUME_E_STARTMD:
1250 		if (!pv->pv_started)
1251 			g_raid_md_promise_start(vol);
1252 		return (0);
1253 	}
1254 	return (-2);
1255 }
1256 
1257 static int
1258 g_raid_md_ctl_promise(struct g_raid_md_object *md,
1259     struct gctl_req *req)
1260 {
1261 	struct g_raid_softc *sc;
1262 	struct g_raid_volume *vol, *vol1;
1263 	struct g_raid_subdisk *sd;
1264 	struct g_raid_disk *disk, *disks[PROMISE_MAX_DISKS];
1265 	struct g_raid_md_promise_perdisk *pd;
1266 	struct g_raid_md_promise_pervolume *pv;
1267 	struct g_consumer *cp;
1268 	struct g_provider *pp;
1269 	char arg[16];
1270 	const char *nodename, *verb, *volname, *levelname, *diskname;
1271 	char *tmp;
1272 	int *nargs, *force;
1273 	off_t esize, offs[PROMISE_MAX_DISKS], size, sectorsize, strip;
1274 	intmax_t *sizearg, *striparg;
1275 	int numdisks, i, len, level, qual;
1276 	int error;
1277 
1278 	sc = md->mdo_softc;
1279 	verb = gctl_get_param(req, "verb", NULL);
1280 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1281 	error = 0;
1282 	if (strcmp(verb, "label") == 0) {
1283 
1284 		if (*nargs < 4) {
1285 			gctl_error(req, "Invalid number of arguments.");
1286 			return (-1);
1287 		}
1288 		volname = gctl_get_asciiparam(req, "arg1");
1289 		if (volname == NULL) {
1290 			gctl_error(req, "No volume name.");
1291 			return (-2);
1292 		}
1293 		levelname = gctl_get_asciiparam(req, "arg2");
1294 		if (levelname == NULL) {
1295 			gctl_error(req, "No RAID level.");
1296 			return (-3);
1297 		}
1298 		if (strcasecmp(levelname, "RAID5") == 0)
1299 			levelname = "RAID5-LA";
1300 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1301 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1302 			return (-4);
1303 		}
1304 		numdisks = *nargs - 3;
1305 		force = gctl_get_paraml(req, "force", sizeof(*force));
1306 		if (!g_raid_md_promise_supported(level, qual, numdisks,
1307 		    force ? *force : 0)) {
1308 			gctl_error(req, "Unsupported RAID level "
1309 			    "(0x%02x/0x%02x), or number of disks (%d).",
1310 			    level, qual, numdisks);
1311 			return (-5);
1312 		}
1313 
1314 		/* Search for disks, connect them and probe. */
1315 		size = INT64_MAX;
1316 		sectorsize = 0;
1317 		bzero(disks, sizeof(disks));
1318 		bzero(offs, sizeof(offs));
1319 		for (i = 0; i < numdisks; i++) {
1320 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1321 			diskname = gctl_get_asciiparam(req, arg);
1322 			if (diskname == NULL) {
1323 				gctl_error(req, "No disk name (%s).", arg);
1324 				error = -6;
1325 				break;
1326 			}
1327 			if (strcmp(diskname, "NONE") == 0)
1328 				continue;
1329 
1330 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1331 				if (disk->d_consumer != NULL &&
1332 				    disk->d_consumer->provider != NULL &&
1333 				    strcmp(disk->d_consumer->provider->name,
1334 				     diskname) == 0)
1335 					break;
1336 			}
1337 			if (disk != NULL) {
1338 				if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
1339 					gctl_error(req, "Disk '%s' is in a "
1340 					    "wrong state (%s).", diskname,
1341 					    g_raid_disk_state2str(disk->d_state));
1342 					error = -7;
1343 					break;
1344 				}
1345 				pd = disk->d_md_data;
1346 				if (pd->pd_subdisks >= PROMISE_MAX_SUBDISKS) {
1347 					gctl_error(req, "Disk '%s' already "
1348 					    "used by %d volumes.",
1349 					    diskname, pd->pd_subdisks);
1350 					error = -7;
1351 					break;
1352 				}
1353 				pp = disk->d_consumer->provider;
1354 				disks[i] = disk;
1355 				promise_meta_unused_range(pd->pd_meta,
1356 				    pd->pd_subdisks,
1357 				    pp->mediasize / pp->sectorsize,
1358 				    &offs[i], &esize);
1359 				size = MIN(size, (off_t)esize * pp->sectorsize);
1360 				sectorsize = MAX(sectorsize, pp->sectorsize);
1361 				continue;
1362 			}
1363 
1364 			g_topology_lock();
1365 			cp = g_raid_open_consumer(sc, diskname);
1366 			if (cp == NULL) {
1367 				gctl_error(req, "Can't open disk '%s'.",
1368 				    diskname);
1369 				g_topology_unlock();
1370 				error = -8;
1371 				break;
1372 			}
1373 			pp = cp->provider;
1374 			pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
1375 			disk = g_raid_create_disk(sc);
1376 			disk->d_md_data = (void *)pd;
1377 			disk->d_consumer = cp;
1378 			disks[i] = disk;
1379 			cp->private = disk;
1380 			g_topology_unlock();
1381 
1382 			g_raid_get_disk_info(disk);
1383 
1384 			/* Reserve some space for metadata. */
1385 			size = MIN(size, pp->mediasize - 131072llu * pp->sectorsize);
1386 			sectorsize = MAX(sectorsize, pp->sectorsize);
1387 		}
1388 		if (error != 0) {
1389 			for (i = 0; i < numdisks; i++) {
1390 				if (disks[i] != NULL &&
1391 				    disks[i]->d_state == G_RAID_DISK_S_NONE)
1392 					g_raid_destroy_disk(disks[i]);
1393 			}
1394 			return (error);
1395 		}
1396 
1397 		if (sectorsize <= 0) {
1398 			gctl_error(req, "Can't get sector size.");
1399 			return (-8);
1400 		}
1401 
1402 		/* Handle size argument. */
1403 		len = sizeof(*sizearg);
1404 		sizearg = gctl_get_param(req, "size", &len);
1405 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1406 		    *sizearg > 0) {
1407 			if (*sizearg > size) {
1408 				gctl_error(req, "Size too big %lld > %lld.",
1409 				    (long long)*sizearg, (long long)size);
1410 				return (-9);
1411 			}
1412 			size = *sizearg;
1413 		}
1414 
1415 		/* Handle strip argument. */
1416 		strip = 131072;
1417 		len = sizeof(*striparg);
1418 		striparg = gctl_get_param(req, "strip", &len);
1419 		if (striparg != NULL && len == sizeof(*striparg) &&
1420 		    *striparg > 0) {
1421 			if (*striparg < sectorsize) {
1422 				gctl_error(req, "Strip size too small.");
1423 				return (-10);
1424 			}
1425 			if (*striparg % sectorsize != 0) {
1426 				gctl_error(req, "Incorrect strip size.");
1427 				return (-11);
1428 			}
1429 			strip = *striparg;
1430 		}
1431 
1432 		/* Round size down to strip or sector. */
1433 		if (level == G_RAID_VOLUME_RL_RAID1 ||
1434 		    level == G_RAID_VOLUME_RL_SINGLE ||
1435 		    level == G_RAID_VOLUME_RL_CONCAT)
1436 			size -= (size % sectorsize);
1437 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1438 		    (numdisks & 1) != 0)
1439 			size -= (size % (2 * strip));
1440 		else
1441 			size -= (size % strip);
1442 		if (size <= 0) {
1443 			gctl_error(req, "Size too small.");
1444 			return (-13);
1445 		}
1446 
1447 		/* We have all we need, create things: volume, ... */
1448 		pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO);
1449 		arc4rand(&pv->pv_id, sizeof(pv->pv_id), 0);
1450 		pv->pv_generation = 0;
1451 		pv->pv_started = 1;
1452 		vol = g_raid_create_volume(sc, volname, -1);
1453 		vol->v_md_data = pv;
1454 		vol->v_raid_level = level;
1455 		vol->v_raid_level_qualifier = qual;
1456 		vol->v_strip_size = strip;
1457 		vol->v_disks_count = numdisks;
1458 		if (level == G_RAID_VOLUME_RL_RAID0 ||
1459 		    level == G_RAID_VOLUME_RL_CONCAT ||
1460 		    level == G_RAID_VOLUME_RL_SINGLE)
1461 			vol->v_mediasize = size * numdisks;
1462 		else if (level == G_RAID_VOLUME_RL_RAID1)
1463 			vol->v_mediasize = size;
1464 		else if (level == G_RAID_VOLUME_RL_RAID3 ||
1465 		    level == G_RAID_VOLUME_RL_RAID5)
1466 			vol->v_mediasize = size * (numdisks - 1);
1467 		else { /* RAID1E */
1468 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1469 			    strip;
1470 		}
1471 		vol->v_sectorsize = sectorsize;
1472 		g_raid_start_volume(vol);
1473 
1474 		/* , and subdisks. */
1475 		for (i = 0; i < numdisks; i++) {
1476 			disk = disks[i];
1477 			sd = &vol->v_subdisks[i];
1478 			sd->sd_disk = disk;
1479 			sd->sd_offset = (off_t)offs[i] * 512;
1480 			sd->sd_size = size;
1481 			if (disk == NULL)
1482 				continue;
1483 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1484 			g_raid_change_disk_state(disk,
1485 			    G_RAID_DISK_S_ACTIVE);
1486 			g_raid_change_subdisk_state(sd,
1487 			    G_RAID_SUBDISK_S_ACTIVE);
1488 			g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1489 			    G_RAID_EVENT_SUBDISK);
1490 		}
1491 
1492 		/* Write metadata based on created entities. */
1493 		G_RAID_DEBUG1(0, sc, "Array started.");
1494 		g_raid_md_write_promise(md, vol, NULL, NULL);
1495 
1496 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1497 		g_raid_md_promise_refill(sc);
1498 
1499 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1500 		    G_RAID_EVENT_VOLUME);
1501 		return (0);
1502 	}
1503 	if (strcmp(verb, "add") == 0) {
1504 
1505 		gctl_error(req, "`add` command is not applicable, "
1506 		    "use `label` instead.");
1507 		return (-99);
1508 	}
1509 	if (strcmp(verb, "delete") == 0) {
1510 
1511 		nodename = gctl_get_asciiparam(req, "arg0");
1512 		if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
1513 			nodename = NULL;
1514 
1515 		/* Full node destruction. */
1516 		if (*nargs == 1 && nodename != NULL) {
1517 			/* Check if some volume is still open. */
1518 			force = gctl_get_paraml(req, "force", sizeof(*force));
1519 			if (force != NULL && *force == 0 &&
1520 			    g_raid_nopens(sc) != 0) {
1521 				gctl_error(req, "Some volume is still open.");
1522 				return (-4);
1523 			}
1524 
1525 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1526 				if (disk->d_consumer)
1527 					promise_meta_erase(disk->d_consumer);
1528 			}
1529 			g_raid_destroy_node(sc, 0);
1530 			return (0);
1531 		}
1532 
1533 		/* Destroy specified volume. If it was last - all node. */
1534 		if (*nargs > 2) {
1535 			gctl_error(req, "Invalid number of arguments.");
1536 			return (-1);
1537 		}
1538 		volname = gctl_get_asciiparam(req,
1539 		    nodename != NULL ? "arg1" : "arg0");
1540 		if (volname == NULL) {
1541 			gctl_error(req, "No volume name.");
1542 			return (-2);
1543 		}
1544 
1545 		/* Search for volume. */
1546 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1547 			if (strcmp(vol->v_name, volname) == 0)
1548 				break;
1549 			pp = vol->v_provider;
1550 			if (pp == NULL)
1551 				continue;
1552 			if (strcmp(pp->name, volname) == 0)
1553 				break;
1554 			if (strncmp(pp->name, "raid/", 5) == 0 &&
1555 			    strcmp(pp->name + 5, volname) == 0)
1556 				break;
1557 		}
1558 		if (vol == NULL) {
1559 			i = strtol(volname, &tmp, 10);
1560 			if (verb != volname && tmp[0] == 0) {
1561 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1562 					if (vol->v_global_id == i)
1563 						break;
1564 				}
1565 			}
1566 		}
1567 		if (vol == NULL) {
1568 			gctl_error(req, "Volume '%s' not found.", volname);
1569 			return (-3);
1570 		}
1571 
1572 		/* Check if volume is still open. */
1573 		force = gctl_get_paraml(req, "force", sizeof(*force));
1574 		if (force != NULL && *force == 0 &&
1575 		    vol->v_provider_open != 0) {
1576 			gctl_error(req, "Volume is still open.");
1577 			return (-4);
1578 		}
1579 
1580 		/* Destroy volume and potentially node. */
1581 		i = 0;
1582 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
1583 			i++;
1584 		if (i >= 2) {
1585 			g_raid_destroy_volume(vol);
1586 			g_raid_md_promise_purge_disks(sc);
1587 			g_raid_md_write_promise(md, NULL, NULL, NULL);
1588 		} else {
1589 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1590 				if (disk->d_consumer)
1591 					promise_meta_erase(disk->d_consumer);
1592 			}
1593 			g_raid_destroy_node(sc, 0);
1594 		}
1595 		return (0);
1596 	}
1597 	if (strcmp(verb, "remove") == 0 ||
1598 	    strcmp(verb, "fail") == 0) {
1599 		if (*nargs < 2) {
1600 			gctl_error(req, "Invalid number of arguments.");
1601 			return (-1);
1602 		}
1603 		for (i = 1; i < *nargs; i++) {
1604 			snprintf(arg, sizeof(arg), "arg%d", i);
1605 			diskname = gctl_get_asciiparam(req, arg);
1606 			if (diskname == NULL) {
1607 				gctl_error(req, "No disk name (%s).", arg);
1608 				error = -2;
1609 				break;
1610 			}
1611 			if (strncmp(diskname, "/dev/", 5) == 0)
1612 				diskname += 5;
1613 
1614 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1615 				if (disk->d_consumer != NULL &&
1616 				    disk->d_consumer->provider != NULL &&
1617 				    strcmp(disk->d_consumer->provider->name,
1618 				     diskname) == 0)
1619 					break;
1620 			}
1621 			if (disk == NULL) {
1622 				gctl_error(req, "Disk '%s' not found.",
1623 				    diskname);
1624 				error = -3;
1625 				break;
1626 			}
1627 
1628 			if (strcmp(verb, "fail") == 0) {
1629 				g_raid_md_fail_disk_promise(md, NULL, disk);
1630 				continue;
1631 			}
1632 
1633 			/* Erase metadata on deleting disk and destroy it. */
1634 			promise_meta_erase(disk->d_consumer);
1635 			g_raid_destroy_disk(disk);
1636 		}
1637 		g_raid_md_promise_purge_volumes(sc);
1638 
1639 		/* Write updated metadata to remaining disks. */
1640 		g_raid_md_write_promise(md, NULL, NULL, NULL);
1641 
1642 		/* Check if anything left. */
1643 		if (g_raid_ndisks(sc, -1) == 0)
1644 			g_raid_destroy_node(sc, 0);
1645 		else
1646 			g_raid_md_promise_refill(sc);
1647 		return (error);
1648 	}
1649 	if (strcmp(verb, "insert") == 0) {
1650 		if (*nargs < 2) {
1651 			gctl_error(req, "Invalid number of arguments.");
1652 			return (-1);
1653 		}
1654 		for (i = 1; i < *nargs; i++) {
1655 			/* Get disk name. */
1656 			snprintf(arg, sizeof(arg), "arg%d", i);
1657 			diskname = gctl_get_asciiparam(req, arg);
1658 			if (diskname == NULL) {
1659 				gctl_error(req, "No disk name (%s).", arg);
1660 				error = -3;
1661 				break;
1662 			}
1663 
1664 			/* Try to find provider with specified name. */
1665 			g_topology_lock();
1666 			cp = g_raid_open_consumer(sc, diskname);
1667 			if (cp == NULL) {
1668 				gctl_error(req, "Can't open disk '%s'.",
1669 				    diskname);
1670 				g_topology_unlock();
1671 				error = -4;
1672 				break;
1673 			}
1674 			pp = cp->provider;
1675 			g_topology_unlock();
1676 
1677 			pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO);
1678 
1679 			disk = g_raid_create_disk(sc);
1680 			disk->d_consumer = cp;
1681 			disk->d_md_data = (void *)pd;
1682 			cp->private = disk;
1683 
1684 			g_raid_get_disk_info(disk);
1685 
1686 			/* Welcome the "new" disk. */
1687 			g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
1688 			promise_meta_write_spare(cp);
1689 			g_raid_md_promise_refill(sc);
1690 		}
1691 		return (error);
1692 	}
1693 	return (-100);
1694 }
1695 
1696 static int
1697 g_raid_md_write_promise(struct g_raid_md_object *md, struct g_raid_volume *tvol,
1698     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1699 {
1700 	struct g_raid_softc *sc;
1701 	struct g_raid_volume *vol;
1702 	struct g_raid_subdisk *sd;
1703 	struct g_raid_disk *disk;
1704 	struct g_raid_md_promise_perdisk *pd;
1705 	struct g_raid_md_promise_pervolume *pv;
1706 	struct promise_raid_conf *meta;
1707 	off_t rebuild_lba64;
1708 	int i, j, pos, rebuild;
1709 
1710 	sc = md->mdo_softc;
1711 
1712 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1713 		return (0);
1714 
1715 	/* Generate new per-volume metadata for affected volumes. */
1716 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1717 		if (vol->v_stopping)
1718 			continue;
1719 
1720 		/* Skip volumes not related to specified targets. */
1721 		if (tvol != NULL && vol != tvol)
1722 			continue;
1723 		if (tsd != NULL && vol != tsd->sd_volume)
1724 			continue;
1725 		if (tdisk != NULL) {
1726 			for (i = 0; i < vol->v_disks_count; i++) {
1727 				if (vol->v_subdisks[i].sd_disk == tdisk)
1728 					break;
1729 			}
1730 			if (i >= vol->v_disks_count)
1731 				continue;
1732 		}
1733 
1734 		pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
1735 		pv->pv_generation++;
1736 
1737 		meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO);
1738 		if (pv->pv_meta != NULL)
1739 			memcpy(meta, pv->pv_meta, sizeof(*meta));
1740 		memcpy(meta->promise_id, PROMISE_MAGIC,
1741 		    sizeof(PROMISE_MAGIC) - 1);
1742 		meta->dummy_0 = 0x00020000;
1743 		meta->integrity = PROMISE_I_VALID;
1744 
1745 		meta->generation = pv->pv_generation;
1746 		meta->status = PROMISE_S_VALID | PROMISE_S_ONLINE |
1747 		    PROMISE_S_INITED | PROMISE_S_READY;
1748 		if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
1749 			meta->status |= PROMISE_S_DEGRADED;
1750 		if (vol->v_dirty)
1751 			meta->status |= PROMISE_S_MARKED; /* XXX: INVENTED! */
1752 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0 ||
1753 		    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE)
1754 			meta->type = PROMISE_T_RAID0;
1755 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1756 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
1757 			meta->type = PROMISE_T_RAID1;
1758 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3)
1759 			meta->type = PROMISE_T_RAID3;
1760 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
1761 			meta->type = PROMISE_T_RAID5;
1762 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT)
1763 			meta->type = PROMISE_T_SPAN;
1764 		else
1765 			meta->type = PROMISE_T_JBOD;
1766 		meta->total_disks = vol->v_disks_count;
1767 		meta->stripe_shift = ffs(vol->v_strip_size / 1024);
1768 		meta->array_width = vol->v_disks_count;
1769 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1770 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
1771 			meta->array_width /= 2;
1772 		meta->array_number = vol->v_global_id;
1773 		meta->total_sectors = vol->v_mediasize / 512;
1774 		meta->total_sectors_high = (vol->v_mediasize / 512) >> 32;
1775 		meta->sector_size = vol->v_sectorsize / 512;
1776 		meta->cylinders = meta->total_sectors / (255 * 63) - 1;
1777 		meta->heads = 254;
1778 		meta->sectors = 63;
1779 		meta->volume_id = pv->pv_id;
1780 		rebuild_lba64 = UINT64_MAX;
1781 		rebuild = 0;
1782 		for (i = 0; i < vol->v_disks_count; i++) {
1783 			sd = &vol->v_subdisks[i];
1784 			/* For RAID0+1 we need to translate order. */
1785 			pos = promise_meta_translate_disk(vol, i);
1786 			meta->disks[pos].flags = PROMISE_F_VALID |
1787 			    PROMISE_F_ASSIGNED;
1788 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE) {
1789 				meta->disks[pos].flags |= 0;
1790 			} else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED) {
1791 				meta->disks[pos].flags |=
1792 				    PROMISE_F_DOWN | PROMISE_F_REDIR;
1793 			} else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD) {
1794 				meta->disks[pos].flags |=
1795 				    PROMISE_F_ONLINE | PROMISE_F_REDIR;
1796 				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) {
1797 					rebuild_lba64 = MIN(rebuild_lba64,
1798 					    sd->sd_rebuild_pos / 512);
1799 				} else
1800 					rebuild_lba64 = 0;
1801 				rebuild = 1;
1802 			} else {
1803 				meta->disks[pos].flags |= PROMISE_F_ONLINE;
1804 				if (sd->sd_state < G_RAID_SUBDISK_S_ACTIVE) {
1805 					meta->status |= PROMISE_S_MARKED;
1806 					if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
1807 						rebuild_lba64 = MIN(rebuild_lba64,
1808 						    sd->sd_rebuild_pos / 512);
1809 					} else
1810 						rebuild_lba64 = 0;
1811 				}
1812 			}
1813 			if (pv->pv_meta != NULL) {
1814 				meta->disks[pos].id = pv->pv_meta->disks[pos].id;
1815 			} else {
1816 				meta->disks[pos].number = i * 2;
1817 				arc4rand(&meta->disks[pos].id,
1818 				    sizeof(meta->disks[pos].id), 0);
1819 			}
1820 		}
1821 		promise_meta_put_name(meta, vol->v_name);
1822 
1823 		/* Try to mimic AMD BIOS rebuild/resync behavior. */
1824 		if (rebuild_lba64 != UINT64_MAX) {
1825 			if (rebuild)
1826 				meta->magic_3 = 0x03040010UL; /* Rebuild? */
1827 			else
1828 				meta->magic_3 = 0x03040008UL; /* Resync? */
1829 			/* Translate from per-disk to per-volume LBA. */
1830 			if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1831 			    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
1832 				rebuild_lba64 *= meta->array_width;
1833 			} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1834 			    vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) {
1835 				rebuild_lba64 *= meta->array_width - 1;
1836 			} else
1837 				rebuild_lba64 = 0;
1838 		} else
1839 			meta->magic_3 = 0x03000000UL;
1840 		meta->rebuild_lba64 = rebuild_lba64;
1841 		meta->magic_4 = 0x04010101UL;
1842 
1843 		/* Replace per-volume metadata with new. */
1844 		if (pv->pv_meta != NULL)
1845 			free(pv->pv_meta, M_MD_PROMISE);
1846 		pv->pv_meta = meta;
1847 
1848 		/* Copy new metadata to the disks, adding or replacing old. */
1849 		for (i = 0; i < vol->v_disks_count; i++) {
1850 			sd = &vol->v_subdisks[i];
1851 			disk = sd->sd_disk;
1852 			if (disk == NULL)
1853 				continue;
1854 			/* For RAID0+1 we need to translate order. */
1855 			pos = promise_meta_translate_disk(vol, i);
1856 			pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
1857 			for (j = 0; j < pd->pd_subdisks; j++) {
1858 				if (pd->pd_meta[j]->volume_id == meta->volume_id)
1859 					break;
1860 			}
1861 			if (j == pd->pd_subdisks)
1862 				pd->pd_subdisks++;
1863 			if (pd->pd_meta[j] != NULL)
1864 				free(pd->pd_meta[j], M_MD_PROMISE);
1865 			pd->pd_meta[j] = promise_meta_copy(meta);
1866 			pd->pd_meta[j]->disk = meta->disks[pos];
1867 			pd->pd_meta[j]->disk.number = pos;
1868 			pd->pd_meta[j]->disk_offset_high =
1869 			    (sd->sd_offset / 512) >> 32;
1870 			pd->pd_meta[j]->disk_offset = sd->sd_offset / 512;
1871 			pd->pd_meta[j]->disk_sectors_high =
1872 			    (sd->sd_size / 512) >> 32;
1873 			pd->pd_meta[j]->disk_sectors = sd->sd_size / 512;
1874 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) {
1875 				pd->pd_meta[j]->disk_rebuild_high =
1876 				    (sd->sd_rebuild_pos / 512) >> 32;
1877 				pd->pd_meta[j]->disk_rebuild =
1878 				    sd->sd_rebuild_pos / 512;
1879 			} else if (sd->sd_state < G_RAID_SUBDISK_S_REBUILD) {
1880 				pd->pd_meta[j]->disk_rebuild_high = 0;
1881 				pd->pd_meta[j]->disk_rebuild = 0;
1882 			} else {
1883 				pd->pd_meta[j]->disk_rebuild_high = UINT32_MAX;
1884 				pd->pd_meta[j]->disk_rebuild = UINT32_MAX;
1885 			}
1886 			pd->pd_updated = 1;
1887 		}
1888 	}
1889 
1890 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1891 		pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
1892 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
1893 			continue;
1894 		if (!pd->pd_updated)
1895 			continue;
1896 		G_RAID_DEBUG(1, "Writing Promise metadata to %s",
1897 		    g_raid_get_diskname(disk));
1898 		for (i = 0; i < pd->pd_subdisks; i++)
1899 			g_raid_md_promise_print(pd->pd_meta[i]);
1900 		promise_meta_write(disk->d_consumer,
1901 		    pd->pd_meta, pd->pd_subdisks);
1902 		pd->pd_updated = 0;
1903 	}
1904 
1905 	return (0);
1906 }
1907 
1908 static int
1909 g_raid_md_fail_disk_promise(struct g_raid_md_object *md,
1910     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1911 {
1912 	struct g_raid_softc *sc;
1913 	struct g_raid_md_promise_perdisk *pd;
1914 	struct g_raid_subdisk *sd;
1915 	int i, pos;
1916 
1917 	sc = md->mdo_softc;
1918 	pd = (struct g_raid_md_promise_perdisk *)tdisk->d_md_data;
1919 
1920 	/* We can't fail disk that is not a part of array now. */
1921 	if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
1922 		return (-1);
1923 
1924 	/*
1925 	 * Mark disk as failed in metadata and try to write that metadata
1926 	 * to the disk itself to prevent it's later resurrection as STALE.
1927 	 */
1928 	if (pd->pd_subdisks > 0 && tdisk->d_consumer != NULL)
1929 		G_RAID_DEBUG(1, "Writing Promise metadata to %s",
1930 		    g_raid_get_diskname(tdisk));
1931 	for (i = 0; i < pd->pd_subdisks; i++) {
1932 		pd->pd_meta[i]->disk.flags |=
1933 		    PROMISE_F_DOWN | PROMISE_F_REDIR;
1934 		pos = pd->pd_meta[i]->disk.number;
1935 		if (pos >= 0 && pos < PROMISE_MAX_DISKS) {
1936 			pd->pd_meta[i]->disks[pos].flags |=
1937 			    PROMISE_F_DOWN | PROMISE_F_REDIR;
1938 		}
1939 		g_raid_md_promise_print(pd->pd_meta[i]);
1940 	}
1941 	if (tdisk->d_consumer != NULL)
1942 		promise_meta_write(tdisk->d_consumer,
1943 		    pd->pd_meta, pd->pd_subdisks);
1944 
1945 	/* Change states. */
1946 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
1947 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
1948 		g_raid_change_subdisk_state(sd,
1949 		    G_RAID_SUBDISK_S_FAILED);
1950 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
1951 		    G_RAID_EVENT_SUBDISK);
1952 	}
1953 
1954 	/* Write updated metadata to remaining disks. */
1955 	g_raid_md_write_promise(md, NULL, NULL, tdisk);
1956 
1957 	g_raid_md_promise_refill(sc);
1958 	return (0);
1959 }
1960 
1961 static int
1962 g_raid_md_free_disk_promise(struct g_raid_md_object *md,
1963     struct g_raid_disk *disk)
1964 {
1965 	struct g_raid_md_promise_perdisk *pd;
1966 	int i;
1967 
1968 	pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data;
1969 	for (i = 0; i < pd->pd_subdisks; i++) {
1970 		if (pd->pd_meta[i] != NULL) {
1971 			free(pd->pd_meta[i], M_MD_PROMISE);
1972 			pd->pd_meta[i] = NULL;
1973 		}
1974 	}
1975 	free(pd, M_MD_PROMISE);
1976 	disk->d_md_data = NULL;
1977 	return (0);
1978 }
1979 
1980 static int
1981 g_raid_md_free_volume_promise(struct g_raid_md_object *md,
1982     struct g_raid_volume *vol)
1983 {
1984 	struct g_raid_md_promise_pervolume *pv;
1985 
1986 	pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data;
1987 	if (pv && pv->pv_meta != NULL) {
1988 		free(pv->pv_meta, M_MD_PROMISE);
1989 		pv->pv_meta = NULL;
1990 	}
1991 	if (pv && !pv->pv_started) {
1992 		pv->pv_started = 1;
1993 		callout_stop(&pv->pv_start_co);
1994 	}
1995 	free(pv, M_MD_PROMISE);
1996 	vol->v_md_data = NULL;
1997 	return (0);
1998 }
1999 
2000 static int
2001 g_raid_md_free_promise(struct g_raid_md_object *md)
2002 {
2003 
2004 	return (0);
2005 }
2006 
2007 G_RAID_MD_DECLARE(promise, "Promise");
2008