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