2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* This is a GEOM module for handling path selection for multi-path
33 * storage devices. It is named "fox" because it, like they, prefer
34 * to have multiple exits to choose from.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/libkern.h>
48 #include <sys/endian.h>
50 #include <sys/errno.h>
51 #include <geom/geom.h>
53 #define FOX_CLASS_NAME "FOX"
54 #define FOX_MAGIC "GEOM::FOX"
56 FEATURE(geom_fox, "GEOM FOX redundant path mitigation support");
61 TAILQ_HEAD(, bio) queue;
64 struct g_consumer *path;
65 struct g_consumer *opath;
71 * This function is called whenever we need to select a new path.
74 g_fox_select_path(void *arg, int flag)
77 struct g_fox_softc *sc;
78 struct g_consumer *cp1;
83 if (flag == EV_CANCEL)
88 if (sc->opath != NULL) {
90 * First, close the old path entirely.
92 printf("Closing old path (%s) on fox (%s)\n",
93 sc->opath->provider->name, gp->name);
95 cp1 = LIST_NEXT(sc->opath, consumer);
97 g_access(sc->opath, -sc->cr, -sc->cw, -(sc->ce + 1));
100 * The attempt to reopen it with a exclusive count
102 error = g_access(sc->opath, 0, 0, 1);
105 * Ok, ditch this consumer, we can't use it.
107 printf("Drop old path (%s) on fox (%s)\n",
108 sc->opath->provider->name, gp->name);
110 g_destroy_consumer(sc->opath);
111 if (LIST_EMPTY(&gp->consumer)) {
112 /* No consumers left */
113 g_wither_geom(gp, ENXIO);
115 bp = TAILQ_FIRST(&sc->queue);
118 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
119 bp->bio_error = ENXIO;
125 printf("Got e-bit on old path (%s) on fox (%s)\n",
126 sc->opath->provider->name, gp->name);
130 cp1 = LIST_FIRST(&gp->consumer);
133 cp1 = LIST_FIRST(&gp->consumer);
134 printf("Open new path (%s) on fox (%s)\n",
135 cp1->provider->name, gp->name);
136 error = g_access(cp1, sc->cr, sc->cw, sc->ce);
139 * If we failed, we take another trip through here
141 printf("Open new path (%s) on fox (%s) failed, reselect.\n",
142 cp1->provider->name, gp->name);
144 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
146 printf("Open new path (%s) on fox (%s) succeeded\n",
147 cp1->provider->name, gp->name);
152 bp = TAILQ_FIRST(&sc->queue);
155 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
156 g_io_request(bp, sc->path);
158 mtx_unlock(&sc->lock);
163 g_fox_orphan(struct g_consumer *cp)
166 struct g_fox_softc *sc;
172 printf("Removing path (%s) from fox (%s)\n",
173 cp->provider->name, gp->name);
175 if (cp == sc->path) {
183 mtx_unlock(&sc->lock);
185 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
186 error = cp->provider->error;
188 g_destroy_consumer(cp);
189 if (!LIST_EMPTY(&gp->consumer)) {
191 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
195 mtx_destroy(&sc->lock);
198 g_wither_geom(gp, ENXIO);
202 g_fox_done(struct bio *bp)
205 struct g_fox_softc *sc;
208 if (bp->bio_error == 0) {
212 gp = bp->bio_from->geom;
214 if (bp->bio_from != sc->path) {
215 g_io_request(bp, sc->path);
219 sc->opath = sc->path;
221 error = g_post_event(g_fox_select_path, gp, M_NOWAIT, gp, NULL);
223 bp->bio_error = ENOMEM;
227 TAILQ_INSERT_TAIL(&sc->queue, bp, bio_queue);
229 mtx_unlock(&sc->lock);
233 g_fox_start(struct bio *bp)
237 struct g_fox_softc *sc;
240 gp = bp->bio_to->geom;
243 g_io_deliver(bp, ENXIO);
246 switch(bp->bio_cmd) {
250 bp2 = g_clone_bio(bp);
252 g_io_deliver(bp, ENOMEM);
255 bp2->bio_offset += sc->sectorsize;
256 bp2->bio_done = g_fox_done;
258 if (sc->path == NULL || !TAILQ_EMPTY(&sc->queue)) {
259 if (sc->waiting == 0) {
260 error = g_post_event(g_fox_select_path, gp,
265 g_io_deliver(bp, error);
271 TAILQ_INSERT_TAIL(&sc->queue, bp2,
274 g_io_request(bp2, sc->path);
276 mtx_unlock(&sc->lock);
279 g_io_deliver(bp, EOPNOTSUPP);
286 g_fox_access(struct g_provider *pp, int dr, int dw, int de)
289 struct g_fox_softc *sc;
290 struct g_consumer *cp1;
297 if (dr <= 0 && dw <= 0 && de <= 0)
303 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
305 * First open, open all consumers with an exclusive bit
308 LIST_FOREACH(cp1, &gp->consumer, consumer) {
309 error = g_access(cp1, 0, 0, 1);
311 printf("FOX: access(%s,0,0,1) = %d\n",
312 cp1->provider->name, error);
317 LIST_FOREACH(cp1, &gp->consumer, consumer) {
319 g_access(cp1, 0, 0, -1);
324 if (sc->path == NULL)
325 g_fox_select_path(gp, 0);
326 if (sc->path == NULL)
329 error = g_access(sc->path, dr, dw, de);
334 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
336 * Last close, remove e-bit on all consumers
338 LIST_FOREACH(cp1, &gp->consumer, consumer)
339 g_access(cp1, 0, 0, -1);
345 static struct g_geom *
346 g_fox_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
348 struct g_geom *gp, *gp2;
349 struct g_provider *pp2;
350 struct g_consumer *cp, *cp2;
351 struct g_fox_softc *sc, *sc2;
356 g_trace(G_T_TOPOLOGY, "fox_taste(%s, %s)", mp->name, pp->name);
358 if (!strcmp(pp->geom->class->name, mp->name))
360 gp = g_new_geomf(mp, "%s.fox", pp->name);
361 gp->softc = g_malloc(sizeof(struct g_fox_softc), M_WAITOK | M_ZERO);
364 cp = g_new_consumer(gp);
366 error = g_access(cp, 1, 0, 0);
370 g_destroy_consumer(cp);
375 sectorsize = cp->provider->sectorsize;
377 buf = g_read_data(cp, 0, sectorsize, NULL);
381 if (memcmp(buf, FOX_MAGIC, strlen(FOX_MAGIC)))
385 * First we need to see if this a new path for an existing fox.
387 LIST_FOREACH(gp2, &mp->geom, geom) {
391 if (memcmp(buf + 16, sc2->magic, sizeof sc2->magic))
397 * It was. Create a new consumer for that fox,
398 * attach it, and if the fox is open, open this
399 * path with an exclusive count of one.
401 printf("Adding path (%s) to fox (%s)\n",
402 pp->name, gp2->name);
403 cp2 = g_new_consumer(gp2);
405 pp2 = LIST_FIRST(&gp2->provider);
406 if (pp2->acr > 0 || pp2->acw > 0 || pp2->ace > 0) {
407 error = g_access(cp2, 0, 0, 1);
410 * This is bad, or more likely,
411 * the user is doing something stupid
414 "WARNING: New path (%s) to fox(%s) not added: %s\n%s",
415 cp2->provider->name, gp2->name,
416 "Could not get exclusive bit.",
417 "WARNING: This indicates a risk of data inconsistency."
420 g_destroy_consumer(cp2);
425 printf("Creating new fox (%s)\n", pp->name);
427 memcpy(sc->magic, buf + 16, sizeof sc->magic);
428 pp2 = g_new_providerf(gp, "%s", gp->name);
429 pp2->mediasize = sc->mediasize = pp->mediasize - pp->sectorsize;
430 pp2->sectorsize = sc->sectorsize = pp->sectorsize;
431 printf("fox %s lock %p\n", gp->name, &sc->lock);
433 mtx_init(&sc->lock, "fox queue", NULL, MTX_DEF);
434 TAILQ_INIT(&sc->queue);
435 g_error_provider(pp2, 0);
439 g_access(cp, -1, 0, 0);
441 if (!LIST_EMPTY(&gp->provider))
446 g_destroy_consumer(cp);
452 g_fox_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
454 struct g_fox_softc *sc;
458 mtx_destroy(&sc->lock);
461 g_wither_geom(gp, ENXIO);
465 static struct g_class g_fox_class = {
466 .name = FOX_CLASS_NAME,
467 .version = G_VERSION,
468 .taste = g_fox_taste,
469 .destroy_geom = g_fox_destroy_geom,
470 .start = g_fox_start,
471 .spoiled = g_fox_orphan,
472 .orphan = g_fox_orphan,
473 .access= g_fox_access,
476 DECLARE_GEOM_CLASS(g_fox_class, g_fox);