xref: /freebsd/sys/geom/raid/md_sii.c (revision 6486b015fc84e96725fef22b0e3363351399ae83)
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 <sys/taskqueue.h>
42 #include <geom/geom.h>
43 #include "geom/raid/g_raid.h"
44 #include "g_raid_md_if.h"
45 
46 static MALLOC_DEFINE(M_MD_SII, "md_sii_data", "GEOM_RAID SiI metadata");
47 
48 struct sii_raid_conf {
49 	uint16_t	ata_params_00_53[54];
50 	uint64_t	total_sectors;		/* 54 - 57 */
51 	uint16_t	ata_params_58_81[72];
52 	uint16_t	product_id;		/* 130 */
53 	uint16_t	vendor_id;		/* 131 */
54 	uint16_t	version_minor;		/* 132 */
55 	uint16_t	version_major;		/* 133 */
56 	uint8_t		timestamp[6];		/* 134 - 136 */
57 	uint16_t	strip_sectors;		/* 137 */
58 	uint16_t	dummy_2;
59 	uint8_t		disk_number;		/* 139 */
60 	uint8_t		type;
61 #define SII_T_RAID0             0x00
62 #define SII_T_RAID1             0x01
63 #define SII_T_RAID01            0x02
64 #define SII_T_SPARE             0x03
65 #define SII_T_CONCAT            0x04
66 #define SII_T_RAID5             0x10
67 #define SII_T_RESERVED          0xfd
68 #define SII_T_JBOD              0xff
69 
70 	uint8_t		raid0_disks;		/* 140 */
71 	uint8_t		raid0_ident;
72 	uint8_t		raid1_disks;		/* 141 */
73 	uint8_t		raid1_ident;
74 	uint64_t	rebuild_lba;		/* 142 - 145 */
75 	uint32_t	generation;		/* 146 - 147 */
76 	uint8_t		disk_status;		/* 148 */
77 #define SII_S_CURRENT           0x01
78 #define SII_S_REBUILD           0x02
79 #define SII_S_DROPPED           0x03
80 #define SII_S_REMOVED           0x04
81 
82 	uint8_t		raid_status;
83 #define SII_S_ONLINE            0x01
84 #define SII_S_AVAILABLE         0x02
85 
86 	uint8_t		raid_location;		/* 149 */
87 	uint8_t		disk_location;
88 	uint8_t		auto_rebuild;		/* 150 */
89 #define SII_R_REBUILD           0x00
90 #define SII_R_NOREBUILD         0xff
91 
92 	uint8_t		dummy_3;
93 	uint8_t		name[16];		/* 151 - 158 */
94 	uint16_t	checksum;		/* 159 */
95 	uint16_t	ata_params_160_255[96];
96 } __packed;
97 
98 struct g_raid_md_sii_perdisk {
99 	struct sii_raid_conf	*pd_meta;
100 	int			 pd_disk_pos;
101 	off_t			 pd_disk_size;
102 };
103 
104 struct g_raid_md_sii_object {
105 	struct g_raid_md_object	 mdio_base;
106 	uint8_t			 mdio_timestamp[6];
107 	uint8_t			 mdio_location;
108 	uint32_t		 mdio_generation;
109 	struct sii_raid_conf	*mdio_meta;
110 	struct callout		 mdio_start_co;	/* STARTING state timer. */
111 	int			 mdio_total_disks;
112 	int			 mdio_disks_present;
113 	int			 mdio_started;
114 	int			 mdio_incomplete;
115 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
116 };
117 
118 static g_raid_md_create_t g_raid_md_create_sii;
119 static g_raid_md_taste_t g_raid_md_taste_sii;
120 static g_raid_md_event_t g_raid_md_event_sii;
121 static g_raid_md_ctl_t g_raid_md_ctl_sii;
122 static g_raid_md_write_t g_raid_md_write_sii;
123 static g_raid_md_fail_disk_t g_raid_md_fail_disk_sii;
124 static g_raid_md_free_disk_t g_raid_md_free_disk_sii;
125 static g_raid_md_free_t g_raid_md_free_sii;
126 
127 static kobj_method_t g_raid_md_sii_methods[] = {
128 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_sii),
129 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_sii),
130 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_sii),
131 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_sii),
132 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_sii),
133 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_sii),
134 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_sii),
135 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_sii),
136 	{ 0, 0 }
137 };
138 
139 static struct g_raid_md_class g_raid_md_sii_class = {
140 	"SiI",
141 	g_raid_md_sii_methods,
142 	sizeof(struct g_raid_md_sii_object),
143 	.mdc_priority = 100
144 };
145 
146 static void
147 g_raid_md_sii_print(struct sii_raid_conf *meta)
148 {
149 
150 	if (g_raid_debug < 1)
151 		return;
152 
153 	printf("********* ATA SiI RAID Metadata *********\n");
154 	printf("total_sectors       %llu\n",
155 	    (long long unsigned)meta->total_sectors);
156 	printf("product_id          0x%04x\n", meta->product_id);
157 	printf("vendor_id           0x%04x\n", meta->vendor_id);
158 	printf("version_minor       0x%04x\n", meta->version_minor);
159 	printf("version_major       0x%04x\n", meta->version_major);
160 	printf("timestamp           0x%02x%02x%02x%02x%02x%02x\n",
161 	    meta->timestamp[5], meta->timestamp[4], meta->timestamp[3],
162 	    meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]);
163 	printf("strip_sectors       %d\n", meta->strip_sectors);
164 	printf("disk_number         %d\n", meta->disk_number);
165 	printf("type                0x%02x\n", meta->type);
166 	printf("raid0_disks         %d\n", meta->raid0_disks);
167 	printf("raid0_ident         %d\n", meta->raid0_ident);
168 	printf("raid1_disks         %d\n", meta->raid1_disks);
169 	printf("raid1_ident         %d\n", meta->raid1_ident);
170 	printf("rebuild_lba         %llu\n",
171 	    (long long unsigned)meta->rebuild_lba);
172 	printf("generation          %d\n", meta->generation);
173 	printf("disk_status         %d\n", meta->disk_status);
174 	printf("raid_status         %d\n", meta->raid_status);
175 	printf("raid_location       %d\n", meta->raid_location);
176 	printf("disk_location       %d\n", meta->disk_location);
177 	printf("auto_rebuild        %d\n", meta->auto_rebuild);
178 	printf("name                <%.16s>\n", meta->name);
179 	printf("checksum            0x%04x\n", meta->checksum);
180 	printf("=================================================\n");
181 }
182 
183 static struct sii_raid_conf *
184 sii_meta_copy(struct sii_raid_conf *meta)
185 {
186 	struct sii_raid_conf *nmeta;
187 
188 	nmeta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
189 	memcpy(nmeta, meta, sizeof(*meta));
190 	return (nmeta);
191 }
192 
193 static int
194 sii_meta_total_disks(struct sii_raid_conf *meta)
195 {
196 
197 	switch (meta->type) {
198 	case SII_T_RAID0:
199 	case SII_T_RAID5:
200 	case SII_T_CONCAT:
201 		return (meta->raid0_disks);
202 	case SII_T_RAID1:
203 		return (meta->raid1_disks);
204 	case SII_T_RAID01:
205 		return (meta->raid0_disks * meta->raid1_disks);
206 	case SII_T_SPARE:
207 	case SII_T_JBOD:
208 		return (1);
209 	}
210 	return (0);
211 }
212 
213 static int
214 sii_meta_disk_pos(struct sii_raid_conf *meta, struct sii_raid_conf *pdmeta)
215 {
216 
217 	if (pdmeta->type == SII_T_SPARE)
218 		return (-3);
219 
220 	if (memcmp(&meta->timestamp, &pdmeta->timestamp, 6) != 0)
221 		return (-1);
222 
223 	switch (pdmeta->type) {
224 	case SII_T_RAID0:
225 	case SII_T_RAID1:
226 	case SII_T_RAID5:
227 	case SII_T_CONCAT:
228 		return (pdmeta->disk_number);
229 	case SII_T_RAID01:
230 		return (pdmeta->raid1_ident * pdmeta->raid1_disks +
231 		    pdmeta->raid0_ident);
232 	case SII_T_JBOD:
233 		return (0);
234 	}
235 	return (-1);
236 }
237 
238 static void
239 sii_meta_get_name(struct sii_raid_conf *meta, char *buf)
240 {
241 	int i;
242 
243 	strncpy(buf, meta->name, 16);
244 	buf[16] = 0;
245 	for (i = 15; i >= 0; i--) {
246 		if (buf[i] > 0x20)
247 			break;
248 		buf[i] = 0;
249 	}
250 }
251 
252 static void
253 sii_meta_put_name(struct sii_raid_conf *meta, char *buf)
254 {
255 
256 	memset(meta->name, 0x20, 16);
257 	memcpy(meta->name, buf, MIN(strlen(buf), 16));
258 }
259 
260 static struct sii_raid_conf *
261 sii_meta_read(struct g_consumer *cp)
262 {
263 	struct g_provider *pp;
264 	struct sii_raid_conf *meta;
265 	char *buf;
266 	int error, i;
267 	uint16_t checksum, *ptr;
268 
269 	pp = cp->provider;
270 
271 	/* Read the anchor sector. */
272 	buf = g_read_data(cp,
273 	    pp->mediasize - pp->sectorsize, pp->sectorsize, &error);
274 	if (buf == NULL) {
275 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
276 		    pp->name, error);
277 		return (NULL);
278 	}
279 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
280 	memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
281 	g_free(buf);
282 
283 	/* Check vendor ID. */
284 	if (meta->vendor_id != 0x1095) {
285 		G_RAID_DEBUG(1, "SiI vendor ID check failed on %s (0x%04x)",
286 		    pp->name, meta->vendor_id);
287 		free(meta, M_MD_SII);
288 		return (NULL);
289 	}
290 
291 	/* Check metadata major version. */
292 	if (meta->version_major != 2) {
293 		G_RAID_DEBUG(1, "SiI version check failed on %s (%d.%d)",
294 		    pp->name, meta->version_major, meta->version_minor);
295 		free(meta, M_MD_SII);
296 		return (NULL);
297 	}
298 
299 	/* Check metadata checksum. */
300 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i <= 159; i++)
301 		checksum += *ptr++;
302 	if (checksum != 0) {
303 		G_RAID_DEBUG(1, "SiI checksum check failed on %s", pp->name);
304 		free(meta, M_MD_SII);
305 		return (NULL);
306 	}
307 
308 	/* Check raid type. */
309 	if (meta->type != SII_T_RAID0 && meta->type != SII_T_RAID1 &&
310 	    meta->type != SII_T_RAID01 && meta->type != SII_T_SPARE &&
311 	    meta->type != SII_T_RAID5 && meta->type != SII_T_CONCAT &&
312 	    meta->type != SII_T_JBOD) {
313 		G_RAID_DEBUG(1, "SiI unknown RAID level on %s (0x%02x)",
314 		    pp->name, meta->type);
315 		free(meta, M_MD_SII);
316 		return (NULL);
317 	}
318 
319 	return (meta);
320 }
321 
322 static int
323 sii_meta_write(struct g_consumer *cp, struct sii_raid_conf *meta)
324 {
325 	struct g_provider *pp;
326 	char *buf;
327 	int error, i;
328 	uint16_t checksum, *ptr;
329 
330 	pp = cp->provider;
331 
332 	/* Recalculate checksum for case if metadata were changed. */
333 	meta->checksum = 0;
334 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 159; i++)
335 		checksum += *ptr++;
336 	meta->checksum -= checksum;
337 
338 	/* Create and fill buffer. */
339 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
340 	memcpy(buf, meta, sizeof(*meta));
341 
342 	/* Write 4 copies of metadata. */
343 	for (i = 0; i < 4; i++) {
344 		error = g_write_data(cp,
345 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
346 		    buf, pp->sectorsize);
347 		if (error != 0) {
348 			G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
349 			    pp->name, error);
350 			break;
351 		}
352 	}
353 
354 	free(buf, M_MD_SII);
355 	return (error);
356 }
357 
358 static int
359 sii_meta_erase(struct g_consumer *cp)
360 {
361 	struct g_provider *pp;
362 	char *buf;
363 	int error, i;
364 
365 	pp = cp->provider;
366 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
367 	/* Write 4 copies of metadata. */
368 	for (i = 0; i < 4; i++) {
369 		error = g_write_data(cp,
370 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
371 		    buf, pp->sectorsize);
372 		if (error != 0) {
373 			G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
374 			    pp->name, error);
375 		}
376 	}
377 	free(buf, M_MD_SII);
378 	return (error);
379 }
380 
381 static int
382 sii_meta_write_spare(struct g_consumer *cp)
383 {
384 	struct sii_raid_conf *meta;
385 	int error;
386 
387 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
388 	meta->total_sectors = cp->provider->mediasize /
389 	    cp->provider->sectorsize - 0x800;
390 	meta->vendor_id = 0x1095;
391 	meta->version_minor = 0;
392 	meta->version_major = 2;
393 	meta->timestamp[0] = arc4random();
394 	meta->timestamp[1] = arc4random();
395 	meta->timestamp[2] = arc4random();
396 	meta->timestamp[3] = arc4random();
397 	meta->timestamp[4] = arc4random();
398 	meta->timestamp[5] = arc4random();
399 	meta->type = SII_T_SPARE;
400 	meta->generation = 1;
401 	meta->raid1_ident = 0xff;
402 	meta->raid_location = arc4random();
403 	error = sii_meta_write(cp, meta);
404 	free(meta, M_MD_SII);
405 	return (error);
406 }
407 
408 static struct g_raid_disk *
409 g_raid_md_sii_get_disk(struct g_raid_softc *sc, int id)
410 {
411 	struct g_raid_disk	*disk;
412 	struct g_raid_md_sii_perdisk *pd;
413 
414 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
415 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
416 		if (pd->pd_disk_pos == id)
417 			break;
418 	}
419 	return (disk);
420 }
421 
422 static int
423 g_raid_md_sii_supported(int level, int qual, int disks, int force)
424 {
425 
426 	if (disks > 8)
427 		return (0);
428 	switch (level) {
429 	case G_RAID_VOLUME_RL_RAID0:
430 		if (disks < 1)
431 			return (0);
432 		if (!force && (disks < 2 || disks > 6))
433 			return (0);
434 		break;
435 	case G_RAID_VOLUME_RL_RAID1:
436 		if (disks < 1)
437 			return (0);
438 		if (!force && (disks != 2))
439 			return (0);
440 		break;
441 	case G_RAID_VOLUME_RL_RAID1E:
442 		if (disks < 2)
443 			return (0);
444 		if (disks % 2 != 0)
445 			return (0);
446 		if (!force && (disks < 4))
447 			return (0);
448 		break;
449 	case G_RAID_VOLUME_RL_SINGLE:
450 		if (disks != 1)
451 			return (0);
452 		break;
453 	case G_RAID_VOLUME_RL_CONCAT:
454 		if (disks < 2)
455 			return (0);
456 		break;
457 	case G_RAID_VOLUME_RL_RAID5:
458 		if (disks < 3)
459 			return (0);
460 		if (qual != G_RAID_VOLUME_RLQ_R5LS)
461 			return (0);
462 		break;
463 	default:
464 		return (0);
465 	}
466 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
467 		return (0);
468 	return (1);
469 }
470 
471 static int
472 g_raid_md_sii_start_disk(struct g_raid_disk *disk)
473 {
474 	struct g_raid_softc *sc;
475 	struct g_raid_subdisk *sd, *tmpsd;
476 	struct g_raid_disk *olddisk, *tmpdisk;
477 	struct g_raid_md_object *md;
478 	struct g_raid_md_sii_object *mdi;
479 	struct g_raid_md_sii_perdisk *pd, *oldpd;
480 	struct sii_raid_conf *meta;
481 	int disk_pos, resurrection = 0;
482 
483 	sc = disk->d_softc;
484 	md = sc->sc_md;
485 	mdi = (struct g_raid_md_sii_object *)md;
486 	meta = mdi->mdio_meta;
487 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
488 	olddisk = NULL;
489 
490 	/* Find disk position in metadata by it's serial. */
491 	if (pd->pd_meta != NULL)
492 		disk_pos = sii_meta_disk_pos(meta, pd->pd_meta);
493 	else
494 		disk_pos = -3;
495 	if (disk_pos < 0) {
496 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
497 		/* If we are in the start process, that's all for now. */
498 		if (!mdi->mdio_started)
499 			goto nofit;
500 		/*
501 		 * If we have already started - try to get use of the disk.
502 		 * Try to replace OFFLINE disks first, then FAILED.
503 		 */
504 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
505 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
506 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
507 				continue;
508 			/* Make sure this disk is big enough. */
509 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
510 				if (sd->sd_offset + sd->sd_size + 512 >
511 				    pd->pd_disk_size) {
512 					G_RAID_DEBUG1(1, sc,
513 					    "Disk too small (%ju < %ju)",
514 					    pd->pd_disk_size,
515 					    sd->sd_offset + sd->sd_size + 512);
516 					break;
517 				}
518 			}
519 			if (sd != NULL)
520 				continue;
521 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
522 				olddisk = tmpdisk;
523 				break;
524 			} else if (olddisk == NULL)
525 				olddisk = tmpdisk;
526 		}
527 		if (olddisk == NULL) {
528 nofit:
529 			if (disk_pos == -3 || pd->pd_disk_pos == -3) {
530 				g_raid_change_disk_state(disk,
531 				    G_RAID_DISK_S_SPARE);
532 				return (1);
533 			} else {
534 				g_raid_change_disk_state(disk,
535 				    G_RAID_DISK_S_STALE);
536 				return (0);
537 			}
538 		}
539 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
540 		disk_pos = oldpd->pd_disk_pos;
541 		resurrection = 1;
542 	}
543 
544 	if (olddisk == NULL) {
545 		/* Find placeholder by position. */
546 		olddisk = g_raid_md_sii_get_disk(sc, disk_pos);
547 		if (olddisk == NULL)
548 			panic("No disk at position %d!", disk_pos);
549 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
550 			G_RAID_DEBUG1(1, sc, "More then one disk for pos %d",
551 			    disk_pos);
552 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
553 			return (0);
554 		}
555 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
556 	}
557 
558 	/* Replace failed disk or placeholder with new disk. */
559 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
560 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
561 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
562 		sd->sd_disk = disk;
563 	}
564 	oldpd->pd_disk_pos = -2;
565 	pd->pd_disk_pos = disk_pos;
566 
567 	/* If it was placeholder -- destroy it. */
568 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
569 		g_raid_destroy_disk(olddisk);
570 	} else {
571 		/* Otherwise, make it STALE_FAILED. */
572 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
573 	}
574 
575 	/* Welcome the new disk. */
576 	if (resurrection)
577 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
578 	else if (pd->pd_meta->disk_status == SII_S_CURRENT ||
579 	    pd->pd_meta->disk_status == SII_S_REBUILD)
580 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
581 	else
582 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
583 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
584 
585 		/*
586 		 * Different disks may have different sizes,
587 		 * in concat mode. Update from real disk size.
588 		 */
589 		if (meta->type == SII_T_CONCAT || meta->type == SII_T_JBOD)
590 			sd->sd_size = pd->pd_disk_size - 0x800 * 512;
591 
592 		if (resurrection) {
593 			/* New or ex-spare disk. */
594 			g_raid_change_subdisk_state(sd,
595 			    G_RAID_SUBDISK_S_NEW);
596 		} else if (pd->pd_meta->disk_status == SII_S_REBUILD) {
597 			/* Rebuilding disk. */
598 			g_raid_change_subdisk_state(sd,
599 			    G_RAID_SUBDISK_S_REBUILD);
600 			if (pd->pd_meta->generation == meta->generation)
601 				sd->sd_rebuild_pos = pd->pd_meta->rebuild_lba * 512;
602 			else
603 				sd->sd_rebuild_pos = 0;
604 		} else if (pd->pd_meta->disk_status == SII_S_CURRENT) {
605 			if (pd->pd_meta->raid_status == SII_S_ONLINE ||
606 			    pd->pd_meta->generation != meta->generation) {
607 				/* Dirty or resyncing disk. */
608 				g_raid_change_subdisk_state(sd,
609 				    G_RAID_SUBDISK_S_STALE);
610 			} else {
611 				/* Up to date disk. */
612 				g_raid_change_subdisk_state(sd,
613 				    G_RAID_SUBDISK_S_ACTIVE);
614 			}
615 		} else {
616 			g_raid_change_subdisk_state(sd,
617 			    G_RAID_SUBDISK_S_FAILED);
618 		}
619 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
620 		    G_RAID_EVENT_SUBDISK);
621 	}
622 
623 	/* Update status of our need for spare. */
624 	if (mdi->mdio_started) {
625 		mdi->mdio_incomplete =
626 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
627 		     mdi->mdio_total_disks);
628 	}
629 
630 	return (resurrection);
631 }
632 
633 static void
634 g_disk_md_sii_retaste(void *arg, int pending)
635 {
636 
637 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
638 	g_retaste(&g_raid_class);
639 	free(arg, M_MD_SII);
640 }
641 
642 static void
643 g_raid_md_sii_refill(struct g_raid_softc *sc)
644 {
645 	struct g_raid_md_object *md;
646 	struct g_raid_md_sii_object *mdi;
647 	struct g_raid_disk *disk;
648 	struct task *task;
649 	int update, na;
650 
651 	md = sc->sc_md;
652 	mdi = (struct g_raid_md_sii_object *)md;
653 	update = 0;
654 	do {
655 		/* Make sure we miss anything. */
656 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
657 		if (na == mdi->mdio_total_disks)
658 			break;
659 
660 		G_RAID_DEBUG1(1, md->mdo_softc,
661 		    "Array is not complete (%d of %d), "
662 		    "trying to refill.", na, mdi->mdio_total_disks);
663 
664 		/* Try to get use some of STALE disks. */
665 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
666 			if (disk->d_state == G_RAID_DISK_S_STALE) {
667 				update += g_raid_md_sii_start_disk(disk);
668 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
669 					break;
670 			}
671 		}
672 		if (disk != NULL)
673 			continue;
674 
675 		/* Try to get use some of SPARE disks. */
676 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
677 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
678 				update += g_raid_md_sii_start_disk(disk);
679 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
680 					break;
681 			}
682 		}
683 	} while (disk != NULL);
684 
685 	/* Write new metadata if we changed something. */
686 	if (update)
687 		g_raid_md_write_sii(md, NULL, NULL, NULL);
688 
689 	/* Update status of our need for spare. */
690 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
691 	    mdi->mdio_total_disks);
692 
693 	/* Request retaste hoping to find spare. */
694 	if (mdi->mdio_incomplete) {
695 		task = malloc(sizeof(struct task),
696 		    M_MD_SII, M_WAITOK | M_ZERO);
697 		TASK_INIT(task, 0, g_disk_md_sii_retaste, task);
698 		taskqueue_enqueue(taskqueue_swi, task);
699 	}
700 }
701 
702 static void
703 g_raid_md_sii_start(struct g_raid_softc *sc)
704 {
705 	struct g_raid_md_object *md;
706 	struct g_raid_md_sii_object *mdi;
707 	struct g_raid_md_sii_perdisk *pd;
708 	struct sii_raid_conf *meta;
709 	struct g_raid_volume *vol;
710 	struct g_raid_subdisk *sd;
711 	struct g_raid_disk *disk, *best;
712 	off_t size;
713 	int j, disk_pos;
714 	uint32_t gendiff, bestgendiff;
715 	char buf[17];
716 
717 	md = sc->sc_md;
718 	mdi = (struct g_raid_md_sii_object *)md;
719 	meta = mdi->mdio_meta;
720 
721 	/* Create volumes and subdisks. */
722 	sii_meta_get_name(meta, buf);
723 	vol = g_raid_create_volume(sc, buf, -1);
724 	vol->v_mediasize = (off_t)meta->total_sectors * 512;
725 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
726 	if (meta->type == SII_T_RAID0) {
727 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
728 		size = vol->v_mediasize / mdi->mdio_total_disks;
729 	} else if (meta->type == SII_T_RAID1) {
730 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
731 		size = vol->v_mediasize;
732 	} else if (meta->type == SII_T_RAID01) {
733 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
734 		size = vol->v_mediasize / (mdi->mdio_total_disks / 2);
735 	} else if (meta->type == SII_T_CONCAT) {
736 		if (mdi->mdio_total_disks == 1)
737 			vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
738 		else
739 			vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
740 		size = 0;
741 	} else if (meta->type == SII_T_RAID5) {
742 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
743 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
744 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
745 	} else if (meta->type == SII_T_JBOD) {
746 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
747 		size = 0;
748 	} else {
749 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
750 		size = 0;
751 	}
752 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
753 	vol->v_disks_count = mdi->mdio_total_disks;
754 	vol->v_sectorsize = 512; //ZZZ
755 	for (j = 0; j < vol->v_disks_count; j++) {
756 		sd = &vol->v_subdisks[j];
757 		sd->sd_offset = 0;
758 		sd->sd_size = size;
759 	}
760 	g_raid_start_volume(vol);
761 
762 	/* Create disk placeholders to store data for later writing. */
763 	for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
764 		pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
765 		pd->pd_disk_pos = disk_pos;
766 		disk = g_raid_create_disk(sc);
767 		disk->d_md_data = (void *)pd;
768 		disk->d_state = G_RAID_DISK_S_OFFLINE;
769 		sd = &vol->v_subdisks[disk_pos];
770 		sd->sd_disk = disk;
771 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
772 	}
773 
774 	/*
775 	 * Make all disks found till the moment take their places
776 	 * in order of their generation numbers.
777 	 */
778 	do {
779 		best = NULL;
780 		bestgendiff = 0xffffffff;
781 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
782 			if (disk->d_state != G_RAID_DISK_S_NONE)
783 				continue;
784 			pd = disk->d_md_data;
785 			if (pd->pd_meta == NULL)
786 				gendiff = 0xfffffffe;
787 			else
788 				gendiff = meta->generation -
789 				    pd->pd_meta->generation;
790 			if (gendiff < bestgendiff) {
791 				best = disk;
792 				bestgendiff = gendiff;
793 			}
794 		}
795 		if (best != NULL)
796 			g_raid_md_sii_start_disk(best);
797 	} while (best != NULL);
798 
799 	mdi->mdio_started = 1;
800 	G_RAID_DEBUG1(0, sc, "Array started.");
801 	g_raid_md_write_sii(md, NULL, NULL, NULL);
802 
803 	/* Pickup any STALE/SPARE disks to refill array if needed. */
804 	g_raid_md_sii_refill(sc);
805 
806 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
807 
808 	callout_stop(&mdi->mdio_start_co);
809 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
810 	root_mount_rel(mdi->mdio_rootmount);
811 	mdi->mdio_rootmount = NULL;
812 }
813 
814 static void
815 g_raid_md_sii_new_disk(struct g_raid_disk *disk)
816 {
817 	struct g_raid_softc *sc;
818 	struct g_raid_md_object *md;
819 	struct g_raid_md_sii_object *mdi;
820 	struct sii_raid_conf *pdmeta;
821 	struct g_raid_md_sii_perdisk *pd;
822 
823 	sc = disk->d_softc;
824 	md = sc->sc_md;
825 	mdi = (struct g_raid_md_sii_object *)md;
826 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
827 	pdmeta = pd->pd_meta;
828 
829 	if (mdi->mdio_started) {
830 		if (g_raid_md_sii_start_disk(disk))
831 			g_raid_md_write_sii(md, NULL, NULL, NULL);
832 	} else {
833 		if (mdi->mdio_meta == NULL ||
834 		    ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) {
835 			G_RAID_DEBUG1(1, sc, "Newer disk");
836 			if (mdi->mdio_meta != NULL)
837 				free(mdi->mdio_meta, M_MD_SII);
838 			mdi->mdio_meta = sii_meta_copy(pdmeta);
839 			mdi->mdio_generation = mdi->mdio_meta->generation;
840 			mdi->mdio_total_disks = sii_meta_total_disks(pdmeta);
841 			mdi->mdio_disks_present = 1;
842 		} else if (pdmeta->generation == mdi->mdio_generation) {
843 			mdi->mdio_disks_present++;
844 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
845 			    mdi->mdio_disks_present,
846 			    mdi->mdio_total_disks);
847 		} else {
848 			G_RAID_DEBUG1(1, sc, "Older disk");
849 		}
850 
851 		/* If we collected all needed disks - start array. */
852 		if (mdi->mdio_disks_present == mdi->mdio_total_disks)
853 			g_raid_md_sii_start(sc);
854 	}
855 }
856 
857 static void
858 g_raid_sii_go(void *arg)
859 {
860 	struct g_raid_softc *sc;
861 	struct g_raid_md_object *md;
862 	struct g_raid_md_sii_object *mdi;
863 
864 	sc = arg;
865 	md = sc->sc_md;
866 	mdi = (struct g_raid_md_sii_object *)md;
867 	if (!mdi->mdio_started) {
868 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
869 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
870 	}
871 }
872 
873 static int
874 g_raid_md_create_sii(struct g_raid_md_object *md, struct g_class *mp,
875     struct g_geom **gp)
876 {
877 	struct g_raid_softc *sc;
878 	struct g_raid_md_sii_object *mdi;
879 	char name[32];
880 
881 	mdi = (struct g_raid_md_sii_object *)md;
882 	mdi->mdio_timestamp[5] = arc4random();
883 	mdi->mdio_timestamp[4] = arc4random();
884 	mdi->mdio_timestamp[3] = arc4random();
885 	mdi->mdio_timestamp[2] = arc4random();
886 	mdi->mdio_timestamp[1] = arc4random();
887 	mdi->mdio_timestamp[0] = arc4random();
888 	mdi->mdio_location = arc4random();
889 	mdi->mdio_generation = 0;
890 	snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x",
891 	    mdi->mdio_timestamp[5], mdi->mdio_timestamp[4],
892 	    mdi->mdio_timestamp[3], mdi->mdio_timestamp[2],
893 	    mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]);
894 	sc = g_raid_create_node(mp, name, md);
895 	if (sc == NULL)
896 		return (G_RAID_MD_TASTE_FAIL);
897 	md->mdo_softc = sc;
898 	*gp = sc->sc_geom;
899 	return (G_RAID_MD_TASTE_NEW);
900 }
901 
902 static int
903 g_raid_md_taste_sii(struct g_raid_md_object *md, struct g_class *mp,
904                               struct g_consumer *cp, struct g_geom **gp)
905 {
906 	struct g_consumer *rcp;
907 	struct g_provider *pp;
908 	struct g_raid_md_sii_object *mdi, *mdi1;
909 	struct g_raid_softc *sc;
910 	struct g_raid_disk *disk;
911 	struct sii_raid_conf *meta;
912 	struct g_raid_md_sii_perdisk *pd;
913 	struct g_geom *geom;
914 	int error, disk_pos, result, spare, len;
915 	char name[32];
916 	uint16_t vendor;
917 
918 	G_RAID_DEBUG(1, "Tasting SiI on %s", cp->provider->name);
919 	mdi = (struct g_raid_md_sii_object *)md;
920 	pp = cp->provider;
921 
922 	/* Read metadata from device. */
923 	meta = NULL;
924 	vendor = 0xffff;
925 	if (g_access(cp, 1, 0, 0) != 0)
926 		return (G_RAID_MD_TASTE_FAIL);
927 	g_topology_unlock();
928 	len = 2;
929 	if (pp->geom->rank == 1)
930 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
931 	meta = sii_meta_read(cp);
932 	g_topology_lock();
933 	g_access(cp, -1, 0, 0);
934 	if (meta == NULL) {
935 		if (g_raid_aggressive_spare) {
936 			if (vendor == 0x1095) {
937 				G_RAID_DEBUG(1,
938 				    "No SiI metadata, forcing spare.");
939 				spare = 2;
940 				goto search;
941 			} else {
942 				G_RAID_DEBUG(1,
943 				    "SiI vendor mismatch 0x%04x != 0x1095",
944 				    vendor);
945 			}
946 		}
947 		return (G_RAID_MD_TASTE_FAIL);
948 	}
949 
950 	/* Check this disk position in obtained metadata. */
951 	disk_pos = sii_meta_disk_pos(meta, meta);
952 	if (disk_pos == -1) {
953 		G_RAID_DEBUG(1, "SiI disk position not found");
954 		goto fail1;
955 	}
956 
957 	/* Metadata valid. Print it. */
958 	g_raid_md_sii_print(meta);
959 	G_RAID_DEBUG(1, "SiI disk position %d", disk_pos);
960 	spare = (meta->type == SII_T_SPARE) ? 1 : 0;
961 
962 search:
963 	/* Search for matching node. */
964 	sc = NULL;
965 	mdi1 = NULL;
966 	LIST_FOREACH(geom, &mp->geom, geom) {
967 		sc = geom->softc;
968 		if (sc == NULL)
969 			continue;
970 		if (sc->sc_stopping != 0)
971 			continue;
972 		if (sc->sc_md->mdo_class != md->mdo_class)
973 			continue;
974 		mdi1 = (struct g_raid_md_sii_object *)sc->sc_md;
975 		if (spare) {
976 			if (mdi1->mdio_incomplete)
977 				break;
978 		} else {
979 			if (mdi1->mdio_location == meta->raid_location &&
980 			    memcmp(&mdi1->mdio_timestamp,
981 			     &meta->timestamp, 6) == 0)
982 				break;
983 		}
984 	}
985 
986 	/* Found matching node. */
987 	if (geom != NULL) {
988 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
989 		result = G_RAID_MD_TASTE_EXISTING;
990 
991 	} else if (spare) { /* Not found needy node -- left for later. */
992 		G_RAID_DEBUG(1, "Spare is not needed at this time");
993 		goto fail1;
994 
995 	} else { /* Not found matching node -- create one. */
996 		result = G_RAID_MD_TASTE_NEW;
997 		memcpy(&mdi->mdio_timestamp, &meta->timestamp, 6);
998 		mdi->mdio_location = meta->raid_location;
999 		snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x",
1000 		    mdi->mdio_timestamp[5], mdi->mdio_timestamp[4],
1001 		    mdi->mdio_timestamp[3], mdi->mdio_timestamp[2],
1002 		    mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]);
1003 		sc = g_raid_create_node(mp, name, md);
1004 		md->mdo_softc = sc;
1005 		geom = sc->sc_geom;
1006 		callout_init(&mdi->mdio_start_co, 1);
1007 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
1008 		    g_raid_sii_go, sc);
1009 		mdi->mdio_rootmount = root_mount_hold("GRAID-SiI");
1010 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
1011 	}
1012 
1013 	rcp = g_new_consumer(geom);
1014 	g_attach(rcp, pp);
1015 	if (g_access(rcp, 1, 1, 1) != 0)
1016 		; //goto fail1;
1017 
1018 	g_topology_unlock();
1019 	sx_xlock(&sc->sc_lock);
1020 
1021 	pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
1022 	pd->pd_meta = meta;
1023 	if (spare == 2) {
1024 		pd->pd_disk_pos = -3;
1025 	} else {
1026 		pd->pd_disk_pos = -1;
1027 	}
1028 	pd->pd_disk_size = pp->mediasize;
1029 	disk = g_raid_create_disk(sc);
1030 	disk->d_md_data = (void *)pd;
1031 	disk->d_consumer = rcp;
1032 	rcp->private = disk;
1033 
1034 	/* Read kernel dumping information. */
1035 	disk->d_kd.offset = 0;
1036 	disk->d_kd.length = OFF_MAX;
1037 	len = sizeof(disk->d_kd);
1038 	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
1039 	if (disk->d_kd.di.dumper == NULL)
1040 		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
1041 		    rcp->provider->name, error);
1042 
1043 	g_raid_md_sii_new_disk(disk);
1044 
1045 	sx_xunlock(&sc->sc_lock);
1046 	g_topology_lock();
1047 	*gp = geom;
1048 	return (result);
1049 fail1:
1050 	free(meta, M_MD_SII);
1051 	return (G_RAID_MD_TASTE_FAIL);
1052 }
1053 
1054 static int
1055 g_raid_md_event_sii(struct g_raid_md_object *md,
1056     struct g_raid_disk *disk, u_int event)
1057 {
1058 	struct g_raid_softc *sc;
1059 	struct g_raid_subdisk *sd;
1060 	struct g_raid_md_sii_object *mdi;
1061 	struct g_raid_md_sii_perdisk *pd;
1062 
1063 	sc = md->mdo_softc;
1064 	mdi = (struct g_raid_md_sii_object *)md;
1065 	if (disk == NULL) {
1066 		switch (event) {
1067 		case G_RAID_NODE_E_START:
1068 			if (!mdi->mdio_started)
1069 				g_raid_md_sii_start(sc);
1070 			return (0);
1071 		}
1072 		return (-1);
1073 	}
1074 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1075 	switch (event) {
1076 	case G_RAID_DISK_E_DISCONNECTED:
1077 		/* If disk was assigned, just update statuses. */
1078 		if (pd->pd_disk_pos >= 0) {
1079 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1080 			if (disk->d_consumer) {
1081 				g_raid_kill_consumer(sc, disk->d_consumer);
1082 				disk->d_consumer = NULL;
1083 			}
1084 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1085 				g_raid_change_subdisk_state(sd,
1086 				    G_RAID_SUBDISK_S_NONE);
1087 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1088 				    G_RAID_EVENT_SUBDISK);
1089 			}
1090 		} else {
1091 			/* Otherwise -- delete. */
1092 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1093 			g_raid_destroy_disk(disk);
1094 		}
1095 
1096 		/* Write updated metadata to all disks. */
1097 		g_raid_md_write_sii(md, NULL, NULL, NULL);
1098 
1099 		/* Check if anything left except placeholders. */
1100 		if (g_raid_ndisks(sc, -1) ==
1101 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1102 			g_raid_destroy_node(sc, 0);
1103 		else
1104 			g_raid_md_sii_refill(sc);
1105 		return (0);
1106 	}
1107 	return (-2);
1108 }
1109 
1110 static int
1111 g_raid_md_ctl_sii(struct g_raid_md_object *md,
1112     struct gctl_req *req)
1113 {
1114 	struct g_raid_softc *sc;
1115 	struct g_raid_volume *vol;
1116 	struct g_raid_subdisk *sd;
1117 	struct g_raid_disk *disk;
1118 	struct g_raid_md_sii_object *mdi;
1119 	struct g_raid_md_sii_perdisk *pd;
1120 	struct g_consumer *cp;
1121 	struct g_provider *pp;
1122 	char arg[16];
1123 	const char *verb, *volname, *levelname, *diskname;
1124 	int *nargs, *force;
1125 	off_t size, sectorsize, strip;
1126 	intmax_t *sizearg, *striparg;
1127 	int numdisks, i, len, level, qual, update;
1128 	int error;
1129 
1130 	sc = md->mdo_softc;
1131 	mdi = (struct g_raid_md_sii_object *)md;
1132 	verb = gctl_get_param(req, "verb", NULL);
1133 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1134 	error = 0;
1135 	if (strcmp(verb, "label") == 0) {
1136 
1137 		if (*nargs < 4) {
1138 			gctl_error(req, "Invalid number of arguments.");
1139 			return (-1);
1140 		}
1141 		volname = gctl_get_asciiparam(req, "arg1");
1142 		if (volname == NULL) {
1143 			gctl_error(req, "No volume name.");
1144 			return (-2);
1145 		}
1146 		levelname = gctl_get_asciiparam(req, "arg2");
1147 		if (levelname == NULL) {
1148 			gctl_error(req, "No RAID level.");
1149 			return (-3);
1150 		}
1151 		if (strcasecmp(levelname, "RAID5") == 0)
1152 			levelname = "RAID5-LS";
1153 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1154 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1155 			return (-4);
1156 		}
1157 		numdisks = *nargs - 3;
1158 		force = gctl_get_paraml(req, "force", sizeof(*force));
1159 		if (!g_raid_md_sii_supported(level, qual, numdisks,
1160 		    force ? *force : 0)) {
1161 			gctl_error(req, "Unsupported RAID level "
1162 			    "(0x%02x/0x%02x), or number of disks (%d).",
1163 			    level, qual, numdisks);
1164 			return (-5);
1165 		}
1166 
1167 		/* Search for disks, connect them and probe. */
1168 		size = 0x7fffffffffffffffllu;
1169 		sectorsize = 0;
1170 		for (i = 0; i < numdisks; i++) {
1171 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1172 			diskname = gctl_get_asciiparam(req, arg);
1173 			if (diskname == NULL) {
1174 				gctl_error(req, "No disk name (%s).", arg);
1175 				error = -6;
1176 				break;
1177 			}
1178 			if (strcmp(diskname, "NONE") == 0) {
1179 				cp = NULL;
1180 				pp = NULL;
1181 			} else {
1182 				g_topology_lock();
1183 				cp = g_raid_open_consumer(sc, diskname);
1184 				if (cp == NULL) {
1185 					gctl_error(req, "Can't open '%s'.",
1186 					    diskname);
1187 					g_topology_unlock();
1188 					error = -7;
1189 					break;
1190 				}
1191 				pp = cp->provider;
1192 			}
1193 			pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
1194 			pd->pd_disk_pos = i;
1195 			disk = g_raid_create_disk(sc);
1196 			disk->d_md_data = (void *)pd;
1197 			disk->d_consumer = cp;
1198 			if (cp == NULL)
1199 				continue;
1200 			cp->private = disk;
1201 			g_topology_unlock();
1202 
1203 			/* Read kernel dumping information. */
1204 			disk->d_kd.offset = 0;
1205 			disk->d_kd.length = OFF_MAX;
1206 			len = sizeof(disk->d_kd);
1207 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1208 			if (disk->d_kd.di.dumper == NULL)
1209 				G_RAID_DEBUG1(2, sc,
1210 				    "Dumping not supported by %s.",
1211 				    cp->provider->name);
1212 
1213 			pd->pd_disk_size = pp->mediasize;
1214 			if (size > pp->mediasize)
1215 				size = pp->mediasize;
1216 			if (sectorsize < pp->sectorsize)
1217 				sectorsize = pp->sectorsize;
1218 		}
1219 		if (error != 0)
1220 			return (error);
1221 
1222 		if (sectorsize <= 0) {
1223 			gctl_error(req, "Can't get sector size.");
1224 			return (-8);
1225 		}
1226 
1227 		/* Reserve space for metadata. */
1228 		size -= 0x800 * sectorsize;
1229 
1230 		/* Handle size argument. */
1231 		len = sizeof(*sizearg);
1232 		sizearg = gctl_get_param(req, "size", &len);
1233 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1234 		    *sizearg > 0) {
1235 			if (*sizearg > size) {
1236 				gctl_error(req, "Size too big %lld > %lld.",
1237 				    (long long)*sizearg, (long long)size);
1238 				return (-9);
1239 			}
1240 			size = *sizearg;
1241 		}
1242 
1243 		/* Handle strip argument. */
1244 		strip = 131072;
1245 		len = sizeof(*striparg);
1246 		striparg = gctl_get_param(req, "strip", &len);
1247 		if (striparg != NULL && len == sizeof(*striparg) &&
1248 		    *striparg > 0) {
1249 			if (*striparg < sectorsize) {
1250 				gctl_error(req, "Strip size too small.");
1251 				return (-10);
1252 			}
1253 			if (*striparg % sectorsize != 0) {
1254 				gctl_error(req, "Incorrect strip size.");
1255 				return (-11);
1256 			}
1257 			if (strip > 65535 * sectorsize) {
1258 				gctl_error(req, "Strip size too big.");
1259 				return (-12);
1260 			}
1261 			strip = *striparg;
1262 		}
1263 
1264 		/* Round size down to strip or sector. */
1265 		if (level == G_RAID_VOLUME_RL_RAID1)
1266 			size -= (size % sectorsize);
1267 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1268 		    (numdisks & 1) != 0)
1269 			size -= (size % (2 * strip));
1270 		else
1271 			size -= (size % strip);
1272 		if (size <= 0) {
1273 			gctl_error(req, "Size too small.");
1274 			return (-13);
1275 		}
1276 		if (size > 0xffffffffffffllu * sectorsize) {
1277 			gctl_error(req, "Size too big.");
1278 			return (-14);
1279 		}
1280 
1281 		/* We have all we need, create things: volume, ... */
1282 		mdi->mdio_total_disks = numdisks;
1283 		mdi->mdio_started = 1;
1284 		vol = g_raid_create_volume(sc, volname, -1);
1285 		vol->v_md_data = (void *)(intptr_t)0;
1286 		vol->v_raid_level = level;
1287 		vol->v_raid_level_qualifier = qual;
1288 		vol->v_strip_size = strip;
1289 		vol->v_disks_count = numdisks;
1290 		if (level == G_RAID_VOLUME_RL_RAID0 ||
1291 		    level == G_RAID_VOLUME_RL_CONCAT ||
1292 		    level == G_RAID_VOLUME_RL_SINGLE)
1293 			vol->v_mediasize = size * numdisks;
1294 		else if (level == G_RAID_VOLUME_RL_RAID1)
1295 			vol->v_mediasize = size;
1296 		else if (level == G_RAID_VOLUME_RL_RAID5)
1297 			vol->v_mediasize = size * (numdisks - 1);
1298 		else { /* RAID1E */
1299 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1300 			    strip;
1301 		}
1302 		vol->v_sectorsize = sectorsize;
1303 		g_raid_start_volume(vol);
1304 
1305 		/* , and subdisks. */
1306 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1307 			pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1308 			sd = &vol->v_subdisks[pd->pd_disk_pos];
1309 			sd->sd_disk = disk;
1310 			sd->sd_offset = 0;
1311 			sd->sd_size = size;
1312 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1313 			if (sd->sd_disk->d_consumer != NULL) {
1314 				g_raid_change_disk_state(disk,
1315 				    G_RAID_DISK_S_ACTIVE);
1316 				g_raid_change_subdisk_state(sd,
1317 				    G_RAID_SUBDISK_S_ACTIVE);
1318 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1319 				    G_RAID_EVENT_SUBDISK);
1320 			} else {
1321 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1322 			}
1323 		}
1324 
1325 		/* Write metadata based on created entities. */
1326 		G_RAID_DEBUG1(0, sc, "Array started.");
1327 		g_raid_md_write_sii(md, NULL, NULL, NULL);
1328 
1329 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1330 		g_raid_md_sii_refill(sc);
1331 
1332 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1333 		    G_RAID_EVENT_VOLUME);
1334 		return (0);
1335 	}
1336 	if (strcmp(verb, "delete") == 0) {
1337 
1338 		/* Check if some volume is still open. */
1339 		force = gctl_get_paraml(req, "force", sizeof(*force));
1340 		if (force != NULL && *force == 0 &&
1341 		    g_raid_nopens(sc) != 0) {
1342 			gctl_error(req, "Some volume is still open.");
1343 			return (-4);
1344 		}
1345 
1346 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1347 			if (disk->d_consumer)
1348 				sii_meta_erase(disk->d_consumer);
1349 		}
1350 		g_raid_destroy_node(sc, 0);
1351 		return (0);
1352 	}
1353 	if (strcmp(verb, "remove") == 0 ||
1354 	    strcmp(verb, "fail") == 0) {
1355 		if (*nargs < 2) {
1356 			gctl_error(req, "Invalid number of arguments.");
1357 			return (-1);
1358 		}
1359 		for (i = 1; i < *nargs; i++) {
1360 			snprintf(arg, sizeof(arg), "arg%d", i);
1361 			diskname = gctl_get_asciiparam(req, arg);
1362 			if (diskname == NULL) {
1363 				gctl_error(req, "No disk name (%s).", arg);
1364 				error = -2;
1365 				break;
1366 			}
1367 			if (strncmp(diskname, "/dev/", 5) == 0)
1368 				diskname += 5;
1369 
1370 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1371 				if (disk->d_consumer != NULL &&
1372 				    disk->d_consumer->provider != NULL &&
1373 				    strcmp(disk->d_consumer->provider->name,
1374 				     diskname) == 0)
1375 					break;
1376 			}
1377 			if (disk == NULL) {
1378 				gctl_error(req, "Disk '%s' not found.",
1379 				    diskname);
1380 				error = -3;
1381 				break;
1382 			}
1383 
1384 			if (strcmp(verb, "fail") == 0) {
1385 				g_raid_md_fail_disk_sii(md, NULL, disk);
1386 				continue;
1387 			}
1388 
1389 			pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1390 
1391 			/* Erase metadata on deleting disk. */
1392 			sii_meta_erase(disk->d_consumer);
1393 
1394 			/* If disk was assigned, just update statuses. */
1395 			if (pd->pd_disk_pos >= 0) {
1396 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1397 				g_raid_kill_consumer(sc, disk->d_consumer);
1398 				disk->d_consumer = NULL;
1399 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1400 					g_raid_change_subdisk_state(sd,
1401 					    G_RAID_SUBDISK_S_NONE);
1402 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1403 					    G_RAID_EVENT_SUBDISK);
1404 				}
1405 			} else {
1406 				/* Otherwise -- delete. */
1407 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1408 				g_raid_destroy_disk(disk);
1409 			}
1410 		}
1411 
1412 		/* Write updated metadata to remaining disks. */
1413 		g_raid_md_write_sii(md, NULL, NULL, NULL);
1414 
1415 		/* Check if anything left except placeholders. */
1416 		if (g_raid_ndisks(sc, -1) ==
1417 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1418 			g_raid_destroy_node(sc, 0);
1419 		else
1420 			g_raid_md_sii_refill(sc);
1421 		return (error);
1422 	}
1423 	if (strcmp(verb, "insert") == 0) {
1424 		if (*nargs < 2) {
1425 			gctl_error(req, "Invalid number of arguments.");
1426 			return (-1);
1427 		}
1428 		update = 0;
1429 		for (i = 1; i < *nargs; i++) {
1430 			/* Get disk name. */
1431 			snprintf(arg, sizeof(arg), "arg%d", i);
1432 			diskname = gctl_get_asciiparam(req, arg);
1433 			if (diskname == NULL) {
1434 				gctl_error(req, "No disk name (%s).", arg);
1435 				error = -3;
1436 				break;
1437 			}
1438 
1439 			/* Try to find provider with specified name. */
1440 			g_topology_lock();
1441 			cp = g_raid_open_consumer(sc, diskname);
1442 			if (cp == NULL) {
1443 				gctl_error(req, "Can't open disk '%s'.",
1444 				    diskname);
1445 				g_topology_unlock();
1446 				error = -4;
1447 				break;
1448 			}
1449 			pp = cp->provider;
1450 
1451 			pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
1452 			pd->pd_disk_pos = -3;
1453 			pd->pd_disk_size = pp->mediasize;
1454 
1455 			disk = g_raid_create_disk(sc);
1456 			disk->d_consumer = cp;
1457 			disk->d_md_data = (void *)pd;
1458 			cp->private = disk;
1459 			g_topology_unlock();
1460 
1461 			/* Read kernel dumping information. */
1462 			disk->d_kd.offset = 0;
1463 			disk->d_kd.length = OFF_MAX;
1464 			len = sizeof(disk->d_kd);
1465 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1466 			if (disk->d_kd.di.dumper == NULL)
1467 				G_RAID_DEBUG1(2, sc,
1468 				    "Dumping not supported by %s.",
1469 				    cp->provider->name);
1470 
1471 			/* Welcome the "new" disk. */
1472 			update += g_raid_md_sii_start_disk(disk);
1473 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
1474 				sii_meta_write_spare(cp);
1475 				g_raid_destroy_disk(disk);
1476 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
1477 				gctl_error(req, "Disk '%s' doesn't fit.",
1478 				    diskname);
1479 				g_raid_destroy_disk(disk);
1480 				error = -8;
1481 				break;
1482 			}
1483 		}
1484 
1485 		/* Write new metadata if we changed something. */
1486 		if (update)
1487 			g_raid_md_write_sii(md, NULL, NULL, NULL);
1488 		return (error);
1489 	}
1490 	gctl_error(req, "Command '%s' is not supported.", verb);
1491 	return (-100);
1492 }
1493 
1494 static int
1495 g_raid_md_write_sii(struct g_raid_md_object *md, struct g_raid_volume *tvol,
1496     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1497 {
1498 	struct g_raid_softc *sc;
1499 	struct g_raid_volume *vol;
1500 	struct g_raid_subdisk *sd;
1501 	struct g_raid_disk *disk;
1502 	struct g_raid_md_sii_object *mdi;
1503 	struct g_raid_md_sii_perdisk *pd;
1504 	struct sii_raid_conf *meta;
1505 	int i;
1506 
1507 	sc = md->mdo_softc;
1508 	mdi = (struct g_raid_md_sii_object *)md;
1509 
1510 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1511 		return (0);
1512 
1513 	/* Bump generation. Newly written metadata may differ from previous. */
1514 	mdi->mdio_generation++;
1515 
1516 	/* There is only one volume. */
1517 	vol = TAILQ_FIRST(&sc->sc_volumes);
1518 
1519 	/* Fill global fields. */
1520 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
1521 	if (mdi->mdio_meta)
1522 		memcpy(meta, mdi->mdio_meta, sizeof(*meta));
1523 	meta->total_sectors = vol->v_mediasize / vol->v_sectorsize;
1524 	meta->vendor_id = 0x1095;
1525 	meta->version_minor = 0;
1526 	meta->version_major = 2;
1527 	memcpy(&meta->timestamp, &mdi->mdio_timestamp, 6);
1528 	meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize;
1529 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) {
1530 		meta->type = SII_T_RAID0;
1531 		meta->raid0_disks = vol->v_disks_count;
1532 		meta->raid1_disks = 0xff;
1533 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
1534 		meta->type = SII_T_RAID1;
1535 		meta->raid0_disks = 0xff;
1536 		meta->raid1_disks = vol->v_disks_count;
1537 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
1538 		meta->type = SII_T_RAID01;
1539 		meta->raid0_disks = vol->v_disks_count / 2;
1540 		meta->raid1_disks = 2;
1541 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
1542 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) {
1543 		meta->type = SII_T_JBOD;
1544 		meta->raid0_disks = vol->v_disks_count;
1545 		meta->raid1_disks = 0xff;
1546 	} else {
1547 		meta->type = SII_T_RAID5;
1548 		meta->raid0_disks = vol->v_disks_count;
1549 		meta->raid1_disks = 0xff;
1550 	}
1551 	meta->generation = mdi->mdio_generation;
1552 	meta->raid_status = vol->v_dirty ? SII_S_ONLINE : SII_S_AVAILABLE;
1553 	for (i = 0; i < vol->v_disks_count; i++) {
1554 		sd = &vol->v_subdisks[i];
1555 		if (sd->sd_state == G_RAID_SUBDISK_S_STALE ||
1556 		    sd->sd_state == G_RAID_SUBDISK_S_RESYNC)
1557 			meta->raid_status = SII_S_ONLINE;
1558 	}
1559 	meta->raid_location = mdi->mdio_location;
1560 	sii_meta_put_name(meta, vol->v_name);
1561 
1562 	/* We are done. Print meta data and store them to disks. */
1563 	if (mdi->mdio_meta != NULL)
1564 		free(mdi->mdio_meta, M_MD_SII);
1565 	mdi->mdio_meta = meta;
1566 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1567 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1568 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
1569 			continue;
1570 		if (pd->pd_meta != NULL) {
1571 			free(pd->pd_meta, M_MD_SII);
1572 			pd->pd_meta = NULL;
1573 		}
1574 		pd->pd_meta = sii_meta_copy(meta);
1575 		if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) {
1576 			if (sd->sd_state < G_RAID_SUBDISK_S_NEW)
1577 				pd->pd_meta->disk_status = SII_S_DROPPED;
1578 			else if (sd->sd_state < G_RAID_SUBDISK_S_STALE) {
1579 				pd->pd_meta->disk_status = SII_S_REBUILD;
1580 				pd->pd_meta->rebuild_lba =
1581 				    sd->sd_rebuild_pos / vol->v_sectorsize;
1582 			} else
1583 				pd->pd_meta->disk_status = SII_S_CURRENT;
1584 			if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
1585 				pd->pd_meta->disk_number = sd->sd_pos;
1586 				pd->pd_meta->raid0_ident = 0xff;
1587 				pd->pd_meta->raid1_ident = 0;
1588 			} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
1589 				pd->pd_meta->disk_number = sd->sd_pos / meta->raid1_disks;
1590 				pd->pd_meta->raid0_ident = sd->sd_pos % meta->raid1_disks;
1591 				pd->pd_meta->raid1_ident = sd->sd_pos / meta->raid1_disks;
1592 			} else {
1593 				pd->pd_meta->disk_number = sd->sd_pos;
1594 				pd->pd_meta->raid0_ident = 0;
1595 				pd->pd_meta->raid1_ident = 0xff;
1596 			}
1597 		}
1598 		G_RAID_DEBUG(1, "Writing SiI metadata to %s",
1599 		    g_raid_get_diskname(disk));
1600 		g_raid_md_sii_print(pd->pd_meta);
1601 		sii_meta_write(disk->d_consumer, pd->pd_meta);
1602 	}
1603 	return (0);
1604 }
1605 
1606 static int
1607 g_raid_md_fail_disk_sii(struct g_raid_md_object *md,
1608     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1609 {
1610 	struct g_raid_softc *sc;
1611 	struct g_raid_md_sii_perdisk *pd;
1612 	struct g_raid_subdisk *sd;
1613 
1614 	sc = md->mdo_softc;
1615 	pd = (struct g_raid_md_sii_perdisk *)tdisk->d_md_data;
1616 
1617 	/* We can't fail disk that is not a part of array now. */
1618 	if (pd->pd_disk_pos < 0)
1619 		return (-1);
1620 
1621 	/*
1622 	 * Mark disk as failed in metadata and try to write that metadata
1623 	 * to the disk itself to prevent it's later resurrection as STALE.
1624 	 */
1625 	if (tdisk->d_consumer) {
1626 		if (pd->pd_meta) {
1627 			pd->pd_meta->disk_status = SII_S_REMOVED;
1628 			sii_meta_write(tdisk->d_consumer, pd->pd_meta);
1629 		} else
1630 			sii_meta_erase(tdisk->d_consumer);
1631 	}
1632 
1633 	/* Change states. */
1634 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
1635 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
1636 		g_raid_change_subdisk_state(sd,
1637 		    G_RAID_SUBDISK_S_FAILED);
1638 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
1639 		    G_RAID_EVENT_SUBDISK);
1640 	}
1641 
1642 	/* Write updated metadata to remaining disks. */
1643 	g_raid_md_write_sii(md, NULL, NULL, tdisk);
1644 
1645 	/* Check if anything left except placeholders. */
1646 	if (g_raid_ndisks(sc, -1) ==
1647 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1648 		g_raid_destroy_node(sc, 0);
1649 	else
1650 		g_raid_md_sii_refill(sc);
1651 	return (0);
1652 }
1653 
1654 static int
1655 g_raid_md_free_disk_sii(struct g_raid_md_object *md,
1656     struct g_raid_disk *disk)
1657 {
1658 	struct g_raid_md_sii_perdisk *pd;
1659 
1660 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1661 	if (pd->pd_meta != NULL) {
1662 		free(pd->pd_meta, M_MD_SII);
1663 		pd->pd_meta = NULL;
1664 	}
1665 	free(pd, M_MD_SII);
1666 	disk->d_md_data = NULL;
1667 	return (0);
1668 }
1669 
1670 static int
1671 g_raid_md_free_sii(struct g_raid_md_object *md)
1672 {
1673 	struct g_raid_md_sii_object *mdi;
1674 
1675 	mdi = (struct g_raid_md_sii_object *)md;
1676 	if (!mdi->mdio_started) {
1677 		mdi->mdio_started = 0;
1678 		callout_stop(&mdi->mdio_start_co);
1679 		G_RAID_DEBUG1(1, md->mdo_softc,
1680 		    "root_mount_rel %p", mdi->mdio_rootmount);
1681 		root_mount_rel(mdi->mdio_rootmount);
1682 		mdi->mdio_rootmount = NULL;
1683 	}
1684 	if (mdi->mdio_meta != NULL) {
1685 		free(mdi->mdio_meta, M_MD_SII);
1686 		mdi->mdio_meta = NULL;
1687 	}
1688 	return (0);
1689 }
1690 
1691 G_RAID_MD_DECLARE(g_raid_md_sii);
1692