geom_slice.c revision 113390
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 113390 2003-04-12 08:41:26Z 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
73111119Simp	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
74111119Simp	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
75104057Sphk	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
76111119Simp	    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{
84107953Sphk	int error;
85107953Sphk	u_int u;
8692108Sphk	struct g_geom *gp;
8792108Sphk	struct g_consumer *cp;
8892108Sphk	struct g_provider *pp2;
8992108Sphk	struct g_slicer *gsp;
9092108Sphk	struct g_slice *gsl, *gsl2;
9192108Sphk
9292108Sphk	gp = pp->geom;
9392108Sphk	cp = LIST_FIRST(&gp->consumer);
9492108Sphk	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
9592108Sphk	gsp = gp->softc;
9692108Sphk	gsl = &gsp->slices[pp->index];
97107953Sphk	for (u = 0; u < gsp->nslice; u++) {
98107953Sphk		gsl2 = &gsp->slices[u];
9992108Sphk		if (gsl2->length == 0)
10092108Sphk			continue;
101107953Sphk		if (u == pp->index)
10292108Sphk			continue;
10392108Sphk		if (gsl->offset + gsl->length <= gsl2->offset)
10492108Sphk			continue;
10592108Sphk		if (gsl2->offset + gsl2->length <= gsl->offset)
10692108Sphk			continue;
10792108Sphk		/* overlap */
10892108Sphk		pp2 = gsl2->provider;
10992108Sphk		if ((pp->acw + dw) > 0 && pp2->ace > 0)
11092108Sphk			return (EPERM);
11192108Sphk		if ((pp->ace + de) > 0 && pp2->acw > 0)
11292108Sphk			return (EPERM);
11392108Sphk	}
11492108Sphk	/* On first open, grab an extra "exclusive" bit */
11592108Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
11692108Sphk		de++;
11792108Sphk	/* ... and let go of it on last close */
11892108Sphk	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
11992108Sphk		de--;
12092108Sphk	error = g_access_rel(cp, dr, dw, de);
12192108Sphk	return (error);
12292108Sphk}
12392108Sphk
124107522Sphkvoid
125107522Sphkg_slice_finish_hot(struct bio *bp)
126107522Sphk{
127107522Sphk	struct bio *bp2;
128107522Sphk	struct g_geom *gp;
129107522Sphk	struct g_consumer *cp;
130107522Sphk	struct g_slicer *gsp;
131107522Sphk	struct g_slice *gsl;
132107953Sphk	int idx;
133107522Sphk
134107522Sphk	KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp));
135107522Sphk	KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp));
136107522Sphk	gp = bp->bio_to->geom;
137107522Sphk	gsp = gp->softc;
138107522Sphk	cp = LIST_FIRST(&gp->consumer);
139107522Sphk	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
140107953Sphk	idx = bp->bio_to->index;
141107953Sphk	gsl = &gsp->slices[idx];
142107522Sphk
143107522Sphk	bp2 = g_clone_bio(bp);
144107522Sphk	if (bp2 == NULL) {
145107522Sphk		g_io_deliver(bp, ENOMEM);
146107522Sphk		return;
147107522Sphk	}
148107522Sphk	if (bp2->bio_offset + bp2->bio_length > gsl->length)
149107522Sphk		bp2->bio_length = gsl->length - bp2->bio_offset;
150107522Sphk	bp2->bio_done = g_std_done;
151107522Sphk	bp2->bio_offset += gsl->offset;
152107522Sphk	g_io_request(bp2, cp);
153107522Sphk	return;
154107522Sphk}
155107522Sphk
15693776Sphkstatic void
15792108Sphkg_slice_start(struct bio *bp)
15892108Sphk{
15992108Sphk	struct bio *bp2;
16092108Sphk	struct g_provider *pp;
16192108Sphk	struct g_geom *gp;
16292108Sphk	struct g_consumer *cp;
16392108Sphk	struct g_slicer *gsp;
164107522Sphk	struct g_slice *gsl, *gmp;
165107953Sphk	int idx, error;
166107522Sphk	u_int m_index;
16794287Sphk	off_t t;
16892108Sphk
16992108Sphk	pp = bp->bio_to;
17092108Sphk	gp = pp->geom;
17192108Sphk	gsp = gp->softc;
17292108Sphk	cp = LIST_FIRST(&gp->consumer);
173107953Sphk	idx = pp->index;
174107953Sphk	gsl = &gsp->slices[idx];
17592108Sphk	switch(bp->bio_cmd) {
17692108Sphk	case BIO_READ:
17792108Sphk	case BIO_WRITE:
17892108Sphk	case BIO_DELETE:
17992108Sphk		if (bp->bio_offset > gsl->length) {
180104195Sphk			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
18192108Sphk			return;
18292108Sphk		}
183107522Sphk		/*
184107522Sphk		 * Check if we collide with any hot spaces, and call the
185107522Sphk		 * method once if so.
186107522Sphk		 */
187107832Sphk		t = bp->bio_offset + gsl->offset;
188108003Sphk		/* .ctl devices may take us negative */
189108003Sphk		if (t < 0 || (t + bp->bio_length) < 0) {
190108003Sphk			g_io_deliver(bp, EINVAL);
191108003Sphk			return;
192108003Sphk		}
193107522Sphk		for (m_index = 0; m_index < gsp->nhot; m_index++) {
194107522Sphk			gmp = &gsp->hot[m_index];
195107832Sphk			if (t >= gmp->offset + gmp->length)
196107522Sphk				continue;
197107832Sphk			if (t + bp->bio_length <= gmp->offset)
198107522Sphk				continue;
199107522Sphk			error = gsp->start(bp);
200107522Sphk			if (error == EJUSTRETURN)
201107522Sphk				return;
202107522Sphk			else if (error) {
203107522Sphk				g_io_deliver(bp, error);
204107522Sphk				return;
205107522Sphk			}
206107522Sphk			break;
207107522Sphk		}
20892108Sphk		bp2 = g_clone_bio(bp);
209104057Sphk		if (bp2 == NULL) {
210104195Sphk			g_io_deliver(bp, ENOMEM);
211104057Sphk			return;
212104057Sphk		}
21392108Sphk		if (bp2->bio_offset + bp2->bio_length > gsl->length)
21492108Sphk			bp2->bio_length = gsl->length - bp2->bio_offset;
21592108Sphk		bp2->bio_done = g_std_done;
21692108Sphk		bp2->bio_offset += gsl->offset;
21792108Sphk		g_io_request(bp2, cp);
21892108Sphk		return;
21992108Sphk	case BIO_GETATTR:
22094287Sphk		/* Give the real method a chance to override */
22194287Sphk		if (gsp->start(bp))
22294287Sphk			return;
22395310Sphk#ifdef _KERNEL
22495038Sphk		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
22595038Sphk			struct g_kerneldump *gkd;
22695038Sphk
22795038Sphk			gkd = (struct g_kerneldump *)bp->bio_data;
228107953Sphk			gkd->offset += gsp->slices[idx].offset;
229107953Sphk			if (gkd->length > gsp->slices[idx].length)
230107953Sphk				gkd->length = gsp->slices[idx].length;
23195038Sphk			/* now, pass it on downwards... */
23295038Sphk		}
23395310Sphk#endif
23492108Sphk		bp2 = g_clone_bio(bp);
235104081Sphk		if (bp2 == NULL) {
236104195Sphk			g_io_deliver(bp, ENOMEM);
237104081Sphk			return;
238104081Sphk		}
23992108Sphk		bp2->bio_done = g_std_done;
24092108Sphk		g_io_request(bp2, cp);
24192108Sphk		break;
24292108Sphk	default:
243104195Sphk		g_io_deliver(bp, EOPNOTSUPP);
24492108Sphk		return;
24592108Sphk	}
24692108Sphk}
24792108Sphk
24892108Sphkvoid
249107953Sphkg_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
25092108Sphk{
25192108Sphk	struct g_slicer *gsp;
25292108Sphk
25392108Sphk	gsp = gp->softc;
254106101Sphk	if (indent == NULL) {
255106101Sphk		sbuf_printf(sb, " i %u", pp->index);
256106101Sphk		sbuf_printf(sb, " o %ju",
257106101Sphk		    (uintmax_t)gsp->slices[pp->index].offset);
258106101Sphk		return;
259106101Sphk	}
26092108Sphk	if (pp != NULL) {
26192108Sphk		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
262105540Sphk		sbuf_printf(sb, "%s<length>%ju</length>\n",
263105540Sphk		    indent, (uintmax_t)gsp->slices[pp->index].length);
264105540Sphk		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
265105540Sphk		    (uintmax_t)gsp->slices[pp->index].length / 512);
266105540Sphk		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
267105540Sphk		    (uintmax_t)gsp->slices[pp->index].offset);
268105540Sphk		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
269105540Sphk		    (uintmax_t)gsp->slices[pp->index].offset / 512);
27092108Sphk	}
27192108Sphk}
27292108Sphk
273104064Sphkint
274107953Sphkg_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
275104064Sphk{
276110710Sphk	struct g_provider *pp, *pp2;
277104064Sphk	struct g_slicer *gsp;
278104064Sphk	struct g_slice *gsl;
279104064Sphk	va_list ap;
280104064Sphk	struct sbuf *sb;
281104064Sphk	int error, acc;
282104064Sphk
283104195Sphk	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
284107953Sphk	     gp->name, idx, how);
285104064Sphk	g_topology_assert();
286104064Sphk	gsp = gp->softc;
287104064Sphk	error = 0;
288107953Sphk	if (idx >= gsp->nslice)
289104064Sphk		return(EINVAL);
290107953Sphk	gsl = &gsp->slices[idx];
291104064Sphk	pp = gsl->provider;
292104064Sphk	if (pp != NULL)
293104064Sphk		acc = pp->acr + pp->acw + pp->ace;
294104064Sphk	else
295104064Sphk		acc = 0;
296104064Sphk	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
297104064Sphk		if (length < gsl->length)
298104064Sphk			return(EBUSY);
299104064Sphk		if (offset != gsl->offset)
300104064Sphk			return(EBUSY);
301104064Sphk	}
302104064Sphk	/* XXX: check offset + length <= MEDIASIZE */
303104064Sphk	if (how == G_SLICE_CONFIG_CHECK)
304104064Sphk		return (0);
305104064Sphk	gsl->length = length;
306104064Sphk	gsl->offset = offset;
307105542Sphk	gsl->sectorsize = sectorsize;
308105957Sphk	if (length == 0) {
309105957Sphk		if (pp == NULL)
310105957Sphk			return (0);
311105957Sphk		if (bootverbose)
312105957Sphk			printf("GEOM: Deconfigure %s\n", pp->name);
313104064Sphk		g_orphan_provider(pp, ENXIO);
314104064Sphk		gsl->provider = NULL;
315104064Sphk		gsp->nprovider--;
316104064Sphk		return (0);
317104064Sphk	}
318105957Sphk	if (pp != NULL) {
319105957Sphk		if (bootverbose)
320105957Sphk			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
321105957Sphk			    pp->name, (intmax_t)offset, (intmax_t)length,
322105957Sphk			    (intmax_t)(offset + length - 1));
323107116Sphk		pp->mediasize = gsl->length;
324105957Sphk		return (0);
325105957Sphk	}
326104064Sphk	va_start(ap, fmt);
327104064Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
328104064Sphk	sbuf_vprintf(sb, fmt, ap);
329104064Sphk	sbuf_finish(sb);
330104064Sphk	pp = g_new_providerf(gp, sbuf_data(sb));
331110710Sphk	pp2 = LIST_FIRST(&gp->consumer)->provider;
332110710Sphk	pp->flags = pp2->flags & G_PF_CANDELETE;
333110713Sphk	if (pp2->stripesize > 0) {
334110713Sphk		pp->stripesize = pp2->stripesize;
335110713Sphk		pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
336110713Sphk	}
337105957Sphk	if (bootverbose)
338105957Sphk		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
339105957Sphk		    pp->name, (intmax_t)offset, (intmax_t)length,
340105957Sphk		    (intmax_t)(offset + length - 1));
341107953Sphk	pp->index = idx;
342105542Sphk	pp->mediasize = gsl->length;
343105542Sphk	pp->sectorsize = gsl->sectorsize;
344104064Sphk	gsl->provider = pp;
345104064Sphk	gsp->nprovider++;
346104064Sphk	g_error_provider(pp, 0);
347104064Sphk	sbuf_delete(sb);
348104064Sphk	return(0);
349104064Sphk}
350104064Sphk
351107522Sphkint
352107953Sphkg_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length)
353107522Sphk{
354107522Sphk	struct g_slicer *gsp;
355107522Sphk	struct g_slice *gsl, *gsl2;
356107522Sphk
357107522Sphk	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()");
358107522Sphk	g_topology_assert();
359107522Sphk	gsp = gp->softc;
360107522Sphk	gsl = gsp->hot;
361107953Sphk	if(idx >= gsp->nhot) {
362111119Simp		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
363107522Sphk		if (gsp->hot != NULL)
364107522Sphk			bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2);
365107522Sphk		gsp->hot = gsl2;
366107522Sphk		if (gsp->hot != NULL)
367107522Sphk			g_free(gsl);
368107522Sphk		gsl = gsl2;
369107953Sphk		gsp->nhot = idx + 1;
370107522Sphk	}
371107522Sphk	if (bootverbose)
372107522Sphk		printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n",
373107953Sphk		    gp->name, idx, (intmax_t)offset, (intmax_t)length,
374107522Sphk		    (intmax_t)(offset + length - 1));
375107953Sphk	gsl[idx].offset = offset;
376107953Sphk	gsl[idx].length = length;
377107522Sphk	return (0);
378107522Sphk}
379107522Sphk
38092108Sphkstruct g_geom *
381107522Sphkg_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
38292108Sphk{
38392108Sphk	struct g_geom *gp;
38492108Sphk	struct g_slicer *gsp;
38592108Sphk	struct g_consumer *cp;
38692108Sphk	void **vp;
387113390Sphk	int error;
38892108Sphk
38992108Sphk	g_topology_assert();
39092108Sphk	vp = (void **)extrap;
39192108Sphk	gp = g_new_geomf(mp, "%s", pp->name);
39292108Sphk	gsp = g_slice_init(slices, extra);
39392108Sphk	gsp->start = start;
39493776Sphk	gp->access = g_slice_access;
39593776Sphk	gp->orphan = g_slice_orphan;
39692108Sphk	gp->softc = gsp;
39792108Sphk	gp->start = g_slice_start;
39892108Sphk	gp->spoiled = g_std_spoiled;
39992108Sphk	gp->dumpconf = g_slice_dumpconf;
40092108Sphk	cp = g_new_consumer(gp);
401105551Sphk	error = g_attach(cp, pp);
402105551Sphk	if (error == 0)
403105551Sphk		error = g_access_rel(cp, 1, 0, 0);
40492108Sphk	if (error) {
405105551Sphk		if (cp->provider != NULL)
406105551Sphk			g_detach(cp);
40792108Sphk		g_destroy_consumer(cp);
40892371Sphk		g_free(gsp->slices);
40992108Sphk		g_free(gp->softc);
41092108Sphk		g_destroy_geom(gp);
41192108Sphk		return (NULL);
41292108Sphk	}
41392108Sphk	*vp = gsp->softc;
41492108Sphk	*cpp = cp;
41592108Sphk	return (gp);
41692108Sphk}
41792108Sphk
41893776Sphkstatic void
41993250Sphkg_slice_orphan(struct g_consumer *cp)
42092108Sphk{
42192108Sphk	struct g_geom *gp;
42292108Sphk	struct g_provider *pp;
42392108Sphk	int error;
42492108Sphk
42592108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
42692108Sphk	g_topology_assert();
42792108Sphk	KASSERT(cp->provider->error != 0,
42892108Sphk	    ("g_slice_orphan with error == 0"));
42992108Sphk
43092108Sphk	gp = cp->geom;
431107522Sphk	/* XXX: Not good enough we leak the softc and its suballocations */
43292108Sphk	gp->flags |= G_GEOM_WITHER;
43392108Sphk	error = cp->provider->error;
43492108Sphk	LIST_FOREACH(pp, &gp->provider, provider)
43592108Sphk		g_orphan_provider(pp, error);
43692108Sphk	return;
43792108Sphk}
438