geom_slice.c revision 105957
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 105957 2002-10-25 20:09:45Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/stdint.h>
41#ifndef _KERNEL
42#include <stdio.h>
43#include <unistd.h>
44#include <stdlib.h>
45#include <signal.h>
46#include <string.h>
47#include <err.h>
48#else
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/bio.h>
53#include <sys/sysctl.h>
54#include <sys/proc.h>
55#include <sys/kthread.h>
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#endif
59#include <sys/errno.h>
60#include <sys/sbuf.h>
61#include <geom/geom.h>
62#include <geom/geom_slice.h>
63#include <machine/stdarg.h>
64
65static g_orphan_t g_slice_orphan;
66static g_access_t g_slice_access;
67static g_start_t g_slice_start;
68
69static struct g_slicer *
70g_slice_init(unsigned nslice, unsigned scsize)
71{
72	struct g_slicer *gsp;
73
74	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
75	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
76	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
77	    M_WAITOK | M_ZERO);
78	gsp->nslice = nslice;
79	return (gsp);
80}
81
82static int
83g_slice_access(struct g_provider *pp, int dr, int dw, int de)
84{
85	int error, i;
86	struct g_geom *gp;
87	struct g_consumer *cp;
88	struct g_provider *pp2;
89	struct g_slicer *gsp;
90	struct g_slice *gsl, *gsl2;
91
92	gp = pp->geom;
93	cp = LIST_FIRST(&gp->consumer);
94	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
95	gsp = gp->softc;
96	gsl = &gsp->slices[pp->index];
97	for (i = 0; i < gsp->nslice; i++) {
98		gsl2 = &gsp->slices[i];
99		if (gsl2->length == 0)
100			continue;
101		if (i == pp->index)
102			continue;
103		if (gsl->offset + gsl->length <= gsl2->offset)
104			continue;
105		if (gsl2->offset + gsl2->length <= gsl->offset)
106			continue;
107		/* overlap */
108		pp2 = gsl2->provider;
109		if ((pp->acw + dw) > 0 && pp2->ace > 0)
110			return (EPERM);
111		if ((pp->ace + de) > 0 && pp2->acw > 0)
112			return (EPERM);
113	}
114	/* On first open, grab an extra "exclusive" bit */
115	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
116		de++;
117	/* ... and let go of it on last close */
118	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
119		de--;
120	error = g_access_rel(cp, dr, dw, de);
121	return (error);
122}
123
124static void
125g_slice_start(struct bio *bp)
126{
127	struct bio *bp2;
128	struct g_provider *pp;
129	struct g_geom *gp;
130	struct g_consumer *cp;
131	struct g_slicer *gsp;
132	struct g_slice *gsl;
133	int index;
134	off_t t;
135
136	pp = bp->bio_to;
137	gp = pp->geom;
138	gsp = gp->softc;
139	cp = LIST_FIRST(&gp->consumer);
140	index = pp->index;
141	gsl = &gsp->slices[index];
142	switch(bp->bio_cmd) {
143	case BIO_READ:
144	case BIO_WRITE:
145	case BIO_DELETE:
146		if (bp->bio_offset > gsl->length) {
147			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
148			return;
149		}
150		bp2 = g_clone_bio(bp);
151		if (bp2 == NULL) {
152			g_io_deliver(bp, ENOMEM);
153			return;
154		}
155		if (bp2->bio_offset + bp2->bio_length > gsl->length)
156			bp2->bio_length = gsl->length - bp2->bio_offset;
157		bp2->bio_done = g_std_done;
158		bp2->bio_offset += gsl->offset;
159		g_io_request(bp2, cp);
160		return;
161	case BIO_GETATTR:
162	case BIO_SETATTR:
163		/* Give the real method a chance to override */
164		if (gsp->start(bp))
165			return;
166		if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
167			t = gsp->cfrontstuff;
168			if (gsp->frontstuff > t)
169				t = gsp->frontstuff;
170			t -= gsl->offset;
171			if (t < 0)
172				t = 0;
173			if (t > gsl->length)
174				t = gsl->length;
175			g_handleattr_off_t(bp, "GEOM::frontstuff", t);
176			return;
177		}
178#ifdef _KERNEL
179		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
180			struct g_kerneldump *gkd;
181
182			gkd = (struct g_kerneldump *)bp->bio_data;
183			gkd->offset += gsp->slices[index].offset;
184			if (gkd->length > gsp->slices[index].length)
185				gkd->length = gsp->slices[index].length;
186			/* now, pass it on downwards... */
187		}
188#endif
189		bp2 = g_clone_bio(bp);
190		if (bp2 == NULL) {
191			g_io_deliver(bp, ENOMEM);
192			return;
193		}
194		bp2->bio_done = g_std_done;
195		g_io_request(bp2, cp);
196		break;
197	default:
198		g_io_deliver(bp, EOPNOTSUPP);
199		return;
200	}
201}
202
203void
204g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
205{
206	struct g_slicer *gsp;
207
208	gsp = gp->softc;
209	if (gp != NULL && (pp == NULL && cp == NULL)) {
210		sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
211		    indent, (intmax_t)gsp->frontstuff);
212	}
213	if (pp != NULL) {
214		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
215		sbuf_printf(sb, "%s<length>%ju</length>\n",
216		    indent, (uintmax_t)gsp->slices[pp->index].length);
217		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
218		    (uintmax_t)gsp->slices[pp->index].length / 512);
219		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
220		    (uintmax_t)gsp->slices[pp->index].offset);
221		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
222		    (uintmax_t)gsp->slices[pp->index].offset / 512);
223	}
224}
225
226int
227g_slice_config(struct g_geom *gp, int index, int how, off_t offset, off_t length, u_int sectorsize, char *fmt, ...)
228{
229	struct g_provider *pp;
230	struct g_slicer *gsp;
231	struct g_slice *gsl;
232	va_list ap;
233	struct sbuf *sb;
234	int error, acc;
235
236	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
237	     gp->name, index, how);
238	g_topology_assert();
239	gsp = gp->softc;
240	error = 0;
241	if (index >= gsp->nslice)
242		return(EINVAL);
243	gsl = &gsp->slices[index];
244	pp = gsl->provider;
245	if (pp != NULL)
246		acc = pp->acr + pp->acw + pp->ace;
247	else
248		acc = 0;
249	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
250		if (length < gsl->length)
251			return(EBUSY);
252		if (offset != gsl->offset)
253			return(EBUSY);
254	}
255	/* XXX: check offset + length <= MEDIASIZE */
256	if (how == G_SLICE_CONFIG_CHECK)
257		return (0);
258	gsl->length = length;
259	gsl->offset = offset;
260	gsl->sectorsize = sectorsize;
261	if (length == 0) {
262		if (pp == NULL)
263			return (0);
264		if (bootverbose)
265			printf("GEOM: Deconfigure %s\n", pp->name);
266		g_orphan_provider(pp, ENXIO);
267		gsl->provider = NULL;
268		gsp->nprovider--;
269		return (0);
270	}
271	if (pp != NULL) {
272		if (bootverbose)
273			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
274			    pp->name, (intmax_t)offset, (intmax_t)length,
275			    (intmax_t)(offset + length - 1));
276		return (0);
277	}
278	va_start(ap, fmt);
279	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
280	sbuf_vprintf(sb, fmt, ap);
281	sbuf_finish(sb);
282	pp = g_new_providerf(gp, sbuf_data(sb));
283	if (bootverbose)
284		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
285		    pp->name, (intmax_t)offset, (intmax_t)length,
286		    (intmax_t)(offset + length - 1));
287	pp->index = index;
288	pp->mediasize = gsl->length;
289	pp->sectorsize = gsl->sectorsize;
290	gsl->provider = pp;
291	gsp->nprovider++;
292	g_error_provider(pp, 0);
293	sbuf_delete(sb);
294	return(0);
295}
296
297struct g_provider *
298g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, u_int sectorsize, char *fmt, ...)
299{
300	struct g_provider *pp;
301	struct g_slicer *gsp;
302	va_list ap;
303	struct sbuf *sb;
304
305	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
306	g_topology_assert();
307	gsp = gp->softc;
308	va_start(ap, fmt);
309	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
310	sbuf_vprintf(sb, fmt, ap);
311	sbuf_finish(sb);
312	pp = g_new_providerf(gp, sbuf_data(sb));
313
314	pp->index = index;
315	gsp->slices[index].length = length;
316	gsp->slices[index].offset = offset;
317	gsp->slices[index].provider = pp;
318	gsp->slices[index].sectorsize = sectorsize;
319	pp->mediasize = gsp->slices[index].length;
320	pp->sectorsize = gsp->slices[index].sectorsize;
321	sbuf_delete(sb);
322	if (bootverbose)
323		printf("GEOM: Add %s, start %jd length %jd end %jd\n",
324		    pp->name, (intmax_t)offset, (intmax_t)length,
325		    (intmax_t)(offset + length - 1));
326	return(pp);
327}
328
329struct g_geom *
330g_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)
331{
332	struct g_geom *gp;
333	struct g_slicer *gsp;
334	struct g_consumer *cp;
335	void **vp;
336	int error, i;
337
338	g_topology_assert();
339	vp = (void **)extrap;
340	gp = g_new_geomf(mp, "%s", pp->name);
341	gsp = g_slice_init(slices, extra);
342	gsp->start = start;
343	gp->access = g_slice_access;
344	gp->orphan = g_slice_orphan;
345	gp->softc = gsp;
346	gp->start = g_slice_start;
347	gp->spoiled = g_std_spoiled;
348	gp->dumpconf = g_slice_dumpconf;
349	cp = g_new_consumer(gp);
350	error = g_attach(cp, pp);
351	if (error == 0)
352		error = g_access_rel(cp, 1, 0, 0);
353	if (error) {
354		if (cp->provider != NULL)
355			g_detach(cp);
356		g_destroy_consumer(cp);
357		g_free(gsp->slices);
358		g_free(gp->softc);
359		g_destroy_geom(gp);
360		return (NULL);
361	}
362	/* Find out if there are any magic bytes on the consumer */
363	i = sizeof gsp->cfrontstuff;
364	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
365	if (error)
366		gsp->cfrontstuff = 0;
367	*vp = gsp->softc;
368	*cpp = cp;
369	return (gp);
370}
371
372static void
373g_slice_orphan(struct g_consumer *cp)
374{
375	struct g_geom *gp;
376	struct g_provider *pp;
377	int error;
378
379	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
380	g_topology_assert();
381	KASSERT(cp->provider->error != 0,
382	    ("g_slice_orphan with error == 0"));
383
384	gp = cp->geom;
385	gp->flags |= G_GEOM_WITHER;
386	error = cp->provider->error;
387	LIST_FOREACH(pp, &gp->provider, provider)
388		g_orphan_provider(pp, error);
389	return;
390}
391