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/dev/vinum/vinumconfig.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  * To do:
    3  *
    4  * Don't store drive configuration on the config DB: read each drive's header
    5  * to decide where it is.
    6  *
    7  * Accept any old crap in the config_<foo> functions, and complain when
    8  * we try to bring it up.
    9  *
   10  * When trying to bring volumes up, check that the complete address range
   11  * is covered.
   12  */
   13 /*-
   14  * Copyright (c) 1997, 1998
   15  *      Nan Yang Computer Services Limited.  All rights reserved.
   16  *
   17  *  This software is distributed under the so-called ``Berkeley
   18  *  License'':
   19  *
   20  * Redistribution and use in source and binary forms, with or without
   21  * modification, are permitted provided that the following conditions
   22  * are met:
   23  * 1. Redistributions of source code must retain the above copyright
   24  *    notice, this list of conditions and the following disclaimer.
   25  * 2. Redistributions in binary form must reproduce the above copyright
   26  *    notice, this list of conditions and the following disclaimer in the
   27  *    documentation and/or other materials provided with the distribution.
   28  * 3. All advertising materials mentioning features or use of this software
   29  *    must display the following acknowledgement:
   30  *      This product includes software developed by Nan Yang Computer
   31  *      Services Limited.
   32  * 4. Neither the name of the Company nor the names of its contributors
   33  *    may be used to endorse or promote products derived from this software
   34  *    without specific prior written permission.
   35  *
   36  * This software is provided ``as is'', and any express or implied
   37  * warranties, including, but not limited to, the implied warranties of
   38  * merchantability and fitness for a particular purpose are disclaimed.
   39  * In no event shall the company or contributors be liable for any
   40  * direct, indirect, incidental, special, exemplary, or consequential
   41  * damages (including, but not limited to, procurement of substitute
   42  * goods or services; loss of use, data, or profits; or business
   43  * interruption) however caused and on any theory of liability, whether
   44  * in contract, strict liability, or tort (including negligence or
   45  * otherwise) arising in any way out of the use of this software, even if
   46  * advised of the possibility of such damage.
   47  *
   48  * $Id: vinumconfig.c,v 1.30 2000/05/01 09:45:50 grog Exp grog $
   49  * $FreeBSD$
   50  */
   51 
   52 #define STATIC static
   53 
   54 #include <dev/vinum/vinumhdr.h>
   55 #include <dev/vinum/request.h>
   56 
   57 #define MAXTOKEN 64                                         /* maximum number of tokens in a line */
   58 
   59 /*
   60  * We can afford the luxury of global variables here,
   61  * since start_config ensures that these functions
   62  * are single-threaded.
   63  */
   64 
   65 /* These are indices in vinum_conf of the last-mentioned of each kind of object */
   66 static int current_drive;                                   /* note the last drive we mention, for
   67                                                             * some defaults */
   68 static int current_plex;                                    /* and the same for the last plex */
   69 static int current_volume;                                  /* and the last volme */
   70 static struct _ioctl_reply *ioctl_reply;                    /* struct to return via ioctl */
   71 
   72 
   73 /* These values are used by most of these routines, so set them as globals */
   74 static char *token[MAXTOKEN];                               /* pointers to individual tokens */
   75 static int tokens;                                          /* number of tokens */
   76 
   77 #define TOCONS  0x01
   78 #define TOTTY   0x02
   79 #define TOLOG   0x04
   80 
   81 struct putchar_arg {
   82     int flags;
   83     struct tty *tty;
   84 };
   85 
   86 #define MSG_MAX 1024                                        /* maximum length of a formatted message */
   87 /*
   88  * Format an error message and return to the user in the reply.
   89  * CARE: This routine is designed to be called only from the
   90  * configuration routines, so it assumes it's the owner of
   91  * the configuration lock, and unlocks it on exit
   92  */
   93 void
   94 throw_rude_remark(int error, char *msg,...)
   95 {
   96     int retval;
   97     va_list ap;
   98     char *text;
   99     static int finishing;                                   /* don't recurse */
  100     int was_finishing;
  101 
  102     if ((vinum_conf.flags & VF_LOCKED) == 0)                /* bug catcher */
  103         panic ("throw_rude_remark: called without config lock");
  104     va_start(ap, msg);
  105     if ((ioctl_reply != NULL)                               /* we're called from the user */
  106     &&(!(vinum_conf.flags & VF_READING_CONFIG))) {          /* and not reading from disk: return msg */
  107         /*
  108          * We can't just format to ioctl_reply, since it
  109          * may contain our input parameters
  110          */
  111         text = Malloc(MSG_MAX);
  112         if (text == NULL) {
  113             log(LOG_ERR, "vinum: can't allocate error message buffer\n");
  114             printf("vinum: ");
  115             vprintf(msg, ap);                               /* print to the console */
  116             printf("\n");
  117         } else {
  118             retval = kvprintf(msg, NULL, (void *) text, 10, ap);
  119             text[retval] = '\0';                            /* delimit */
  120             strcpy(ioctl_reply->msg, text);
  121             ioctl_reply->error = error;                     /* first byte is the error number */
  122             Free(text);
  123         }
  124     } else {
  125         printf("vinum: ");
  126         vprintf(msg, ap);                                   /* print to the console */
  127         printf("\n");
  128     }
  129     va_end(ap);
  130 
  131     if (vinum_conf.flags & VF_READING_CONFIG) {             /* go through to the bitter end, */
  132         if ((vinum_conf.flags & VF_READING_CONFIG)          /* we're reading from disk, */
  133         &&((daemon_options & daemon_noupdate) == 0)) {
  134             log(LOG_NOTICE, "Disabling configuration updates\n");
  135             daemon_options |= daemon_noupdate;
  136         }
  137         return;
  138     }
  139     /*
  140      * We have a problem here: we want to unlock the
  141      * configuration, which implies tidying up, but
  142      * if we find an error while tidying up, we could
  143      * recurse for ever.  Use this kludge to only try
  144      * once
  145      */
  146     was_finishing = finishing;
  147     finishing = 1;
  148     finish_config(was_finishing);                           /* unlock anything we may be holding */
  149     finishing = was_finishing;
  150     longjmp(command_fail, error);
  151 }
  152 
  153 /*
  154  * Check a volume to see if the plex is already assigned to it.
  155  * Return index in volume->plex, or -1 if not assigned
  156  */
  157 int
  158 my_plex(int volno, int plexno)
  159 {
  160     int i;
  161     struct volume *vol;
  162 
  163     vol = &VOL[volno];                                      /* point to volno */
  164     for (i = 0; i < vol->plexes; i++)
  165         if (vol->plex[i] == plexno)
  166             return i;
  167     return -1;                                              /* not found */
  168 }
  169 
  170 /*
  171  * Check a plex to see if the subdisk is already assigned to it.
  172  * Return index in plex->sd, or -1 if not assigned
  173  */
  174 int
  175 my_sd(int plexno, int sdno)
  176 {
  177     int i;
  178     struct plex *plex;
  179 
  180     plex = &PLEX[plexno];
  181     for (i = 0; i < plex->subdisks; i++)
  182         if (plex->sdnos[i] == sdno)
  183             return i;
  184     return -1;                                              /* not found */
  185 }
  186 
  187 /* Add plex to the volume if possible */
  188 int
  189 give_plex_to_volume(int volno, int plexno)
  190 {
  191     struct volume *vol;
  192 
  193     /*
  194      * It's not an error for the plex to already
  195      * belong to the volume, but we need to check a
  196      * number of things to make sure it's done right.
  197      * Some day.
  198      */
  199     if (my_plex(volno, plexno) >= 0)
  200         return plexno;                                      /* that's it */
  201 
  202     vol = &VOL[volno];                                      /* point to volume */
  203     if (vol->plexes == MAXPLEX)                             /* all plexes allocated */
  204         throw_rude_remark(ENOSPC,
  205             "Too many plexes for volume %s",
  206             vol->name);
  207     else if ((vol->plexes > 0)                              /* we have other plexes */
  208     &&((vol->flags & VF_CONFIG_SETUPSTATE) == 0))           /* and we're not setting up state */
  209         invalidate_subdisks(&PLEX[plexno], sd_stale);       /* make the subdisks invalid */
  210     vol->plex[vol->plexes] = plexno;                        /* this one */
  211     vol->plexes++;                                          /* add another plex */
  212     PLEX[plexno].volno = volno;                             /* note the number of our volume */
  213 
  214     return vol->plexes - 1;                                 /* and return its index */
  215 }
  216 
  217 /*
  218  * Add subdisk to a plex if possible
  219  */
  220 int
  221 give_sd_to_plex(int plexno, int sdno)
  222 {
  223     int i;
  224     struct plex *plex;
  225     struct sd *sd;
  226 
  227     /*
  228      * It's not an error for the sd to already
  229      * belong to the plex, but we need to check a
  230      * number of things to make sure it's done right.
  231      * Some day.
  232      */
  233     i = my_sd(plexno, sdno);
  234     if (i >= 0)                                             /* does it already belong to us? */
  235         return i;                                           /* that's it */
  236 
  237     plex = &PLEX[plexno];                                   /* point to the plex */
  238     sd = &SD[sdno];                                         /* and the subdisk */
  239 
  240     /* Do we have an offset?  Otherwise put it after the last one */
  241     if (sd->plexoffset < 0) {                               /* no offset specified */
  242         if (plex->subdisks > 0) {
  243             struct sd *lastsd = &SD[plex->sdnos[plex->subdisks - 1]]; /* last subdisk */
  244 
  245             if (plex->organization == plex_concat)          /* concat, */
  246                 sd->plexoffset = lastsd->sectors + lastsd->plexoffset; /* starts here */
  247             else                                            /* striped, RAID-4 or RAID-5 */
  248                 sd->plexoffset = plex->stripesize * plex->subdisks; /* starts here */
  249         } else                                              /* first subdisk */
  250             sd->plexoffset = 0;                             /* start at the beginning */
  251     }
  252     if (plex->subdisks == MAXSD)                            /* we already have our maximum */
  253         throw_rude_remark(ENOSPC,                           /* crap out */
  254             "Can't add %s to %s: plex full",
  255             sd->name,
  256             plex->name);
  257 
  258     plex->subdisks++;                                       /* another entry */
  259     if (plex->subdisks >= plex->subdisks_allocated)         /* need more space */
  260         EXPAND(plex->sdnos, int, plex->subdisks_allocated, INITIAL_SUBDISKS_IN_PLEX);
  261 
  262     /* Adjust size of plex and volume. */
  263     if (isparity(plex))                                     /* RAID-4 or RAID-5 */
  264         plex->length = (plex->subdisks - 1) * sd->sectors;  /* size is one disk short */
  265     else
  266         plex->length += sd->sectors;                        /* plex gets this much bigger */
  267     if (plex->volno >= 0)                                   /* we have a volume */
  268         VOL[plex->volno].size = max(VOL[plex->volno].size, plex->length); /* adjust its size */
  269 
  270     /*
  271      * We need to check that the subdisks don't overlap,
  272      * but we can't do that until a point where we *must*
  273      * know the size of all the subdisks.  That's not
  274      * here.  But we need to sort them by offset
  275      */
  276     for (i = 0; i < plex->subdisks - 1; i++) {
  277         if (sd->plexoffset < SD[plex->sdnos[i]].plexoffset) { /* it fits before this one */
  278             /* First move any remaining subdisks by one */
  279             int j;
  280 
  281             for (j = plex->subdisks - 1; j > i; j--)        /* move up one at a time */
  282                 plex->sdnos[j] = plex->sdnos[j - 1];
  283             plex->sdnos[i] = sdno;
  284             sd->plexsdno = i;                               /* note where we are in the subdisk */
  285             return i;
  286         }
  287     }
  288 
  289     /*
  290      * The plex doesn't have any subdisk with a
  291      * larger offset.  Insert it here.
  292      */
  293     plex->sdnos[i] = sdno;
  294     sd->plexsdno = i;                                       /* note where we are in the subdisk */
  295     sd->plexno = plex->plexno;                              /* and who we belong to */
  296     return i;
  297 }
  298 
  299 /*
  300  * Add a subdisk to drive if possible.  The
  301  * pointer to the drive must already be stored in
  302  * the sd structure, but the drive doesn't know
  303  * about the subdisk yet.
  304  */
  305 void
  306 give_sd_to_drive(int sdno)
  307 {
  308     struct sd *sd;                                          /* pointer to subdisk */
  309     struct drive *drive;                                    /* and drive */
  310     int fe;                                                 /* index in free list */
  311     int sfe;                                                /* and index of subdisk when assigning max */
  312 
  313     sd = &SD[sdno];                                         /* point to sd */
  314     drive = &DRIVE[sd->driveno];                            /* and drive */
  315 
  316     if (drive->state != drive_up) {
  317         update_sd_state(sdno);                              /* that crashes the subdisk */
  318         return;
  319     }
  320     if (drive->flags & VF_HOTSPARE)                         /* the drive is a hot spare, */
  321         throw_rude_remark(ENOSPC,
  322             "Can't place %s on hot spare drive %s",
  323             sd->name,
  324             drive->label.name);
  325     if ((drive->sectors_available == 0)                     /* no space left */
  326     ||(sd->sectors > drive->sectors_available)) {           /* or too big, */
  327         sd->driveoffset = -1;                               /* don't be confusing */
  328         free_sd(sd->sdno);
  329         throw_rude_remark(ENOSPC, "No space for %s on %s", sd->name, drive->label.name);
  330         return;                                             /* in case we come back here */
  331     }
  332     drive->subdisks_used++;                                 /* one more subdisk */
  333 
  334     if (sd->sectors == 0) {                                 /* take the largest chunk */
  335         sfe = 0;                                            /* to keep the compiler happy */
  336         for (fe = 0; fe < drive->freelist_entries; fe++) {
  337             if (drive->freelist[fe].sectors >= sd->sectors) { /* more space here */
  338                 sd->sectors = drive->freelist[fe].sectors;  /* take it */
  339                 sd->driveoffset = drive->freelist[fe].offset;
  340                 sfe = fe;                                   /* and note the index for later */
  341             }
  342         }
  343         if (sd->sectors == 0) {                             /* no luck, */
  344             sd->driveoffset = -1;                           /* don't be confusing */
  345             free_sd(sd->sdno);
  346             throw_rude_remark(ENOSPC,                       /* give up */
  347                 "No space for %s on %s",
  348                 sd->name,
  349                 drive->label.name);
  350         }
  351         if (sfe < (drive->freelist_entries - 1))            /* not the last one, */
  352             bcopy(&drive->freelist[sfe + 1],
  353                 &drive->freelist[sfe],
  354                 (drive->freelist_entries - sfe) * sizeof(struct drive_freelist));
  355         drive->freelist_entries--;                          /* one less entry */
  356         drive->sectors_available -= sd->sectors;            /* and note how much less space we have */
  357     } else if (sd->driveoffset < 0) {                       /* no offset specified, find one */
  358         for (fe = 0; fe < drive->freelist_entries; fe++) {
  359             if (drive->freelist[fe].sectors >= sd->sectors) { /* it'll fit here */
  360                 sd->driveoffset = drive->freelist[fe].offset;
  361                 if (sd->sectors == drive->freelist[fe].sectors) { /* used up the entire entry */
  362                     if (fe < (drive->freelist_entries - 1)) /* not the last one, */
  363                         bcopy(&drive->freelist[fe + 1],
  364                             &drive->freelist[fe],
  365                             (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
  366                     drive->freelist_entries--;              /* one less entry */
  367                 } else {
  368                     drive->freelist[fe].sectors -= sd->sectors; /* this much less space */
  369                     drive->freelist[fe].offset += sd->sectors; /* this much further on */
  370                 }
  371                 drive->sectors_available -= sd->sectors;    /* and note how much less space we have */
  372                 break;
  373             }
  374         }
  375         if (sd->driveoffset < 0)
  376             /*
  377              * Didn't find anything.  Although the drive has
  378              * enough space, it's too fragmented
  379              */
  380         {
  381             free_sd(sd->sdno);
  382             throw_rude_remark(ENOSPC, "No space for %s on %s", sd->name, drive->label.name);
  383         }
  384     } else {                                                /* specific offset */
  385         /*
  386          * For a specific offset to work, the space must be
  387          * entirely in a single freelist entry.  Look for it.
  388          */
  389         u_int64_t sdend = sd->driveoffset + sd->sectors;    /* end of our subdisk */
  390         for (fe = 0; fe < drive->freelist_entries; fe++) {
  391             u_int64_t dend = drive->freelist[fe].offset + drive->freelist[fe].sectors; /* end of entry */
  392             if (dend >= sdend) {                            /* fits before here */
  393                 if (drive->freelist[fe].offset > sd->driveoffset) { /* starts after the beginning of sd area */
  394                     sd->driveoffset = -1;                   /* don't be confusing */
  395                     set_sd_state(sd->sdno, sd_down, setstate_force);
  396                     throw_rude_remark(ENOSPC,
  397                         "No space for %s on drive %s at offset %lld",
  398                         sd->name,
  399                         drive->label.name,
  400                         sd->driveoffset);
  401                     return;
  402                 }
  403                 /*
  404                  * We've found the space, and we can allocate it.
  405                  * We don't need to say that to the subdisk, which
  406                  * already knows about it.  We need to tell it to
  407                  * the free list, though.  We have four possibilities:
  408                  *
  409                  * 1.  The subdisk exactly eats up the entry.  That's the
  410                  *     same as above.
  411                  * 2.  The subdisk starts at the beginning and leaves space
  412                  *     at the end.
  413                  * 3.  The subdisk starts after the beginning and leaves
  414                  *     space at the end as well: we end up with another
  415                  *     fragment.
  416                  * 4.  The subdisk leaves space at the beginning and finishes
  417                  *     at the end.
  418                  */
  419                 drive->sectors_available -= sd->sectors;    /* note how much less space we have */
  420                 if (sd->driveoffset == drive->freelist[fe].offset) { /* 1 or 2 */
  421                     if (sd->sectors == drive->freelist[fe].sectors) { /* 1: used up the entire entry */
  422                         if (fe < (drive->freelist_entries - 1)) /* not the last one, */
  423                             bcopy(&drive->freelist[fe + 1],
  424                                 &drive->freelist[fe],
  425                                 (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
  426                         drive->freelist_entries--;          /* one less entry */
  427                     } else {                                /* 2: space at the end */
  428                         drive->freelist[fe].sectors -= sd->sectors; /* this much less space */
  429                         drive->freelist[fe].offset += sd->sectors; /* this much further on */
  430                     }
  431                 } else {                                    /* 3 or 4 */
  432                     drive->freelist[fe].sectors = sd->driveoffset - drive->freelist[fe].offset;
  433                     if (dend > sdend) {                     /* 3: space at the end as well */
  434                         if (fe < (drive->freelist_entries - 1)) /* not the last one */
  435                             bcopy(&drive->freelist[fe],     /* move the rest down */
  436                                 &drive->freelist[fe + 1],
  437                                 (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
  438                         drive->freelist_entries++;          /* one less entry */
  439                         drive->freelist[fe + 1].offset = sdend; /* second entry starts after sd */
  440                         drive->freelist[fe + 1].sectors = dend - sdend; /* and is this long */
  441                     }
  442                 }
  443                 break;
  444             }
  445         }
  446     }
  447     drive->opencount++;                                     /* one more subdisk attached */
  448 }
  449 
  450 /* Get an empty drive entry from the drive table */
  451 int
  452 get_empty_drive(void)
  453 {
  454     int driveno;
  455     struct drive *drive;
  456 
  457     /* first see if we have one which has been deallocated */
  458     for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
  459         if (DRIVE[driveno].state == drive_unallocated)      /* bingo */
  460             break;
  461     }
  462 
  463     if (driveno >= vinum_conf.drives_allocated)             /* we've used all our allocation */
  464         EXPAND(DRIVE, struct drive, vinum_conf.drives_allocated, INITIAL_DRIVES);
  465 
  466     /* got a drive entry.  Make it pretty */
  467     drive = &DRIVE[driveno];
  468     bzero(drive, sizeof(struct drive));
  469     drive->driveno = driveno;                               /* put number in structure */
  470     drive->flags |= VF_NEWBORN;                             /* newly born drive */
  471     strcpy("unknown", drive->devicename);                   /* and make the name ``unknown'' */
  472     return driveno;                                         /* return the index */
  473 }
  474 
  475 /*
  476  * Find the named drive in vinum_conf.drive, return a pointer
  477  * return the index in vinum_conf.drive.
  478  * Don't mark the drive as allocated (XXX SMP)
  479  * If create != 0, create an entry if it doesn't exist
  480  */
  481 /* XXX check if we have it open from attach */
  482 int
  483 find_drive(const char *name, int create)
  484 {
  485     int driveno;
  486     struct drive *drive;
  487 
  488     if (name != NULL) {
  489         for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
  490             drive = &DRIVE[driveno];                        /* point to drive */
  491             if ((drive->label.name[0] != '\0')              /* it has a name */
  492 &&(strcmp(drive->label.name, name) == 0)                    /* and it's this one */
  493             &&(drive->state > drive_unallocated))           /* and it's a real one: found */
  494                 return driveno;
  495         }
  496     }
  497     /* the drive isn't in the list.  Add it if he wants */
  498     if (create == 0)                                        /* don't want to create */
  499         return -1;                                          /* give up */
  500 
  501     driveno = get_empty_drive();
  502     drive = &DRIVE[driveno];
  503     if (name != NULL)
  504         bcopy(name,                                         /* put in its name */
  505             drive->label.name,
  506             min(sizeof(drive->label.name),
  507                 strlen(name)));
  508     drive->state = drive_referenced;                        /* in use, nothing worthwhile there */
  509     return driveno;                                         /* return the index */
  510 }
  511 
  512 /*
  513  * Find a drive given its device name.
  514  * devname must be valid.
  515  * Otherwise the same as find_drive above
  516  */
  517 int
  518 find_drive_by_dev(const char *devname, int create)
  519 {
  520     int driveno;
  521     struct drive *drive;
  522 
  523     for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
  524         drive = &DRIVE[driveno];                            /* point to drive */
  525         if ((strcmp(drive->devicename, devname) == 0)       /* it's this device */
  526         &&(drive->state > drive_unallocated))               /* and it's a real one: found */
  527             return driveno;
  528     }
  529 
  530     /* the drive isn't in the list.  Add it if he wants */
  531     if (create == 0)                                        /* don't want to create */
  532         return -1;                                          /* give up */
  533 
  534     driveno = get_empty_drive();
  535     drive = &DRIVE[driveno];
  536     bcopy(devname,                                          /* put in its name */
  537         drive->devicename,
  538         min(sizeof(drive->devicename),
  539             strlen(devname)));
  540     drive->state = drive_referenced;                        /* in use, nothing worthwhile there */
  541     return driveno;                                         /* return the index */
  542 }
  543 
  544 /* Find an empty subdisk in the subdisk table */
  545 int
  546 get_empty_sd(void)
  547 {
  548     int sdno;
  549     struct sd *sd;
  550 
  551     /* first see if we have one which has been deallocated */
  552     for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
  553         if (SD[sdno].state == sd_unallocated)               /* bingo */
  554             break;
  555     }
  556     if (sdno >= vinum_conf.subdisks_allocated)
  557         /*
  558          * We've run out of space.  sdno is pointing
  559          * where we want it, but at the moment we
  560          * don't have the space.  Get it.
  561          */
  562         EXPAND(SD, struct sd, vinum_conf.subdisks_allocated, INITIAL_SUBDISKS);
  563 
  564     /* initialize some things */
  565     sd = &SD[sdno];                                         /* point to it */
  566     bzero(sd, sizeof(struct sd));                           /* initialize */
  567     sd->flags |= VF_NEWBORN;                                /* newly born subdisk */
  568     sd->plexno = -1;                                        /* no plex */
  569     sd->sectors = -1;                                       /* no space */
  570     sd->driveno = -1;                                       /* no drive */
  571     sd->plexoffset = -1;                                    /* and no offsets */
  572     sd->driveoffset = -1;
  573     return sdno;                                            /* return the index */
  574 }
  575 
  576 /* return a drive to the free pool */
  577 void
  578 free_drive(struct drive *drive)
  579 {
  580     if ((drive->state > drive_referenced)                   /* real drive */
  581     ||(drive->flags & VF_OPEN)) {                           /* how can it be open without a state? */
  582         LOCKDRIVE(drive);
  583         if (drive->flags & VF_OPEN) {                       /* it's open, */
  584             close_locked_drive(drive);                      /* close it */
  585             drive->state = drive_down;                      /* and note the fact */
  586         }
  587         if (drive->freelist)
  588             Free(drive->freelist);
  589         bzero(drive, sizeof(struct drive));                 /* this also sets drive_unallocated */
  590         unlockdrive(drive);
  591     }
  592 }
  593 
  594 /*
  595  * Find the named subdisk in vinum_conf.sd.
  596  *
  597  * If create != 0, create an entry if it doesn't exist
  598  *
  599  * Return index in vinum_conf.sd
  600  */
  601 int
  602 find_subdisk(const char *name, int create)
  603 {
  604     int sdno;
  605     struct sd *sd;
  606 
  607     for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
  608         if (strcmp(SD[sdno].name, name) == 0)               /* found it */
  609             return sdno;
  610     }
  611 
  612     /* the subdisk isn't in the list.  Add it if he wants */
  613     if (create == 0)                                        /* don't want to create */
  614         return -1;                                          /* give up */
  615 
  616     /* Allocate one and insert the name */
  617     sdno = get_empty_sd();
  618     sd = &SD[sdno];
  619     bcopy(name, sd->name, min(sizeof(sd->name), strlen(name))); /* put in its name */
  620     return sdno;                                            /* return the pointer */
  621 }
  622 
  623 /* Return space to a drive */
  624 void
  625 return_drive_space(int driveno, int64_t offset, int length)
  626 {
  627     struct drive *drive;
  628     int fe;                                                 /* free list entry */
  629     u_int64_t sdend;                                        /* end of our subdisk */
  630     u_int64_t dend;                                         /* end of our freelist entry */
  631 
  632     drive = &DRIVE[driveno];
  633     if (drive->state == drive_up) {
  634         sdend = offset + length;                            /* end of our subdisk */
  635 
  636         /* Look for where to return the sd address space */
  637         for (fe = 0;
  638             (fe < drive->freelist_entries) && (drive->freelist[fe].offset < offset);
  639             fe++);
  640         /*
  641          * Now we are pointing to the last entry, the first
  642          * with a higher offset than the subdisk, or both.
  643          */
  644         if ((fe > 1)                                        /* not the first entry */
  645 &&((fe == drive->freelist_entries)                          /* gone past the end */
  646         ||(drive->freelist[fe].offset > offset)))           /* or past the block were looking for */
  647             fe--;                                           /* point to the block before */
  648         dend = drive->freelist[fe].offset + drive->freelist[fe].sectors; /* end of the entry */
  649 
  650         /*
  651          * At this point, we are pointing to the correct
  652          * place in the free list.  A number of possibilities
  653          * exist:
  654          *
  655          * 1.  The block to be freed starts at the end of the
  656          *     block to which we are pointing.  This has two
  657          *     subcases:
  658          *
  659          * a.  The block to be freed ends at the beginning
  660          *     of the following block.  Merge the three
  661          *     areas into a single block.
  662          *
  663          * b.  The block is shorter than the space between
  664          *     the current block and the next one.  Enlarge
  665          *     the current block.
  666          *
  667          * 2.  The block to be freed starts after the end
  668          *     of the block.  Again, we have two cases:
  669          *
  670          * a.  It ends before the start of the following block.
  671          *     Create a new free block.
  672          *
  673          * b.  It ends at the start of the following block.
  674          *     Enlarge the following block downwards.
  675          *
  676          * When there is only one free space block, and the
  677          * space to be returned is before it, the pointer is
  678          * to a non-existent zeroth block. XXX check this
  679          */
  680         if (offset == dend) {                               /* Case 1: it starts at the end of this block */
  681             if ((fe < drive->freelist_entries - 1)          /* we're not the last block in the free list */
  682             /* and the subdisk ends at the start of the next block */
  683             &&(sdend == drive->freelist[fe + 1].offset)) {
  684                 drive->freelist[fe].sectors                 /* 1a: merge all three blocks */
  685                     = drive->freelist[fe + 1].sectors;
  686                 if (fe < drive->freelist_entries - 2)       /* still more blocks after next */
  687                     bcopy(&drive->freelist[fe + 2],         /* move down one */
  688                         &drive->freelist[fe + 1],
  689                         (drive->freelist_entries - 2 - fe)
  690                         * sizeof(struct drive_freelist));
  691                 drive->freelist_entries--;                  /* one less entry in the free list */
  692             } else                                          /* 1b: just enlarge this block */
  693                 drive->freelist[fe].sectors += length;
  694         } else {                                            /* Case 2 */
  695             if (offset > dend)                              /* it starts after this block */
  696                 fe++;                                       /* so look at the next block */
  697             if ((fe < drive->freelist_entries)              /* we're not the last block in the free list */
  698             /* and the subdisk ends at the start of this block: case 4 */
  699             &&(sdend == drive->freelist[fe].offset)) {
  700                 drive->freelist[fe].offset = offset;        /* it starts where the sd was */
  701                 drive->freelist[fe].sectors += length;      /* and it's this much bigger */
  702             } else {                                        /* case 3: non-contiguous */
  703                 if (fe < drive->freelist_entries)           /* not after the last block, */
  704                     bcopy(&drive->freelist[fe],             /* move the rest up one entry */
  705                         &drive->freelist[fe + 1],
  706                         (drive->freelist_entries - fe)
  707                         * sizeof(struct drive_freelist));
  708                 drive->freelist_entries++;                  /* one less entry */
  709                 drive->freelist[fe].offset = offset;        /* this entry represents the sd */
  710                 drive->freelist[fe].sectors = length;
  711             }
  712         }
  713         drive->sectors_available += length;                 /* the sectors are now available */
  714     }
  715 }
  716 
  717 /*
  718  * Free an allocated sd entry.
  719  * This performs memory management only.  remove()
  720  * is responsible for checking relationships.
  721  */
  722 void
  723 free_sd(int sdno)
  724 {
  725     struct sd *sd;
  726 
  727     sd = &SD[sdno];
  728     if ((sd->driveno >= 0)                                  /* we have a drive, */
  729     &&(sd->sectors > 0))                                    /* and some space on it */
  730         return_drive_space(sd->driveno,                     /* return the space */
  731             sd->driveoffset,
  732             sd->sectors);
  733     if (sd->plexno >= 0)
  734         PLEX[sd->plexno].subdisks--;                        /* one less subdisk */
  735     bzero(sd, sizeof(struct sd));                           /* and clear it out */
  736     sd->state = sd_unallocated;
  737     vinum_conf.subdisks_used--;                             /* one less sd */
  738 }
  739 
  740 /* Find an empty plex in the plex table */
  741 int
  742 get_empty_plex(void)
  743 {
  744     int plexno;
  745     struct plex *plex;                                      /* if we allocate one */
  746 
  747     /* first see if we have one which has been deallocated */
  748     for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++) {
  749         if (PLEX[plexno].state == plex_unallocated)         /* bingo */
  750             break;                                          /* and get out of here */
  751     }
  752 
  753     if (plexno >= vinum_conf.plexes_allocated)
  754         EXPAND(PLEX, struct plex, vinum_conf.plexes_allocated, INITIAL_PLEXES);
  755 
  756     /* Found a plex.  Give it an sd structure */
  757     plex = &PLEX[plexno];                                   /* this one is ours */
  758     bzero(plex, sizeof(struct plex));                       /* polish it up */
  759     plex->sdnos = (int *) Malloc(sizeof(int) * INITIAL_SUBDISKS_IN_PLEX); /* allocate sd table */
  760     CHECKALLOC(plex->sdnos, "vinum: Can't allocate plex subdisk table");
  761     bzero(plex->sdnos, (sizeof(int) * INITIAL_SUBDISKS_IN_PLEX)); /* do we need this? */
  762     plex->flags |= VF_NEWBORN;                              /* newly born plex */
  763     plex->subdisks = 0;                                     /* no subdisks in use */
  764     plex->subdisks_allocated = INITIAL_SUBDISKS_IN_PLEX;    /* and we have space for this many */
  765     plex->organization = plex_disorg;                       /* and it's not organized */
  766     plex->volno = -1;                                       /* no volume yet */
  767     return plexno;                                          /* return the index */
  768 }
  769 
  770 /*
  771  * Find the named plex in vinum_conf.plex
  772  *
  773  * If create != 0, create an entry if it doesn't exist
  774  * return index in vinum_conf.plex
  775  */
  776 int
  777 find_plex(const char *name, int create)
  778 {
  779     int plexno;
  780     struct plex *plex;
  781 
  782     for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++) {
  783         if (strcmp(PLEX[plexno].name, name) == 0)           /* found it */
  784             return plexno;
  785     }
  786 
  787     /* the plex isn't in the list.  Add it if he wants */
  788     if (create == 0)                                        /* don't want to create */
  789         return -1;                                          /* give up */
  790 
  791     /* Allocate one and insert the name */
  792     plexno = get_empty_plex();
  793     plex = &PLEX[plexno];                                   /* point to it */
  794     bcopy(name, plex->name, min(sizeof(plex->name), strlen(name))); /* put in its name */
  795     return plexno;                                          /* return the pointer */
  796 }
  797 
  798 /*
  799  * Free an allocated plex entry
  800  * and its associated memory areas
  801  */
  802 void
  803 free_plex(int plexno)
  804 {
  805     struct plex *plex;
  806 
  807     plex = &PLEX[plexno];
  808     if (plex->sdnos)
  809         Free(plex->sdnos);
  810     if (plex->lock)
  811         Free(plex->lock);
  812     bzero(plex, sizeof(struct plex));                       /* and clear it out */
  813     plex->state = plex_unallocated;
  814 }
  815 
  816 /* Find an empty volume in the volume table */
  817 int
  818 get_empty_volume(void)
  819 {
  820     int volno;
  821     struct volume *vol;
  822     int i;
  823 
  824     /* first see if we have one which has been deallocated */
  825     for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
  826         if (VOL[volno].state == volume_unallocated)         /* bingo */
  827             break;
  828     }
  829 
  830     if (volno >= vinum_conf.volumes_allocated)
  831         EXPAND(VOL, struct volume, vinum_conf.volumes_allocated, INITIAL_VOLUMES);
  832 
  833     /* Now initialize fields */
  834     vol = &VOL[volno];
  835     bzero(vol, sizeof(struct volume));
  836     vol->flags |= VF_NEWBORN | VF_CREATED;                  /* newly born volume */
  837     vol->preferred_plex = ROUND_ROBIN_READPOL;              /* round robin */
  838     for (i = 0; i < MAXPLEX; i++)                           /* mark the plexes missing */
  839         vol->plex[i] = -1;
  840     return volno;                                           /* return the index */
  841 }
  842 
  843 /*
  844  * Find the named volume in vinum_conf.volume.
  845  *
  846  * If create != 0, create an entry if it doesn't exist
  847  * return the index in vinum_conf
  848  */
  849 int
  850 find_volume(const char *name, int create)
  851 {
  852     int volno;
  853     struct volume *vol;
  854 
  855     for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
  856         if (strcmp(VOL[volno].name, name) == 0)             /* found it */
  857             return volno;
  858     }
  859 
  860     /* the volume isn't in the list.  Add it if he wants */
  861     if (create == 0)                                        /* don't want to create */
  862         return -1;                                          /* give up */
  863 
  864     /* Allocate one and insert the name */
  865     volno = get_empty_volume();
  866     vol = &VOL[volno];
  867     bcopy(name, vol->name, min(sizeof(vol->name), strlen(name))); /* put in its name */
  868     vol->blocksize = DEV_BSIZE;                             /* block size of this volume */
  869     return volno;                                           /* return the pointer */
  870 }
  871 
  872 /*
  873  * Free an allocated volume entry
  874  * and its associated memory areas
  875  */
  876 void
  877 free_volume(int volno)
  878 {
  879     struct volume *vol;
  880 
  881     vol = &VOL[volno];
  882     bzero(vol, sizeof(struct volume));                      /* and clear it out */
  883     vol->state = volume_unallocated;
  884 }
  885 
  886 /*
  887  * Handle a drive definition.  We store the information in the global variable
  888  * drive, so we don't need to allocate.
  889  *
  890  * If we find an error, print a message and return
  891  */
  892 void
  893 config_drive(int update)
  894 {
  895     enum drive_label_info partition_status;                 /* info about the partition */
  896     int parameter;
  897     int driveno;                                            /* index of drive in vinum_conf */
  898     struct drive *drive;                                    /* and pointer to it */
  899     int otherdriveno;                                       /* index of possible second drive */
  900     int sdno;
  901 
  902     if (tokens < 2)                                         /* not enough tokens */
  903         throw_rude_remark(EINVAL, "Drive has no name\n");
  904     driveno = find_drive(token[1], 1);                      /* allocate a drive to initialize */
  905     drive = &DRIVE[driveno];                                /* and get a pointer */
  906     if (update && ((drive->flags & VF_NEWBORN) == 0))       /* this drive exists already */
  907         return;                                             /* don't do anything */
  908     drive->flags &= ~VF_NEWBORN;                            /* no longer newly born */
  909 
  910     if (drive->state != drive_referenced) {                 /* we already know this drive */
  911         /*
  912          * XXX Check which definition is more up-to-date.  Give
  913          * preference for the definition on its own drive
  914          */
  915         return;                                             /* XXX */
  916     }
  917     for (parameter = 2; parameter < tokens; parameter++) {  /* look at the other tokens */
  918         switch (get_keyword(token[parameter], &keyword_set)) {
  919         case kw_device:
  920             parameter++;
  921             otherdriveno = find_drive_by_dev(token[parameter], 0); /* see if it exists already */
  922             if (otherdriveno >= 0) {                        /* yup, */
  923                 drive->state = drive_unallocated;           /* deallocate the drive */
  924                 throw_rude_remark(EEXIST,                   /* and complain */
  925                     "Drive %s would have same device as drive %s",
  926                     token[1],
  927                     DRIVE[otherdriveno].label.name);
  928             }
  929             if (drive->devicename[0] == '/') {              /* we know this drive... */
  930                 if (strcmp(drive->devicename, token[parameter])) /* different name */
  931                     close_drive(drive);                     /* close it if it's open */
  932                 else                                        /* no change */
  933                     break;
  934             }
  935             /* open the device and get the configuration */
  936             bcopy(token[parameter],                         /* insert device information */
  937                 drive->devicename,
  938                 min(sizeof(drive->devicename),
  939                     strlen(token[parameter])));
  940             partition_status = read_drive_label(drive, 1);
  941             switch (partition_status) {
  942             case DL_CANT_OPEN:                              /* not our kind */
  943                 close_drive(drive);
  944                 if (drive->lasterror == EFTYPE)             /* wrong kind of partition */
  945                     throw_rude_remark(drive->lasterror,
  946                         "Drive %s has invalid partition type",
  947                         drive->label.name);
  948                 else                                        /* I/O error of some kind */
  949                     throw_rude_remark(drive->lasterror,
  950                         "Can't initialize drive %s",
  951                         drive->label.name);
  952                 break;
  953 
  954             case DL_WRONG_DRIVE:                            /* valid drive, not the name we expected */
  955                 if (vinum_conf.flags & VF_FORCECONFIG) {    /* but we'll accept that */
  956                     bcopy(token[1], drive->label.name, sizeof(drive->label.name));
  957                     break;
  958                 }
  959                 close_drive(drive);
  960                 /*
  961                  * There's a potential race condition here:
  962                  * the rude remark refers to a field in an
  963                  * unallocated drive, which potentially could
  964                  * be reused.  This works because we're the only
  965                  * thread accessing the config at the moment.
  966                  */
  967                 drive->state = drive_unallocated;           /* throw it away completely */
  968                 throw_rude_remark(drive->lasterror,
  969                     "Incorrect drive name %s specified for drive %s",
  970                     token[1],
  971                     drive->label.name);
  972                 break;
  973 
  974             case DL_DELETED_LABEL:                          /* it was a drive, but we deleted it */
  975                 break;
  976 
  977             case DL_NOT_OURS:                               /* nothing to do with the rest */
  978             case DL_OURS:
  979                 break;
  980             }
  981             /*
  982              * read_drive_label overwrites the device name.
  983              * If we get here, we can have the drive,
  984              * so put it back again
  985              */
  986             bcopy(token[parameter],
  987                 drive->devicename,
  988                 min(sizeof(drive->devicename),
  989                     strlen(token[parameter])));
  990             break;
  991 
  992         case kw_state:
  993             parameter++;                                    /* skip the keyword */
  994             if (vinum_conf.flags & VF_READING_CONFIG)
  995                 drive->state = DriveState(token[parameter]); /* set the state */
  996             break;
  997 
  998         case kw_hotspare:                                   /* this drive is a hot spare */
  999             drive->flags |= VF_HOTSPARE;
 1000             break;
 1001 
 1002         default:
 1003             close_drive(drive);
 1004             throw_rude_remark(EINVAL,
 1005                 "Drive %s, invalid keyword: %s",
 1006                 token[1],
 1007                 token[parameter]);
 1008         }
 1009     }
 1010 
 1011     if (drive->devicename[0] != '/') {
 1012         drive->state = drive_unallocated;                   /* deallocate the drive */
 1013         throw_rude_remark(EINVAL, "No device name for %s", drive->label.name);
 1014     }
 1015     vinum_conf.drives_used++;                               /* passed all hurdles: one more in use */
 1016     /*
 1017      * If we're replacing a drive, it could be that
 1018      * we already have subdisks referencing this
 1019      * drive.  Note where they should be and change
 1020      * their state to obsolete.
 1021      */
 1022     for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
 1023         if ((SD[sdno].state > sd_referenced)
 1024             && (SD[sdno].driveno == driveno)) {
 1025             give_sd_to_drive(sdno);
 1026             if (SD[sdno].state > sd_stale)
 1027                 SD[sdno].state = sd_stale;
 1028         }
 1029     }
 1030 }
 1031 
 1032 /*
 1033  * Handle a subdisk definition.  We store the information in the global variable
 1034  * sd, so we don't need to allocate.
 1035  *
 1036  * If we find an error, print a message and return
 1037  */
 1038 void
 1039 config_subdisk(int update)
 1040 {
 1041     int parameter;
 1042     int sdno;                                               /* index of sd in vinum_conf */
 1043     struct sd *sd;                                          /* and pointer to it */
 1044     u_int64_t size;
 1045     int detached = 0;                                       /* set to 1 if this is a detached subdisk */
 1046     int sdindex = -1;                                       /* index in plexes subdisk table */
 1047     enum sdstate state = sd_unallocated;                    /* state to set, if specified */
 1048     int autosize = 0;                                       /* set if we autosize in give_sd_to_drive */
 1049     int namedsdno;                                          /* index of another with this name */
 1050     char partition = 0;                                     /* partition of external subdisk */
 1051 
 1052     sdno = get_empty_sd();                                  /* allocate an SD to initialize */
 1053     sd = &SD[sdno];                                         /* and get a pointer */
 1054 
 1055     for (parameter = 1; parameter < tokens; parameter++) {  /* look at the other tokens */
 1056         switch (get_keyword(token[parameter], &keyword_set)) {
 1057             /*
 1058              * If we have a 'name' parameter, it must
 1059              * come first, because we're too lazy to tidy
 1060              * up dangling refs if it comes later.
 1061              */
 1062         case kw_name:
 1063             namedsdno = find_subdisk(token[++parameter], 0); /* find an existing sd with this name */
 1064             if (namedsdno >= 0) {                           /* got one */
 1065                 if (SD[namedsdno].state == sd_referenced) { /* we've been told about this one */
 1066                     if (parameter > 2)
 1067                         throw_rude_remark(EINVAL,
 1068                             "sd %s: name parameter must come first\n", /* no go */
 1069                             token[parameter]);
 1070                     else {
 1071                         int i;
 1072                         struct plex *plex;                  /* for tidying up dangling references */
 1073 
 1074                         *sd = SD[namedsdno];                /* copy from the referenced one */
 1075                         SD[namedsdno].state = sd_unallocated; /* and deallocate the referenced one */
 1076                         plex = &PLEX[sd->plexno];           /* now take a look at our plex */
 1077                         for (i = 0; i < plex->subdisks; i++) { /* look for the pointer */
 1078                             if (plex->sdnos[i] == namedsdno) /* pointing to the old subdisk */
 1079                                 plex->sdnos[i] = sdno;      /* bend it to point here */
 1080                         }
 1081                     }
 1082                 }
 1083                 if (update)                                 /* are we updating? */
 1084                     return;                                 /* that's OK, nothing more to do */
 1085                 else
 1086                     throw_rude_remark(EINVAL, "Duplicate subdisk %s", token[parameter]);
 1087             } else
 1088                 bcopy(token[parameter],
 1089                     sd->name,
 1090                     min(sizeof(sd->name), strlen(token[parameter])));
 1091             break;
 1092 
 1093         case kw_detached:
 1094             detached = 1;
 1095             break;
 1096 
 1097         case kw_plexoffset:
 1098             size = sizespec(token[++parameter]);
 1099             if ((size == -1)                                /* unallocated */
 1100             &&(vinum_conf.flags & VF_READING_CONFIG))       /* reading from disk */
 1101                 break;                                      /* invalid sd; just ignore it */
 1102             if ((size % DEV_BSIZE) != 0)
 1103                 throw_rude_remark(EINVAL,
 1104                     "sd %s, bad plex offset alignment: %lld",
 1105                     sd->name,
 1106                     (long long) size);
 1107             else
 1108                 sd->plexoffset = size / DEV_BSIZE;
 1109             break;
 1110 
 1111         case kw_driveoffset:
 1112             size = sizespec(token[++parameter]);
 1113             if ((size == -1)                                /* unallocated */
 1114             &&(vinum_conf.flags & VF_READING_CONFIG))       /* reading from disk */
 1115                 break;                                      /* invalid sd; just ignore it */
 1116             if ((size % DEV_BSIZE) != 0)
 1117                 throw_rude_remark(EINVAL,
 1118                     "sd %s, bad drive offset alignment: %lld",
 1119                     sd->name,
 1120                     (long long) size);
 1121             else
 1122                 sd->driveoffset = size / DEV_BSIZE;
 1123             break;
 1124 
 1125         case kw_len:
 1126             if (get_keyword(token[++parameter], &keyword_set) == kw_max) /* select maximum size from drive */
 1127                 size = 0;                                   /* this is how we say it :-) */
 1128             else
 1129                 size = sizespec(token[parameter]);
 1130             if ((size % DEV_BSIZE) != 0)
 1131                 throw_rude_remark(EINVAL, "sd %s, length %d not multiple of sector size", sd->name, size);
 1132             else
 1133                 sd->sectors = size / DEV_BSIZE;
 1134             /*
 1135              * We have a problem with autosizing: we need to
 1136              * give the drive to the plex before we give it
 1137              * to the drive, in order to be clean if we give
 1138              * up in the middle, but at this time the size hasn't
 1139              * been set.  Note that we have to fix up after
 1140              * giving the subdisk to the drive.
 1141              */
 1142             if (size == 0)
 1143                 autosize = 1;                               /* note that we're autosizing */
 1144             break;
 1145 
 1146         case kw_drive:
 1147             sd->driveno = find_drive(token[++parameter], 1); /* insert drive information */
 1148             break;
 1149 
 1150         case kw_plex:
 1151             sd->plexno = find_plex(token[++parameter], 1);  /* insert plex information */
 1152             break;
 1153 
 1154             /*
 1155              * Set the state.  We can't do this directly,
 1156              * because give_sd_to_plex may change it
 1157              */
 1158         case kw_state:
 1159             parameter++;                                    /* skip the keyword */
 1160             if (vinum_conf.flags & VF_READING_CONFIG)
 1161                 state = SdState(token[parameter]);          /* set the state */
 1162             break;
 1163 
 1164         case kw_partition:
 1165             parameter++;                                    /* skip the keyword */
 1166             if ((strlen(token[parameter]) != 1)
 1167                 || (token[parameter][0] < 'a')
 1168                 || (token[parameter][0] > 'h'))
 1169                 throw_rude_remark(EINVAL,
 1170                     "%s: invalid partition %c",
 1171                     sd->name,
 1172                     token[parameter][0]);
 1173             else
 1174                 partition = token[parameter][0];
 1175             break;
 1176 
 1177 
 1178         default:
 1179             throw_rude_remark(EINVAL, "%s: invalid keyword: %s", sd->name, token[parameter]);
 1180         }
 1181     }
 1182 
 1183     /* Check we have a drive name */
 1184     if (sd->driveno < 0) {                                  /* didn't specify a drive */
 1185         sd->driveno = current_drive;                        /* set to the current drive */
 1186         if (sd->driveno < 0)                                /* no current drive? */
 1187             throw_rude_remark(EINVAL, "Subdisk %s is not associated with a drive", sd->name);
 1188     }
 1189     /*
 1190      * This is tacky.  If something goes wrong
 1191      * with the checks, we may end up losing drive
 1192      * space.  FIXME.
 1193      */
 1194     if (autosize != 0)                                      /* need to find a size, */
 1195         give_sd_to_drive(sdno);                             /* do it before the plex */
 1196 
 1197     /*  Check for a plex name */
 1198     if ((sd->plexno < 0)                                    /* didn't specify a plex */
 1199     &&(!detached))                                          /* and didn't say not to, */
 1200         sd->plexno = current_plex;                          /* set to the current plex */
 1201 
 1202     if (sd->plexno >= 0)
 1203         sdindex = give_sd_to_plex(sd->plexno, sdno);        /* now tell the plex that it has this sd */
 1204 
 1205     sd->sdno = sdno;                                        /* point to our entry in the table */
 1206 
 1207     /* Does the subdisk have a name?  If not, give it one */
 1208     if (sd->name[0] == '\0') {                              /* no name */
 1209         char sdsuffix[8];                                   /* form sd name suffix here */
 1210 
 1211         /* Do we have a plex name? */
 1212         if (sdindex >= 0)                                   /* we have a plex */
 1213             strcpy(sd->name, PLEX[sd->plexno].name);        /* take it from there */
 1214         else                                                /* no way */
 1215             throw_rude_remark(EINVAL, "Unnamed sd is not associated with a plex");
 1216         sprintf(sdsuffix, ".s%d", sdindex);                 /* form the suffix */
 1217         strcat(sd->name, sdsuffix);                         /* and add it to the name */
 1218     }
 1219     /* do we have complete info for this subdisk? */
 1220     if (sd->sectors < 0)
 1221         throw_rude_remark(EINVAL, "sd %s has no length spec", sd->name);
 1222 
 1223     if (state != sd_unallocated)                            /* we had a specific state to set */
 1224         sd->state = state;                                  /* do it now */
 1225     else if (sd->state == sd_unallocated)                   /* no, nothing set yet, */
 1226         sd->state = sd_empty;                               /* must be empty */
 1227     if (autosize == 0)                                      /* no autoconfig, do the drive now */
 1228         give_sd_to_drive(sdno);
 1229     vinum_conf.subdisks_used++;                             /* one more in use */
 1230 }
 1231 
 1232 /*
 1233  * Handle a plex definition.
 1234  */
 1235 void
 1236 config_plex(int update)
 1237 {
 1238     int parameter;
 1239     int plexno;                                             /* index of plex in vinum_conf */
 1240     struct plex *plex;                                      /* and pointer to it */
 1241     int pindex = MAXPLEX;                                   /* index in volume's plex list */
 1242     int detached = 0;                                       /* don't give it to a volume */
 1243     int namedplexno;
 1244     enum plexstate state = plex_init;                       /* state to set at end */
 1245 
 1246     current_plex = -1;                                      /* forget the previous plex */
 1247     plexno = get_empty_plex();                              /* allocate a plex */
 1248     plex = &PLEX[plexno];                                   /* and point to it */
 1249     plex->plexno = plexno;                                  /* and back to the config */
 1250 
 1251     for (parameter = 1; parameter < tokens; parameter++) {  /* look at the other tokens */
 1252         switch (get_keyword(token[parameter], &keyword_set)) {
 1253             /*
 1254              * If we have a 'name' parameter, it must
 1255              * come first, because we're too lazy to tidy
 1256              * up dangling refs if it comes later.
 1257              */
 1258         case kw_name:
 1259             namedplexno = find_plex(token[++parameter], 0); /* find an existing plex with this name */
 1260             if (namedplexno >= 0) {                         /* plex exists already, */
 1261                 if (PLEX[namedplexno].state == plex_referenced) { /* we've been told about this one */
 1262                     if (parameter > 2)                      /* we've done other things first, */
 1263                         throw_rude_remark(EINVAL,
 1264                             "plex %s: name parameter must come first\n", /* no go */
 1265                             token[parameter]);
 1266                     else {
 1267                         int i;
 1268                         struct volume *vol;                 /* for tidying up dangling references */
 1269 
 1270                         *plex = PLEX[namedplexno];          /* get the info */
 1271                         PLEX[namedplexno].state = plex_unallocated; /* and deallocate the other one */
 1272                         vol = &VOL[plex->volno];            /* point to the volume */
 1273                         for (i = 0; i < MAXPLEX; i++) {     /* for each plex */
 1274                             if (vol->plex[i] == namedplexno)
 1275                                 vol->plex[i] = plexno;      /* bend the pointer */
 1276                         }
 1277                     }
 1278                     break;                                  /* use this one */
 1279                 }
 1280                 if (update)                                 /* are we updating? */
 1281                     return;                                 /* yes: that's OK, just return */
 1282                 else
 1283                     throw_rude_remark(EINVAL, "Duplicate plex %s", token[parameter]);
 1284             } else
 1285                 bcopy(token[parameter],                     /* put in the name */
 1286                     plex->name,
 1287                     min(MAXPLEXNAME, strlen(token[parameter])));
 1288             break;
 1289 
 1290         case kw_detached:
 1291             detached = 1;
 1292             break;
 1293 
 1294         case kw_org:                                        /* plex organization */
 1295             switch (get_keyword(token[++parameter], &keyword_set)) {
 1296             case kw_concat:
 1297                 plex->organization = plex_concat;
 1298                 break;
 1299 
 1300             case kw_striped:
 1301                 {
 1302                     int stripesize = sizespec(token[++parameter]);
 1303 
 1304                     plex->organization = plex_striped;
 1305                     if (stripesize % DEV_BSIZE != 0)        /* not a multiple of block size, */
 1306                         throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
 1307                             plex->name,
 1308                             stripesize);
 1309                     else
 1310                         plex->stripesize = stripesize / DEV_BSIZE;
 1311                     break;
 1312                 }
 1313 
 1314             case kw_raid4:
 1315                 {
 1316                     int stripesize = sizespec(token[++parameter]);
 1317 
 1318                     plex->organization = plex_raid4;
 1319                     if (stripesize % DEV_BSIZE != 0)        /* not a multiple of block size, */
 1320                         throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
 1321                             plex->name,
 1322                             stripesize);
 1323                     else
 1324                         plex->stripesize = stripesize / DEV_BSIZE;
 1325                     break;
 1326                 }
 1327 
 1328             case kw_raid5:
 1329                 {
 1330                     int stripesize = sizespec(token[++parameter]);
 1331 
 1332                     plex->organization = plex_raid5;
 1333                     if (stripesize % DEV_BSIZE != 0)        /* not a multiple of block size, */
 1334                         throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
 1335                             plex->name,
 1336                             stripesize);
 1337                     else
 1338                         plex->stripesize = stripesize / DEV_BSIZE;
 1339                     break;
 1340                 }
 1341 
 1342             default:
 1343                 throw_rude_remark(EINVAL, "Invalid plex organization");
 1344             }
 1345             if (isstriped(plex)
 1346                 && (plex->stripesize == 0))                 /* didn't specify a valid stripe size */
 1347                 throw_rude_remark(EINVAL, "Need a stripe size parameter");
 1348             break;
 1349 
 1350         case kw_volume:
 1351             plex->volno = find_volume(token[++parameter], 1); /* insert a pointer to the volume */
 1352             break;
 1353 
 1354         case kw_sd:                                         /* add a subdisk */
 1355             {
 1356                 int sdno;
 1357 
 1358                 sdno = find_subdisk(token[++parameter], 1); /* find a subdisk */
 1359                 SD[sdno].plexoffset = sizespec(token[++parameter]); /* get the offset */
 1360                 give_sd_to_plex(plexno, sdno);              /* and insert it there */
 1361                 break;
 1362             }
 1363 
 1364         case kw_state:
 1365             parameter++;                                    /* skip the keyword */
 1366             if (vinum_conf.flags & VF_READING_CONFIG)
 1367                 state = PlexState(token[parameter]);        /* set the state */
 1368             break;
 1369 
 1370         default:
 1371             throw_rude_remark(EINVAL, "plex %s, invalid keyword: %s",
 1372                 plex->name,
 1373                 token[parameter]);
 1374         }
 1375     }
 1376 
 1377     if ((plex->volno < 0)                                   /* we don't have a volume */
 1378     &&(!detached))                                          /* and we wouldn't object */
 1379         plex->volno = current_volume;
 1380 
 1381     if (plex->volno >= 0)
 1382         pindex = give_plex_to_volume(plex->volno, plexno);  /* Now tell the volume that it has this plex */
 1383 
 1384     /* Does the plex have a name?  If not, give it one */
 1385     if (plex->name[0] == '\0') {                            /* no name */
 1386         char plexsuffix[8];                                 /* form plex name suffix here */
 1387         /* Do we have a volume name? */
 1388         if (plex->volno >= 0)                               /* we have a volume */
 1389             strcpy(plex->name,                              /* take it from there */
 1390                 VOL[plex->volno].name);
 1391         else                                                /* no way */
 1392             throw_rude_remark(EINVAL, "Unnamed plex is not associated with a volume");
 1393         sprintf(plexsuffix, ".p%d", pindex);                /* form the suffix */
 1394         strcat(plex->name, plexsuffix);                     /* and add it to the name */
 1395     }
 1396     /* Note the last plex we configured */
 1397     current_plex = plexno;
 1398     plex->state = state;                                    /* set whatever state we chose */
 1399     vinum_conf.plexes_used++;                               /* one more in use */
 1400 }
 1401 
 1402 /*
 1403  * Handle a volume definition.
 1404  * If we find an error, print a message, deallocate the nascent volume, and return
 1405  */
 1406 void
 1407 config_volume(int update)
 1408 {
 1409     int parameter;
 1410     int volno;
 1411     struct volume *vol;                                     /* collect volume info here */
 1412     int i;
 1413 
 1414     if (tokens < 2)                                         /* not enough tokens */
 1415         throw_rude_remark(EINVAL, "Volume has no name");
 1416     current_volume = -1;                                    /* forget the previous volume */
 1417     volno = find_volume(token[1], 1);                       /* allocate a volume to initialize */
 1418     vol = &VOL[volno];                                      /* and get a pointer */
 1419     if (update && ((vol->flags & VF_CREATED) == 0))         /* this volume exists already */
 1420         return;                                             /* don't do anything */
 1421     vol->flags &= ~VF_CREATED;                              /* it exists now */
 1422 
 1423     for (parameter = 2; parameter < tokens; parameter++) {  /* look at all tokens */
 1424         switch (get_keyword(token[parameter], &keyword_set)) {
 1425         case kw_plex:
 1426             {
 1427                 int plexno;                                 /* index of this plex */
 1428                 int myplexno;                               /* and index if it's already ours */
 1429 
 1430                 plexno = find_plex(token[++parameter], 1);  /* find a plex */
 1431                 if (plexno < 0)                             /* couldn't */
 1432                     break;                                  /* we've already had an error message */
 1433                 myplexno = my_plex(volno, plexno);          /* does it already belong to us? */
 1434                 if (myplexno > 0)                           /* yes, shouldn't get it again */
 1435                     throw_rude_remark(EINVAL,
 1436                         "Plex %s already belongs to volume %s",
 1437                         token[parameter],
 1438                         vol->name);
 1439                 else if (++vol->plexes > 8)                 /* another entry */
 1440                     throw_rude_remark(EINVAL,
 1441                         "Too many plexes for volume %s",
 1442                         vol->name);
 1443                 vol->plex[vol->plexes - 1] = plexno;
 1444                 PLEX[plexno].state = plex_referenced;       /* we know something about it */
 1445                 PLEX[plexno].volno = volno;                 /* and this volume references it */
 1446             }
 1447             break;
 1448 
 1449         case kw_readpol:
 1450             switch (get_keyword(token[++parameter], &keyword_set)) { /* decide what to do */
 1451             case kw_round:
 1452                 vol->preferred_plex = ROUND_ROBIN_READPOL;  /* default */
 1453                 break;
 1454 
 1455             case kw_prefer:
 1456                 {
 1457                     int myplexno;                           /* index of this plex */
 1458 
 1459                     myplexno = find_plex(token[++parameter], 1); /* find a plex */
 1460                     if (myplexno < 0)                       /* couldn't */
 1461                         break;                              /* we've already had an error message */
 1462                     myplexno = my_plex(volno, myplexno);    /* does it already belong to us? */
 1463                     if (myplexno > 0)                       /* yes */
 1464                         vol->preferred_plex = myplexno;     /* just note the index */
 1465                     else if (++vol->plexes > 8)             /* another entry */
 1466                         throw_rude_remark(EINVAL, "Too many plexes");
 1467                     else {                                  /* space for the new plex */
 1468                         vol->plex[vol->plexes - 1] = myplexno; /* add it to our list */
 1469                         vol->preferred_plex = vol->plexes - 1; /* and note the index */
 1470                     }
 1471                 }
 1472                 break;
 1473 
 1474             default:
 1475                 throw_rude_remark(EINVAL, "Invalid read policy");
 1476             }
 1477 
 1478         case kw_setupstate:
 1479             vol->flags |= VF_CONFIG_SETUPSTATE;             /* set the volume up later on */
 1480             break;
 1481 
 1482         case kw_state:
 1483             parameter++;                                    /* skip the keyword */
 1484             if (vinum_conf.flags & VF_READING_CONFIG)
 1485                 vol->state = VolState(token[parameter]);    /* set the state */
 1486             break;
 1487 
 1488             /*
 1489              * XXX experimental ideas.  These are not
 1490              * documented, and will not be until I
 1491              * decide they're worth keeping
 1492              */
 1493         case kw_writethrough:                               /* set writethrough mode */
 1494             vol->flags |= VF_WRITETHROUGH;
 1495             break;
 1496 
 1497         case kw_writeback:                                  /* set writeback mode */
 1498             vol->flags &= ~VF_WRITETHROUGH;
 1499             break;
 1500 
 1501         case kw_raw:
 1502             vol->flags |= VF_RAW;                           /* raw volume (no label) */
 1503             break;
 1504 
 1505         default:
 1506             throw_rude_remark(EINVAL, "volume %s, invalid keyword: %s",
 1507                 vol->name,
 1508                 token[parameter]);
 1509         }
 1510     }
 1511     current_volume = volno;                                 /* note last referred volume */
 1512     vol->volno = volno;                                     /* also note in volume */
 1513 
 1514     /*
 1515      * Before we can actually use the volume, we need
 1516      * a volume label.  We could start to fake one here,
 1517      * but it will be a lot easier when we have some
 1518      * to copy from the drives, so defer it until we
 1519      * set up the configuration. XXX
 1520      */
 1521     if (vol->state == volume_unallocated)
 1522         vol->state = volume_down;                           /* now ready to bring up at the end */
 1523 
 1524     /* Find out how big our volume is */
 1525     for (i = 0; i < vol->plexes; i++)
 1526         vol->size = max(vol->size, PLEX[vol->plex[i]].length);
 1527     vinum_conf.volumes_used++;                              /* one more in use */
 1528 }
 1529 
 1530 /*
 1531  * Parse a config entry.  CARE!  This destroys the original contents of the
 1532  * config entry, which we don't really need after this.  More specifically, it
 1533  * places \0 characters at the end of each token.
 1534  *
 1535  * Return 0 if all is well, otherwise EINVAL for invalid keyword,
 1536  * or ENOENT if 'read' command doesn't find any drives.
 1537  */
 1538 int
 1539 parse_config(char *cptr, struct keywordset *keyset, int update)
 1540 {
 1541     int status;
 1542 
 1543     status = 0;                                             /* until proven otherwise */
 1544     tokens = tokenize(cptr, token);                         /* chop up into tokens */
 1545 
 1546     if (tokens <= 0)                                        /* screwed up or empty line */
 1547         return tokens;                                      /* give up */
 1548 
 1549     if (token[0][0] == '#')                                 /* comment line */
 1550         return 0;
 1551 
 1552     switch (get_keyword(token[0], keyset)) {                /* decide what to do */
 1553     case kw_read:                                           /* read config from a specified drive */
 1554         status = vinum_scandisk(&token[1], tokens - 1);     /* read the config from disk */
 1555         break;
 1556 
 1557     case kw_drive:
 1558         config_drive(update);
 1559         break;
 1560 
 1561     case kw_subdisk:
 1562         config_subdisk(update);
 1563         break;
 1564 
 1565     case kw_plex:
 1566         config_plex(update);
 1567         break;
 1568 
 1569     case kw_volume:
 1570         config_volume(update);
 1571         break;
 1572 
 1573         /* Anything else is invalid in this context */
 1574     default:
 1575         throw_rude_remark(EINVAL,                           /* should we die? */
 1576             "Invalid configuration information: %s",
 1577             token[0]);
 1578     }
 1579     return status;
 1580 }
 1581 
 1582 /*
 1583  * parse a line handed in from userland via ioctl.
 1584  * This differs only by the error reporting mechanism:
 1585  * we return the error indication in the reply to the
 1586  * ioctl, so we need to set a global static pointer in
 1587  * this file.  This technique works because we have
 1588  * ensured that configuration is performed in a single-
 1589  * threaded manner
 1590  */
 1591 int
 1592 parse_user_config(char *cptr, struct keywordset *keyset)
 1593 {
 1594     int status;
 1595 
 1596     ioctl_reply = (struct _ioctl_reply *) cptr;
 1597     status = parse_config(cptr, keyset, 0);
 1598     if (status == ENOENT)                                   /* from scandisk, but it can't tell us */
 1599         strcpy(ioctl_reply->msg, "no drives found");
 1600     ioctl_reply = NULL;                                     /* don't do this again */
 1601     return status;
 1602 }
 1603 
 1604 /* Remove an object */
 1605 void
 1606 remove(struct vinum_ioctl_msg *msg)
 1607 {
 1608     struct vinum_ioctl_msg message = *msg;                  /* make a copy to hand on */
 1609 
 1610     ioctl_reply = (struct _ioctl_reply *) msg;              /* reinstate the address to reply to */
 1611     ioctl_reply->error = 0;                                 /* no error, */
 1612     ioctl_reply->msg[0] = '\0';                             /* no message */
 1613 
 1614     switch (message.type) {
 1615     case drive_object:
 1616         remove_drive_entry(message.index, message.force);
 1617         updateconfig(0);
 1618         return;
 1619 
 1620     case sd_object:
 1621         remove_sd_entry(message.index, message.force, message.recurse);
 1622         updateconfig(0);
 1623         return;
 1624 
 1625     case plex_object:
 1626         remove_plex_entry(message.index, message.force, message.recurse);
 1627         updateconfig(0);
 1628         return;
 1629 
 1630     case volume_object:
 1631         remove_volume_entry(message.index, message.force, message.recurse);
 1632         updateconfig(0);
 1633         return;
 1634 
 1635     default:
 1636         ioctl_reply->error = EINVAL;
 1637         strcpy(ioctl_reply->msg, "Invalid object type");
 1638     }
 1639 }
 1640 
 1641 /* Remove a drive.  */
 1642 void
 1643 remove_drive_entry(int driveno, int force)
 1644 {
 1645     struct drive *drive = &DRIVE[driveno];
 1646     int sdno;
 1647 
 1648     if ((driveno > vinum_conf.drives_allocated)             /* not a valid drive */
 1649     ||(drive->state == drive_unallocated)) {                /* or nothing there */
 1650         ioctl_reply->error = EINVAL;
 1651         strcpy(ioctl_reply->msg, "No such drive");
 1652     } else if (drive->opencount > 0) {                      /* we have subdisks */
 1653         if (force) {                                        /* do it at any cost */
 1654             for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
 1655                 if ((SD[sdno].state != sd_unallocated)      /* subdisk is allocated */
 1656                 &&(SD[sdno].driveno == driveno))            /* and it belongs to this drive */
 1657                     remove_sd_entry(sdno, force, 0);
 1658             }
 1659             remove_drive(driveno);                          /* now remove it */
 1660             vinum_conf.drives_used--;                       /* one less drive */
 1661         } else
 1662             ioctl_reply->error = EBUSY;                     /* can't do that */
 1663     } else {
 1664         remove_drive(driveno);                              /* just remove it */
 1665         vinum_conf.drives_used--;                           /* one less drive */
 1666     }
 1667 }
 1668 
 1669 /* remove a subdisk */
 1670 void
 1671 remove_sd_entry(int sdno, int force, int recurse)
 1672 {
 1673     struct sd *sd = &SD[sdno];
 1674 
 1675     if ((sdno > vinum_conf.subdisks_allocated)              /* not a valid sd */
 1676     ||(sd->state == sd_unallocated)) {                      /* or nothing there */
 1677         ioctl_reply->error = EINVAL;
 1678         strcpy(ioctl_reply->msg, "No such subdisk");
 1679     } else if (sd->flags & VF_OPEN) {                       /* we're open */
 1680         ioctl_reply->error = EBUSY;                         /* no getting around that */
 1681         return;
 1682     } else if (sd->plexno >= 0) {                           /* we have a plex */
 1683         if (force) {                                        /* do it at any cost */
 1684             struct plex *plex = &PLEX[sd->plexno];          /* point to our plex */
 1685             int mysdno;
 1686 
 1687             for (mysdno = 0;                                /* look for ourselves */
 1688                 mysdno < plex->subdisks && &SD[plex->sdnos[mysdno]] != sd;
 1689                 mysdno++);
 1690             if (mysdno == plex->subdisks)                   /* didn't find it */
 1691                 log(LOG_ERR,
 1692                     "Error removing subdisk %s: not found in plex %s\n",
 1693                     SD[mysdno].name,
 1694                     plex->name);
 1695             else {                                          /* remove the subdisk from plex */
 1696                 if (mysdno < (plex->subdisks - 1))          /* not the last subdisk */
 1697                     bcopy(&plex->sdnos[mysdno + 1],
 1698                         &plex->sdnos[mysdno],
 1699                         (plex->subdisks - 1 - mysdno) * sizeof(int));
 1700                 plex->subdisks--;
 1701                 sd->plexno = -1;                            /* disown the subdisk */
 1702             }
 1703 
 1704             /*
 1705              * Removing a subdisk from a striped or
 1706              * RAID-4 or RAID-5 plex really tears the
 1707              * hell out of the structure, and it needs
 1708              * to be reinitialized.
 1709              */
 1710             if (plex->organization != plex_concat)          /* not concatenated, */
 1711                 set_plex_state(plex->plexno, plex_faulty, setstate_force); /* need to reinitialize */
 1712             log(LOG_INFO, "vinum: removing %s\n", sd->name);
 1713             free_sd(sdno);
 1714         } else
 1715             ioctl_reply->error = EBUSY;                     /* can't do that */
 1716     } else {
 1717         log(LOG_INFO, "vinum: removing %s\n", sd->name);
 1718         free_sd(sdno);
 1719     }
 1720 }
 1721 
 1722 /* remove a plex */
 1723 void
 1724 remove_plex_entry(int plexno, int force, int recurse)
 1725 {
 1726     struct plex *plex = &PLEX[plexno];
 1727     int sdno;
 1728 
 1729     if ((plexno > vinum_conf.plexes_allocated)              /* not a valid plex */
 1730     ||(plex->state == plex_unallocated)) {                  /* or nothing there */
 1731         ioctl_reply->error = EINVAL;
 1732         strcpy(ioctl_reply->msg, "No such plex");
 1733     } else if (plex->flags & VF_OPEN) {                     /* we're open */
 1734         ioctl_reply->error = EBUSY;                         /* no getting around that */
 1735         return;
 1736     }
 1737     if (plex->subdisks) {
 1738         if (force) {                                        /* do it anyway */
 1739             if (recurse) {                                  /* remove all below */
 1740                 int sds = plex->subdisks;
 1741                 for (sdno = 0; sdno < sds; sdno++)
 1742                     free_sd(plex->sdnos[sdno]);             /* free all subdisks */
 1743             } else {                                        /* just tear them out */
 1744                 int sds = plex->subdisks;
 1745                 for (sdno = 0; sdno < sds; sdno++)
 1746                     SD[plex->sdnos[sdno]].plexno = -1;      /* no plex any more */
 1747             }
 1748         } else {                                            /* can't do it without force */
 1749             ioctl_reply->error = EBUSY;                     /* can't do that */
 1750             return;
 1751         }
 1752     }
 1753     if (plex->volno >= 0) {                                 /* we are part of a volume */
 1754         if (force) {                                        /* do it at any cost */
 1755             struct volume *vol = &VOL[plex->volno];
 1756             int myplexno;
 1757 
 1758             for (myplexno = 0; myplexno < vol->plexes; myplexno++)
 1759                 if (vol->plex[myplexno] == plexno)          /* found it */
 1760                     break;
 1761             if (myplexno == vol->plexes)                    /* didn't find it.  Huh? */
 1762                 log(LOG_ERR,
 1763                     "Error removing plex %s: not found in volume %s\n",
 1764                     plex->name,
 1765                     vol->name);
 1766             if (myplexno < (vol->plexes - 1))               /* not the last plex in the list */
 1767                 bcopy(&vol->plex[myplexno + 1],
 1768                     &vol->plex[myplexno],
 1769                     vol->plexes - 1 - myplexno);
 1770             vol->plexes--;
 1771         } else {
 1772             ioctl_reply->error = EBUSY;                     /* can't do that */
 1773             return;
 1774         }
 1775     }
 1776     log(LOG_INFO, "vinum: removing %s\n", plex->name);
 1777     free_plex(plexno);
 1778     vinum_conf.plexes_used--;                               /* one less plex */
 1779 }
 1780 
 1781 /* remove a volume */
 1782 void
 1783 remove_volume_entry(int volno, int force, int recurse)
 1784 {
 1785     struct volume *vol = &VOL[volno];
 1786     int plexno;
 1787 
 1788     if ((volno > vinum_conf.volumes_allocated)              /* not a valid volume */
 1789     ||(vol->state == volume_unallocated)) {                 /* or nothing there */
 1790         ioctl_reply->error = EINVAL;
 1791         strcpy(ioctl_reply->msg, "No such volume");
 1792     } else if (vol->flags & VF_OPEN)                        /* we're open */
 1793         ioctl_reply->error = EBUSY;                         /* no getting around that */
 1794     else if (vol->plexes) {
 1795         if (recurse && force) {                             /* remove all below */
 1796             int plexes = vol->plexes;
 1797 
 1798 /*       for (plexno = plexes - 1; plexno >= 0; plexno--) */
 1799             for (plexno = 0; plexno < plexes; plexno++)
 1800                 remove_plex_entry(vol->plex[plexno], force, recurse);
 1801             log(LOG_INFO, "vinum: removing %s\n", vol->name);
 1802             free_volume(volno);
 1803             vinum_conf.volumes_used--;                      /* one less volume */
 1804         } else
 1805             ioctl_reply->error = EBUSY;                     /* can't do that */
 1806     } else {
 1807         log(LOG_INFO, "vinum: removing %s\n", vol->name);
 1808         free_volume(volno);
 1809         vinum_conf.volumes_used--;                          /* one less volume */
 1810     }
 1811 }
 1812 
 1813 /* Currently called only from ioctl */
 1814 void
 1815 update_sd_config(int sdno, int diskconfig)
 1816 {
 1817     if (!diskconfig)
 1818         set_sd_state(sdno, sd_up, setstate_configuring);
 1819     SD[sdno].flags &= ~VF_NEWBORN;
 1820 }
 1821 
 1822 void
 1823 update_plex_config(int plexno, int diskconfig)
 1824 {
 1825     u_int64_t size;
 1826     int sdno;
 1827     struct plex *plex = &PLEX[plexno];
 1828     enum plexstate state = plex_up;                         /* state we want the plex in */
 1829     int remainder;                                          /* size of fractional stripe at end */
 1830     int added_plex;                                         /* set if we add a plex to a volume */
 1831     int required_sds;                                       /* number of subdisks we need */
 1832     struct sd *sd;
 1833     struct volume *vol;
 1834     int data_sds;                                           /* number of sds carrying data */
 1835 
 1836     if (plex->state < plex_init)                            /* not a real plex, */
 1837         return;
 1838     added_plex = 0;
 1839     if (plex->volno >= 0) {                                 /* we have a volume */
 1840         vol = &VOL[plex->volno];
 1841 
 1842         /*
 1843          * If we're newly born,
 1844          * and the volume isn't,
 1845          * and it has other plexes,
 1846          * and we didn't read this mess from disk,
 1847          * we were added later.
 1848          */
 1849         if ((plex->flags & VF_NEWBORN)
 1850             && ((vol->flags & VF_NEWBORN) == 0)
 1851             && (vol->plexes > 0)
 1852             && (diskconfig == 0)) {
 1853             added_plex = 1;
 1854             state = plex_down;                              /* so take ourselves down */
 1855         }
 1856     }
 1857     /*
 1858      * Check that our subdisks make sense.  For
 1859      * striped, RAID-4 and RAID-5 plexes, we need at
 1860      * least two subdisks, and they must all be the
 1861      * same size.
 1862      */
 1863     if (plex->organization == plex_striped) {
 1864         data_sds = plex->subdisks;
 1865         required_sds = 2;
 1866     } else if (isparity(plex)) {                            /* RAID 4 or 5 */
 1867         data_sds = plex->subdisks - 1;
 1868         required_sds = 3;
 1869     } else
 1870         required_sds = 0;
 1871     if (required_sds > 0) {                                 /* striped, RAID-4 or RAID-5 */
 1872         if (plex->subdisks < required_sds) {
 1873             log(LOG_ERR,
 1874                 "vinum: plex %s does not have at least %d subdisks\n",
 1875                 plex->name,
 1876                 required_sds);
 1877             state = plex_faulty;
 1878         }
 1879         /*
 1880          * Now see if the plex size is a multiple of
 1881          * the stripe size.  If not, trim off the end
 1882          * of each subdisk and return it to the drive.
 1883          */
 1884         if (plex->length > 0) {
 1885             if (data_sds > 0) {
 1886                 if (plex->stripesize > 0) {
 1887                     remainder = (int) (plex->length         /* are we exact? */
 1888                         % ((u_int64_t) plex->stripesize * data_sds));
 1889                     if (remainder) {                        /* no */
 1890                         log(LOG_INFO, "vinum: removing %d blocks of partial stripe at the end of %s\n",
 1891                             remainder,
 1892                             plex->name);
 1893                         plex->length -= remainder;          /* shorten the plex */
 1894                         remainder /= data_sds;              /* spread the remainder amongst the sds */
 1895                         for (sdno = 0; sdno < plex->subdisks; sdno++) {
 1896                             sd = &SD[plex->sdnos[sdno]];    /* point to the subdisk */
 1897                             return_drive_space(sd->driveno, /* return the space */
 1898                                 sd->driveoffset + sd->sectors - remainder,
 1899                                 remainder);
 1900                             sd->sectors -= remainder;       /* and shorten it */
 1901                         }
 1902                     }
 1903                 } else                                      /* no data sds, */
 1904                     plex->length = 0;                       /* reset length */
 1905             }
 1906         }
 1907     }
 1908     size = 0;
 1909     for (sdno = 0; sdno < plex->subdisks; sdno++) {
 1910         sd = &SD[plex->sdnos[sdno]];
 1911         if (isstriped(plex)
 1912             && (sdno > 0)
 1913             && (sd->sectors != SD[plex->sdnos[sdno - 1]].sectors)) {
 1914             log(LOG_ERR, "vinum: %s must have equal sized subdisks\n", plex->name);
 1915             state = plex_down;
 1916         }
 1917         size += sd->sectors;
 1918         if (added_plex)                                     /* we were added later */
 1919             sd->state = sd_stale;                           /* stale until proven otherwise */
 1920     }
 1921 
 1922     if (plex->subdisks) {                                   /* plex has subdisks, calculate size */
 1923         /*
 1924          * XXX We shouldn't need to calculate the size any
 1925          * more.  Check this some time
 1926          */
 1927         if (isparity(plex))
 1928             size = size / plex->subdisks * (plex->subdisks - 1); /* less space for RAID-4 and RAID-5 */
 1929         if (plex->length != size)
 1930             log(LOG_INFO,
 1931                 "Correcting length of %s: was %lld, is %lld\n",
 1932                 plex->name,
 1933                 (long long) plex->length,
 1934                 (long long) size);
 1935         plex->length = size;
 1936     } else {                                                /* no subdisks, */
 1937         plex->length = 0;                                   /* no size */
 1938         state = plex_down;                                  /* take it down */
 1939     }
 1940     update_plex_state(plexno);                              /* set the state */
 1941     plex->flags &= ~VF_NEWBORN;
 1942 }
 1943 
 1944 void
 1945 update_volume_config(int volno, int diskconfig)
 1946 {
 1947     struct volume *vol = &VOL[volno];
 1948     struct plex *plex;
 1949     int plexno;
 1950 
 1951     if (vol->state != volume_unallocated)
 1952         /*
 1953          * Recalculate the size of the volume,
 1954          * which might change if the original
 1955          * plexes were not a multiple of the
 1956          * stripe size.
 1957          */
 1958     {
 1959         vol->size = 0;
 1960         for (plexno = 0; plexno < vol->plexes; plexno++) {
 1961             plex = &PLEX[vol->plex[plexno]];
 1962             vol->size = max(plex->length, vol->size);       /* maximum size */
 1963             plex->volplexno = plexno;                       /* note it in the plex */
 1964         }
 1965     }
 1966     vol->flags &= ~VF_NEWBORN;                              /* no longer newly born */
 1967 }
 1968 
 1969 /*
 1970  * Update the global configuration.
 1971  * diskconfig is != 0 if we're reading in a config
 1972  * from disk.  In this case, we don't try to
 1973  * bring the devices up, though we will bring
 1974  * them down if there's some error which got
 1975  * missed when writing to disk.
 1976  */
 1977 void
 1978 updateconfig(int diskconfig)
 1979 {
 1980     int plexno;
 1981     int volno;
 1982 
 1983     for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++)
 1984         update_plex_config(plexno, diskconfig);
 1985 
 1986     for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
 1987         if (VOL[volno].state > volume_uninit) {
 1988             VOL[volno].flags &= ~VF_CONFIG_SETUPSTATE;      /* no more setupstate */
 1989             update_volume_state(volno);
 1990             update_volume_config(volno, diskconfig);
 1991         }
 1992     }
 1993     save_config();
 1994 }
 1995 
 1996 /*
 1997  * Start manual changes to the configuration and lock out
 1998  * others who may wish to do so.
 1999  * XXX why do we need this and lock_config too?
 2000  */
 2001 int
 2002 start_config(int force)
 2003 {
 2004     int error;
 2005 
 2006     current_drive = -1;                                     /* note the last drive we mention, for
 2007                                                             * some defaults */
 2008     current_plex = -1;                                      /* and the same for the last plex */
 2009     current_volume = -1;                                    /* and the last volume */
 2010     while ((vinum_conf.flags & VF_CONFIGURING) != 0) {
 2011         vinum_conf.flags |= VF_WILL_CONFIGURE;
 2012         if ((error = tsleep(&vinum_conf, PRIBIO | PCATCH, "vincfg", 0)) != 0)
 2013             return error;
 2014     }
 2015     /*
 2016      * We need two flags here: VF_CONFIGURING
 2017      * tells other processes to hold off (this
 2018      * function), and VF_CONFIG_INCOMPLETE
 2019      * tells the state change routines not to
 2020      * propagate incrememntal state changes
 2021      */
 2022     vinum_conf.flags |= VF_CONFIGURING | VF_CONFIG_INCOMPLETE;
 2023     if (force)
 2024         vinum_conf.flags |= VF_FORCECONFIG;                 /* overwrite differently named drives */
 2025     current_drive = -1;                                     /* reset the defaults */
 2026     current_plex = -1;                                      /* and the same for the last plex */
 2027     current_volume = -1;                                    /* and the last volme */
 2028     return 0;
 2029 }
 2030 
 2031 /*
 2032  * Update the config if update is 1, and unlock
 2033  * it.  We won't update the configuration if we
 2034  * are called in a recursive loop via throw_rude_remark.
 2035  */
 2036 void
 2037 finish_config(int update)
 2038 {
 2039     /* we've finished our config */
 2040     vinum_conf.flags &= ~(VF_CONFIG_INCOMPLETE | VF_READING_CONFIG | VF_FORCECONFIG);
 2041     if (update)
 2042         updateconfig(0);                                    /* so update things */
 2043     else
 2044         updateconfig(1);                                    /* do some updates only */
 2045     vinum_conf.flags &= ~VF_CONFIGURING;                    /* and now other people can take a turn */
 2046     if ((vinum_conf.flags & VF_WILL_CONFIGURE) != 0) {
 2047         vinum_conf.flags &= ~VF_WILL_CONFIGURE;
 2048         wakeup_one(&vinum_conf);
 2049     }
 2050 }
 2051 /* Local Variables: */
 2052 /* fill-column: 50 */
 2053 /* End: */

Cache object: 8a801927bdf66baee0f11d0555767b4a


[ 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.