geom_slice.c revision 96952
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_slice.c 96952 2002-05-19 18:59:39Z phk $
36 */
37
38
39#include <sys/param.h>
40#ifndef _KERNEL
41#include <stdio.h>
42#include <unistd.h>
43#include <stdlib.h>
44#include <signal.h>
45#include <string.h>
46#include <err.h>
47#else
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/malloc.h>
51#include <sys/bio.h>
52#include <sys/sysctl.h>
53#include <sys/proc.h>
54#include <sys/kthread.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#endif
58#include <sys/errno.h>
59#include <sys/sbuf.h>
60#include <geom/geom.h>
61#include <geom/geom_slice.h>
62#include <machine/stdarg.h>
63
64static g_orphan_t g_slice_orphan;
65static g_access_t g_slice_access;
66static g_start_t g_slice_start;
67
68static struct g_slicer *
69g_slice_init(unsigned nslice, unsigned scsize)
70{
71	struct g_slicer *gsp;
72
73	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
74	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
75	gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO);
76	gsp->nslice = nslice;
77	return (gsp);
78}
79
80static int
81g_slice_access(struct g_provider *pp, int dr, int dw, int de)
82{
83	int error, i;
84	struct g_geom *gp;
85	struct g_consumer *cp;
86	struct g_provider *pp2;
87	struct g_slicer *gsp;
88	struct g_slice *gsl, *gsl2;
89
90	gp = pp->geom;
91	cp = LIST_FIRST(&gp->consumer);
92	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
93	gsp = gp->softc;
94	gsl = &gsp->slices[pp->index];
95	for (i = 0; i < gsp->nslice; i++) {
96		gsl2 = &gsp->slices[i];
97		if (gsl2->length == 0)
98			continue;
99		if (i == pp->index)
100			continue;
101		if (gsl->offset + gsl->length <= gsl2->offset)
102			continue;
103		if (gsl2->offset + gsl2->length <= gsl->offset)
104			continue;
105		/* overlap */
106		pp2 = gsl2->provider;
107		if ((pp->acw + dw) > 0 && pp2->ace > 0)
108			return (EPERM);
109		if ((pp->ace + de) > 0 && pp2->acw > 0)
110			return (EPERM);
111	}
112	/* On first open, grab an extra "exclusive" bit */
113	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
114		de++;
115	/* ... and let go of it on last close */
116	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
117		de--;
118	error = g_access_rel(cp, dr, dw, de);
119	pp->mediasize = gsp->slices[pp->index].length;
120	return (error);
121}
122
123static void
124g_slice_start(struct bio *bp)
125{
126	struct bio *bp2;
127	struct g_provider *pp;
128	struct g_geom *gp;
129	struct g_consumer *cp;
130	struct g_slicer *gsp;
131	struct g_slice *gsl;
132	int index;
133	off_t t;
134
135	pp = bp->bio_to;
136	gp = pp->geom;
137	gsp = gp->softc;
138	cp = LIST_FIRST(&gp->consumer);
139	index = pp->index;
140	gsl = &gsp->slices[index];
141	switch(bp->bio_cmd) {
142	case BIO_READ:
143	case BIO_WRITE:
144	case BIO_DELETE:
145		if (bp->bio_offset > gsl->length) {
146			bp->bio_error = EINVAL; /* XXX: EWHAT ? */
147			g_io_deliver(bp);
148			return;
149		}
150		bp2 = g_clone_bio(bp);
151		if (bp2->bio_offset + bp2->bio_length > gsl->length)
152			bp2->bio_length = gsl->length - bp2->bio_offset;
153		bp2->bio_done = g_std_done;
154		bp2->bio_offset += gsl->offset;
155		g_io_request(bp2, cp);
156		return;
157	case BIO_GETATTR:
158	case BIO_SETATTR:
159		/* Give the real method a chance to override */
160		if (gsp->start(bp))
161			return;
162		if (g_haveattr_off_t(bp, "GEOM::mediasize",
163		    gsp->slices[index].length))
164			return;
165		if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
166			t = gsp->cfrontstuff;
167			if (gsp->frontstuff > t)
168				t = gsp->frontstuff;
169			t -= gsl->offset;
170			if (t < 0)
171				t = 0;
172			if (t > gsl->length)
173				t = gsl->length;
174			g_haveattr_off_t(bp, "GEOM::frontstuff", t);
175			return;
176		}
177#ifdef _KERNEL
178		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
179			struct g_kerneldump *gkd;
180
181			gkd = (struct g_kerneldump *)bp->bio_data;
182			gkd->offset += gsp->slices[index].offset;
183			if (gkd->length > gsp->slices[index].length)
184				gkd->length = gsp->slices[index].length;
185			/* now, pass it on downwards... */
186		}
187#endif
188		bp2 = g_clone_bio(bp);
189		bp2->bio_done = g_std_done;
190		g_io_request(bp2, cp);
191		break;
192	default:
193		bp->bio_error = EOPNOTSUPP;
194		g_io_deliver(bp);
195		return;
196	}
197}
198
199void
200g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
201{
202	struct g_mbr_softc *mp;
203	struct g_slicer *gsp;
204
205	gsp = gp->softc;
206	mp = gsp->softc;
207	if (gp != NULL && (pp == NULL && cp == NULL)) {
208		sbuf_printf(sb, "%s<frontstuff>%llu</frontstuff>\n",
209		    indent, (unsigned long long)gsp->frontstuff);
210	}
211	if (pp != NULL) {
212		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
213		sbuf_printf(sb, "%s<length>%llu</length>\n",
214		    indent, (unsigned long long)gsp->slices[pp->index].length);
215		sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent,
216		    (unsigned long long)gsp->slices[pp->index].length / 512);
217		sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent,
218		    (unsigned long long)gsp->slices[pp->index].offset);
219		sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent,
220		    (unsigned long long)gsp->slices[pp->index].offset / 512);
221	}
222}
223
224struct g_provider *
225g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...)
226{
227	struct g_provider *pp;
228	struct g_slicer *gsp;
229	va_list ap;
230	struct sbuf *sb;
231
232	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
233	g_topology_lock();
234	gsp = gp->softc;
235	va_start(ap, fmt);
236	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
237	sbuf_vprintf(sb, fmt, ap);
238	sbuf_finish(sb);
239	pp = g_new_providerf(gp, sbuf_data(sb));
240
241	pp->index = index;
242	gsp->slices[index].length = length;
243	gsp->slices[index].offset = offset;
244	gsp->slices[index].provider = pp;
245	sbuf_delete(sb);
246	g_topology_unlock();
247	return(pp);
248}
249
250struct g_geom *
251g_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)
252{
253	struct g_geom *gp;
254	struct g_slicer *gsp;
255	struct g_consumer *cp;
256	void **vp;
257	int error, i;
258
259	g_topology_assert();
260	vp = (void **)extrap;
261	gp = g_new_geomf(mp, "%s", pp->name);
262	gsp = g_slice_init(slices, extra);
263	gsp->start = start;
264	gp->access = g_slice_access;
265	gp->orphan = g_slice_orphan;
266	gp->softc = gsp;
267	gp->start = g_slice_start;
268	gp->spoiled = g_std_spoiled;
269	gp->dumpconf = g_slice_dumpconf;
270	cp = g_new_consumer(gp);
271	g_attach(cp, pp);
272	error = g_access_rel(cp, 1, 0, 0);
273	if (error) {
274		g_dettach(cp);
275		g_destroy_consumer(cp);
276		g_free(gsp->slices);
277		g_free(gp->softc);
278		g_destroy_geom(gp);
279		return (NULL);
280	}
281	/* Find out if there are any magic bytes on the consumer */
282	i = sizeof gsp->cfrontstuff;
283	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
284	if (error)
285		gsp->cfrontstuff = 0;
286	*vp = gsp->softc;
287	*cpp = cp;
288	return (gp);
289}
290
291static void
292g_slice_orphan(struct g_consumer *cp)
293{
294	struct g_geom *gp;
295	struct g_provider *pp;
296	int error;
297
298	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
299	g_topology_assert();
300	KASSERT(cp->provider->error != 0,
301	    ("g_slice_orphan with error == 0"));
302
303	gp = cp->geom;
304	gp->flags |= G_GEOM_WITHER;
305	error = cp->provider->error;
306	LIST_FOREACH(pp, &gp->provider, provider)
307		g_orphan_provider(pp, error);
308	return;
309}
310