4 * Don't store drive configuration on the config DB: read each drive's header
5 * to decide where it is.
7 * Accept any old crap in the config_<foo> functions, and complain when
8 * we try to bring it up.
10 * When trying to bring volumes up, check that the complete address range
14 * Copyright (c) 1997, 1998
15 * Nan Yang Computer Services Limited. All rights reserved.
17 * This software is distributed under the so-called ``Berkeley
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 * $Id: vinumconfig.c,v 1.30 2000/05/01 09:45:50 grog Exp grog $
54 #include <dev/vinum/vinumhdr.h>
55 #include <dev/vinum/request.h>
57 #define MAXTOKEN 64 /* maximum number of tokens in a line */
60 * We can afford the luxury of global variables here,
61 * since start_config ensures that these functions
62 * are single-threaded.
65 /* These are indices in vinum_conf of the last-mentioned of each kind of object */
66 static int current_drive; /* note the last drive we mention, for
68 static int current_plex; /* and the same for the last plex */
69 static int current_volume; /* and the last volme */
70 static struct _ioctl_reply *ioctl_reply; /* struct to return via ioctl */
73 /* These values are used by most of these routines, so set them as globals */
74 static char *token[MAXTOKEN]; /* pointers to individual tokens */
75 static int tokens; /* number of tokens */
86 #define MSG_MAX 1024 /* maximum length of a formatted message */
88 * Format an error message and return to the user in the reply.
89 * CARE: This routine is designed to be called only from the
90 * configuration routines, so it assumes it's the owner of
91 * the configuration lock, and unlocks it on exit
94 throw_rude_remark(int error, char *msg,...)
99 static int finishing; /* don't recurse */
102 if ((vinum_conf.flags & VF_LOCKED) == 0) /* bug catcher */
103 panic ("throw_rude_remark: called without config lock");
105 if ((ioctl_reply != NULL) /* we're called from the user */
106 &&(!(vinum_conf.flags & VF_READING_CONFIG))) { /* and not reading from disk: return msg */
108 * We can't just format to ioctl_reply, since it
109 * may contain our input parameters
111 text = Malloc(MSG_MAX);
113 log(LOG_ERR, "vinum: can't allocate error message buffer\n");
115 vprintf(msg, ap); /* print to the console */
118 retval = kvprintf(msg, NULL, (void *) text, 10, ap);
119 text[retval] = '\0'; /* delimit */
120 strcpy(ioctl_reply->msg, text);
121 ioctl_reply->error = error; /* first byte is the error number */
126 vprintf(msg, ap); /* print to the console */
131 if (vinum_conf.flags & VF_READING_CONFIG) { /* go through to the bitter end, */
132 if ((vinum_conf.flags & VF_READING_CONFIG) /* we're reading from disk, */
133 &&((daemon_options & daemon_noupdate) == 0)) {
134 log(LOG_NOTICE, "Disabling configuration updates\n");
135 daemon_options |= daemon_noupdate;
140 * We have a problem here: we want to unlock the
141 * configuration, which implies tidying up, but
142 * if we find an error while tidying up, we could
143 * recurse for ever. Use this kludge to only try
146 was_finishing = finishing;
148 finish_config(was_finishing); /* unlock anything we may be holding */
149 finishing = was_finishing;
150 longjmp(command_fail, error);
154 * Check a volume to see if the plex is already assigned to it.
155 * Return index in volume->plex, or -1 if not assigned
158 my_plex(int volno, int plexno)
163 vol = &VOL[volno]; /* point to volno */
164 for (i = 0; i < vol->plexes; i++)
165 if (vol->plex[i] == plexno)
167 return -1; /* not found */
171 * Check a plex to see if the subdisk is already assigned to it.
172 * Return index in plex->sd, or -1 if not assigned
175 my_sd(int plexno, int sdno)
180 plex = &PLEX[plexno];
181 for (i = 0; i < plex->subdisks; i++)
182 if (plex->sdnos[i] == sdno)
184 return -1; /* not found */
187 /* Add plex to the volume if possible */
189 give_plex_to_volume(int volno, int plexno)
195 * It's not an error for the plex to already
196 * belong to the volume, but we need to check a
197 * number of things to make sure it's done right.
200 if (my_plex(volno, plexno) >= 0)
201 return plexno; /* that's it */
203 vol = &VOL[volno]; /* point to volume */
204 if (vol->plexes == MAXPLEX) /* all plexes allocated */
205 throw_rude_remark(ENOSPC,
206 "Too many plexes for volume %s",
208 else if ((vol->plexes > 0) /* we have other plexes */
209 &&((vol->flags & VF_CONFIG_SETUPSTATE) == 0)) /* and we're not setting up state */
210 invalidate_subdisks(&PLEX[plexno], sd_stale); /* make the subdisks invalid */
211 vol->plex[vol->plexes] = plexno; /* this one */
212 vol->plexes++; /* add another plex */
213 PLEX[plexno].volno = volno; /* note the number of our volume */
215 /* Find out how big our volume is */
216 for (i = 0; i < vol->plexes; i++)
217 vol->size = max(vol->size, PLEX[vol->plex[i]].length);
218 return vol->plexes - 1; /* and return its index */
222 * Add subdisk to a plex if possible
225 give_sd_to_plex(int plexno, int sdno)
232 * It's not an error for the sd to already
233 * belong to the plex, but we need to check a
234 * number of things to make sure it's done right.
237 i = my_sd(plexno, sdno);
238 if (i >= 0) /* does it already belong to us? */
239 return i; /* that's it */
241 plex = &PLEX[plexno]; /* point to the plex */
242 sd = &SD[sdno]; /* and the subdisk */
244 /* Do we have an offset? Otherwise put it after the last one */
245 if (sd->plexoffset < 0) { /* no offset specified */
246 if (plex->subdisks > 0) {
247 struct sd *lastsd = &SD[plex->sdnos[plex->subdisks - 1]]; /* last subdisk */
249 if (plex->organization == plex_concat) /* concat, */
250 sd->plexoffset = lastsd->sectors + lastsd->plexoffset; /* starts here */
251 else /* striped, RAID-4 or RAID-5 */
252 sd->plexoffset = plex->stripesize * plex->subdisks; /* starts here */
253 } else /* first subdisk */
254 sd->plexoffset = 0; /* start at the beginning */
256 if (plex->subdisks == MAXSD) /* we already have our maximum */
257 throw_rude_remark(ENOSPC, /* crap out */
258 "Can't add %s to %s: plex full",
262 plex->subdisks++; /* another entry */
263 if (plex->subdisks >= plex->subdisks_allocated) /* need more space */
264 EXPAND(plex->sdnos, int, plex->subdisks_allocated, INITIAL_SUBDISKS_IN_PLEX);
266 /* Adjust size of plex and volume. */
267 if (isparity(plex)) /* RAID-4 or RAID-5 */
268 plex->length = (plex->subdisks - 1) * sd->sectors; /* size is one disk short */
270 plex->length += sd->sectors; /* plex gets this much bigger */
271 if (plex->volno >= 0) /* we have a volume */
272 VOL[plex->volno].size = max(VOL[plex->volno].size, plex->length); /* adjust its size */
275 * We need to check that the subdisks don't overlap,
276 * but we can't do that until a point where we *must*
277 * know the size of all the subdisks. That's not
278 * here. But we need to sort them by offset
280 for (i = 0; i < plex->subdisks - 1; i++) {
281 if (sd->plexoffset < SD[plex->sdnos[i]].plexoffset) { /* it fits before this one */
282 /* First move any remaining subdisks by one */
285 for (j = plex->subdisks - 1; j > i; j--) /* move up one at a time */
286 plex->sdnos[j] = plex->sdnos[j - 1];
287 plex->sdnos[i] = sdno;
288 sd->plexsdno = i; /* note where we are in the subdisk */
294 * The plex doesn't have any subdisk with a
295 * larger offset. Insert it here.
297 plex->sdnos[i] = sdno;
298 sd->plexsdno = i; /* note where we are in the subdisk */
299 sd->plexno = plex->plexno; /* and who we belong to */
304 * Add a subdisk to drive if possible. The
305 * pointer to the drive must already be stored in
306 * the sd structure, but the drive doesn't know
307 * about the subdisk yet.
310 give_sd_to_drive(int sdno)
312 struct sd *sd; /* pointer to subdisk */
313 struct drive *drive; /* and drive */
314 int fe; /* index in free list */
315 int sfe; /* and index of subdisk when assigning max */
317 sd = &SD[sdno]; /* point to sd */
318 drive = &DRIVE[sd->driveno]; /* and drive */
320 if (drive->state != drive_up) {
321 update_sd_state(sdno); /* that crashes the subdisk */
324 if (drive->flags & VF_HOTSPARE) /* the drive is a hot spare, */
325 throw_rude_remark(ENOSPC,
326 "Can't place %s on hot spare drive %s",
329 if ((drive->sectors_available == 0) /* no space left */
330 ||(sd->sectors > drive->sectors_available)) { /* or too big, */
331 sd->driveoffset = -1; /* don't be confusing */
333 throw_rude_remark(ENOSPC, "No space for %s on %s", sd->name, drive->label.name);
334 return; /* in case we come back here */
336 drive->subdisks_used++; /* one more subdisk */
338 if (sd->sectors == 0) { /* take the largest chunk */
339 sfe = 0; /* to keep the compiler happy */
340 for (fe = 0; fe < drive->freelist_entries; fe++) {
341 if (drive->freelist[fe].sectors >= sd->sectors) { /* more space here */
342 sd->sectors = drive->freelist[fe].sectors; /* take it */
343 sd->driveoffset = drive->freelist[fe].offset;
344 sfe = fe; /* and note the index for later */
347 if (sd->sectors == 0) { /* no luck, */
348 sd->driveoffset = -1; /* don't be confusing */
350 throw_rude_remark(ENOSPC, /* give up */
351 "No space for %s on %s",
355 if (sfe < (drive->freelist_entries - 1)) /* not the last one, */
356 bcopy(&drive->freelist[sfe + 1],
357 &drive->freelist[sfe],
358 (drive->freelist_entries - sfe) * sizeof(struct drive_freelist));
359 drive->freelist_entries--; /* one less entry */
360 drive->sectors_available -= sd->sectors; /* and note how much less space we have */
361 } else if (sd->driveoffset < 0) { /* no offset specified, find one */
362 for (fe = 0; fe < drive->freelist_entries; fe++) {
363 if (drive->freelist[fe].sectors >= sd->sectors) { /* it'll fit here */
364 sd->driveoffset = drive->freelist[fe].offset;
365 if (sd->sectors == drive->freelist[fe].sectors) { /* used up the entire entry */
366 if (fe < (drive->freelist_entries - 1)) /* not the last one, */
367 bcopy(&drive->freelist[fe + 1],
368 &drive->freelist[fe],
369 (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
370 drive->freelist_entries--; /* one less entry */
372 drive->freelist[fe].sectors -= sd->sectors; /* this much less space */
373 drive->freelist[fe].offset += sd->sectors; /* this much further on */
375 drive->sectors_available -= sd->sectors; /* and note how much less space we have */
379 if (sd->driveoffset < 0)
381 * Didn't find anything. Although the drive has
382 * enough space, it's too fragmented
386 throw_rude_remark(ENOSPC, "No space for %s on %s", sd->name, drive->label.name);
388 } else { /* specific offset */
390 * For a specific offset to work, the space must be
391 * entirely in a single freelist entry. Look for it.
393 u_int64_t sdend = sd->driveoffset + sd->sectors; /* end of our subdisk */
394 for (fe = 0; fe < drive->freelist_entries; fe++) {
395 u_int64_t dend = drive->freelist[fe].offset + drive->freelist[fe].sectors; /* end of entry */
396 if (dend >= sdend) { /* fits before here */
397 if (drive->freelist[fe].offset > sd->driveoffset) { /* starts after the beginning of sd area */
398 sd->driveoffset = -1; /* don't be confusing */
399 set_sd_state(sd->sdno, sd_down, setstate_force);
400 throw_rude_remark(ENOSPC,
401 "No space for %s on drive %s at offset %lld",
408 * We've found the space, and we can allocate it.
409 * We don't need to say that to the subdisk, which
410 * already knows about it. We need to tell it to
411 * the free list, though. We have four possibilities:
413 * 1. The subdisk exactly eats up the entry. That's the
415 * 2. The subdisk starts at the beginning and leaves space
417 * 3. The subdisk starts after the beginning and leaves
418 * space at the end as well: we end up with another
420 * 4. The subdisk leaves space at the beginning and finishes
423 drive->sectors_available -= sd->sectors; /* note how much less space we have */
424 if (sd->driveoffset == drive->freelist[fe].offset) { /* 1 or 2 */
425 if (sd->sectors == drive->freelist[fe].sectors) { /* 1: used up the entire entry */
426 if (fe < (drive->freelist_entries - 1)) /* not the last one, */
427 bcopy(&drive->freelist[fe + 1],
428 &drive->freelist[fe],
429 (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
430 drive->freelist_entries--; /* one less entry */
431 } else { /* 2: space at the end */
432 drive->freelist[fe].sectors -= sd->sectors; /* this much less space */
433 drive->freelist[fe].offset += sd->sectors; /* this much further on */
435 } else { /* 3 or 4 */
436 drive->freelist[fe].sectors = sd->driveoffset - drive->freelist[fe].offset;
437 if (dend > sdend) { /* 3: space at the end as well */
438 if (fe < (drive->freelist_entries - 1)) /* not the last one */
439 bcopy(&drive->freelist[fe], /* move the rest down */
440 &drive->freelist[fe + 1],
441 (drive->freelist_entries - fe) * sizeof(struct drive_freelist));
442 drive->freelist_entries++; /* one less entry */
443 drive->freelist[fe + 1].offset = sdend; /* second entry starts after sd */
444 drive->freelist[fe + 1].sectors = dend - sdend; /* and is this long */
451 drive->opencount++; /* one more subdisk attached */
454 /* Get an empty drive entry from the drive table */
456 get_empty_drive(void)
461 /* first see if we have one which has been deallocated */
462 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
463 if (DRIVE[driveno].state == drive_unallocated) /* bingo */
467 if (driveno >= vinum_conf.drives_allocated) /* we've used all our allocation */
468 EXPAND(DRIVE, struct drive, vinum_conf.drives_allocated, INITIAL_DRIVES);
470 /* got a drive entry. Make it pretty */
471 drive = &DRIVE[driveno];
472 bzero(drive, sizeof(struct drive));
473 drive->driveno = driveno; /* put number in structure */
474 drive->flags |= VF_NEWBORN; /* newly born drive */
475 strcpy("unknown", drive->devicename); /* and make the name ``unknown'' */
476 return driveno; /* return the index */
480 * Find the named drive in vinum_conf.drive, return a pointer
481 * return the index in vinum_conf.drive.
482 * Don't mark the drive as allocated (XXX SMP)
483 * If create != 0, create an entry if it doesn't exist
485 /* XXX check if we have it open from attach */
487 find_drive(const char *name, int create)
493 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
494 drive = &DRIVE[driveno]; /* point to drive */
495 if ((drive->label.name[0] != '\0') /* it has a name */
496 &&(strcmp(drive->label.name, name) == 0) /* and it's this one */
497 &&(drive->state > drive_unallocated)) /* and it's a real one: found */
501 /* the drive isn't in the list. Add it if he wants */
502 if (create == 0) /* don't want to create */
503 return -1; /* give up */
505 driveno = get_empty_drive();
506 drive = &DRIVE[driveno];
508 bcopy(name, /* put in its name */
510 min(sizeof(drive->label.name),
512 drive->state = drive_referenced; /* in use, nothing worthwhile there */
513 return driveno; /* return the index */
517 * Find a drive given its device name.
518 * devname must be valid.
519 * Otherwise the same as find_drive above
522 find_drive_by_dev(const char *devname, int create)
527 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
528 drive = &DRIVE[driveno]; /* point to drive */
529 if ((strcmp(drive->devicename, devname) == 0) /* it's this device */
530 &&(drive->state > drive_unallocated)) /* and it's a real one: found */
534 /* the drive isn't in the list. Add it if he wants */
535 if (create == 0) /* don't want to create */
536 return -1; /* give up */
538 driveno = get_empty_drive();
539 drive = &DRIVE[driveno];
540 bcopy(devname, /* put in its name */
542 min(sizeof(drive->devicename),
544 drive->state = drive_referenced; /* in use, nothing worthwhile there */
545 return driveno; /* return the index */
548 /* Find an empty subdisk in the subdisk table */
555 /* first see if we have one which has been deallocated */
556 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
557 if (SD[sdno].state == sd_unallocated) /* bingo */
560 if (sdno >= vinum_conf.subdisks_allocated)
562 * We've run out of space. sdno is pointing
563 * where we want it, but at the moment we
564 * don't have the space. Get it.
566 EXPAND(SD, struct sd, vinum_conf.subdisks_allocated, INITIAL_SUBDISKS);
568 /* initialize some things */
569 sd = &SD[sdno]; /* point to it */
570 bzero(sd, sizeof(struct sd)); /* initialize */
571 sd->flags |= VF_NEWBORN; /* newly born subdisk */
572 sd->plexno = -1; /* no plex */
573 sd->sectors = -1; /* no space */
574 sd->driveno = -1; /* no drive */
575 sd->plexoffset = -1; /* and no offsets */
576 sd->driveoffset = -1;
577 return sdno; /* return the index */
580 /* return a drive to the free pool */
582 free_drive(struct drive *drive)
584 if ((drive->state > drive_referenced) /* real drive */
585 ||(drive->flags & VF_OPEN)) { /* how can it be open without a state? */
587 if (drive->flags & VF_OPEN) { /* it's open, */
588 close_locked_drive(drive); /* close it */
589 drive->state = drive_down; /* and note the fact */
592 Free(drive->freelist);
593 bzero(drive, sizeof(struct drive)); /* this also sets drive_unallocated */
599 * Find the named subdisk in vinum_conf.sd.
601 * If create != 0, create an entry if it doesn't exist
603 * Return index in vinum_conf.sd
606 find_subdisk(const char *name, int create)
611 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
612 if (strcmp(SD[sdno].name, name) == 0) /* found it */
616 /* the subdisk isn't in the list. Add it if he wants */
617 if (create == 0) /* don't want to create */
618 return -1; /* give up */
620 /* Allocate one and insert the name */
621 sdno = get_empty_sd();
623 bcopy(name, sd->name, min(sizeof(sd->name), strlen(name))); /* put in its name */
624 return sdno; /* return the pointer */
627 /* Return space to a drive */
629 return_drive_space(int driveno, int64_t offset, int length)
632 int fe; /* free list entry */
633 u_int64_t sdend; /* end of our subdisk */
634 u_int64_t dend; /* end of our freelist entry */
636 drive = &DRIVE[driveno];
637 if (drive->state == drive_up) {
638 sdend = offset + length; /* end of our subdisk */
640 /* Look for where to return the sd address space */
642 (fe < drive->freelist_entries) && (drive->freelist[fe].offset < offset);
645 * Now we are pointing to the last entry, the first
646 * with a higher offset than the subdisk, or both.
648 if ((fe > 1) /* not the first entry */
649 &&((fe == drive->freelist_entries) /* gone past the end */
650 ||(drive->freelist[fe].offset > offset))) /* or past the block were looking for */
651 fe--; /* point to the block before */
652 dend = drive->freelist[fe].offset + drive->freelist[fe].sectors; /* end of the entry */
655 * At this point, we are pointing to the correct
656 * place in the free list. A number of possibilities
659 * 1. The block to be freed starts at the end of the
660 * block to which we are pointing. This has two
663 * a. The block to be freed ends at the beginning
664 * of the following block. Merge the three
665 * areas into a single block.
667 * b. The block is shorter than the space between
668 * the current block and the next one. Enlarge
671 * 2. The block to be freed starts after the end
672 * of the block. Again, we have two cases:
674 * a. It ends before the start of the following block.
675 * Create a new free block.
677 * b. It ends at the start of the following block.
678 * Enlarge the following block downwards.
680 * When there is only one free space block, and the
681 * space to be returned is before it, the pointer is
682 * to a non-existent zeroth block. XXX check this
684 if (offset == dend) { /* Case 1: it starts at the end of this block */
685 if ((fe < drive->freelist_entries - 1) /* we're not the last block in the free list */
686 /* and the subdisk ends at the start of the next block */
687 &&(sdend == drive->freelist[fe + 1].offset)) {
688 drive->freelist[fe].sectors /* 1a: merge all three blocks */
689 = drive->freelist[fe + 1].sectors;
690 if (fe < drive->freelist_entries - 2) /* still more blocks after next */
691 bcopy(&drive->freelist[fe + 2], /* move down one */
692 &drive->freelist[fe + 1],
693 (drive->freelist_entries - 2 - fe)
694 * sizeof(struct drive_freelist));
695 drive->freelist_entries--; /* one less entry in the free list */
696 } else /* 1b: just enlarge this block */
697 drive->freelist[fe].sectors += length;
698 } else { /* Case 2 */
699 if (offset > dend) /* it starts after this block */
700 fe++; /* so look at the next block */
701 if ((fe < drive->freelist_entries) /* we're not the last block in the free list */
702 /* and the subdisk ends at the start of this block: case 4 */
703 &&(sdend == drive->freelist[fe].offset)) {
704 drive->freelist[fe].offset = offset; /* it starts where the sd was */
705 drive->freelist[fe].sectors += length; /* and it's this much bigger */
706 } else { /* case 3: non-contiguous */
707 if (fe < drive->freelist_entries) /* not after the last block, */
708 bcopy(&drive->freelist[fe], /* move the rest up one entry */
709 &drive->freelist[fe + 1],
710 (drive->freelist_entries - fe)
711 * sizeof(struct drive_freelist));
712 drive->freelist_entries++; /* one less entry */
713 drive->freelist[fe].offset = offset; /* this entry represents the sd */
714 drive->freelist[fe].sectors = length;
717 drive->sectors_available += length; /* the sectors are now available */
722 * Free an allocated sd entry.
723 * This performs memory management only. remove()
724 * is responsible for checking relationships.
732 if ((sd->driveno >= 0) /* we have a drive, */
733 &&(sd->sectors > 0)) /* and some space on it */
734 return_drive_space(sd->driveno, /* return the space */
738 PLEX[sd->plexno].subdisks--; /* one less subdisk */
739 destroy_dev(sd->dev);
740 bzero(sd, sizeof(struct sd)); /* and clear it out */
741 sd->state = sd_unallocated;
742 vinum_conf.subdisks_used--; /* one less sd */
745 /* Find an empty plex in the plex table */
750 struct plex *plex; /* if we allocate one */
752 /* first see if we have one which has been deallocated */
753 for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++) {
754 if (PLEX[plexno].state == plex_unallocated) /* bingo */
755 break; /* and get out of here */
758 if (plexno >= vinum_conf.plexes_allocated)
759 EXPAND(PLEX, struct plex, vinum_conf.plexes_allocated, INITIAL_PLEXES);
761 /* Found a plex. Give it an sd structure */
762 plex = &PLEX[plexno]; /* this one is ours */
763 bzero(plex, sizeof(struct plex)); /* polish it up */
764 plex->sdnos = (int *) Malloc(sizeof(int) * INITIAL_SUBDISKS_IN_PLEX); /* allocate sd table */
765 CHECKALLOC(plex->sdnos, "vinum: Can't allocate plex subdisk table");
766 bzero(plex->sdnos, (sizeof(int) * INITIAL_SUBDISKS_IN_PLEX)); /* do we need this? */
767 plex->flags |= VF_NEWBORN; /* newly born plex */
768 plex->subdisks = 0; /* no subdisks in use */
769 plex->subdisks_allocated = INITIAL_SUBDISKS_IN_PLEX; /* and we have space for this many */
770 plex->organization = plex_disorg; /* and it's not organized */
771 plex->volno = -1; /* no volume yet */
772 return plexno; /* return the index */
776 * Find the named plex in vinum_conf.plex
778 * If create != 0, create an entry if it doesn't exist
779 * return index in vinum_conf.plex
782 find_plex(const char *name, int create)
787 for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++) {
788 if (strcmp(PLEX[plexno].name, name) == 0) /* found it */
792 /* the plex isn't in the list. Add it if he wants */
793 if (create == 0) /* don't want to create */
794 return -1; /* give up */
796 /* Allocate one and insert the name */
797 plexno = get_empty_plex();
798 plex = &PLEX[plexno]; /* point to it */
799 bcopy(name, plex->name, min(sizeof(plex->name), strlen(name))); /* put in its name */
800 return plexno; /* return the pointer */
804 * Free an allocated plex entry
805 * and its associated memory areas
808 free_plex(int plexno)
812 plex = &PLEX[plexno];
817 destroy_dev(plex->dev);
818 bzero(plex, sizeof(struct plex)); /* and clear it out */
819 plex->state = plex_unallocated;
822 /* Find an empty volume in the volume table */
824 get_empty_volume(void)
830 /* first see if we have one which has been deallocated */
831 for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
832 if (VOL[volno].state == volume_unallocated) /* bingo */
836 if (volno >= vinum_conf.volumes_allocated)
837 EXPAND(VOL, struct volume, vinum_conf.volumes_allocated, INITIAL_VOLUMES);
839 /* Now initialize fields */
841 bzero(vol, sizeof(struct volume));
842 vol->flags |= VF_NEWBORN | VF_CREATED; /* newly born volume */
843 vol->preferred_plex = ROUND_ROBIN_READPOL; /* round robin */
844 for (i = 0; i < MAXPLEX; i++) /* mark the plexes missing */
846 return volno; /* return the index */
850 * Find the named volume in vinum_conf.volume.
852 * If create != 0, create an entry if it doesn't exist
853 * return the index in vinum_conf
856 find_volume(const char *name, int create)
861 for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
862 if (strcmp(VOL[volno].name, name) == 0) /* found it */
866 /* the volume isn't in the list. Add it if he wants */
867 if (create == 0) /* don't want to create */
868 return -1; /* give up */
870 /* Allocate one and insert the name */
871 volno = get_empty_volume();
873 bcopy(name, vol->name, min(sizeof(vol->name), strlen(name))); /* put in its name */
874 vol->blocksize = DEV_BSIZE; /* block size of this volume */
875 return volno; /* return the pointer */
879 * Free an allocated volume entry
880 * and its associated memory areas
883 free_volume(int volno)
888 destroy_dev(vol->dev);
889 bzero(vol, sizeof(struct volume)); /* and clear it out */
890 vol->state = volume_unallocated;
894 * Handle a drive definition. We store the information in the global variable
895 * drive, so we don't need to allocate.
897 * If we find an error, print a message and return
900 config_drive(int update)
902 enum drive_label_info partition_status; /* info about the partition */
904 int driveno; /* index of drive in vinum_conf */
905 struct drive *drive; /* and pointer to it */
906 int otherdriveno; /* index of possible second drive */
909 if (tokens < 2) /* not enough tokens */
910 throw_rude_remark(EINVAL, "Drive has no name\n");
911 driveno = find_drive(token[1], 1); /* allocate a drive to initialize */
912 drive = &DRIVE[driveno]; /* and get a pointer */
913 if (update && ((drive->flags & VF_NEWBORN) == 0)) /* this drive exists already */
914 return; /* don't do anything */
915 drive->flags &= ~VF_NEWBORN; /* no longer newly born */
917 if (drive->state != drive_referenced) { /* we already know this drive */
919 * XXX Check which definition is more up-to-date. Give
920 * preference for the definition on its own drive.
924 for (parameter = 2; parameter < tokens; parameter++) { /* look at the other tokens */
925 switch (get_keyword(token[parameter], &keyword_set)) {
928 otherdriveno = find_drive_by_dev(token[parameter], 0); /* see if it exists already */
929 if (otherdriveno >= 0) { /* yup, */
930 drive->state = drive_unallocated; /* deallocate the drive */
931 throw_rude_remark(EEXIST, /* and complain */
932 "Drive %s would have same device as drive %s",
934 DRIVE[otherdriveno].label.name);
936 if (drive->devicename[0] == '/') { /* we know this drive... */
937 if (strcmp(drive->devicename, token[parameter])) /* different name */
938 close_drive(drive); /* close it if it's open */
942 /* open the device and get the configuration */
943 bcopy(token[parameter], /* insert device information */
945 min(sizeof(drive->devicename),
946 strlen(token[parameter])));
947 partition_status = read_drive_label(drive, 1);
948 switch (partition_status) {
949 case DL_CANT_OPEN: /* not our kind */
951 if (drive->lasterror == EFTYPE) /* wrong kind of partition */
952 throw_rude_remark(drive->lasterror,
953 "Drive %s has invalid partition type",
955 else /* I/O error of some kind */
956 throw_rude_remark(drive->lasterror,
957 "Can't initialize drive %s",
961 case DL_WRONG_DRIVE: /* valid drive, not the name we expected */
962 if (vinum_conf.flags & VF_FORCECONFIG) { /* but we'll accept that */
963 bcopy(token[1], drive->label.name, sizeof(drive->label.name));
968 * There's a potential race condition here:
969 * the rude remark refers to a field in an
970 * unallocated drive, which potentially could
971 * be reused. This works because we're the only
972 * thread accessing the config at the moment.
974 drive->state = drive_unallocated; /* throw it away completely */
975 throw_rude_remark(drive->lasterror,
976 "Incorrect drive name %s specified for drive %s",
981 case DL_DELETED_LABEL: /* it was a drive, but we deleted it */
982 case DL_NOT_OURS: /* nothing to do with the rest */
987 * read_drive_label overwrites the device name.
988 * If we get here, we can have the drive,
989 * so put it back again
991 bcopy(token[parameter],
993 min(sizeof(drive->devicename),
994 strlen(token[parameter])));
998 parameter++; /* skip the keyword */
999 if (vinum_conf.flags & VF_READING_CONFIG)
1000 drive->state = DriveState(token[parameter]); /* set the state */
1003 case kw_hotspare: /* this drive is a hot spare */
1004 drive->flags |= VF_HOTSPARE;
1009 throw_rude_remark(EINVAL,
1010 "Drive %s, invalid keyword: %s",
1016 if (drive->devicename[0] != '/') {
1017 drive->state = drive_unallocated; /* deallocate the drive */
1018 throw_rude_remark(EINVAL, "No device name for %s", drive->label.name);
1020 vinum_conf.drives_used++; /* passed all hurdles: one more in use */
1022 * If we're replacing a drive, it could be that
1023 * we already have subdisks referencing this
1024 * drive. Note where they should be and change
1025 * their state to obsolete.
1027 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
1028 if ((SD[sdno].state > sd_referenced)
1029 && (SD[sdno].driveno == driveno)) {
1030 give_sd_to_drive(sdno);
1031 if (SD[sdno].state > sd_stale)
1032 SD[sdno].state = sd_stale;
1038 * Handle a subdisk definition. We store the information in the global variable
1039 * sd, so we don't need to allocate.
1041 * If we find an error, print a message and return
1044 config_subdisk(int update)
1047 int sdno; /* index of sd in vinum_conf */
1048 struct sd *sd; /* and pointer to it */
1050 int detached = 0; /* set to 1 if this is a detached subdisk */
1051 int sdindex = -1; /* index in plexes subdisk table */
1052 enum sdstate state = sd_unallocated; /* state to set, if specified */
1053 int autosize = 0; /* set if we autosize in give_sd_to_drive */
1054 int namedsdno; /* index of another with this name */
1055 char partition = 0; /* partition of external subdisk */
1057 sdno = get_empty_sd(); /* allocate an SD to initialize */
1058 sd = &SD[sdno]; /* and get a pointer */
1060 for (parameter = 1; parameter < tokens; parameter++) { /* look at the other tokens */
1061 switch (get_keyword(token[parameter], &keyword_set)) {
1063 * If we have a 'name' parameter, it must
1064 * come first, because we're too lazy to tidy
1065 * up dangling refs if it comes later.
1068 namedsdno = find_subdisk(token[++parameter], 0); /* find an existing sd with this name */
1069 if (namedsdno >= 0) { /* got one */
1070 if (SD[namedsdno].state == sd_referenced) { /* we've been told about this one */
1072 throw_rude_remark(EINVAL,
1073 "sd %s: name parameter must come first\n", /* no go */
1077 struct plex *plex; /* for tidying up dangling references */
1079 *sd = SD[namedsdno]; /* copy from the referenced one */
1080 SD[namedsdno].state = sd_unallocated; /* and deallocate the referenced one */
1081 plex = &PLEX[sd->plexno]; /* now take a look at our plex */
1082 for (i = 0; i < plex->subdisks; i++) { /* look for the pointer */
1083 if (plex->sdnos[i] == namedsdno) /* pointing to the old subdisk */
1084 plex->sdnos[i] = sdno; /* bend it to point here */
1088 if (update) /* are we updating? */
1089 return; /* that's OK, nothing more to do */
1091 throw_rude_remark(EINVAL, "Duplicate subdisk %s", token[parameter]);
1093 bcopy(token[parameter],
1095 min(sizeof(sd->name), strlen(token[parameter])));
1103 size = sizespec(token[++parameter]);
1104 if ((size == -1) /* unallocated */
1105 &&(vinum_conf.flags & VF_READING_CONFIG)) /* reading from disk */
1106 break; /* invalid sd; just ignore it */
1107 if ((size % DEV_BSIZE) != 0)
1108 throw_rude_remark(EINVAL,
1109 "sd %s, bad plex offset alignment: %lld",
1113 sd->plexoffset = size / DEV_BSIZE;
1116 case kw_driveoffset:
1117 size = sizespec(token[++parameter]);
1118 if ((size == -1) /* unallocated */
1119 &&(vinum_conf.flags & VF_READING_CONFIG)) /* reading from disk */
1120 break; /* invalid sd; just ignore it */
1121 if ((size % DEV_BSIZE) != 0)
1122 throw_rude_remark(EINVAL,
1123 "sd %s, bad drive offset alignment: %lld",
1127 sd->driveoffset = size / DEV_BSIZE;
1131 if (get_keyword(token[++parameter], &keyword_set) == kw_max) /* select maximum size from drive */
1132 size = 0; /* this is how we say it :-) */
1134 size = sizespec(token[parameter]);
1135 if ((size % DEV_BSIZE) != 0)
1136 throw_rude_remark(EINVAL, "sd %s, length %d not multiple of sector size", sd->name, size);
1138 sd->sectors = size / DEV_BSIZE;
1140 * We have a problem with autosizing: we need to
1141 * give the drive to the plex before we give it
1142 * to the drive, in order to be clean if we give
1143 * up in the middle, but at this time the size hasn't
1144 * been set. Note that we have to fix up after
1145 * giving the subdisk to the drive.
1148 autosize = 1; /* note that we're autosizing */
1152 sd->driveno = find_drive(token[++parameter], 1); /* insert drive information */
1156 sd->plexno = find_plex(token[++parameter], 1); /* insert plex information */
1160 * Set the state. We can't do this directly,
1161 * because give_sd_to_plex may change it
1164 parameter++; /* skip the keyword */
1165 if (vinum_conf.flags & VF_READING_CONFIG)
1166 state = SdState(token[parameter]); /* set the state */
1170 parameter++; /* skip the keyword */
1171 if ((strlen(token[parameter]) != 1)
1172 || (token[parameter][0] < 'a')
1173 || (token[parameter][0] > 'h'))
1174 throw_rude_remark(EINVAL,
1175 "%s: invalid partition %c",
1177 token[parameter][0]);
1179 partition = token[parameter][0];
1182 case kw_retryerrors:
1183 sd->flags |= VF_RETRYERRORS;
1187 throw_rude_remark(EINVAL, "%s: invalid keyword: %s", sd->name, token[parameter]);
1191 /* Check we have a drive name */
1192 if (sd->driveno < 0) { /* didn't specify a drive */
1193 sd->driveno = current_drive; /* set to the current drive */
1194 if (sd->driveno < 0) /* no current drive? */
1195 throw_rude_remark(EINVAL, "Subdisk %s is not associated with a drive", sd->name);
1198 * This is tacky. If something goes wrong
1199 * with the checks, we may end up losing drive
1202 if (autosize != 0) /* need to find a size, */
1203 give_sd_to_drive(sdno); /* do it before the plex */
1205 /* Check for a plex name */
1206 if ((sd->plexno < 0) /* didn't specify a plex */
1207 &&(!detached)) /* and didn't say not to, */
1208 sd->plexno = current_plex; /* set to the current plex */
1210 if (sd->plexno >= 0)
1211 sdindex = give_sd_to_plex(sd->plexno, sdno); /* now tell the plex that it has this sd */
1213 sd->sdno = sdno; /* point to our entry in the table */
1215 /* Does the subdisk have a name? If not, give it one */
1216 if (sd->name[0] == '\0') { /* no name */
1217 char sdsuffix[8]; /* form sd name suffix here */
1219 /* Do we have a plex name? */
1220 if (sdindex >= 0) /* we have a plex */
1221 strcpy(sd->name, PLEX[sd->plexno].name); /* take it from there */
1223 throw_rude_remark(EINVAL, "Unnamed sd is not associated with a plex");
1224 sprintf(sdsuffix, ".s%d", sdindex); /* form the suffix */
1225 strcat(sd->name, sdsuffix); /* and add it to the name */
1227 /* do we have complete info for this subdisk? */
1228 if (sd->sectors < 0)
1229 throw_rude_remark(EINVAL, "sd %s has no length spec", sd->name);
1231 sd->dev = make_dev(&vinum_cdevsw, VINUMRMINOR(sdno, VINUM_SD_TYPE),
1237 if (state != sd_unallocated) /* we had a specific state to set */
1238 sd->state = state; /* do it now */
1239 else if (sd->state == sd_unallocated) /* no, nothing set yet, */
1240 sd->state = sd_empty; /* must be empty */
1241 if (autosize == 0) /* no autoconfig, do the drive now */
1242 give_sd_to_drive(sdno);
1243 vinum_conf.subdisks_used++; /* one more in use */
1247 * Handle a plex definition.
1250 config_plex(int update)
1253 int plexno; /* index of plex in vinum_conf */
1254 struct plex *plex; /* and pointer to it */
1255 int pindex = MAXPLEX; /* index in volume's plex list */
1256 int detached = 0; /* don't give it to a volume */
1258 enum plexstate state = plex_init; /* state to set at end */
1260 current_plex = -1; /* forget the previous plex */
1261 plexno = get_empty_plex(); /* allocate a plex */
1262 plex = &PLEX[plexno]; /* and point to it */
1263 plex->plexno = plexno; /* and back to the config */
1265 for (parameter = 1; parameter < tokens; parameter++) { /* look at the other tokens */
1266 switch (get_keyword(token[parameter], &keyword_set)) {
1268 * If we have a 'name' parameter, it must
1269 * come first, because we're too lazy to tidy
1270 * up dangling refs if it comes later.
1273 namedplexno = find_plex(token[++parameter], 0); /* find an existing plex with this name */
1274 if (namedplexno >= 0) { /* plex exists already, */
1275 if (PLEX[namedplexno].state == plex_referenced) { /* we've been told about this one */
1276 if (parameter > 2) /* we've done other things first, */
1277 throw_rude_remark(EINVAL,
1278 "plex %s: name parameter must come first\n", /* no go */
1282 struct volume *vol; /* for tidying up dangling references */
1284 *plex = PLEX[namedplexno]; /* get the info */
1285 PLEX[namedplexno].state = plex_unallocated; /* and deallocate the other one */
1286 vol = &VOL[plex->volno]; /* point to the volume */
1287 for (i = 0; i < MAXPLEX; i++) { /* for each plex */
1288 if (vol->plex[i] == namedplexno)
1289 vol->plex[i] = plexno; /* bend the pointer */
1292 break; /* use this one */
1294 if (update) /* are we updating? */
1295 return; /* yes: that's OK, just return */
1297 throw_rude_remark(EINVAL, "Duplicate plex %s", token[parameter]);
1299 bcopy(token[parameter], /* put in the name */
1301 min(MAXPLEXNAME, strlen(token[parameter])));
1308 case kw_org: /* plex organization */
1309 switch (get_keyword(token[++parameter], &keyword_set)) {
1311 plex->organization = plex_concat;
1316 int stripesize = sizespec(token[++parameter]);
1318 plex->organization = plex_striped;
1319 if (stripesize % DEV_BSIZE != 0) /* not a multiple of block size, */
1320 throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
1324 plex->stripesize = stripesize / DEV_BSIZE;
1330 int stripesize = sizespec(token[++parameter]);
1332 plex->organization = plex_raid4;
1333 if (stripesize % DEV_BSIZE != 0) /* not a multiple of block size, */
1334 throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
1338 plex->stripesize = stripesize / DEV_BSIZE;
1344 int stripesize = sizespec(token[++parameter]);
1346 plex->organization = plex_raid5;
1347 if (stripesize % DEV_BSIZE != 0) /* not a multiple of block size, */
1348 throw_rude_remark(EINVAL, "plex %s: stripe size %d not a multiple of sector size",
1352 plex->stripesize = stripesize / DEV_BSIZE;
1357 throw_rude_remark(EINVAL, "Invalid plex organization");
1360 && (plex->stripesize == 0)) /* didn't specify a valid stripe size */
1361 throw_rude_remark(EINVAL, "Need a stripe size parameter");
1365 plex->volno = find_volume(token[++parameter], 1); /* insert a pointer to the volume */
1368 case kw_sd: /* add a subdisk */
1372 sdno = find_subdisk(token[++parameter], 1); /* find a subdisk */
1373 SD[sdno].plexoffset = sizespec(token[++parameter]); /* get the offset */
1374 give_sd_to_plex(plexno, sdno); /* and insert it there */
1379 parameter++; /* skip the keyword */
1380 if (vinum_conf.flags & VF_READING_CONFIG)
1381 state = PlexState(token[parameter]); /* set the state */
1385 throw_rude_remark(EINVAL, "plex %s, invalid keyword: %s",
1391 if (plex->organization == plex_disorg)
1392 throw_rude_remark(EINVAL, "No plex organization specified");
1394 if ((plex->volno < 0) /* we don't have a volume */
1395 &&(!detached)) /* and we wouldn't object */
1396 plex->volno = current_volume;
1398 if (plex->volno >= 0)
1399 pindex = give_plex_to_volume(plex->volno, plexno); /* Now tell the volume that it has this plex */
1401 /* Does the plex have a name? If not, give it one */
1402 if (plex->name[0] == '\0') { /* no name */
1403 char plexsuffix[8]; /* form plex name suffix here */
1404 /* Do we have a volume name? */
1405 if (plex->volno >= 0) /* we have a volume */
1406 strcpy(plex->name, /* take it from there */
1407 VOL[plex->volno].name);
1409 throw_rude_remark(EINVAL, "Unnamed plex is not associated with a volume");
1410 sprintf(plexsuffix, ".p%d", pindex); /* form the suffix */
1411 strcat(plex->name, plexsuffix); /* and add it to the name */
1413 if (isstriped(plex)) {
1414 plex->lock = (struct rangelock *)
1415 Malloc(PLEX_LOCKS * sizeof(struct rangelock));
1416 CHECKALLOC(plex->lock, "vinum: Can't allocate lock table\n");
1417 bzero((char *) plex->lock, PLEX_LOCKS * sizeof(struct rangelock));
1418 mtx_init(&plex->lockmtx, plex->name, "plex", MTX_DEF);
1420 /* Note the last plex we configured */
1421 current_plex = plexno;
1422 plex->state = state; /* set whatever state we chose */
1423 vinum_conf.plexes_used++; /* one more in use */
1424 plex->dev = make_dev(&vinum_cdevsw,
1425 VINUMRMINOR(plexno, VINUM_PLEX_TYPE),
1434 * Handle a volume definition.
1435 * If we find an error, print a message, deallocate the nascent volume, and return
1438 config_volume(int update)
1442 struct volume *vol; /* collect volume info here */
1445 if (tokens < 2) /* not enough tokens */
1446 throw_rude_remark(EINVAL, "Volume has no name");
1447 current_volume = -1; /* forget the previous volume */
1448 volno = find_volume(token[1], 1); /* allocate a volume to initialize */
1449 vol = &VOL[volno]; /* and get a pointer */
1450 if (update && ((vol->flags & VF_CREATED) == 0)) /* this volume exists already */
1451 return; /* don't do anything */
1452 vol->flags &= ~VF_CREATED; /* it exists now */
1454 for (parameter = 2; parameter < tokens; parameter++) { /* look at all tokens */
1455 switch (get_keyword(token[parameter], &keyword_set)) {
1458 int plexno; /* index of this plex */
1459 int myplexno; /* and index if it's already ours */
1461 plexno = find_plex(token[++parameter], 1); /* find a plex */
1462 if (plexno < 0) /* couldn't */
1463 break; /* we've already had an error message */
1464 myplexno = my_plex(volno, plexno); /* does it already belong to us? */
1465 if (myplexno > 0) /* yes, shouldn't get it again */
1466 throw_rude_remark(EINVAL,
1467 "Plex %s already belongs to volume %s",
1470 else if (++vol->plexes > 8) /* another entry */
1471 throw_rude_remark(EINVAL,
1472 "Too many plexes for volume %s",
1474 vol->plex[vol->plexes - 1] = plexno;
1475 PLEX[plexno].state = plex_referenced; /* we know something about it */
1476 PLEX[plexno].volno = volno; /* and this volume references it */
1481 switch (get_keyword(token[++parameter], &keyword_set)) { /* decide what to do */
1483 vol->preferred_plex = ROUND_ROBIN_READPOL; /* default */
1488 int myplexno; /* index of this plex */
1490 myplexno = find_plex(token[++parameter], 1); /* find a plex */
1491 if (myplexno < 0) /* couldn't */
1492 break; /* we've already had an error message */
1493 myplexno = my_plex(volno, myplexno); /* does it already belong to us? */
1494 if (myplexno > 0) /* yes */
1495 vol->preferred_plex = myplexno; /* just note the index */
1496 else if (++vol->plexes > 8) /* another entry */
1497 throw_rude_remark(EINVAL, "Too many plexes");
1498 else { /* space for the new plex */
1499 vol->plex[vol->plexes - 1] = myplexno; /* add it to our list */
1500 vol->preferred_plex = vol->plexes - 1; /* and note the index */
1506 throw_rude_remark(EINVAL, "Invalid read policy");
1510 vol->flags |= VF_CONFIG_SETUPSTATE; /* set the volume up later on */
1514 parameter++; /* skip the keyword */
1515 if (vinum_conf.flags & VF_READING_CONFIG)
1516 vol->state = VolState(token[parameter]); /* set the state */
1520 * XXX experimental ideas. These are not
1521 * documented, and will not be until I
1522 * decide they're worth keeping
1524 case kw_writethrough: /* set writethrough mode */
1525 vol->flags |= VF_WRITETHROUGH;
1528 case kw_writeback: /* set writeback mode */
1529 vol->flags &= ~VF_WRITETHROUGH;
1533 vol->flags |= VF_RAW; /* raw volume (no label) */
1537 throw_rude_remark(EINVAL, "volume %s, invalid keyword: %s",
1542 current_volume = volno; /* note last referred volume */
1543 vol->volno = volno; /* also note in volume */
1546 * Before we can actually use the volume, we need
1547 * a volume label. We could start to fake one here,
1548 * but it will be a lot easier when we have some
1549 * to copy from the drives, so defer it until we
1550 * set up the configuration. XXX
1552 if (vol->state == volume_unallocated)
1553 vol->state = volume_down; /* now ready to bring up at the end */
1555 /* Find out how big our volume is */
1556 for (i = 0; i < vol->plexes; i++)
1557 vol->size = max(vol->size, PLEX[vol->plex[i]].length);
1558 vinum_conf.volumes_used++; /* one more in use */
1559 vol->dev = make_dev(&vinum_cdevsw,
1560 VINUMRMINOR(volno, VINUM_VOLUME_TYPE),
1569 * Parse a config entry. CARE! This destroys the original contents of the
1570 * config entry, which we don't really need after this. More specifically, it
1571 * places \0 characters at the end of each token.
1573 * Return 0 if all is well, otherwise EINVAL for invalid keyword,
1574 * or ENOENT if 'read' command doesn't find any drives.
1577 parse_config(char *cptr, struct keywordset *keyset, int update)
1581 status = 0; /* until proven otherwise */
1582 tokens = tokenize(cptr, token, MAXTOKEN); /* chop up into tokens */
1584 if (tokens <= 0) /* screwed up or empty line */
1585 return tokens; /* give up */
1586 else if (tokens == MAXTOKEN) /* too many */
1587 throw_rude_remark(E2BIG,
1588 "Configuration error for %s: too many parameters",
1591 if (token[0][0] == '#') /* comment line */
1594 switch (get_keyword(token[0], keyset)) { /* decide what to do */
1595 case kw_read: /* read config from a specified drive */
1596 status = vinum_scandisk(&token[1], tokens - 1); /* read the config from disk */
1600 config_drive(update);
1604 config_subdisk(update);
1608 config_plex(update);
1612 config_volume(update);
1615 /* Anything else is invalid in this context */
1617 throw_rude_remark(EINVAL, /* should we die? */
1618 "Invalid configuration information: %s",
1625 * parse a line handed in from userland via ioctl.
1626 * This differs only by the error reporting mechanism:
1627 * we return the error indication in the reply to the
1628 * ioctl, so we need to set a global static pointer in
1629 * this file. This technique works because we have
1630 * ensured that configuration is performed in a single-
1634 parse_user_config(char *cptr, struct keywordset *keyset)
1638 ioctl_reply = (struct _ioctl_reply *) cptr;
1639 status = parse_config(cptr, keyset, 0);
1640 if (status == ENOENT) /* from scandisk, but it can't tell us */
1641 strcpy(ioctl_reply->msg, "no drives found");
1642 ioctl_reply = NULL; /* don't do this again */
1646 /* Remove an object */
1648 remove(struct vinum_ioctl_msg *msg)
1650 struct vinum_ioctl_msg message = *msg; /* make a copy to hand on */
1652 ioctl_reply = (struct _ioctl_reply *) msg; /* reinstate the address to reply to */
1653 ioctl_reply->error = 0; /* no error, */
1654 ioctl_reply->msg[0] = '\0'; /* no message */
1656 switch (message.type) {
1658 remove_drive_entry(message.index, message.force);
1663 remove_sd_entry(message.index, message.force, message.recurse);
1668 remove_plex_entry(message.index, message.force, message.recurse);
1673 remove_volume_entry(message.index, message.force, message.recurse);
1678 ioctl_reply->error = EINVAL;
1679 strcpy(ioctl_reply->msg, "Invalid object type");
1683 /* Remove a drive. */
1685 remove_drive_entry(int driveno, int force)
1687 struct drive *drive = &DRIVE[driveno];
1690 if ((driveno > vinum_conf.drives_allocated) /* not a valid drive */
1691 ||(drive->state == drive_unallocated)) { /* or nothing there */
1692 ioctl_reply->error = EINVAL;
1693 strcpy(ioctl_reply->msg, "No such drive");
1694 } else if (drive->opencount > 0) { /* we have subdisks */
1695 if (force) { /* do it at any cost */
1696 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
1697 if ((SD[sdno].state != sd_unallocated) /* subdisk is allocated */
1698 &&(SD[sdno].driveno == driveno)) /* and it belongs to this drive */
1699 remove_sd_entry(sdno, force, 0);
1701 remove_drive(driveno); /* now remove it */
1702 vinum_conf.drives_used--; /* one less drive */
1704 ioctl_reply->error = EBUSY; /* can't do that */
1706 remove_drive(driveno); /* just remove it */
1707 vinum_conf.drives_used--; /* one less drive */
1711 /* remove a subdisk */
1713 remove_sd_entry(int sdno, int force, int recurse)
1715 struct sd *sd = &SD[sdno];
1717 if ((sdno > vinum_conf.subdisks_allocated) /* not a valid sd */
1718 ||(sd->state == sd_unallocated)) { /* or nothing there */
1719 ioctl_reply->error = EINVAL;
1720 strcpy(ioctl_reply->msg, "No such subdisk");
1721 } else if (sd->flags & VF_OPEN) /* we're open */
1722 ioctl_reply->error = EBUSY; /* no getting around that */
1723 else if (sd->plexno >= 0) { /* we have a plex */
1724 if (force) { /* do it at any cost */
1725 struct plex *plex = &PLEX[sd->plexno]; /* point to our plex */
1728 for (mysdno = 0; /* look for ourselves */
1729 mysdno < plex->subdisks && &SD[plex->sdnos[mysdno]] != sd;
1731 if (mysdno == plex->subdisks) /* didn't find it */
1733 "Error removing subdisk %s: not found in plex %s\n",
1736 else { /* remove the subdisk from plex */
1737 if (mysdno < (plex->subdisks - 1)) /* not the last subdisk */
1738 bcopy(&plex->sdnos[mysdno + 1],
1739 &plex->sdnos[mysdno],
1740 (plex->subdisks - 1 - mysdno) * sizeof(int));
1742 sd->plexno = -1; /* disown the subdisk */
1746 * Removing a subdisk from a striped or
1747 * RAID-4 or RAID-5 plex really tears the
1748 * hell out of the structure, and it needs
1749 * to be reinitialized.
1751 if (plex->organization != plex_concat) /* not concatenated, */
1752 set_plex_state(plex->plexno, plex_faulty, setstate_force); /* need to reinitialize */
1753 log(LOG_INFO, "vinum: removing %s\n", sd->name);
1756 ioctl_reply->error = EBUSY; /* can't do that */
1758 log(LOG_INFO, "vinum: removing %s\n", sd->name);
1765 remove_plex_entry(int plexno, int force, int recurse)
1767 struct plex *plex = &PLEX[plexno];
1770 if ((plexno > vinum_conf.plexes_allocated) /* not a valid plex */
1771 ||(plex->state == plex_unallocated)) { /* or nothing there */
1772 ioctl_reply->error = EINVAL;
1773 strcpy(ioctl_reply->msg, "No such plex");
1774 } else if (plex->flags & VF_OPEN) { /* we're open */
1775 ioctl_reply->error = EBUSY; /* no getting around that */
1778 if (plex->subdisks) {
1779 if (force) { /* do it anyway */
1780 if (recurse) { /* remove all below */
1781 int sds = plex->subdisks;
1782 for (sdno = 0; sdno < sds; sdno++)
1783 free_sd(plex->sdnos[sdno]); /* free all subdisks */
1784 } else { /* just tear them out */
1785 int sds = plex->subdisks;
1786 for (sdno = 0; sdno < sds; sdno++)
1787 SD[plex->sdnos[sdno]].plexno = -1; /* no plex any more */
1789 } else { /* can't do it without force */
1790 ioctl_reply->error = EBUSY; /* can't do that */
1794 if (plex->volno >= 0) { /* we are part of a volume */
1795 if (force) { /* do it at any cost */
1796 struct volume *vol = &VOL[plex->volno];
1799 for (myplexno = 0; myplexno < vol->plexes; myplexno++)
1800 if (vol->plex[myplexno] == plexno) /* found it */
1802 if (myplexno == vol->plexes) /* didn't find it. Huh? */
1804 "Error removing plex %s: not found in volume %s\n",
1807 if (myplexno < (vol->plexes - 1)) /* not the last plex in the list */
1808 bcopy(&vol->plex[myplexno + 1],
1809 &vol->plex[myplexno],
1810 vol->plexes - 1 - myplexno);
1813 ioctl_reply->error = EBUSY; /* can't do that */
1817 log(LOG_INFO, "vinum: removing %s\n", plex->name);
1818 if (isstriped(plex))
1819 mtx_destroy(&plex->lockmtx);
1821 vinum_conf.plexes_used--; /* one less plex */
1824 /* remove a volume */
1826 remove_volume_entry(int volno, int force, int recurse)
1828 struct volume *vol = &VOL[volno];
1831 if ((volno > vinum_conf.volumes_allocated) /* not a valid volume */
1832 ||(vol->state == volume_unallocated)) { /* or nothing there */
1833 ioctl_reply->error = EINVAL;
1834 strcpy(ioctl_reply->msg, "No such volume");
1835 } else if (vol->flags & VF_OPEN) /* we're open */
1836 ioctl_reply->error = EBUSY; /* no getting around that */
1837 else if (vol->plexes) {
1838 if (recurse && force) { /* remove all below */
1839 int plexes = vol->plexes;
1841 /* for (plexno = plexes - 1; plexno >= 0; plexno--) */
1842 for (plexno = 0; plexno < plexes; plexno++)
1843 remove_plex_entry(vol->plex[plexno], force, recurse);
1844 log(LOG_INFO, "vinum: removing %s\n", vol->name);
1846 vinum_conf.volumes_used--; /* one less volume */
1848 ioctl_reply->error = EBUSY; /* can't do that */
1850 log(LOG_INFO, "vinum: removing %s\n", vol->name);
1852 vinum_conf.volumes_used--; /* one less volume */
1856 /* Currently called only from ioctl */
1858 update_sd_config(int sdno, int diskconfig)
1861 set_sd_state(sdno, sd_up, setstate_configuring);
1862 SD[sdno].flags &= ~VF_NEWBORN;
1866 update_plex_config(int plexno, int diskconfig)
1870 struct plex *plex = &PLEX[plexno];
1871 enum plexstate state = plex_up; /* state we want the plex in */
1872 int remainder; /* size of fractional stripe at end */
1873 int added_plex; /* set if we add a plex to a volume */
1874 int required_sds; /* number of subdisks we need */
1877 int data_sds = 0; /* number of sds carrying data */
1879 if (plex->state < plex_init) /* not a real plex, */
1882 if (plex->volno >= 0) { /* we have a volume */
1883 vol = &VOL[plex->volno];
1886 * If we're newly born,
1887 * and the volume isn't,
1888 * and it has other plexes,
1889 * and we didn't read this mess from disk,
1890 * we were added later.
1892 if ((plex->flags & VF_NEWBORN)
1893 && ((vol->flags & VF_NEWBORN) == 0)
1894 && (vol->plexes > 0)
1895 && (diskconfig == 0)) {
1897 state = plex_down; /* so take ourselves down */
1901 * Check that our subdisks make sense. For
1902 * striped plexes, we need at least two
1903 * subdisks, and for RAID-4 and RAID-5 plexes we
1904 * need at least three subdisks. In each case
1905 * they must all be the same size.
1907 if (plex->organization == plex_striped) {
1908 data_sds = plex->subdisks;
1910 } else if (isparity(plex)) { /* RAID 4 or 5 */
1911 data_sds = plex->subdisks - 1;
1915 if (required_sds > 0) { /* striped, RAID-4 or RAID-5 */
1916 if (plex->subdisks < required_sds) {
1918 "vinum: plex %s does not have at least %d subdisks\n",
1921 state = plex_faulty;
1924 * Now see if the plex size is a multiple of
1925 * the stripe size. If not, trim off the end
1926 * of each subdisk and return it to the drive.
1928 if (plex->length > 0) {
1930 if (plex->stripesize > 0) {
1931 remainder = (int) (plex->length /* are we exact? */
1932 % ((u_int64_t) plex->stripesize * data_sds));
1933 if (remainder) { /* no */
1934 log(LOG_INFO, "vinum: removing %d blocks of partial stripe at the end of %s\n",
1937 plex->length -= remainder; /* shorten the plex */
1938 remainder /= data_sds; /* spread the remainder amongst the sds */
1939 for (sdno = 0; sdno < plex->subdisks; sdno++) {
1940 sd = &SD[plex->sdnos[sdno]]; /* point to the subdisk */
1941 return_drive_space(sd->driveno, /* return the space */
1942 sd->driveoffset + sd->sectors - remainder,
1944 sd->sectors -= remainder; /* and shorten it */
1947 } else /* no data sds, */
1948 plex->length = 0; /* reset length */
1953 for (sdno = 0; sdno < plex->subdisks; sdno++) {
1954 sd = &SD[plex->sdnos[sdno]];
1957 && (sd->sectors != SD[plex->sdnos[sdno - 1]].sectors)) {
1958 log(LOG_ERR, "vinum: %s must have equal sized subdisks\n", plex->name);
1961 size += sd->sectors;
1962 if (added_plex) /* we were added later */
1963 sd->state = sd_stale; /* stale until proven otherwise */
1966 if (plex->subdisks) { /* plex has subdisks, calculate size */
1968 * XXX We shouldn't need to calculate the size any
1969 * more. Check this some time
1972 size = size / plex->subdisks * (plex->subdisks - 1); /* less space for RAID-4 and RAID-5 */
1973 if (plex->length != size)
1975 "Correcting length of %s: was %lld, is %lld\n",
1977 (long long) plex->length,
1979 plex->length = size;
1980 } else { /* no subdisks, */
1981 plex->length = 0; /* no size */
1982 state = plex_down; /* take it down */
1984 update_plex_state(plexno); /* set the state */
1985 plex->flags &= ~VF_NEWBORN;
1989 update_volume_config(int volno, int diskconfig)
1991 struct volume *vol = &VOL[volno];
1995 if (vol->state != volume_unallocated)
1997 * Recalculate the size of the volume,
1998 * which might change if the original
1999 * plexes were not a multiple of the
2004 for (plexno = 0; plexno < vol->plexes; plexno++) {
2005 plex = &PLEX[vol->plex[plexno]];
2006 vol->size = max(plex->length, vol->size); /* maximum size */
2007 plex->volplexno = plexno; /* note it in the plex */
2010 vol->flags &= ~VF_NEWBORN; /* no longer newly born */
2014 * Update the global configuration.
2015 * diskconfig is != 0 if we're reading in a config
2016 * from disk. In this case, we don't try to
2017 * bring the devices up, though we will bring
2018 * them down if there's some error which got
2019 * missed when writing to disk.
2022 updateconfig(int diskconfig)
2027 for (plexno = 0; plexno < vinum_conf.plexes_allocated; plexno++)
2028 update_plex_config(plexno, diskconfig);
2030 for (volno = 0; volno < vinum_conf.volumes_allocated; volno++) {
2031 if (VOL[volno].state > volume_uninit) {
2032 VOL[volno].flags &= ~VF_CONFIG_SETUPSTATE; /* no more setupstate */
2033 update_volume_state(volno);
2034 update_volume_config(volno, diskconfig);
2041 * Start manual changes to the configuration and lock out
2042 * others who may wish to do so.
2043 * XXX why do we need this and lock_config too?
2046 start_config(int force)
2050 current_drive = -1; /* note the last drive we mention, for
2052 current_plex = -1; /* and the same for the last plex */
2053 current_volume = -1; /* and the last volume */
2054 while ((vinum_conf.flags & VF_CONFIGURING) != 0) {
2055 vinum_conf.flags |= VF_WILL_CONFIGURE;
2056 if ((error = tsleep(&vinum_conf, PRIBIO | PCATCH, "vincfg", 0)) != 0)
2060 * We need two flags here: VF_CONFIGURING
2061 * tells other processes to hold off (this
2062 * function), and VF_CONFIG_INCOMPLETE
2063 * tells the state change routines not to
2064 * propagate incrememntal state changes
2066 vinum_conf.flags |= VF_CONFIGURING | VF_CONFIG_INCOMPLETE;
2068 vinum_conf.flags |= VF_FORCECONFIG; /* overwrite differently named drives */
2069 current_drive = -1; /* reset the defaults */
2070 current_plex = -1; /* and the same for the last plex */
2071 current_volume = -1; /* and the last volme */
2076 * Update the config if update is 1, and unlock
2077 * it. We won't update the configuration if we
2078 * are called in a recursive loop via throw_rude_remark.
2081 finish_config(int update)
2083 /* we've finished our config */
2084 vinum_conf.flags &= ~(VF_CONFIG_INCOMPLETE | VF_READING_CONFIG | VF_FORCECONFIG);
2086 updateconfig(0); /* so update things */
2088 updateconfig(1); /* do some updates only */
2089 vinum_conf.flags &= ~VF_CONFIGURING; /* and now other people can take a turn */
2090 if ((vinum_conf.flags & VF_WILL_CONFIGURE) != 0) {
2091 vinum_conf.flags &= ~VF_WILL_CONFIGURE;
2092 wakeup_one(&vinum_conf);
2095 /* Local Variables: */
2096 /* fill-column: 50 */