geom_slice.c revision 104195
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 104195 2002-09-30 08:54:46Z 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>
4596952Sphk#include <string.h>
4692108Sphk#include <err.h>
4792108Sphk#else
4892108Sphk#include <sys/systm.h>
4992108Sphk#include <sys/kernel.h>
5092108Sphk#include <sys/malloc.h>
5192108Sphk#include <sys/bio.h>
5292108Sphk#include <sys/sysctl.h>
5392108Sphk#include <sys/proc.h>
5492108Sphk#include <sys/kthread.h>
5592108Sphk#include <sys/lock.h>
5692108Sphk#include <sys/mutex.h>
5792108Sphk#endif
5892108Sphk#include <sys/errno.h>
5992108Sphk#include <sys/sbuf.h>
6092108Sphk#include <geom/geom.h>
6192108Sphk#include <geom/geom_slice.h>
6292108Sphk#include <machine/stdarg.h>
6392108Sphk
6493776Sphkstatic g_orphan_t g_slice_orphan;
6593776Sphkstatic g_access_t g_slice_access;
6693776Sphkstatic g_start_t g_slice_start;
6793776Sphk
6895321Sphkstatic struct g_slicer *
6992108Sphkg_slice_init(unsigned nslice, unsigned scsize)
7092108Sphk{
7192108Sphk	struct g_slicer *gsp;
7292108Sphk
7392371Sphk	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
7492371Sphk	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
75104057Sphk	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
76104057Sphk	    M_WAITOK | M_ZERO);
7792108Sphk	gsp->nslice = nslice;
7892108Sphk	return (gsp);
7992108Sphk}
8092108Sphk
8193776Sphkstatic int
8292108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de)
8392108Sphk{
8492108Sphk	int error, i;
8592108Sphk	struct g_geom *gp;
8692108Sphk	struct g_consumer *cp;
8792108Sphk	struct g_provider *pp2;
8892108Sphk	struct g_slicer *gsp;
8992108Sphk	struct g_slice *gsl, *gsl2;
9092108Sphk
9192108Sphk	gp = pp->geom;
9292108Sphk	cp = LIST_FIRST(&gp->consumer);
9392108Sphk	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
9492108Sphk	gsp = gp->softc;
9592108Sphk	gsl = &gsp->slices[pp->index];
9692108Sphk	for (i = 0; i < gsp->nslice; i++) {
9792108Sphk		gsl2 = &gsp->slices[i];
9892108Sphk		if (gsl2->length == 0)
9992108Sphk			continue;
10092108Sphk		if (i == pp->index)
10192108Sphk			continue;
10292108Sphk		if (gsl->offset + gsl->length <= gsl2->offset)
10392108Sphk			continue;
10492108Sphk		if (gsl2->offset + gsl2->length <= gsl->offset)
10592108Sphk			continue;
10692108Sphk		/* overlap */
10792108Sphk		pp2 = gsl2->provider;
10892108Sphk		if ((pp->acw + dw) > 0 && pp2->ace > 0)
10992108Sphk			return (EPERM);
11092108Sphk		if ((pp->ace + de) > 0 && pp2->acw > 0)
11192108Sphk			return (EPERM);
11292108Sphk	}
11392108Sphk	/* On first open, grab an extra "exclusive" bit */
11492108Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
11592108Sphk		de++;
11692108Sphk	/* ... and let go of it on last close */
11792108Sphk	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
11892108Sphk		de--;
11992108Sphk	error = g_access_rel(cp, dr, dw, de);
12093778Sphk	pp->mediasize = gsp->slices[pp->index].length;
12192108Sphk	return (error);
12292108Sphk}
12392108Sphk
12493776Sphkstatic void
12592108Sphkg_slice_start(struct bio *bp)
12692108Sphk{
12792108Sphk	struct bio *bp2;
12892108Sphk	struct g_provider *pp;
12992108Sphk	struct g_geom *gp;
13092108Sphk	struct g_consumer *cp;
13192108Sphk	struct g_slicer *gsp;
13292108Sphk	struct g_slice *gsl;
13392108Sphk	int index;
13494287Sphk	off_t t;
13592108Sphk
13692108Sphk	pp = bp->bio_to;
13792108Sphk	gp = pp->geom;
13892108Sphk	gsp = gp->softc;
13992108Sphk	cp = LIST_FIRST(&gp->consumer);
14092108Sphk	index = pp->index;
14194287Sphk	gsl = &gsp->slices[index];
14292108Sphk	switch(bp->bio_cmd) {
14392108Sphk	case BIO_READ:
14492108Sphk	case BIO_WRITE:
14592108Sphk	case BIO_DELETE:
14692108Sphk		if (bp->bio_offset > gsl->length) {
147104195Sphk			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
14892108Sphk			return;
14992108Sphk		}
15092108Sphk		bp2 = g_clone_bio(bp);
151104057Sphk		if (bp2 == NULL) {
152104195Sphk			g_io_deliver(bp, ENOMEM);
153104057Sphk			return;
154104057Sphk		}
15592108Sphk		if (bp2->bio_offset + bp2->bio_length > gsl->length)
15692108Sphk			bp2->bio_length = gsl->length - bp2->bio_offset;
15792108Sphk		bp2->bio_done = g_std_done;
15892108Sphk		bp2->bio_offset += gsl->offset;
15992108Sphk		g_io_request(bp2, cp);
16092108Sphk		return;
16192108Sphk	case BIO_GETATTR:
16292108Sphk	case BIO_SETATTR:
16394287Sphk		/* Give the real method a chance to override */
16494287Sphk		if (gsp->start(bp))
16594287Sphk			return;
16698066Sphk		if (g_handleattr_off_t(bp, "GEOM::mediasize",
16792108Sphk		    gsp->slices[index].length))
16892108Sphk			return;
16994287Sphk		if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
17094287Sphk			t = gsp->cfrontstuff;
17194287Sphk			if (gsp->frontstuff > t)
17294287Sphk				t = gsp->frontstuff;
17394287Sphk			t -= gsl->offset;
17494287Sphk			if (t < 0)
17594287Sphk				t = 0;
17694287Sphk			if (t > gsl->length)
17794287Sphk				t = gsl->length;
17898066Sphk			g_handleattr_off_t(bp, "GEOM::frontstuff", t);
17992108Sphk			return;
18094287Sphk		}
18195310Sphk#ifdef _KERNEL
18295038Sphk		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
18395038Sphk			struct g_kerneldump *gkd;
18495038Sphk
18595038Sphk			gkd = (struct g_kerneldump *)bp->bio_data;
18695038Sphk			gkd->offset += gsp->slices[index].offset;
18795038Sphk			if (gkd->length > gsp->slices[index].length)
18895038Sphk				gkd->length = gsp->slices[index].length;
18995038Sphk			/* now, pass it on downwards... */
19095038Sphk		}
19195310Sphk#endif
19292108Sphk		bp2 = g_clone_bio(bp);
193104081Sphk		if (bp2 == NULL) {
194104195Sphk			g_io_deliver(bp, ENOMEM);
195104081Sphk			return;
196104081Sphk		}
19792108Sphk		bp2->bio_done = g_std_done;
19892108Sphk		g_io_request(bp2, cp);
19992108Sphk		break;
20092108Sphk	default:
201104195Sphk		g_io_deliver(bp, EOPNOTSUPP);
20292108Sphk		return;
20392108Sphk	}
20492108Sphk}
20592108Sphk
20692108Sphkvoid
207104087Sphkg_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
20892108Sphk{
20992108Sphk	struct g_mbr_softc *mp;
21092108Sphk	struct g_slicer *gsp;
21192108Sphk
21292108Sphk	gsp = gp->softc;
21392108Sphk	mp = gsp->softc;
21495323Sphk	if (gp != NULL && (pp == NULL && cp == NULL)) {
21594287Sphk		sbuf_printf(sb, "%s<frontstuff>%llu</frontstuff>\n",
21694287Sphk		    indent, (unsigned long long)gsp->frontstuff);
21794287Sphk	}
21892108Sphk	if (pp != NULL) {
21992108Sphk		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
22092108Sphk		sbuf_printf(sb, "%s<length>%llu</length>\n",
22193326Sphk		    indent, (unsigned long long)gsp->slices[pp->index].length);
22293326Sphk		sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent,
22393326Sphk		    (unsigned long long)gsp->slices[pp->index].length / 512);
22493326Sphk		sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent,
22593326Sphk		    (unsigned long long)gsp->slices[pp->index].offset);
22693326Sphk		sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent,
22793326Sphk		    (unsigned long long)gsp->slices[pp->index].offset / 512);
22892108Sphk	}
22992108Sphk}
23092108Sphk
231104064Sphkint
232104064Sphkg_slice_config(struct g_geom *gp, int index, int how, off_t offset, off_t length, char *fmt, ...)
233104064Sphk{
234104064Sphk	struct g_provider *pp;
235104064Sphk	struct g_slicer *gsp;
236104064Sphk	struct g_slice *gsl;
237104064Sphk	va_list ap;
238104064Sphk	struct sbuf *sb;
239104064Sphk	int error, acc;
240104064Sphk
241104195Sphk	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
242104195Sphk	     gp->name, index, how);
243104064Sphk	g_topology_assert();
244104064Sphk	gsp = gp->softc;
245104064Sphk	error = 0;
246104064Sphk	if (index >= gsp->nslice)
247104064Sphk		return(EINVAL);
248104064Sphk	gsl = &gsp->slices[index];
249104064Sphk	pp = gsl->provider;
250104064Sphk	if (pp != NULL)
251104064Sphk		acc = pp->acr + pp->acw + pp->ace;
252104064Sphk	else
253104064Sphk		acc = 0;
254104064Sphk	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
255104064Sphk		if (length < gsl->length)
256104064Sphk			return(EBUSY);
257104064Sphk		if (offset != gsl->offset)
258104064Sphk			return(EBUSY);
259104064Sphk	}
260104064Sphk	/* XXX: check offset + length <= MEDIASIZE */
261104064Sphk	if (how == G_SLICE_CONFIG_CHECK)
262104064Sphk		return (0);
263104064Sphk	gsl->length = length;
264104064Sphk	gsl->offset = offset;
265104064Sphk	if (length != 0 && pp != NULL)
266104064Sphk		return (0);
267104064Sphk	if (length == 0 && pp == NULL)
268104064Sphk		return (0);
269104064Sphk	if (length == 0 && pp != NULL) {
270104064Sphk		g_orphan_provider(pp, ENXIO);
271104064Sphk		gsl->provider = NULL;
272104064Sphk		gsp->nprovider--;
273104064Sphk		return (0);
274104064Sphk	}
275104064Sphk	va_start(ap, fmt);
276104064Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
277104064Sphk	sbuf_vprintf(sb, fmt, ap);
278104064Sphk	sbuf_finish(sb);
279104064Sphk	pp = g_new_providerf(gp, sbuf_data(sb));
280104064Sphk	pp->index = index;
281104064Sphk	gsl->provider = pp;
282104064Sphk	gsp->nprovider++;
283104064Sphk	g_error_provider(pp, 0);
284104064Sphk	sbuf_delete(sb);
285104064Sphk	return(0);
286104064Sphk}
287104064Sphk
28892108Sphkstruct g_provider *
28992108Sphkg_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...)
29092108Sphk{
29192108Sphk	struct g_provider *pp;
29292108Sphk	struct g_slicer *gsp;
29392108Sphk	va_list ap;
29492108Sphk	struct sbuf *sb;
29592108Sphk
29692108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
297104064Sphk	g_topology_assert();
29892108Sphk	gsp = gp->softc;
29992108Sphk	va_start(ap, fmt);
30092108Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
30192108Sphk	sbuf_vprintf(sb, fmt, ap);
30292108Sphk	sbuf_finish(sb);
30392108Sphk	pp = g_new_providerf(gp, sbuf_data(sb));
30492108Sphk
30592108Sphk	pp->index = index;
30692108Sphk	gsp->slices[index].length = length;
30792108Sphk	gsp->slices[index].offset = offset;
30892108Sphk	gsp->slices[index].provider = pp;
30992108Sphk	sbuf_delete(sb);
31092108Sphk	return(pp);
31192108Sphk}
31292108Sphk
31392108Sphkstruct g_geom *
31493248Sphkg_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)
31592108Sphk{
31692108Sphk	struct g_geom *gp;
31792108Sphk	struct g_slicer *gsp;
31892108Sphk	struct g_consumer *cp;
31992108Sphk	void **vp;
32094287Sphk	int error, i;
32192108Sphk
32292108Sphk	g_topology_assert();
32392108Sphk	vp = (void **)extrap;
32492108Sphk	gp = g_new_geomf(mp, "%s", pp->name);
32592108Sphk	gsp = g_slice_init(slices, extra);
32692108Sphk	gsp->start = start;
32793776Sphk	gp->access = g_slice_access;
32893776Sphk	gp->orphan = g_slice_orphan;
32992108Sphk	gp->softc = gsp;
33092108Sphk	gp->start = g_slice_start;
33192108Sphk	gp->spoiled = g_std_spoiled;
33292108Sphk	gp->dumpconf = g_slice_dumpconf;
33392108Sphk	cp = g_new_consumer(gp);
33492108Sphk	g_attach(cp, pp);
33592108Sphk	error = g_access_rel(cp, 1, 0, 0);
33692108Sphk	if (error) {
33798066Sphk		g_detach(cp);
33892108Sphk		g_destroy_consumer(cp);
33992371Sphk		g_free(gsp->slices);
34092108Sphk		g_free(gp->softc);
34192108Sphk		g_destroy_geom(gp);
34292108Sphk		return (NULL);
34392108Sphk	}
34494287Sphk	/* Find out if there are any magic bytes on the consumer */
34594287Sphk	i = sizeof gsp->cfrontstuff;
34694287Sphk	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
34794287Sphk	if (error)
34894287Sphk		gsp->cfrontstuff = 0;
34992108Sphk	*vp = gsp->softc;
35092108Sphk	*cpp = cp;
35192108Sphk	return (gp);
35292108Sphk}
35392108Sphk
35493776Sphkstatic void
35593250Sphkg_slice_orphan(struct g_consumer *cp)
35692108Sphk{
35792108Sphk	struct g_geom *gp;
35892108Sphk	struct g_provider *pp;
35992108Sphk	int error;
36092108Sphk
36192108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
36292108Sphk	g_topology_assert();
36392108Sphk	KASSERT(cp->provider->error != 0,
36492108Sphk	    ("g_slice_orphan with error == 0"));
36592108Sphk
36692108Sphk	gp = cp->geom;
36792108Sphk	gp->flags |= G_GEOM_WITHER;
36892108Sphk	error = cp->provider->error;
36992108Sphk	LIST_FOREACH(pp, &gp->provider, provider)
37092108Sphk		g_orphan_provider(pp, error);
37192108Sphk	return;
37292108Sphk}
373