2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* This is a GEOM module for handling path selection for multi-path
35 * storage devices. It is named "fox" because it, like they, prefer
36 * to have multiple exits to choose from.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/libkern.h>
50 #include <sys/endian.h>
52 #include <sys/errno.h>
53 #include <geom/geom.h>
55 #define FOX_CLASS_NAME "FOX"
56 #define FOX_MAGIC "GEOM::FOX"
58 static int g_fox_once;
60 FEATURE(geom_fox, "GEOM FOX redundant path mitigation support");
65 TAILQ_HEAD(, bio) queue;
68 struct g_consumer *path;
69 struct g_consumer *opath;
75 * This function is called whenever we need to select a new path.
78 g_fox_select_path(void *arg, int flag)
81 struct g_fox_softc *sc;
82 struct g_consumer *cp1;
87 if (flag == EV_CANCEL)
92 if (sc->opath != NULL) {
94 * First, close the old path entirely.
96 printf("Closing old path (%s) on fox (%s)\n",
97 sc->opath->provider->name, gp->name);
99 cp1 = LIST_NEXT(sc->opath, consumer);
101 g_access(sc->opath, -sc->cr, -sc->cw, -(sc->ce + 1));
104 * The attempt to reopen it with a exclusive count
106 error = g_access(sc->opath, 0, 0, 1);
109 * Ok, ditch this consumer, we can't use it.
111 printf("Drop old path (%s) on fox (%s)\n",
112 sc->opath->provider->name, gp->name);
114 g_destroy_consumer(sc->opath);
115 if (LIST_EMPTY(&gp->consumer)) {
116 /* No consumers left */
117 g_wither_geom(gp, ENXIO);
119 bp = TAILQ_FIRST(&sc->queue);
122 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
123 bp->bio_error = ENXIO;
129 printf("Got e-bit on old path (%s) on fox (%s)\n",
130 sc->opath->provider->name, gp->name);
134 cp1 = LIST_FIRST(&gp->consumer);
137 cp1 = LIST_FIRST(&gp->consumer);
138 printf("Open new path (%s) on fox (%s)\n",
139 cp1->provider->name, gp->name);
140 error = g_access(cp1, sc->cr, sc->cw, sc->ce);
143 * If we failed, we take another trip through here
145 printf("Open new path (%s) on fox (%s) failed, reselect.\n",
146 cp1->provider->name, gp->name);
148 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
150 printf("Open new path (%s) on fox (%s) succeeded\n",
151 cp1->provider->name, gp->name);
156 bp = TAILQ_FIRST(&sc->queue);
159 TAILQ_REMOVE(&sc->queue, bp, bio_queue);
160 g_io_request(bp, sc->path);
162 mtx_unlock(&sc->lock);
167 g_fox_orphan(struct g_consumer *cp)
170 struct g_fox_softc *sc;
176 printf("Removing path (%s) from fox (%s)\n",
177 cp->provider->name, gp->name);
179 if (cp == sc->path) {
187 mtx_unlock(&sc->lock);
189 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
190 error = cp->provider->error;
192 g_destroy_consumer(cp);
193 if (!LIST_EMPTY(&gp->consumer)) {
195 g_post_event(g_fox_select_path, gp, M_WAITOK, gp, NULL);
199 mtx_destroy(&sc->lock);
202 g_wither_geom(gp, ENXIO);
206 g_fox_done(struct bio *bp)
209 struct g_fox_softc *sc;
212 if (bp->bio_error == 0) {
216 gp = bp->bio_from->geom;
218 if (bp->bio_from != sc->path) {
219 g_io_request(bp, sc->path);
223 sc->opath = sc->path;
225 error = g_post_event(g_fox_select_path, gp, M_NOWAIT, gp, NULL);
227 bp->bio_error = ENOMEM;
231 TAILQ_INSERT_TAIL(&sc->queue, bp, bio_queue);
233 mtx_unlock(&sc->lock);
237 g_fox_start(struct bio *bp)
241 struct g_fox_softc *sc;
244 gp = bp->bio_to->geom;
247 g_io_deliver(bp, ENXIO);
250 switch(bp->bio_cmd) {
254 bp2 = g_clone_bio(bp);
256 g_io_deliver(bp, ENOMEM);
259 bp2->bio_offset += sc->sectorsize;
260 bp2->bio_done = g_fox_done;
262 if (sc->path == NULL || !TAILQ_EMPTY(&sc->queue)) {
263 if (sc->waiting == 0) {
264 error = g_post_event(g_fox_select_path, gp,
269 g_io_deliver(bp, error);
275 TAILQ_INSERT_TAIL(&sc->queue, bp2,
278 g_io_request(bp2, sc->path);
280 mtx_unlock(&sc->lock);
283 g_io_deliver(bp, EOPNOTSUPP);
290 g_fox_access(struct g_provider *pp, int dr, int dw, int de)
293 struct g_fox_softc *sc;
294 struct g_consumer *cp1;
301 if (dr <= 0 && dw <= 0 && de <= 0)
307 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
309 * First open, open all consumers with an exclusive bit
312 LIST_FOREACH(cp1, &gp->consumer, consumer) {
313 error = g_access(cp1, 0, 0, 1);
315 printf("FOX: access(%s,0,0,1) = %d\n",
316 cp1->provider->name, error);
321 LIST_FOREACH(cp1, &gp->consumer, consumer) {
323 g_access(cp1, 0, 0, -1);
328 if (sc->path == NULL)
329 g_fox_select_path(gp, 0);
330 if (sc->path == NULL)
333 error = g_access(sc->path, dr, dw, de);
338 if (sc->cr == 0 && sc->cw == 0 && sc->ce == 0) {
340 * Last close, remove e-bit on all consumers
342 LIST_FOREACH(cp1, &gp->consumer, consumer)
343 g_access(cp1, 0, 0, -1);
349 static struct g_geom *
350 g_fox_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
352 struct g_geom *gp, *gp2;
353 struct g_provider *pp2;
354 struct g_consumer *cp, *cp2;
355 struct g_fox_softc *sc, *sc2;
360 g_trace(G_T_TOPOLOGY, "fox_taste(%s, %s)", mp->name, pp->name);
362 if (!strcmp(pp->geom->class->name, mp->name))
364 gp = g_new_geomf(mp, "%s.fox", pp->name);
365 gp->softc = g_malloc(sizeof(struct g_fox_softc), M_WAITOK | M_ZERO);
368 cp = g_new_consumer(gp);
370 error = g_access(cp, 1, 0, 0);
374 g_destroy_consumer(cp);
379 sectorsize = cp->provider->sectorsize;
381 buf = g_read_data(cp, 0, sectorsize, NULL);
385 if (memcmp(buf, FOX_MAGIC, strlen(FOX_MAGIC)))
389 * First we need to see if this a new path for an existing fox.
391 LIST_FOREACH(gp2, &mp->geom, geom) {
395 if (memcmp(buf + 16, sc2->magic, sizeof sc2->magic))
401 * It was. Create a new consumer for that fox,
402 * attach it, and if the fox is open, open this
403 * path with an exclusive count of one.
405 printf("Adding path (%s) to fox (%s)\n",
406 pp->name, gp2->name);
407 cp2 = g_new_consumer(gp2);
409 pp2 = LIST_FIRST(&gp2->provider);
410 if (pp2->acr > 0 || pp2->acw > 0 || pp2->ace > 0) {
411 error = g_access(cp2, 0, 0, 1);
414 * This is bad, or more likely,
415 * the user is doing something stupid
418 "WARNING: New path (%s) to fox(%s) not added: %s\n%s",
419 cp2->provider->name, gp2->name,
420 "Could not get exclusive bit.",
421 "WARNING: This indicates a risk of data inconsistency."
424 g_destroy_consumer(cp2);
429 printf("Creating new fox (%s)\n", pp->name);
431 memcpy(sc->magic, buf + 16, sizeof sc->magic);
432 pp2 = g_new_providerf(gp, "%s", gp->name);
433 pp2->mediasize = sc->mediasize = pp->mediasize - pp->sectorsize;
434 pp2->sectorsize = sc->sectorsize = pp->sectorsize;
435 printf("fox %s lock %p\n", gp->name, &sc->lock);
437 mtx_init(&sc->lock, "fox queue", NULL, MTX_DEF);
438 TAILQ_INIT(&sc->queue);
439 g_error_provider(pp2, 0);
443 g_access(cp, -1, 0, 0);
445 if (!LIST_EMPTY(&gp->provider)) {
449 "WARNING: geom_fox (geom %s) is deprecated, "
450 "use gmultipath instead.\n", gp->name);
457 g_destroy_consumer(cp);
463 g_fox_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
465 struct g_fox_softc *sc;
469 mtx_destroy(&sc->lock);
472 g_wither_geom(gp, ENXIO);
476 static struct g_class g_fox_class = {
477 .name = FOX_CLASS_NAME,
478 .version = G_VERSION,
479 .taste = g_fox_taste,
480 .destroy_geom = g_fox_destroy_geom,
481 .start = g_fox_start,
482 .spoiled = g_fox_orphan,
483 .orphan = g_fox_orphan,
484 .access= g_fox_access,
487 DECLARE_GEOM_CLASS(g_fox_class, g_fox);
488 MODULE_VERSION(geom_fox, 0);