xref: /freebsd/sys/geom/vinum/geom_vinum.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  *  Copyright (c) 2004, 2007 Lukas Ertl
5  *  Copyright (c) 2007, 2009 Ulf Lilleengen
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 AUTHOR 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 AUTHOR 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 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/bio.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/sbuf.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 
46 #include <geom/geom.h>
47 #include <geom/geom_dbg.h>
48 #include <geom/vinum/geom_vinum_var.h>
49 #include <geom/vinum/geom_vinum.h>
50 #include <geom/vinum/geom_vinum_raid5.h>
51 
52 SYSCTL_DECL(_kern_geom);
53 static SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54     "GEOM_VINUM stuff");
55 u_int g_vinum_debug = 0;
56 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RWTUN, &g_vinum_debug, 0,
57     "Debug level");
58 
59 static int	gv_create(struct g_geom *, struct gctl_req *);
60 static void	gv_attach(struct gv_softc *, struct gctl_req *);
61 static void	gv_detach(struct gv_softc *, struct gctl_req *);
62 static void	gv_parityop(struct gv_softc *, struct gctl_req *);
63 
64 static void
65 gv_orphan(struct g_consumer *cp)
66 {
67 	struct g_geom *gp;
68 	struct gv_softc *sc;
69 	struct gv_drive *d;
70 
71 	g_topology_assert();
72 
73 	KASSERT(cp != NULL, ("gv_orphan: null cp"));
74 	gp = cp->geom;
75 	KASSERT(gp != NULL, ("gv_orphan: null gp"));
76 	sc = gp->softc;
77 	KASSERT(sc != NULL, ("gv_orphan: null sc"));
78 	d = cp->private;
79 	KASSERT(d != NULL, ("gv_orphan: null d"));
80 
81 	g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);
82 
83 	gv_post_event(sc, GV_EVENT_DRIVE_LOST, d, NULL, 0, 0);
84 }
85 
86 void
87 gv_start(struct bio *bp)
88 {
89 	struct g_geom *gp;
90 	struct gv_softc *sc;
91 
92 	gp = bp->bio_to->geom;
93 	sc = gp->softc;
94 
95 	switch (bp->bio_cmd) {
96 	case BIO_READ:
97 	case BIO_WRITE:
98 	case BIO_DELETE:
99 		break;
100 	case BIO_GETATTR:
101 	default:
102 		g_io_deliver(bp, EOPNOTSUPP);
103 		return;
104 	}
105 	mtx_lock(&sc->bqueue_mtx);
106 	bioq_disksort(sc->bqueue_down, bp);
107 	wakeup(sc);
108 	mtx_unlock(&sc->bqueue_mtx);
109 }
110 
111 void
112 gv_done(struct bio *bp)
113 {
114 	struct g_geom *gp;
115 	struct gv_softc *sc;
116 
117 	KASSERT(bp != NULL, ("NULL bp"));
118 
119 	gp = bp->bio_from->geom;
120 	sc = gp->softc;
121 
122 	mtx_lock(&sc->bqueue_mtx);
123 	bioq_disksort(sc->bqueue_up, bp);
124 	wakeup(sc);
125 	mtx_unlock(&sc->bqueue_mtx);
126 }
127 
128 int
129 gv_access(struct g_provider *pp, int dr, int dw, int de)
130 {
131 	struct g_geom *gp;
132 	struct gv_softc *sc;
133 	struct gv_drive *d, *d2;
134 	int error;
135 
136 	gp = pp->geom;
137 	sc = gp->softc;
138 	/*
139 	 * We want to modify the read count with the write count in case we have
140 	 * plexes in a RAID-5 organization.
141 	 */
142 	dr += dw;
143 
144 	LIST_FOREACH(d, &sc->drives, drive) {
145 		if (d->consumer == NULL)
146 			continue;
147 		error = g_access(d->consumer, dr, dw, de);
148 		if (error) {
149 			LIST_FOREACH(d2, &sc->drives, drive) {
150 				if (d == d2)
151 					break;
152 				g_access(d2->consumer, -dr, -dw, -de);
153 			}
154 			G_VINUM_DEBUG(0, "g_access '%s' failed: %d", d->name,
155 			    error);
156 			return (error);
157 		}
158 	}
159 	return (0);
160 }
161 
162 static void
163 gv_init(struct g_class *mp)
164 {
165 	struct g_geom *gp;
166 	struct gv_softc *sc;
167 
168 	g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);
169 
170 	gp = g_new_geomf(mp, "VINUM");
171 	gp->spoiled = gv_orphan;
172 	gp->orphan = gv_orphan;
173 	gp->access = gv_access;
174 	gp->start = gv_start;
175 	gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO);
176 	sc = gp->softc;
177 	sc->geom = gp;
178 	sc->bqueue_down = g_malloc(sizeof(struct bio_queue_head),
179 	    M_WAITOK | M_ZERO);
180 	sc->bqueue_up = g_malloc(sizeof(struct bio_queue_head),
181 	    M_WAITOK | M_ZERO);
182 	bioq_init(sc->bqueue_down);
183 	bioq_init(sc->bqueue_up);
184 	LIST_INIT(&sc->drives);
185 	LIST_INIT(&sc->subdisks);
186 	LIST_INIT(&sc->plexes);
187 	LIST_INIT(&sc->volumes);
188 	TAILQ_INIT(&sc->equeue);
189 	mtx_init(&sc->config_mtx, "gv_config", NULL, MTX_DEF);
190 	mtx_init(&sc->equeue_mtx, "gv_equeue", NULL, MTX_DEF);
191 	mtx_init(&sc->bqueue_mtx, "gv_bqueue", NULL, MTX_DEF);
192 	kproc_create(gv_worker, sc, &sc->worker, 0, 0, "gv_worker");
193 }
194 
195 static int
196 gv_unload(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
197 {
198 	struct gv_softc *sc;
199 
200 	g_trace(G_T_TOPOLOGY, "gv_unload(%p)", mp);
201 
202 	g_topology_assert();
203 	sc = gp->softc;
204 
205 	if (sc != NULL) {
206 		gv_worker_exit(sc);
207 		gp->softc = NULL;
208 		g_wither_geom(gp, ENXIO);
209 	}
210 
211 	return (0);
212 }
213 
214 /* Handle userland request of attaching object. */
215 static void
216 gv_attach(struct gv_softc *sc, struct gctl_req *req)
217 {
218 	struct gv_volume *v;
219 	struct gv_plex *p;
220 	struct gv_sd *s;
221 	off_t *offset;
222 	int *rename, type_child, type_parent;
223 	char *child, *parent;
224 
225 	child = gctl_get_param(req, "child", NULL);
226 	if (child == NULL) {
227 		gctl_error(req, "no child given");
228 		return;
229 	}
230 	parent = gctl_get_param(req, "parent", NULL);
231 	if (parent == NULL) {
232 		gctl_error(req, "no parent given");
233 		return;
234 	}
235 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
236 	if (offset == NULL) {
237 		gctl_error(req, "no offset given");
238 		return;
239 	}
240 	rename = gctl_get_paraml(req, "rename", sizeof(*rename));
241 	if (rename == NULL) {
242 		gctl_error(req, "no rename flag given");
243 		return;
244 	}
245 
246 	type_child = gv_object_type(sc, child);
247 	type_parent = gv_object_type(sc, parent);
248 
249 	switch (type_child) {
250 	case GV_TYPE_PLEX:
251 		if (type_parent != GV_TYPE_VOL) {
252 			gctl_error(req, "no such volume to attach to");
253 			return;
254 		}
255 		v = gv_find_vol(sc, parent);
256 		p = gv_find_plex(sc, child);
257 		gv_post_event(sc, GV_EVENT_ATTACH_PLEX, p, v, *offset, *rename);
258 		break;
259 	case GV_TYPE_SD:
260 		if (type_parent != GV_TYPE_PLEX) {
261 			gctl_error(req, "no such plex to attach to");
262 			return;
263 		}
264 		p = gv_find_plex(sc, parent);
265 		s = gv_find_sd(sc, child);
266 		gv_post_event(sc, GV_EVENT_ATTACH_SD, s, p, *offset, *rename);
267 		break;
268 	default:
269 		gctl_error(req, "invalid child type");
270 		break;
271 	}
272 }
273 
274 /* Handle userland request of detaching object. */
275 static void
276 gv_detach(struct gv_softc *sc, struct gctl_req *req)
277 {
278 	struct gv_plex *p;
279 	struct gv_sd *s;
280 	int *flags, type;
281 	char *object;
282 
283 	object = gctl_get_param(req, "object", NULL);
284 	if (object == NULL) {
285 		gctl_error(req, "no argument given");
286 		return;
287 	}
288 
289 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
290 	type = gv_object_type(sc, object);
291 	switch (type) {
292 	case GV_TYPE_PLEX:
293 		p = gv_find_plex(sc, object);
294 		gv_post_event(sc, GV_EVENT_DETACH_PLEX, p, NULL, *flags, 0);
295 		break;
296 	case GV_TYPE_SD:
297 		s = gv_find_sd(sc, object);
298 		gv_post_event(sc, GV_EVENT_DETACH_SD, s, NULL, *flags, 0);
299 		break;
300 	default:
301 		gctl_error(req, "invalid object type");
302 		break;
303 	}
304 }
305 
306 /* Handle userland requests for creating new objects. */
307 static int
308 gv_create(struct g_geom *gp, struct gctl_req *req)
309 {
310 	struct gv_softc *sc;
311 	struct gv_drive *d, *d2;
312 	struct gv_plex *p, *p2;
313 	struct gv_sd *s, *s2;
314 	struct gv_volume *v, *v2;
315 	struct g_provider *pp;
316 	int error, i, *drives, *flags, *plexes, *subdisks, *volumes;
317 	char buf[20];
318 
319 	g_topology_assert();
320 
321 	sc = gp->softc;
322 
323 	/* Find out how many of each object have been passed in. */
324 	volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes));
325 	plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes));
326 	subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks));
327 	drives = gctl_get_paraml(req, "drives", sizeof(*drives));
328 	if (volumes == NULL || plexes == NULL || subdisks == NULL ||
329 	    drives == NULL) {
330 		gctl_error(req, "number of objects not given");
331 		return (-1);
332 	}
333 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
334 	if (flags == NULL) {
335 		gctl_error(req, "flags not given");
336 		return (-1);
337 	}
338 
339 	/* First, handle drive definitions ... */
340 	for (i = 0; i < *drives; i++) {
341 		snprintf(buf, sizeof(buf), "drive%d", i);
342 		d2 = gctl_get_paraml(req, buf, sizeof(*d2));
343 		if (d2 == NULL) {
344 			gctl_error(req, "no drive definition given");
345 			return (-1);
346 		}
347 		/*
348 		 * Make sure that the device specified in the drive config is
349 		 * an active GEOM provider.
350 		 */
351 		pp = g_provider_by_name(d2->device);
352 		if (pp == NULL) {
353 			gctl_error(req, "%s: device not found", d2->device);
354 			goto error;
355 		}
356 		if (gv_find_drive(sc, d2->name) != NULL) {
357 			/* Ignore error. */
358 			if (*flags & GV_FLAG_F)
359 				continue;
360 			gctl_error(req, "drive '%s' already exists", d2->name);
361 			goto error;
362 		}
363 		if (gv_find_drive_device(sc, d2->device) != NULL) {
364 			gctl_error(req, "device '%s' already configured in "
365 			    "gvinum", d2->device);
366 			goto error;
367 		}
368 
369 		d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
370 		bcopy(d2, d, sizeof(*d));
371 
372 		gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0);
373 	}
374 
375 	/* ... then volume definitions ... */
376 	for (i = 0; i < *volumes; i++) {
377 		error = 0;
378 		snprintf(buf, sizeof(buf), "volume%d", i);
379 		v2 = gctl_get_paraml(req, buf, sizeof(*v2));
380 		if (v2 == NULL) {
381 			gctl_error(req, "no volume definition given");
382 			return (-1);
383 		}
384 		if (gv_find_vol(sc, v2->name) != NULL) {
385 			/* Ignore error. */
386 			if (*flags & GV_FLAG_F)
387 				continue;
388 			gctl_error(req, "volume '%s' already exists", v2->name);
389 			goto error;
390 		}
391 
392 		v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
393 		bcopy(v2, v, sizeof(*v));
394 
395 		gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
396 	}
397 
398 	/* ... then plex definitions ... */
399 	for (i = 0; i < *plexes; i++) {
400 		error = 0;
401 		snprintf(buf, sizeof(buf), "plex%d", i);
402 		p2 = gctl_get_paraml(req, buf, sizeof(*p2));
403 		if (p2 == NULL) {
404 			gctl_error(req, "no plex definition given");
405 			return (-1);
406 		}
407 		if (gv_find_plex(sc, p2->name) != NULL) {
408 			/* Ignore error. */
409 			if (*flags & GV_FLAG_F)
410 				continue;
411 			gctl_error(req, "plex '%s' already exists", p2->name);
412 			goto error;
413 		}
414 
415 		p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
416 		bcopy(p2, p, sizeof(*p));
417 
418 		gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
419 	}
420 
421 	/* ... and, finally, subdisk definitions. */
422 	for (i = 0; i < *subdisks; i++) {
423 		error = 0;
424 		snprintf(buf, sizeof(buf), "sd%d", i);
425 		s2 = gctl_get_paraml(req, buf, sizeof(*s2));
426 		if (s2 == NULL) {
427 			gctl_error(req, "no subdisk definition given");
428 			return (-1);
429 		}
430 		if (gv_find_sd(sc, s2->name) != NULL) {
431 			/* Ignore error. */
432 			if (*flags & GV_FLAG_F)
433 				continue;
434 			gctl_error(req, "sd '%s' already exists", s2->name);
435 			goto error;
436 		}
437 
438 		s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
439 		bcopy(s2, s, sizeof(*s));
440 
441 		gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
442 	}
443 
444 error:
445 	gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
446 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
447 
448 	return (0);
449 }
450 
451 static void
452 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
453 {
454 	struct g_geom *gp;
455 	struct gv_softc *sc;
456 	struct sbuf *sb;
457 	char *comment;
458 
459 	g_topology_assert();
460 
461 	gp = LIST_FIRST(&mp->geom);
462 	sc = gp->softc;
463 
464 	if (!strcmp(verb, "attach")) {
465 		gv_attach(sc, req);
466 
467 	} else if (!strcmp(verb, "concat")) {
468 		gv_concat(gp, req);
469 
470 	} else if (!strcmp(verb, "detach")) {
471 		gv_detach(sc, req);
472 
473 	} else if (!strcmp(verb, "list")) {
474 		gv_list(gp, req);
475 
476 	/* Save our configuration back to disk. */
477 	} else if (!strcmp(verb, "saveconfig")) {
478 		gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
479 
480 	/* Return configuration in string form. */
481 	} else if (!strcmp(verb, "getconfig")) {
482 		comment = gctl_get_param(req, "comment", NULL);
483 		if (comment == NULL) {
484 			gctl_error(req, "no comment parameter given");
485 			return;
486 		}
487 		sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
488 		gv_format_config(sc, sb, 0, comment);
489 		sbuf_finish(sb);
490 		gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
491 		sbuf_delete(sb);
492 
493 	} else if (!strcmp(verb, "create")) {
494 		gv_create(gp, req);
495 
496 	} else if (!strcmp(verb, "mirror")) {
497 		gv_mirror(gp, req);
498 
499 	} else if (!strcmp(verb, "move")) {
500 		gv_move(gp, req);
501 
502 	} else if (!strcmp(verb, "raid5")) {
503 		gv_raid5(gp, req);
504 
505 	} else if (!strcmp(verb, "rebuildparity") ||
506 	    !strcmp(verb, "checkparity")) {
507 		gv_parityop(sc, req);
508 
509 	} else if (!strcmp(verb, "remove")) {
510 		gv_remove(gp, req);
511 
512 	} else if (!strcmp(verb, "rename")) {
513 		gv_rename(gp, req);
514 
515 	} else if (!strcmp(verb, "resetconfig")) {
516 		gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0);
517 
518 	} else if (!strcmp(verb, "start")) {
519 		gv_start_obj(gp, req);
520 
521 	} else if (!strcmp(verb, "stripe")) {
522 		gv_stripe(gp, req);
523 
524 	} else if (!strcmp(verb, "setstate")) {
525 		gv_setstate(gp, req);
526 	} else
527 		gctl_error(req, "Unknown verb parameter");
528 }
529 
530 static void
531 gv_parityop(struct gv_softc *sc, struct gctl_req *req)
532 {
533 	struct gv_plex *p;
534 	int *flags, *rebuild, type;
535 	char *plex;
536 
537 	plex = gctl_get_param(req, "plex", NULL);
538 	if (plex == NULL) {
539 		gctl_error(req, "no plex given");
540 		return;
541 	}
542 
543 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
544 	if (flags == NULL) {
545 		gctl_error(req, "no flags given");
546 		return;
547 	}
548 
549 	rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild));
550 	if (rebuild == NULL) {
551 		gctl_error(req, "no operation given");
552 		return;
553 	}
554 
555 	type = gv_object_type(sc, plex);
556 	if (type != GV_TYPE_PLEX) {
557 		gctl_error(req, "'%s' is not a plex", plex);
558 		return;
559 	}
560 	p = gv_find_plex(sc, plex);
561 
562 	if (p->state != GV_PLEX_UP) {
563 		gctl_error(req, "plex %s is not completely accessible",
564 		    p->name);
565 		return;
566 	}
567 
568 	if (p->org != GV_PLEX_RAID5) {
569 		gctl_error(req, "plex %s is not a RAID5 plex", p->name);
570 		return;
571 	}
572 
573 	/* Put it in the event queue. */
574 	/* XXX: The state of the plex might have changed when this event is
575 	 * picked up ... We should perhaps check this afterwards. */
576 	if (*rebuild)
577 		gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0);
578 	else
579 		gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0);
580 }
581 
582 static struct g_geom *
583 gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
584 {
585 	struct g_geom *gp;
586 	struct g_consumer *cp;
587 	struct gv_softc *sc;
588 	struct gv_hdr vhdr;
589 	int error;
590 
591  	g_topology_assert();
592 	g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name);
593 
594 	gp = LIST_FIRST(&mp->geom);
595 	if (gp == NULL) {
596 		G_VINUM_DEBUG(0, "error: tasting, but not initialized?");
597 		return (NULL);
598 	}
599 	sc = gp->softc;
600 
601 	cp = g_new_consumer(gp);
602 	if (g_attach(cp, pp) != 0) {
603 		g_destroy_consumer(cp);
604 		return (NULL);
605 	}
606 	if (g_access(cp, 1, 0, 0) != 0) {
607 		g_detach(cp);
608 		g_destroy_consumer(cp);
609 		return (NULL);
610 	}
611 	g_topology_unlock();
612 
613 	error = gv_read_header(cp, &vhdr);
614 
615 	g_topology_lock();
616 	g_access(cp, -1, 0, 0);
617 	g_detach(cp);
618 	g_destroy_consumer(cp);
619 
620 	/* Check if what we've been given is a valid vinum drive. */
621 	if (!error)
622 		gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0);
623 
624 	return (NULL);
625 }
626 
627 void
628 gv_worker(void *arg)
629 {
630 	struct g_provider *pp;
631 	struct gv_softc *sc;
632 	struct gv_event *ev;
633 	struct gv_volume *v;
634 	struct gv_plex *p;
635 	struct gv_sd *s;
636 	struct gv_drive *d;
637 	struct bio *bp;
638 	int newstate, flags, err, rename;
639 	char *newname;
640 	off_t offset;
641 
642 	sc = arg;
643 	KASSERT(sc != NULL, ("NULL sc"));
644 	for (;;) {
645 		/* Look at the events first... */
646 		ev = gv_get_event(sc);
647 		if (ev != NULL) {
648 			gv_remove_event(sc, ev);
649 
650 			switch (ev->type) {
651 			case GV_EVENT_DRIVE_TASTED:
652 				G_VINUM_DEBUG(2, "event 'drive tasted'");
653 				pp = ev->arg1;
654 				gv_drive_tasted(sc, pp);
655 				break;
656 
657 			case GV_EVENT_DRIVE_LOST:
658 				G_VINUM_DEBUG(2, "event 'drive lost'");
659 				d = ev->arg1;
660 				gv_drive_lost(sc, d);
661 				break;
662 
663 			case GV_EVENT_CREATE_DRIVE:
664 				G_VINUM_DEBUG(2, "event 'create drive'");
665 				d = ev->arg1;
666 				gv_create_drive(sc, d);
667 				break;
668 
669 			case GV_EVENT_CREATE_VOLUME:
670 				G_VINUM_DEBUG(2, "event 'create volume'");
671 				v = ev->arg1;
672 				gv_create_volume(sc, v);
673 				break;
674 
675 			case GV_EVENT_CREATE_PLEX:
676 				G_VINUM_DEBUG(2, "event 'create plex'");
677 				p = ev->arg1;
678 				gv_create_plex(sc, p);
679 				break;
680 
681 			case GV_EVENT_CREATE_SD:
682 				G_VINUM_DEBUG(2, "event 'create sd'");
683 				s = ev->arg1;
684 				gv_create_sd(sc, s);
685 				break;
686 
687 			case GV_EVENT_RM_DRIVE:
688 				G_VINUM_DEBUG(2, "event 'remove drive'");
689 				d = ev->arg1;
690 				flags = ev->arg3;
691 				gv_rm_drive(sc, d, flags);
692 				/*gv_setup_objects(sc);*/
693 				break;
694 
695 			case GV_EVENT_RM_VOLUME:
696 				G_VINUM_DEBUG(2, "event 'remove volume'");
697 				v = ev->arg1;
698 				gv_rm_vol(sc, v);
699 				/*gv_setup_objects(sc);*/
700 				break;
701 
702 			case GV_EVENT_RM_PLEX:
703 				G_VINUM_DEBUG(2, "event 'remove plex'");
704 				p = ev->arg1;
705 				gv_rm_plex(sc, p);
706 				/*gv_setup_objects(sc);*/
707 				break;
708 
709 			case GV_EVENT_RM_SD:
710 				G_VINUM_DEBUG(2, "event 'remove sd'");
711 				s = ev->arg1;
712 				gv_rm_sd(sc, s);
713 				/*gv_setup_objects(sc);*/
714 				break;
715 
716 			case GV_EVENT_SAVE_CONFIG:
717 				G_VINUM_DEBUG(2, "event 'save config'");
718 				gv_save_config(sc);
719 				break;
720 
721 			case GV_EVENT_SET_SD_STATE:
722 				G_VINUM_DEBUG(2, "event 'setstate sd'");
723 				s = ev->arg1;
724 				newstate = ev->arg3;
725 				flags = ev->arg4;
726 				err = gv_set_sd_state(s, newstate, flags);
727 				if (err)
728 					G_VINUM_DEBUG(0, "error setting subdisk"
729 					    " state: error code %d", err);
730 				break;
731 
732 			case GV_EVENT_SET_DRIVE_STATE:
733 				G_VINUM_DEBUG(2, "event 'setstate drive'");
734 				d = ev->arg1;
735 				newstate = ev->arg3;
736 				flags = ev->arg4;
737 				err = gv_set_drive_state(d, newstate, flags);
738 				if (err)
739 					G_VINUM_DEBUG(0, "error setting drive "
740 					    "state: error code %d", err);
741 				break;
742 
743 			case GV_EVENT_SET_VOL_STATE:
744 				G_VINUM_DEBUG(2, "event 'setstate volume'");
745 				v = ev->arg1;
746 				newstate = ev->arg3;
747 				flags = ev->arg4;
748 				err = gv_set_vol_state(v, newstate, flags);
749 				if (err)
750 					G_VINUM_DEBUG(0, "error setting volume "
751 					    "state: error code %d", err);
752 				break;
753 
754 			case GV_EVENT_SET_PLEX_STATE:
755 				G_VINUM_DEBUG(2, "event 'setstate plex'");
756 				p = ev->arg1;
757 				newstate = ev->arg3;
758 				flags = ev->arg4;
759 				err = gv_set_plex_state(p, newstate, flags);
760 				if (err)
761 					G_VINUM_DEBUG(0, "error setting plex "
762 					    "state: error code %d", err);
763 				break;
764 
765 			case GV_EVENT_SETUP_OBJECTS:
766 				G_VINUM_DEBUG(2, "event 'setup objects'");
767 				gv_setup_objects(sc);
768 				break;
769 
770 			case GV_EVENT_RESET_CONFIG:
771 				G_VINUM_DEBUG(2, "event 'resetconfig'");
772 				err = gv_resetconfig(sc);
773 				if (err)
774 					G_VINUM_DEBUG(0, "error resetting "
775 					    "config: error code %d", err);
776 				break;
777 
778 			case GV_EVENT_PARITY_REBUILD:
779 				/*
780 				 * Start the rebuild. The gv_plex_done will
781 				 * handle issuing of the remaining rebuild bio's
782 				 * until it's finished.
783 				 */
784 				G_VINUM_DEBUG(2, "event 'rebuild'");
785 				p = ev->arg1;
786 				if (p->state != GV_PLEX_UP) {
787 					G_VINUM_DEBUG(0, "plex %s is not "
788 					    "completely accessible", p->name);
789 					break;
790 				}
791 				if (p->flags & GV_PLEX_SYNCING ||
792 				    p->flags & GV_PLEX_REBUILDING ||
793 				    p->flags & GV_PLEX_GROWING) {
794 					G_VINUM_DEBUG(0, "plex %s is busy with "
795 					    "syncing or parity build", p->name);
796 					break;
797 				}
798 				p->synced = 0;
799 				p->flags |= GV_PLEX_REBUILDING;
800 				g_topology_assert_not();
801 				g_topology_lock();
802 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
803 				if (err) {
804 					G_VINUM_DEBUG(0, "unable to access "
805 					    "provider");
806 					break;
807 				}
808 				g_topology_unlock();
809 				gv_parity_request(p, GV_BIO_CHECK |
810 				    GV_BIO_PARITY, 0);
811 				break;
812 
813 			case GV_EVENT_PARITY_CHECK:
814 				/* Start parity check. */
815 				G_VINUM_DEBUG(2, "event 'check'");
816 				p = ev->arg1;
817 				if (p->state != GV_PLEX_UP) {
818 					G_VINUM_DEBUG(0, "plex %s is not "
819 					    "completely accessible", p->name);
820 					break;
821 				}
822 				if (p->flags & GV_PLEX_SYNCING ||
823 				    p->flags & GV_PLEX_REBUILDING ||
824 				    p->flags & GV_PLEX_GROWING) {
825 					G_VINUM_DEBUG(0, "plex %s is busy with "
826 					    "syncing or parity build", p->name);
827 					break;
828 				}
829 				p->synced = 0;
830 				g_topology_assert_not();
831 				g_topology_lock();
832 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
833 				if (err) {
834 					G_VINUM_DEBUG(0, "unable to access "
835 					    "provider");
836 					break;
837 				}
838 				g_topology_unlock();
839 				gv_parity_request(p, GV_BIO_CHECK, 0);
840 				break;
841 
842 			case GV_EVENT_START_PLEX:
843 				G_VINUM_DEBUG(2, "event 'start' plex");
844 				p = ev->arg1;
845 				gv_start_plex(p);
846 				break;
847 
848 			case GV_EVENT_START_VOLUME:
849 				G_VINUM_DEBUG(2, "event 'start' volume");
850 				v = ev->arg1;
851 				gv_start_vol(v);
852 				break;
853 
854 			case GV_EVENT_ATTACH_PLEX:
855 				G_VINUM_DEBUG(2, "event 'attach' plex");
856 				p = ev->arg1;
857 				v = ev->arg2;
858 				rename = ev->arg4;
859 				err = gv_attach_plex(p, v, rename);
860 				if (err)
861 					G_VINUM_DEBUG(0, "error attaching %s to"
862 					    " %s: error code %d", p->name,
863 					    v->name, err);
864 				break;
865 
866 			case GV_EVENT_ATTACH_SD:
867 				G_VINUM_DEBUG(2, "event 'attach' sd");
868 				s = ev->arg1;
869 				p = ev->arg2;
870 				offset = ev->arg3;
871 				rename = ev->arg4;
872 				err = gv_attach_sd(s, p, offset, rename);
873 				if (err)
874 					G_VINUM_DEBUG(0, "error attaching %s to"
875 					    " %s: error code %d", s->name,
876 					    p->name, err);
877 				break;
878 
879 			case GV_EVENT_DETACH_PLEX:
880 				G_VINUM_DEBUG(2, "event 'detach' plex");
881 				p = ev->arg1;
882 				flags = ev->arg3;
883 				err = gv_detach_plex(p, flags);
884 				if (err)
885 					G_VINUM_DEBUG(0, "error detaching %s: "
886 					    "error code %d", p->name, err);
887 				break;
888 
889 			case GV_EVENT_DETACH_SD:
890 				G_VINUM_DEBUG(2, "event 'detach' sd");
891 				s = ev->arg1;
892 				flags = ev->arg3;
893 				err = gv_detach_sd(s, flags);
894 				if (err)
895 					G_VINUM_DEBUG(0, "error detaching %s: "
896 					    "error code %d", s->name, err);
897 				break;
898 
899 			case GV_EVENT_RENAME_VOL:
900 				G_VINUM_DEBUG(2, "event 'rename' volume");
901 				v = ev->arg1;
902 				newname = ev->arg2;
903 				flags = ev->arg3;
904 				err = gv_rename_vol(sc, v, newname, flags);
905 				if (err)
906 					G_VINUM_DEBUG(0, "error renaming %s to "
907 					    "%s: error code %d", v->name,
908 					    newname, err);
909 				g_free(newname);
910 				/* Destroy and recreate the provider if we can. */
911 				if (gv_provider_is_open(v->provider)) {
912 					G_VINUM_DEBUG(0, "unable to rename "
913 					    "provider to %s: provider in use",
914 					    v->name);
915 					break;
916 				}
917 				g_topology_lock();
918 				g_wither_provider(v->provider, ENOENT);
919 				g_topology_unlock();
920 				v->provider = NULL;
921 				gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc,
922 				    NULL, 0, 0);
923 				break;
924 
925 			case GV_EVENT_RENAME_PLEX:
926 				G_VINUM_DEBUG(2, "event 'rename' plex");
927 				p = ev->arg1;
928 				newname = ev->arg2;
929 				flags = ev->arg3;
930 				err = gv_rename_plex(sc, p, newname, flags);
931 				if (err)
932 					G_VINUM_DEBUG(0, "error renaming %s to "
933 					    "%s: error code %d", p->name,
934 					    newname, err);
935 				g_free(newname);
936 				break;
937 
938 			case GV_EVENT_RENAME_SD:
939 				G_VINUM_DEBUG(2, "event 'rename' sd");
940 				s = ev->arg1;
941 				newname = ev->arg2;
942 				flags = ev->arg3;
943 				err = gv_rename_sd(sc, s, newname, flags);
944 				if (err)
945 					G_VINUM_DEBUG(0, "error renaming %s to "
946 					    "%s: error code %d", s->name,
947 					    newname, err);
948 				g_free(newname);
949 				break;
950 
951 			case GV_EVENT_RENAME_DRIVE:
952 				G_VINUM_DEBUG(2, "event 'rename' drive");
953 				d = ev->arg1;
954 				newname = ev->arg2;
955 				flags = ev->arg3;
956 				err = gv_rename_drive(sc, d, newname, flags);
957 				if (err)
958 					G_VINUM_DEBUG(0, "error renaming %s to "
959 					    "%s: error code %d", d->name,
960 					    newname, err);
961 				g_free(newname);
962 				break;
963 
964 			case GV_EVENT_MOVE_SD:
965 				G_VINUM_DEBUG(2, "event 'move' sd");
966 				s = ev->arg1;
967 				d = ev->arg2;
968 				flags = ev->arg3;
969 				err = gv_move_sd(sc, s, d, flags);
970 				if (err)
971 					G_VINUM_DEBUG(0, "error moving %s to "
972 					    "%s: error code %d", s->name,
973 					    d->name, err);
974 				break;
975 
976 			case GV_EVENT_THREAD_EXIT:
977 				G_VINUM_DEBUG(2, "event 'thread exit'");
978 				g_free(ev);
979 				mtx_lock(&sc->equeue_mtx);
980 				mtx_lock(&sc->bqueue_mtx);
981 				gv_cleanup(sc);
982 				mtx_destroy(&sc->bqueue_mtx);
983 				mtx_destroy(&sc->equeue_mtx);
984 				g_free(sc->bqueue_down);
985 				g_free(sc->bqueue_up);
986 				g_free(sc);
987 				kproc_exit(0);
988 				/* NOTREACHED */
989 
990 			default:
991 				G_VINUM_DEBUG(1, "unknown event %d", ev->type);
992 			}
993 
994 			g_free(ev);
995 			continue;
996 		}
997 
998 		/* ... then do I/O processing. */
999 		mtx_lock(&sc->bqueue_mtx);
1000 		/* First do new requests. */
1001 		bp = bioq_takefirst(sc->bqueue_down);
1002 		if (bp != NULL) {
1003 			mtx_unlock(&sc->bqueue_mtx);
1004 			/* A bio that interfered with another bio. */
1005 			if (bp->bio_pflags & GV_BIO_ONHOLD) {
1006 				s = bp->bio_caller1;
1007 				p = s->plex_sc;
1008 				/* Is it still locked out? */
1009 				if (gv_stripe_active(p, bp)) {
1010 					/* Park the bio on the waiting queue. */
1011 					bioq_disksort(p->wqueue, bp);
1012 				} else {
1013 					bp->bio_pflags &= ~GV_BIO_ONHOLD;
1014 					g_io_request(bp, s->drive_sc->consumer);
1015 				}
1016 			/* A special request requireing special handling. */
1017 			} else if (bp->bio_pflags & GV_BIO_INTERNAL) {
1018 				p = bp->bio_caller1;
1019 				gv_plex_start(p, bp);
1020 			} else {
1021 				gv_volume_start(sc, bp);
1022 			}
1023 			mtx_lock(&sc->bqueue_mtx);
1024 		}
1025 		/* Then do completed requests. */
1026 		bp = bioq_takefirst(sc->bqueue_up);
1027 		if (bp == NULL) {
1028 			msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10);
1029 			mtx_unlock(&sc->bqueue_mtx);
1030 			continue;
1031 		}
1032 		mtx_unlock(&sc->bqueue_mtx);
1033 		gv_bio_done(sc, bp);
1034 	}
1035 }
1036 
1037 #define	VINUM_CLASS_NAME "VINUM"
1038 
1039 static struct g_class g_vinum_class	= {
1040 	.name = VINUM_CLASS_NAME,
1041 	.version = G_VERSION,
1042 	.init = gv_init,
1043 	.taste = gv_taste,
1044 	.ctlreq = gv_config,
1045 	.destroy_geom = gv_unload,
1046 };
1047 
1048 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);
1049 MODULE_VERSION(geom_vinum, 0);
1050