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