geom_vinum_drive.c revision 130697
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_drive.c 130697 2004-06-18 19:53:33Z le $");
29
30#include <sys/param.h>
31#include <sys/bio.h>
32#include <sys/errno.h>
33#include <sys/conf.h>
34#include <sys/kernel.h>
35#include <sys/kthread.h>
36#include <sys/libkern.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/mutex.h>
41#include <sys/sbuf.h>
42#include <sys/systm.h>
43#include <sys/time.h>
44
45#include <geom/geom.h>
46#include <geom/vinum/geom_vinum_var.h>
47#include <geom/vinum/geom_vinum.h>
48#include <geom/vinum/geom_vinum_share.h>
49
50void	gv_drive_modify(struct gv_drive *);
51
52void
53gv_save_config_all(struct gv_softc *sc)
54{
55	struct gv_drive *d;
56
57	g_topology_assert();
58
59	LIST_FOREACH(d, &sc->drives, drive) {
60		if (d->geom == NULL)
61			continue;
62		gv_save_config(NULL, d, sc);
63	}
64}
65
66/* Save the vinum configuration back to disk. */
67void
68gv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc)
69{
70	struct g_geom *gp;
71	struct g_consumer *cp2;
72	struct gv_hdr *vhdr, *hdr;
73	struct sbuf *sb;
74	int error;
75
76	g_topology_assert();
77
78	KASSERT(d != NULL, ("gv_save_config: null d"));
79	KASSERT(sc != NULL, ("gv_save_config: null sc"));
80
81	if (cp == NULL) {
82		gp = d->geom;
83		KASSERT(gp != NULL, ("gv_save_config: null gp"));
84		cp2 = LIST_FIRST(&gp->consumer);
85		KASSERT(cp2 != NULL, ("gv_save_config: null cp2"));
86	} else
87		cp2 = cp;
88
89	vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO);
90	vhdr->magic = GV_MAGIC;
91	vhdr->config_length = GV_CFG_LEN;
92
93	hdr = d->hdr;
94	if (hdr == NULL) {
95		printf("NULL hdr!!!\n");
96		g_free(vhdr);
97		return;
98	}
99	microtime(&hdr->label.last_update);
100	bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label));
101
102	sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
103	gv_format_config(sc, sb, 1, NULL);
104	sbuf_finish(sb);
105
106	error = g_access(cp2, 0, 1, 0);
107	if (error) {
108		printf("g_access failed: %d\n", error);
109		sbuf_delete(sb);
110		return;
111	}
112	g_topology_unlock();
113
114	do {
115		error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN);
116		if (error) {
117			printf("writing vhdr failed: %d", error);
118			break;
119		}
120
121		error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb),
122		    GV_CFG_LEN);
123		if (error) {
124			printf("writing first config copy failed: %d", error);
125			break;
126		}
127
128		error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN,
129		    sbuf_data(sb), GV_CFG_LEN);
130		if (error)
131			printf("writing second config copy failed: %d", error);
132	} while (0);
133
134	g_topology_lock();
135	g_access(cp2, 0, -1, 0);
136	sbuf_delete(sb);
137	g_free(vhdr);
138
139	if (d->geom != NULL)
140		gv_drive_modify(d);
141}
142
143/* This resembles g_slice_access(). */
144static int
145gv_drive_access(struct g_provider *pp, int dr, int dw, int de)
146{
147	struct g_geom *gp;
148	struct g_consumer *cp;
149	struct g_provider *pp2;
150	struct gv_drive *d;
151	struct gv_sd *s, *s2;
152	int error;
153
154	gp = pp->geom;
155	cp = LIST_FIRST(&gp->consumer);
156	KASSERT(cp != NULL, ("gv_drive_access: NULL cp"));
157
158	d = gp->softc;
159
160	s = pp->private;
161	KASSERT(s != NULL, ("gv_drive_access: NULL s"));
162
163	LIST_FOREACH(s2, &d->subdisks, from_drive) {
164		if (s == s2)
165			continue;
166		if (s->drive_offset + s->size <= s2->drive_offset)
167			continue;
168		if (s2->drive_offset + s2->size <= s->drive_offset)
169			continue;
170
171		/* Overlap. */
172		pp2 = s2->provider;
173		KASSERT(s2 != NULL, ("gv_drive_access: NULL s2"));
174		if ((pp->acw + dw) > 0 && pp2->ace > 0) {
175			printf("FOOO: permission denied - e\n");
176			return (EPERM);
177		}
178		if ((pp->ace + de) > 0 && pp2->acw > 0) {
179			printf("FOOO: permission denied - w\n");
180			return (EPERM);
181		}
182	}
183
184	/* On first open, grab an extra "exclusive" bit */
185	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
186		de++;
187	/* ... and let go of it on last close */
188	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
189		de--;
190	error = g_access(cp, dr, dw, de);
191	if (error) {
192		printf("FOOO: g_access failed: %d\n", error);
193	}
194	return (error);
195}
196
197static void
198gv_drive_start(struct bio *bp)
199{
200	struct bio *bp2;
201	struct g_geom *gp;
202	struct g_consumer *cp;
203	struct g_provider *pp;
204	struct gv_drive *d;
205	struct gv_sd *s;
206
207	pp = bp->bio_to;
208	gp = pp->geom;
209	cp = LIST_FIRST(&gp->consumer);
210	d = gp->softc;
211	s = pp->private;
212
213	if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) {
214		g_io_deliver(bp, ENXIO);
215		return;
216	}
217
218	switch(bp->bio_cmd) {
219	case BIO_READ:
220	case BIO_WRITE:
221	case BIO_DELETE:
222		if (bp->bio_offset > s->size) {
223			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
224			return;
225		}
226		bp2 = g_clone_bio(bp);
227		if (bp2 == NULL) {
228			g_io_deliver(bp, ENOMEM);
229			return;
230		}
231		if (bp2->bio_offset + bp2->bio_length > s->size)
232			bp2->bio_length = s->size - bp2->bio_offset;
233		bp2->bio_done = g_std_done;
234		bp2->bio_offset += s->drive_offset;
235		g_io_request(bp2, cp);
236		return;
237
238	case BIO_GETATTR:
239		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
240			struct g_kerneldump *gkd;
241
242			gkd = (struct g_kerneldump *)bp->bio_data;
243			gkd->offset += s->drive_offset;
244			if (gkd->length > s->size)
245				gkd->length = s->size;
246			/* now, pass it on downwards... */
247		}
248		bp2 = g_clone_bio(bp);
249		if (bp2 == NULL) {
250			g_io_deliver(bp, ENOMEM);
251			return;
252		}
253		bp2->bio_done = g_std_done;
254		g_io_request(bp2, cp);
255		return;
256
257	default:
258		g_io_deliver(bp, EOPNOTSUPP);
259		return;
260	}
261}
262
263static void
264gv_drive_orphan(struct g_consumer *cp)
265{
266	struct g_geom *gp;
267	struct gv_drive *d;
268	struct gv_sd *s;
269	int error;
270
271	g_topology_assert();
272	gp = cp->geom;
273	g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name);
274	if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
275		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
276	error = cp->provider->error;
277	if (error == 0)
278		error = ENXIO;
279	g_detach(cp);
280	g_destroy_consumer(cp);
281	if (!LIST_EMPTY(&gp->consumer))
282		return;
283	d = gp->softc;
284	if (d != NULL) {
285		printf("gvinum: lost drive '%s'\n", d->name);
286		d->geom = NULL;
287		LIST_FOREACH(s, &d->subdisks, from_drive) {
288			s->provider = NULL;
289			s->consumer = NULL;
290		}
291		gv_set_drive_state(d, GV_DRIVE_DOWN, GV_SETSTATE_FORCE);
292	}
293	gp->softc = NULL;
294	g_wither_geom(gp, error);
295}
296
297static struct g_geom *
298gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
299{
300	struct g_geom *gp, *gp2;
301	struct g_consumer *cp;
302	struct gv_drive *d;
303	struct gv_sd *s;
304	struct gv_softc *sc;
305	struct gv_freelist *fl;
306	struct gv_hdr *vhdr;
307	int error;
308	char errstr[ERRBUFSIZ];
309
310	vhdr = NULL;
311	d = NULL;
312
313	g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name);
314	g_topology_assert();
315
316	if (pp->sectorsize == 0)
317		return(NULL);
318
319	/* Find the VINUM class and its associated geom. */
320	gp2 = find_vinum_geom();
321	if (gp2 == NULL)
322		return (NULL);
323	sc = gp2->softc;
324
325	gp = g_new_geomf(mp, "%s.vinumdrive", pp->name);
326	gp->start = gv_drive_start;
327	gp->spoiled = gv_drive_orphan;
328	gp->orphan = gv_drive_orphan;
329	gp->access = gv_drive_access;
330	gp->start = gv_drive_start;
331
332	cp = g_new_consumer(gp);
333	g_attach(cp, pp);
334	error = g_access(cp, 1, 0, 0);
335	if (error) {
336		g_detach(cp);
337		g_destroy_consumer(cp);
338		g_destroy_geom(gp);
339		return (NULL);
340	}
341
342	g_topology_unlock();
343
344	/* Now check if the provided slice is a valid vinum drive. */
345	do {
346		vhdr = g_read_data(cp, GV_HDR_OFFSET, GV_HDR_LEN, &error);
347		if (vhdr == NULL || error != 0)
348			break;
349		if (vhdr->magic != GV_MAGIC) {
350			g_free(vhdr);
351			break;
352		}
353
354		/*
355		 * We have found a valid vinum drive.  Let's see if it is
356		 * already known in the configuration.
357		 */
358		g_topology_lock();
359		g_access(cp, -1, 0, 0);
360
361		d = gv_find_drive(sc, vhdr->label.name);
362
363		/* We already know about this drive. */
364		if (d != NULL) {
365			bcopy(vhdr, d->hdr, sizeof(*vhdr));
366
367		/* This is a new drive. */
368		} else {
369			d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
370
371			/* Initialize all needed variables. */
372			d->size = pp->mediasize - GV_DATA_START;
373			d->avail = d->size;
374			d->hdr = vhdr;
375			strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME);
376			LIST_INIT(&d->subdisks);
377			LIST_INIT(&d->freelist);
378
379			/* We also need a freelist entry. */
380			fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
381			fl->offset = GV_DATA_START;
382			fl->size = d->avail;
383			LIST_INSERT_HEAD(&d->freelist, fl, freelist);
384			d->freelist_entries = 1;
385
386			/* Save it into the main configuration. */
387			LIST_INSERT_HEAD(&sc->drives, d, drive);
388		}
389
390		gp->softc = d;
391		d->geom = gp;
392		strncpy(d->device, pp->name, GV_MAXDRIVENAME);
393
394		/*
395		 * Find out which subdisks belong to this drive and crosslink
396		 * them.
397		 */
398		LIST_FOREACH(s, &sc->subdisks, sd) {
399			if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME))
400				/* XXX: errors ignored */
401				gv_sd_to_drive(sc, d, s, errstr,
402				    sizeof(errstr));
403		}
404
405		/* This drive is now up for sure. */
406		gv_set_drive_state(d, GV_DRIVE_UP, 0);
407
408		/*
409		 * If there are subdisks on this drive, we need to create
410		 * providers for them.
411		 */
412		if (d->sdcount)
413			gv_drive_modify(d);
414
415		return (gp);
416
417	} while (0);
418
419	g_topology_lock();
420	g_access(cp, -1, 0, 0);
421
422	g_detach(cp);
423	g_destroy_consumer(cp);
424	g_free(gp->softc);
425	g_destroy_geom(gp);
426	return (NULL);
427}
428
429/*
430 * Modify the providers for the given drive 'd'.  It is assumed that the
431 * subdisk list of 'd' is already correctly set up.
432 */
433void
434gv_drive_modify(struct gv_drive *d)
435{
436	struct g_geom *gp;
437	struct g_consumer *cp;
438	struct g_provider *pp, *pp2;
439	struct gv_sd *s;
440	int nsd;
441
442	KASSERT(d != NULL, ("gv_drive_modify: null d"));
443	gp = d->geom;
444	KASSERT(gp != NULL, ("gv_drive_modify: null gp"));
445	cp = LIST_FIRST(&gp->consumer);
446	KASSERT(cp != NULL, ("gv_drive_modify: null cp"));
447	pp = cp->provider;
448	KASSERT(pp != NULL, ("gv_drive_modify: null pp"));
449
450	g_topology_assert();
451
452	nsd = 0;
453	LIST_FOREACH(s, &d->subdisks, from_drive) {
454		/* This subdisk already has a provider. */
455		if (s->provider != NULL)
456			continue;
457		pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name);
458		pp2->mediasize = s->size;
459		pp2->sectorsize = pp->sectorsize;
460		g_error_provider(pp2, 0);
461		s->provider = pp2;
462		pp2->private = s;
463	}
464}
465
466static int
467gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp,
468    struct g_geom *gp)
469{
470	g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name);
471	g_topology_assert();
472
473	g_wither_geom(gp, ENXIO);
474	return (0);
475}
476
477#define	VINUMDRIVE_CLASS_NAME "VINUMDRIVE"
478
479static struct g_class g_vinum_drive_class = {
480	.name = VINUMDRIVE_CLASS_NAME,
481	.taste = gv_drive_taste,
482	.destroy_geom = gv_drive_destroy_geom
483};
484
485DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive);
486