geom_slice.c revision 113878
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 113878 2003-04-22 21:19:17Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/bio.h>
44#include <sys/sysctl.h>
45#include <sys/proc.h>
46#include <sys/kthread.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/errno.h>
50#include <sys/sbuf.h>
51#include <geom/geom.h>
52#include <geom/geom_slice.h>
53#include <machine/stdarg.h>
54
55static g_orphan_t g_slice_orphan;
56static g_access_t g_slice_access;
57static g_start_t g_slice_start;
58
59static struct g_slicer *
60g_slice_init(unsigned nslice, unsigned scsize)
61{
62	struct g_slicer *gsp;
63
64	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
65	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
66	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
67	    M_WAITOK | M_ZERO);
68	gsp->nslice = nslice;
69	return (gsp);
70}
71
72static int
73g_slice_access(struct g_provider *pp, int dr, int dw, int de)
74{
75	int error;
76	u_int u;
77	struct g_geom *gp;
78	struct g_consumer *cp;
79	struct g_provider *pp2;
80	struct g_slicer *gsp;
81	struct g_slice *gsl, *gsl2;
82
83	gp = pp->geom;
84	cp = LIST_FIRST(&gp->consumer);
85	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
86	gsp = gp->softc;
87	gsl = &gsp->slices[pp->index];
88	for (u = 0; u < gsp->nslice; u++) {
89		gsl2 = &gsp->slices[u];
90		if (gsl2->length == 0)
91			continue;
92		if (u == pp->index)
93			continue;
94		if (gsl->offset + gsl->length <= gsl2->offset)
95			continue;
96		if (gsl2->offset + gsl2->length <= gsl->offset)
97			continue;
98		/* overlap */
99		pp2 = gsl2->provider;
100		if ((pp->acw + dw) > 0 && pp2->ace > 0)
101			return (EPERM);
102		if ((pp->ace + de) > 0 && pp2->acw > 0)
103			return (EPERM);
104	}
105	/* On first open, grab an extra "exclusive" bit */
106	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
107		de++;
108	/* ... and let go of it on last close */
109	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
110		de--;
111	error = g_access_rel(cp, dr, dw, de);
112	return (error);
113}
114
115/*
116 * XXX: It should be possible to specify here if we should finish all of the
117 * XXX: bio, or only the non-hot bits.  This would get messy if there were
118 * XXX: two hot spots in the same bio, so for now we simply finish off the
119 * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
120 * XXX: so making that considerably harder is not a bad idea anyway.
121 */
122void
123g_slice_finish_hot(struct bio *bp)
124{
125	struct bio *bp2;
126	struct g_geom *gp;
127	struct g_consumer *cp;
128	struct g_slicer *gsp;
129	struct g_slice *gsl;
130	int idx;
131
132	KASSERT(bp->bio_to != NULL,
133	    ("NULL bio_to in g_slice_finish_hot(%p)", bp));
134	KASSERT(bp->bio_from != NULL,
135	    ("NULL bio_from in g_slice_finish_hot(%p)", bp));
136	gp = bp->bio_to->geom;
137	gsp = gp->softc;
138	cp = LIST_FIRST(&gp->consumer);
139	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
140	idx = bp->bio_to->index;
141	gsl = &gsp->slices[idx];
142
143	bp2 = g_clone_bio(bp);
144	if (bp2 == NULL) {
145		g_io_deliver(bp, ENOMEM);
146		return;
147	}
148	if (bp2->bio_offset + bp2->bio_length > gsl->length)
149		bp2->bio_length = gsl->length - bp2->bio_offset;
150	bp2->bio_done = g_std_done;
151	bp2->bio_offset += gsl->offset;
152	g_io_request(bp2, cp);
153	return;
154}
155
156static void
157g_slice_start(struct bio *bp)
158{
159	struct bio *bp2;
160	struct g_provider *pp;
161	struct g_geom *gp;
162	struct g_consumer *cp;
163	struct g_slicer *gsp;
164	struct g_slice *gsl;
165	struct g_slice_hot *ghp;
166	int idx, error;
167	u_int m_index;
168	off_t t;
169
170	pp = bp->bio_to;
171	gp = pp->geom;
172	gsp = gp->softc;
173	cp = LIST_FIRST(&gp->consumer);
174	idx = pp->index;
175	gsl = &gsp->slices[idx];
176	switch(bp->bio_cmd) {
177	case BIO_READ:
178	case BIO_WRITE:
179	case BIO_DELETE:
180		if (bp->bio_offset > gsl->length) {
181			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
182			return;
183		}
184		/*
185		 * Check if we collide with any hot spaces, and call the
186		 * method once if so.
187		 */
188		t = bp->bio_offset + gsl->offset;
189		for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
190			ghp = &gsp->hotspot[m_index];
191			if (t >= ghp->offset + ghp->length)
192				continue;
193			if (t + bp->bio_length <= ghp->offset)
194				continue;
195			switch(bp->bio_cmd) {
196			case BIO_READ:		idx = ghp->ract; break;
197			case BIO_WRITE:		idx = ghp->wact; break;
198			case BIO_DELETE:	idx = ghp->dact; break;
199			}
200			switch(idx) {
201			case G_SLICE_HOT_ALLOW:
202				/* Fall out and continue normal processing */
203				continue;
204			case G_SLICE_HOT_DENY:
205				g_io_deliver(bp, EROFS);
206				return;
207			case G_SLICE_HOT_START:
208				error = gsp->start(bp);
209				if (error && error != EJUSTRETURN)
210					g_io_deliver(bp, error);
211				return;
212			case G_SLICE_HOT_CALL:
213				error = g_call_me(gsp->hot, bp, gp, NULL);
214				if (error)
215					g_io_deliver(bp, error);
216				return;
217			}
218			break;
219		}
220		bp2 = g_clone_bio(bp);
221		if (bp2 == NULL) {
222			g_io_deliver(bp, ENOMEM);
223			return;
224		}
225		if (bp2->bio_offset + bp2->bio_length > gsl->length)
226			bp2->bio_length = gsl->length - bp2->bio_offset;
227		bp2->bio_done = g_std_done;
228		bp2->bio_offset += gsl->offset;
229		g_io_request(bp2, cp);
230		return;
231	case BIO_GETATTR:
232		/* Give the real method a chance to override */
233		if (gsp->start != NULL && gsp->start(bp))
234			return;
235		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
236			struct g_kerneldump *gkd;
237
238			gkd = (struct g_kerneldump *)bp->bio_data;
239			gkd->offset += gsp->slices[idx].offset;
240			if (gkd->length > gsp->slices[idx].length)
241				gkd->length = gsp->slices[idx].length;
242			/* now, pass it on downwards... */
243		}
244		bp2 = g_clone_bio(bp);
245		if (bp2 == NULL) {
246			g_io_deliver(bp, ENOMEM);
247			return;
248		}
249		bp2->bio_done = g_std_done;
250		g_io_request(bp2, cp);
251		break;
252	default:
253		g_io_deliver(bp, EOPNOTSUPP);
254		return;
255	}
256}
257
258void
259g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
260{
261	struct g_slicer *gsp;
262
263	gsp = gp->softc;
264	if (indent == NULL) {
265		sbuf_printf(sb, " i %u", pp->index);
266		sbuf_printf(sb, " o %ju",
267		    (uintmax_t)gsp->slices[pp->index].offset);
268		return;
269	}
270	if (pp != NULL) {
271		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
272		sbuf_printf(sb, "%s<length>%ju</length>\n",
273		    indent, (uintmax_t)gsp->slices[pp->index].length);
274		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
275		    (uintmax_t)gsp->slices[pp->index].length / 512);
276		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
277		    (uintmax_t)gsp->slices[pp->index].offset);
278		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
279		    (uintmax_t)gsp->slices[pp->index].offset / 512);
280	}
281}
282
283int
284g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
285{
286	struct g_provider *pp, *pp2;
287	struct g_slicer *gsp;
288	struct g_slice *gsl;
289	va_list ap;
290	struct sbuf *sb;
291	int error, acc;
292
293	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
294	     gp->name, idx, how);
295	g_topology_assert();
296	gsp = gp->softc;
297	error = 0;
298	if (idx >= gsp->nslice)
299		return(EINVAL);
300	gsl = &gsp->slices[idx];
301	pp = gsl->provider;
302	if (pp != NULL)
303		acc = pp->acr + pp->acw + pp->ace;
304	else
305		acc = 0;
306	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
307		if (length < gsl->length)
308			return(EBUSY);
309		if (offset != gsl->offset)
310			return(EBUSY);
311	}
312	/* XXX: check offset + length <= MEDIASIZE */
313	if (how == G_SLICE_CONFIG_CHECK)
314		return (0);
315	gsl->length = length;
316	gsl->offset = offset;
317	gsl->sectorsize = sectorsize;
318	if (length == 0) {
319		if (pp == NULL)
320			return (0);
321		if (bootverbose)
322			printf("GEOM: Deconfigure %s\n", pp->name);
323		g_orphan_provider(pp, ENXIO);
324		gsl->provider = NULL;
325		gsp->nprovider--;
326		return (0);
327	}
328	if (pp != NULL) {
329		if (bootverbose)
330			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
331			    pp->name, (intmax_t)offset, (intmax_t)length,
332			    (intmax_t)(offset + length - 1));
333		pp->mediasize = gsl->length;
334		return (0);
335	}
336	va_start(ap, fmt);
337	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
338	sbuf_vprintf(sb, fmt, ap);
339	sbuf_finish(sb);
340	pp = g_new_providerf(gp, sbuf_data(sb));
341	pp2 = LIST_FIRST(&gp->consumer)->provider;
342	pp->flags = pp2->flags & G_PF_CANDELETE;
343	if (pp2->stripesize > 0) {
344		pp->stripesize = pp2->stripesize;
345		pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
346	}
347	if (bootverbose)
348		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
349		    pp->name, (intmax_t)offset, (intmax_t)length,
350		    (intmax_t)(offset + length - 1));
351	pp->index = idx;
352	pp->mediasize = gsl->length;
353	pp->sectorsize = gsl->sectorsize;
354	gsl->provider = pp;
355	gsp->nprovider++;
356	g_error_provider(pp, 0);
357	sbuf_delete(sb);
358	return(0);
359}
360
361/*
362 * Configure "hotspots".  A hotspot is a piece of the parent device which
363 * this particular slicer cares about for some reason.  Typically because
364 * it contains meta-data used to configure the slicer.
365 * A hotspot is identified by its index number. The offset and length are
366 * relative to the parent device, and the three "?act" fields specify
367 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
368 *
369 * XXX: There may be a race relative to g_slice_start() here, if an existing
370 * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
371 * XXX: we can protect the hotspot stuff with a mutex.
372 */
373
374int
375g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
376{
377	struct g_slicer *gsp;
378	struct g_slice_hot *gsl, *gsl2;
379
380	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
381	    gp->name, idx, (intmax_t)offset, (intmax_t)length);
382	g_topology_assert();
383	gsp = gp->softc;
384	gsl = gsp->hotspot;
385	if(idx >= gsp->nhotspot) {
386		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
387		if (gsp->hotspot != NULL)
388			bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
389		gsp->hotspot = gsl2;
390		if (gsp->hotspot != NULL)
391			g_free(gsl);
392		gsl = gsl2;
393		gsp->nhotspot = idx + 1;
394	}
395	gsl[idx].offset = offset;
396	gsl[idx].length = length;
397	KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
398	    || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
399	/* XXX: check that we _have_ a start function if HOT_START specified */
400	gsl[idx].ract = ract;
401	gsl[idx].dact = dact;
402	gsl[idx].wact = wact;
403	return (0);
404}
405
406struct g_geom *
407g_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)
408{
409	struct g_geom *gp;
410	struct g_slicer *gsp;
411	struct g_consumer *cp;
412	void **vp;
413	int error;
414
415	g_topology_assert();
416	vp = (void **)extrap;
417	gp = g_new_geomf(mp, "%s", pp->name);
418	gsp = g_slice_init(slices, extra);
419	gsp->start = start;
420	gp->access = g_slice_access;
421	gp->orphan = g_slice_orphan;
422	gp->softc = gsp;
423	gp->start = g_slice_start;
424	gp->spoiled = g_std_spoiled;
425	gp->dumpconf = g_slice_dumpconf;
426	cp = g_new_consumer(gp);
427	error = g_attach(cp, pp);
428	if (error == 0)
429		error = g_access_rel(cp, 1, 0, 0);
430	if (error) {
431		if (cp->provider != NULL)
432			g_detach(cp);
433		g_destroy_consumer(cp);
434		g_free(gsp->slices);
435		g_free(gp->softc);
436		g_destroy_geom(gp);
437		return (NULL);
438	}
439	*vp = gsp->softc;
440	*cpp = cp;
441	return (gp);
442}
443
444static void
445g_slice_orphan(struct g_consumer *cp)
446{
447	struct g_geom *gp;
448	struct g_provider *pp;
449	int error;
450
451	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
452	g_topology_assert();
453	KASSERT(cp->provider->error != 0,
454	    ("g_slice_orphan with error == 0"));
455
456	gp = cp->geom;
457	/* XXX: Not good enough we leak the softc and its suballocations */
458	gp->flags |= G_GEOM_WITHER;
459	error = cp->provider->error;
460	LIST_FOREACH(pp, &gp->provider, provider)
461		g_orphan_provider(pp, error);
462	return;
463}
464