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