geom_slice.c revision 95321
192108Sphk/*-
292108Sphk * Copyright (c) 2002 Poul-Henning Kamp
392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
492108Sphk * All rights reserved.
592108Sphk *
692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
992108Sphk * DARPA CHATS research program.
1092108Sphk *
1192108Sphk * Redistribution and use in source and binary forms, with or without
1292108Sphk * modification, are permitted provided that the following conditions
1392108Sphk * are met:
1492108Sphk * 1. Redistributions of source code must retain the above copyright
1592108Sphk *    notice, this list of conditions and the following disclaimer.
1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright
1792108Sphk *    notice, this list of conditions and the following disclaimer in the
1892108Sphk *    documentation and/or other materials provided with the distribution.
1992108Sphk * 3. The names of the authors may not be used to endorse or promote
2092108Sphk *    products derived from this software without specific prior written
2192108Sphk *    permission.
2292108Sphk *
2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2692108Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3392108Sphk * SUCH DAMAGE.
3492108Sphk *
3592108Sphk * $FreeBSD: head/sys/geom/geom_slice.c 95321 2002-04-23 19:03:03Z phk $
3692108Sphk */
3792108Sphk
3892108Sphk
3992108Sphk#include <sys/param.h>
4092108Sphk#ifndef _KERNEL
4192108Sphk#include <stdio.h>
4292108Sphk#include <unistd.h>
4392108Sphk#include <stdlib.h>
4492108Sphk#include <signal.h>
4592108Sphk#include <err.h>
4692108Sphk#else
4792108Sphk#include <sys/systm.h>
4892108Sphk#include <sys/kernel.h>
4992108Sphk#include <sys/malloc.h>
5092108Sphk#include <sys/bio.h>
5192108Sphk#include <sys/sysctl.h>
5292108Sphk#include <sys/proc.h>
5392108Sphk#include <sys/kthread.h>
5492108Sphk#include <sys/lock.h>
5592108Sphk#include <sys/mutex.h>
5692108Sphk#endif
5792108Sphk#include <sys/errno.h>
5892108Sphk#include <sys/sbuf.h>
5992108Sphk#include <geom/geom.h>
6092108Sphk#include <geom/geom_slice.h>
6192108Sphk#include <machine/stdarg.h>
6292108Sphk
6393776Sphkstatic g_orphan_t g_slice_orphan;
6493776Sphkstatic g_access_t g_slice_access;
6593776Sphkstatic g_start_t g_slice_start;
6693776Sphk
6795321Sphkstatic struct g_slicer *
6892108Sphkg_slice_init(unsigned nslice, unsigned scsize)
6992108Sphk{
7092108Sphk	struct g_slicer *gsp;
7192108Sphk
7292371Sphk	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
7392371Sphk	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
7492371Sphk	gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO);
7592108Sphk	gsp->nslice = nslice;
7692108Sphk	return (gsp);
7792108Sphk}
7892108Sphk
7993776Sphkstatic int
8092108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de)
8192108Sphk{
8292108Sphk	int error, i;
8392108Sphk	struct g_geom *gp;
8492108Sphk	struct g_consumer *cp;
8592108Sphk	struct g_provider *pp2;
8692108Sphk	struct g_slicer *gsp;
8792108Sphk	struct g_slice *gsl, *gsl2;
8892108Sphk
8992108Sphk	gp = pp->geom;
9092108Sphk	cp = LIST_FIRST(&gp->consumer);
9192108Sphk	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
9292108Sphk	gsp = gp->softc;
9392108Sphk	gsl = &gsp->slices[pp->index];
9492108Sphk	for (i = 0; i < gsp->nslice; i++) {
9592108Sphk		gsl2 = &gsp->slices[i];
9692108Sphk		if (gsl2->length == 0)
9792108Sphk			continue;
9892108Sphk		if (i == pp->index)
9992108Sphk			continue;
10092108Sphk		if (gsl->offset + gsl->length <= gsl2->offset)
10192108Sphk			continue;
10292108Sphk		if (gsl2->offset + gsl2->length <= gsl->offset)
10392108Sphk			continue;
10492108Sphk		/* overlap */
10592108Sphk		pp2 = gsl2->provider;
10692108Sphk		if ((pp->acw + dw) > 0 && pp2->ace > 0)
10792108Sphk			return (EPERM);
10892108Sphk		if ((pp->ace + de) > 0 && pp2->acw > 0)
10992108Sphk			return (EPERM);
11092108Sphk	}
11192108Sphk	/* On first open, grab an extra "exclusive" bit */
11292108Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
11392108Sphk		de++;
11492108Sphk	/* ... and let go of it on last close */
11592108Sphk	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
11692108Sphk		de--;
11792108Sphk	error = g_access_rel(cp, dr, dw, de);
11893778Sphk	pp->mediasize = gsp->slices[pp->index].length;
11992108Sphk	return (error);
12092108Sphk}
12192108Sphk
12293776Sphkstatic void
12392108Sphkg_slice_start(struct bio *bp)
12492108Sphk{
12592108Sphk	struct bio *bp2;
12692108Sphk	struct g_provider *pp;
12792108Sphk	struct g_geom *gp;
12892108Sphk	struct g_consumer *cp;
12992108Sphk	struct g_slicer *gsp;
13092108Sphk	struct g_slice *gsl;
13192108Sphk	int index;
13294287Sphk	off_t t;
13392108Sphk
13492108Sphk	pp = bp->bio_to;
13592108Sphk	gp = pp->geom;
13692108Sphk	gsp = gp->softc;
13792108Sphk	cp = LIST_FIRST(&gp->consumer);
13892108Sphk	index = pp->index;
13994287Sphk	gsl = &gsp->slices[index];
14092108Sphk	switch(bp->bio_cmd) {
14192108Sphk	case BIO_READ:
14292108Sphk	case BIO_WRITE:
14392108Sphk	case BIO_DELETE:
14492108Sphk		if (bp->bio_offset > gsl->length) {
14592108Sphk			bp->bio_error = EINVAL; /* XXX: EWHAT ? */
14692108Sphk			g_io_deliver(bp);
14792108Sphk			return;
14892108Sphk		}
14992108Sphk		bp2 = g_clone_bio(bp);
15092108Sphk		if (bp2->bio_offset + bp2->bio_length > gsl->length)
15192108Sphk			bp2->bio_length = gsl->length - bp2->bio_offset;
15292108Sphk		bp2->bio_done = g_std_done;
15392108Sphk		bp2->bio_offset += gsl->offset;
15492108Sphk		g_io_request(bp2, cp);
15592108Sphk		return;
15692108Sphk	case BIO_GETATTR:
15792108Sphk	case BIO_SETATTR:
15894287Sphk		/* Give the real method a chance to override */
15994287Sphk		if (gsp->start(bp))
16094287Sphk			return;
16192108Sphk		if (g_haveattr_off_t(bp, "GEOM::mediasize",
16292108Sphk		    gsp->slices[index].length))
16392108Sphk			return;
16494287Sphk		if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
16594287Sphk			t = gsp->cfrontstuff;
16694287Sphk			if (gsp->frontstuff > t)
16794287Sphk				t = gsp->frontstuff;
16894287Sphk			t -= gsl->offset;
16994287Sphk			if (t < 0)
17094287Sphk				t = 0;
17194287Sphk			if (t > gsl->length)
17294287Sphk				t = gsl->length;
17394287Sphk			g_haveattr_off_t(bp, "GEOM::frontstuff", t);
17492108Sphk			return;
17594287Sphk		}
17695310Sphk#ifdef _KERNEL
17795038Sphk		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
17895038Sphk			struct g_kerneldump *gkd;
17995038Sphk
18095038Sphk			gkd = (struct g_kerneldump *)bp->bio_data;
18195038Sphk			gkd->offset += gsp->slices[index].offset;
18295038Sphk			if (gkd->length > gsp->slices[index].length)
18395038Sphk				gkd->length = gsp->slices[index].length;
18495038Sphk			/* now, pass it on downwards... */
18595038Sphk		}
18695310Sphk#endif
18792108Sphk		bp2 = g_clone_bio(bp);
18892108Sphk		bp2->bio_done = g_std_done;
18992108Sphk		g_io_request(bp2, cp);
19092108Sphk		break;
19192108Sphk	default:
19292108Sphk		bp->bio_error = EOPNOTSUPP;
19392108Sphk		g_io_deliver(bp);
19492108Sphk		return;
19592108Sphk	}
19692108Sphk}
19792108Sphk
19892108Sphkvoid
19992108Sphkg_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
20092108Sphk{
20192108Sphk	struct g_mbr_softc *mp;
20292108Sphk	struct g_slicer *gsp;
20392108Sphk
20492108Sphk	gsp = gp->softc;
20592108Sphk	mp = gsp->softc;
20694287Sphk	if (gp != NULL) {
20794287Sphk		sbuf_printf(sb, "%s<frontstuff>%llu</frontstuff>\n",
20894287Sphk		    indent, (unsigned long long)gsp->frontstuff);
20994287Sphk	}
21092108Sphk	if (pp != NULL) {
21192108Sphk		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
21292108Sphk		sbuf_printf(sb, "%s<length>%llu</length>\n",
21393326Sphk		    indent, (unsigned long long)gsp->slices[pp->index].length);
21493326Sphk		sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent,
21593326Sphk		    (unsigned long long)gsp->slices[pp->index].length / 512);
21693326Sphk		sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent,
21793326Sphk		    (unsigned long long)gsp->slices[pp->index].offset);
21893326Sphk		sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent,
21993326Sphk		    (unsigned long long)gsp->slices[pp->index].offset / 512);
22092108Sphk	}
22192108Sphk}
22292108Sphk
22392108Sphkstruct g_provider *
22492108Sphkg_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...)
22592108Sphk{
22692108Sphk	struct g_provider *pp;
22792108Sphk	struct g_slicer *gsp;
22892108Sphk	va_list ap;
22992108Sphk	struct sbuf *sb;
23092108Sphk
23192108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
23292108Sphk	g_topology_lock();
23392108Sphk	gsp = gp->softc;
23492108Sphk	va_start(ap, fmt);
23592108Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
23692108Sphk	sbuf_vprintf(sb, fmt, ap);
23792108Sphk	sbuf_finish(sb);
23892108Sphk	pp = g_new_providerf(gp, sbuf_data(sb));
23992108Sphk
24092108Sphk	pp->index = index;
24192108Sphk	gsp->slices[index].length = length;
24292108Sphk	gsp->slices[index].offset = offset;
24392108Sphk	gsp->slices[index].provider = pp;
24492108Sphk	sbuf_delete(sb);
24592108Sphk	g_topology_unlock();
24692108Sphk	return(pp);
24792108Sphk}
24892108Sphk
24992108Sphkstruct g_geom *
25093248Sphkg_slice_new(struct g_class *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
25192108Sphk{
25292108Sphk	struct g_geom *gp;
25392108Sphk	struct g_slicer *gsp;
25492108Sphk	struct g_consumer *cp;
25592108Sphk	void **vp;
25694287Sphk	int error, i;
25792108Sphk
25892108Sphk	g_topology_assert();
25992108Sphk	vp = (void **)extrap;
26092108Sphk	gp = g_new_geomf(mp, "%s", pp->name);
26192108Sphk	gsp = g_slice_init(slices, extra);
26292108Sphk	gsp->start = start;
26393776Sphk	gp->access = g_slice_access;
26493776Sphk	gp->orphan = g_slice_orphan;
26592108Sphk	gp->softc = gsp;
26692108Sphk	gp->start = g_slice_start;
26792108Sphk	gp->spoiled = g_std_spoiled;
26892108Sphk	gp->dumpconf = g_slice_dumpconf;
26992108Sphk	cp = g_new_consumer(gp);
27092108Sphk	g_attach(cp, pp);
27192108Sphk	error = g_access_rel(cp, 1, 0, 0);
27292108Sphk	if (error) {
27392108Sphk		g_dettach(cp);
27492108Sphk		g_destroy_consumer(cp);
27592371Sphk		g_free(gsp->slices);
27692108Sphk		g_free(gp->softc);
27792108Sphk		g_destroy_geom(gp);
27892108Sphk		return (NULL);
27992108Sphk	}
28094287Sphk	/* Find out if there are any magic bytes on the consumer */
28194287Sphk	i = sizeof gsp->cfrontstuff;
28294287Sphk	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
28394287Sphk	if (error)
28494287Sphk		gsp->cfrontstuff = 0;
28592108Sphk	*vp = gsp->softc;
28692108Sphk	*cpp = cp;
28792108Sphk	return (gp);
28892108Sphk}
28992108Sphk
29093776Sphkstatic void
29193250Sphkg_slice_orphan(struct g_consumer *cp)
29292108Sphk{
29392108Sphk	struct g_geom *gp;
29492108Sphk	struct g_provider *pp;
29592108Sphk	int error;
29692108Sphk
29792108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
29892108Sphk	g_topology_assert();
29992108Sphk	KASSERT(cp->provider->error != 0,
30092108Sphk	    ("g_slice_orphan with error == 0"));
30192108Sphk
30292108Sphk	gp = cp->geom;
30392108Sphk	gp->flags |= G_GEOM_WITHER;
30492108Sphk	/* First prevent any new requests */
30592108Sphk	error = cp->provider->error;
30692108Sphk	LIST_FOREACH(pp, &gp->provider, provider)
30792108Sphk		g_orphan_provider(pp, error);
30892108Sphk
30992108Sphk	return;
31092108Sphk}
311