xref: /freebsd/sys/geom/raid/md_intel.c (revision 10b59a9b4add0320d52c15ce057dd697261e7dfc)
1 /*-
2  * Copyright (c) 2010 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_INTEL, "md_intel_data", "GEOM_RAID Intel metadata");
46 
47 struct intel_raid_map {
48 	uint32_t	offset;
49 	uint32_t	disk_sectors;
50 	uint32_t	stripe_count;
51 	uint16_t	strip_sectors;
52 	uint8_t		status;
53 #define INTEL_S_READY           0x00
54 #define INTEL_S_UNINITIALIZED   0x01
55 #define INTEL_S_DEGRADED        0x02
56 #define INTEL_S_FAILURE         0x03
57 
58 	uint8_t		type;
59 #define INTEL_T_RAID0           0x00
60 #define INTEL_T_RAID1           0x01
61 #define INTEL_T_RAID5           0x05
62 
63 	uint8_t		total_disks;
64 	uint8_t		total_domains;
65 	uint8_t		failed_disk_num;
66 	uint8_t		ddf;
67 	uint32_t	filler_2[7];
68 	uint32_t	disk_idx[1];	/* total_disks entries. */
69 #define INTEL_DI_IDX	0x00ffffff
70 #define INTEL_DI_RBLD	0x01000000
71 } __packed;
72 
73 struct intel_raid_vol {
74 	uint8_t		name[16];
75 	u_int64_t	total_sectors __packed;
76 	uint32_t	state;
77 #define INTEL_ST_BOOTABLE		0x00000001
78 #define INTEL_ST_BOOT_DEVICE		0x00000002
79 #define INTEL_ST_READ_COALESCING	0x00000004
80 #define INTEL_ST_WRITE_COALESCING	0x00000008
81 #define INTEL_ST_LAST_SHUTDOWN_DIRTY	0x00000010
82 #define INTEL_ST_HIDDEN_AT_BOOT		0x00000020
83 #define INTEL_ST_CURRENTLY_HIDDEN	0x00000040
84 #define INTEL_ST_VERIFY_AND_FIX		0x00000080
85 #define INTEL_ST_MAP_STATE_UNINIT	0x00000100
86 #define INTEL_ST_NO_AUTO_RECOVERY	0x00000200
87 #define INTEL_ST_CLONE_N_GO		0x00000400
88 #define INTEL_ST_CLONE_MAN_SYNC		0x00000800
89 #define INTEL_ST_CNG_MASTER_DISK_NUM	0x00001000
90 	uint32_t	reserved;
91 	uint8_t		migr_priority;
92 	uint8_t		num_sub_vols;
93 	uint8_t		tid;
94 	uint8_t		cng_master_disk;
95 	uint16_t	cache_policy;
96 	uint8_t		cng_state;
97 	uint8_t		cng_sub_state;
98 	uint32_t	filler_0[10];
99 
100 	uint32_t	curr_migr_unit;
101 	uint32_t	checkpoint_id;
102 	uint8_t		migr_state;
103 	uint8_t		migr_type;
104 #define INTEL_MT_INIT		0
105 #define INTEL_MT_REBUILD	1
106 #define INTEL_MT_VERIFY		2
107 #define INTEL_MT_GEN_MIGR	3
108 #define INTEL_MT_STATE_CHANGE	4
109 #define INTEL_MT_REPAIR		5
110 	uint8_t		dirty;
111 	uint8_t		fs_state;
112 	uint16_t	verify_errors;
113 	uint16_t	bad_blocks;
114 	uint32_t	filler_1[4];
115 	struct intel_raid_map map[1];	/* 2 entries if migr_state != 0. */
116 } __packed;
117 
118 struct intel_raid_disk {
119 #define INTEL_SERIAL_LEN	16
120 	uint8_t		serial[INTEL_SERIAL_LEN];
121 	uint32_t	sectors;
122 	uint32_t	id;
123 	uint32_t	flags;
124 #define INTEL_F_SPARE		0x01
125 #define INTEL_F_ASSIGNED	0x02
126 #define INTEL_F_FAILED		0x04
127 #define INTEL_F_ONLINE		0x08
128 
129 	uint32_t	filler[5];
130 } __packed;
131 
132 struct intel_raid_conf {
133 	uint8_t		intel_id[24];
134 #define INTEL_MAGIC             "Intel Raid ISM Cfg Sig. "
135 
136 	uint8_t		version[6];
137 #define INTEL_VERSION_1000	"1.0.00"	/* RAID0 */
138 #define INTEL_VERSION_1100	"1.1.00"	/* RAID1 */
139 #define INTEL_VERSION_1200	"1.2.00"	/* Many volumes */
140 #define INTEL_VERSION_1201	"1.2.01"	/* 3 or 4 disks */
141 #define INTEL_VERSION_1202	"1.2.02"	/* RAID5 */
142 #define INTEL_VERSION_1204	"1.2.04"	/* 5 or 6 disks */
143 #define INTEL_VERSION_1206	"1.2.06"	/* CNG */
144 #define INTEL_VERSION_1300	"1.3.00"	/* Attributes */
145 
146 	uint8_t		dummy_0[2];
147 	uint32_t	checksum;
148 	uint32_t	config_size;
149 	uint32_t	config_id;
150 	uint32_t	generation;
151 	uint32_t	error_log_size;
152 	uint32_t	attributes;
153 #define INTEL_ATTR_RAID0	0x00000001
154 #define INTEL_ATTR_RAID1	0x00000002
155 #define INTEL_ATTR_RAID10	0x00000004
156 #define INTEL_ATTR_RAID1E	0x00000008
157 #define INTEL_ATTR_RAID5	0x00000010
158 #define INTEL_ATTR_RAIDCNG	0x00000020
159 #define INTEL_ATTR_2TB		0x20000000
160 #define INTEL_ATTR_PM		0x40000000
161 #define INTEL_ATTR_CHECKSUM	0x80000000
162 
163 	uint8_t		total_disks;
164 	uint8_t		total_volumes;
165 	uint8_t		dummy_2[2];
166 	uint32_t	filler_0[39];
167 	struct intel_raid_disk	disk[1];	/* total_disks entries. */
168 	/* Here goes total_volumes of struct intel_raid_vol. */
169 } __packed;
170 
171 #define INTEL_MAX_MD_SIZE(ndisks)				\
172     (sizeof(struct intel_raid_conf) +				\
173      sizeof(struct intel_raid_disk) * (ndisks - 1) +		\
174      sizeof(struct intel_raid_vol) * 2 +			\
175      sizeof(struct intel_raid_map) * 2 +			\
176      sizeof(uint32_t) * (ndisks - 1) * 4)
177 
178 struct g_raid_md_intel_perdisk {
179 	struct intel_raid_conf	*pd_meta;
180 	int			 pd_disk_pos;
181 	struct intel_raid_disk	 pd_disk_meta;
182 };
183 
184 struct g_raid_md_intel_object {
185 	struct g_raid_md_object	 mdio_base;
186 	uint32_t		 mdio_config_id;
187 	uint32_t		 mdio_generation;
188 	struct intel_raid_conf	*mdio_meta;
189 	struct callout		 mdio_start_co;	/* STARTING state timer. */
190 	int			 mdio_disks_present;
191 	int			 mdio_started;
192 	int			 mdio_incomplete;
193 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
194 };
195 
196 static g_raid_md_create_t g_raid_md_create_intel;
197 static g_raid_md_taste_t g_raid_md_taste_intel;
198 static g_raid_md_event_t g_raid_md_event_intel;
199 static g_raid_md_ctl_t g_raid_md_ctl_intel;
200 static g_raid_md_write_t g_raid_md_write_intel;
201 static g_raid_md_fail_disk_t g_raid_md_fail_disk_intel;
202 static g_raid_md_free_disk_t g_raid_md_free_disk_intel;
203 static g_raid_md_free_t g_raid_md_free_intel;
204 
205 static kobj_method_t g_raid_md_intel_methods[] = {
206 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_intel),
207 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_intel),
208 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_intel),
209 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_intel),
210 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_intel),
211 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_intel),
212 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_intel),
213 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_intel),
214 	{ 0, 0 }
215 };
216 
217 static struct g_raid_md_class g_raid_md_intel_class = {
218 	"Intel",
219 	g_raid_md_intel_methods,
220 	sizeof(struct g_raid_md_intel_object),
221 	.mdc_priority = 100
222 };
223 
224 
225 static struct intel_raid_map *
226 intel_get_map(struct intel_raid_vol *mvol, int i)
227 {
228 	struct intel_raid_map *mmap;
229 
230 	if (i > (mvol->migr_state ? 1 : 0))
231 		return (NULL);
232 	mmap = &mvol->map[0];
233 	for (; i > 0; i--) {
234 		mmap = (struct intel_raid_map *)
235 		    &mmap->disk_idx[mmap->total_disks];
236 	}
237 	return ((struct intel_raid_map *)mmap);
238 }
239 
240 static struct intel_raid_vol *
241 intel_get_volume(struct intel_raid_conf *meta, int i)
242 {
243 	struct intel_raid_vol *mvol;
244 	struct intel_raid_map *mmap;
245 
246 	if (i > 1)
247 		return (NULL);
248 	mvol = (struct intel_raid_vol *)&meta->disk[meta->total_disks];
249 	for (; i > 0; i--) {
250 		mmap = intel_get_map(mvol, mvol->migr_state ? 1 : 0);
251 		mvol = (struct intel_raid_vol *)
252 		    &mmap->disk_idx[mmap->total_disks];
253 	}
254 	return (mvol);
255 }
256 
257 static void
258 g_raid_md_intel_print(struct intel_raid_conf *meta)
259 {
260 	struct intel_raid_vol *mvol;
261 	struct intel_raid_map *mmap;
262 	int i, j, k;
263 
264 	if (g_raid_debug < 1)
265 		return;
266 
267 	printf("********* ATA Intel MatrixRAID Metadata *********\n");
268 	printf("intel_id            <%.24s>\n", meta->intel_id);
269 	printf("version             <%.6s>\n", meta->version);
270 	printf("checksum            0x%08x\n", meta->checksum);
271 	printf("config_size         0x%08x\n", meta->config_size);
272 	printf("config_id           0x%08x\n", meta->config_id);
273 	printf("generation          0x%08x\n", meta->generation);
274 	printf("attributes          0x%08x\n", meta->attributes);
275 	printf("total_disks         %u\n", meta->total_disks);
276 	printf("total_volumes       %u\n", meta->total_volumes);
277 	printf("DISK#   serial disk_sectors disk_id flags\n");
278 	for (i = 0; i < meta->total_disks; i++ ) {
279 		printf("    %d   <%.16s> %u 0x%08x 0x%08x\n", i,
280 		    meta->disk[i].serial, meta->disk[i].sectors,
281 		    meta->disk[i].id, meta->disk[i].flags);
282 	}
283 	for (i = 0; i < meta->total_volumes; i++) {
284 		mvol = intel_get_volume(meta, i);
285 		printf(" ****** Volume %d ******\n", i);
286 		printf(" name               %.16s\n", mvol->name);
287 		printf(" total_sectors      %ju\n", mvol->total_sectors);
288 		printf(" state              %u\n", mvol->state);
289 		printf(" reserved           %u\n", mvol->reserved);
290 		printf(" curr_migr_unit     %u\n", mvol->curr_migr_unit);
291 		printf(" checkpoint_id      %u\n", mvol->checkpoint_id);
292 		printf(" migr_state         %u\n", mvol->migr_state);
293 		printf(" migr_type          %u\n", mvol->migr_type);
294 		printf(" dirty              %u\n", mvol->dirty);
295 
296 		for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) {
297 			printf("  *** Map %d ***\n", j);
298 			mmap = intel_get_map(mvol, j);
299 			printf("  offset            %u\n", mmap->offset);
300 			printf("  disk_sectors      %u\n", mmap->disk_sectors);
301 			printf("  stripe_count      %u\n", mmap->stripe_count);
302 			printf("  strip_sectors     %u\n", mmap->strip_sectors);
303 			printf("  status            %u\n", mmap->status);
304 			printf("  type              %u\n", mmap->type);
305 			printf("  total_disks       %u\n", mmap->total_disks);
306 			printf("  total_domains     %u\n", mmap->total_domains);
307 			printf("  failed_disk_num   %u\n", mmap->failed_disk_num);
308 			printf("  ddf               %u\n", mmap->ddf);
309 			printf("  disk_idx         ");
310 			for (k = 0; k < mmap->total_disks; k++)
311 				printf(" 0x%08x", mmap->disk_idx[k]);
312 			printf("\n");
313 		}
314 	}
315 	printf("=================================================\n");
316 }
317 
318 static struct intel_raid_conf *
319 intel_meta_copy(struct intel_raid_conf *meta)
320 {
321 	struct intel_raid_conf *nmeta;
322 
323 	nmeta = malloc(meta->config_size, M_MD_INTEL, M_WAITOK);
324 	memcpy(nmeta, meta, meta->config_size);
325 	return (nmeta);
326 }
327 
328 static int
329 intel_meta_find_disk(struct intel_raid_conf *meta, char *serial)
330 {
331 	int pos;
332 
333 	for (pos = 0; pos < meta->total_disks; pos++) {
334 		if (strncmp(meta->disk[pos].serial,
335 		    serial, INTEL_SERIAL_LEN) == 0)
336 			return (pos);
337 	}
338 	return (-1);
339 }
340 
341 static struct intel_raid_conf *
342 intel_meta_read(struct g_consumer *cp)
343 {
344 	struct g_provider *pp;
345 	struct intel_raid_conf *meta;
346 	struct intel_raid_vol *mvol;
347 	struct intel_raid_map *mmap;
348 	char *buf;
349 	int error, i, j, k, left, size;
350 	uint32_t checksum, *ptr;
351 
352 	pp = cp->provider;
353 
354 	/* Read the anchor sector. */
355 	buf = g_read_data(cp,
356 	    pp->mediasize - pp->sectorsize * 2, pp->sectorsize, &error);
357 	if (buf == NULL) {
358 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
359 		    pp->name, error);
360 		return (NULL);
361 	}
362 	meta = (struct intel_raid_conf *)buf;
363 
364 	/* Check if this is an Intel RAID struct */
365 	if (strncmp(meta->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
366 		G_RAID_DEBUG(1, "Intel signature check failed on %s", pp->name);
367 		g_free(buf);
368 		return (NULL);
369 	}
370 	if (meta->config_size > 65536 ||
371 	    meta->config_size < sizeof(struct intel_raid_conf)) {
372 		G_RAID_DEBUG(1, "Intel metadata size looks wrong: %d",
373 		    meta->config_size);
374 		g_free(buf);
375 		return (NULL);
376 	}
377 	size = meta->config_size;
378 	meta = malloc(size, M_MD_INTEL, M_WAITOK);
379 	memcpy(meta, buf, min(size, pp->sectorsize));
380 	g_free(buf);
381 
382 	/* Read all the rest, if needed. */
383 	if (meta->config_size > pp->sectorsize) {
384 		left = (meta->config_size - 1) / pp->sectorsize;
385 		buf = g_read_data(cp,
386 		    pp->mediasize - pp->sectorsize * (2 + left),
387 		    pp->sectorsize * left, &error);
388 		if (buf == NULL) {
389 			G_RAID_DEBUG(1, "Cannot read remaining metadata"
390 			    " part from %s (error=%d).",
391 			    pp->name, error);
392 			free(meta, M_MD_INTEL);
393 			return (NULL);
394 		}
395 		memcpy(((char *)meta) + pp->sectorsize, buf,
396 		    pp->sectorsize * left);
397 		g_free(buf);
398 	}
399 
400 	/* Check metadata checksum. */
401 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0;
402 	    i < (meta->config_size / sizeof(uint32_t)); i++) {
403 		checksum += *ptr++;
404 	}
405 	checksum -= meta->checksum;
406 	if (checksum != meta->checksum) {
407 		G_RAID_DEBUG(1, "Intel checksum check failed on %s", pp->name);
408 		free(meta, M_MD_INTEL);
409 		return (NULL);
410 	}
411 
412 	/* Validate metadata size. */
413 	size = sizeof(struct intel_raid_conf) +
414 	    sizeof(struct intel_raid_disk) * (meta->total_disks - 1) +
415 	    sizeof(struct intel_raid_vol) * meta->total_volumes;
416 	if (size > meta->config_size) {
417 badsize:
418 		G_RAID_DEBUG(1, "Intel metadata size incorrect %d < %d",
419 		    meta->config_size, size);
420 		free(meta, M_MD_INTEL);
421 		return (NULL);
422 	}
423 	for (i = 0; i < meta->total_volumes; i++) {
424 		mvol = intel_get_volume(meta, i);
425 		mmap = intel_get_map(mvol, 0);
426 		size += 4 * (mmap->total_disks - 1);
427 		if (size > meta->config_size)
428 			goto badsize;
429 		if (mvol->migr_state) {
430 			size += sizeof(struct intel_raid_map);
431 			if (size > meta->config_size)
432 				goto badsize;
433 			mmap = intel_get_map(mvol, 1);
434 			size += 4 * (mmap->total_disks - 1);
435 			if (size > meta->config_size)
436 				goto badsize;
437 		}
438 	}
439 
440 	/* Validate disk indexes. */
441 	for (i = 0; i < meta->total_volumes; i++) {
442 		mvol = intel_get_volume(meta, i);
443 		for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) {
444 			mmap = intel_get_map(mvol, j);
445 			for (k = 0; k < mmap->total_disks; k++) {
446 				if ((mmap->disk_idx[k] & INTEL_DI_IDX) >
447 				    meta->total_disks) {
448 					G_RAID_DEBUG(1, "Intel metadata disk"
449 					    " index %d too big (>%d)",
450 					    mmap->disk_idx[k] & INTEL_DI_IDX,
451 					    meta->total_disks);
452 					free(meta, M_MD_INTEL);
453 					return (NULL);
454 				}
455 			}
456 		}
457 	}
458 
459 	/* Validate migration types. */
460 	for (i = 0; i < meta->total_volumes; i++) {
461 		mvol = intel_get_volume(meta, i);
462 		if (mvol->migr_state &&
463 		    mvol->migr_type != INTEL_MT_INIT &&
464 		    mvol->migr_type != INTEL_MT_REBUILD &&
465 		    mvol->migr_type != INTEL_MT_VERIFY &&
466 		    mvol->migr_type != INTEL_MT_REPAIR) {
467 			G_RAID_DEBUG(1, "Intel metadata has unsupported"
468 			    " migration type %d", mvol->migr_type);
469 			free(meta, M_MD_INTEL);
470 			return (NULL);
471 		}
472 	}
473 
474 	return (meta);
475 }
476 
477 static int
478 intel_meta_write(struct g_consumer *cp, struct intel_raid_conf *meta)
479 {
480 	struct g_provider *pp;
481 	char *buf;
482 	int error, i, sectors;
483 	uint32_t checksum, *ptr;
484 
485 	pp = cp->provider;
486 
487 	/* Recalculate checksum for case if metadata were changed. */
488 	meta->checksum = 0;
489 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0;
490 	    i < (meta->config_size / sizeof(uint32_t)); i++) {
491 		checksum += *ptr++;
492 	}
493 	meta->checksum = checksum;
494 
495 	/* Create and fill buffer. */
496 	sectors = (meta->config_size + pp->sectorsize - 1) / pp->sectorsize;
497 	buf = malloc(sectors * pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO);
498 	if (sectors > 1) {
499 		memcpy(buf, ((char *)meta) + pp->sectorsize,
500 		    (sectors - 1) * pp->sectorsize);
501 	}
502 	memcpy(buf + (sectors - 1) * pp->sectorsize, meta, pp->sectorsize);
503 
504 	error = g_write_data(cp,
505 	    pp->mediasize - pp->sectorsize * (1 + sectors),
506 	    buf, pp->sectorsize * sectors);
507 	if (error != 0) {
508 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
509 		    pp->name, error);
510 	}
511 
512 	free(buf, M_MD_INTEL);
513 	return (error);
514 }
515 
516 static int
517 intel_meta_erase(struct g_consumer *cp)
518 {
519 	struct g_provider *pp;
520 	char *buf;
521 	int error;
522 
523 	pp = cp->provider;
524 	buf = malloc(pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO);
525 	error = g_write_data(cp,
526 	    pp->mediasize - 2 * pp->sectorsize,
527 	    buf, pp->sectorsize);
528 	if (error != 0) {
529 		G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
530 		    pp->name, error);
531 	}
532 	free(buf, M_MD_INTEL);
533 	return (error);
534 }
535 
536 static int
537 intel_meta_write_spare(struct g_consumer *cp, struct intel_raid_disk *d)
538 {
539 	struct intel_raid_conf *meta;
540 	int error;
541 
542 	/* Fill anchor and single disk. */
543 	meta = malloc(INTEL_MAX_MD_SIZE(1), M_MD_INTEL, M_WAITOK | M_ZERO);
544 	memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1);
545 	memcpy(&meta->version[0], INTEL_VERSION_1000,
546 	    sizeof(INTEL_VERSION_1000) - 1);
547 	meta->config_size = INTEL_MAX_MD_SIZE(1);
548 	meta->config_id = arc4random();
549 	meta->generation = 1;
550 	meta->total_disks = 1;
551 	meta->disk[0] = *d;
552 	error = intel_meta_write(cp, meta);
553 	free(meta, M_MD_INTEL);
554 	return (error);
555 }
556 
557 static struct g_raid_disk *
558 g_raid_md_intel_get_disk(struct g_raid_softc *sc, int id)
559 {
560 	struct g_raid_disk	*disk;
561 	struct g_raid_md_intel_perdisk *pd;
562 
563 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
564 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
565 		if (pd->pd_disk_pos == id)
566 			break;
567 	}
568 	return (disk);
569 }
570 
571 static int
572 g_raid_md_intel_supported(int level, int qual, int disks, int force)
573 {
574 
575 	switch (level) {
576 	case G_RAID_VOLUME_RL_RAID0:
577 		if (disks < 1)
578 			return (0);
579 		if (!force && (disks < 2 || disks > 6))
580 			return (0);
581 		break;
582 	case G_RAID_VOLUME_RL_RAID1:
583 		if (disks < 1)
584 			return (0);
585 		if (!force && (disks != 2))
586 			return (0);
587 		break;
588 	case G_RAID_VOLUME_RL_RAID1E:
589 		if (disks < 2)
590 			return (0);
591 		if (!force && (disks != 4))
592 			return (0);
593 		break;
594 	case G_RAID_VOLUME_RL_RAID5:
595 		if (disks < 3)
596 			return (0);
597 		if (!force && disks > 6)
598 			return (0);
599 		break;
600 	default:
601 		return (0);
602 	}
603 	if (qual != G_RAID_VOLUME_RLQ_NONE)
604 		return (0);
605 	return (1);
606 }
607 
608 static struct g_raid_volume *
609 g_raid_md_intel_get_volume(struct g_raid_softc *sc, int id)
610 {
611 	struct g_raid_volume	*mvol;
612 
613 	TAILQ_FOREACH(mvol, &sc->sc_volumes, v_next) {
614 		if ((intptr_t)(mvol->v_md_data) == id)
615 			break;
616 	}
617 	return (mvol);
618 }
619 
620 static int
621 g_raid_md_intel_start_disk(struct g_raid_disk *disk)
622 {
623 	struct g_raid_softc *sc;
624 	struct g_raid_subdisk *sd, *tmpsd;
625 	struct g_raid_disk *olddisk, *tmpdisk;
626 	struct g_raid_md_object *md;
627 	struct g_raid_md_intel_object *mdi;
628 	struct g_raid_md_intel_perdisk *pd, *oldpd;
629 	struct intel_raid_conf *meta;
630 	struct intel_raid_vol *mvol;
631 	struct intel_raid_map *mmap0, *mmap1;
632 	int disk_pos, resurrection = 0;
633 
634 	sc = disk->d_softc;
635 	md = sc->sc_md;
636 	mdi = (struct g_raid_md_intel_object *)md;
637 	meta = mdi->mdio_meta;
638 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
639 	olddisk = NULL;
640 
641 	/* Find disk position in metadata by it's serial. */
642 	disk_pos = intel_meta_find_disk(meta, pd->pd_disk_meta.serial);
643 	if (disk_pos < 0) {
644 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
645 		/* Failed stale disk is useless for us. */
646 		if (pd->pd_disk_meta.flags & INTEL_F_FAILED) {
647 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
648 			return (0);
649 		}
650 		/* If we are in the start process, that's all for now. */
651 		if (!mdi->mdio_started)
652 			goto nofit;
653 		/*
654 		 * If we have already started - try to get use of the disk.
655 		 * Try to replace OFFLINE disks first, then FAILED.
656 		 */
657 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
658 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
659 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
660 				continue;
661 			/* Make sure this disk is big enough. */
662 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
663 				if (sd->sd_offset + sd->sd_size + 4096 >
664 				    (off_t)pd->pd_disk_meta.sectors * 512) {
665 					G_RAID_DEBUG1(1, sc,
666 					    "Disk too small (%llu < %llu)",
667 					    ((unsigned long long)
668 					    pd->pd_disk_meta.sectors) * 512,
669 					    (unsigned long long)
670 					    sd->sd_offset + sd->sd_size + 4096);
671 					break;
672 				}
673 			}
674 			if (sd != NULL)
675 				continue;
676 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
677 				olddisk = tmpdisk;
678 				break;
679 			} else if (olddisk == NULL)
680 				olddisk = tmpdisk;
681 		}
682 		if (olddisk == NULL) {
683 nofit:
684 			if (pd->pd_disk_meta.flags & INTEL_F_SPARE) {
685 				g_raid_change_disk_state(disk,
686 				    G_RAID_DISK_S_SPARE);
687 				return (1);
688 			} else {
689 				g_raid_change_disk_state(disk,
690 				    G_RAID_DISK_S_STALE);
691 				return (0);
692 			}
693 		}
694 		oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data;
695 		disk_pos = oldpd->pd_disk_pos;
696 		resurrection = 1;
697 	}
698 
699 	if (olddisk == NULL) {
700 		/* Find placeholder by position. */
701 		olddisk = g_raid_md_intel_get_disk(sc, disk_pos);
702 		if (olddisk == NULL)
703 			panic("No disk at position %d!", disk_pos);
704 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
705 			G_RAID_DEBUG1(1, sc, "More then one disk for pos %d",
706 			    disk_pos);
707 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
708 			return (0);
709 		}
710 		oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data;
711 	}
712 
713 	/* Replace failed disk or placeholder with new disk. */
714 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
715 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
716 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
717 		sd->sd_disk = disk;
718 	}
719 	oldpd->pd_disk_pos = -2;
720 	pd->pd_disk_pos = disk_pos;
721 
722 	/* If it was placeholder -- destroy it. */
723 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
724 		g_raid_destroy_disk(olddisk);
725 	} else {
726 		/* Otherwise, make it STALE_FAILED. */
727 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
728 		/* Update global metadata just in case. */
729 		memcpy(&meta->disk[disk_pos], &pd->pd_disk_meta,
730 		    sizeof(struct intel_raid_disk));
731 	}
732 
733 	/* Welcome the new disk. */
734 	if (resurrection)
735 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
736 	else if (meta->disk[disk_pos].flags & INTEL_F_FAILED)
737 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
738 	else if (meta->disk[disk_pos].flags & INTEL_F_SPARE)
739 		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
740 	else
741 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
742 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
743 		mvol = intel_get_volume(meta,
744 		    (uintptr_t)(sd->sd_volume->v_md_data));
745 		mmap0 = intel_get_map(mvol, 0);
746 		if (mvol->migr_state)
747 			mmap1 = intel_get_map(mvol, 1);
748 		else
749 			mmap1 = mmap0;
750 
751 		if (resurrection) {
752 			/* Stale disk, almost same as new. */
753 			g_raid_change_subdisk_state(sd,
754 			    G_RAID_SUBDISK_S_NEW);
755 		} else if (meta->disk[disk_pos].flags & INTEL_F_FAILED) {
756 			/* Failed disk, almost useless. */
757 			g_raid_change_subdisk_state(sd,
758 			    G_RAID_SUBDISK_S_FAILED);
759 		} else if (mvol->migr_state == 0) {
760 			if (mmap0->status == INTEL_S_UNINITIALIZED) {
761 				/* Freshly created uninitialized volume. */
762 				g_raid_change_subdisk_state(sd,
763 				    G_RAID_SUBDISK_S_UNINITIALIZED);
764 			} else if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
765 				/* Freshly inserted disk. */
766 				g_raid_change_subdisk_state(sd,
767 				    G_RAID_SUBDISK_S_NEW);
768 			} else if (mvol->dirty) {
769 				/* Dirty volume (unclean shutdown). */
770 				g_raid_change_subdisk_state(sd,
771 				    G_RAID_SUBDISK_S_STALE);
772 			} else {
773 				/* Up to date disk. */
774 				g_raid_change_subdisk_state(sd,
775 				    G_RAID_SUBDISK_S_ACTIVE);
776 			}
777 		} else if (mvol->migr_type == INTEL_MT_INIT ||
778 			   mvol->migr_type == INTEL_MT_REBUILD) {
779 			if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
780 				/* Freshly inserted disk. */
781 				g_raid_change_subdisk_state(sd,
782 				    G_RAID_SUBDISK_S_NEW);
783 			} else if (mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
784 				/* Rebuilding disk. */
785 				g_raid_change_subdisk_state(sd,
786 				    G_RAID_SUBDISK_S_REBUILD);
787 				if (mvol->dirty) {
788 					sd->sd_rebuild_pos = 0;
789 				} else {
790 					sd->sd_rebuild_pos =
791 					    (off_t)mvol->curr_migr_unit *
792 					    sd->sd_volume->v_strip_size *
793 					    mmap0->total_domains;
794 				}
795 			} else if (mvol->dirty) {
796 				/* Dirty volume (unclean shutdown). */
797 				g_raid_change_subdisk_state(sd,
798 				    G_RAID_SUBDISK_S_STALE);
799 			} else {
800 				/* Up to date disk. */
801 				g_raid_change_subdisk_state(sd,
802 				    G_RAID_SUBDISK_S_ACTIVE);
803 			}
804 		} else if (mvol->migr_type == INTEL_MT_VERIFY ||
805 			   mvol->migr_type == INTEL_MT_REPAIR) {
806 			if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
807 				/* Freshly inserted disk. */
808 				g_raid_change_subdisk_state(sd,
809 				    G_RAID_SUBDISK_S_NEW);
810 			} else if (mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
811 				/* Resyncing disk. */
812 				g_raid_change_subdisk_state(sd,
813 				    G_RAID_SUBDISK_S_RESYNC);
814 				if (mvol->dirty) {
815 					sd->sd_rebuild_pos = 0;
816 				} else {
817 					sd->sd_rebuild_pos =
818 					    (off_t)mvol->curr_migr_unit *
819 					    sd->sd_volume->v_strip_size *
820 					    mmap0->total_domains;
821 				}
822 			} else if (mvol->dirty) {
823 				/* Dirty volume (unclean shutdown). */
824 				g_raid_change_subdisk_state(sd,
825 				    G_RAID_SUBDISK_S_STALE);
826 			} else {
827 				/* Up to date disk. */
828 				g_raid_change_subdisk_state(sd,
829 				    G_RAID_SUBDISK_S_ACTIVE);
830 			}
831 		}
832 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
833 		    G_RAID_EVENT_SUBDISK);
834 	}
835 
836 	/* Update status of our need for spare. */
837 	if (mdi->mdio_started) {
838 		mdi->mdio_incomplete =
839 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
840 		     meta->total_disks);
841 	}
842 
843 	return (resurrection);
844 }
845 
846 static void
847 g_disk_md_intel_retaste(void *arg, int pending)
848 {
849 
850 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
851 	g_retaste(&g_raid_class);
852 	free(arg, M_MD_INTEL);
853 }
854 
855 static void
856 g_raid_md_intel_refill(struct g_raid_softc *sc)
857 {
858 	struct g_raid_md_object *md;
859 	struct g_raid_md_intel_object *mdi;
860 	struct intel_raid_conf *meta;
861 	struct g_raid_disk *disk;
862 	struct task *task;
863 	int update, na;
864 
865 	md = sc->sc_md;
866 	mdi = (struct g_raid_md_intel_object *)md;
867 	meta = mdi->mdio_meta;
868 	update = 0;
869 	do {
870 		/* Make sure we miss anything. */
871 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
872 		if (na == meta->total_disks)
873 			break;
874 
875 		G_RAID_DEBUG1(1, md->mdo_softc,
876 		    "Array is not complete (%d of %d), "
877 		    "trying to refill.", na, meta->total_disks);
878 
879 		/* Try to get use some of STALE disks. */
880 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
881 			if (disk->d_state == G_RAID_DISK_S_STALE) {
882 				update += g_raid_md_intel_start_disk(disk);
883 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
884 					break;
885 			}
886 		}
887 		if (disk != NULL)
888 			continue;
889 
890 		/* Try to get use some of SPARE disks. */
891 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
892 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
893 				update += g_raid_md_intel_start_disk(disk);
894 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
895 					break;
896 			}
897 		}
898 	} while (disk != NULL);
899 
900 	/* Write new metadata if we changed something. */
901 	if (update) {
902 		g_raid_md_write_intel(md, NULL, NULL, NULL);
903 		meta = mdi->mdio_meta;
904 	}
905 
906 	/* Update status of our need for spare. */
907 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
908 	    meta->total_disks);
909 
910 	/* Request retaste hoping to find spare. */
911 	if (mdi->mdio_incomplete) {
912 		task = malloc(sizeof(struct task),
913 		    M_MD_INTEL, M_WAITOK | M_ZERO);
914 		TASK_INIT(task, 0, g_disk_md_intel_retaste, task);
915 		taskqueue_enqueue(taskqueue_swi, task);
916 	}
917 }
918 
919 static void
920 g_raid_md_intel_start(struct g_raid_softc *sc)
921 {
922 	struct g_raid_md_object *md;
923 	struct g_raid_md_intel_object *mdi;
924 	struct g_raid_md_intel_perdisk *pd;
925 	struct intel_raid_conf *meta;
926 	struct intel_raid_vol *mvol;
927 	struct intel_raid_map *mmap;
928 	struct g_raid_volume *vol;
929 	struct g_raid_subdisk *sd;
930 	struct g_raid_disk *disk;
931 	int i, j, disk_pos;
932 
933 	md = sc->sc_md;
934 	mdi = (struct g_raid_md_intel_object *)md;
935 	meta = mdi->mdio_meta;
936 
937 	/* Create volumes and subdisks. */
938 	for (i = 0; i < meta->total_volumes; i++) {
939 		mvol = intel_get_volume(meta, i);
940 		mmap = intel_get_map(mvol, 0);
941 		vol = g_raid_create_volume(sc, mvol->name, -1);
942 		vol->v_md_data = (void *)(intptr_t)i;
943 		if (mmap->type == INTEL_T_RAID0)
944 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
945 		else if (mmap->type == INTEL_T_RAID1 &&
946 		    mmap->total_domains >= 2 &&
947 		    mmap->total_domains <= mmap->total_disks) {
948 			/* Assume total_domains is correct. */
949 			if (mmap->total_domains == mmap->total_disks)
950 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
951 			else
952 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
953 		} else if (mmap->type == INTEL_T_RAID1) {
954 			/* total_domains looks wrong. */
955 			if (mmap->total_disks <= 2)
956 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
957 			else
958 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
959 		} else if (mmap->type == INTEL_T_RAID5)
960 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
961 		else
962 			vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
963 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
964 		vol->v_strip_size = (u_int)mmap->strip_sectors * 512; //ZZZ
965 		vol->v_disks_count = mmap->total_disks;
966 		vol->v_mediasize = (off_t)mvol->total_sectors * 512; //ZZZ
967 		vol->v_sectorsize = 512; //ZZZ
968 		for (j = 0; j < vol->v_disks_count; j++) {
969 			sd = &vol->v_subdisks[j];
970 			sd->sd_offset = (off_t)mmap->offset * 512; //ZZZ
971 			sd->sd_size = (off_t)mmap->disk_sectors * 512; //ZZZ
972 		}
973 		g_raid_start_volume(vol);
974 	}
975 
976 	/* Create disk placeholders to store data for later writing. */
977 	for (disk_pos = 0; disk_pos < meta->total_disks; disk_pos++) {
978 		pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
979 		pd->pd_disk_pos = disk_pos;
980 		pd->pd_disk_meta = meta->disk[disk_pos];
981 		disk = g_raid_create_disk(sc);
982 		disk->d_md_data = (void *)pd;
983 		disk->d_state = G_RAID_DISK_S_OFFLINE;
984 		for (i = 0; i < meta->total_volumes; i++) {
985 			mvol = intel_get_volume(meta, i);
986 			mmap = intel_get_map(mvol, 0);
987 			for (j = 0; j < mmap->total_disks; j++) {
988 				if ((mmap->disk_idx[j] & INTEL_DI_IDX) == disk_pos)
989 					break;
990 			}
991 			if (j == mmap->total_disks)
992 				continue;
993 			vol = g_raid_md_intel_get_volume(sc, i);
994 			sd = &vol->v_subdisks[j];
995 			sd->sd_disk = disk;
996 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
997 		}
998 	}
999 
1000 	/* Make all disks found till the moment take their places. */
1001 	do {
1002 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1003 			if (disk->d_state == G_RAID_DISK_S_NONE) {
1004 				g_raid_md_intel_start_disk(disk);
1005 				break;
1006 			}
1007 		}
1008 	} while (disk != NULL);
1009 
1010 	mdi->mdio_started = 1;
1011 	G_RAID_DEBUG1(0, sc, "Array started.");
1012 	g_raid_md_write_intel(md, NULL, NULL, NULL);
1013 
1014 	/* Pickup any STALE/SPARE disks to refill array if needed. */
1015 	g_raid_md_intel_refill(sc);
1016 
1017 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1018 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1019 		    G_RAID_EVENT_VOLUME);
1020 	}
1021 
1022 	callout_stop(&mdi->mdio_start_co);
1023 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
1024 	root_mount_rel(mdi->mdio_rootmount);
1025 	mdi->mdio_rootmount = NULL;
1026 }
1027 
1028 static void
1029 g_raid_md_intel_new_disk(struct g_raid_disk *disk)
1030 {
1031 	struct g_raid_softc *sc;
1032 	struct g_raid_md_object *md;
1033 	struct g_raid_md_intel_object *mdi;
1034 	struct intel_raid_conf *pdmeta;
1035 	struct g_raid_md_intel_perdisk *pd;
1036 
1037 	sc = disk->d_softc;
1038 	md = sc->sc_md;
1039 	mdi = (struct g_raid_md_intel_object *)md;
1040 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1041 	pdmeta = pd->pd_meta;
1042 
1043 	if (mdi->mdio_started) {
1044 		if (g_raid_md_intel_start_disk(disk))
1045 			g_raid_md_write_intel(md, NULL, NULL, NULL);
1046 	} else {
1047 		/* If we haven't started yet - check metadata freshness. */
1048 		if (mdi->mdio_meta == NULL ||
1049 		    ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) {
1050 			G_RAID_DEBUG1(1, sc, "Newer disk");
1051 			if (mdi->mdio_meta != NULL)
1052 				free(mdi->mdio_meta, M_MD_INTEL);
1053 			mdi->mdio_meta = intel_meta_copy(pdmeta);
1054 			mdi->mdio_generation = mdi->mdio_meta->generation;
1055 			mdi->mdio_disks_present = 1;
1056 		} else if (pdmeta->generation == mdi->mdio_generation) {
1057 			mdi->mdio_disks_present++;
1058 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
1059 			    mdi->mdio_disks_present,
1060 			    mdi->mdio_meta->total_disks);
1061 		} else {
1062 			G_RAID_DEBUG1(1, sc, "Older disk");
1063 		}
1064 		/* If we collected all needed disks - start array. */
1065 		if (mdi->mdio_disks_present == mdi->mdio_meta->total_disks)
1066 			g_raid_md_intel_start(sc);
1067 	}
1068 }
1069 
1070 static void
1071 g_raid_intel_go(void *arg)
1072 {
1073 	struct g_raid_softc *sc;
1074 	struct g_raid_md_object *md;
1075 	struct g_raid_md_intel_object *mdi;
1076 
1077 	sc = arg;
1078 	md = sc->sc_md;
1079 	mdi = (struct g_raid_md_intel_object *)md;
1080 	if (!mdi->mdio_started) {
1081 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
1082 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
1083 	}
1084 }
1085 
1086 static int
1087 g_raid_md_create_intel(struct g_raid_md_object *md, struct g_class *mp,
1088     struct g_geom **gp)
1089 {
1090 	struct g_raid_softc *sc;
1091 	struct g_raid_md_intel_object *mdi;
1092 	char name[16];
1093 
1094 	mdi = (struct g_raid_md_intel_object *)md;
1095 	mdi->mdio_config_id = arc4random();
1096 	mdi->mdio_generation = 0;
1097 	snprintf(name, sizeof(name), "Intel-%08x", mdi->mdio_config_id);
1098 	sc = g_raid_create_node(mp, name, md);
1099 	if (sc == NULL)
1100 		return (G_RAID_MD_TASTE_FAIL);
1101 	md->mdo_softc = sc;
1102 	*gp = sc->sc_geom;
1103 	return (G_RAID_MD_TASTE_NEW);
1104 }
1105 
1106 /*
1107  * Return the last N characters of the serial label.  The Linux and
1108  * ataraid(7) code always uses the last 16 characters of the label to
1109  * store into the Intel meta format.  Generalize this to N characters
1110  * since that's easy.  Labels can be up to 20 characters for SATA drives
1111  * and up 251 characters for SAS drives.  Since intel controllers don't
1112  * support SAS drives, just stick with the SATA limits for stack friendliness.
1113  */
1114 static int
1115 g_raid_md_get_label(struct g_consumer *cp, char *serial, int serlen)
1116 {
1117 	char serial_buffer[24];
1118 	int len, error;
1119 
1120 	len = sizeof(serial_buffer);
1121 	error = g_io_getattr("GEOM::ident", cp, &len, serial_buffer);
1122 	if (error != 0)
1123 		return (error);
1124 	len = strlen(serial_buffer);
1125 	if (len > serlen)
1126 		len -= serlen;
1127 	else
1128 		len = 0;
1129 	strncpy(serial, serial_buffer + len, serlen);
1130 	return (0);
1131 }
1132 
1133 static int
1134 g_raid_md_taste_intel(struct g_raid_md_object *md, struct g_class *mp,
1135                               struct g_consumer *cp, struct g_geom **gp)
1136 {
1137 	struct g_consumer *rcp;
1138 	struct g_provider *pp;
1139 	struct g_raid_md_intel_object *mdi, *mdi1;
1140 	struct g_raid_softc *sc;
1141 	struct g_raid_disk *disk;
1142 	struct intel_raid_conf *meta;
1143 	struct g_raid_md_intel_perdisk *pd;
1144 	struct g_geom *geom;
1145 	int error, disk_pos, result, spare, len;
1146 	char serial[INTEL_SERIAL_LEN];
1147 	char name[16];
1148 	uint16_t vendor;
1149 
1150 	G_RAID_DEBUG(1, "Tasting Intel on %s", cp->provider->name);
1151 	mdi = (struct g_raid_md_intel_object *)md;
1152 	pp = cp->provider;
1153 
1154 	/* Read metadata from device. */
1155 	meta = NULL;
1156 	vendor = 0xffff;
1157 	disk_pos = 0;
1158 	if (g_access(cp, 1, 0, 0) != 0)
1159 		return (G_RAID_MD_TASTE_FAIL);
1160 	g_topology_unlock();
1161 	error = g_raid_md_get_label(cp, serial, sizeof(serial));
1162 	if (error != 0) {
1163 		G_RAID_DEBUG(1, "Cannot get serial number from %s (error=%d).",
1164 		    pp->name, error);
1165 		goto fail2;
1166 	}
1167 	len = 2;
1168 	if (pp->geom->rank == 1)
1169 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
1170 	meta = intel_meta_read(cp);
1171 	g_topology_lock();
1172 	g_access(cp, -1, 0, 0);
1173 	if (meta == NULL) {
1174 		if (g_raid_aggressive_spare) {
1175 			if (vendor != 0x8086) {
1176 				G_RAID_DEBUG(1,
1177 				    "Intel vendor mismatch 0x%04x != 0x8086",
1178 				    vendor);
1179 			} else if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
1180 				G_RAID_DEBUG(1,
1181 				    "Intel disk '%s' is too big.", pp->name);
1182 			} else {
1183 				G_RAID_DEBUG(1,
1184 				    "No Intel metadata, forcing spare.");
1185 				spare = 2;
1186 				goto search;
1187 			}
1188 		}
1189 		return (G_RAID_MD_TASTE_FAIL);
1190 	}
1191 
1192 	/* Check this disk position in obtained metadata. */
1193 	disk_pos = intel_meta_find_disk(meta, serial);
1194 	if (disk_pos < 0) {
1195 		G_RAID_DEBUG(1, "Intel serial '%s' not found", serial);
1196 		goto fail1;
1197 	}
1198 	if (meta->disk[disk_pos].sectors !=
1199 	    (pp->mediasize / pp->sectorsize)) {
1200 		G_RAID_DEBUG(1, "Intel size mismatch %ju != %ju",
1201 		    (off_t)meta->disk[disk_pos].sectors,
1202 		    (off_t)(pp->mediasize / pp->sectorsize));
1203 		goto fail1;
1204 	}
1205 
1206 	/* Metadata valid. Print it. */
1207 	g_raid_md_intel_print(meta);
1208 	G_RAID_DEBUG(1, "Intel disk position %d", disk_pos);
1209 	spare = meta->disk[disk_pos].flags & INTEL_F_SPARE;
1210 
1211 search:
1212 	/* Search for matching node. */
1213 	sc = NULL;
1214 	mdi1 = NULL;
1215 	LIST_FOREACH(geom, &mp->geom, geom) {
1216 		sc = geom->softc;
1217 		if (sc == NULL)
1218 			continue;
1219 		if (sc->sc_stopping != 0)
1220 			continue;
1221 		if (sc->sc_md->mdo_class != md->mdo_class)
1222 			continue;
1223 		mdi1 = (struct g_raid_md_intel_object *)sc->sc_md;
1224 		if (spare) {
1225 			if (mdi1->mdio_incomplete)
1226 				break;
1227 		} else {
1228 			if (mdi1->mdio_config_id == meta->config_id)
1229 				break;
1230 		}
1231 	}
1232 
1233 	/* Found matching node. */
1234 	if (geom != NULL) {
1235 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
1236 		result = G_RAID_MD_TASTE_EXISTING;
1237 
1238 	} else if (spare) { /* Not found needy node -- left for later. */
1239 		G_RAID_DEBUG(1, "Spare is not needed at this time");
1240 		goto fail1;
1241 
1242 	} else { /* Not found matching node -- create one. */
1243 		result = G_RAID_MD_TASTE_NEW;
1244 		mdi->mdio_config_id = meta->config_id;
1245 		snprintf(name, sizeof(name), "Intel-%08x", meta->config_id);
1246 		sc = g_raid_create_node(mp, name, md);
1247 		md->mdo_softc = sc;
1248 		geom = sc->sc_geom;
1249 		callout_init(&mdi->mdio_start_co, 1);
1250 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
1251 		    g_raid_intel_go, sc);
1252 		mdi->mdio_rootmount = root_mount_hold("GRAID-Intel");
1253 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
1254 	}
1255 
1256 	rcp = g_new_consumer(geom);
1257 	g_attach(rcp, pp);
1258 	if (g_access(rcp, 1, 1, 1) != 0)
1259 		; //goto fail1;
1260 
1261 	g_topology_unlock();
1262 	sx_xlock(&sc->sc_lock);
1263 
1264 	pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1265 	pd->pd_meta = meta;
1266 	pd->pd_disk_pos = -1;
1267 	if (spare == 2) {
1268 		memcpy(&pd->pd_disk_meta.serial[0], serial, INTEL_SERIAL_LEN);
1269 		pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
1270 		pd->pd_disk_meta.id = 0;
1271 		pd->pd_disk_meta.flags = INTEL_F_SPARE;
1272 	} else {
1273 		pd->pd_disk_meta = meta->disk[disk_pos];
1274 	}
1275 	disk = g_raid_create_disk(sc);
1276 	disk->d_md_data = (void *)pd;
1277 	disk->d_consumer = rcp;
1278 	rcp->private = disk;
1279 
1280 	/* Read kernel dumping information. */
1281 	disk->d_kd.offset = 0;
1282 	disk->d_kd.length = OFF_MAX;
1283 	len = sizeof(disk->d_kd);
1284 	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
1285 	if (disk->d_kd.di.dumper == NULL)
1286 		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
1287 		    rcp->provider->name, error);
1288 
1289 	g_raid_md_intel_new_disk(disk);
1290 
1291 	sx_xunlock(&sc->sc_lock);
1292 	g_topology_lock();
1293 	*gp = geom;
1294 	return (result);
1295 fail2:
1296 	g_topology_lock();
1297 	g_access(cp, -1, 0, 0);
1298 fail1:
1299 	free(meta, M_MD_INTEL);
1300 	return (G_RAID_MD_TASTE_FAIL);
1301 }
1302 
1303 static int
1304 g_raid_md_event_intel(struct g_raid_md_object *md,
1305     struct g_raid_disk *disk, u_int event)
1306 {
1307 	struct g_raid_softc *sc;
1308 	struct g_raid_subdisk *sd;
1309 	struct g_raid_md_intel_object *mdi;
1310 	struct g_raid_md_intel_perdisk *pd;
1311 
1312 	sc = md->mdo_softc;
1313 	mdi = (struct g_raid_md_intel_object *)md;
1314 	if (disk == NULL) {
1315 		switch (event) {
1316 		case G_RAID_NODE_E_START:
1317 			if (!mdi->mdio_started)
1318 				g_raid_md_intel_start(sc);
1319 			return (0);
1320 		}
1321 		return (-1);
1322 	}
1323 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1324 	switch (event) {
1325 	case G_RAID_DISK_E_DISCONNECTED:
1326 		/* If disk was assigned, just update statuses. */
1327 		if (pd->pd_disk_pos >= 0) {
1328 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1329 			if (disk->d_consumer) {
1330 				g_raid_kill_consumer(sc, disk->d_consumer);
1331 				disk->d_consumer = NULL;
1332 			}
1333 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1334 				g_raid_change_subdisk_state(sd,
1335 				    G_RAID_SUBDISK_S_NONE);
1336 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1337 				    G_RAID_EVENT_SUBDISK);
1338 			}
1339 		} else {
1340 			/* Otherwise -- delete. */
1341 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1342 			g_raid_destroy_disk(disk);
1343 		}
1344 
1345 		/* Write updated metadata to all disks. */
1346 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1347 
1348 		/* Check if anything left except placeholders. */
1349 		if (g_raid_ndisks(sc, -1) ==
1350 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1351 			g_raid_destroy_node(sc, 0);
1352 		else
1353 			g_raid_md_intel_refill(sc);
1354 		return (0);
1355 	}
1356 	return (-2);
1357 }
1358 
1359 static int
1360 g_raid_md_ctl_intel(struct g_raid_md_object *md,
1361     struct gctl_req *req)
1362 {
1363 	struct g_raid_softc *sc;
1364 	struct g_raid_volume *vol, *vol1;
1365 	struct g_raid_subdisk *sd;
1366 	struct g_raid_disk *disk;
1367 	struct g_raid_md_intel_object *mdi;
1368 	struct g_raid_md_intel_perdisk *pd;
1369 	struct g_consumer *cp;
1370 	struct g_provider *pp;
1371 	char arg[16], serial[INTEL_SERIAL_LEN];
1372 	const char *verb, *volname, *levelname, *diskname;
1373 	char *tmp;
1374 	int *nargs, *force;
1375 	off_t off, size, sectorsize, strip;
1376 	intmax_t *sizearg, *striparg;
1377 	int numdisks, i, len, level, qual, update;
1378 	int error;
1379 
1380 	sc = md->mdo_softc;
1381 	mdi = (struct g_raid_md_intel_object *)md;
1382 	verb = gctl_get_param(req, "verb", NULL);
1383 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1384 	error = 0;
1385 	if (strcmp(verb, "label") == 0) {
1386 
1387 		if (*nargs < 4) {
1388 			gctl_error(req, "Invalid number of arguments.");
1389 			return (-1);
1390 		}
1391 		volname = gctl_get_asciiparam(req, "arg1");
1392 		if (volname == NULL) {
1393 			gctl_error(req, "No volume name.");
1394 			return (-2);
1395 		}
1396 		levelname = gctl_get_asciiparam(req, "arg2");
1397 		if (levelname == NULL) {
1398 			gctl_error(req, "No RAID level.");
1399 			return (-3);
1400 		}
1401 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1402 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1403 			return (-4);
1404 		}
1405 		numdisks = *nargs - 3;
1406 		force = gctl_get_paraml(req, "force", sizeof(*force));
1407 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1408 		    force ? *force : 0)) {
1409 			gctl_error(req, "Unsupported RAID level "
1410 			    "(0x%02x/0x%02x), or number of disks (%d).",
1411 			    level, qual, numdisks);
1412 			return (-5);
1413 		}
1414 
1415 		/* Search for disks, connect them and probe. */
1416 		size = 0x7fffffffffffffffllu;
1417 		sectorsize = 0;
1418 		for (i = 0; i < numdisks; i++) {
1419 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1420 			diskname = gctl_get_asciiparam(req, arg);
1421 			if (diskname == NULL) {
1422 				gctl_error(req, "No disk name (%s).", arg);
1423 				error = -6;
1424 				break;
1425 			}
1426 			if (strcmp(diskname, "NONE") == 0) {
1427 				cp = NULL;
1428 				pp = NULL;
1429 			} else {
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 = -7;
1437 					break;
1438 				}
1439 				pp = cp->provider;
1440 			}
1441 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1442 			pd->pd_disk_pos = i;
1443 			disk = g_raid_create_disk(sc);
1444 			disk->d_md_data = (void *)pd;
1445 			disk->d_consumer = cp;
1446 			if (cp == NULL) {
1447 				strcpy(&pd->pd_disk_meta.serial[0], "NONE");
1448 				pd->pd_disk_meta.id = 0xffffffff;
1449 				pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
1450 				continue;
1451 			}
1452 			cp->private = disk;
1453 			g_topology_unlock();
1454 
1455 			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
1456 				gctl_error(req,
1457 				    "Disk '%s' is too big.", diskname);
1458 				error = -8;
1459 				break;
1460 			}
1461 
1462 			error = g_raid_md_get_label(cp,
1463 			    &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN);
1464 			if (error != 0) {
1465 				gctl_error(req,
1466 				    "Can't get serial for provider '%s'.",
1467 				    diskname);
1468 				error = -8;
1469 				break;
1470 			}
1471 
1472 			/* Read kernel dumping information. */
1473 			disk->d_kd.offset = 0;
1474 			disk->d_kd.length = OFF_MAX;
1475 			len = sizeof(disk->d_kd);
1476 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1477 			if (disk->d_kd.di.dumper == NULL)
1478 				G_RAID_DEBUG1(2, sc,
1479 				    "Dumping not supported by %s.",
1480 				    cp->provider->name);
1481 
1482 			pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
1483 			if (size > pp->mediasize)
1484 				size = pp->mediasize;
1485 			if (sectorsize < pp->sectorsize)
1486 				sectorsize = pp->sectorsize;
1487 			pd->pd_disk_meta.id = 0;
1488 			pd->pd_disk_meta.flags = INTEL_F_ASSIGNED | INTEL_F_ONLINE;
1489 		}
1490 		if (error != 0)
1491 			return (error);
1492 
1493 		if (sectorsize <= 0) {
1494 			gctl_error(req, "Can't get sector size.");
1495 			return (-8);
1496 		}
1497 
1498 		/* Reserve some space for metadata. */
1499 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1500 
1501 		/* Handle size argument. */
1502 		len = sizeof(*sizearg);
1503 		sizearg = gctl_get_param(req, "size", &len);
1504 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1505 		    *sizearg > 0) {
1506 			if (*sizearg > size) {
1507 				gctl_error(req, "Size too big %lld > %lld.",
1508 				    (long long)*sizearg, (long long)size);
1509 				return (-9);
1510 			}
1511 			size = *sizearg;
1512 		}
1513 
1514 		/* Handle strip argument. */
1515 		strip = 131072;
1516 		len = sizeof(*striparg);
1517 		striparg = gctl_get_param(req, "strip", &len);
1518 		if (striparg != NULL && len == sizeof(*striparg) &&
1519 		    *striparg > 0) {
1520 			if (*striparg < sectorsize) {
1521 				gctl_error(req, "Strip size too small.");
1522 				return (-10);
1523 			}
1524 			if (*striparg % sectorsize != 0) {
1525 				gctl_error(req, "Incorrect strip size.");
1526 				return (-11);
1527 			}
1528 			if (strip > 65535 * sectorsize) {
1529 				gctl_error(req, "Strip size too big.");
1530 				return (-12);
1531 			}
1532 			strip = *striparg;
1533 		}
1534 
1535 		/* Round size down to strip or sector. */
1536 		if (level == G_RAID_VOLUME_RL_RAID1)
1537 			size -= (size % sectorsize);
1538 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1539 		    (numdisks & 1) != 0)
1540 			size -= (size % (2 * strip));
1541 		else
1542 			size -= (size % strip);
1543 		if (size <= 0) {
1544 			gctl_error(req, "Size too small.");
1545 			return (-13);
1546 		}
1547 		if (size > 0xffffffffllu * sectorsize) {
1548 			gctl_error(req, "Size too big.");
1549 			return (-14);
1550 		}
1551 
1552 		/* We have all we need, create things: volume, ... */
1553 		mdi->mdio_started = 1;
1554 		vol = g_raid_create_volume(sc, volname, -1);
1555 		vol->v_md_data = (void *)(intptr_t)0;
1556 		vol->v_raid_level = level;
1557 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
1558 		vol->v_strip_size = strip;
1559 		vol->v_disks_count = numdisks;
1560 		if (level == G_RAID_VOLUME_RL_RAID0)
1561 			vol->v_mediasize = size * numdisks;
1562 		else if (level == G_RAID_VOLUME_RL_RAID1)
1563 			vol->v_mediasize = size;
1564 		else if (level == G_RAID_VOLUME_RL_RAID5)
1565 			vol->v_mediasize = size * (numdisks - 1);
1566 		else { /* RAID1E */
1567 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1568 			    strip;
1569 		}
1570 		vol->v_sectorsize = sectorsize;
1571 		g_raid_start_volume(vol);
1572 
1573 		/* , and subdisks. */
1574 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1575 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1576 			sd = &vol->v_subdisks[pd->pd_disk_pos];
1577 			sd->sd_disk = disk;
1578 			sd->sd_offset = 0;
1579 			sd->sd_size = size;
1580 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1581 			if (sd->sd_disk->d_consumer != NULL) {
1582 				g_raid_change_disk_state(disk,
1583 				    G_RAID_DISK_S_ACTIVE);
1584 				g_raid_change_subdisk_state(sd,
1585 				    G_RAID_SUBDISK_S_ACTIVE);
1586 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1587 				    G_RAID_EVENT_SUBDISK);
1588 			} else {
1589 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1590 			}
1591 		}
1592 
1593 		/* Write metadata based on created entities. */
1594 		G_RAID_DEBUG1(0, sc, "Array started.");
1595 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1596 
1597 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1598 		g_raid_md_intel_refill(sc);
1599 
1600 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1601 		    G_RAID_EVENT_VOLUME);
1602 		return (0);
1603 	}
1604 	if (strcmp(verb, "add") == 0) {
1605 
1606 		if (*nargs != 3) {
1607 			gctl_error(req, "Invalid number of arguments.");
1608 			return (-1);
1609 		}
1610 		volname = gctl_get_asciiparam(req, "arg1");
1611 		if (volname == NULL) {
1612 			gctl_error(req, "No volume name.");
1613 			return (-2);
1614 		}
1615 		levelname = gctl_get_asciiparam(req, "arg2");
1616 		if (levelname == NULL) {
1617 			gctl_error(req, "No RAID level.");
1618 			return (-3);
1619 		}
1620 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1621 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1622 			return (-4);
1623 		}
1624 
1625 		/* Look for existing volumes. */
1626 		i = 0;
1627 		vol1 = NULL;
1628 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1629 			vol1 = vol;
1630 			i++;
1631 		}
1632 		if (i > 1) {
1633 			gctl_error(req, "Maximum two volumes supported.");
1634 			return (-6);
1635 		}
1636 		if (vol1 == NULL) {
1637 			gctl_error(req, "At least one volume must exist.");
1638 			return (-7);
1639 		}
1640 
1641 		numdisks = vol1->v_disks_count;
1642 		force = gctl_get_paraml(req, "force", sizeof(*force));
1643 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1644 		    force ? *force : 0)) {
1645 			gctl_error(req, "Unsupported RAID level "
1646 			    "(0x%02x/0x%02x), or number of disks (%d).",
1647 			    level, qual, numdisks);
1648 			return (-5);
1649 		}
1650 
1651 		/* Collect info about present disks. */
1652 		size = 0x7fffffffffffffffllu;
1653 		sectorsize = 512;
1654 		for (i = 0; i < numdisks; i++) {
1655 			disk = vol1->v_subdisks[i].sd_disk;
1656 			pd = (struct g_raid_md_intel_perdisk *)
1657 			    disk->d_md_data;
1658 			if ((off_t)pd->pd_disk_meta.sectors * 512 < size)
1659 				size = (off_t)pd->pd_disk_meta.sectors * 512;
1660 			if (disk->d_consumer != NULL &&
1661 			    disk->d_consumer->provider != NULL &&
1662 			    disk->d_consumer->provider->sectorsize >
1663 			     sectorsize) {
1664 				sectorsize =
1665 				    disk->d_consumer->provider->sectorsize;
1666 			}
1667 		}
1668 
1669 		/* Reserve some space for metadata. */
1670 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1671 
1672 		/* Decide insert before or after. */
1673 		sd = &vol1->v_subdisks[0];
1674 		if (sd->sd_offset >
1675 		    size - (sd->sd_offset + sd->sd_size)) {
1676 			off = 0;
1677 			size = sd->sd_offset;
1678 		} else {
1679 			off = sd->sd_offset + sd->sd_size;
1680 			size = size - (sd->sd_offset + sd->sd_size);
1681 		}
1682 
1683 		/* Handle strip argument. */
1684 		strip = 131072;
1685 		len = sizeof(*striparg);
1686 		striparg = gctl_get_param(req, "strip", &len);
1687 		if (striparg != NULL && len == sizeof(*striparg) &&
1688 		    *striparg > 0) {
1689 			if (*striparg < sectorsize) {
1690 				gctl_error(req, "Strip size too small.");
1691 				return (-10);
1692 			}
1693 			if (*striparg % sectorsize != 0) {
1694 				gctl_error(req, "Incorrect strip size.");
1695 				return (-11);
1696 			}
1697 			if (strip > 65535 * sectorsize) {
1698 				gctl_error(req, "Strip size too big.");
1699 				return (-12);
1700 			}
1701 			strip = *striparg;
1702 		}
1703 
1704 		/* Round offset up to strip. */
1705 		if (off % strip != 0) {
1706 			size -= strip - off % strip;
1707 			off += strip - off % strip;
1708 		}
1709 
1710 		/* Handle size argument. */
1711 		len = sizeof(*sizearg);
1712 		sizearg = gctl_get_param(req, "size", &len);
1713 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1714 		    *sizearg > 0) {
1715 			if (*sizearg > size) {
1716 				gctl_error(req, "Size too big %lld > %lld.",
1717 				    (long long)*sizearg, (long long)size);
1718 				return (-9);
1719 			}
1720 			size = *sizearg;
1721 		}
1722 
1723 		/* Round size down to strip or sector. */
1724 		if (level == G_RAID_VOLUME_RL_RAID1)
1725 			size -= (size % sectorsize);
1726 		else
1727 			size -= (size % strip);
1728 		if (size <= 0) {
1729 			gctl_error(req, "Size too small.");
1730 			return (-13);
1731 		}
1732 		if (size > 0xffffffffllu * sectorsize) {
1733 			gctl_error(req, "Size too big.");
1734 			return (-14);
1735 		}
1736 
1737 		/* We have all we need, create things: volume, ... */
1738 		vol = g_raid_create_volume(sc, volname, -1);
1739 		vol->v_md_data = (void *)(intptr_t)i;
1740 		vol->v_raid_level = level;
1741 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
1742 		vol->v_strip_size = strip;
1743 		vol->v_disks_count = numdisks;
1744 		if (level == G_RAID_VOLUME_RL_RAID0)
1745 			vol->v_mediasize = size * numdisks;
1746 		else if (level == G_RAID_VOLUME_RL_RAID1)
1747 			vol->v_mediasize = size;
1748 		else if (level == G_RAID_VOLUME_RL_RAID5)
1749 			vol->v_mediasize = size * (numdisks - 1);
1750 		else { /* RAID1E */
1751 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1752 			    strip;
1753 		}
1754 		vol->v_sectorsize = sectorsize;
1755 		g_raid_start_volume(vol);
1756 
1757 		/* , and subdisks. */
1758 		for (i = 0; i < numdisks; i++) {
1759 			disk = vol1->v_subdisks[i].sd_disk;
1760 			sd = &vol->v_subdisks[i];
1761 			sd->sd_disk = disk;
1762 			sd->sd_offset = off;
1763 			sd->sd_size = size;
1764 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1765 			if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
1766 				g_raid_change_subdisk_state(sd,
1767 				    G_RAID_SUBDISK_S_ACTIVE);
1768 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1769 				    G_RAID_EVENT_SUBDISK);
1770 			}
1771 		}
1772 
1773 		/* Write metadata based on created entities. */
1774 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1775 
1776 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1777 		    G_RAID_EVENT_VOLUME);
1778 		return (0);
1779 	}
1780 	if (strcmp(verb, "delete") == 0) {
1781 
1782 		/* Full node destruction. */
1783 		if (*nargs == 1) {
1784 			/* Check if some volume is still open. */
1785 			force = gctl_get_paraml(req, "force", sizeof(*force));
1786 			if (force != NULL && *force == 0 &&
1787 			    g_raid_nopens(sc) != 0) {
1788 				gctl_error(req, "Some volume is still open.");
1789 				return (-4);
1790 			}
1791 
1792 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1793 				if (disk->d_consumer)
1794 					intel_meta_erase(disk->d_consumer);
1795 			}
1796 			g_raid_destroy_node(sc, 0);
1797 			return (0);
1798 		}
1799 
1800 		/* Destroy specified volume. If it was last - all node. */
1801 		if (*nargs != 2) {
1802 			gctl_error(req, "Invalid number of arguments.");
1803 			return (-1);
1804 		}
1805 		volname = gctl_get_asciiparam(req, "arg1");
1806 		if (volname == NULL) {
1807 			gctl_error(req, "No volume name.");
1808 			return (-2);
1809 		}
1810 
1811 		/* Search for volume. */
1812 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1813 			if (strcmp(vol->v_name, volname) == 0)
1814 				break;
1815 		}
1816 		if (vol == NULL) {
1817 			i = strtol(volname, &tmp, 10);
1818 			if (verb != volname && tmp[0] == 0) {
1819 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1820 					if (vol->v_global_id == i)
1821 						break;
1822 				}
1823 			}
1824 		}
1825 		if (vol == NULL) {
1826 			gctl_error(req, "Volume '%s' not found.", volname);
1827 			return (-3);
1828 		}
1829 
1830 		/* Check if volume is still open. */
1831 		force = gctl_get_paraml(req, "force", sizeof(*force));
1832 		if (force != NULL && *force == 0 &&
1833 		    vol->v_provider_open != 0) {
1834 			gctl_error(req, "Volume is still open.");
1835 			return (-4);
1836 		}
1837 
1838 		/* Destroy volume and potentially node. */
1839 		i = 0;
1840 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
1841 			i++;
1842 		if (i >= 2) {
1843 			g_raid_destroy_volume(vol);
1844 			g_raid_md_write_intel(md, NULL, NULL, NULL);
1845 		} else {
1846 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1847 				if (disk->d_consumer)
1848 					intel_meta_erase(disk->d_consumer);
1849 			}
1850 			g_raid_destroy_node(sc, 0);
1851 		}
1852 		return (0);
1853 	}
1854 	if (strcmp(verb, "remove") == 0 ||
1855 	    strcmp(verb, "fail") == 0) {
1856 		if (*nargs < 2) {
1857 			gctl_error(req, "Invalid number of arguments.");
1858 			return (-1);
1859 		}
1860 		for (i = 1; i < *nargs; i++) {
1861 			snprintf(arg, sizeof(arg), "arg%d", i);
1862 			diskname = gctl_get_asciiparam(req, arg);
1863 			if (diskname == NULL) {
1864 				gctl_error(req, "No disk name (%s).", arg);
1865 				error = -2;
1866 				break;
1867 			}
1868 			if (strncmp(diskname, "/dev/", 5) == 0)
1869 				diskname += 5;
1870 
1871 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1872 				if (disk->d_consumer != NULL &&
1873 				    disk->d_consumer->provider != NULL &&
1874 				    strcmp(disk->d_consumer->provider->name,
1875 				     diskname) == 0)
1876 					break;
1877 			}
1878 			if (disk == NULL) {
1879 				gctl_error(req, "Disk '%s' not found.",
1880 				    diskname);
1881 				error = -3;
1882 				break;
1883 			}
1884 
1885 			if (strcmp(verb, "fail") == 0) {
1886 				g_raid_md_fail_disk_intel(md, NULL, disk);
1887 				continue;
1888 			}
1889 
1890 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1891 
1892 			/* Erase metadata on deleting disk. */
1893 			intel_meta_erase(disk->d_consumer);
1894 
1895 			/* If disk was assigned, just update statuses. */
1896 			if (pd->pd_disk_pos >= 0) {
1897 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1898 				g_raid_kill_consumer(sc, disk->d_consumer);
1899 				disk->d_consumer = NULL;
1900 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1901 					g_raid_change_subdisk_state(sd,
1902 					    G_RAID_SUBDISK_S_NONE);
1903 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1904 					    G_RAID_EVENT_SUBDISK);
1905 				}
1906 			} else {
1907 				/* Otherwise -- delete. */
1908 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1909 				g_raid_destroy_disk(disk);
1910 			}
1911 		}
1912 
1913 		/* Write updated metadata to remaining disks. */
1914 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1915 
1916 		/* Check if anything left except placeholders. */
1917 		if (g_raid_ndisks(sc, -1) ==
1918 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1919 			g_raid_destroy_node(sc, 0);
1920 		else
1921 			g_raid_md_intel_refill(sc);
1922 		return (error);
1923 	}
1924 	if (strcmp(verb, "insert") == 0) {
1925 		if (*nargs < 2) {
1926 			gctl_error(req, "Invalid number of arguments.");
1927 			return (-1);
1928 		}
1929 		update = 0;
1930 		for (i = 1; i < *nargs; i++) {
1931 			/* Get disk name. */
1932 			snprintf(arg, sizeof(arg), "arg%d", i);
1933 			diskname = gctl_get_asciiparam(req, arg);
1934 			if (diskname == NULL) {
1935 				gctl_error(req, "No disk name (%s).", arg);
1936 				error = -3;
1937 				break;
1938 			}
1939 
1940 			/* Try to find provider with specified name. */
1941 			g_topology_lock();
1942 			cp = g_raid_open_consumer(sc, diskname);
1943 			if (cp == NULL) {
1944 				gctl_error(req, "Can't open disk '%s'.",
1945 				    diskname);
1946 				g_topology_unlock();
1947 				error = -4;
1948 				break;
1949 			}
1950 			pp = cp->provider;
1951 			g_topology_unlock();
1952 
1953 			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
1954 				gctl_error(req,
1955 				    "Disk '%s' is too big.", diskname);
1956 				g_raid_kill_consumer(sc, cp);
1957 				error = -8;
1958 				break;
1959 			}
1960 
1961 			/* Read disk serial. */
1962 			error = g_raid_md_get_label(cp,
1963 			    &serial[0], INTEL_SERIAL_LEN);
1964 			if (error != 0) {
1965 				gctl_error(req,
1966 				    "Can't get serial for provider '%s'.",
1967 				    diskname);
1968 				g_raid_kill_consumer(sc, cp);
1969 				error = -7;
1970 				break;
1971 			}
1972 
1973 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1974 			pd->pd_disk_pos = -1;
1975 
1976 			disk = g_raid_create_disk(sc);
1977 			disk->d_consumer = cp;
1978 			disk->d_md_data = (void *)pd;
1979 			cp->private = disk;
1980 
1981 			/* Read kernel dumping information. */
1982 			disk->d_kd.offset = 0;
1983 			disk->d_kd.length = OFF_MAX;
1984 			len = sizeof(disk->d_kd);
1985 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1986 			if (disk->d_kd.di.dumper == NULL)
1987 				G_RAID_DEBUG1(2, sc,
1988 				    "Dumping not supported by %s.",
1989 				    cp->provider->name);
1990 
1991 			memcpy(&pd->pd_disk_meta.serial[0], &serial[0],
1992 			    INTEL_SERIAL_LEN);
1993 			pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
1994 			pd->pd_disk_meta.id = 0;
1995 			pd->pd_disk_meta.flags = INTEL_F_SPARE;
1996 
1997 			/* Welcome the "new" disk. */
1998 			update += g_raid_md_intel_start_disk(disk);
1999 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
2000 				intel_meta_write_spare(cp, &pd->pd_disk_meta);
2001 				g_raid_destroy_disk(disk);
2002 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2003 				gctl_error(req, "Disk '%s' doesn't fit.",
2004 				    diskname);
2005 				g_raid_destroy_disk(disk);
2006 				error = -8;
2007 				break;
2008 			}
2009 		}
2010 
2011 		/* Write new metadata if we changed something. */
2012 		if (update)
2013 			g_raid_md_write_intel(md, NULL, NULL, NULL);
2014 		return (error);
2015 	}
2016 	return (-100);
2017 }
2018 
2019 static int
2020 g_raid_md_write_intel(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2021     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2022 {
2023 	struct g_raid_softc *sc;
2024 	struct g_raid_volume *vol;
2025 	struct g_raid_subdisk *sd;
2026 	struct g_raid_disk *disk;
2027 	struct g_raid_md_intel_object *mdi;
2028 	struct g_raid_md_intel_perdisk *pd;
2029 	struct intel_raid_conf *meta;
2030 	struct intel_raid_vol *mvol;
2031 	struct intel_raid_map *mmap0, *mmap1;
2032 	off_t sectorsize = 512, pos;
2033 	const char *version, *cv;
2034 	int vi, sdi, numdisks, len, state, stale;
2035 
2036 	sc = md->mdo_softc;
2037 	mdi = (struct g_raid_md_intel_object *)md;
2038 
2039 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2040 		return (0);
2041 
2042 	/* Bump generation. Newly written metadata may differ from previous. */
2043 	mdi->mdio_generation++;
2044 
2045 	/* Count number of disks. */
2046 	numdisks = 0;
2047 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2048 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2049 		if (pd->pd_disk_pos < 0)
2050 			continue;
2051 		numdisks++;
2052 		if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
2053 			pd->pd_disk_meta.flags =
2054 			    INTEL_F_ONLINE | INTEL_F_ASSIGNED;
2055 		} else if (disk->d_state == G_RAID_DISK_S_FAILED) {
2056 			pd->pd_disk_meta.flags = INTEL_F_FAILED | INTEL_F_ASSIGNED;
2057 		} else {
2058 			pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
2059 			if (pd->pd_disk_meta.id != 0xffffffff) {
2060 				pd->pd_disk_meta.id = 0xffffffff;
2061 				len = strlen(pd->pd_disk_meta.serial);
2062 				len = min(len, INTEL_SERIAL_LEN - 3);
2063 				strcpy(pd->pd_disk_meta.serial + len, ":0");
2064 			}
2065 		}
2066 	}
2067 
2068 	/* Fill anchor and disks. */
2069 	meta = malloc(INTEL_MAX_MD_SIZE(numdisks),
2070 	    M_MD_INTEL, M_WAITOK | M_ZERO);
2071 	memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1);
2072 	meta->config_size = INTEL_MAX_MD_SIZE(numdisks);
2073 	meta->config_id = mdi->mdio_config_id;
2074 	meta->generation = mdi->mdio_generation;
2075 	meta->attributes = INTEL_ATTR_CHECKSUM;
2076 	meta->total_disks = numdisks;
2077 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2078 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2079 		if (pd->pd_disk_pos < 0)
2080 			continue;
2081 		meta->disk[pd->pd_disk_pos] = pd->pd_disk_meta;
2082 	}
2083 
2084 	/* Fill volumes and maps. */
2085 	vi = 0;
2086 	version = INTEL_VERSION_1000;
2087 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2088 		if (vol->v_stopping)
2089 			continue;
2090 		mvol = intel_get_volume(meta, vi);
2091 
2092 		/* New metadata may have different volumes order. */
2093 		vol->v_md_data = (void *)(intptr_t)vi;
2094 
2095 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2096 			sd = &vol->v_subdisks[sdi];
2097 			if (sd->sd_disk != NULL)
2098 				break;
2099 		}
2100 		if (sdi >= vol->v_disks_count)
2101 			panic("No any filled subdisk in volume");
2102 		if (vol->v_mediasize >= 0x20000000000llu)
2103 			meta->attributes |= INTEL_ATTR_2TB;
2104 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2105 			meta->attributes |= INTEL_ATTR_RAID0;
2106 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2107 			meta->attributes |= INTEL_ATTR_RAID1;
2108 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2109 			meta->attributes |= INTEL_ATTR_RAID5;
2110 		else
2111 			meta->attributes |= INTEL_ATTR_RAID10;
2112 
2113 		if (meta->attributes & INTEL_ATTR_2TB)
2114 			cv = INTEL_VERSION_1300;
2115 //		else if (dev->status == DEV_CLONE_N_GO)
2116 //			cv = INTEL_VERSION_1206;
2117 		else if (vol->v_disks_count > 4)
2118 			cv = INTEL_VERSION_1204;
2119 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2120 			cv = INTEL_VERSION_1202;
2121 		else if (vol->v_disks_count > 2)
2122 			cv = INTEL_VERSION_1201;
2123 		else if (vi > 0)
2124 			cv = INTEL_VERSION_1200;
2125 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2126 			cv = INTEL_VERSION_1100;
2127 		else
2128 			cv = INTEL_VERSION_1000;
2129 		if (strcmp(cv, version) > 0)
2130 			version = cv;
2131 
2132 		strlcpy(&mvol->name[0], vol->v_name, sizeof(mvol->name));
2133 		mvol->total_sectors = vol->v_mediasize / sectorsize;
2134 
2135 		/* Check for any recovery in progress. */
2136 		state = G_RAID_SUBDISK_S_ACTIVE;
2137 		pos = 0x7fffffffffffffffllu;
2138 		stale = 0;
2139 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2140 			sd = &vol->v_subdisks[sdi];
2141 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD)
2142 				state = G_RAID_SUBDISK_S_REBUILD;
2143 			else if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC &&
2144 			    state != G_RAID_SUBDISK_S_REBUILD)
2145 				state = G_RAID_SUBDISK_S_RESYNC;
2146 			else if (sd->sd_state == G_RAID_SUBDISK_S_STALE)
2147 				stale = 1;
2148 			if ((sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2149 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
2150 			     sd->sd_rebuild_pos < pos)
2151 			        pos = sd->sd_rebuild_pos;
2152 		}
2153 		if (state == G_RAID_SUBDISK_S_REBUILD) {
2154 			mvol->migr_state = 1;
2155 			mvol->migr_type = INTEL_MT_REBUILD;
2156 		} else if (state == G_RAID_SUBDISK_S_RESYNC) {
2157 			mvol->migr_state = 1;
2158 			/* mvol->migr_type = INTEL_MT_REPAIR; */
2159 			mvol->migr_type = INTEL_MT_VERIFY;
2160 			mvol->state |= INTEL_ST_VERIFY_AND_FIX;
2161 		} else
2162 			mvol->migr_state = 0;
2163 		mvol->dirty = (vol->v_dirty || stale);
2164 
2165 		mmap0 = intel_get_map(mvol, 0);
2166 
2167 		/* Write map / common part of two maps. */
2168 		mmap0->offset = sd->sd_offset / sectorsize;
2169 		mmap0->disk_sectors = sd->sd_size / sectorsize;
2170 		mmap0->strip_sectors = vol->v_strip_size / sectorsize;
2171 		if (vol->v_state == G_RAID_VOLUME_S_BROKEN)
2172 			mmap0->status = INTEL_S_FAILURE;
2173 		else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED)
2174 			mmap0->status = INTEL_S_DEGRADED;
2175 		else
2176 			mmap0->status = INTEL_S_READY;
2177 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2178 			mmap0->type = INTEL_T_RAID0;
2179 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
2180 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2181 			mmap0->type = INTEL_T_RAID1;
2182 		else
2183 			mmap0->type = INTEL_T_RAID5;
2184 		mmap0->total_disks = vol->v_disks_count;
2185 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2186 			mmap0->total_domains = vol->v_disks_count;
2187 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2188 			mmap0->total_domains = 2;
2189 		else
2190 			mmap0->total_domains = 1;
2191 		mmap0->stripe_count = sd->sd_size / vol->v_strip_size /
2192 		    mmap0->total_domains;
2193 		mmap0->failed_disk_num = 0xff;
2194 		mmap0->ddf = 1;
2195 
2196 		/* If there are two maps - copy common and update. */
2197 		if (mvol->migr_state) {
2198 			mvol->curr_migr_unit = pos /
2199 			    vol->v_strip_size / mmap0->total_domains;
2200 			mmap1 = intel_get_map(mvol, 1);
2201 			memcpy(mmap1, mmap0, sizeof(struct intel_raid_map));
2202 			mmap0->status = INTEL_S_READY;
2203 		} else
2204 			mmap1 = NULL;
2205 
2206 		/* Write disk indexes and put rebuild flags. */
2207 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2208 			sd = &vol->v_subdisks[sdi];
2209 			pd = (struct g_raid_md_intel_perdisk *)
2210 			    sd->sd_disk->d_md_data;
2211 			mmap0->disk_idx[sdi] = pd->pd_disk_pos;
2212 			if (mvol->migr_state)
2213 				mmap1->disk_idx[sdi] = pd->pd_disk_pos;
2214 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2215 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2216 				mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2217 			} else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE &&
2218 			    sd->sd_state != G_RAID_SUBDISK_S_STALE) {
2219 				mmap0->disk_idx[sdi] |= INTEL_DI_RBLD;
2220 				if (mvol->migr_state)
2221 					mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2222 			}
2223 			if ((sd->sd_state == G_RAID_SUBDISK_S_NONE ||
2224 			     sd->sd_state == G_RAID_SUBDISK_S_FAILED) &&
2225 			    mmap0->failed_disk_num == 0xff) {
2226 				mmap0->failed_disk_num = sdi;
2227 				if (mvol->migr_state)
2228 					mmap1->failed_disk_num = sdi;
2229 			}
2230 		}
2231 		vi++;
2232 	}
2233 	meta->total_volumes = vi;
2234 	if (strcmp(version, INTEL_VERSION_1300) != 0)
2235 		meta->attributes &= INTEL_ATTR_CHECKSUM;
2236 	memcpy(&meta->version[0], version, sizeof(INTEL_VERSION_1000) - 1);
2237 
2238 	/* We are done. Print meta data and store them to disks. */
2239 	g_raid_md_intel_print(meta);
2240 	if (mdi->mdio_meta != NULL)
2241 		free(mdi->mdio_meta, M_MD_INTEL);
2242 	mdi->mdio_meta = meta;
2243 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2244 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2245 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
2246 			continue;
2247 		if (pd->pd_meta != NULL) {
2248 			free(pd->pd_meta, M_MD_INTEL);
2249 			pd->pd_meta = NULL;
2250 		}
2251 		pd->pd_meta = intel_meta_copy(meta);
2252 		intel_meta_write(disk->d_consumer, meta);
2253 	}
2254 	return (0);
2255 }
2256 
2257 static int
2258 g_raid_md_fail_disk_intel(struct g_raid_md_object *md,
2259     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2260 {
2261 	struct g_raid_softc *sc;
2262 	struct g_raid_md_intel_object *mdi;
2263 	struct g_raid_md_intel_perdisk *pd;
2264 	struct g_raid_subdisk *sd;
2265 
2266 	sc = md->mdo_softc;
2267 	mdi = (struct g_raid_md_intel_object *)md;
2268 	pd = (struct g_raid_md_intel_perdisk *)tdisk->d_md_data;
2269 
2270 	/* We can't fail disk that is not a part of array now. */
2271 	if (pd->pd_disk_pos < 0)
2272 		return (-1);
2273 
2274 	/*
2275 	 * Mark disk as failed in metadata and try to write that metadata
2276 	 * to the disk itself to prevent it's later resurrection as STALE.
2277 	 */
2278 	mdi->mdio_meta->disk[pd->pd_disk_pos].flags = INTEL_F_FAILED;
2279 	pd->pd_disk_meta.flags = INTEL_F_FAILED;
2280 	g_raid_md_intel_print(mdi->mdio_meta);
2281 	if (tdisk->d_consumer)
2282 		intel_meta_write(tdisk->d_consumer, mdi->mdio_meta);
2283 
2284 	/* Change states. */
2285 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
2286 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
2287 		g_raid_change_subdisk_state(sd,
2288 		    G_RAID_SUBDISK_S_FAILED);
2289 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
2290 		    G_RAID_EVENT_SUBDISK);
2291 	}
2292 
2293 	/* Write updated metadata to remaining disks. */
2294 	g_raid_md_write_intel(md, NULL, NULL, tdisk);
2295 
2296 	/* Check if anything left except placeholders. */
2297 	if (g_raid_ndisks(sc, -1) ==
2298 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
2299 		g_raid_destroy_node(sc, 0);
2300 	else
2301 		g_raid_md_intel_refill(sc);
2302 	return (0);
2303 }
2304 
2305 static int
2306 g_raid_md_free_disk_intel(struct g_raid_md_object *md,
2307     struct g_raid_disk *disk)
2308 {
2309 	struct g_raid_md_intel_perdisk *pd;
2310 
2311 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2312 	if (pd->pd_meta != NULL) {
2313 		free(pd->pd_meta, M_MD_INTEL);
2314 		pd->pd_meta = NULL;
2315 	}
2316 	free(pd, M_MD_INTEL);
2317 	disk->d_md_data = NULL;
2318 	return (0);
2319 }
2320 
2321 static int
2322 g_raid_md_free_intel(struct g_raid_md_object *md)
2323 {
2324 	struct g_raid_md_intel_object *mdi;
2325 
2326 	mdi = (struct g_raid_md_intel_object *)md;
2327 	if (!mdi->mdio_started) {
2328 		mdi->mdio_started = 0;
2329 		callout_stop(&mdi->mdio_start_co);
2330 		G_RAID_DEBUG1(1, md->mdo_softc,
2331 		    "root_mount_rel %p", mdi->mdio_rootmount);
2332 		root_mount_rel(mdi->mdio_rootmount);
2333 		mdi->mdio_rootmount = NULL;
2334 	}
2335 	if (mdi->mdio_meta != NULL) {
2336 		free(mdi->mdio_meta, M_MD_INTEL);
2337 		mdi->mdio_meta = NULL;
2338 	}
2339 	return (0);
2340 }
2341 
2342 G_RAID_MD_DECLARE(g_raid_md_intel);
2343