geom_slice.c revision 92108
179643Sobrien/*-
279643Sobrien * Copyright (c) 2002 Poul-Henning Kamp
379643Sobrien * Copyright (c) 2002 Networks Associates Technology, Inc.
479643Sobrien * All rights reserved.
579643Sobrien *
679643Sobrien * This software was developed for the FreeBSD Project by Poul-Henning Kamp
779643Sobrien * and NAI Labs, the Security Research Division of Network Associates, Inc.
879643Sobrien * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
979643Sobrien * DARPA CHATS research program.
1079643Sobrien *
1179643Sobrien * Redistribution and use in source and binary forms, with or without
1279643Sobrien * modification, are permitted provided that the following conditions
1379643Sobrien * are met:
1496330Sobrien * 1. Redistributions of source code must retain the above copyright
1596330Sobrien *    notice, this list of conditions and the following disclaimer.
1679643Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1779643Sobrien *    notice, this list of conditions and the following disclaimer in the
1879643Sobrien *    documentation and/or other materials provided with the distribution.
1996330Sobrien * 3. The names of the authors may not be used to endorse or promote
2079643Sobrien *    products derived from this software without specific prior written
2179643Sobrien *    permission.
2279643Sobrien *
2379643Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2479643Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2596330Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2696330Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2796330Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2896330Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2996330Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3096330Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3196330Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3296330Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3396330Sobrien * SUCH DAMAGE.
3479643Sobrien *
3579643Sobrien * $FreeBSD: head/sys/geom/geom_slice.c 92108 2002-03-11 21:42:35Z phk $
3679643Sobrien */
3779643Sobrien
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 <err.h>
46#else
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/malloc.h>
50#include <sys/bio.h>
51#include <sys/sysctl.h>
52#include <sys/proc.h>
53#include <sys/kthread.h>
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#endif
57#include <sys/errno.h>
58#include <sys/sbuf.h>
59#include <geom/geom.h>
60#include <geom/geom_slice.h>
61#include <machine/stdarg.h>
62
63struct g_slicer *
64g_slice_init(unsigned nslice, unsigned scsize)
65{
66	struct g_slicer *gsp;
67
68	gsp = g_malloc(sizeof *gsp + nslice * sizeof(struct g_slice) + scsize,
69	    M_WAITOK | M_ZERO);
70	gsp->softc = gsp + 1;
71	gsp->slices = (struct g_slice *)((u_char *)gsp->softc + scsize);
72	gsp->nslice = nslice;
73	return (gsp);
74}
75
76int
77g_slice_access(struct g_provider *pp, int dr, int dw, int de)
78{
79	int error, i;
80	struct g_geom *gp;
81	struct g_consumer *cp;
82	struct g_provider *pp2;
83	struct g_slicer *gsp;
84	struct g_slice *gsl, *gsl2;
85
86	gp = pp->geom;
87	cp = LIST_FIRST(&gp->consumer);
88	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
89	gsp = gp->softc;
90	gsl = &gsp->slices[pp->index];
91	for (i = 0; i < gsp->nslice; i++) {
92		gsl2 = &gsp->slices[i];
93		if (gsl2->length == 0)
94			continue;
95		if (i == pp->index)
96			continue;
97		if (gsl->offset + gsl->length <= gsl2->offset)
98			continue;
99		if (gsl2->offset + gsl2->length <= gsl->offset)
100			continue;
101		/* overlap */
102		pp2 = gsl2->provider;
103		if ((pp->acw + dw) > 0 && pp2->ace > 0)
104			return (EPERM);
105		if ((pp->ace + de) > 0 && pp2->acw > 0)
106			return (EPERM);
107	}
108	/* On first open, grab an extra "exclusive" bit */
109	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
110		de++;
111	/* ... and let go of it on last close */
112	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
113		de--;
114	error = g_access_rel(cp, dr, dw, de);
115	return (error);
116}
117
118void
119g_slice_start(struct bio *bp)
120{
121	struct bio *bp2;
122	struct g_provider *pp;
123	struct g_geom *gp;
124	struct g_consumer *cp;
125	struct g_slicer *gsp;
126	struct g_slice *gsl;
127	int index;
128
129	pp = bp->bio_to;
130	gp = pp->geom;
131	gsp = gp->softc;
132	cp = LIST_FIRST(&gp->consumer);
133	index = pp->index;
134	switch(bp->bio_cmd) {
135	case BIO_READ:
136	case BIO_WRITE:
137	case BIO_DELETE:
138		gsl = &gsp->slices[index];
139		if (bp->bio_offset > gsl->length) {
140			bp->bio_error = EINVAL; /* XXX: EWHAT ? */
141			g_io_deliver(bp);
142			return;
143		}
144		bp2 = g_clone_bio(bp);
145		if (bp2->bio_offset + bp2->bio_length > gsl->length)
146			bp2->bio_length = gsl->length - bp2->bio_offset;
147		bp2->bio_done = g_std_done;
148		bp2->bio_offset += gsl->offset;
149		g_io_request(bp2, cp);
150		return;
151	case BIO_GETATTR:
152	case BIO_SETATTR:
153		if (g_haveattr_off_t(bp, "GEOM::mediasize",
154		    gsp->slices[index].length))
155			return;
156		if (gsp->start(bp))
157			return;
158		bp2 = g_clone_bio(bp);
159		bp2->bio_done = g_std_done;
160		g_io_request(bp2, cp);
161		break;
162	default:
163		bp->bio_error = EOPNOTSUPP;
164		g_io_deliver(bp);
165		return;
166	}
167}
168
169void
170g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
171{
172	struct g_mbr_softc *mp;
173	struct g_slicer *gsp;
174
175	gsp = gp->softc;
176	mp = gsp->softc;
177	if (pp != NULL) {
178		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
179		sbuf_printf(sb, "%s<length>%llu</length>\n",
180		    indent, gsp->slices[pp->index].length);
181		sbuf_printf(sb, "%s<seclength>%llu</seclength>\n",
182		    indent, gsp->slices[pp->index].length / 512);
183		sbuf_printf(sb, "%s<offset>%llu</offset>\n",
184		    indent, gsp->slices[pp->index].offset);
185		sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n",
186		    indent, gsp->slices[pp->index].offset / 512);
187	}
188}
189
190struct g_provider *
191g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...)
192{
193	struct g_provider *pp;
194	struct g_slicer *gsp;
195	va_list ap;
196	struct sbuf *sb;
197
198	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
199	g_topology_lock();
200	gsp = gp->softc;
201	va_start(ap, fmt);
202	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
203	sbuf_vprintf(sb, fmt, ap);
204	sbuf_finish(sb);
205	pp = g_new_providerf(gp, sbuf_data(sb));
206
207	pp->index = index;
208	gsp->slices[index].length = length;
209	gsp->slices[index].offset = offset;
210	gsp->slices[index].provider = pp;
211	sbuf_delete(sb);
212	g_topology_unlock();
213	return(pp);
214}
215
216struct g_geom *
217g_slice_new(struct g_method *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
218{
219	struct g_geom *gp;
220	struct g_slicer *gsp;
221	struct g_consumer *cp;
222	void **vp;
223	int error;
224
225	g_topology_assert();
226	vp = (void **)extrap;
227	gp = g_new_geomf(mp, "%s", pp->name);
228	gsp = g_slice_init(slices, extra);
229	gsp->start = start;
230	gp->softc = gsp;
231	gp->start = g_slice_start;
232	gp->spoiled = g_std_spoiled;
233	gp->dumpconf = g_slice_dumpconf;
234	cp = g_new_consumer(gp);
235	g_attach(cp, pp);
236	error = g_access_rel(cp, 1, 0, 0);
237	if (error) {
238		g_dettach(cp);
239		g_destroy_consumer(cp);
240		g_free(gp->softc);
241		g_destroy_geom(gp);
242		return (NULL);
243	}
244	*vp = gsp->softc;
245	*cpp = cp;
246	return (gp);
247}
248
249void
250g_slice_orphan(struct g_consumer *cp, struct thread *tp __unused)
251{
252	struct g_geom *gp;
253	struct g_provider *pp;
254	int error;
255
256	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
257	g_topology_assert();
258	KASSERT(cp->provider->error != 0,
259	    ("g_slice_orphan with error == 0"));
260
261	gp = cp->geom;
262	gp->flags |= G_GEOM_WITHER;
263	/* First prevent any new requests */
264	error = cp->provider->error;
265	LIST_FOREACH(pp, &gp->provider, provider)
266		g_orphan_provider(pp, error);
267
268	return;
269	if (cp->biocount > 0)
270		return;
271
272	/* Then selfdestruct */
273	if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
274		g_access_abs(cp, 0, 0, 0);
275	g_dettach(cp);
276	g_destroy_consumer(cp);
277	return;
278}
279