xref: /freebsd/sys/geom/vinum/geom_vinum.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
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/vinum/geom_vinum_var.h>
48 #include <geom/vinum/geom_vinum.h>
49 #include <geom/vinum/geom_vinum_raid5.h>
50 
51 SYSCTL_DECL(_kern_geom);
52 static SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW, 0,
53     "GEOM_VINUM stuff");
54 u_int g_vinum_debug = 0;
55 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RWTUN, &g_vinum_debug, 0,
56     "Debug level");
57 
58 static int	gv_create(struct g_geom *, struct gctl_req *);
59 static void	gv_attach(struct gv_softc *, struct gctl_req *);
60 static void	gv_detach(struct gv_softc *, struct gctl_req *);
61 static void	gv_parityop(struct gv_softc *, struct gctl_req *);
62 
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 
370 		d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
371 		bcopy(d2, d, sizeof(*d));
372 
373 		gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0);
374 	}
375 
376 	/* ... then volume definitions ... */
377 	for (i = 0; i < *volumes; i++) {
378 		error = 0;
379 		snprintf(buf, sizeof(buf), "volume%d", i);
380 		v2 = gctl_get_paraml(req, buf, sizeof(*v2));
381 		if (v2 == NULL) {
382 			gctl_error(req, "no volume definition given");
383 			return (-1);
384 		}
385 		if (gv_find_vol(sc, v2->name) != NULL) {
386 			/* Ignore error. */
387 			if (*flags & GV_FLAG_F)
388 				continue;
389 			gctl_error(req, "volume '%s' already exists", v2->name);
390 			goto error;
391 		}
392 
393 		v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
394 		bcopy(v2, v, sizeof(*v));
395 
396 		gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
397 	}
398 
399 	/* ... then plex definitions ... */
400 	for (i = 0; i < *plexes; i++) {
401 		error = 0;
402 		snprintf(buf, sizeof(buf), "plex%d", i);
403 		p2 = gctl_get_paraml(req, buf, sizeof(*p2));
404 		if (p2 == NULL) {
405 			gctl_error(req, "no plex definition given");
406 			return (-1);
407 		}
408 		if (gv_find_plex(sc, p2->name) != NULL) {
409 			/* Ignore error. */
410 			if (*flags & GV_FLAG_F)
411 				continue;
412 			gctl_error(req, "plex '%s' already exists", p2->name);
413 			goto error;
414 		}
415 
416 		p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
417 		bcopy(p2, p, sizeof(*p));
418 
419 		gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
420 	}
421 
422 	/* ... and, finally, subdisk definitions. */
423 	for (i = 0; i < *subdisks; i++) {
424 		error = 0;
425 		snprintf(buf, sizeof(buf), "sd%d", i);
426 		s2 = gctl_get_paraml(req, buf, sizeof(*s2));
427 		if (s2 == NULL) {
428 			gctl_error(req, "no subdisk definition given");
429 			return (-1);
430 		}
431 		if (gv_find_sd(sc, s2->name) != NULL) {
432 			/* Ignore error. */
433 			if (*flags & GV_FLAG_F)
434 				continue;
435 			gctl_error(req, "sd '%s' already exists", s2->name);
436 			goto error;
437 		}
438 
439 		s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
440 		bcopy(s2, s, sizeof(*s));
441 
442 		gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
443 	}
444 
445 error:
446 	gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
447 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
448 
449 	return (0);
450 }
451 
452 static void
453 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
454 {
455 	struct g_geom *gp;
456 	struct gv_softc *sc;
457 	struct sbuf *sb;
458 	char *comment;
459 
460 	g_topology_assert();
461 
462 	gp = LIST_FIRST(&mp->geom);
463 	sc = gp->softc;
464 
465 	if (!strcmp(verb, "attach")) {
466 		gv_attach(sc, req);
467 
468 	} else if (!strcmp(verb, "concat")) {
469 		gv_concat(gp, req);
470 
471 	} else if (!strcmp(verb, "detach")) {
472 		gv_detach(sc, req);
473 
474 	} else if (!strcmp(verb, "list")) {
475 		gv_list(gp, req);
476 
477 	/* Save our configuration back to disk. */
478 	} else if (!strcmp(verb, "saveconfig")) {
479 		gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
480 
481 	/* Return configuration in string form. */
482 	} else if (!strcmp(verb, "getconfig")) {
483 		comment = gctl_get_param(req, "comment", NULL);
484 		if (comment == NULL) {
485 			gctl_error(req, "no comment parameter given");
486 			return;
487 		}
488 		sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
489 		gv_format_config(sc, sb, 0, comment);
490 		sbuf_finish(sb);
491 		gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
492 		sbuf_delete(sb);
493 
494 	} else if (!strcmp(verb, "create")) {
495 		gv_create(gp, req);
496 
497 	} else if (!strcmp(verb, "mirror")) {
498 		gv_mirror(gp, req);
499 
500 	} else if (!strcmp(verb, "move")) {
501 		gv_move(gp, req);
502 
503 	} else if (!strcmp(verb, "raid5")) {
504 		gv_raid5(gp, req);
505 
506 	} else if (!strcmp(verb, "rebuildparity") ||
507 	    !strcmp(verb, "checkparity")) {
508 		gv_parityop(sc, req);
509 
510 	} else if (!strcmp(verb, "remove")) {
511 		gv_remove(gp, req);
512 
513 	} else if (!strcmp(verb, "rename")) {
514 		gv_rename(gp, req);
515 
516 	} else if (!strcmp(verb, "resetconfig")) {
517 		gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0);
518 
519 	} else if (!strcmp(verb, "start")) {
520 		gv_start_obj(gp, req);
521 
522 	} else if (!strcmp(verb, "stripe")) {
523 		gv_stripe(gp, req);
524 
525 	} else if (!strcmp(verb, "setstate")) {
526 		gv_setstate(gp, req);
527 	} else
528 		gctl_error(req, "Unknown verb parameter");
529 }
530 
531 static void
532 gv_parityop(struct gv_softc *sc, struct gctl_req *req)
533 {
534 	struct gv_plex *p;
535 	int *flags, *rebuild, type;
536 	char *plex;
537 
538 	plex = gctl_get_param(req, "plex", NULL);
539 	if (plex == NULL) {
540 		gctl_error(req, "no plex given");
541 		return;
542 	}
543 
544 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
545 	if (flags == NULL) {
546 		gctl_error(req, "no flags given");
547 		return;
548 	}
549 
550 	rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild));
551 	if (rebuild == NULL) {
552 		gctl_error(req, "no operation given");
553 		return;
554 	}
555 
556 	type = gv_object_type(sc, plex);
557 	if (type != GV_TYPE_PLEX) {
558 		gctl_error(req, "'%s' is not a plex", plex);
559 		return;
560 	}
561 	p = gv_find_plex(sc, plex);
562 
563 	if (p->state != GV_PLEX_UP) {
564 		gctl_error(req, "plex %s is not completely accessible",
565 		    p->name);
566 		return;
567 	}
568 
569 	if (p->org != GV_PLEX_RAID5) {
570 		gctl_error(req, "plex %s is not a RAID5 plex", p->name);
571 		return;
572 	}
573 
574 	/* Put it in the event queue. */
575 	/* XXX: The state of the plex might have changed when this event is
576 	 * picked up ... We should perhaps check this afterwards. */
577 	if (*rebuild)
578 		gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0);
579 	else
580 		gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0);
581 }
582 
583 
584 static struct g_geom *
585 gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
586 {
587 	struct g_geom *gp;
588 	struct g_consumer *cp;
589 	struct gv_softc *sc;
590 	struct gv_hdr vhdr;
591 	int error;
592 
593  	g_topology_assert();
594 	g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name);
595 
596 	gp = LIST_FIRST(&mp->geom);
597 	if (gp == NULL) {
598 		G_VINUM_DEBUG(0, "error: tasting, but not initialized?");
599 		return (NULL);
600 	}
601 	sc = gp->softc;
602 
603 	cp = g_new_consumer(gp);
604 	if (g_attach(cp, pp) != 0) {
605 		g_destroy_consumer(cp);
606 		return (NULL);
607 	}
608 	if (g_access(cp, 1, 0, 0) != 0) {
609 		g_detach(cp);
610 		g_destroy_consumer(cp);
611 		return (NULL);
612 	}
613 	g_topology_unlock();
614 
615 	error = gv_read_header(cp, &vhdr);
616 
617 	g_topology_lock();
618 	g_access(cp, -1, 0, 0);
619 	g_detach(cp);
620 	g_destroy_consumer(cp);
621 
622 	/* Check if what we've been given is a valid vinum drive. */
623 	if (!error)
624 		gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0);
625 
626 	return (NULL);
627 }
628 
629 void
630 gv_worker(void *arg)
631 {
632 	struct g_provider *pp;
633 	struct gv_softc *sc;
634 	struct gv_event *ev;
635 	struct gv_volume *v;
636 	struct gv_plex *p;
637 	struct gv_sd *s;
638 	struct gv_drive *d;
639 	struct bio *bp;
640 	int newstate, flags, err, rename;
641 	char *newname;
642 	off_t offset;
643 
644 	sc = arg;
645 	KASSERT(sc != NULL, ("NULL sc"));
646 	for (;;) {
647 		/* Look at the events first... */
648 		ev = gv_get_event(sc);
649 		if (ev != NULL) {
650 			gv_remove_event(sc, ev);
651 
652 			switch (ev->type) {
653 			case GV_EVENT_DRIVE_TASTED:
654 				G_VINUM_DEBUG(2, "event 'drive tasted'");
655 				pp = ev->arg1;
656 				gv_drive_tasted(sc, pp);
657 				break;
658 
659 			case GV_EVENT_DRIVE_LOST:
660 				G_VINUM_DEBUG(2, "event 'drive lost'");
661 				d = ev->arg1;
662 				gv_drive_lost(sc, d);
663 				break;
664 
665 			case GV_EVENT_CREATE_DRIVE:
666 				G_VINUM_DEBUG(2, "event 'create drive'");
667 				d = ev->arg1;
668 				gv_create_drive(sc, d);
669 				break;
670 
671 			case GV_EVENT_CREATE_VOLUME:
672 				G_VINUM_DEBUG(2, "event 'create volume'");
673 				v = ev->arg1;
674 				gv_create_volume(sc, v);
675 				break;
676 
677 			case GV_EVENT_CREATE_PLEX:
678 				G_VINUM_DEBUG(2, "event 'create plex'");
679 				p = ev->arg1;
680 				gv_create_plex(sc, p);
681 				break;
682 
683 			case GV_EVENT_CREATE_SD:
684 				G_VINUM_DEBUG(2, "event 'create sd'");
685 				s = ev->arg1;
686 				gv_create_sd(sc, s);
687 				break;
688 
689 			case GV_EVENT_RM_DRIVE:
690 				G_VINUM_DEBUG(2, "event 'remove drive'");
691 				d = ev->arg1;
692 				flags = ev->arg3;
693 				gv_rm_drive(sc, d, flags);
694 				/*gv_setup_objects(sc);*/
695 				break;
696 
697 			case GV_EVENT_RM_VOLUME:
698 				G_VINUM_DEBUG(2, "event 'remove volume'");
699 				v = ev->arg1;
700 				gv_rm_vol(sc, v);
701 				/*gv_setup_objects(sc);*/
702 				break;
703 
704 			case GV_EVENT_RM_PLEX:
705 				G_VINUM_DEBUG(2, "event 'remove plex'");
706 				p = ev->arg1;
707 				gv_rm_plex(sc, p);
708 				/*gv_setup_objects(sc);*/
709 				break;
710 
711 			case GV_EVENT_RM_SD:
712 				G_VINUM_DEBUG(2, "event 'remove sd'");
713 				s = ev->arg1;
714 				gv_rm_sd(sc, s);
715 				/*gv_setup_objects(sc);*/
716 				break;
717 
718 			case GV_EVENT_SAVE_CONFIG:
719 				G_VINUM_DEBUG(2, "event 'save config'");
720 				gv_save_config(sc);
721 				break;
722 
723 			case GV_EVENT_SET_SD_STATE:
724 				G_VINUM_DEBUG(2, "event 'setstate sd'");
725 				s = ev->arg1;
726 				newstate = ev->arg3;
727 				flags = ev->arg4;
728 				err = gv_set_sd_state(s, newstate, flags);
729 				if (err)
730 					G_VINUM_DEBUG(0, "error setting subdisk"
731 					    " state: error code %d", err);
732 				break;
733 
734 			case GV_EVENT_SET_DRIVE_STATE:
735 				G_VINUM_DEBUG(2, "event 'setstate drive'");
736 				d = ev->arg1;
737 				newstate = ev->arg3;
738 				flags = ev->arg4;
739 				err = gv_set_drive_state(d, newstate, flags);
740 				if (err)
741 					G_VINUM_DEBUG(0, "error setting drive "
742 					    "state: error code %d", err);
743 				break;
744 
745 			case GV_EVENT_SET_VOL_STATE:
746 				G_VINUM_DEBUG(2, "event 'setstate volume'");
747 				v = ev->arg1;
748 				newstate = ev->arg3;
749 				flags = ev->arg4;
750 				err = gv_set_vol_state(v, newstate, flags);
751 				if (err)
752 					G_VINUM_DEBUG(0, "error setting volume "
753 					    "state: error code %d", err);
754 				break;
755 
756 			case GV_EVENT_SET_PLEX_STATE:
757 				G_VINUM_DEBUG(2, "event 'setstate plex'");
758 				p = ev->arg1;
759 				newstate = ev->arg3;
760 				flags = ev->arg4;
761 				err = gv_set_plex_state(p, newstate, flags);
762 				if (err)
763 					G_VINUM_DEBUG(0, "error setting plex "
764 					    "state: error code %d", err);
765 				break;
766 
767 			case GV_EVENT_SETUP_OBJECTS:
768 				G_VINUM_DEBUG(2, "event 'setup objects'");
769 				gv_setup_objects(sc);
770 				break;
771 
772 			case GV_EVENT_RESET_CONFIG:
773 				G_VINUM_DEBUG(2, "event 'resetconfig'");
774 				err = gv_resetconfig(sc);
775 				if (err)
776 					G_VINUM_DEBUG(0, "error resetting "
777 					    "config: error code %d", err);
778 				break;
779 
780 			case GV_EVENT_PARITY_REBUILD:
781 				/*
782 				 * Start the rebuild. The gv_plex_done will
783 				 * handle issuing of the remaining rebuild bio's
784 				 * until it's finished.
785 				 */
786 				G_VINUM_DEBUG(2, "event 'rebuild'");
787 				p = ev->arg1;
788 				if (p->state != GV_PLEX_UP) {
789 					G_VINUM_DEBUG(0, "plex %s is not "
790 					    "completely accessible", p->name);
791 					break;
792 				}
793 				if (p->flags & GV_PLEX_SYNCING ||
794 				    p->flags & GV_PLEX_REBUILDING ||
795 				    p->flags & GV_PLEX_GROWING) {
796 					G_VINUM_DEBUG(0, "plex %s is busy with "
797 					    "syncing or parity build", p->name);
798 					break;
799 				}
800 				p->synced = 0;
801 				p->flags |= GV_PLEX_REBUILDING;
802 				g_topology_assert_not();
803 				g_topology_lock();
804 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
805 				if (err) {
806 					G_VINUM_DEBUG(0, "unable to access "
807 					    "provider");
808 					break;
809 				}
810 				g_topology_unlock();
811 				gv_parity_request(p, GV_BIO_CHECK |
812 				    GV_BIO_PARITY, 0);
813 				break;
814 
815 			case GV_EVENT_PARITY_CHECK:
816 				/* Start parity check. */
817 				G_VINUM_DEBUG(2, "event 'check'");
818 				p = ev->arg1;
819 				if (p->state != GV_PLEX_UP) {
820 					G_VINUM_DEBUG(0, "plex %s is not "
821 					    "completely accessible", p->name);
822 					break;
823 				}
824 				if (p->flags & GV_PLEX_SYNCING ||
825 				    p->flags & GV_PLEX_REBUILDING ||
826 				    p->flags & GV_PLEX_GROWING) {
827 					G_VINUM_DEBUG(0, "plex %s is busy with "
828 					    "syncing or parity build", p->name);
829 					break;
830 				}
831 				p->synced = 0;
832 				g_topology_assert_not();
833 				g_topology_lock();
834 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
835 				if (err) {
836 					G_VINUM_DEBUG(0, "unable to access "
837 					    "provider");
838 					break;
839 				}
840 				g_topology_unlock();
841 				gv_parity_request(p, GV_BIO_CHECK, 0);
842 				break;
843 
844 			case GV_EVENT_START_PLEX:
845 				G_VINUM_DEBUG(2, "event 'start' plex");
846 				p = ev->arg1;
847 				gv_start_plex(p);
848 				break;
849 
850 			case GV_EVENT_START_VOLUME:
851 				G_VINUM_DEBUG(2, "event 'start' volume");
852 				v = ev->arg1;
853 				gv_start_vol(v);
854 				break;
855 
856 			case GV_EVENT_ATTACH_PLEX:
857 				G_VINUM_DEBUG(2, "event 'attach' plex");
858 				p = ev->arg1;
859 				v = ev->arg2;
860 				rename = ev->arg4;
861 				err = gv_attach_plex(p, v, rename);
862 				if (err)
863 					G_VINUM_DEBUG(0, "error attaching %s to"
864 					    " %s: error code %d", p->name,
865 					    v->name, err);
866 				break;
867 
868 			case GV_EVENT_ATTACH_SD:
869 				G_VINUM_DEBUG(2, "event 'attach' sd");
870 				s = ev->arg1;
871 				p = ev->arg2;
872 				offset = ev->arg3;
873 				rename = ev->arg4;
874 				err = gv_attach_sd(s, p, offset, rename);
875 				if (err)
876 					G_VINUM_DEBUG(0, "error attaching %s to"
877 					    " %s: error code %d", s->name,
878 					    p->name, err);
879 				break;
880 
881 			case GV_EVENT_DETACH_PLEX:
882 				G_VINUM_DEBUG(2, "event 'detach' plex");
883 				p = ev->arg1;
884 				flags = ev->arg3;
885 				err = gv_detach_plex(p, flags);
886 				if (err)
887 					G_VINUM_DEBUG(0, "error detaching %s: "
888 					    "error code %d", p->name, err);
889 				break;
890 
891 			case GV_EVENT_DETACH_SD:
892 				G_VINUM_DEBUG(2, "event 'detach' sd");
893 				s = ev->arg1;
894 				flags = ev->arg3;
895 				err = gv_detach_sd(s, flags);
896 				if (err)
897 					G_VINUM_DEBUG(0, "error detaching %s: "
898 					    "error code %d", s->name, err);
899 				break;
900 
901 			case GV_EVENT_RENAME_VOL:
902 				G_VINUM_DEBUG(2, "event 'rename' volume");
903 				v = ev->arg1;
904 				newname = ev->arg2;
905 				flags = ev->arg3;
906 				err = gv_rename_vol(sc, v, newname, flags);
907 				if (err)
908 					G_VINUM_DEBUG(0, "error renaming %s to "
909 					    "%s: error code %d", v->name,
910 					    newname, err);
911 				g_free(newname);
912 				/* Destroy and recreate the provider if we can. */
913 				if (gv_provider_is_open(v->provider)) {
914 					G_VINUM_DEBUG(0, "unable to rename "
915 					    "provider to %s: provider in use",
916 					    v->name);
917 					break;
918 				}
919 				g_topology_lock();
920 				g_wither_provider(v->provider, ENOENT);
921 				g_topology_unlock();
922 				v->provider = NULL;
923 				gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc,
924 				    NULL, 0, 0);
925 				break;
926 
927 			case GV_EVENT_RENAME_PLEX:
928 				G_VINUM_DEBUG(2, "event 'rename' plex");
929 				p = ev->arg1;
930 				newname = ev->arg2;
931 				flags = ev->arg3;
932 				err = gv_rename_plex(sc, p, newname, flags);
933 				if (err)
934 					G_VINUM_DEBUG(0, "error renaming %s to "
935 					    "%s: error code %d", p->name,
936 					    newname, err);
937 				g_free(newname);
938 				break;
939 
940 			case GV_EVENT_RENAME_SD:
941 				G_VINUM_DEBUG(2, "event 'rename' sd");
942 				s = ev->arg1;
943 				newname = ev->arg2;
944 				flags = ev->arg3;
945 				err = gv_rename_sd(sc, s, newname, flags);
946 				if (err)
947 					G_VINUM_DEBUG(0, "error renaming %s to "
948 					    "%s: error code %d", s->name,
949 					    newname, err);
950 				g_free(newname);
951 				break;
952 
953 			case GV_EVENT_RENAME_DRIVE:
954 				G_VINUM_DEBUG(2, "event 'rename' drive");
955 				d = ev->arg1;
956 				newname = ev->arg2;
957 				flags = ev->arg3;
958 				err = gv_rename_drive(sc, d, newname, flags);
959 				if (err)
960 					G_VINUM_DEBUG(0, "error renaming %s to "
961 					    "%s: error code %d", d->name,
962 					    newname, err);
963 				g_free(newname);
964 				break;
965 
966 			case GV_EVENT_MOVE_SD:
967 				G_VINUM_DEBUG(2, "event 'move' sd");
968 				s = ev->arg1;
969 				d = ev->arg2;
970 				flags = ev->arg3;
971 				err = gv_move_sd(sc, s, d, flags);
972 				if (err)
973 					G_VINUM_DEBUG(0, "error moving %s to "
974 					    "%s: error code %d", s->name,
975 					    d->name, err);
976 				break;
977 
978 			case GV_EVENT_THREAD_EXIT:
979 				G_VINUM_DEBUG(2, "event 'thread exit'");
980 				g_free(ev);
981 				mtx_lock(&sc->equeue_mtx);
982 				mtx_lock(&sc->bqueue_mtx);
983 				gv_cleanup(sc);
984 				mtx_destroy(&sc->bqueue_mtx);
985 				mtx_destroy(&sc->equeue_mtx);
986 				g_free(sc->bqueue_down);
987 				g_free(sc->bqueue_up);
988 				g_free(sc);
989 				kproc_exit(0);
990 				/* NOTREACHED */
991 
992 			default:
993 				G_VINUM_DEBUG(1, "unknown event %d", ev->type);
994 			}
995 
996 			g_free(ev);
997 			continue;
998 		}
999 
1000 		/* ... then do I/O processing. */
1001 		mtx_lock(&sc->bqueue_mtx);
1002 		/* First do new requests. */
1003 		bp = bioq_takefirst(sc->bqueue_down);
1004 		if (bp != NULL) {
1005 			mtx_unlock(&sc->bqueue_mtx);
1006 			/* A bio that interfered with another bio. */
1007 			if (bp->bio_pflags & GV_BIO_ONHOLD) {
1008 				s = bp->bio_caller1;
1009 				p = s->plex_sc;
1010 				/* Is it still locked out? */
1011 				if (gv_stripe_active(p, bp)) {
1012 					/* Park the bio on the waiting queue. */
1013 					bioq_disksort(p->wqueue, bp);
1014 				} else {
1015 					bp->bio_pflags &= ~GV_BIO_ONHOLD;
1016 					g_io_request(bp, s->drive_sc->consumer);
1017 				}
1018 			/* A special request requireing special handling. */
1019 			} else if (bp->bio_pflags & GV_BIO_INTERNAL) {
1020 				p = bp->bio_caller1;
1021 				gv_plex_start(p, bp);
1022 			} else {
1023 				gv_volume_start(sc, bp);
1024 			}
1025 			mtx_lock(&sc->bqueue_mtx);
1026 		}
1027 		/* Then do completed requests. */
1028 		bp = bioq_takefirst(sc->bqueue_up);
1029 		if (bp == NULL) {
1030 			msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10);
1031 			mtx_unlock(&sc->bqueue_mtx);
1032 			continue;
1033 		}
1034 		mtx_unlock(&sc->bqueue_mtx);
1035 		gv_bio_done(sc, bp);
1036 	}
1037 }
1038 
1039 #define	VINUM_CLASS_NAME "VINUM"
1040 
1041 static struct g_class g_vinum_class	= {
1042 	.name = VINUM_CLASS_NAME,
1043 	.version = G_VERSION,
1044 	.init = gv_init,
1045 	.taste = gv_taste,
1046 	.ctlreq = gv_config,
1047 	.destroy_geom = gv_unload,
1048 };
1049 
1050 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);
1051 MODULE_VERSION(geom_vinum, 0);
1052