xref: /freebsd/sys/geom/raid/md_sii.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #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/geom_dbg.h>
44 #include "geom/raid/g_raid.h"
45 #include "g_raid_md_if.h"
46 
47 static MALLOC_DEFINE(M_MD_SII, "md_sii_data", "GEOM_RAID SiI metadata");
48 
49 struct sii_raid_conf {
50 	uint16_t	ata_params_00_53[54];
51 	uint64_t	total_sectors;		/* 54 - 57 */
52 	uint16_t	ata_params_58_81[72];
53 	uint16_t	product_id;		/* 130 */
54 	uint16_t	vendor_id;		/* 131 */
55 	uint16_t	version_minor;		/* 132 */
56 	uint16_t	version_major;		/* 133 */
57 	uint8_t		timestamp[6];		/* 134 - 136 */
58 	uint16_t	strip_sectors;		/* 137 */
59 	uint16_t	dummy_2;
60 	uint8_t		disk_number;		/* 139 */
61 	uint8_t		type;
62 #define SII_T_RAID0             0x00
63 #define SII_T_RAID1             0x01
64 #define SII_T_RAID01            0x02
65 #define SII_T_SPARE             0x03
66 #define SII_T_CONCAT            0x04
67 #define SII_T_RAID5             0x10
68 #define SII_T_RESERVED          0xfd
69 #define SII_T_JBOD              0xff
70 
71 	uint8_t		raid0_disks;		/* 140 */
72 	uint8_t		raid0_ident;
73 	uint8_t		raid1_disks;		/* 141 */
74 	uint8_t		raid1_ident;
75 	uint64_t	rebuild_lba;		/* 142 - 145 */
76 	uint32_t	generation;		/* 146 - 147 */
77 	uint8_t		disk_status;		/* 148 */
78 #define SII_S_CURRENT           0x01
79 #define SII_S_REBUILD           0x02
80 #define SII_S_DROPPED           0x03
81 #define SII_S_REMOVED           0x04
82 
83 	uint8_t		raid_status;
84 #define SII_S_ONLINE            0x01
85 #define SII_S_AVAILABLE         0x02
86 
87 	uint8_t		raid_location;		/* 149 */
88 	uint8_t		disk_location;
89 	uint8_t		auto_rebuild;		/* 150 */
90 #define SII_R_REBUILD           0x00
91 #define SII_R_NOREBUILD         0xff
92 
93 	uint8_t		dummy_3;
94 	uint8_t		name[16];		/* 151 - 158 */
95 	uint16_t	checksum;		/* 159 */
96 	uint16_t	ata_params_160_255[96];
97 } __packed;
98 
99 struct g_raid_md_sii_perdisk {
100 	struct sii_raid_conf	*pd_meta;
101 	int			 pd_disk_pos;
102 	off_t			 pd_disk_size;
103 };
104 
105 struct g_raid_md_sii_object {
106 	struct g_raid_md_object	 mdio_base;
107 	uint8_t			 mdio_timestamp[6];
108 	uint8_t			 mdio_location;
109 	uint32_t		 mdio_generation;
110 	struct sii_raid_conf	*mdio_meta;
111 	struct callout		 mdio_start_co;	/* STARTING state timer. */
112 	int			 mdio_total_disks;
113 	int			 mdio_disks_present;
114 	int			 mdio_started;
115 	int			 mdio_incomplete;
116 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
117 };
118 
119 static g_raid_md_create_t g_raid_md_create_sii;
120 static g_raid_md_taste_t g_raid_md_taste_sii;
121 static g_raid_md_event_t g_raid_md_event_sii;
122 static g_raid_md_ctl_t g_raid_md_ctl_sii;
123 static g_raid_md_write_t g_raid_md_write_sii;
124 static g_raid_md_fail_disk_t g_raid_md_fail_disk_sii;
125 static g_raid_md_free_disk_t g_raid_md_free_disk_sii;
126 static g_raid_md_free_t g_raid_md_free_sii;
127 
128 static kobj_method_t g_raid_md_sii_methods[] = {
129 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_sii),
130 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_sii),
131 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_sii),
132 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_sii),
133 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_sii),
134 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_sii),
135 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_sii),
136 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_sii),
137 	{ 0, 0 }
138 };
139 
140 static struct g_raid_md_class g_raid_md_sii_class = {
141 	"SiI",
142 	g_raid_md_sii_methods,
143 	sizeof(struct g_raid_md_sii_object),
144 	.mdc_enable = 1,
145 	.mdc_priority = 100
146 };
147 
148 static void
149 g_raid_md_sii_print(struct sii_raid_conf *meta)
150 {
151 
152 	if (g_raid_debug < 1)
153 		return;
154 
155 	printf("********* ATA SiI RAID Metadata *********\n");
156 	printf("total_sectors       %llu\n",
157 	    (long long unsigned)meta->total_sectors);
158 	printf("product_id          0x%04x\n", meta->product_id);
159 	printf("vendor_id           0x%04x\n", meta->vendor_id);
160 	printf("version_minor       0x%04x\n", meta->version_minor);
161 	printf("version_major       0x%04x\n", meta->version_major);
162 	printf("timestamp           0x%02x%02x%02x%02x%02x%02x\n",
163 	    meta->timestamp[5], meta->timestamp[4], meta->timestamp[3],
164 	    meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]);
165 	printf("strip_sectors       %d\n", meta->strip_sectors);
166 	printf("disk_number         %d\n", meta->disk_number);
167 	printf("type                0x%02x\n", meta->type);
168 	printf("raid0_disks         %d\n", meta->raid0_disks);
169 	printf("raid0_ident         %d\n", meta->raid0_ident);
170 	printf("raid1_disks         %d\n", meta->raid1_disks);
171 	printf("raid1_ident         %d\n", meta->raid1_ident);
172 	printf("rebuild_lba         %llu\n",
173 	    (long long unsigned)meta->rebuild_lba);
174 	printf("generation          %d\n", meta->generation);
175 	printf("disk_status         %d\n", meta->disk_status);
176 	printf("raid_status         %d\n", meta->raid_status);
177 	printf("raid_location       %d\n", meta->raid_location);
178 	printf("disk_location       %d\n", meta->disk_location);
179 	printf("auto_rebuild        %d\n", meta->auto_rebuild);
180 	printf("name                <%.16s>\n", meta->name);
181 	printf("checksum            0x%04x\n", meta->checksum);
182 	printf("=================================================\n");
183 }
184 
185 static struct sii_raid_conf *
186 sii_meta_copy(struct sii_raid_conf *meta)
187 {
188 	struct sii_raid_conf *nmeta;
189 
190 	nmeta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
191 	memcpy(nmeta, meta, sizeof(*meta));
192 	return (nmeta);
193 }
194 
195 static int
196 sii_meta_total_disks(struct sii_raid_conf *meta)
197 {
198 
199 	switch (meta->type) {
200 	case SII_T_RAID0:
201 	case SII_T_RAID5:
202 	case SII_T_CONCAT:
203 		return (meta->raid0_disks);
204 	case SII_T_RAID1:
205 		return (meta->raid1_disks);
206 	case SII_T_RAID01:
207 		return (meta->raid0_disks * meta->raid1_disks);
208 	case SII_T_SPARE:
209 	case SII_T_JBOD:
210 		return (1);
211 	}
212 	return (0);
213 }
214 
215 static int
216 sii_meta_disk_pos(struct sii_raid_conf *meta, struct sii_raid_conf *pdmeta)
217 {
218 
219 	if (pdmeta->type == SII_T_SPARE)
220 		return (-3);
221 
222 	if (memcmp(&meta->timestamp, &pdmeta->timestamp, 6) != 0)
223 		return (-1);
224 
225 	switch (pdmeta->type) {
226 	case SII_T_RAID0:
227 	case SII_T_RAID1:
228 	case SII_T_RAID5:
229 	case SII_T_CONCAT:
230 		return (pdmeta->disk_number);
231 	case SII_T_RAID01:
232 		return (pdmeta->raid1_ident * pdmeta->raid1_disks +
233 		    pdmeta->raid0_ident);
234 	case SII_T_JBOD:
235 		return (0);
236 	}
237 	return (-1);
238 }
239 
240 static void
241 sii_meta_get_name(struct sii_raid_conf *meta, char *buf)
242 {
243 	int i;
244 
245 	strncpy(buf, meta->name, 16);
246 	buf[16] = 0;
247 	for (i = 15; i >= 0; i--) {
248 		if (buf[i] > 0x20)
249 			break;
250 		buf[i] = 0;
251 	}
252 }
253 
254 static void
255 sii_meta_put_name(struct sii_raid_conf *meta, char *buf)
256 {
257 
258 	memset(meta->name, 0x20, 16);
259 	memcpy(meta->name, buf, MIN(strlen(buf), 16));
260 }
261 
262 static struct sii_raid_conf *
263 sii_meta_read(struct g_consumer *cp)
264 {
265 	struct g_provider *pp;
266 	struct sii_raid_conf *meta;
267 	char *buf;
268 	int error, i;
269 	uint16_t checksum, *ptr;
270 
271 	pp = cp->provider;
272 	if (pp->sectorsize < sizeof(*meta))
273 		return (NULL);
274 	/* Read the anchor sector. */
275 	buf = g_read_data(cp,
276 	    pp->mediasize - pp->sectorsize, pp->sectorsize, &error);
277 	if (buf == NULL) {
278 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
279 		    pp->name, error);
280 		return (NULL);
281 	}
282 	meta = (struct sii_raid_conf *)buf;
283 
284 	/* Check vendor ID. */
285 	if (meta->vendor_id != 0x1095) {
286 		G_RAID_DEBUG(1, "SiI vendor ID check failed on %s (0x%04x)",
287 		    pp->name, meta->vendor_id);
288 		g_free(buf);
289 		return (NULL);
290 	}
291 
292 	/* Check metadata major version. */
293 	if (meta->version_major != 2) {
294 		G_RAID_DEBUG(1, "SiI version check failed on %s (%d.%d)",
295 		    pp->name, meta->version_major, meta->version_minor);
296 		g_free(buf);
297 		return (NULL);
298 	}
299 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
300 	memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
301 	g_free(buf);
302 
303 	/* Check metadata checksum. */
304 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i <= 159; i++)
305 		checksum += *ptr++;
306 	if (checksum != 0) {
307 		G_RAID_DEBUG(1, "SiI checksum check failed on %s", pp->name);
308 		free(meta, M_MD_SII);
309 		return (NULL);
310 	}
311 
312 	/* Check raid type. */
313 	if (meta->type != SII_T_RAID0 && meta->type != SII_T_RAID1 &&
314 	    meta->type != SII_T_RAID01 && meta->type != SII_T_SPARE &&
315 	    meta->type != SII_T_RAID5 && meta->type != SII_T_CONCAT &&
316 	    meta->type != SII_T_JBOD) {
317 		G_RAID_DEBUG(1, "SiI unknown RAID level on %s (0x%02x)",
318 		    pp->name, meta->type);
319 		free(meta, M_MD_SII);
320 		return (NULL);
321 	}
322 
323 	return (meta);
324 }
325 
326 static int
327 sii_meta_write(struct g_consumer *cp, struct sii_raid_conf *meta)
328 {
329 	struct g_provider *pp;
330 	char *buf;
331 	int error, i;
332 	uint16_t checksum, *ptr;
333 
334 	pp = cp->provider;
335 
336 	/* Recalculate checksum for case if metadata were changed. */
337 	meta->checksum = 0;
338 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 159; i++)
339 		checksum += *ptr++;
340 	meta->checksum -= checksum;
341 
342 	/* Create and fill buffer. */
343 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
344 	memcpy(buf, meta, sizeof(*meta));
345 
346 	/* Write 4 copies of metadata. */
347 	for (i = 0; i < 4; i++) {
348 		error = g_write_data(cp,
349 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
350 		    buf, pp->sectorsize);
351 		if (error != 0) {
352 			G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
353 			    pp->name, error);
354 			break;
355 		}
356 	}
357 
358 	free(buf, M_MD_SII);
359 	return (error);
360 }
361 
362 static int
363 sii_meta_erase(struct g_consumer *cp)
364 {
365 	struct g_provider *pp;
366 	char *buf;
367 	int error, i;
368 
369 	pp = cp->provider;
370 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
371 	/* Write 4 copies of metadata. */
372 	for (i = 0; i < 4; i++) {
373 		error = g_write_data(cp,
374 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
375 		    buf, pp->sectorsize);
376 		if (error != 0) {
377 			G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
378 			    pp->name, error);
379 		}
380 	}
381 	free(buf, M_MD_SII);
382 	return (error);
383 }
384 
385 static int
386 sii_meta_write_spare(struct g_consumer *cp)
387 {
388 	struct sii_raid_conf *meta;
389 	int error;
390 
391 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
392 	meta->total_sectors = cp->provider->mediasize /
393 	    cp->provider->sectorsize - 0x800;
394 	meta->vendor_id = 0x1095;
395 	meta->version_minor = 0;
396 	meta->version_major = 2;
397 	meta->timestamp[0] = arc4random();
398 	meta->timestamp[1] = arc4random();
399 	meta->timestamp[2] = arc4random();
400 	meta->timestamp[3] = arc4random();
401 	meta->timestamp[4] = arc4random();
402 	meta->timestamp[5] = arc4random();
403 	meta->type = SII_T_SPARE;
404 	meta->generation = 1;
405 	meta->raid1_ident = 0xff;
406 	meta->raid_location = arc4random();
407 	error = sii_meta_write(cp, meta);
408 	free(meta, M_MD_SII);
409 	return (error);
410 }
411 
412 static struct g_raid_disk *
413 g_raid_md_sii_get_disk(struct g_raid_softc *sc, int id)
414 {
415 	struct g_raid_disk	*disk;
416 	struct g_raid_md_sii_perdisk *pd;
417 
418 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
419 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
420 		if (pd->pd_disk_pos == id)
421 			break;
422 	}
423 	return (disk);
424 }
425 
426 static int
427 g_raid_md_sii_supported(int level, int qual, int disks, int force)
428 {
429 
430 	if (disks > 8)
431 		return (0);
432 	switch (level) {
433 	case G_RAID_VOLUME_RL_RAID0:
434 		if (disks < 1)
435 			return (0);
436 		if (!force && (disks < 2 || disks > 6))
437 			return (0);
438 		break;
439 	case G_RAID_VOLUME_RL_RAID1:
440 		if (disks < 1)
441 			return (0);
442 		if (!force && (disks != 2))
443 			return (0);
444 		break;
445 	case G_RAID_VOLUME_RL_RAID1E:
446 		if (disks < 2)
447 			return (0);
448 		if (disks % 2 != 0)
449 			return (0);
450 		if (!force && (disks < 4))
451 			return (0);
452 		break;
453 	case G_RAID_VOLUME_RL_SINGLE:
454 		if (disks != 1)
455 			return (0);
456 		break;
457 	case G_RAID_VOLUME_RL_CONCAT:
458 		if (disks < 2)
459 			return (0);
460 		break;
461 	case G_RAID_VOLUME_RL_RAID5:
462 		if (disks < 3)
463 			return (0);
464 		if (qual != G_RAID_VOLUME_RLQ_R5LS)
465 			return (0);
466 		break;
467 	default:
468 		return (0);
469 	}
470 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
471 		return (0);
472 	return (1);
473 }
474 
475 static int
476 g_raid_md_sii_start_disk(struct g_raid_disk *disk)
477 {
478 	struct g_raid_softc *sc;
479 	struct g_raid_subdisk *sd, *tmpsd;
480 	struct g_raid_disk *olddisk, *tmpdisk;
481 	struct g_raid_md_object *md;
482 	struct g_raid_md_sii_object *mdi;
483 	struct g_raid_md_sii_perdisk *pd, *oldpd;
484 	struct sii_raid_conf *meta;
485 	int disk_pos, resurrection = 0;
486 
487 	sc = disk->d_softc;
488 	md = sc->sc_md;
489 	mdi = (struct g_raid_md_sii_object *)md;
490 	meta = mdi->mdio_meta;
491 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
492 	olddisk = NULL;
493 
494 	/* Find disk position in metadata by its serial. */
495 	if (pd->pd_meta != NULL)
496 		disk_pos = sii_meta_disk_pos(meta, pd->pd_meta);
497 	else
498 		disk_pos = -3;
499 	if (disk_pos < 0) {
500 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
501 		/* If we are in the start process, that's all for now. */
502 		if (!mdi->mdio_started)
503 			goto nofit;
504 		/*
505 		 * If we have already started - try to get use of the disk.
506 		 * Try to replace OFFLINE disks first, then FAILED.
507 		 */
508 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
509 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
510 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
511 				continue;
512 			/* Make sure this disk is big enough. */
513 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
514 				if (sd->sd_offset + sd->sd_size + 512 >
515 				    pd->pd_disk_size) {
516 					G_RAID_DEBUG1(1, sc,
517 					    "Disk too small (%ju < %ju)",
518 					    pd->pd_disk_size,
519 					    sd->sd_offset + sd->sd_size + 512);
520 					break;
521 				}
522 			}
523 			if (sd != NULL)
524 				continue;
525 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
526 				olddisk = tmpdisk;
527 				break;
528 			} else if (olddisk == NULL)
529 				olddisk = tmpdisk;
530 		}
531 		if (olddisk == NULL) {
532 nofit:
533 			if (disk_pos == -3 || pd->pd_disk_pos == -3) {
534 				g_raid_change_disk_state(disk,
535 				    G_RAID_DISK_S_SPARE);
536 				return (1);
537 			} else {
538 				g_raid_change_disk_state(disk,
539 				    G_RAID_DISK_S_STALE);
540 				return (0);
541 			}
542 		}
543 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
544 		disk_pos = oldpd->pd_disk_pos;
545 		resurrection = 1;
546 	}
547 
548 	if (olddisk == NULL) {
549 		/* Find placeholder by position. */
550 		olddisk = g_raid_md_sii_get_disk(sc, disk_pos);
551 		if (olddisk == NULL)
552 			panic("No disk at position %d!", disk_pos);
553 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
554 			G_RAID_DEBUG1(1, sc, "More than one disk for pos %d",
555 			    disk_pos);
556 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
557 			return (0);
558 		}
559 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
560 	}
561 
562 	/* Replace failed disk or placeholder with new disk. */
563 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
564 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
565 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
566 		sd->sd_disk = disk;
567 	}
568 	oldpd->pd_disk_pos = -2;
569 	pd->pd_disk_pos = disk_pos;
570 
571 	/* If it was placeholder -- destroy it. */
572 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
573 		g_raid_destroy_disk(olddisk);
574 	} else {
575 		/* Otherwise, make it STALE_FAILED. */
576 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
577 	}
578 
579 	/* Welcome the new disk. */
580 	if (resurrection)
581 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
582 	else if (pd->pd_meta->disk_status == SII_S_CURRENT ||
583 	    pd->pd_meta->disk_status == SII_S_REBUILD)
584 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
585 	else
586 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
587 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
588 		/*
589 		 * Different disks may have different sizes,
590 		 * in concat mode. Update from real disk size.
591 		 */
592 		if (meta->type == SII_T_CONCAT || meta->type == SII_T_JBOD)
593 			sd->sd_size = pd->pd_disk_size - 0x800 * 512;
594 
595 		if (resurrection) {
596 			/* New or ex-spare disk. */
597 			g_raid_change_subdisk_state(sd,
598 			    G_RAID_SUBDISK_S_NEW);
599 		} else if (pd->pd_meta->disk_status == SII_S_REBUILD) {
600 			/* Rebuilding disk. */
601 			g_raid_change_subdisk_state(sd,
602 			    G_RAID_SUBDISK_S_REBUILD);
603 			if (pd->pd_meta->generation == meta->generation)
604 				sd->sd_rebuild_pos = pd->pd_meta->rebuild_lba * 512;
605 			else
606 				sd->sd_rebuild_pos = 0;
607 		} else if (pd->pd_meta->disk_status == SII_S_CURRENT) {
608 			if (pd->pd_meta->raid_status == SII_S_ONLINE ||
609 			    pd->pd_meta->generation != meta->generation) {
610 				/* Dirty or resyncing disk. */
611 				g_raid_change_subdisk_state(sd,
612 				    G_RAID_SUBDISK_S_STALE);
613 			} else {
614 				/* Up to date disk. */
615 				g_raid_change_subdisk_state(sd,
616 				    G_RAID_SUBDISK_S_ACTIVE);
617 			}
618 		} else {
619 			g_raid_change_subdisk_state(sd,
620 			    G_RAID_SUBDISK_S_FAILED);
621 		}
622 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
623 		    G_RAID_EVENT_SUBDISK);
624 	}
625 
626 	/* Update status of our need for spare. */
627 	if (mdi->mdio_started) {
628 		mdi->mdio_incomplete =
629 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
630 		     mdi->mdio_total_disks);
631 	}
632 
633 	return (resurrection);
634 }
635 
636 static void
637 g_disk_md_sii_retaste(void *arg, int pending)
638 {
639 
640 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
641 	g_retaste(&g_raid_class);
642 	free(arg, M_MD_SII);
643 }
644 
645 static void
646 g_raid_md_sii_refill(struct g_raid_softc *sc)
647 {
648 	struct g_raid_md_object *md;
649 	struct g_raid_md_sii_object *mdi;
650 	struct g_raid_disk *disk;
651 	struct task *task;
652 	int update, na;
653 
654 	md = sc->sc_md;
655 	mdi = (struct g_raid_md_sii_object *)md;
656 	update = 0;
657 	do {
658 		/* Make sure we miss anything. */
659 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
660 		if (na == mdi->mdio_total_disks)
661 			break;
662 
663 		G_RAID_DEBUG1(1, md->mdo_softc,
664 		    "Array is not complete (%d of %d), "
665 		    "trying to refill.", na, mdi->mdio_total_disks);
666 
667 		/* Try to get use some of STALE disks. */
668 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
669 			if (disk->d_state == G_RAID_DISK_S_STALE) {
670 				update += g_raid_md_sii_start_disk(disk);
671 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
672 					break;
673 			}
674 		}
675 		if (disk != NULL)
676 			continue;
677 
678 		/* Try to get use some of SPARE disks. */
679 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
680 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
681 				update += g_raid_md_sii_start_disk(disk);
682 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
683 					break;
684 			}
685 		}
686 	} while (disk != NULL);
687 
688 	/* Write new metadata if we changed something. */
689 	if (update)
690 		g_raid_md_write_sii(md, NULL, NULL, NULL);
691 
692 	/* Update status of our need for spare. */
693 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
694 	    mdi->mdio_total_disks);
695 
696 	/* Request retaste hoping to find spare. */
697 	if (mdi->mdio_incomplete) {
698 		task = malloc(sizeof(struct task),
699 		    M_MD_SII, M_WAITOK | M_ZERO);
700 		TASK_INIT(task, 0, g_disk_md_sii_retaste, task);
701 		taskqueue_enqueue(taskqueue_swi, task);
702 	}
703 }
704 
705 static void
706 g_raid_md_sii_start(struct g_raid_softc *sc)
707 {
708 	struct g_raid_md_object *md;
709 	struct g_raid_md_sii_object *mdi;
710 	struct g_raid_md_sii_perdisk *pd;
711 	struct sii_raid_conf *meta;
712 	struct g_raid_volume *vol;
713 	struct g_raid_subdisk *sd;
714 	struct g_raid_disk *disk, *best;
715 	off_t size;
716 	int j, disk_pos;
717 	uint32_t gendiff, bestgendiff;
718 	char buf[17];
719 
720 	md = sc->sc_md;
721 	mdi = (struct g_raid_md_sii_object *)md;
722 	meta = mdi->mdio_meta;
723 
724 	/* Create volumes and subdisks. */
725 	sii_meta_get_name(meta, buf);
726 	vol = g_raid_create_volume(sc, buf, -1);
727 	vol->v_mediasize = (off_t)meta->total_sectors * 512;
728 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
729 	if (meta->type == SII_T_RAID0) {
730 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
731 		size = vol->v_mediasize / mdi->mdio_total_disks;
732 	} else if (meta->type == SII_T_RAID1) {
733 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
734 		size = vol->v_mediasize;
735 	} else if (meta->type == SII_T_RAID01) {
736 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
737 		size = vol->v_mediasize / (mdi->mdio_total_disks / 2);
738 	} else if (meta->type == SII_T_CONCAT) {
739 		if (mdi->mdio_total_disks == 1)
740 			vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
741 		else
742 			vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
743 		size = 0;
744 	} else if (meta->type == SII_T_RAID5) {
745 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
746 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
747 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
748 	} else if (meta->type == SII_T_JBOD) {
749 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
750 		size = 0;
751 	} else {
752 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
753 		size = 0;
754 	}
755 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
756 	vol->v_disks_count = mdi->mdio_total_disks;
757 	vol->v_sectorsize = 512; //ZZZ
758 	for (j = 0; j < vol->v_disks_count; j++) {
759 		sd = &vol->v_subdisks[j];
760 		sd->sd_offset = 0;
761 		sd->sd_size = size;
762 	}
763 	g_raid_start_volume(vol);
764 
765 	/* Create disk placeholders to store data for later writing. */
766 	for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
767 		pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
768 		pd->pd_disk_pos = disk_pos;
769 		disk = g_raid_create_disk(sc);
770 		disk->d_md_data = (void *)pd;
771 		disk->d_state = G_RAID_DISK_S_OFFLINE;
772 		sd = &vol->v_subdisks[disk_pos];
773 		sd->sd_disk = disk;
774 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
775 	}
776 
777 	/*
778 	 * Make all disks found till the moment take their places
779 	 * in order of their generation numbers.
780 	 */
781 	do {
782 		best = NULL;
783 		bestgendiff = 0xffffffff;
784 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
785 			if (disk->d_state != G_RAID_DISK_S_NONE)
786 				continue;
787 			pd = disk->d_md_data;
788 			if (pd->pd_meta == NULL)
789 				gendiff = 0xfffffffe;
790 			else
791 				gendiff = meta->generation -
792 				    pd->pd_meta->generation;
793 			if (gendiff < bestgendiff) {
794 				best = disk;
795 				bestgendiff = gendiff;
796 			}
797 		}
798 		if (best != NULL)
799 			g_raid_md_sii_start_disk(best);
800 	} while (best != NULL);
801 
802 	mdi->mdio_started = 1;
803 	G_RAID_DEBUG1(0, sc, "Array started.");
804 	g_raid_md_write_sii(md, NULL, NULL, NULL);
805 
806 	/* Pickup any STALE/SPARE disks to refill array if needed. */
807 	g_raid_md_sii_refill(sc);
808 
809 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
810 
811 	callout_stop(&mdi->mdio_start_co);
812 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
813 	root_mount_rel(mdi->mdio_rootmount);
814 	mdi->mdio_rootmount = NULL;
815 }
816 
817 static void
818 g_raid_md_sii_new_disk(struct g_raid_disk *disk)
819 {
820 	struct g_raid_softc *sc;
821 	struct g_raid_md_object *md;
822 	struct g_raid_md_sii_object *mdi;
823 	struct sii_raid_conf *pdmeta;
824 	struct g_raid_md_sii_perdisk *pd;
825 
826 	sc = disk->d_softc;
827 	md = sc->sc_md;
828 	mdi = (struct g_raid_md_sii_object *)md;
829 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
830 	pdmeta = pd->pd_meta;
831 
832 	if (mdi->mdio_started) {
833 		if (g_raid_md_sii_start_disk(disk))
834 			g_raid_md_write_sii(md, NULL, NULL, NULL);
835 	} else {
836 		if (mdi->mdio_meta == NULL ||
837 		    ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) {
838 			G_RAID_DEBUG1(1, sc, "Newer disk");
839 			if (mdi->mdio_meta != NULL)
840 				free(mdi->mdio_meta, M_MD_SII);
841 			mdi->mdio_meta = sii_meta_copy(pdmeta);
842 			mdi->mdio_generation = mdi->mdio_meta->generation;
843 			mdi->mdio_total_disks = sii_meta_total_disks(pdmeta);
844 			mdi->mdio_disks_present = 1;
845 		} else if (pdmeta->generation == mdi->mdio_generation) {
846 			mdi->mdio_disks_present++;
847 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
848 			    mdi->mdio_disks_present,
849 			    mdi->mdio_total_disks);
850 		} else {
851 			G_RAID_DEBUG1(1, sc, "Older disk");
852 		}
853 
854 		/* If we collected all needed disks - start array. */
855 		if (mdi->mdio_disks_present == mdi->mdio_total_disks)
856 			g_raid_md_sii_start(sc);
857 	}
858 }
859 
860 static void
861 g_raid_sii_go(void *arg)
862 {
863 	struct g_raid_softc *sc;
864 	struct g_raid_md_object *md;
865 	struct g_raid_md_sii_object *mdi;
866 
867 	sc = arg;
868 	md = sc->sc_md;
869 	mdi = (struct g_raid_md_sii_object *)md;
870 	if (!mdi->mdio_started) {
871 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
872 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
873 	}
874 }
875 
876 static int
877 g_raid_md_create_sii(struct g_raid_md_object *md, struct g_class *mp,
878     struct g_geom **gp)
879 {
880 	struct g_raid_softc *sc;
881 	struct g_raid_md_sii_object *mdi;
882 	char name[32];
883 
884 	mdi = (struct g_raid_md_sii_object *)md;
885 	mdi->mdio_timestamp[5] = arc4random();
886 	mdi->mdio_timestamp[4] = arc4random();
887 	mdi->mdio_timestamp[3] = arc4random();
888 	mdi->mdio_timestamp[2] = arc4random();
889 	mdi->mdio_timestamp[1] = arc4random();
890 	mdi->mdio_timestamp[0] = arc4random();
891 	mdi->mdio_location = arc4random();
892 	mdi->mdio_generation = 0;
893 	snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x",
894 	    mdi->mdio_timestamp[5], mdi->mdio_timestamp[4],
895 	    mdi->mdio_timestamp[3], mdi->mdio_timestamp[2],
896 	    mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]);
897 	sc = g_raid_create_node(mp, name, md);
898 	if (sc == NULL)
899 		return (G_RAID_MD_TASTE_FAIL);
900 	md->mdo_softc = sc;
901 	*gp = sc->sc_geom;
902 	return (G_RAID_MD_TASTE_NEW);
903 }
904 
905 static int
906 g_raid_md_taste_sii(struct g_raid_md_object *md, struct g_class *mp,
907                               struct g_consumer *cp, struct g_geom **gp)
908 {
909 	struct g_consumer *rcp;
910 	struct g_provider *pp;
911 	struct g_raid_md_sii_object *mdi, *mdi1;
912 	struct g_raid_softc *sc;
913 	struct g_raid_disk *disk;
914 	struct sii_raid_conf *meta;
915 	struct g_raid_md_sii_perdisk *pd;
916 	struct g_geom *geom;
917 	int disk_pos, result, spare, len;
918 	char name[32];
919 	uint16_t vendor;
920 
921 	G_RAID_DEBUG(1, "Tasting SiI on %s", cp->provider->name);
922 	mdi = (struct g_raid_md_sii_object *)md;
923 	pp = cp->provider;
924 
925 	/* Read metadata from device. */
926 	meta = NULL;
927 	g_topology_unlock();
928 	vendor = 0xffff;
929 	len = sizeof(vendor);
930 	if (pp->geom->rank == 1)
931 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
932 	meta = sii_meta_read(cp);
933 	g_topology_lock();
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 	/* There is no return after this point, so we close passed consumer. */
1014 	g_access(cp, -1, 0, 0);
1015 
1016 	rcp = g_new_consumer(geom);
1017 	rcp->flags |= G_CF_DIRECT_RECEIVE;
1018 	g_attach(rcp, pp);
1019 	if (g_access(rcp, 1, 1, 1) != 0)
1020 		; //goto fail1;
1021 
1022 	g_topology_unlock();
1023 	sx_xlock(&sc->sc_lock);
1024 
1025 	pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
1026 	pd->pd_meta = meta;
1027 	if (spare == 2) {
1028 		pd->pd_disk_pos = -3;
1029 	} else {
1030 		pd->pd_disk_pos = -1;
1031 	}
1032 	pd->pd_disk_size = pp->mediasize;
1033 	disk = g_raid_create_disk(sc);
1034 	disk->d_md_data = (void *)pd;
1035 	disk->d_consumer = rcp;
1036 	rcp->private = disk;
1037 
1038 	g_raid_get_disk_info(disk);
1039 
1040 	g_raid_md_sii_new_disk(disk);
1041 
1042 	sx_xunlock(&sc->sc_lock);
1043 	g_topology_lock();
1044 	*gp = geom;
1045 	return (result);
1046 fail1:
1047 	free(meta, M_MD_SII);
1048 	return (G_RAID_MD_TASTE_FAIL);
1049 }
1050 
1051 static int
1052 g_raid_md_event_sii(struct g_raid_md_object *md,
1053     struct g_raid_disk *disk, u_int event)
1054 {
1055 	struct g_raid_softc *sc;
1056 	struct g_raid_subdisk *sd;
1057 	struct g_raid_md_sii_object *mdi;
1058 	struct g_raid_md_sii_perdisk *pd;
1059 
1060 	sc = md->mdo_softc;
1061 	mdi = (struct g_raid_md_sii_object *)md;
1062 	if (disk == NULL) {
1063 		switch (event) {
1064 		case G_RAID_NODE_E_START:
1065 			if (!mdi->mdio_started)
1066 				g_raid_md_sii_start(sc);
1067 			return (0);
1068 		}
1069 		return (-1);
1070 	}
1071 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1072 	switch (event) {
1073 	case G_RAID_DISK_E_DISCONNECTED:
1074 		/* If disk was assigned, just update statuses. */
1075 		if (pd->pd_disk_pos >= 0) {
1076 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1077 			if (disk->d_consumer) {
1078 				g_raid_kill_consumer(sc, disk->d_consumer);
1079 				disk->d_consumer = NULL;
1080 			}
1081 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1082 				g_raid_change_subdisk_state(sd,
1083 				    G_RAID_SUBDISK_S_NONE);
1084 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1085 				    G_RAID_EVENT_SUBDISK);
1086 			}
1087 		} else {
1088 			/* Otherwise -- delete. */
1089 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1090 			g_raid_destroy_disk(disk);
1091 		}
1092 
1093 		/* Write updated metadata to all disks. */
1094 		g_raid_md_write_sii(md, NULL, NULL, NULL);
1095 
1096 		/* Check if anything left except placeholders. */
1097 		if (g_raid_ndisks(sc, -1) ==
1098 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1099 			g_raid_destroy_node(sc, 0);
1100 		else
1101 			g_raid_md_sii_refill(sc);
1102 		return (0);
1103 	}
1104 	return (-2);
1105 }
1106 
1107 static int
1108 g_raid_md_ctl_sii(struct g_raid_md_object *md,
1109     struct gctl_req *req)
1110 {
1111 	struct g_raid_softc *sc;
1112 	struct g_raid_volume *vol;
1113 	struct g_raid_subdisk *sd;
1114 	struct g_raid_disk *disk;
1115 	struct g_raid_md_sii_object *mdi;
1116 	struct g_raid_md_sii_perdisk *pd;
1117 	struct g_consumer *cp;
1118 	struct g_provider *pp;
1119 	char arg[16];
1120 	const char *verb, *volname, *levelname, *diskname;
1121 	int *nargs, *force;
1122 	off_t size, sectorsize, strip;
1123 	intmax_t *sizearg, *striparg;
1124 	int numdisks, i, len, level, qual, update;
1125 	int error;
1126 
1127 	sc = md->mdo_softc;
1128 	mdi = (struct g_raid_md_sii_object *)md;
1129 	verb = gctl_get_param(req, "verb", NULL);
1130 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1131 	error = 0;
1132 	if (strcmp(verb, "label") == 0) {
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 		/* Check if some volume is still open. */
1326 		force = gctl_get_paraml(req, "force", sizeof(*force));
1327 		if (force != NULL && *force == 0 &&
1328 		    g_raid_nopens(sc) != 0) {
1329 			gctl_error(req, "Some volume is still open.");
1330 			return (-4);
1331 		}
1332 
1333 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1334 			if (disk->d_consumer)
1335 				sii_meta_erase(disk->d_consumer);
1336 		}
1337 		g_raid_destroy_node(sc, 0);
1338 		return (0);
1339 	}
1340 	if (strcmp(verb, "remove") == 0 ||
1341 	    strcmp(verb, "fail") == 0) {
1342 		if (*nargs < 2) {
1343 			gctl_error(req, "Invalid number of arguments.");
1344 			return (-1);
1345 		}
1346 		for (i = 1; i < *nargs; i++) {
1347 			snprintf(arg, sizeof(arg), "arg%d", i);
1348 			diskname = gctl_get_asciiparam(req, arg);
1349 			if (diskname == NULL) {
1350 				gctl_error(req, "No disk name (%s).", arg);
1351 				error = -2;
1352 				break;
1353 			}
1354 			if (strncmp(diskname, _PATH_DEV, 5) == 0)
1355 				diskname += 5;
1356 
1357 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1358 				if (disk->d_consumer != NULL &&
1359 				    disk->d_consumer->provider != NULL &&
1360 				    strcmp(disk->d_consumer->provider->name,
1361 				     diskname) == 0)
1362 					break;
1363 			}
1364 			if (disk == NULL) {
1365 				gctl_error(req, "Disk '%s' not found.",
1366 				    diskname);
1367 				error = -3;
1368 				break;
1369 			}
1370 
1371 			if (strcmp(verb, "fail") == 0) {
1372 				g_raid_md_fail_disk_sii(md, NULL, disk);
1373 				continue;
1374 			}
1375 
1376 			pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1377 
1378 			/* Erase metadata on deleting disk. */
1379 			sii_meta_erase(disk->d_consumer);
1380 
1381 			/* If disk was assigned, just update statuses. */
1382 			if (pd->pd_disk_pos >= 0) {
1383 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1384 				g_raid_kill_consumer(sc, disk->d_consumer);
1385 				disk->d_consumer = NULL;
1386 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1387 					g_raid_change_subdisk_state(sd,
1388 					    G_RAID_SUBDISK_S_NONE);
1389 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1390 					    G_RAID_EVENT_SUBDISK);
1391 				}
1392 			} else {
1393 				/* Otherwise -- delete. */
1394 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1395 				g_raid_destroy_disk(disk);
1396 			}
1397 		}
1398 
1399 		/* Write updated metadata to remaining disks. */
1400 		g_raid_md_write_sii(md, NULL, NULL, NULL);
1401 
1402 		/* Check if anything left except placeholders. */
1403 		if (g_raid_ndisks(sc, -1) ==
1404 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1405 			g_raid_destroy_node(sc, 0);
1406 		else
1407 			g_raid_md_sii_refill(sc);
1408 		return (error);
1409 	}
1410 	if (strcmp(verb, "insert") == 0) {
1411 		if (*nargs < 2) {
1412 			gctl_error(req, "Invalid number of arguments.");
1413 			return (-1);
1414 		}
1415 		update = 0;
1416 		for (i = 1; i < *nargs; i++) {
1417 			/* Get disk name. */
1418 			snprintf(arg, sizeof(arg), "arg%d", i);
1419 			diskname = gctl_get_asciiparam(req, arg);
1420 			if (diskname == NULL) {
1421 				gctl_error(req, "No disk name (%s).", arg);
1422 				error = -3;
1423 				break;
1424 			}
1425 
1426 			/* Try to find provider with specified name. */
1427 			g_topology_lock();
1428 			cp = g_raid_open_consumer(sc, diskname);
1429 			if (cp == NULL) {
1430 				gctl_error(req, "Can't open disk '%s'.",
1431 				    diskname);
1432 				g_topology_unlock();
1433 				error = -4;
1434 				break;
1435 			}
1436 			pp = cp->provider;
1437 
1438 			pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
1439 			pd->pd_disk_pos = -3;
1440 			pd->pd_disk_size = pp->mediasize;
1441 
1442 			disk = g_raid_create_disk(sc);
1443 			disk->d_consumer = cp;
1444 			disk->d_md_data = (void *)pd;
1445 			cp->private = disk;
1446 			g_topology_unlock();
1447 
1448 			g_raid_get_disk_info(disk);
1449 
1450 			/* Welcome the "new" disk. */
1451 			update += g_raid_md_sii_start_disk(disk);
1452 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
1453 				sii_meta_write_spare(cp);
1454 				g_raid_destroy_disk(disk);
1455 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
1456 				gctl_error(req, "Disk '%s' doesn't fit.",
1457 				    diskname);
1458 				g_raid_destroy_disk(disk);
1459 				error = -8;
1460 				break;
1461 			}
1462 		}
1463 
1464 		/* Write new metadata if we changed something. */
1465 		if (update)
1466 			g_raid_md_write_sii(md, NULL, NULL, NULL);
1467 		return (error);
1468 	}
1469 	gctl_error(req, "Command '%s' is not supported.", verb);
1470 	return (-100);
1471 }
1472 
1473 static int
1474 g_raid_md_write_sii(struct g_raid_md_object *md, struct g_raid_volume *tvol,
1475     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1476 {
1477 	struct g_raid_softc *sc;
1478 	struct g_raid_volume *vol;
1479 	struct g_raid_subdisk *sd;
1480 	struct g_raid_disk *disk;
1481 	struct g_raid_md_sii_object *mdi;
1482 	struct g_raid_md_sii_perdisk *pd;
1483 	struct sii_raid_conf *meta;
1484 	u_int i;
1485 
1486 	sc = md->mdo_softc;
1487 	mdi = (struct g_raid_md_sii_object *)md;
1488 
1489 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1490 		return (0);
1491 
1492 	/* Bump generation. Newly written metadata may differ from previous. */
1493 	mdi->mdio_generation++;
1494 
1495 	/* There is only one volume. */
1496 	vol = TAILQ_FIRST(&sc->sc_volumes);
1497 
1498 	/* Fill global fields. */
1499 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
1500 	if (mdi->mdio_meta)
1501 		memcpy(meta, mdi->mdio_meta, sizeof(*meta));
1502 	meta->total_sectors = vol->v_mediasize / vol->v_sectorsize;
1503 	meta->vendor_id = 0x1095;
1504 	meta->version_minor = 0;
1505 	meta->version_major = 2;
1506 	memcpy(&meta->timestamp, &mdi->mdio_timestamp, 6);
1507 	meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize;
1508 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) {
1509 		meta->type = SII_T_RAID0;
1510 		meta->raid0_disks = vol->v_disks_count;
1511 		meta->raid1_disks = 0xff;
1512 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
1513 		meta->type = SII_T_RAID1;
1514 		meta->raid0_disks = 0xff;
1515 		meta->raid1_disks = vol->v_disks_count;
1516 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
1517 		meta->type = SII_T_RAID01;
1518 		meta->raid0_disks = vol->v_disks_count / 2;
1519 		meta->raid1_disks = 2;
1520 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
1521 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) {
1522 		meta->type = SII_T_JBOD;
1523 		meta->raid0_disks = vol->v_disks_count;
1524 		meta->raid1_disks = 0xff;
1525 	} else {
1526 		meta->type = SII_T_RAID5;
1527 		meta->raid0_disks = vol->v_disks_count;
1528 		meta->raid1_disks = 0xff;
1529 	}
1530 	meta->generation = mdi->mdio_generation;
1531 	meta->raid_status = vol->v_dirty ? SII_S_ONLINE : SII_S_AVAILABLE;
1532 	for (i = 0; i < vol->v_disks_count; i++) {
1533 		sd = &vol->v_subdisks[i];
1534 		if (sd->sd_state == G_RAID_SUBDISK_S_STALE ||
1535 		    sd->sd_state == G_RAID_SUBDISK_S_RESYNC)
1536 			meta->raid_status = SII_S_ONLINE;
1537 	}
1538 	meta->raid_location = mdi->mdio_location;
1539 	sii_meta_put_name(meta, vol->v_name);
1540 
1541 	/* We are done. Print meta data and store them to disks. */
1542 	if (mdi->mdio_meta != NULL)
1543 		free(mdi->mdio_meta, M_MD_SII);
1544 	mdi->mdio_meta = meta;
1545 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1546 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1547 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
1548 			continue;
1549 		if (pd->pd_meta != NULL) {
1550 			free(pd->pd_meta, M_MD_SII);
1551 			pd->pd_meta = NULL;
1552 		}
1553 		pd->pd_meta = sii_meta_copy(meta);
1554 		if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) {
1555 			if (sd->sd_state < G_RAID_SUBDISK_S_NEW)
1556 				pd->pd_meta->disk_status = SII_S_DROPPED;
1557 			else if (sd->sd_state < G_RAID_SUBDISK_S_STALE) {
1558 				pd->pd_meta->disk_status = SII_S_REBUILD;
1559 				pd->pd_meta->rebuild_lba =
1560 				    sd->sd_rebuild_pos / vol->v_sectorsize;
1561 			} else
1562 				pd->pd_meta->disk_status = SII_S_CURRENT;
1563 			if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
1564 				pd->pd_meta->disk_number = sd->sd_pos;
1565 				pd->pd_meta->raid0_ident = 0xff;
1566 				pd->pd_meta->raid1_ident = 0;
1567 			} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
1568 				pd->pd_meta->disk_number = sd->sd_pos / meta->raid1_disks;
1569 				pd->pd_meta->raid0_ident = sd->sd_pos % meta->raid1_disks;
1570 				pd->pd_meta->raid1_ident = sd->sd_pos / meta->raid1_disks;
1571 			} else {
1572 				pd->pd_meta->disk_number = sd->sd_pos;
1573 				pd->pd_meta->raid0_ident = 0;
1574 				pd->pd_meta->raid1_ident = 0xff;
1575 			}
1576 		}
1577 		G_RAID_DEBUG(1, "Writing SiI metadata to %s",
1578 		    g_raid_get_diskname(disk));
1579 		g_raid_md_sii_print(pd->pd_meta);
1580 		sii_meta_write(disk->d_consumer, pd->pd_meta);
1581 	}
1582 	return (0);
1583 }
1584 
1585 static int
1586 g_raid_md_fail_disk_sii(struct g_raid_md_object *md,
1587     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1588 {
1589 	struct g_raid_softc *sc;
1590 	struct g_raid_md_sii_perdisk *pd;
1591 	struct g_raid_subdisk *sd;
1592 
1593 	sc = md->mdo_softc;
1594 	pd = (struct g_raid_md_sii_perdisk *)tdisk->d_md_data;
1595 
1596 	/* We can't fail disk that is not a part of array now. */
1597 	if (pd->pd_disk_pos < 0)
1598 		return (-1);
1599 
1600 	/*
1601 	 * Mark disk as failed in metadata and try to write that metadata
1602 	 * to the disk itself to prevent it's later resurrection as STALE.
1603 	 */
1604 	if (tdisk->d_consumer) {
1605 		if (pd->pd_meta) {
1606 			pd->pd_meta->disk_status = SII_S_REMOVED;
1607 			sii_meta_write(tdisk->d_consumer, pd->pd_meta);
1608 		} else
1609 			sii_meta_erase(tdisk->d_consumer);
1610 	}
1611 
1612 	/* Change states. */
1613 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
1614 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
1615 		g_raid_change_subdisk_state(sd,
1616 		    G_RAID_SUBDISK_S_FAILED);
1617 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
1618 		    G_RAID_EVENT_SUBDISK);
1619 	}
1620 
1621 	/* Write updated metadata to remaining disks. */
1622 	g_raid_md_write_sii(md, NULL, NULL, tdisk);
1623 
1624 	/* Check if anything left except placeholders. */
1625 	if (g_raid_ndisks(sc, -1) ==
1626 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1627 		g_raid_destroy_node(sc, 0);
1628 	else
1629 		g_raid_md_sii_refill(sc);
1630 	return (0);
1631 }
1632 
1633 static int
1634 g_raid_md_free_disk_sii(struct g_raid_md_object *md,
1635     struct g_raid_disk *disk)
1636 {
1637 	struct g_raid_md_sii_perdisk *pd;
1638 
1639 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
1640 	if (pd->pd_meta != NULL) {
1641 		free(pd->pd_meta, M_MD_SII);
1642 		pd->pd_meta = NULL;
1643 	}
1644 	free(pd, M_MD_SII);
1645 	disk->d_md_data = NULL;
1646 	return (0);
1647 }
1648 
1649 static int
1650 g_raid_md_free_sii(struct g_raid_md_object *md)
1651 {
1652 	struct g_raid_md_sii_object *mdi;
1653 
1654 	mdi = (struct g_raid_md_sii_object *)md;
1655 	if (!mdi->mdio_started) {
1656 		mdi->mdio_started = 0;
1657 		callout_stop(&mdi->mdio_start_co);
1658 		G_RAID_DEBUG1(1, md->mdo_softc,
1659 		    "root_mount_rel %p", mdi->mdio_rootmount);
1660 		root_mount_rel(mdi->mdio_rootmount);
1661 		mdi->mdio_rootmount = NULL;
1662 	}
1663 	if (mdi->mdio_meta != NULL) {
1664 		free(mdi->mdio_meta, M_MD_SII);
1665 		mdi->mdio_meta = NULL;
1666 	}
1667 	return (0);
1668 }
1669 
1670 G_RAID_MD_DECLARE(sii, "SiI");
1671