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

Cache object: 77a8ee16194b7dec6cb65b9c66a3b3cc


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