geom_vinum_drive.c revision 140475
1130389Sle/*-
2130389Sle * Copyright (c) 2004 Lukas Ertl
3130389Sle * All rights reserved.
4130389Sle *
5130389Sle * Redistribution and use in source and binary forms, with or without
6130389Sle * modification, are permitted provided that the following conditions
7130389Sle * are met:
8130389Sle * 1. Redistributions of source code must retain the above copyright
9130389Sle *    notice, this list of conditions and the following disclaimer.
10130389Sle * 2. Redistributions in binary form must reproduce the above copyright
11130389Sle *    notice, this list of conditions and the following disclaimer in the
12130389Sle *    documentation and/or other materials provided with the distribution.
13130389Sle *
14130389Sle * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15130389Sle * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16130389Sle * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17130389Sle * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18130389Sle * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19130389Sle * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20130389Sle * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21130389Sle * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22130389Sle * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23130389Sle * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24130389Sle * SUCH DAMAGE.
25130389Sle */
26130389Sle
27130389Sle#include <sys/cdefs.h>
28130389Sle__FBSDID("$FreeBSD: head/sys/geom/vinum/geom_vinum_drive.c 140475 2005-01-19 14:08:16Z le $");
29130389Sle
30130389Sle#include <sys/param.h>
31130389Sle#include <sys/bio.h>
32130389Sle#include <sys/errno.h>
33130389Sle#include <sys/conf.h>
34130389Sle#include <sys/kernel.h>
35130389Sle#include <sys/kthread.h>
36130389Sle#include <sys/libkern.h>
37130389Sle#include <sys/lock.h>
38130389Sle#include <sys/malloc.h>
39130389Sle#include <sys/module.h>
40130389Sle#include <sys/mutex.h>
41130389Sle#include <sys/sbuf.h>
42130389Sle#include <sys/systm.h>
43130389Sle#include <sys/time.h>
44130389Sle
45130389Sle#include <geom/geom.h>
46130389Sle#include <geom/vinum/geom_vinum_var.h>
47130389Sle#include <geom/vinum/geom_vinum.h>
48130389Sle#include <geom/vinum/geom_vinum_share.h>
49130389Sle
50135173Slestatic void	gv_drive_worker(void *);
51130389Slevoid	gv_drive_modify(struct gv_drive *);
52130389Sle
53130389Slevoid
54134407Slegv_config_new_drive(struct gv_drive *d)
55134407Sle{
56134407Sle	struct gv_hdr *vhdr;
57134407Sle	struct gv_freelist *fl;
58134407Sle
59134407Sle	KASSERT(d != NULL, ("config_new_drive: NULL d"));
60134407Sle
61134407Sle	vhdr = g_malloc(sizeof(*vhdr), M_WAITOK | M_ZERO);
62134407Sle	vhdr->magic = GV_MAGIC;
63134407Sle	vhdr->config_length = GV_CFG_LEN;
64134407Sle
65134407Sle	bcopy(hostname, vhdr->label.sysname, GV_HOSTNAME_LEN);
66134407Sle	strncpy(vhdr->label.name, d->name, GV_MAXDRIVENAME);
67134407Sle	microtime(&vhdr->label.date_of_birth);
68134407Sle
69134407Sle	d->hdr = vhdr;
70134407Sle
71134407Sle	LIST_INIT(&d->subdisks);
72134407Sle	LIST_INIT(&d->freelist);
73134407Sle
74134407Sle	fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO);
75134407Sle	fl->offset = GV_DATA_START;
76134407Sle	fl->size = d->avail;
77134407Sle	LIST_INSERT_HEAD(&d->freelist, fl, freelist);
78134407Sle	d->freelist_entries = 1;
79135173Sle
80135173Sle	TAILQ_INIT(&d->bqueue);
81135173Sle	mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF);
82135173Sle	kthread_create(gv_drive_worker, d, NULL, 0, 0, "gv_d %s", d->name);
83135173Sle	d->flags |= GV_DRIVE_THREAD_ACTIVE;
84134407Sle}
85134407Sle
86134407Slevoid
87130389Slegv_save_config_all(struct gv_softc *sc)
88130389Sle{
89130389Sle	struct gv_drive *d;
90130389Sle
91130389Sle	g_topology_assert();
92130389Sle
93130389Sle	LIST_FOREACH(d, &sc->drives, drive) {
94130389Sle		if (d->geom == NULL)
95130389Sle			continue;
96130389Sle		gv_save_config(NULL, d, sc);
97130389Sle	}
98130389Sle}
99130389Sle
100130389Sle/* Save the vinum configuration back to disk. */
101130389Slevoid
102130389Slegv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc)
103130389Sle{
104130389Sle	struct g_geom *gp;
105130389Sle	struct g_consumer *cp2;
106130389Sle	struct gv_hdr *vhdr, *hdr;
107130389Sle	struct sbuf *sb;
108130389Sle	int error;
109130389Sle
110130389Sle	g_topology_assert();
111130389Sle
112130389Sle	KASSERT(d != NULL, ("gv_save_config: null d"));
113130389Sle	KASSERT(sc != NULL, ("gv_save_config: null sc"));
114130389Sle
115130389Sle	if (cp == NULL) {
116130389Sle		gp = d->geom;
117130389Sle		KASSERT(gp != NULL, ("gv_save_config: null gp"));
118130389Sle		cp2 = LIST_FIRST(&gp->consumer);
119130389Sle		KASSERT(cp2 != NULL, ("gv_save_config: null cp2"));
120130389Sle	} else
121130389Sle		cp2 = cp;
122130389Sle
123130389Sle	vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO);
124130389Sle	vhdr->magic = GV_MAGIC;
125130389Sle	vhdr->config_length = GV_CFG_LEN;
126130389Sle
127130389Sle	hdr = d->hdr;
128130389Sle	if (hdr == NULL) {
129130389Sle		printf("NULL hdr!!!\n");
130130389Sle		g_free(vhdr);
131130389Sle		return;
132130389Sle	}
133130389Sle	microtime(&hdr->label.last_update);
134130389Sle	bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label));
135130389Sle
136130389Sle	sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
137130389Sle	gv_format_config(sc, sb, 1, NULL);
138130389Sle	sbuf_finish(sb);
139130389Sle
140130389Sle	error = g_access(cp2, 0, 1, 0);
141130389Sle	if (error) {
142130389Sle		printf("g_access failed: %d\n", error);
143130389Sle		sbuf_delete(sb);
144130389Sle		return;
145130389Sle	}
146130389Sle	g_topology_unlock();
147130389Sle
148130389Sle	do {
149130389Sle		error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN);
150130389Sle		if (error) {
151130389Sle			printf("writing vhdr failed: %d", error);
152130389Sle			break;
153130389Sle		}
154130389Sle
155130389Sle		error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb),
156130389Sle		    GV_CFG_LEN);
157130389Sle		if (error) {
158130389Sle			printf("writing first config copy failed: %d", error);
159130389Sle			break;
160130389Sle		}
161130389Sle
162130389Sle		error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN,
163130389Sle		    sbuf_data(sb), GV_CFG_LEN);
164130389Sle		if (error)
165130389Sle			printf("writing second config copy failed: %d", error);
166130389Sle	} while (0);
167130389Sle
168130389Sle	g_topology_lock();
169130389Sle	g_access(cp2, 0, -1, 0);
170130389Sle	sbuf_delete(sb);
171130389Sle	g_free(vhdr);
172130389Sle
173130389Sle	if (d->geom != NULL)
174130389Sle		gv_drive_modify(d);
175130389Sle}
176130389Sle
177130389Sle/* This resembles g_slice_access(). */
178130389Slestatic int
179130389Slegv_drive_access(struct g_provider *pp, int dr, int dw, int de)
180130389Sle{
181130389Sle	struct g_geom *gp;
182130389Sle	struct g_consumer *cp;
183130389Sle	struct g_provider *pp2;
184130389Sle	struct gv_drive *d;
185130389Sle	struct gv_sd *s, *s2;
186130389Sle	int error;
187130389Sle
188130389Sle	gp = pp->geom;
189130389Sle	cp = LIST_FIRST(&gp->consumer);
190135173Sle	if (cp == NULL)
191135173Sle		return (0);
192130389Sle
193130389Sle	d = gp->softc;
194130389Sle
195130389Sle	s = pp->private;
196130389Sle	KASSERT(s != NULL, ("gv_drive_access: NULL s"));
197130389Sle
198130389Sle	LIST_FOREACH(s2, &d->subdisks, from_drive) {
199130389Sle		if (s == s2)
200130389Sle			continue;
201130389Sle		if (s->drive_offset + s->size <= s2->drive_offset)
202130389Sle			continue;
203130389Sle		if (s2->drive_offset + s2->size <= s->drive_offset)
204130389Sle			continue;
205130389Sle
206130389Sle		/* Overlap. */
207130389Sle		pp2 = s2->provider;
208130389Sle		KASSERT(s2 != NULL, ("gv_drive_access: NULL s2"));
209130389Sle		if ((pp->acw + dw) > 0 && pp2->ace > 0) {
210130389Sle			printf("FOOO: permission denied - e\n");
211130389Sle			return (EPERM);
212130389Sle		}
213130389Sle		if ((pp->ace + de) > 0 && pp2->acw > 0) {
214130389Sle			printf("FOOO: permission denied - w\n");
215130389Sle			return (EPERM);
216130389Sle		}
217130389Sle	}
218130389Sle
219132617Sle#if 0
220130389Sle	/* On first open, grab an extra "exclusive" bit */
221130389Sle	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
222130389Sle		de++;
223130389Sle	/* ... and let go of it on last close */
224130389Sle	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
225130389Sle		de--;
226132617Sle#endif
227130389Sle	error = g_access(cp, dr, dw, de);
228130389Sle	if (error) {
229130389Sle		printf("FOOO: g_access failed: %d\n", error);
230130389Sle	}
231130389Sle	return (error);
232130389Sle}
233130389Sle
234130389Slestatic void
235135173Slegv_drive_done(struct bio *bp)
236135173Sle{
237135173Sle	struct gv_drive *d;
238135173Sle	struct gv_bioq *bq;
239135173Sle
240135173Sle	/* Put the BIO on the worker queue again. */
241135173Sle	d = bp->bio_from->geom->softc;
242135173Sle	bp->bio_cflags |= GV_BIO_DONE;
243135173Sle	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
244135173Sle	bq->bp = bp;
245135173Sle	mtx_lock(&d->bqueue_mtx);
246135173Sle	TAILQ_INSERT_TAIL(&d->bqueue, bq, queue);
247135173Sle	wakeup(d);
248135173Sle	mtx_unlock(&d->bqueue_mtx);
249135173Sle}
250135173Sle
251135173Sle
252135173Slestatic void
253130389Slegv_drive_start(struct bio *bp)
254130389Sle{
255130389Sle	struct gv_drive *d;
256130389Sle	struct gv_sd *s;
257135173Sle	struct gv_bioq *bq;
258130389Sle
259135173Sle	switch (bp->bio_cmd) {
260135173Sle	case BIO_READ:
261135173Sle	case BIO_WRITE:
262135173Sle	case BIO_DELETE:
263135173Sle		break;
264135173Sle	case BIO_GETATTR:
265135173Sle	default:
266135173Sle		g_io_deliver(bp, EOPNOTSUPP);
267135173Sle		return;
268135173Sle	}
269130389Sle
270135173Sle	s = bp->bio_to->private;
271130389Sle	if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) {
272130389Sle		g_io_deliver(bp, ENXIO);
273130389Sle		return;
274130389Sle	}
275130389Sle
276135173Sle	d = bp->bio_to->geom->softc;
277130389Sle
278135173Sle	/*
279135173Sle	 * Put the BIO on the worker queue, where the worker thread will pick
280135173Sle	 * it up.
281135173Sle	 */
282135173Sle	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
283135173Sle	bq->bp = bp;
284135173Sle	mtx_lock(&d->bqueue_mtx);
285135173Sle	TAILQ_INSERT_TAIL(&d->bqueue, bq, queue);
286135173Sle	wakeup(d);
287135173Sle	mtx_unlock(&d->bqueue_mtx);
288130389Sle
289135173Sle}
290135173Sle
291135173Slestatic void
292135173Slegv_drive_worker(void *arg)
293135173Sle{
294135173Sle	struct bio *bp, *cbp;
295135173Sle	struct g_geom *gp;
296135173Sle	struct g_provider *pp;
297135173Sle	struct g_consumer *cp;
298135173Sle	struct gv_drive *d;
299135173Sle	struct gv_sd *s;
300135173Sle	struct gv_bioq *bq, *bq2;
301135173Sle	int error;
302135173Sle
303135173Sle	d = arg;
304135173Sle
305135173Sle	mtx_lock(&d->bqueue_mtx);
306135173Sle	for (;;) {
307135173Sle		/* We were signaled to exit. */
308135173Sle		if (d->flags & GV_DRIVE_THREAD_DIE)
309135173Sle			break;
310135173Sle
311135173Sle		/* Take the first BIO from out queue. */
312135173Sle		bq = TAILQ_FIRST(&d->bqueue);
313135173Sle		if (bq == NULL) {
314135173Sle			msleep(d, &d->bqueue_mtx, PRIBIO, "-", hz/10);
315135173Sle			continue;
316135173Sle 		}
317135173Sle		TAILQ_REMOVE(&d->bqueue, bq, queue);
318135173Sle		mtx_unlock(&d->bqueue_mtx);
319135173Sle
320135173Sle		bp = bq->bp;
321135173Sle		g_free(bq);
322135173Sle		pp = bp->bio_to;
323135173Sle		gp = pp->geom;
324135173Sle
325135173Sle		/* Completed request. */
326135173Sle		if (bp->bio_cflags & GV_BIO_DONE) {
327135173Sle			error = bp->bio_error;
328135173Sle
329135173Sle			/* Deliver the original request. */
330135173Sle			g_std_done(bp);
331135173Sle
332135173Sle			/* The request had an error, we need to clean up. */
333135173Sle			if (error != 0) {
334135173Sle				g_topology_lock();
335135173Sle				cp = LIST_FIRST(&gp->consumer);
336135173Sle				if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
337135173Sle					g_access(cp, -cp->acr, -cp->acw,
338135173Sle					    -cp->ace);
339135173Sle				gv_set_drive_state(d, GV_DRIVE_DOWN,
340135173Sle				    GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG);
341135173Sle				if (cp->nstart == cp->nend) {
342135173Sle					g_detach(cp);
343135173Sle					g_destroy_consumer(cp);
344135173Sle				}
345135173Sle				g_topology_unlock();
346135173Sle			}
347135173Sle
348135173Sle		/* New request, needs to be sent downwards. */
349135173Sle		} else {
350135173Sle			s = pp->private;
351135173Sle
352135173Sle			if ((s->state == GV_SD_DOWN) ||
353135173Sle			    (s->state == GV_SD_STALE)) {
354135173Sle				g_io_deliver(bp, ENXIO);
355135173Sle				mtx_lock(&d->bqueue_mtx);
356135173Sle				continue;
357135173Sle			}
358135173Sle			if (bp->bio_offset > s->size) {
359135173Sle				g_io_deliver(bp, EINVAL);
360135173Sle				mtx_lock(&d->bqueue_mtx);
361135173Sle				continue;
362135173Sle			}
363135173Sle
364135173Sle			cbp = g_clone_bio(bp);
365135173Sle			if (cbp == NULL) {
366135173Sle				g_io_deliver(bp, ENOMEM);
367135173Sle				mtx_lock(&d->bqueue_mtx);
368135173Sle				continue;
369135173Sle			}
370135173Sle			if (cbp->bio_offset + cbp->bio_length > s->size)
371135173Sle				cbp->bio_length = s->size -
372135173Sle				    cbp->bio_offset;
373135173Sle			cbp->bio_done = gv_drive_done;
374135173Sle			cbp->bio_offset += s->drive_offset;
375135173Sle			g_io_request(cbp, LIST_FIRST(&gp->consumer));
376130389Sle		}
377130389Sle
378135173Sle		mtx_lock(&d->bqueue_mtx);
379130389Sle	}
380135173Sle
381135173Sle	TAILQ_FOREACH_SAFE(bq, &d->bqueue, queue, bq2) {
382135173Sle		TAILQ_REMOVE(&d->bqueue, bq, queue);
383135173Sle		mtx_unlock(&d->bqueue_mtx);
384135173Sle		bp = bq->bp;
385135173Sle		g_free(bq);
386135173Sle		if (bp->bio_cflags & GV_BIO_DONE)
387135173Sle			g_std_done(bp);
388135173Sle		else
389135173Sle			g_io_deliver(bp, ENXIO);
390135173Sle		mtx_lock(&d->bqueue_mtx);
391135173Sle	}
392135173Sle	mtx_unlock(&d->bqueue_mtx);
393135173Sle	d->flags |= GV_DRIVE_THREAD_DEAD;
394135173Sle
395135173Sle	kthread_exit(ENXIO);
396130389Sle}
397130389Sle
398135173Sle
399130389Slestatic void
400130389Slegv_drive_orphan(struct g_consumer *cp)
401130389Sle{
402130389Sle	struct g_geom *gp;
403130597Sle	struct gv_drive *d;
404130597Sle	struct gv_sd *s;
405130389Sle	int error;
406130389Sle
407130389Sle	g_topology_assert();
408130389Sle	gp = cp->geom;
409130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name);
410130389Sle	if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
411130389Sle		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
412130389Sle	error = cp->provider->error;
413130389Sle	if (error == 0)
414130389Sle		error = ENXIO;
415130389Sle	g_detach(cp);
416130389Sle	g_destroy_consumer(cp);
417130389Sle	if (!LIST_EMPTY(&gp->consumer))
418130389Sle		return;
419130597Sle	d = gp->softc;
420130697Sle	if (d != NULL) {
421130697Sle		printf("gvinum: lost drive '%s'\n", d->name);
422130697Sle		d->geom = NULL;
423130697Sle		LIST_FOREACH(s, &d->subdisks, from_drive) {
424130697Sle			s->provider = NULL;
425130697Sle			s->consumer = NULL;
426130697Sle		}
427135173Sle		gv_kill_drive_thread(d);
428135162Sle		gv_set_drive_state(d, GV_DRIVE_DOWN,
429135162Sle		    GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG);
430130597Sle	}
431130597Sle	gp->softc = NULL;
432130389Sle	g_wither_geom(gp, error);
433130389Sle}
434130389Sle
435130389Slestatic struct g_geom *
436130389Slegv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
437130389Sle{
438130389Sle	struct g_geom *gp, *gp2;
439130389Sle	struct g_consumer *cp;
440130389Sle	struct gv_drive *d;
441130389Sle	struct gv_sd *s;
442130389Sle	struct gv_softc *sc;
443130389Sle	struct gv_freelist *fl;
444130389Sle	struct gv_hdr *vhdr;
445130389Sle	int error;
446132642Sle	char *buf, errstr[ERRBUFSIZ];
447130389Sle
448130389Sle	vhdr = NULL;
449130389Sle	d = NULL;
450130389Sle
451130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name);
452130389Sle	g_topology_assert();
453130389Sle
454130389Sle	if (pp->sectorsize == 0)
455130389Sle		return(NULL);
456130389Sle
457130389Sle	/* Find the VINUM class and its associated geom. */
458130389Sle	gp2 = find_vinum_geom();
459130389Sle	if (gp2 == NULL)
460130389Sle		return (NULL);
461130389Sle	sc = gp2->softc;
462130389Sle
463130389Sle	gp = g_new_geomf(mp, "%s.vinumdrive", pp->name);
464133983Sle	gp->start = gv_drive_start;
465133983Sle	gp->orphan = gv_drive_orphan;
466133983Sle	gp->access = gv_drive_access;
467133983Sle	gp->start = gv_drive_start;
468130389Sle
469130389Sle	cp = g_new_consumer(gp);
470130389Sle	g_attach(cp, pp);
471130389Sle	error = g_access(cp, 1, 0, 0);
472130389Sle	if (error) {
473130389Sle		g_detach(cp);
474130389Sle		g_destroy_consumer(cp);
475130389Sle		g_destroy_geom(gp);
476130389Sle		return (NULL);
477130389Sle	}
478130389Sle
479130389Sle	g_topology_unlock();
480130389Sle
481130389Sle	/* Now check if the provided slice is a valid vinum drive. */
482130389Sle	do {
483135161Sle		vhdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, &error);
484130389Sle		if (vhdr == NULL || error != 0)
485130389Sle			break;
486130389Sle		if (vhdr->magic != GV_MAGIC) {
487130389Sle			g_free(vhdr);
488130389Sle			break;
489130389Sle		}
490130389Sle
491130389Sle		/*
492130389Sle		 * We have found a valid vinum drive.  Let's see if it is
493132642Sle		 * already known in the configuration.  There's a chance that
494132642Sle		 * the VINUMDRIVE class tastes before the VINUM class could
495132642Sle		 * taste, so parse the configuration here too, just to be on
496132642Sle		 * the safe side.
497130389Sle		 */
498132642Sle		buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error);
499132642Sle		if (buf == NULL || error != 0) {
500132642Sle			g_free(vhdr);
501132642Sle			break;
502132642Sle		}
503133449Sle		g_topology_lock();
504132642Sle		gv_parse_config(sc, buf, 1);
505132642Sle		g_free(buf);
506132642Sle
507130389Sle		d = gv_find_drive(sc, vhdr->label.name);
508130389Sle
509130389Sle		/* We already know about this drive. */
510130389Sle		if (d != NULL) {
511133983Sle			/* Check if this drive already has a geom. */
512133983Sle			if (d->geom != NULL) {
513133983Sle				g_topology_unlock();
514133983Sle				break;
515133983Sle			}
516130389Sle			bcopy(vhdr, d->hdr, sizeof(*vhdr));
517130389Sle
518130389Sle		/* This is a new drive. */
519130389Sle		} else {
520130389Sle			d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
521130389Sle
522130389Sle			/* Initialize all needed variables. */
523130389Sle			d->size = pp->mediasize - GV_DATA_START;
524130389Sle			d->avail = d->size;
525130389Sle			d->hdr = vhdr;
526130389Sle			strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME);
527130389Sle			LIST_INIT(&d->subdisks);
528130389Sle			LIST_INIT(&d->freelist);
529130389Sle
530130389Sle			/* We also need a freelist entry. */
531130389Sle			fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
532130389Sle			fl->offset = GV_DATA_START;
533130389Sle			fl->size = d->avail;
534130389Sle			LIST_INSERT_HEAD(&d->freelist, fl, freelist);
535130389Sle			d->freelist_entries = 1;
536130389Sle
537135173Sle			TAILQ_INIT(&d->bqueue);
538140475Sle
539140475Sle			/* Save it into the main configuration. */
540140475Sle			LIST_INSERT_HEAD(&sc->drives, d, drive);
541140475Sle		}
542140475Sle
543140475Sle		/*
544140475Sle		 * Create a bio queue mutex and a worker thread, if necessary.
545140475Sle		 */
546140475Sle		if (mtx_initialized(&d->bqueue_mtx) == 0)
547135173Sle			mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF);
548140475Sle
549140475Sle		if (!(d->flags & GV_DRIVE_THREAD_ACTIVE)) {
550135173Sle			kthread_create(gv_drive_worker, d, NULL, 0, 0,
551135173Sle			    "gv_d %s", d->name);
552135173Sle			d->flags |= GV_DRIVE_THREAD_ACTIVE;
553130389Sle		}
554130389Sle
555133983Sle		g_access(cp, -1, 0, 0);
556132617Sle
557130389Sle		gp->softc = d;
558130389Sle		d->geom = gp;
559135173Sle		d->vinumconf = sc;
560130389Sle		strncpy(d->device, pp->name, GV_MAXDRIVENAME);
561130389Sle
562130389Sle		/*
563130389Sle		 * Find out which subdisks belong to this drive and crosslink
564130389Sle		 * them.
565130389Sle		 */
566130389Sle		LIST_FOREACH(s, &sc->subdisks, sd) {
567130389Sle			if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME))
568130389Sle				/* XXX: errors ignored */
569130389Sle				gv_sd_to_drive(sc, d, s, errstr,
570130389Sle				    sizeof(errstr));
571130389Sle		}
572130389Sle
573130389Sle		/* This drive is now up for sure. */
574130389Sle		gv_set_drive_state(d, GV_DRIVE_UP, 0);
575130389Sle
576130389Sle		/*
577130389Sle		 * If there are subdisks on this drive, we need to create
578130389Sle		 * providers for them.
579130389Sle		 */
580130389Sle		if (d->sdcount)
581130389Sle			gv_drive_modify(d);
582130389Sle
583130389Sle		return (gp);
584130389Sle
585130389Sle	} while (0);
586130389Sle
587130389Sle	g_topology_lock();
588130389Sle	g_access(cp, -1, 0, 0);
589130389Sle
590130389Sle	g_detach(cp);
591130389Sle	g_destroy_consumer(cp);
592130389Sle	g_destroy_geom(gp);
593130389Sle	return (NULL);
594130389Sle}
595130389Sle
596130389Sle/*
597130389Sle * Modify the providers for the given drive 'd'.  It is assumed that the
598130389Sle * subdisk list of 'd' is already correctly set up.
599130389Sle */
600130389Slevoid
601130389Slegv_drive_modify(struct gv_drive *d)
602130389Sle{
603130389Sle	struct g_geom *gp;
604130389Sle	struct g_consumer *cp;
605130389Sle	struct g_provider *pp, *pp2;
606130389Sle	struct gv_sd *s;
607130389Sle	int nsd;
608130389Sle
609130389Sle	KASSERT(d != NULL, ("gv_drive_modify: null d"));
610130389Sle	gp = d->geom;
611130389Sle	KASSERT(gp != NULL, ("gv_drive_modify: null gp"));
612130389Sle	cp = LIST_FIRST(&gp->consumer);
613130389Sle	KASSERT(cp != NULL, ("gv_drive_modify: null cp"));
614130389Sle	pp = cp->provider;
615130389Sle	KASSERT(pp != NULL, ("gv_drive_modify: null pp"));
616130389Sle
617130389Sle	g_topology_assert();
618130389Sle
619130389Sle	nsd = 0;
620130389Sle	LIST_FOREACH(s, &d->subdisks, from_drive) {
621130389Sle		/* This subdisk already has a provider. */
622130389Sle		if (s->provider != NULL)
623130389Sle			continue;
624130389Sle		pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name);
625130389Sle		pp2->mediasize = s->size;
626130389Sle		pp2->sectorsize = pp->sectorsize;
627130389Sle		g_error_provider(pp2, 0);
628130389Sle		s->provider = pp2;
629130389Sle		pp2->private = s;
630130389Sle	}
631130389Sle}
632130389Sle
633130389Slestatic int
634130389Slegv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp,
635130389Sle    struct g_geom *gp)
636130389Sle{
637135173Sle	struct gv_drive *d;
638135173Sle
639130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name);
640130389Sle	g_topology_assert();
641130389Sle
642135173Sle	d = gp->softc;
643135173Sle	gv_kill_drive_thread(d);
644135173Sle
645130389Sle	g_wither_geom(gp, ENXIO);
646130389Sle	return (0);
647130389Sle}
648130389Sle
649130389Sle#define	VINUMDRIVE_CLASS_NAME "VINUMDRIVE"
650130389Sle
651130389Slestatic struct g_class g_vinum_drive_class = {
652130389Sle	.name = VINUMDRIVE_CLASS_NAME,
653133318Sphk	.version = G_VERSION,
654130389Sle	.taste = gv_drive_taste,
655130389Sle	.destroy_geom = gv_drive_destroy_geom
656130389Sle};
657130389Sle
658130389SleDECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive);
659