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