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