geom_vinum_plex.c revision 136983
1/*-
2 * Copyright (c) 2004 Lukas Ertl
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/vinum/geom_vinum_plex.c 136983 2004-10-26 21:01:42Z le $");
29
30#include <sys/param.h>
31#include <sys/bio.h>
32#include <sys/kernel.h>
33#include <sys/kthread.h>
34#include <sys/libkern.h>
35#include <sys/lock.h>
36#include <sys/malloc.h>
37#include <sys/module.h>
38#include <sys/mutex.h>
39#include <sys/systm.h>
40
41#include <geom/geom.h>
42#include <geom/vinum/geom_vinum_var.h>
43#include <geom/vinum/geom_vinum_raid5.h>
44#include <geom/vinum/geom_vinum.h>
45
46static void gv_plex_completed_request(struct gv_plex *, struct bio *);
47static void gv_plex_normal_request(struct gv_plex *, struct bio *);
48static void gv_plex_worker(void *);
49
50/* XXX: is this the place to catch dying subdisks? */
51static void
52gv_plex_orphan(struct g_consumer *cp)
53{
54	struct g_geom *gp;
55	struct gv_plex *p;
56	int error;
57
58	g_topology_assert();
59	gp = cp->geom;
60	g_trace(G_T_TOPOLOGY, "gv_plex_orphan(%s)", gp->name);
61
62	if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
63		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
64	error = cp->provider->error;
65	if (error == 0)
66		error = ENXIO;
67	g_detach(cp);
68	g_destroy_consumer(cp);
69	if (!LIST_EMPTY(&gp->consumer))
70		return;
71
72	p = gp->softc;
73	if (p != NULL) {
74		gv_kill_plex_thread(p);
75		p->geom = NULL;
76		p->provider = NULL;
77		p->consumer = NULL;
78	}
79	gp->softc = NULL;
80	g_wither_geom(gp, error);
81}
82
83void
84gv_plex_done(struct bio *bp)
85{
86	struct gv_plex *p;
87	struct gv_bioq *bq;
88
89	p = bp->bio_from->geom->softc;
90	bp->bio_cflags |= GV_BIO_DONE;
91	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
92	bq->bp = bp;
93	mtx_lock(&p->bqueue_mtx);
94	TAILQ_INSERT_TAIL(&p->bqueue, bq, queue);
95	wakeup(p);
96	mtx_unlock(&p->bqueue_mtx);
97}
98
99/* Find the correct subdisk to send the bio to and build a bio to send. */
100static int
101gv_plexbuffer(struct gv_plex *p, struct bio *bp, caddr_t addr, off_t boff, off_t bcount)
102{
103	struct g_geom *gp;
104	struct gv_sd *s;
105	struct bio *cbp, *pbp;
106	int i, sdno;
107	off_t len_left, real_len, real_off;
108	off_t stripeend, stripeno, stripestart;
109
110	if (p == NULL || LIST_EMPTY(&p->subdisks))
111		return (ENXIO);
112
113	s = NULL;
114	gp = bp->bio_to->geom;
115
116	/*
117	 * We only handle concatenated and striped plexes here.  RAID5 plexes
118	 * are handled in build_raid5_request().
119	 */
120	switch (p->org) {
121	case GV_PLEX_CONCAT:
122		/*
123		 * Find the subdisk where this request starts.  The subdisks in
124		 * this list must be ordered by plex_offset.
125		 */
126		LIST_FOREACH(s, &p->subdisks, in_plex) {
127			if (s->plex_offset <= boff &&
128			    s->plex_offset + s->size > boff)
129				break;
130		}
131		/* Subdisk not found. */
132		if (s == NULL)
133			return (ENXIO);
134
135		/* Calculate corresponding offsets on disk. */
136		real_off = boff - s->plex_offset;
137		len_left = s->size - real_off;
138		real_len = (bcount > len_left) ? len_left : bcount;
139		break;
140
141	case GV_PLEX_STRIPED:
142		/* The number of the stripe where the request starts. */
143		stripeno = boff / p->stripesize;
144
145		/* The number of the subdisk where the stripe resides. */
146		sdno = stripeno % p->sdcount;
147
148		/* Find the right subdisk. */
149		i = 0;
150		LIST_FOREACH(s, &p->subdisks, in_plex) {
151			if (i == sdno)
152				break;
153			i++;
154		}
155
156		/* Subdisk not found. */
157		if (s == NULL)
158			return (ENXIO);
159
160		/* The offset of the stripe from the start of the subdisk. */
161		stripestart = (stripeno / p->sdcount) *
162		    p->stripesize;
163
164		/* The offset at the end of the stripe. */
165		stripeend = stripestart + p->stripesize;
166
167		/* The offset of the request on this subdisk. */
168		real_off = boff - (stripeno * p->stripesize) +
169		    stripestart;
170
171		/* The length left in this stripe. */
172		len_left = stripeend - real_off;
173
174		real_len = (bcount <= len_left) ? bcount : len_left;
175		break;
176
177	default:
178		return (EINVAL);
179	}
180
181	/* Now check if we can handle the request on this subdisk. */
182	switch (s->state) {
183	case GV_SD_UP:
184		/* If the subdisk is up, just continue. */
185		break;
186
187	case GV_SD_STALE:
188		if (!(bp->bio_cflags & GV_BIO_SYNCREQ))
189			return (ENXIO);
190
191		printf("GEOM_VINUM: sd %s is initializing\n", s->name);
192		gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
193		break;
194
195	case GV_SD_INITIALIZING:
196		if (bp->bio_cmd == BIO_READ)
197			return (ENXIO);
198		break;
199
200	default:
201		/* All other subdisk states mean it's not accessible. */
202		return (ENXIO);
203	}
204
205	/* Clone the bio and adjust the offsets and sizes. */
206	cbp = g_clone_bio(bp);
207	if (cbp == NULL)
208		return (ENOMEM);
209	cbp->bio_offset = real_off;
210	cbp->bio_length = real_len;
211	cbp->bio_data = addr;
212	cbp->bio_done = g_std_done;
213	cbp->bio_caller2 = s->consumer;
214	if ((bp->bio_cflags & GV_BIO_SYNCREQ)) {
215		cbp->bio_cflags |= GV_BIO_SYNCREQ;
216		cbp->bio_done = gv_plex_done;
217	}
218
219	if (bp->bio_driver1 == NULL) {
220		bp->bio_driver1 = cbp;
221	} else {
222		pbp = bp->bio_driver1;
223		while (pbp->bio_caller1 != NULL)
224			pbp = pbp->bio_caller1;
225		pbp->bio_caller1 = cbp;
226	}
227
228	return (0);
229}
230
231static void
232gv_plex_start(struct bio *bp)
233{
234	struct gv_plex *p;
235	struct gv_bioq *bq;
236
237	switch(bp->bio_cmd) {
238	case BIO_READ:
239	case BIO_WRITE:
240	case BIO_DELETE:
241		break;
242	case BIO_GETATTR:
243	default:
244		g_io_deliver(bp, EOPNOTSUPP);
245		return;
246	}
247
248	/*
249	 * We cannot handle this request if too many of our subdisks are
250	 * inaccessible.
251	 */
252	p = bp->bio_to->geom->softc;
253	if ((p->state < GV_PLEX_DEGRADED) &&
254	    !(bp->bio_cflags & GV_BIO_SYNCREQ)) {
255		g_io_deliver(bp, ENXIO);
256		return;
257	}
258
259	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
260	bq->bp = bp;
261	mtx_lock(&p->bqueue_mtx);
262	TAILQ_INSERT_TAIL(&p->bqueue, bq, queue);
263	wakeup(p);
264	mtx_unlock(&p->bqueue_mtx);
265}
266
267static void
268gv_plex_worker(void *arg)
269{
270	struct bio *bp;
271	struct gv_plex *p;
272	struct gv_sd *s;
273	struct gv_bioq *bq;
274
275	p = arg;
276	KASSERT(p != NULL, ("NULL p"));
277
278	mtx_lock(&p->bqueue_mtx);
279	for (;;) {
280		/* We were signaled to exit. */
281		if (p->flags & GV_PLEX_THREAD_DIE)
282			break;
283
284		/* Take the first BIO from our queue. */
285		bq = TAILQ_FIRST(&p->bqueue);
286		if (bq == NULL) {
287			msleep(p, &p->bqueue_mtx, PRIBIO, "-", hz/10);
288			continue;
289		}
290		TAILQ_REMOVE(&p->bqueue, bq, queue);
291		mtx_unlock(&p->bqueue_mtx);
292
293		bp = bq->bp;
294
295		/* A completed request. */
296		if (bp->bio_cflags & GV_BIO_DONE) {
297			g_free(bq);
298
299			if (bp->bio_cflags & GV_BIO_SYNCREQ ||
300			    bp->bio_cflags & GV_BIO_REBUILD) {
301				s = bp->bio_to->private;
302				if (bp->bio_error == 0)
303					s->initialized += bp->bio_length;
304				if (s->initialized >= s->size) {
305					g_topology_lock();
306					gv_set_sd_state(s, GV_SD_UP,
307					    GV_SETSTATE_CONFIG);
308					g_topology_unlock();
309					s->initialized = 0;
310				}
311			}
312
313			if (bp->bio_cflags & GV_BIO_SYNCREQ)
314				g_std_done(bp);
315			else
316				gv_plex_completed_request(p, bp);
317		/*
318		 * A sub-request that was hold back because it interfered with
319		 * another sub-request.
320		 */
321		} else if (bp->bio_cflags & GV_BIO_ONHOLD) {
322			/* Is it still locked out? */
323			if (gv_stripe_active(p, bp)) {
324				/* Park the bio on the waiting queue. */
325				mtx_lock(&p->bqueue_mtx);
326				TAILQ_INSERT_TAIL(&p->wqueue, bq, queue);
327				mtx_unlock(&p->bqueue_mtx);
328			} else {
329				g_free(bq);
330				bp->bio_cflags &= ~GV_BIO_ONHOLD;
331				g_io_request(bp, bp->bio_caller2);
332			}
333
334		/* A normal request to this plex. */
335		} else {
336			g_free(bq);
337			gv_plex_normal_request(p, bp);
338		}
339
340		mtx_lock(&p->bqueue_mtx);
341	}
342	mtx_unlock(&p->bqueue_mtx);
343	p->flags |= GV_PLEX_THREAD_DEAD;
344	wakeup(p);
345
346	kthread_exit(ENXIO);
347}
348
349void
350gv_plex_completed_request(struct gv_plex *p, struct bio *bp)
351{
352	struct bio *cbp, *pbp;
353	struct gv_bioq *bq, *bq2;
354	struct gv_raid5_packet *wp;
355	int i;
356
357	wp = bp->bio_driver1;
358
359	switch (bp->bio_parent->bio_cmd) {
360	case BIO_READ:
361		if (wp == NULL)
362			break;
363
364		TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
365			if (bq->bp == bp) {
366				TAILQ_REMOVE(&wp->bits, bq, queue);
367				g_free(bq);
368				for (i = 0; i < wp->length; i++)
369					wp->data[i] ^= bp->bio_data[i];
370				break;
371			}
372		}
373		if (TAILQ_EMPTY(&wp->bits)) {
374			bp->bio_parent->bio_completed += wp->length;
375			if (wp->lockbase != -1) {
376				TAILQ_REMOVE(&p->packets, wp, list);
377				/* Bring the waiting bios back into the game. */
378				mtx_lock(&p->bqueue_mtx);
379				TAILQ_CONCAT(&p->bqueue, &p->wqueue, queue);
380				mtx_unlock(&p->bqueue_mtx);
381			}
382			g_free(wp);
383		}
384
385		break;
386
387 	case BIO_WRITE:
388		if (wp == NULL)
389			break;
390
391		/* Check if we need to handle parity data. */
392		TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
393			if (bq->bp == bp) {
394				TAILQ_REMOVE(&wp->bits, bq, queue);
395				g_free(bq);
396				cbp = wp->parity;
397				if (cbp != NULL) {
398					for (i = 0; i < wp->length; i++)
399						cbp->bio_data[i] ^=
400						    bp->bio_data[i];
401				}
402				break;
403			}
404		}
405
406		/* Handle parity data. */
407		if (TAILQ_EMPTY(&wp->bits)) {
408			if (wp->waiting != NULL) {
409				pbp = wp->waiting;
410				wp->waiting = NULL;
411				cbp = wp->parity;
412				for (i = 0; i < wp->length; i++)
413					cbp->bio_data[i] ^= pbp->bio_data[i];
414				g_io_request(pbp, pbp->bio_caller2);
415			} else if (wp->parity != NULL) {
416				cbp = wp->parity;
417				wp->parity = NULL;
418				g_io_request(cbp, cbp->bio_caller2);
419			} else {
420				bp->bio_parent->bio_completed += wp->length;
421				TAILQ_REMOVE(&p->packets, wp, list);
422				/* Bring the waiting bios back into the game. */
423				mtx_lock(&p->bqueue_mtx);
424				TAILQ_CONCAT(&p->bqueue, &p->wqueue, queue);
425				mtx_unlock(&p->bqueue_mtx);
426				g_free(wp);
427			}
428		}
429
430		break;
431	}
432
433	pbp = bp->bio_parent;
434	if (pbp->bio_error == 0)
435		pbp->bio_error = bp->bio_error;
436
437	/* When the original request is finished, we deliver it. */
438	pbp->bio_inbed++;
439	if (pbp->bio_inbed == pbp->bio_children)
440		g_io_deliver(pbp, pbp->bio_error);
441
442	/* Clean up what we allocated. */
443	if (bp->bio_cflags & GV_BIO_MALLOC)
444		g_free(bp->bio_data);
445	g_destroy_bio(bp);
446}
447
448void
449gv_plex_normal_request(struct gv_plex *p, struct bio *bp)
450{
451	struct bio *cbp, *pbp;
452	struct gv_bioq *bq, *bq2;
453	struct gv_raid5_packet *wp, *wp2;
454	caddr_t addr;
455	off_t bcount, boff;
456	int err;
457
458	bcount = bp->bio_length;
459	addr = bp->bio_data;
460	boff = bp->bio_offset;
461
462	/* Walk over the whole length of the request, we might split it up. */
463	while (bcount > 0) {
464		wp = NULL;
465
466 		/*
467		 * RAID5 plexes need special treatment, as a single write
468		 * request involves several read/write sub-requests.
469 		 */
470		if (p->org == GV_PLEX_RAID5) {
471			wp = g_malloc(sizeof(*wp), M_WAITOK | M_ZERO);
472			wp->bio = bp;
473			TAILQ_INIT(&wp->bits);
474
475			if (bp->bio_cflags & GV_BIO_REBUILD)
476				err = gv_rebuild_raid5(p, wp, bp, addr,
477				    boff, bcount);
478			else
479				err = gv_build_raid5_req(p, wp, bp, addr,
480				    boff, bcount);
481
482 			/*
483			 * Building the sub-request failed, we probably need to
484			 * clean up a lot.
485 			 */
486 			if (err) {
487				printf("GEOM_VINUM: plex request failed for ");
488				g_print_bio(bp);
489				printf("\n");
490				TAILQ_FOREACH_SAFE(bq, &wp->bits, queue, bq2) {
491					TAILQ_REMOVE(&wp->bits, bq, queue);
492					g_free(bq);
493				}
494				if (wp->waiting != NULL) {
495					if (wp->waiting->bio_cflags &
496					    GV_BIO_MALLOC)
497						g_free(wp->waiting->bio_data);
498					g_destroy_bio(wp->waiting);
499				}
500				if (wp->parity != NULL) {
501					if (wp->parity->bio_cflags &
502					    GV_BIO_MALLOC)
503						g_free(wp->parity->bio_data);
504					g_destroy_bio(wp->parity);
505				}
506				g_free(wp);
507
508				TAILQ_FOREACH_SAFE(wp, &p->packets, list, wp2) {
509					if (wp->bio == bp) {
510						TAILQ_REMOVE(&p->packets, wp,
511						    list);
512						TAILQ_FOREACH_SAFE(bq,
513						    &wp->bits, queue, bq2) {
514							TAILQ_REMOVE(&wp->bits,
515							    bq, queue);
516							g_free(bq);
517						}
518						g_free(wp);
519					}
520				}
521
522				cbp = bp->bio_driver1;
523				while (cbp != NULL) {
524					pbp = cbp->bio_caller1;
525					if (cbp->bio_cflags & GV_BIO_MALLOC)
526						g_free(cbp->bio_data);
527					g_destroy_bio(cbp);
528					cbp = pbp;
529				}
530
531				g_io_deliver(bp, err);
532 				return;
533 			}
534
535			if (TAILQ_EMPTY(&wp->bits))
536				g_free(wp);
537			else if (wp->lockbase != -1)
538				TAILQ_INSERT_TAIL(&p->packets, wp, list);
539
540		/*
541		 * Requests to concatenated and striped plexes go straight
542		 * through.
543		 */
544		} else {
545			err = gv_plexbuffer(p, bp, addr, boff, bcount);
546
547			/* Building the sub-request failed. */
548			if (err) {
549				printf("GEOM_VINUM: plex request failed for ");
550				g_print_bio(bp);
551				printf("\n");
552				cbp = bp->bio_driver1;
553				while (cbp != NULL) {
554					pbp = cbp->bio_caller1;
555					g_destroy_bio(cbp);
556					cbp = pbp;
557				}
558				g_io_deliver(bp, err);
559				return;
560			}
561		}
562
563		/* Abuse bio_caller1 as linked list. */
564		pbp = bp->bio_driver1;
565		while (pbp->bio_caller1 != NULL)
566			pbp = pbp->bio_caller1;
567		bcount -= pbp->bio_length;
568		addr += pbp->bio_length;
569		boff += pbp->bio_length;
570	}
571
572	/* Fire off all sub-requests. */
573	pbp = bp->bio_driver1;
574	while (pbp != NULL) {
575		/*
576		 * RAID5 sub-requests need to come in correct order, otherwise
577		 * we trip over the parity, as it might be overwritten by
578		 * another sub-request.
579		 */
580		if (pbp->bio_driver1 != NULL &&
581		    gv_stripe_active(p, pbp)) {
582			/* Park the bio on the waiting queue. */
583			pbp->bio_cflags |= GV_BIO_ONHOLD;
584			bq = g_malloc(sizeof(*bq), M_WAITOK | M_ZERO);
585			bq->bp = pbp;
586			mtx_lock(&p->bqueue_mtx);
587			TAILQ_INSERT_TAIL(&p->wqueue, bq, queue);
588			mtx_unlock(&p->bqueue_mtx);
589		} else
590			g_io_request(pbp, pbp->bio_caller2);
591		pbp = pbp->bio_caller1;
592	}
593}
594
595static int
596gv_plex_access(struct g_provider *pp, int dr, int dw, int de)
597{
598	struct g_geom *gp;
599	struct g_consumer *cp, *cp2;
600	int error;
601
602	gp = pp->geom;
603
604	error = ENXIO;
605	LIST_FOREACH(cp, &gp->consumer, consumer) {
606		error = g_access(cp, dr, dw, de);
607		if (error) {
608			LIST_FOREACH(cp2, &gp->consumer, consumer) {
609				if (cp == cp2)
610					break;
611				g_access(cp2, -dr, -dw, -de);
612			}
613			return (error);
614		}
615	}
616	return (error);
617}
618
619static struct g_geom *
620gv_plex_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
621{
622	struct g_geom *gp;
623	struct g_consumer *cp, *cp2;
624	struct g_provider *pp2;
625	struct gv_plex *p;
626	struct gv_sd *s;
627	struct gv_softc *sc;
628	int error;
629
630	g_trace(G_T_TOPOLOGY, "gv_plex_taste(%s, %s)", mp->name, pp->name);
631	g_topology_assert();
632
633	/* We only want to attach to subdisks. */
634	if (strcmp(pp->geom->class->name, "VINUMDRIVE"))
635		return (NULL);
636
637	/* Find the VINUM class and its associated geom. */
638	gp = find_vinum_geom();
639	if (gp == NULL)
640		return (NULL);
641	sc = gp->softc;
642	KASSERT(sc != NULL, ("gv_plex_taste: NULL sc"));
643
644	/* Find out which subdisk the offered provider corresponds to. */
645	s = pp->private;
646	KASSERT(s != NULL, ("gv_plex_taste: NULL s"));
647
648	/* Now find the correct plex where this subdisk belongs to. */
649	p = gv_find_plex(sc, s->plex);
650	KASSERT(p != NULL, ("gv_plex_taste: NULL p"));
651
652	/*
653	 * Add this subdisk to this plex.  Since we trust the on-disk
654	 * configuration, we don't check the given value (should we?).
655	 * XXX: shouldn't be done here
656	 */
657	gv_sd_to_plex(p, s, 0);
658
659	/* Now check if there's already a geom for this plex. */
660	gp = p->geom;
661
662	/* Yes, there is already a geom, so we just add the consumer. */
663	if (gp != NULL) {
664		cp2 = LIST_FIRST(&gp->consumer);
665		/* Need to attach a new consumer to this subdisk. */
666		cp = g_new_consumer(gp);
667		error = g_attach(cp, pp);
668		if (error) {
669			printf("geom_vinum: couldn't attach consumer to %s\n",
670			    pp->name);
671			g_destroy_consumer(cp);
672			return (NULL);
673		}
674		/* Adjust the access counts of the new consumer. */
675		if ((cp2 != NULL) && (cp2->acr || cp2->acw || cp2->ace)) {
676			error = g_access(cp, cp2->acr, cp2->acw, cp2->ace);
677			if (error) {
678				printf("geom_vinum: couldn't set access counts"
679				    " for consumer on %s\n", pp->name);
680				g_detach(cp);
681				g_destroy_consumer(cp);
682				return (NULL);
683			}
684		}
685		s->consumer = cp;
686
687		/* Adjust the size of the providers this plex has. */
688		LIST_FOREACH(pp2, &gp->provider, provider)
689			pp2->mediasize = p->size;
690
691		/* Update the size of the volume this plex is attached to. */
692		if (p->vol_sc != NULL)
693			gv_update_vol_size(p->vol_sc, p->size);
694
695		return (NULL);
696
697	/* We need to create a new geom. */
698	} else {
699		gp = g_new_geomf(mp, "%s", p->name);
700		gp->start = gv_plex_start;
701		gp->orphan = gv_plex_orphan;
702		gp->access = gv_plex_access;
703		gp->softc = p;
704		p->geom = gp;
705
706		TAILQ_INIT(&p->packets);
707		TAILQ_INIT(&p->bqueue);
708		TAILQ_INIT(&p->wqueue);
709		mtx_init(&p->bqueue_mtx, "gv_plex", NULL, MTX_DEF);
710		kthread_create(gv_plex_worker, p, NULL, 0, 0, "gv_p %s",
711		    p->name);
712		p->flags |= GV_PLEX_THREAD_ACTIVE;
713
714		/* Attach a consumer to this provider. */
715		cp = g_new_consumer(gp);
716		g_attach(cp, pp);
717		s->consumer = cp;
718
719		/* Create a provider for the outside world. */
720		pp2 = g_new_providerf(gp, "gvinum/plex/%s", p->name);
721		pp2->mediasize = p->size;
722		pp2->sectorsize = pp->sectorsize;
723		p->provider = pp2;
724		g_error_provider(pp2, 0);
725		return (gp);
726	}
727}
728
729static int
730gv_plex_destroy_geom(struct gctl_req *req, struct g_class *mp,
731    struct g_geom *gp)
732{
733	struct gv_plex *p;
734
735	g_trace(G_T_TOPOLOGY, "gv_plex_destroy_geom: %s", gp->name);
736	g_topology_assert();
737
738	p = gp->softc;
739
740	KASSERT(p != NULL, ("gv_plex_destroy_geom: null p of '%s'", gp->name));
741
742	/*
743	 * If this is a RAID5 plex, check if its worker thread is still active
744	 * and signal it to self destruct.
745	 */
746	gv_kill_plex_thread(p);
747	/* g_free(sc); */
748	g_wither_geom(gp, ENXIO);
749	return (0);
750}
751
752#define	VINUMPLEX_CLASS_NAME "VINUMPLEX"
753
754static struct g_class g_vinum_plex_class = {
755	.name = VINUMPLEX_CLASS_NAME,
756	.version = G_VERSION,
757	.taste = gv_plex_taste,
758	.destroy_geom = gv_plex_destroy_geom,
759};
760
761DECLARE_GEOM_CLASS(g_vinum_plex_class, g_vinum_plex);
762