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/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/libkern.h>
47 #include <sys/endian.h>
49 #include <sys/errno.h>
50 #include <geom/geom.h>
52 #define FOX_CLASS_NAME "FOX"
53 #define FOX_MAGIC "GEOM::FOX"
58 TAILQ_HEAD(, bio) queue;
61 struct g_consumer *path;
62 struct g_consumer *opath;
68 * This function is called whenever we need to select a new path.
71 g_fox_select_path(void *arg, int flag)
74 struct g_fox_softc *sc;
75 struct g_consumer *cp1;
80 if (flag == EV_CANCEL)
85 if (sc->opath != NULL) {
87 * First, close the old path entirely.
89 printf("Closing old path (%s) on fox (%s)\n",
90 sc->opath->provider->name, gp->name);
92 cp1 = LIST_NEXT(sc->opath, consumer);
94 g_access(sc->opath, -sc->cr, -sc->cw, -(sc->ce + 1));
97 * The attempt to reopen it with a exclusive count
99 error = g_access(sc->opath, 0, 0, 1);
102 * Ok, ditch this consumer, we can't use it.
104 printf("Drop old path (%s) on fox (%s)\n",
105 sc->opath->provider->name, gp->name);
107 g_destroy_consumer(sc->opath);
108 if (LIST_EMPTY(&gp->consumer)) {
109 /* No consumers left */
110 g_wither_geom(gp, ENXIO);
112 bp = TAILQ_FIRST(&sc->queue);
115 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
116 bp->bio_error = ENXIO;
122 printf("Got e-bit on old path (%s) on fox (%s)\n",
123 sc->opath->provider->name, gp->name);
127 cp1 = LIST_FIRST(&gp->consumer);
130 cp1 = LIST_FIRST(&gp->consumer);
131 printf("Open new path (%s) on fox (%s)\n",
132 cp1->provider->name, gp->name);
133 error = g_access(cp1, sc->cr, sc->cw, sc->ce);
136 * If we failed, we take another trip through here
138 printf("Open new path (%s) on fox (%s) failed, reselect.\n",
139 cp1->provider->name, gp->name);
141 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
143 printf("Open new path (%s) on fox (%s) succeeded\n",
144 cp1->provider->name, gp->name);
149 bp = TAILQ_FIRST(&sc->queue);
152 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
153 g_io_request(bp, sc->path);
155 mtx_unlock(&sc->lock);
160 g_fox_orphan(struct g_consumer *cp)
163 struct g_fox_softc *sc;
169 printf("Removing path (%s) from fox (%s)\n",
170 cp->provider->name, gp->name);
172 if (cp == sc->path) {
180 mtx_unlock(&sc->lock);
182 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
183 error = cp->provider->error;
185 g_destroy_consumer(cp);
186 if (!LIST_EMPTY(&gp->consumer)) {
188 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
192 mtx_destroy(&sc->lock);
195 g_wither_geom(gp, ENXIO);
199 g_fox_done(struct bio *bp)
202 struct g_fox_softc *sc;
205 if (bp->bio_error == 0) {
209 gp = bp->bio_from->geom;
211 if (bp->bio_from != sc->path) {
212 g_io_request(bp, sc->path);
216 sc->opath = sc->path;
218 error = g_post_event(g_fox_select_path, gp, M_NOWAIT, gp, NULL);
220 bp->bio_error = ENOMEM;
224 TAILQ_INSERT_TAIL(&sc->queue, bp, bio_queue);
226 mtx_unlock(&sc->lock);
230 g_fox_start(struct bio *bp)
234 struct g_fox_softc *sc;
237 gp = bp->bio_to->geom;
240 g_io_deliver(bp, ENXIO);
243 switch(bp->bio_cmd) {
247 bp2 = g_clone_bio(bp);
249 g_io_deliver(bp, ENOMEM);
252 bp2->bio_offset += sc->sectorsize;
253 bp2->bio_done = g_fox_done;
255 if (sc->path == NULL || !TAILQ_EMPTY(&sc->queue)) {
256 if (sc->waiting == 0) {
257 error = g_post_event(g_fox_select_path, gp,
262 g_io_deliver(bp, error);
268 TAILQ_INSERT_TAIL(&sc->queue, bp2,
271 g_io_request(bp2, sc->path);
273 mtx_unlock(&sc->lock);
276 g_io_deliver(bp, EOPNOTSUPP);
283 g_fox_access(struct g_provider *pp, int dr, int dw, int de)
286 struct g_fox_softc *sc;
287 struct g_consumer *cp1;
294 if (dr <= 0 && dw <= 0 && de <= 0)
300 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
302 * First open, open all consumers with an exclusive bit
305 LIST_FOREACH(cp1, &gp->consumer, consumer) {
306 error = g_access(cp1, 0, 0, 1);
308 printf("FOX: access(%s,0,0,1) = %d\n",
309 cp1->provider->name, error);
314 LIST_FOREACH(cp1, &gp->consumer, consumer) {
316 g_access(cp1, 0, 0, -1);
321 if (sc->path == NULL)
322 g_fox_select_path(gp, 0);
323 if (sc->path == NULL)
326 error = g_access(sc->path, dr, dw, de);
331 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
333 * Last close, remove e-bit on all consumers
335 LIST_FOREACH(cp1, &gp->consumer, consumer)
336 g_access(cp1, 0, 0, -1);
342 static struct g_geom *
343 g_fox_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
345 struct g_geom *gp, *gp2;
346 struct g_provider *pp2;
347 struct g_consumer *cp, *cp2;
348 struct g_fox_softc *sc, *sc2;
353 g_trace(G_T_TOPOLOGY, "fox_taste(%s, %s)", mp->name, pp->name);
355 if (!strcmp(pp->geom->class->name, mp->name))
357 gp = g_new_geomf(mp, "%s.fox", pp->name);
358 gp->softc = g_malloc(sizeof(struct g_fox_softc), M_WAITOK | M_ZERO);
361 cp = g_new_consumer(gp);
363 error = g_access(cp, 1, 0, 0);
367 g_destroy_consumer(cp);
372 sectorsize = cp->provider->sectorsize;
374 buf = g_read_data(cp, 0, sectorsize, NULL);
378 if (memcmp(buf, FOX_MAGIC, strlen(FOX_MAGIC)))
382 * First we need to see if this a new path for an existing fox.
384 LIST_FOREACH(gp2, &mp->geom, geom) {
388 if (memcmp(buf + 16, sc2->magic, sizeof sc2->magic))
394 * It was. Create a new consumer for that fox,
395 * attach it, and if the fox is open, open this
396 * path with an exclusive count of one.
398 printf("Adding path (%s) to fox (%s)\n",
399 pp->name, gp2->name);
400 cp2 = g_new_consumer(gp2);
402 pp2 = LIST_FIRST(&gp2->provider);
403 if (pp2->acr > 0 || pp2->acw > 0 || pp2->ace > 0) {
404 error = g_access(cp2, 0, 0, 1);
407 * This is bad, or more likely,
408 * the user is doing something stupid
411 "WARNING: New path (%s) to fox(%s) not added: %s\n%s",
412 cp2->provider->name, gp2->name,
413 "Could not get exclusive bit.",
414 "WARNING: This indicates a risk of data inconsistency."
417 g_destroy_consumer(cp2);
422 printf("Creating new fox (%s)\n", pp->name);
424 memcpy(sc->magic, buf + 16, sizeof sc->magic);
425 pp2 = g_new_providerf(gp, "%s", gp->name);
426 pp2->mediasize = sc->mediasize = pp->mediasize - pp->sectorsize;
427 pp2->sectorsize = sc->sectorsize = pp->sectorsize;
428 printf("fox %s lock %p\n", gp->name, &sc->lock);
430 mtx_init(&sc->lock, "fox queue", NULL, MTX_DEF);
431 TAILQ_INIT(&sc->queue);
432 g_error_provider(pp2, 0);
436 g_access(cp, -1, 0, 0);
438 if (!LIST_EMPTY(&gp->provider))
443 g_destroy_consumer(cp);
449 g_fox_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
451 struct g_fox_softc *sc;
455 mtx_destroy(&sc->lock);
458 g_wither_geom(gp, ENXIO);
462 static struct g_class g_fox_class = {
463 .name = FOX_CLASS_NAME,
464 .version = G_VERSION,
465 .taste = g_fox_taste,
466 .destroy_geom = g_fox_destroy_geom,
467 .start = g_fox_start,
468 .spoiled = g_fox_orphan,
469 .orphan = g_fox_orphan,
470 .access= g_fox_access,
473 DECLARE_GEOM_CLASS(g_fox_class, g_fox);