The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/vinum/geom_vinum.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  *  Copyright (c) 2004, 2007 Lukas Ertl
    3  *  Copyright (c) 2007, 2009 Ulf Lilleengen
    4  *  All rights reserved.
    5  * 
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 
   15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: releng/8.0/sys/geom/vinum/geom_vinum.c 195752 2009-07-18 11:12:48Z lulf $");
   31 
   32 #include <sys/param.h>
   33 #include <sys/bio.h>
   34 #include <sys/kernel.h>
   35 #include <sys/kthread.h>
   36 #include <sys/lock.h>
   37 #include <sys/malloc.h>
   38 #include <sys/module.h>
   39 #include <sys/mutex.h>
   40 #include <sys/sysctl.h>
   41 #include <sys/systm.h>
   42 
   43 #include <geom/geom.h>
   44 #include <geom/vinum/geom_vinum_var.h>
   45 #include <geom/vinum/geom_vinum.h>
   46 #include <geom/vinum/geom_vinum_raid5.h>
   47 
   48 SYSCTL_DECL(_kern_geom);
   49 SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW, 0, "GEOM_VINUM stuff");
   50 u_int g_vinum_debug = 0;
   51 TUNABLE_INT("kern.geom.vinum.debug", &g_vinum_debug);
   52 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RW, &g_vinum_debug, 0,
   53     "Debug level");
   54 
   55 static int      gv_create(struct g_geom *, struct gctl_req *);
   56 static void     gv_attach(struct gv_softc *, struct gctl_req *);
   57 static void     gv_detach(struct gv_softc *, struct gctl_req *);
   58 static void     gv_parityop(struct gv_softc *, struct gctl_req *);
   59 
   60 
   61 static void
   62 gv_orphan(struct g_consumer *cp)
   63 {
   64         struct g_geom *gp;
   65         struct gv_softc *sc;
   66         struct gv_drive *d;
   67         
   68         g_topology_assert();
   69 
   70         KASSERT(cp != NULL, ("gv_orphan: null cp"));
   71         gp = cp->geom;
   72         KASSERT(gp != NULL, ("gv_orphan: null gp"));
   73         sc = gp->softc;
   74         KASSERT(sc != NULL, ("gv_orphan: null sc"));
   75         d = cp->private;
   76         KASSERT(d != NULL, ("gv_orphan: null d"));
   77 
   78         g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);
   79 
   80         gv_post_event(sc, GV_EVENT_DRIVE_LOST, d, NULL, 0, 0);
   81 }
   82 
   83 void
   84 gv_start(struct bio *bp)
   85 {
   86         struct g_geom *gp;
   87         struct gv_softc *sc;
   88         
   89         gp = bp->bio_to->geom;
   90         sc = gp->softc;
   91 
   92         switch (bp->bio_cmd) {
   93         case BIO_READ:
   94         case BIO_WRITE:
   95         case BIO_DELETE:
   96                 break;
   97         case BIO_GETATTR:
   98         default:
   99                 g_io_deliver(bp, EOPNOTSUPP);
  100                 return;
  101         }
  102         mtx_lock(&sc->bqueue_mtx);
  103         bioq_disksort(sc->bqueue_down, bp);
  104         wakeup(sc);
  105         mtx_unlock(&sc->bqueue_mtx);
  106 }
  107 
  108 void
  109 gv_done(struct bio *bp)
  110 {
  111         struct g_geom *gp;
  112         struct gv_softc *sc;
  113         
  114         KASSERT(bp != NULL, ("NULL bp"));
  115 
  116         gp = bp->bio_from->geom;
  117         sc = gp->softc;
  118 
  119         mtx_lock(&sc->bqueue_mtx);
  120         bioq_disksort(sc->bqueue_up, bp);
  121         wakeup(sc);
  122         mtx_unlock(&sc->bqueue_mtx);
  123 }
  124 
  125 int
  126 gv_access(struct g_provider *pp, int dr, int dw, int de)
  127 {
  128         struct g_geom *gp;
  129         struct gv_softc *sc;
  130         struct gv_drive *d, *d2;
  131         int error;
  132         
  133         error = ENXIO;
  134         gp = pp->geom;
  135         sc = gp->softc;
  136         /*
  137          * We want to modify the read count with the write count in case we have
  138          * plexes in a RAID-5 organization.
  139          */
  140         dr += dw;
  141 
  142         LIST_FOREACH(d, &sc->drives, drive) {
  143                 if (d->consumer == NULL)
  144                         continue;
  145                 error = g_access(d->consumer, dr, dw, de);
  146                 if (error) {
  147                         LIST_FOREACH(d2, &sc->drives, drive) {
  148                                 if (d == d2)
  149                                         break;
  150                                 g_access(d2->consumer, -dr, -dw, -de);
  151                         }
  152                         G_VINUM_DEBUG(0, "g_access '%s' failed: %d", d->name,
  153                             error);
  154                         return (error);
  155                 }
  156         }
  157         return (0);
  158 }
  159 
  160 static void
  161 gv_init(struct g_class *mp)
  162 {
  163         struct g_geom *gp;
  164         struct gv_softc *sc;
  165 
  166         g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);
  167 
  168         gp = g_new_geomf(mp, "VINUM");
  169         gp->spoiled = gv_orphan;
  170         gp->orphan = gv_orphan;
  171         gp->access = gv_access;
  172         gp->start = gv_start;
  173         gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO);
  174         sc = gp->softc;
  175         sc->geom = gp;
  176         sc->bqueue_down = g_malloc(sizeof(struct bio_queue_head),
  177             M_WAITOK | M_ZERO);
  178         sc->bqueue_up = g_malloc(sizeof(struct bio_queue_head),
  179             M_WAITOK | M_ZERO);
  180         bioq_init(sc->bqueue_down);
  181         bioq_init(sc->bqueue_up);
  182         LIST_INIT(&sc->drives);
  183         LIST_INIT(&sc->subdisks);
  184         LIST_INIT(&sc->plexes);
  185         LIST_INIT(&sc->volumes);
  186         TAILQ_INIT(&sc->equeue);
  187         mtx_init(&sc->config_mtx, "gv_config", NULL, MTX_DEF);
  188         mtx_init(&sc->equeue_mtx, "gv_equeue", NULL, MTX_DEF);
  189         mtx_init(&sc->bqueue_mtx, "gv_bqueue", NULL, MTX_DEF);
  190         kproc_create(gv_worker, sc, NULL, 0, 0, "gv_worker");
  191 }
  192 
  193 static int
  194 gv_unload(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
  195 {
  196         struct gv_softc *sc;
  197 
  198         g_trace(G_T_TOPOLOGY, "gv_unload(%p)", mp);
  199 
  200         g_topology_assert();
  201         sc = gp->softc;
  202 
  203         if (sc != NULL) {
  204                 gv_post_event(sc, GV_EVENT_THREAD_EXIT, NULL, NULL, 0, 0);
  205                 gp->softc = NULL;
  206                 g_wither_geom(gp, ENXIO);
  207                 return (EAGAIN);
  208         }
  209 
  210         return (0);
  211 }
  212 
  213 /* Handle userland request of attaching object. */
  214 static void
  215 gv_attach(struct gv_softc *sc, struct gctl_req *req)
  216 {
  217         struct gv_volume *v;
  218         struct gv_plex *p;
  219         struct gv_sd *s;
  220         off_t *offset;
  221         int *rename, type_child, type_parent;
  222         char *child, *parent;
  223 
  224         child = gctl_get_param(req, "child", NULL);
  225         if (child == NULL) {
  226                 gctl_error(req, "no child given");
  227                 return;
  228         }
  229         parent = gctl_get_param(req, "parent", NULL);
  230         if (parent == NULL) {
  231                 gctl_error(req, "no parent given");
  232                 return;
  233         }
  234         offset = gctl_get_paraml(req, "offset", sizeof(*offset));
  235         if (offset == NULL) {
  236                 gctl_error(req, "no offset given");
  237                 return;
  238         }
  239         rename = gctl_get_paraml(req, "rename", sizeof(*rename));
  240         if (rename == NULL) {
  241                 gctl_error(req, "no rename flag given");
  242                 return;
  243         }
  244 
  245         type_child = gv_object_type(sc, child);
  246         type_parent = gv_object_type(sc, parent);
  247 
  248         switch (type_child) {
  249         case GV_TYPE_PLEX:
  250                 if (type_parent != GV_TYPE_VOL) {
  251                         gctl_error(req, "no such volume to attach to");
  252                         return;
  253                 }
  254                 v = gv_find_vol(sc, parent);
  255                 p = gv_find_plex(sc, child);
  256                 gv_post_event(sc, GV_EVENT_ATTACH_PLEX, p, v, *offset, *rename);
  257                 break;
  258         case GV_TYPE_SD:
  259                 if (type_parent != GV_TYPE_PLEX) {
  260                         gctl_error(req, "no such plex to attach to");
  261                         return;
  262                 }
  263                 p = gv_find_plex(sc, parent);
  264                 s = gv_find_sd(sc, child);
  265                 gv_post_event(sc, GV_EVENT_ATTACH_SD, s, p, *offset, *rename);
  266                 break;
  267         default:
  268                 gctl_error(req, "invalid child type");
  269                 break;
  270         }
  271 }
  272 
  273 /* Handle userland request of detaching object. */
  274 static void
  275 gv_detach(struct gv_softc *sc, struct gctl_req *req)
  276 {
  277         struct gv_plex *p;
  278         struct gv_sd *s;
  279         int *flags, type;
  280         char *object;
  281 
  282         object = gctl_get_param(req, "object", NULL);
  283         if (object == NULL) {
  284                 gctl_error(req, "no argument given");
  285                 return;
  286         }
  287 
  288         flags = gctl_get_paraml(req, "flags", sizeof(*flags));
  289         type = gv_object_type(sc, object);
  290         switch (type) {
  291         case GV_TYPE_PLEX:
  292                 p = gv_find_plex(sc, object);
  293                 gv_post_event(sc, GV_EVENT_DETACH_PLEX, p, NULL, *flags, 0);
  294                 break;
  295         case GV_TYPE_SD:
  296                 s = gv_find_sd(sc, object);
  297                 gv_post_event(sc, GV_EVENT_DETACH_SD, s, NULL, *flags, 0);
  298                 break;
  299         default:
  300                 gctl_error(req, "invalid object type");
  301                 break;
  302         }
  303 }
  304 
  305 /* Handle userland requests for creating new objects. */
  306 static int
  307 gv_create(struct g_geom *gp, struct gctl_req *req)
  308 {
  309         struct gv_softc *sc;
  310         struct gv_drive *d, *d2;
  311         struct gv_plex *p, *p2;
  312         struct gv_sd *s, *s2;
  313         struct gv_volume *v, *v2;
  314         struct g_provider *pp;
  315         int error, i, *drives, *flags, *plexes, *subdisks, *volumes;
  316         char buf[20];
  317 
  318         g_topology_assert();
  319 
  320         sc = gp->softc;
  321 
  322         /* Find out how many of each object have been passed in. */
  323         volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes));
  324         plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes));
  325         subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks));
  326         drives = gctl_get_paraml(req, "drives", sizeof(*drives));
  327         if (volumes == NULL || plexes == NULL || subdisks == NULL ||
  328             drives == NULL) {
  329                 gctl_error(req, "number of objects not given");
  330                 return (-1);
  331         }
  332         flags = gctl_get_paraml(req, "flags", sizeof(*flags));
  333         if (flags == NULL) {
  334                 gctl_error(req, "flags not given");
  335                 return (-1);
  336         }
  337 
  338         /* First, handle drive definitions ... */
  339         for (i = 0; i < *drives; i++) {
  340                 snprintf(buf, sizeof(buf), "drive%d", i);
  341                 d2 = gctl_get_paraml(req, buf, sizeof(*d2));
  342                 if (d2 == NULL) {
  343                         gctl_error(req, "no drive definition given");
  344                         return (-1);
  345                 }
  346                 /*
  347                  * Make sure that the device specified in the drive config is
  348                  * an active GEOM provider.
  349                  */
  350                 pp = g_provider_by_name(d2->device);
  351                 if (pp == NULL) {
  352                         gctl_error(req, "%s: device not found", d2->device);
  353                         goto error;
  354                 }
  355                 if (gv_find_drive(sc, d2->name) != NULL) {
  356                         /* Ignore error. */
  357                         if (*flags & GV_FLAG_F)
  358                                 continue;
  359                         gctl_error(req, "drive '%s' already exists", d2->name);
  360                         goto error;
  361                 }
  362                 if (gv_find_drive_device(sc, d2->device) != NULL) {
  363                         gctl_error(req, "device '%s' already configured in "
  364                             "gvinum", d2->device);
  365                         goto error;
  366                 }
  367 
  368 
  369                 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
  370                 bcopy(d2, d, sizeof(*d));
  371 
  372                 gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0);
  373         }
  374 
  375         /* ... then volume definitions ... */
  376         for (i = 0; i < *volumes; i++) {
  377                 error = 0;
  378                 snprintf(buf, sizeof(buf), "volume%d", i);
  379                 v2 = gctl_get_paraml(req, buf, sizeof(*v2));
  380                 if (v2 == NULL) {
  381                         gctl_error(req, "no volume definition given");
  382                         return (-1);
  383                 }
  384                 if (gv_find_vol(sc, v2->name) != NULL) {
  385                         /* Ignore error. */
  386                         if (*flags & GV_FLAG_F)
  387                                 continue;
  388                         gctl_error(req, "volume '%s' already exists", v2->name);
  389                         goto error;
  390                 }
  391 
  392                 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
  393                 bcopy(v2, v, sizeof(*v));
  394 
  395                 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
  396         }
  397 
  398         /* ... then plex definitions ... */
  399         for (i = 0; i < *plexes; i++) {
  400                 error = 0;
  401                 snprintf(buf, sizeof(buf), "plex%d", i);
  402                 p2 = gctl_get_paraml(req, buf, sizeof(*p2));
  403                 if (p2 == NULL) {
  404                         gctl_error(req, "no plex definition given");
  405                         return (-1);
  406                 }
  407                 if (gv_find_plex(sc, p2->name) != NULL) {
  408                         /* Ignore error. */
  409                         if (*flags & GV_FLAG_F)
  410                                 continue;
  411                         gctl_error(req, "plex '%s' already exists", p2->name);
  412                         goto error;
  413                 }
  414 
  415                 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
  416                 bcopy(p2, p, sizeof(*p));
  417 
  418                 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
  419         }
  420 
  421         /* ... and, finally, subdisk definitions. */
  422         for (i = 0; i < *subdisks; i++) {
  423                 error = 0;
  424                 snprintf(buf, sizeof(buf), "sd%d", i);
  425                 s2 = gctl_get_paraml(req, buf, sizeof(*s2));
  426                 if (s2 == NULL) {
  427                         gctl_error(req, "no subdisk definition given");
  428                         return (-1);
  429                 }
  430                 if (gv_find_sd(sc, s2->name) != NULL) {
  431                         /* Ignore error. */
  432                         if (*flags & GV_FLAG_F)
  433                                 continue;
  434                         gctl_error(req, "sd '%s' already exists", s2->name);
  435                         goto error;
  436                 }
  437 
  438                 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
  439                 bcopy(s2, s, sizeof(*s));
  440 
  441                 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
  442         }
  443 
  444 error:
  445         gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
  446         gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
  447 
  448         return (0);
  449 }
  450 
  451 static void
  452 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
  453 {
  454         struct g_geom *gp;
  455         struct gv_softc *sc;
  456         struct sbuf *sb;
  457         char *comment;
  458 
  459         g_topology_assert();
  460 
  461         gp = LIST_FIRST(&mp->geom);
  462         sc = gp->softc;
  463 
  464         if (!strcmp(verb, "attach")) {
  465                 gv_attach(sc, req);
  466 
  467         } else if (!strcmp(verb, "concat")) {
  468                 gv_concat(gp, req);
  469 
  470         } else if (!strcmp(verb, "detach")) {
  471                 gv_detach(sc, req);
  472 
  473         } else if (!strcmp(verb, "list")) {
  474                 gv_list(gp, req);
  475 
  476         /* Save our configuration back to disk. */
  477         } else if (!strcmp(verb, "saveconfig")) {
  478                 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
  479 
  480         /* Return configuration in string form. */
  481         } else if (!strcmp(verb, "getconfig")) {
  482                 comment = gctl_get_param(req, "comment", NULL);
  483                 if (comment == NULL) {
  484                         gctl_error(req, "no comment parameter given");
  485                         return;
  486                 }
  487                 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
  488                 gv_format_config(sc, sb, 0, comment);
  489                 sbuf_finish(sb);
  490                 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
  491                 sbuf_delete(sb);
  492 
  493         } else if (!strcmp(verb, "create")) {
  494                 gv_create(gp, req);
  495 
  496         } else if (!strcmp(verb, "mirror")) {
  497                 gv_mirror(gp, req);
  498 
  499         } else if (!strcmp(verb, "move")) {
  500                 gv_move(gp, req);
  501 
  502         } else if (!strcmp(verb, "raid5")) {
  503                 gv_raid5(gp, req);
  504 
  505         } else if (!strcmp(verb, "rebuildparity") ||
  506             !strcmp(verb, "checkparity")) {
  507                 gv_parityop(sc, req);
  508 
  509         } else if (!strcmp(verb, "remove")) {
  510                 gv_remove(gp, req);
  511 
  512         } else if (!strcmp(verb, "rename")) {
  513                 gv_rename(gp, req);
  514         
  515         } else if (!strcmp(verb, "resetconfig")) {
  516                 gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0);
  517 
  518         } else if (!strcmp(verb, "start")) {
  519                 gv_start_obj(gp, req);
  520 
  521         } else if (!strcmp(verb, "stripe")) {
  522                 gv_stripe(gp, req);
  523 
  524         } else if (!strcmp(verb, "setstate")) {
  525                 gv_setstate(gp, req);
  526         } else
  527                 gctl_error(req, "Unknown verb parameter");
  528 }
  529 
  530 static void
  531 gv_parityop(struct gv_softc *sc, struct gctl_req *req)
  532 {
  533         struct gv_plex *p;
  534         int *flags, *rebuild, type;
  535         char *plex;
  536 
  537         plex = gctl_get_param(req, "plex", NULL);
  538         if (plex == NULL) {
  539                 gctl_error(req, "no plex given");
  540                 return;
  541         }
  542 
  543         flags = gctl_get_paraml(req, "flags", sizeof(*flags));
  544         if (flags == NULL) {
  545                 gctl_error(req, "no flags given");
  546                 return;
  547         }
  548 
  549         rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild));
  550         if (rebuild == NULL) {
  551                 gctl_error(req, "no operation given");
  552                 return;
  553         }
  554 
  555         type = gv_object_type(sc, plex);
  556         if (type != GV_TYPE_PLEX) {
  557                 gctl_error(req, "'%s' is not a plex", plex);
  558                 return;
  559         }
  560         p = gv_find_plex(sc, plex);
  561 
  562         if (p->state != GV_PLEX_UP) {
  563                 gctl_error(req, "plex %s is not completely accessible",
  564                     p->name);
  565                 return;
  566         }
  567 
  568         if (p->org != GV_PLEX_RAID5) {
  569                 gctl_error(req, "plex %s is not a RAID5 plex", p->name);
  570                 return;
  571         }
  572 
  573         /* Put it in the event queue. */
  574         /* XXX: The state of the plex might have changed when this event is
  575          * picked up ... We should perhaps check this afterwards. */
  576         if (*rebuild)
  577                 gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0);
  578         else
  579                 gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0);
  580 }
  581 
  582 
  583 static struct g_geom *
  584 gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
  585 {
  586         struct g_geom *gp;
  587         struct g_consumer *cp;
  588         struct gv_softc *sc;
  589         struct gv_hdr vhdr;
  590         int error;
  591 
  592         g_topology_assert();
  593         g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name);
  594 
  595         gp = LIST_FIRST(&mp->geom);
  596         if (gp == NULL) {
  597                 G_VINUM_DEBUG(0, "error: tasting, but not initialized?");
  598                 return (NULL);
  599         }
  600         sc = gp->softc;
  601 
  602         cp = g_new_consumer(gp);
  603         if (g_attach(cp, pp) != 0) {
  604                 g_destroy_consumer(cp);
  605                 return (NULL);
  606         }
  607         if (g_access(cp, 1, 0, 0) != 0) {
  608                 g_detach(cp);
  609                 g_destroy_consumer(cp);
  610                 return (NULL);
  611         }
  612         g_topology_unlock();
  613 
  614         error = gv_read_header(cp, &vhdr);
  615 
  616         g_topology_lock();
  617         g_access(cp, -1, 0, 0);
  618         g_detach(cp);
  619         g_destroy_consumer(cp);
  620 
  621         /* Check if what we've been given is a valid vinum drive. */
  622         if (!error)
  623                 gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0);
  624 
  625         return (NULL);
  626 }
  627 
  628 void
  629 gv_worker(void *arg)
  630 {
  631         struct g_provider *pp;
  632         struct gv_softc *sc;
  633         struct gv_event *ev;
  634         struct gv_volume *v;
  635         struct gv_plex *p;
  636         struct gv_sd *s;
  637         struct gv_drive *d;
  638         struct bio *bp;
  639         int newstate, flags, err, rename;
  640         char *newname;
  641         off_t offset;
  642 
  643         sc = arg;
  644         KASSERT(sc != NULL, ("NULL sc"));
  645         for (;;) {
  646                 /* Look at the events first... */
  647                 ev = gv_get_event(sc);
  648                 if (ev != NULL) {
  649                         gv_remove_event(sc, ev);
  650 
  651                         switch (ev->type) {
  652                         case GV_EVENT_DRIVE_TASTED:
  653                                 G_VINUM_DEBUG(2, "event 'drive tasted'");
  654                                 pp = ev->arg1;
  655                                 gv_drive_tasted(sc, pp);
  656                                 break;
  657 
  658                         case GV_EVENT_DRIVE_LOST:
  659                                 G_VINUM_DEBUG(2, "event 'drive lost'");
  660                                 d = ev->arg1;
  661                                 gv_drive_lost(sc, d);
  662                                 break;
  663 
  664                         case GV_EVENT_CREATE_DRIVE:
  665                                 G_VINUM_DEBUG(2, "event 'create drive'");
  666                                 d = ev->arg1;
  667                                 gv_create_drive(sc, d);
  668                                 break;
  669 
  670                         case GV_EVENT_CREATE_VOLUME:
  671                                 G_VINUM_DEBUG(2, "event 'create volume'");
  672                                 v = ev->arg1;
  673                                 gv_create_volume(sc, v);
  674                                 break;
  675 
  676                         case GV_EVENT_CREATE_PLEX:
  677                                 G_VINUM_DEBUG(2, "event 'create plex'");
  678                                 p = ev->arg1;
  679                                 gv_create_plex(sc, p);
  680                                 break;
  681 
  682                         case GV_EVENT_CREATE_SD:
  683                                 G_VINUM_DEBUG(2, "event 'create sd'");
  684                                 s = ev->arg1;
  685                                 gv_create_sd(sc, s);
  686                                 break;
  687 
  688                         case GV_EVENT_RM_DRIVE:
  689                                 G_VINUM_DEBUG(2, "event 'remove drive'");
  690                                 d = ev->arg1;
  691                                 flags = ev->arg3;
  692                                 gv_rm_drive(sc, d, flags);
  693                                 /*gv_setup_objects(sc);*/
  694                                 break;
  695 
  696                         case GV_EVENT_RM_VOLUME:
  697                                 G_VINUM_DEBUG(2, "event 'remove volume'");
  698                                 v = ev->arg1;
  699                                 gv_rm_vol(sc, v);
  700                                 /*gv_setup_objects(sc);*/
  701                                 break;
  702 
  703                         case GV_EVENT_RM_PLEX:
  704                                 G_VINUM_DEBUG(2, "event 'remove plex'");
  705                                 p = ev->arg1;
  706                                 gv_rm_plex(sc, p);
  707                                 /*gv_setup_objects(sc);*/
  708                                 break;
  709 
  710                         case GV_EVENT_RM_SD:
  711                                 G_VINUM_DEBUG(2, "event 'remove sd'");
  712                                 s = ev->arg1;
  713                                 gv_rm_sd(sc, s);
  714                                 /*gv_setup_objects(sc);*/
  715                                 break;
  716 
  717                         case GV_EVENT_SAVE_CONFIG:
  718                                 G_VINUM_DEBUG(2, "event 'save config'");
  719                                 gv_save_config(sc);
  720                                 break;
  721 
  722                         case GV_EVENT_SET_SD_STATE:
  723                                 G_VINUM_DEBUG(2, "event 'setstate sd'");
  724                                 s = ev->arg1;
  725                                 newstate = ev->arg3;
  726                                 flags = ev->arg4;
  727                                 err = gv_set_sd_state(s, newstate, flags);
  728                                 if (err)
  729                                         G_VINUM_DEBUG(0, "error setting subdisk"
  730                                             " state: error code %d", err);
  731                                 break;
  732 
  733                         case GV_EVENT_SET_DRIVE_STATE:
  734                                 G_VINUM_DEBUG(2, "event 'setstate drive'");
  735                                 d = ev->arg1;
  736                                 newstate = ev->arg3;
  737                                 flags = ev->arg4;
  738                                 err = gv_set_drive_state(d, newstate, flags);
  739                                 if (err)
  740                                         G_VINUM_DEBUG(0, "error setting drive "
  741                                             "state: error code %d", err);
  742                                 break;
  743 
  744                         case GV_EVENT_SET_VOL_STATE:
  745                                 G_VINUM_DEBUG(2, "event 'setstate volume'");
  746                                 v = ev->arg1;
  747                                 newstate = ev->arg3;
  748                                 flags = ev->arg4;
  749                                 err = gv_set_vol_state(v, newstate, flags);
  750                                 if (err)
  751                                         G_VINUM_DEBUG(0, "error setting volume "
  752                                             "state: error code %d", err);
  753                                 break;
  754 
  755                         case GV_EVENT_SET_PLEX_STATE:
  756                                 G_VINUM_DEBUG(2, "event 'setstate plex'");
  757                                 p = ev->arg1;
  758                                 newstate = ev->arg3;
  759                                 flags = ev->arg4;
  760                                 err = gv_set_plex_state(p, newstate, flags);
  761                                 if (err)
  762                                         G_VINUM_DEBUG(0, "error setting plex "
  763                                             "state: error code %d", err);
  764                                 break;
  765 
  766                         case GV_EVENT_SETUP_OBJECTS:
  767                                 G_VINUM_DEBUG(2, "event 'setup objects'");
  768                                 gv_setup_objects(sc);
  769                                 break;
  770 
  771                         case GV_EVENT_RESET_CONFIG:
  772                                 G_VINUM_DEBUG(2, "event 'resetconfig'");
  773                                 err = gv_resetconfig(sc);
  774                                 if (err)
  775                                         G_VINUM_DEBUG(0, "error resetting "
  776                                             "config: error code %d", err);
  777                                 break;
  778 
  779                         case GV_EVENT_PARITY_REBUILD:
  780                                 /*
  781                                  * Start the rebuild. The gv_plex_done will
  782                                  * handle issuing of the remaining rebuild bio's
  783                                  * until it's finished. 
  784                                  */
  785                                 G_VINUM_DEBUG(2, "event 'rebuild'");
  786                                 p = ev->arg1;
  787                                 if (p->state != GV_PLEX_UP) {
  788                                         G_VINUM_DEBUG(0, "plex %s is not "
  789                                             "completely accessible", p->name);
  790                                         break;
  791                                 }
  792                                 p->synced = 0;
  793                                 g_topology_assert_not();
  794                                 g_topology_lock();
  795                                 err = gv_access(p->vol_sc->provider, 1, 1, 0);
  796                                 if (err) {
  797                                         G_VINUM_DEBUG(0, "unable to access "
  798                                             "provider");
  799                                         break;
  800                                 }
  801                                 g_topology_unlock();
  802                                 gv_parity_request(p, GV_BIO_CHECK |
  803                                     GV_BIO_PARITY, 0);
  804                                 break;
  805 
  806                         case GV_EVENT_PARITY_CHECK:
  807                                 /* Start parity check. */
  808                                 G_VINUM_DEBUG(2, "event 'check'");
  809                                 p = ev->arg1;
  810                                 if (p->state != GV_PLEX_UP) {
  811                                         G_VINUM_DEBUG(0, "plex %s is not "
  812                                             "completely accessible", p->name);
  813                                         break;
  814                                 }
  815                                 p->synced = 0;
  816                                 g_topology_assert_not();
  817                                 g_topology_lock();
  818                                 err = gv_access(p->vol_sc->provider, 1, 1, 0);
  819                                 if (err) {
  820                                         G_VINUM_DEBUG(0, "unable to access "
  821                                             "provider");
  822                                         break;
  823                                 }
  824                                 g_topology_unlock();
  825                                 gv_parity_request(p, GV_BIO_CHECK, 0);
  826                                 break;
  827 
  828                         case GV_EVENT_START_PLEX:
  829                                 G_VINUM_DEBUG(2, "event 'start' plex");
  830                                 p = ev->arg1;
  831                                 gv_start_plex(p);
  832                                 break;
  833 
  834                         case GV_EVENT_START_VOLUME:
  835                                 G_VINUM_DEBUG(2, "event 'start' volume");
  836                                 v = ev->arg1;
  837                                 gv_start_vol(v);
  838                                 break;
  839 
  840                         case GV_EVENT_ATTACH_PLEX:
  841                                 G_VINUM_DEBUG(2, "event 'attach' plex");
  842                                 p = ev->arg1;
  843                                 v = ev->arg2;
  844                                 rename = ev->arg4;
  845                                 err = gv_attach_plex(p, v, rename);
  846                                 if (err)
  847                                         G_VINUM_DEBUG(0, "error attaching %s to"
  848                                             " %s: error code %d", p->name,
  849                                             v->name, err);
  850                                 break;
  851 
  852                         case GV_EVENT_ATTACH_SD:
  853                                 G_VINUM_DEBUG(2, "event 'attach' sd");
  854                                 s = ev->arg1;
  855                                 p = ev->arg2;
  856                                 offset = ev->arg3;
  857                                 rename = ev->arg4;
  858                                 err = gv_attach_sd(s, p, offset, rename);
  859                                 if (err)
  860                                         G_VINUM_DEBUG(0, "error attaching %s to"
  861                                             " %s: error code %d", s->name,
  862                                             p->name, err);
  863                                 break;
  864 
  865                         case GV_EVENT_DETACH_PLEX:
  866                                 G_VINUM_DEBUG(2, "event 'detach' plex");
  867                                 p = ev->arg1;
  868                                 flags = ev->arg3;
  869                                 err = gv_detach_plex(p, flags);
  870                                 if (err)
  871                                         G_VINUM_DEBUG(0, "error detaching %s: "
  872                                             "error code %d", p->name, err);
  873                                 break;
  874 
  875                         case GV_EVENT_DETACH_SD:
  876                                 G_VINUM_DEBUG(2, "event 'detach' sd");
  877                                 s = ev->arg1;
  878                                 flags = ev->arg3;
  879                                 err = gv_detach_sd(s, flags);
  880                                 if (err)
  881                                         G_VINUM_DEBUG(0, "error detaching %s: "
  882                                             "error code %d", s->name, err);
  883                                 break;
  884 
  885                         case GV_EVENT_RENAME_VOL:
  886                                 G_VINUM_DEBUG(2, "event 'rename' volume");
  887                                 v = ev->arg1;
  888                                 newname = ev->arg2;
  889                                 flags = ev->arg3;
  890                                 err = gv_rename_vol(sc, v, newname, flags);
  891                                 if (err)
  892                                         G_VINUM_DEBUG(0, "error renaming %s to "
  893                                             "%s: error code %d", v->name,
  894                                             newname, err);
  895                                 g_free(newname);
  896                                 /* Destroy and recreate the provider if we can. */
  897                                 if (gv_provider_is_open(v->provider)) {
  898                                         G_VINUM_DEBUG(0, "unable to rename "
  899                                             "provider to %s: provider in use",
  900                                             v->name);
  901                                         break;
  902                                 }
  903                                 g_topology_lock();
  904                                 g_wither_provider(v->provider, ENOENT);
  905                                 g_topology_unlock();
  906                                 v->provider = NULL;
  907                                 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc,
  908                                     NULL, 0, 0);
  909                                 break;
  910 
  911                         case GV_EVENT_RENAME_PLEX:
  912                                 G_VINUM_DEBUG(2, "event 'rename' plex");
  913                                 p = ev->arg1;
  914                                 newname = ev->arg2;
  915                                 flags = ev->arg3;
  916                                 err = gv_rename_plex(sc, p, newname, flags);
  917                                 if (err)
  918                                         G_VINUM_DEBUG(0, "error renaming %s to "
  919                                             "%s: error code %d", p->name,
  920                                             newname, err);
  921                                 g_free(newname);
  922                                 break;
  923 
  924                         case GV_EVENT_RENAME_SD:
  925                                 G_VINUM_DEBUG(2, "event 'rename' sd");
  926                                 s = ev->arg1;
  927                                 newname = ev->arg2;
  928                                 flags = ev->arg3;
  929                                 err = gv_rename_sd(sc, s, newname, flags);
  930                                 if (err)
  931                                         G_VINUM_DEBUG(0, "error renaming %s to "
  932                                             "%s: error code %d", s->name,
  933                                             newname, err);
  934                                 g_free(newname);
  935                                 break;
  936 
  937                         case GV_EVENT_RENAME_DRIVE:
  938                                 G_VINUM_DEBUG(2, "event 'rename' drive");
  939                                 d = ev->arg1;
  940                                 newname = ev->arg2;
  941                                 flags = ev->arg3;
  942                                 err = gv_rename_drive(sc, d, newname, flags);
  943                                 if (err)
  944                                         G_VINUM_DEBUG(0, "error renaming %s to "
  945                                             "%s: error code %d", d->name,
  946                                             newname, err);
  947                                 g_free(newname);
  948                                 break;
  949 
  950                         case GV_EVENT_MOVE_SD:
  951                                 G_VINUM_DEBUG(2, "event 'move' sd");
  952                                 s = ev->arg1;
  953                                 d = ev->arg2;
  954                                 flags = ev->arg3;
  955                                 err = gv_move_sd(sc, s, d, flags);
  956                                 if (err)
  957                                         G_VINUM_DEBUG(0, "error moving %s to "
  958                                             "%s: error code %d", s->name,
  959                                             d->name, err);
  960                                 break;
  961 
  962                         case GV_EVENT_THREAD_EXIT:
  963                                 G_VINUM_DEBUG(2, "event 'thread exit'");
  964                                 g_free(ev);
  965                                 mtx_lock(&sc->equeue_mtx);
  966                                 mtx_lock(&sc->bqueue_mtx);
  967                                 gv_cleanup(sc);
  968                                 mtx_destroy(&sc->bqueue_mtx);
  969                                 mtx_destroy(&sc->equeue_mtx);
  970                                 g_free(sc->bqueue_down);
  971                                 g_free(sc->bqueue_up);
  972                                 g_free(sc);
  973                                 kproc_exit(ENXIO);
  974                                 break;                  /* not reached */
  975 
  976                         default:
  977                                 G_VINUM_DEBUG(1, "unknown event %d", ev->type);
  978                         }
  979 
  980                         g_free(ev);
  981                         continue;
  982                 }
  983 
  984                 /* ... then do I/O processing. */
  985                 mtx_lock(&sc->bqueue_mtx);
  986                 /* First do new requests. */
  987                 bp = bioq_takefirst(sc->bqueue_down);
  988                 if (bp != NULL) {
  989                         mtx_unlock(&sc->bqueue_mtx);
  990                         /* A bio that interfered with another bio. */
  991                         if (bp->bio_pflags & GV_BIO_ONHOLD) {
  992                                 s = bp->bio_caller1;
  993                                 p = s->plex_sc;
  994                                 /* Is it still locked out? */
  995                                 if (gv_stripe_active(p, bp)) {
  996                                         /* Park the bio on the waiting queue. */
  997                                         bioq_disksort(p->wqueue, bp);
  998                                 } else {
  999                                         bp->bio_pflags &= ~GV_BIO_ONHOLD;
 1000                                         g_io_request(bp, s->drive_sc->consumer);
 1001                                 }
 1002                         /* A special request requireing special handling. */
 1003                         } else if (bp->bio_pflags & GV_BIO_INTERNAL) {
 1004                                 p = bp->bio_caller1;
 1005                                 gv_plex_start(p, bp);
 1006                         } else {
 1007                                 gv_volume_start(sc, bp);
 1008                         }
 1009                         mtx_lock(&sc->bqueue_mtx);
 1010                 }
 1011                 /* Then do completed requests. */
 1012                 bp = bioq_takefirst(sc->bqueue_up);
 1013                 if (bp == NULL) {
 1014                         msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10);
 1015                         mtx_unlock(&sc->bqueue_mtx);
 1016                         continue;
 1017                 }
 1018                 mtx_unlock(&sc->bqueue_mtx);
 1019                 gv_bio_done(sc, bp);
 1020         }
 1021 }
 1022 
 1023 #define VINUM_CLASS_NAME "VINUM"
 1024 
 1025 static struct g_class g_vinum_class     = {
 1026         .name = VINUM_CLASS_NAME,
 1027         .version = G_VERSION,
 1028         .init = gv_init,
 1029         .taste = gv_taste,
 1030         .ctlreq = gv_config,
 1031         .destroy_geom = gv_unload,
 1032 };
 1033 
 1034 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);

Cache object: a2932741b59d824b7d68bbf1b6159ef0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.