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

Cache object: 8ce4cc40834976e9da5b5383a40e62d3


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