xref: /freebsd/sys/geom/geom_slice.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/bio.h>
46 #include <sys/sysctl.h>
47 #include <sys/proc.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/errno.h>
51 #include <sys/sbuf.h>
52 #include <geom/geom.h>
53 #include <geom/geom_slice.h>
54 #include <machine/stdarg.h>
55 
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 	if (scsize > 0)
66 		gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
67 	else
68 		gsp->softc = NULL;
69 	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
70 	    M_WAITOK | M_ZERO);
71 	gsp->nslice = nslice;
72 	return (gsp);
73 }
74 
75 static void
76 g_slice_free(struct g_geom *gp)
77 {
78 	struct g_slicer *gsp;
79 
80 	gsp = gp->softc;
81 	gp->softc = NULL;
82 
83 	/*
84 	 * We can get multiple spoiled events before wither-washer
85 	 * detaches our consumer, so this can get called multiple
86 	 * times.
87 	 */
88 	if (gsp == NULL)
89 		return;
90 	g_free(gsp->slices);
91 	g_free(gsp->hotspot);
92 	g_free(gsp->softc);
93 	g_free(gsp);
94 }
95 
96 static int
97 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
98 {
99 	int error;
100 	u_int u;
101 	struct g_geom *gp;
102 	struct g_consumer *cp;
103 	struct g_provider *pp2;
104 	struct g_slicer *gsp;
105 	struct g_slice *gsl, *gsl2;
106 
107 	gp = pp->geom;
108 	cp = LIST_FIRST(&gp->consumer);
109 	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
110 	gsp = gp->softc;
111 	if (dr > 0 || dw > 0 || de > 0) {
112 		gsl = &gsp->slices[pp->index];
113 		for (u = 0; u < gsp->nslice; u++) {
114 			gsl2 = &gsp->slices[u];
115 			if (gsl2->length == 0)
116 				continue;
117 			if (u == pp->index)
118 				continue;
119 			if (gsl->offset + gsl->length <= gsl2->offset)
120 				continue;
121 			if (gsl2->offset + gsl2->length <= gsl->offset)
122 				continue;
123 			/* overlap */
124 			pp2 = gsl2->provider;
125 			if ((pp->acw + dw) > 0 && pp2->ace > 0)
126 				return (EPERM);
127 			if ((pp->ace + de) > 0 && pp2->acw > 0)
128 				return (EPERM);
129 		}
130 	}
131 	/* On first open, grab an extra "exclusive" bit */
132 	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
133 		de++;
134 	/* ... and let go of it on last close */
135 	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
136 		de--;
137 	error = g_access(cp, dr, dw, de);
138 
139 	/*
140 	 * Free the softc if all providers have been closed and this geom
141 	 * is being removed.
142 	 */
143 	if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 &&
144 	    (cp->acr + cp->acw + cp->ace) == 0)
145 		g_slice_free(gp);
146 
147 	return (error);
148 }
149 
150 /*
151  * XXX: It should be possible to specify here if we should finish all of the
152  * XXX: bio, or only the non-hot bits.  This would get messy if there were
153  * XXX: two hot spots in the same bio, so for now we simply finish off the
154  * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
155  * XXX: so making that considerably harder is not a bad idea anyway.
156  */
157 void
158 g_slice_finish_hot(struct bio *bp)
159 {
160 	struct bio *bp2;
161 	struct g_geom *gp;
162 	struct g_consumer *cp;
163 	struct g_slicer *gsp;
164 	struct g_slice *gsl;
165 	int idx;
166 
167 	KASSERT(bp->bio_to != NULL,
168 	    ("NULL bio_to in g_slice_finish_hot(%p)", bp));
169 	KASSERT(bp->bio_from != NULL,
170 	    ("NULL bio_from in g_slice_finish_hot(%p)", bp));
171 	gp = bp->bio_to->geom;
172 	gsp = gp->softc;
173 	cp = LIST_FIRST(&gp->consumer);
174 	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
175 	idx = bp->bio_to->index;
176 	gsl = &gsp->slices[idx];
177 
178 	bp2 = g_clone_bio(bp);
179 	if (bp2 == NULL) {
180 		g_io_deliver(bp, ENOMEM);
181 		return;
182 	}
183 	if (bp2->bio_offset + bp2->bio_length > gsl->length)
184 		bp2->bio_length = gsl->length - bp2->bio_offset;
185 	bp2->bio_done = g_std_done;
186 	bp2->bio_offset += gsl->offset;
187 	g_io_request(bp2, cp);
188 	return;
189 }
190 
191 static void
192 g_slice_done(struct bio *bp)
193 {
194 
195 	KASSERT(bp->bio_cmd == BIO_GETATTR &&
196 	    strcmp(bp->bio_attribute, "GEOM::ident") == 0,
197 	    ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute));
198 
199 	if (bp->bio_error == 0 && bp->bio_data[0] != '\0') {
200 		char idx[8];
201 
202 		/* Add index to the ident received. */
203 		snprintf(idx, sizeof(idx), "s%d",
204 		    bp->bio_parent->bio_to->index);
205 		if (strlcat(bp->bio_data, idx, bp->bio_length) >=
206 		    bp->bio_length) {
207 			bp->bio_error = EFAULT;
208 		}
209 	}
210 	g_std_done(bp);
211 }
212 
213 static void
214 g_slice_start(struct bio *bp)
215 {
216 	struct bio *bp2;
217 	struct g_provider *pp;
218 	struct g_geom *gp;
219 	struct g_consumer *cp;
220 	struct g_slicer *gsp;
221 	struct g_slice *gsl;
222 	struct g_slice_hot *ghp;
223 	int idx, error;
224 	u_int m_index;
225 	off_t t;
226 
227 	pp = bp->bio_to;
228 	gp = pp->geom;
229 	gsp = gp->softc;
230 	cp = LIST_FIRST(&gp->consumer);
231 	idx = pp->index;
232 	gsl = &gsp->slices[idx];
233 	switch(bp->bio_cmd) {
234 	case BIO_READ:
235 	case BIO_WRITE:
236 	case BIO_DELETE:
237 		if (bp->bio_offset > gsl->length) {
238 			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
239 			return;
240 		}
241 		/*
242 		 * Check if we collide with any hot spaces, and call the
243 		 * method once if so.
244 		 */
245 		t = bp->bio_offset + gsl->offset;
246 		for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
247 			ghp = &gsp->hotspot[m_index];
248 			if (t >= ghp->offset + ghp->length)
249 				continue;
250 			if (t + bp->bio_length <= ghp->offset)
251 				continue;
252 			switch(bp->bio_cmd) {
253 			case BIO_READ:		idx = ghp->ract; break;
254 			case BIO_WRITE:		idx = ghp->wact; break;
255 			case BIO_DELETE:	idx = ghp->dact; break;
256 			}
257 			switch(idx) {
258 			case G_SLICE_HOT_ALLOW:
259 				/* Fall out and continue normal processing */
260 				continue;
261 			case G_SLICE_HOT_DENY:
262 				g_io_deliver(bp, EROFS);
263 				return;
264 			case G_SLICE_HOT_START:
265 				error = gsp->start(bp);
266 				if (error && error != EJUSTRETURN)
267 					g_io_deliver(bp, error);
268 				return;
269 			case G_SLICE_HOT_CALL:
270 				error = g_post_event(gsp->hot, bp, M_NOWAIT,
271 				    gp, NULL);
272 				if (error)
273 					g_io_deliver(bp, error);
274 				return;
275 			}
276 			break;
277 		}
278 		bp2 = g_clone_bio(bp);
279 		if (bp2 == NULL) {
280 			g_io_deliver(bp, ENOMEM);
281 			return;
282 		}
283 		if (bp2->bio_offset + bp2->bio_length > gsl->length)
284 			bp2->bio_length = gsl->length - bp2->bio_offset;
285 		bp2->bio_done = g_std_done;
286 		bp2->bio_offset += gsl->offset;
287 		g_io_request(bp2, cp);
288 		return;
289 	case BIO_GETATTR:
290 		/* Give the real method a chance to override */
291 		if (gsp->start != NULL && gsp->start(bp))
292 			return;
293 		if (!strcmp("GEOM::ident", bp->bio_attribute)) {
294 			bp2 = g_clone_bio(bp);
295 			if (bp2 == NULL) {
296 				g_io_deliver(bp, ENOMEM);
297 				return;
298 			}
299 			bp2->bio_done = g_slice_done;
300 			g_io_request(bp2, cp);
301 			return;
302 		}
303 		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
304 			struct g_kerneldump *gkd;
305 
306 			gkd = (struct g_kerneldump *)bp->bio_data;
307 			gkd->offset += gsp->slices[idx].offset;
308 			if (gkd->length > gsp->slices[idx].length)
309 				gkd->length = gsp->slices[idx].length;
310 			/* now, pass it on downwards... */
311 		}
312 		/* FALLTHROUGH */
313 	case BIO_SPEEDUP:
314 	case BIO_FLUSH:
315 		bp2 = g_clone_bio(bp);
316 		if (bp2 == NULL) {
317 			g_io_deliver(bp, ENOMEM);
318 			return;
319 		}
320 		bp2->bio_done = g_std_done;
321 		g_io_request(bp2, cp);
322 		break;
323 	default:
324 		g_io_deliver(bp, EOPNOTSUPP);
325 		return;
326 	}
327 }
328 
329 void
330 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
331 {
332 	struct g_slicer *gsp;
333 
334 	gsp = gp->softc;
335 	if (indent == NULL) {
336 		sbuf_printf(sb, " i %u", pp->index);
337 		sbuf_printf(sb, " o %ju",
338 		    (uintmax_t)gsp->slices[pp->index].offset);
339 		return;
340 	}
341 	if (pp != NULL) {
342 		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
343 		sbuf_printf(sb, "%s<length>%ju</length>\n",
344 		    indent, (uintmax_t)gsp->slices[pp->index].length);
345 		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
346 		    (uintmax_t)gsp->slices[pp->index].length / 512);
347 		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
348 		    (uintmax_t)gsp->slices[pp->index].offset);
349 		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
350 		    (uintmax_t)gsp->slices[pp->index].offset / 512);
351 	}
352 }
353 
354 int
355 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
356 {
357 	struct g_provider *pp, *pp2;
358 	struct g_slicer *gsp;
359 	struct g_slice *gsl;
360 	va_list ap;
361 	struct sbuf *sb;
362 	int acc;
363 
364 	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
365 	     gp->name, idx, how);
366 	g_topology_assert();
367 	gsp = gp->softc;
368 	if (idx >= gsp->nslice)
369 		return(EINVAL);
370 	gsl = &gsp->slices[idx];
371 	pp = gsl->provider;
372 	if (pp != NULL)
373 		acc = pp->acr + pp->acw + pp->ace;
374 	else
375 		acc = 0;
376 	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
377 		if (length < gsl->length)
378 			return(EBUSY);
379 		if (offset != gsl->offset)
380 			return(EBUSY);
381 	}
382 	/* XXX: check offset + length <= MEDIASIZE */
383 	if (how == G_SLICE_CONFIG_CHECK)
384 		return (0);
385 	gsl->length = length;
386 	gsl->offset = offset;
387 	gsl->sectorsize = sectorsize;
388 	if (length == 0) {
389 		if (pp == NULL)
390 			return (0);
391 		if (bootverbose)
392 			printf("GEOM: Deconfigure %s\n", pp->name);
393 		g_wither_provider(pp, ENXIO);
394 		gsl->provider = NULL;
395 		gsp->nprovider--;
396 		return (0);
397 	}
398 	if (pp != NULL) {
399 		if (bootverbose)
400 			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
401 			    pp->name, (intmax_t)offset, (intmax_t)length,
402 			    (intmax_t)(offset + length - 1));
403 		g_resize_provider(pp, gsl->length);
404 		return (0);
405 	}
406 	sb = sbuf_new_auto();
407 	va_start(ap, fmt);
408 	sbuf_vprintf(sb, fmt, ap);
409 	va_end(ap);
410 	sbuf_finish(sb);
411 	pp = g_new_providerf(gp, "%s", sbuf_data(sb));
412 	pp2 = LIST_FIRST(&gp->consumer)->provider;
413 	pp->stripesize = pp2->stripesize;
414 	pp->stripeoffset = pp2->stripeoffset + offset;
415 	if (pp->stripesize > 0)
416 		pp->stripeoffset %= pp->stripesize;
417 	if (gsp->nhotspot == 0) {
418 		pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED;
419 		pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
420 	}
421 	if (0 && bootverbose)
422 		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
423 		    pp->name, (intmax_t)offset, (intmax_t)length,
424 		    (intmax_t)(offset + length - 1));
425 	pp->index = idx;
426 	pp->mediasize = gsl->length;
427 	pp->sectorsize = gsl->sectorsize;
428 	gsl->provider = pp;
429 	gsp->nprovider++;
430 	g_error_provider(pp, 0);
431 	sbuf_delete(sb);
432 	return(0);
433 }
434 
435 /*
436  * Configure "hotspots".  A hotspot is a piece of the parent device which
437  * this particular slicer cares about for some reason.  Typically because
438  * it contains meta-data used to configure the slicer.
439  * A hotspot is identified by its index number. The offset and length are
440  * relative to the parent device, and the three "?act" fields specify
441  * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
442  *
443  * XXX: There may be a race relative to g_slice_start() here, if an existing
444  * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
445  * XXX: we can protect the hotspot stuff with a mutex.
446  */
447 
448 int
449 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
450 {
451 	struct g_slicer *gsp;
452 	struct g_slice_hot *gsl, *gsl2;
453 	struct g_consumer *cp;
454 	struct g_provider *pp;
455 
456 	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
457 	    gp->name, idx, (intmax_t)offset, (intmax_t)length);
458 	g_topology_assert();
459 	gsp = gp->softc;
460 	/* Deny unmapped I/O and direct dispatch if hotspots are used. */
461 	if (gsp->nhotspot == 0) {
462 		LIST_FOREACH(pp, &gp->provider, provider)
463 			pp->flags &= ~(G_PF_ACCEPT_UNMAPPED |
464 			    G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE);
465 		LIST_FOREACH(cp, &gp->consumer, consumer)
466 			cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE);
467 	}
468 	gsl = gsp->hotspot;
469 	if(idx >= gsp->nhotspot) {
470 		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
471 		if (gsp->hotspot != NULL)
472 			bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
473 		gsp->hotspot = gsl2;
474 		if (gsp->hotspot != NULL)
475 			g_free(gsl);
476 		gsl = gsl2;
477 		gsp->nhotspot = idx + 1;
478 	}
479 	gsl[idx].offset = offset;
480 	gsl[idx].length = length;
481 	KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
482 	    || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
483 	/* XXX: check that we _have_ a start function if HOT_START specified */
484 	gsl[idx].ract = ract;
485 	gsl[idx].dact = dact;
486 	gsl[idx].wact = wact;
487 	return (0);
488 }
489 
490 void
491 g_slice_orphan(struct g_consumer *cp)
492 {
493 	struct g_geom *gp;
494 
495 	g_topology_assert();
496 	gp = cp->geom;
497 	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
498 	g_wither_geom(gp, ENXIO);
499 
500 	/*
501 	 * We can safely free the softc now if there are no accesses,
502 	 * otherwise g_slice_access() will do that after the last close.
503 	 */
504 	if ((cp->acr + cp->acw + cp->ace) == 0)
505 		g_slice_free(gp);
506 }
507 
508 void
509 g_slice_spoiled(struct g_consumer *cp)
510 {
511 
512 	g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name);
513 	cp->flags |= G_CF_ORPHAN;
514 	g_slice_orphan(cp);
515 }
516 
517 int
518 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
519 {
520 
521 	g_slice_spoiled(LIST_FIRST(&gp->consumer));
522 	return (0);
523 }
524 
525 struct g_geom *
526 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)
527 {
528 	struct g_geom *gp;
529 	struct g_slicer *gsp;
530 	struct g_consumer *cp;
531 	void **vp;
532 	int error;
533 
534 	g_topology_assert();
535 	vp = (void **)extrap;
536 	gp = g_new_geomf(mp, "%s", pp->name);
537 	gsp = g_slice_alloc(slices, extra);
538 	gsp->start = start;
539 	gp->softc = gsp;
540 	gp->start = g_slice_start;
541 	gp->access = g_slice_access;
542 	gp->orphan = g_slice_orphan;
543 	gp->spoiled = g_slice_spoiled;
544 	if (gp->dumpconf == NULL)
545 		gp->dumpconf = g_slice_dumpconf;
546 	if (gp->class->destroy_geom == NULL)
547 		gp->class->destroy_geom = g_slice_destroy_geom;
548 	cp = g_new_consumer(gp);
549 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
550 	error = g_attach(cp, pp);
551 	if (error == 0)
552 		error = g_access(cp, 1, 0, 0);
553 	if (error) {
554 		g_wither_geom(gp, ENXIO);
555 		return (NULL);
556 	}
557 	if (extrap != NULL)
558 		*vp = gsp->softc;
559 	*cpp = cp;
560 	return (gp);
561 }
562