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