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