geom_slice.c revision 106101
1183370Simp/*-
2183370Simp * Copyright (c) 2002 Poul-Henning Kamp
3183370Simp * Copyright (c) 2002 Networks Associates Technology, Inc.
4183370Simp * All rights reserved.
5183370Simp *
6183370Simp * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7183370Simp * and NAI Labs, the Security Research Division of Network Associates, Inc.
8183370Simp * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9183370Simp * DARPA CHATS research program.
10183370Simp *
11183370Simp * Redistribution and use in source and binary forms, with or without
12183370Simp * modification, are permitted provided that the following conditions
13183370Simp * are met:
14183370Simp * 1. Redistributions of source code must retain the above copyright
15183370Simp *    notice, this list of conditions and the following disclaimer.
16183370Simp * 2. Redistributions in binary form must reproduce the above copyright
17183370Simp *    notice, this list of conditions and the following disclaimer in the
18183370Simp *    documentation and/or other materials provided with the distribution.
19183370Simp * 3. The names of the authors may not be used to endorse or promote
20183370Simp *    products derived from this software without specific prior written
21183370Simp *    permission.
22183370Simp *
23183370Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24183370Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25183370Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26183370Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27183370Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28183370Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29183370Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30183370Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31183370Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32183370Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33183370Simp * SUCH DAMAGE.
34183370Simp *
35183370Simp * $FreeBSD: head/sys/geom/geom_slice.c 106101 2002-10-28 22:43:54Z phk $
36183370Simp */
37183370Simp
38183370Simp
39183370Simp#include <sys/param.h>
40183370Simp#include <sys/stdint.h>
41183370Simp#ifndef _KERNEL
42183370Simp#include <stdio.h>
43183370Simp#include <unistd.h>
44183370Simp#include <stdlib.h>
45183370Simp#include <signal.h>
46183370Simp#include <string.h>
47183370Simp#include <err.h>
48183370Simp#else
49183370Simp#include <sys/systm.h>
50183370Simp#include <sys/kernel.h>
51183370Simp#include <sys/malloc.h>
52183370Simp#include <sys/bio.h>
53183370Simp#include <sys/sysctl.h>
54183370Simp#include <sys/proc.h>
55183370Simp#include <sys/kthread.h>
56183370Simp#include <sys/lock.h>
57183370Simp#include <sys/mutex.h>
58183370Simp#endif
59183370Simp#include <sys/errno.h>
60183370Simp#include <sys/sbuf.h>
61183370Simp#include <geom/geom.h>
62183370Simp#include <geom/geom_slice.h>
63183370Simp#include <machine/stdarg.h>
64183370Simp
65183370Simpstatic g_orphan_t g_slice_orphan;
66183370Simpstatic g_access_t g_slice_access;
67183370Simpstatic g_start_t g_slice_start;
68183370Simp
69183370Simpstatic struct g_slicer *
70183370Simpg_slice_init(unsigned nslice, unsigned scsize)
71183370Simp{
72183370Simp	struct g_slicer *gsp;
73183370Simp
74183370Simp	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
75183370Simp	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
76183370Simp	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
77183370Simp	    M_WAITOK | M_ZERO);
78183370Simp	gsp->nslice = nslice;
79183370Simp	return (gsp);
80183370Simp}
81183370Simp
82183370Simpstatic int
83183370Simpg_slice_access(struct g_provider *pp, int dr, int dw, int de)
84183370Simp{
85183370Simp	int error, i;
86183370Simp	struct g_geom *gp;
87183370Simp	struct g_consumer *cp;
88183370Simp	struct g_provider *pp2;
89183370Simp	struct g_slicer *gsp;
90183370Simp	struct g_slice *gsl, *gsl2;
91183370Simp
92183370Simp	gp = pp->geom;
93183370Simp	cp = LIST_FIRST(&gp->consumer);
94183370Simp	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
95183370Simp	gsp = gp->softc;
96183370Simp	gsl = &gsp->slices[pp->index];
97183370Simp	for (i = 0; i < gsp->nslice; i++) {
98183370Simp		gsl2 = &gsp->slices[i];
99183370Simp		if (gsl2->length == 0)
100183370Simp			continue;
101183370Simp		if (i == pp->index)
102183370Simp			continue;
103183370Simp		if (gsl->offset + gsl->length <= gsl2->offset)
104183370Simp			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 (indent == NULL) {
210		sbuf_printf(sb, " i %u", pp->index);
211		sbuf_printf(sb, " o %ju",
212		    (uintmax_t)gsp->slices[pp->index].offset);
213		return;
214	}
215	if (gp != NULL && (pp == NULL && cp == NULL)) {
216		sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
217		    indent, (intmax_t)gsp->frontstuff);
218	}
219	if (pp != NULL) {
220		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
221		sbuf_printf(sb, "%s<length>%ju</length>\n",
222		    indent, (uintmax_t)gsp->slices[pp->index].length);
223		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
224		    (uintmax_t)gsp->slices[pp->index].length / 512);
225		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
226		    (uintmax_t)gsp->slices[pp->index].offset);
227		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
228		    (uintmax_t)gsp->slices[pp->index].offset / 512);
229	}
230}
231
232int
233g_slice_config(struct g_geom *gp, int index, int how, off_t offset, off_t length, u_int sectorsize, char *fmt, ...)
234{
235	struct g_provider *pp;
236	struct g_slicer *gsp;
237	struct g_slice *gsl;
238	va_list ap;
239	struct sbuf *sb;
240	int error, acc;
241
242	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
243	     gp->name, index, how);
244	g_topology_assert();
245	gsp = gp->softc;
246	error = 0;
247	if (index >= gsp->nslice)
248		return(EINVAL);
249	gsl = &gsp->slices[index];
250	pp = gsl->provider;
251	if (pp != NULL)
252		acc = pp->acr + pp->acw + pp->ace;
253	else
254		acc = 0;
255	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
256		if (length < gsl->length)
257			return(EBUSY);
258		if (offset != gsl->offset)
259			return(EBUSY);
260	}
261	/* XXX: check offset + length <= MEDIASIZE */
262	if (how == G_SLICE_CONFIG_CHECK)
263		return (0);
264	gsl->length = length;
265	gsl->offset = offset;
266	gsl->sectorsize = sectorsize;
267	if (length == 0) {
268		if (pp == NULL)
269			return (0);
270		if (bootverbose)
271			printf("GEOM: Deconfigure %s\n", pp->name);
272		g_orphan_provider(pp, ENXIO);
273		gsl->provider = NULL;
274		gsp->nprovider--;
275		return (0);
276	}
277	if (pp != NULL) {
278		if (bootverbose)
279			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
280			    pp->name, (intmax_t)offset, (intmax_t)length,
281			    (intmax_t)(offset + length - 1));
282		return (0);
283	}
284	va_start(ap, fmt);
285	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
286	sbuf_vprintf(sb, fmt, ap);
287	sbuf_finish(sb);
288	pp = g_new_providerf(gp, sbuf_data(sb));
289	if (bootverbose)
290		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
291		    pp->name, (intmax_t)offset, (intmax_t)length,
292		    (intmax_t)(offset + length - 1));
293	pp->index = index;
294	pp->mediasize = gsl->length;
295	pp->sectorsize = gsl->sectorsize;
296	gsl->provider = pp;
297	gsp->nprovider++;
298	g_error_provider(pp, 0);
299	sbuf_delete(sb);
300	return(0);
301}
302
303struct g_provider *
304g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, u_int sectorsize, char *fmt, ...)
305{
306	struct g_provider *pp;
307	struct g_slicer *gsp;
308	va_list ap;
309	struct sbuf *sb;
310
311	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
312	g_topology_assert();
313	gsp = gp->softc;
314	va_start(ap, fmt);
315	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
316	sbuf_vprintf(sb, fmt, ap);
317	sbuf_finish(sb);
318	pp = g_new_providerf(gp, sbuf_data(sb));
319
320	pp->index = index;
321	gsp->slices[index].length = length;
322	gsp->slices[index].offset = offset;
323	gsp->slices[index].provider = pp;
324	gsp->slices[index].sectorsize = sectorsize;
325	pp->mediasize = gsp->slices[index].length;
326	pp->sectorsize = gsp->slices[index].sectorsize;
327	sbuf_delete(sb);
328	if (bootverbose)
329		printf("GEOM: Add %s, start %jd length %jd end %jd\n",
330		    pp->name, (intmax_t)offset, (intmax_t)length,
331		    (intmax_t)(offset + length - 1));
332	return(pp);
333}
334
335struct g_geom *
336g_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)
337{
338	struct g_geom *gp;
339	struct g_slicer *gsp;
340	struct g_consumer *cp;
341	void **vp;
342	int error, i;
343
344	g_topology_assert();
345	vp = (void **)extrap;
346	gp = g_new_geomf(mp, "%s", pp->name);
347	gsp = g_slice_init(slices, extra);
348	gsp->start = start;
349	gp->access = g_slice_access;
350	gp->orphan = g_slice_orphan;
351	gp->softc = gsp;
352	gp->start = g_slice_start;
353	gp->spoiled = g_std_spoiled;
354	gp->dumpconf = g_slice_dumpconf;
355	cp = g_new_consumer(gp);
356	error = g_attach(cp, pp);
357	if (error == 0)
358		error = g_access_rel(cp, 1, 0, 0);
359	if (error) {
360		if (cp->provider != NULL)
361			g_detach(cp);
362		g_destroy_consumer(cp);
363		g_free(gsp->slices);
364		g_free(gp->softc);
365		g_destroy_geom(gp);
366		return (NULL);
367	}
368	/* Find out if there are any magic bytes on the consumer */
369	i = sizeof gsp->cfrontstuff;
370	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
371	if (error)
372		gsp->cfrontstuff = 0;
373	*vp = gsp->softc;
374	*cpp = cp;
375	return (gp);
376}
377
378static void
379g_slice_orphan(struct g_consumer *cp)
380{
381	struct g_geom *gp;
382	struct g_provider *pp;
383	int error;
384
385	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
386	g_topology_assert();
387	KASSERT(cp->provider->error != 0,
388	    ("g_slice_orphan with error == 0"));
389
390	gp = cp->geom;
391	gp->flags |= G_GEOM_WITHER;
392	error = cp->provider->error;
393	LIST_FOREACH(pp, &gp->provider, provider)
394		g_orphan_provider(pp, error);
395	return;
396}
397