2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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
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
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
47 #include <sys/mutex.h>
48 #include <sys/errno.h>
50 #include <geom/geom.h>
51 #include <geom/geom_slice.h>
52 #include <machine/stdarg.h>
54 static g_access_t g_slice_access;
55 static g_start_t g_slice_start;
57 static struct g_slicer *
58 g_slice_alloc(unsigned nslice, unsigned scsize)
62 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
64 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
67 gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
74 g_slice_free(struct g_geom *gp)
82 * We can get multiple spoiled events before wither-washer
83 * detaches our consumer, so this can get called multiple
95 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
100 struct g_consumer *cp;
101 struct g_provider *pp2;
102 struct g_slicer *gsp;
103 struct g_slice *gsl, *gsl2;
106 cp = LIST_FIRST(&gp->consumer);
107 KASSERT (cp != NULL, ("g_slice_access but no consumer"));
109 if (dr > 0 || dw > 0 || de > 0) {
110 gsl = &gsp->slices[pp->index];
111 for (u = 0; u < gsp->nslice; u++) {
112 gsl2 = &gsp->slices[u];
113 if (gsl2->length == 0)
117 if (gsl->offset + gsl->length <= gsl2->offset)
119 if (gsl2->offset + gsl2->length <= gsl->offset)
122 pp2 = gsl2->provider;
123 if ((pp->acw + dw) > 0 && pp2->ace > 0)
125 if ((pp->ace + de) > 0 && pp2->acw > 0)
129 /* On first open, grab an extra "exclusive" bit */
130 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
132 /* ... and let go of it on last close */
133 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
135 error = g_access(cp, dr, dw, de);
138 * Free the softc if all providers have been closed and this geom
141 if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 &&
142 (cp->acr + cp->acw + cp->ace) == 0)
149 * XXX: It should be possible to specify here if we should finish all of the
150 * XXX: bio, or only the non-hot bits. This would get messy if there were
151 * XXX: two hot spots in the same bio, so for now we simply finish off the
152 * XXX: entire bio. Modifying hot data on the way to disk is frowned on
153 * XXX: so making that considerably harder is not a bad idea anyway.
156 g_slice_finish_hot(struct bio *bp)
160 struct g_consumer *cp;
161 struct g_slicer *gsp;
165 KASSERT(bp->bio_to != NULL,
166 ("NULL bio_to in g_slice_finish_hot(%p)", bp));
167 KASSERT(bp->bio_from != NULL,
168 ("NULL bio_from in g_slice_finish_hot(%p)", bp));
169 gp = bp->bio_to->geom;
171 cp = LIST_FIRST(&gp->consumer);
172 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
173 idx = bp->bio_to->index;
174 gsl = &gsp->slices[idx];
176 bp2 = g_clone_bio(bp);
178 g_io_deliver(bp, ENOMEM);
181 if (bp2->bio_offset + bp2->bio_length > gsl->length)
182 bp2->bio_length = gsl->length - bp2->bio_offset;
183 bp2->bio_done = g_std_done;
184 bp2->bio_offset += gsl->offset;
185 g_io_request(bp2, cp);
190 g_slice_done(struct bio *bp)
193 KASSERT(bp->bio_cmd == BIO_GETATTR &&
194 strcmp(bp->bio_attribute, "GEOM::ident") == 0,
195 ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute));
197 if (bp->bio_error == 0 && bp->bio_data[0] != '\0') {
200 /* Add index to the ident received. */
201 snprintf(idx, sizeof(idx), "s%d",
202 bp->bio_parent->bio_to->index);
203 if (strlcat(bp->bio_data, idx, bp->bio_length) >=
205 bp->bio_error = EFAULT;
212 g_slice_start(struct bio *bp)
215 struct g_provider *pp;
217 struct g_consumer *cp;
218 struct g_slicer *gsp;
220 struct g_slice_hot *ghp;
228 cp = LIST_FIRST(&gp->consumer);
230 gsl = &gsp->slices[idx];
231 switch(bp->bio_cmd) {
235 if (bp->bio_offset > gsl->length) {
236 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
240 * Check if we collide with any hot spaces, and call the
243 t = bp->bio_offset + gsl->offset;
244 for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
245 ghp = &gsp->hotspot[m_index];
246 if (t >= ghp->offset + ghp->length)
248 if (t + bp->bio_length <= ghp->offset)
250 switch(bp->bio_cmd) {
251 case BIO_READ: idx = ghp->ract; break;
252 case BIO_WRITE: idx = ghp->wact; break;
253 case BIO_DELETE: idx = ghp->dact; break;
256 case G_SLICE_HOT_ALLOW:
257 /* Fall out and continue normal processing */
259 case G_SLICE_HOT_DENY:
260 g_io_deliver(bp, EROFS);
262 case G_SLICE_HOT_START:
263 error = gsp->start(bp);
264 if (error && error != EJUSTRETURN)
265 g_io_deliver(bp, error);
267 case G_SLICE_HOT_CALL:
268 error = g_post_event(gsp->hot, bp, M_NOWAIT,
271 g_io_deliver(bp, error);
276 bp2 = g_clone_bio(bp);
278 g_io_deliver(bp, ENOMEM);
281 if (bp2->bio_offset + bp2->bio_length > gsl->length)
282 bp2->bio_length = gsl->length - bp2->bio_offset;
283 bp2->bio_done = g_std_done;
284 bp2->bio_offset += gsl->offset;
285 g_io_request(bp2, cp);
288 /* Give the real method a chance to override */
289 if (gsp->start != NULL && gsp->start(bp))
291 if (!strcmp("GEOM::ident", bp->bio_attribute)) {
292 bp2 = g_clone_bio(bp);
294 g_io_deliver(bp, ENOMEM);
297 bp2->bio_done = g_slice_done;
298 g_io_request(bp2, cp);
301 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
302 struct g_kerneldump *gkd;
304 gkd = (struct g_kerneldump *)bp->bio_data;
305 gkd->offset += gsp->slices[idx].offset;
306 if (gkd->length > gsp->slices[idx].length)
307 gkd->length = gsp->slices[idx].length;
308 /* now, pass it on downwards... */
313 bp2 = g_clone_bio(bp);
315 g_io_deliver(bp, ENOMEM);
318 bp2->bio_done = g_std_done;
319 g_io_request(bp2, cp);
322 g_io_deliver(bp, EOPNOTSUPP);
328 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
330 struct g_slicer *gsp;
333 if (indent == NULL) {
334 sbuf_printf(sb, " i %u", pp->index);
335 sbuf_printf(sb, " o %ju",
336 (uintmax_t)gsp->slices[pp->index].offset);
340 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
341 sbuf_printf(sb, "%s<length>%ju</length>\n",
342 indent, (uintmax_t)gsp->slices[pp->index].length);
343 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
344 (uintmax_t)gsp->slices[pp->index].length / 512);
345 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
346 (uintmax_t)gsp->slices[pp->index].offset);
347 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
348 (uintmax_t)gsp->slices[pp->index].offset / 512);
353 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
355 struct g_provider *pp, *pp2;
356 struct g_slicer *gsp;
362 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
366 if (idx >= gsp->nslice)
368 gsl = &gsp->slices[idx];
371 acc = pp->acr + pp->acw + pp->ace;
374 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
375 if (length < gsl->length)
377 if (offset != gsl->offset)
380 /* XXX: check offset + length <= MEDIASIZE */
381 if (how == G_SLICE_CONFIG_CHECK)
383 gsl->length = length;
384 gsl->offset = offset;
385 gsl->sectorsize = sectorsize;
390 printf("GEOM: Deconfigure %s\n", pp->name);
391 g_wither_provider(pp, ENXIO);
392 gsl->provider = NULL;
398 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
399 pp->name, (intmax_t)offset, (intmax_t)length,
400 (intmax_t)(offset + length - 1));
401 g_resize_provider(pp, gsl->length);
404 sb = sbuf_new_auto();
406 sbuf_vprintf(sb, fmt, ap);
409 pp = g_new_providerf(gp, "%s", sbuf_data(sb));
410 pp2 = LIST_FIRST(&gp->consumer)->provider;
411 pp->stripesize = pp2->stripesize;
412 pp->stripeoffset = pp2->stripeoffset + offset;
413 if (pp->stripesize > 0)
414 pp->stripeoffset %= pp->stripesize;
415 if (gsp->nhotspot == 0) {
416 pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED;
417 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
419 if (0 && bootverbose)
420 printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
421 pp->name, (intmax_t)offset, (intmax_t)length,
422 (intmax_t)(offset + length - 1));
424 pp->mediasize = gsl->length;
425 pp->sectorsize = gsl->sectorsize;
428 g_error_provider(pp, 0);
434 * Configure "hotspots". A hotspot is a piece of the parent device which
435 * this particular slicer cares about for some reason. Typically because
436 * it contains meta-data used to configure the slicer.
437 * A hotspot is identified by its index number. The offset and length are
438 * relative to the parent device, and the three "?act" fields specify
439 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
441 * XXX: There may be a race relative to g_slice_start() here, if an existing
442 * XXX: hotspot is changed wile I/O is happening. Should this become a problem
443 * XXX: we can protect the hotspot stuff with a mutex.
447 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
449 struct g_slicer *gsp;
450 struct g_slice_hot *gsl, *gsl2;
451 struct g_consumer *cp;
452 struct g_provider *pp;
454 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
455 gp->name, idx, (intmax_t)offset, (intmax_t)length);
458 /* Deny unmapped I/O and direct dispatch if hotspots are used. */
459 if (gsp->nhotspot == 0) {
460 LIST_FOREACH(pp, &gp->provider, provider)
461 pp->flags &= ~(G_PF_ACCEPT_UNMAPPED |
462 G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE);
463 LIST_FOREACH(cp, &gp->consumer, consumer)
464 cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE);
467 if(idx >= gsp->nhotspot) {
468 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
469 if (gsp->hotspot != NULL)
470 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
472 if (gsp->hotspot != NULL)
475 gsp->nhotspot = idx + 1;
477 gsl[idx].offset = offset;
478 gsl[idx].length = length;
479 KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
480 || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
481 /* XXX: check that we _have_ a start function if HOT_START specified */
482 gsl[idx].ract = ract;
483 gsl[idx].dact = dact;
484 gsl[idx].wact = wact;
489 g_slice_orphan(struct g_consumer *cp)
495 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name);
496 g_wither_geom(gp, ENXIO);
499 * We can safely free the softc now if there are no accesses,
500 * otherwise g_slice_access() will do that after the last close.
502 if ((cp->acr + cp->acw + cp->ace) == 0)
507 g_slice_spoiled(struct g_consumer *cp)
510 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name);
511 cp->flags |= G_CF_ORPHAN;
516 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
519 g_slice_spoiled(LIST_FIRST(&gp->consumer));
524 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 struct g_slicer *gsp;
528 struct g_consumer *cp;
533 vp = (void **)extrap;
534 gp = g_new_geomf(mp, "%s", pp->name);
535 gsp = g_slice_alloc(slices, extra);
538 gp->start = g_slice_start;
539 gp->access = g_slice_access;
540 gp->orphan = g_slice_orphan;
541 gp->spoiled = g_slice_spoiled;
542 if (gp->dumpconf == NULL)
543 gp->dumpconf = g_slice_dumpconf;
544 if (gp->class->destroy_geom == NULL)
545 gp->class->destroy_geom = g_slice_destroy_geom;
546 cp = g_new_consumer(gp);
547 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
548 error = g_attach(cp, pp);
550 error = g_access(cp, 1, 0, 0);
552 g_wither_geom(gp, ENXIO);