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