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