geom_vinum_drive.c revision 146325
1130389Sle/*-
2142020Sle * Copyright (c) 2004, 2005 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 146325 2005-05-17 16:38:30Z 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
50146325Slestatic void	gv_drive_dead(void *, int);
51135173Slestatic void	gv_drive_worker(void *);
52130389Slevoid	gv_drive_modify(struct gv_drive *);
53130389Sle
54130389Slevoid
55134407Slegv_config_new_drive(struct gv_drive *d)
56134407Sle{
57134407Sle	struct gv_hdr *vhdr;
58134407Sle	struct gv_freelist *fl;
59134407Sle
60134407Sle	KASSERT(d != NULL, ("config_new_drive: NULL d"));
61134407Sle
62134407Sle	vhdr = g_malloc(sizeof(*vhdr), M_WAITOK | M_ZERO);
63134407Sle	vhdr->magic = GV_MAGIC;
64134407Sle	vhdr->config_length = GV_CFG_LEN;
65134407Sle
66134407Sle	bcopy(hostname, vhdr->label.sysname, GV_HOSTNAME_LEN);
67134407Sle	strncpy(vhdr->label.name, d->name, GV_MAXDRIVENAME);
68134407Sle	microtime(&vhdr->label.date_of_birth);
69134407Sle
70134407Sle	d->hdr = vhdr;
71134407Sle
72134407Sle	LIST_INIT(&d->subdisks);
73134407Sle	LIST_INIT(&d->freelist);
74134407Sle
75134407Sle	fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO);
76134407Sle	fl->offset = GV_DATA_START;
77134407Sle	fl->size = d->avail;
78134407Sle	LIST_INSERT_HEAD(&d->freelist, fl, freelist);
79134407Sle	d->freelist_entries = 1;
80135173Sle
81135173Sle	TAILQ_INIT(&d->bqueue);
82135173Sle	mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF);
83135173Sle	kthread_create(gv_drive_worker, d, NULL, 0, 0, "gv_d %s", d->name);
84135173Sle	d->flags |= GV_DRIVE_THREAD_ACTIVE;
85134407Sle}
86134407Sle
87134407Slevoid
88130389Slegv_save_config_all(struct gv_softc *sc)
89130389Sle{
90130389Sle	struct gv_drive *d;
91130389Sle
92130389Sle	g_topology_assert();
93130389Sle
94130389Sle	LIST_FOREACH(d, &sc->drives, drive) {
95130389Sle		if (d->geom == NULL)
96130389Sle			continue;
97130389Sle		gv_save_config(NULL, d, sc);
98130389Sle	}
99130389Sle}
100130389Sle
101130389Sle/* Save the vinum configuration back to disk. */
102130389Slevoid
103130389Slegv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc)
104130389Sle{
105130389Sle	struct g_geom *gp;
106130389Sle	struct g_consumer *cp2;
107130389Sle	struct gv_hdr *vhdr, *hdr;
108130389Sle	struct sbuf *sb;
109130389Sle	int error;
110130389Sle
111130389Sle	g_topology_assert();
112130389Sle
113130389Sle	KASSERT(d != NULL, ("gv_save_config: null d"));
114130389Sle	KASSERT(sc != NULL, ("gv_save_config: null sc"));
115130389Sle
116146325Sle	if (d->state != GV_DRIVE_UP)
117146325Sle		return;
118146325Sle
119130389Sle	if (cp == NULL) {
120130389Sle		gp = d->geom;
121130389Sle		KASSERT(gp != NULL, ("gv_save_config: null gp"));
122130389Sle		cp2 = LIST_FIRST(&gp->consumer);
123130389Sle		KASSERT(cp2 != NULL, ("gv_save_config: null cp2"));
124130389Sle	} else
125130389Sle		cp2 = cp;
126130389Sle
127130389Sle	vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO);
128130389Sle	vhdr->magic = GV_MAGIC;
129130389Sle	vhdr->config_length = GV_CFG_LEN;
130130389Sle
131130389Sle	hdr = d->hdr;
132130389Sle	if (hdr == NULL) {
133146325Sle		printf("GEOM_VINUM: drive %s has NULL hdr\n", d->name);
134130389Sle		g_free(vhdr);
135130389Sle		return;
136130389Sle	}
137130389Sle	microtime(&hdr->label.last_update);
138130389Sle	bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label));
139130389Sle
140130389Sle	sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
141130389Sle	gv_format_config(sc, sb, 1, NULL);
142130389Sle	sbuf_finish(sb);
143130389Sle
144130389Sle	error = g_access(cp2, 0, 1, 0);
145130389Sle	if (error) {
146146325Sle		printf("GEOM_VINUM: g_access failed on drive %s, errno %d\n",
147146325Sle		    d->name, error);
148130389Sle		sbuf_delete(sb);
149146325Sle		g_free(vhdr);
150130389Sle		return;
151130389Sle	}
152130389Sle	g_topology_unlock();
153130389Sle
154130389Sle	do {
155130389Sle		error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN);
156130389Sle		if (error) {
157146325Sle			printf("GEOM_VINUM: writing vhdr failed on drive %s, "
158146325Sle			    "errno %d", d->name, error);
159130389Sle			break;
160130389Sle		}
161130389Sle
162130389Sle		error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb),
163130389Sle		    GV_CFG_LEN);
164130389Sle		if (error) {
165146325Sle			printf("GEOM_VINUM: writing first config copy failed "
166146325Sle			    "on drive %s, errno %d", d->name, error);
167130389Sle			break;
168130389Sle		}
169130389Sle
170130389Sle		error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN,
171130389Sle		    sbuf_data(sb), GV_CFG_LEN);
172130389Sle		if (error)
173146325Sle			printf("GEOM_VINUM: writing second config copy failed "
174146325Sle			    "on drive %s, errno %d", d->name, error);
175130389Sle	} while (0);
176130389Sle
177130389Sle	g_topology_lock();
178130389Sle	g_access(cp2, 0, -1, 0);
179130389Sle	sbuf_delete(sb);
180130389Sle	g_free(vhdr);
181130389Sle
182130389Sle	if (d->geom != NULL)
183130389Sle		gv_drive_modify(d);
184130389Sle}
185130389Sle
186130389Sle/* This resembles g_slice_access(). */
187130389Slestatic int
188130389Slegv_drive_access(struct g_provider *pp, int dr, int dw, int de)
189130389Sle{
190130389Sle	struct g_geom *gp;
191130389Sle	struct g_consumer *cp;
192130389Sle	struct g_provider *pp2;
193130389Sle	struct gv_drive *d;
194130389Sle	struct gv_sd *s, *s2;
195130389Sle	int error;
196130389Sle
197130389Sle	gp = pp->geom;
198130389Sle	cp = LIST_FIRST(&gp->consumer);
199135173Sle	if (cp == NULL)
200135173Sle		return (0);
201130389Sle
202130389Sle	d = gp->softc;
203146325Sle	if (d == NULL)
204146325Sle		return (0);
205130389Sle
206130389Sle	s = pp->private;
207130389Sle	KASSERT(s != NULL, ("gv_drive_access: NULL s"));
208130389Sle
209130389Sle	LIST_FOREACH(s2, &d->subdisks, from_drive) {
210130389Sle		if (s == s2)
211130389Sle			continue;
212130389Sle		if (s->drive_offset + s->size <= s2->drive_offset)
213130389Sle			continue;
214130389Sle		if (s2->drive_offset + s2->size <= s->drive_offset)
215130389Sle			continue;
216130389Sle
217130389Sle		/* Overlap. */
218130389Sle		pp2 = s2->provider;
219130389Sle		KASSERT(s2 != NULL, ("gv_drive_access: NULL s2"));
220146325Sle		if ((pp->acw + dw) > 0 && pp2->ace > 0)
221130389Sle			return (EPERM);
222146325Sle		if ((pp->ace + de) > 0 && pp2->acw > 0)
223130389Sle			return (EPERM);
224130389Sle	}
225130389Sle
226130389Sle	error = g_access(cp, dr, dw, de);
227130389Sle	return (error);
228130389Sle}
229130389Sle
230130389Slestatic void
231135173Slegv_drive_done(struct bio *bp)
232135173Sle{
233135173Sle	struct gv_drive *d;
234135173Sle	struct gv_bioq *bq;
235135173Sle
236135173Sle	/* Put the BIO on the worker queue again. */
237135173Sle	d = bp->bio_from->geom->softc;
238135173Sle	bp->bio_cflags |= GV_BIO_DONE;
239135173Sle	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
240135173Sle	bq->bp = bp;
241135173Sle	mtx_lock(&d->bqueue_mtx);
242135173Sle	TAILQ_INSERT_TAIL(&d->bqueue, bq, queue);
243135173Sle	wakeup(d);
244135173Sle	mtx_unlock(&d->bqueue_mtx);
245135173Sle}
246135173Sle
247135173Sle
248135173Slestatic void
249130389Slegv_drive_start(struct bio *bp)
250130389Sle{
251130389Sle	struct gv_drive *d;
252130389Sle	struct gv_sd *s;
253135173Sle	struct gv_bioq *bq;
254130389Sle
255135173Sle	switch (bp->bio_cmd) {
256135173Sle	case BIO_READ:
257135173Sle	case BIO_WRITE:
258135173Sle	case BIO_DELETE:
259135173Sle		break;
260135173Sle	case BIO_GETATTR:
261135173Sle	default:
262135173Sle		g_io_deliver(bp, EOPNOTSUPP);
263135173Sle		return;
264135173Sle	}
265130389Sle
266135173Sle	s = bp->bio_to->private;
267130389Sle	if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) {
268130389Sle		g_io_deliver(bp, ENXIO);
269130389Sle		return;
270130389Sle	}
271130389Sle
272135173Sle	d = bp->bio_to->geom->softc;
273130389Sle
274135173Sle	/*
275135173Sle	 * Put the BIO on the worker queue, where the worker thread will pick
276135173Sle	 * it up.
277135173Sle	 */
278135173Sle	bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO);
279135173Sle	bq->bp = bp;
280135173Sle	mtx_lock(&d->bqueue_mtx);
281135173Sle	TAILQ_INSERT_TAIL(&d->bqueue, bq, queue);
282135173Sle	wakeup(d);
283135173Sle	mtx_unlock(&d->bqueue_mtx);
284130389Sle
285135173Sle}
286135173Sle
287135173Slestatic void
288135173Slegv_drive_worker(void *arg)
289135173Sle{
290135173Sle	struct bio *bp, *cbp;
291135173Sle	struct g_geom *gp;
292135173Sle	struct g_provider *pp;
293135173Sle	struct gv_drive *d;
294135173Sle	struct gv_sd *s;
295135173Sle	struct gv_bioq *bq, *bq2;
296135173Sle	int error;
297135173Sle
298135173Sle	d = arg;
299135173Sle
300135173Sle	mtx_lock(&d->bqueue_mtx);
301135173Sle	for (;;) {
302135173Sle		/* We were signaled to exit. */
303135173Sle		if (d->flags & GV_DRIVE_THREAD_DIE)
304135173Sle			break;
305135173Sle
306135173Sle		/* Take the first BIO from out queue. */
307135173Sle		bq = TAILQ_FIRST(&d->bqueue);
308135173Sle		if (bq == NULL) {
309135173Sle			msleep(d, &d->bqueue_mtx, PRIBIO, "-", hz/10);
310135173Sle			continue;
311135173Sle 		}
312135173Sle		TAILQ_REMOVE(&d->bqueue, bq, queue);
313135173Sle		mtx_unlock(&d->bqueue_mtx);
314135173Sle
315135173Sle		bp = bq->bp;
316135173Sle		g_free(bq);
317135173Sle		pp = bp->bio_to;
318135173Sle		gp = pp->geom;
319135173Sle
320135173Sle		/* Completed request. */
321135173Sle		if (bp->bio_cflags & GV_BIO_DONE) {
322135173Sle			error = bp->bio_error;
323135173Sle
324135173Sle			/* Deliver the original request. */
325135173Sle			g_std_done(bp);
326135173Sle
327135173Sle			/* The request had an error, we need to clean up. */
328135173Sle			if (error != 0) {
329135173Sle				g_topology_lock();
330135173Sle				gv_set_drive_state(d, GV_DRIVE_DOWN,
331135173Sle				    GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG);
332135173Sle				g_topology_unlock();
333146325Sle				g_post_event(gv_drive_dead, d, M_WAITOK, d,
334146325Sle				    NULL);
335135173Sle			}
336135173Sle
337135173Sle		/* New request, needs to be sent downwards. */
338135173Sle		} else {
339135173Sle			s = pp->private;
340135173Sle
341135173Sle			if ((s->state == GV_SD_DOWN) ||
342135173Sle			    (s->state == GV_SD_STALE)) {
343135173Sle				g_io_deliver(bp, ENXIO);
344135173Sle				mtx_lock(&d->bqueue_mtx);
345135173Sle				continue;
346135173Sle			}
347135173Sle			if (bp->bio_offset > s->size) {
348135173Sle				g_io_deliver(bp, EINVAL);
349135173Sle				mtx_lock(&d->bqueue_mtx);
350135173Sle				continue;
351135173Sle			}
352135173Sle
353135173Sle			cbp = g_clone_bio(bp);
354135173Sle			if (cbp == NULL) {
355135173Sle				g_io_deliver(bp, ENOMEM);
356135173Sle				mtx_lock(&d->bqueue_mtx);
357135173Sle				continue;
358135173Sle			}
359135173Sle			if (cbp->bio_offset + cbp->bio_length > s->size)
360135173Sle				cbp->bio_length = s->size -
361135173Sle				    cbp->bio_offset;
362135173Sle			cbp->bio_done = gv_drive_done;
363135173Sle			cbp->bio_offset += s->drive_offset;
364135173Sle			g_io_request(cbp, LIST_FIRST(&gp->consumer));
365130389Sle		}
366130389Sle
367135173Sle		mtx_lock(&d->bqueue_mtx);
368130389Sle	}
369135173Sle
370135173Sle	TAILQ_FOREACH_SAFE(bq, &d->bqueue, queue, bq2) {
371135173Sle		TAILQ_REMOVE(&d->bqueue, bq, queue);
372135173Sle		mtx_unlock(&d->bqueue_mtx);
373135173Sle		bp = bq->bp;
374135173Sle		g_free(bq);
375135173Sle		if (bp->bio_cflags & GV_BIO_DONE)
376135173Sle			g_std_done(bp);
377135173Sle		else
378135173Sle			g_io_deliver(bp, ENXIO);
379135173Sle		mtx_lock(&d->bqueue_mtx);
380135173Sle	}
381135173Sle	mtx_unlock(&d->bqueue_mtx);
382135173Sle	d->flags |= GV_DRIVE_THREAD_DEAD;
383135173Sle
384135173Sle	kthread_exit(ENXIO);
385130389Sle}
386130389Sle
387135173Sle
388130389Slestatic void
389130389Slegv_drive_orphan(struct g_consumer *cp)
390130389Sle{
391130389Sle	struct g_geom *gp;
392130597Sle	struct gv_drive *d;
393130389Sle
394130389Sle	g_topology_assert();
395130389Sle	gp = cp->geom;
396130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name);
397130597Sle	d = gp->softc;
398130697Sle	if (d != NULL) {
399135162Sle		gv_set_drive_state(d, GV_DRIVE_DOWN,
400135162Sle		    GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG);
401146325Sle		g_post_event(gv_drive_dead, d, M_WAITOK, d, NULL);
402146325Sle	} else
403146325Sle		g_wither_geom(gp, ENXIO);
404130389Sle}
405130389Sle
406130389Slestatic struct g_geom *
407130389Slegv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
408130389Sle{
409130389Sle	struct g_geom *gp, *gp2;
410130389Sle	struct g_consumer *cp;
411130389Sle	struct gv_drive *d;
412130389Sle	struct gv_sd *s;
413130389Sle	struct gv_softc *sc;
414130389Sle	struct gv_freelist *fl;
415130389Sle	struct gv_hdr *vhdr;
416130389Sle	int error;
417132642Sle	char *buf, errstr[ERRBUFSIZ];
418130389Sle
419130389Sle	vhdr = NULL;
420130389Sle	d = NULL;
421130389Sle
422130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name);
423130389Sle	g_topology_assert();
424130389Sle
425130389Sle	/* Find the VINUM class and its associated geom. */
426130389Sle	gp2 = find_vinum_geom();
427130389Sle	if (gp2 == NULL)
428130389Sle		return (NULL);
429130389Sle	sc = gp2->softc;
430130389Sle
431130389Sle	gp = g_new_geomf(mp, "%s.vinumdrive", pp->name);
432133983Sle	gp->start = gv_drive_start;
433133983Sle	gp->orphan = gv_drive_orphan;
434133983Sle	gp->access = gv_drive_access;
435133983Sle	gp->start = gv_drive_start;
436130389Sle
437130389Sle	cp = g_new_consumer(gp);
438130389Sle	g_attach(cp, pp);
439130389Sle	error = g_access(cp, 1, 0, 0);
440130389Sle	if (error) {
441130389Sle		g_detach(cp);
442130389Sle		g_destroy_consumer(cp);
443130389Sle		g_destroy_geom(gp);
444130389Sle		return (NULL);
445130389Sle	}
446130389Sle
447130389Sle	g_topology_unlock();
448130389Sle
449130389Sle	/* Now check if the provided slice is a valid vinum drive. */
450130389Sle	do {
451135161Sle		vhdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, &error);
452130389Sle		if (vhdr == NULL || error != 0)
453130389Sle			break;
454130389Sle		if (vhdr->magic != GV_MAGIC) {
455130389Sle			g_free(vhdr);
456130389Sle			break;
457130389Sle		}
458130389Sle
459130389Sle		/*
460130389Sle		 * We have found a valid vinum drive.  Let's see if it is
461132642Sle		 * already known in the configuration.  There's a chance that
462132642Sle		 * the VINUMDRIVE class tastes before the VINUM class could
463132642Sle		 * taste, so parse the configuration here too, just to be on
464132642Sle		 * the safe side.
465130389Sle		 */
466132642Sle		buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error);
467132642Sle		if (buf == NULL || error != 0) {
468132642Sle			g_free(vhdr);
469132642Sle			break;
470132642Sle		}
471133449Sle		g_topology_lock();
472132642Sle		gv_parse_config(sc, buf, 1);
473132642Sle		g_free(buf);
474132642Sle
475130389Sle		d = gv_find_drive(sc, vhdr->label.name);
476130389Sle
477130389Sle		/* We already know about this drive. */
478130389Sle		if (d != NULL) {
479133983Sle			/* Check if this drive already has a geom. */
480133983Sle			if (d->geom != NULL) {
481133983Sle				g_topology_unlock();
482133983Sle				break;
483133983Sle			}
484130389Sle			bcopy(vhdr, d->hdr, sizeof(*vhdr));
485130389Sle
486130389Sle		/* This is a new drive. */
487130389Sle		} else {
488130389Sle			d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
489130389Sle
490130389Sle			/* Initialize all needed variables. */
491130389Sle			d->size = pp->mediasize - GV_DATA_START;
492130389Sle			d->avail = d->size;
493130389Sle			d->hdr = vhdr;
494130389Sle			strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME);
495130389Sle			LIST_INIT(&d->subdisks);
496130389Sle			LIST_INIT(&d->freelist);
497130389Sle
498130389Sle			/* We also need a freelist entry. */
499130389Sle			fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
500130389Sle			fl->offset = GV_DATA_START;
501130389Sle			fl->size = d->avail;
502130389Sle			LIST_INSERT_HEAD(&d->freelist, fl, freelist);
503130389Sle			d->freelist_entries = 1;
504130389Sle
505135173Sle			TAILQ_INIT(&d->bqueue);
506140475Sle
507140475Sle			/* Save it into the main configuration. */
508140475Sle			LIST_INSERT_HEAD(&sc->drives, d, drive);
509140475Sle		}
510140475Sle
511140475Sle		/*
512140475Sle		 * Create a bio queue mutex and a worker thread, if necessary.
513140475Sle		 */
514140475Sle		if (mtx_initialized(&d->bqueue_mtx) == 0)
515135173Sle			mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF);
516140475Sle
517140475Sle		if (!(d->flags & GV_DRIVE_THREAD_ACTIVE)) {
518135173Sle			kthread_create(gv_drive_worker, d, NULL, 0, 0,
519135173Sle			    "gv_d %s", d->name);
520135173Sle			d->flags |= GV_DRIVE_THREAD_ACTIVE;
521130389Sle		}
522130389Sle
523133983Sle		g_access(cp, -1, 0, 0);
524132617Sle
525130389Sle		gp->softc = d;
526130389Sle		d->geom = gp;
527135173Sle		d->vinumconf = sc;
528130389Sle		strncpy(d->device, pp->name, GV_MAXDRIVENAME);
529130389Sle
530130389Sle		/*
531130389Sle		 * Find out which subdisks belong to this drive and crosslink
532130389Sle		 * them.
533130389Sle		 */
534130389Sle		LIST_FOREACH(s, &sc->subdisks, sd) {
535130389Sle			if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME))
536130389Sle				/* XXX: errors ignored */
537130389Sle				gv_sd_to_drive(sc, d, s, errstr,
538130389Sle				    sizeof(errstr));
539130389Sle		}
540130389Sle
541130389Sle		/* This drive is now up for sure. */
542130389Sle		gv_set_drive_state(d, GV_DRIVE_UP, 0);
543130389Sle
544130389Sle		/*
545130389Sle		 * If there are subdisks on this drive, we need to create
546130389Sle		 * providers for them.
547130389Sle		 */
548130389Sle		if (d->sdcount)
549130389Sle			gv_drive_modify(d);
550130389Sle
551130389Sle		return (gp);
552130389Sle
553130389Sle	} while (0);
554130389Sle
555130389Sle	g_topology_lock();
556130389Sle	g_access(cp, -1, 0, 0);
557130389Sle
558130389Sle	g_detach(cp);
559130389Sle	g_destroy_consumer(cp);
560130389Sle	g_destroy_geom(gp);
561130389Sle	return (NULL);
562130389Sle}
563130389Sle
564130389Sle/*
565130389Sle * Modify the providers for the given drive 'd'.  It is assumed that the
566130389Sle * subdisk list of 'd' is already correctly set up.
567130389Sle */
568130389Slevoid
569130389Slegv_drive_modify(struct gv_drive *d)
570130389Sle{
571130389Sle	struct g_geom *gp;
572130389Sle	struct g_consumer *cp;
573130389Sle	struct g_provider *pp, *pp2;
574130389Sle	struct gv_sd *s;
575130389Sle
576130389Sle	KASSERT(d != NULL, ("gv_drive_modify: null d"));
577130389Sle	gp = d->geom;
578130389Sle	KASSERT(gp != NULL, ("gv_drive_modify: null gp"));
579130389Sle	cp = LIST_FIRST(&gp->consumer);
580130389Sle	KASSERT(cp != NULL, ("gv_drive_modify: null cp"));
581130389Sle	pp = cp->provider;
582130389Sle	KASSERT(pp != NULL, ("gv_drive_modify: null pp"));
583130389Sle
584130389Sle	g_topology_assert();
585130389Sle
586130389Sle	LIST_FOREACH(s, &d->subdisks, from_drive) {
587130389Sle		/* This subdisk already has a provider. */
588130389Sle		if (s->provider != NULL)
589130389Sle			continue;
590130389Sle		pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name);
591130389Sle		pp2->mediasize = s->size;
592130389Sle		pp2->sectorsize = pp->sectorsize;
593130389Sle		g_error_provider(pp2, 0);
594130389Sle		s->provider = pp2;
595130389Sle		pp2->private = s;
596130389Sle	}
597130389Sle}
598130389Sle
599146325Slestatic void
600146325Slegv_drive_dead(void *arg, int flag)
601146325Sle{
602146325Sle	struct g_geom *gp;
603146325Sle	struct g_consumer *cp;
604146325Sle	struct gv_drive *d;
605146325Sle	struct gv_sd *s;
606146325Sle
607146325Sle	g_topology_assert();
608146325Sle	KASSERT(arg != NULL, ("gv_drive_dead: NULL arg"));
609146325Sle
610146325Sle	if (flag == EV_CANCEL)
611146325Sle		return;
612146325Sle
613146325Sle	d = arg;
614146325Sle	if (d->state != GV_DRIVE_DOWN)
615146325Sle		return;
616146325Sle
617146325Sle	g_trace(G_T_TOPOLOGY, "gv_drive_dead(%s)", d->name);
618146325Sle
619146325Sle	gp = d->geom;
620146325Sle	if (gp == NULL)
621146325Sle		return;
622146325Sle
623146325Sle	LIST_FOREACH(cp, &gp->consumer, consumer) {
624146325Sle		if (cp->nstart != cp->nend) {
625146325Sle			printf("GEOM_VINUM: dead drive '%s' has still "
626146325Sle			    "active requests, can't detach consumer\n",
627146325Sle			    d->name);
628146325Sle			g_post_event(gv_drive_dead, d, M_WAITOK, d,
629146325Sle			    NULL);
630146325Sle			return;
631146325Sle		}
632146325Sle		if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
633146325Sle			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
634146325Sle	}
635146325Sle
636146325Sle	printf("GEOM_VINUM: lost drive '%s'\n", d->name);
637146325Sle	d->geom = NULL;
638146325Sle	LIST_FOREACH(s, &d->subdisks, from_drive) {
639146325Sle		s->provider = NULL;
640146325Sle		s->consumer = NULL;
641146325Sle	}
642146325Sle	gv_kill_drive_thread(d);
643146325Sle	gp->softc = NULL;
644146325Sle	g_wither_geom(gp, ENXIO);
645146325Sle}
646146325Sle
647130389Slestatic int
648130389Slegv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp,
649130389Sle    struct g_geom *gp)
650130389Sle{
651135173Sle	struct gv_drive *d;
652135173Sle
653130389Sle	g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name);
654130389Sle	g_topology_assert();
655130389Sle
656135173Sle	d = gp->softc;
657135173Sle	gv_kill_drive_thread(d);
658135173Sle
659130389Sle	g_wither_geom(gp, ENXIO);
660130389Sle	return (0);
661130389Sle}
662130389Sle
663130389Sle#define	VINUMDRIVE_CLASS_NAME "VINUMDRIVE"
664130389Sle
665130389Slestatic struct g_class g_vinum_drive_class = {
666130389Sle	.name = VINUMDRIVE_CLASS_NAME,
667133318Sphk	.version = G_VERSION,
668130389Sle	.taste = gv_drive_taste,
669130389Sle	.destroy_geom = gv_drive_destroy_geom
670130389Sle};
671130389Sle
672130389SleDECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive);
673