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 $
49 * $FreeBSD: src/sys/dev/vinum/vinumconfig.c,v 1.32.2.6 2002/02/03 00:43:35 grog Exp $
50 * $DragonFly: src/sys/dev/raid/vinum/vinumconfig.c,v 1.12 2007/06/07 22:58:00 corecode Exp $
58 #define MAXTOKEN 64 /* maximum number of tokens in a line */
61 * We can afford the luxury of global variables here,
62 * since start_config ensures that these functions
63 * are single-threaded.
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
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 */
74 /* These values are used by most of these routines, so set them as globals */
75 static char *token
[MAXTOKEN
]; /* pointers to individual tokens */
76 static int tokens
; /* number of tokens */
87 #define MSG_MAX 1024 /* maximum length of a formatted message */
89 * Format an error message and return to the user in the reply.
90 * CARE: This routine is designed to be called only from the
91 * configuration routines, so it assumes it's the owner of
92 * the configuration lock, and unlocks it on exit
95 throw_rude_remark(int error
, char *msg
,...)
100 static int finishing
; /* don't recurse */
103 if ((vinum_conf
.flags
& VF_LOCKED
) == 0) /* bug catcher */
104 panic ("throw_rude_remark: called without config lock");
106 if ((ioctl_reply
!= NULL
) /* we're called from the user */
107 &&(!(vinum_conf
.flags
& VF_READING_CONFIG
))) { /* and not reading from disk: return msg */
109 * We can't just format to ioctl_reply, since it
110 * may contain our input parameters
112 text
= Malloc(MSG_MAX
);
114 log(LOG_ERR
, "vinum: can't allocate error message buffer\n");
116 kvprintf(msg
, ap
); /* print to the console */
119 retval
= kvcprintf(msg
, NULL
, (void *) text
, 10, ap
);
120 text
[retval
] = '\0'; /* delimit */
121 strcpy(ioctl_reply
->msg
, text
);
122 ioctl_reply
->error
= error
; /* first byte is the error number */
127 kvprintf(msg
, ap
); /* print to the console */
132 if (vinum_conf
.flags
& VF_READING_CONFIG
) { /* go through to the bitter end, */
133 if ((vinum_conf
.flags
& VF_READING_CONFIG
) /* we're reading from disk, */
134 &&((daemon_options
& daemon_noupdate
) == 0)) {
135 log(LOG_NOTICE
, "Disabling configuration updates\n");
136 daemon_options
|= daemon_noupdate
;
141 * We have a problem here: we want to unlock the
142 * configuration, which implies tidying up, but
143 * if we find an error while tidying up, we could
144 * recurse for ever. Use this kludge to only try
147 was_finishing
= finishing
;
149 finish_config(was_finishing
); /* unlock anything we may be holding */
150 finishing
= was_finishing
;
151 longjmp(command_fail
, error
);
155 * Check a volume to see if the plex is already assigned to it.
156 * Return index in volume->plex, or -1 if not assigned
159 my_plex(int volno
, int plexno
)
164 vol
= &VOL
[volno
]; /* point to volno */
165 for (i
= 0; i
< vol
->plexes
; i
++)
166 if (vol
->plex
[i
] == plexno
)
168 return -1; /* not found */
172 * Check a plex to see if the subdisk is already assigned to it.
173 * Return index in plex->sd, or -1 if not assigned
176 my_sd(int plexno
, int sdno
)
181 plex
= &PLEX
[plexno
];
182 for (i
= 0; i
< plex
->subdisks
; i
++)
183 if (plex
->sdnos
[i
] == sdno
)
185 return -1; /* not found */
188 /* Add plex to the volume if possible */
190 give_plex_to_volume(int volno
, int plexno
)
196 * It's not an error for the plex to already
197 * belong to the volume, but we need to check a
198 * number of things to make sure it's done right.
201 if (my_plex(volno
, plexno
) >= 0)
202 return plexno
; /* that's it */
204 vol
= &VOL
[volno
]; /* point to volume */
205 if (vol
->plexes
== MAXPLEX
) /* all plexes allocated */
206 throw_rude_remark(ENOSPC
,
207 "Too many plexes for volume %s",
209 else if ((vol
->plexes
> 0) /* we have other plexes */
210 &&((vol
->flags
& VF_CONFIG_SETUPSTATE
) == 0)) /* and we're not setting up state */
211 invalidate_subdisks(&PLEX
[plexno
], sd_stale
); /* make the subdisks invalid */
212 vol
->plex
[vol
->plexes
] = plexno
; /* this one */
213 vol
->plexes
++; /* add another plex */
214 PLEX
[plexno
].volno
= volno
; /* note the number of our volume */
216 /* Find out how big our volume is */
217 for (i
= 0; i
< vol
->plexes
; i
++)
218 vol
->size
= max(vol
->size
, PLEX
[vol
->plex
[i
]].length
);
219 return vol
->plexes
- 1; /* and return its index */
223 * Add subdisk to a plex if possible
226 give_sd_to_plex(int plexno
, int sdno
)
233 * It's not an error for the sd to already
234 * belong to the plex, but we need to check a
235 * number of things to make sure it's done right.
238 i
= my_sd(plexno
, sdno
);
239 if (i
>= 0) /* does it already belong to us? */
240 return i
; /* that's it */
242 plex
= &PLEX
[plexno
]; /* point to the plex */
243 sd
= &SD
[sdno
]; /* and the subdisk */
245 /* Do we have an offset? Otherwise put it after the last one */
246 if (sd
->plexoffset
< 0) { /* no offset specified */
247 if (plex
->subdisks
> 0) {
248 struct sd
*lastsd
= &SD
[plex
->sdnos
[plex
->subdisks
- 1]]; /* last subdisk */
250 if (plex
->organization
== plex_concat
) /* concat, */
251 sd
->plexoffset
= lastsd
->sectors
+ lastsd
->plexoffset
; /* starts here */
252 else /* striped, RAID-4 or RAID-5 */
253 sd
->plexoffset
= plex
->stripesize
* plex
->subdisks
; /* starts here */
254 } else /* first subdisk */
255 sd
->plexoffset
= 0; /* start at the beginning */
257 if (plex
->subdisks
== MAXSD
) /* we already have our maximum */
258 throw_rude_remark(ENOSPC
, /* crap out */
259 "Can't add %s to %s: plex full",
263 plex
->subdisks
++; /* another entry */
264 if (plex
->subdisks
>= plex
->subdisks_allocated
) /* need more space */
265 EXPAND(plex
->sdnos
, int, plex
->subdisks_allocated
, INITIAL_SUBDISKS_IN_PLEX
);
267 /* Adjust size of plex and volume. */
268 if (isparity(plex
)) /* RAID-4 or RAID-5 */
269 plex
->length
= (plex
->subdisks
- 1) * sd
->sectors
; /* size is one disk short */
271 plex
->length
+= sd
->sectors
; /* plex gets this much bigger */
272 if (plex
->volno
>= 0) /* we have a volume */
273 VOL
[plex
->volno
].size
= max(VOL
[plex
->volno
].size
, plex
->length
); /* adjust its size */
276 * We need to check that the subdisks don't overlap,
277 * but we can't do that until a point where we *must*
278 * know the size of all the subdisks. That's not
279 * here. But we need to sort them by offset
281 for (i
= 0; i
< plex
->subdisks
- 1; i
++) {
282 if (sd
->plexoffset
< SD
[plex
->sdnos
[i
]].plexoffset
) { /* it fits before this one */
283 /* First move any remaining subdisks by one */
286 for (j
= plex
->subdisks
- 1; j
> i
; j
--) /* move up one at a time */
287 plex
->sdnos
[j
] = plex
->sdnos
[j
- 1];
288 plex
->sdnos
[i
] = sdno
;
289 sd
->plexsdno
= i
; /* note where we are in the subdisk */
295 * The plex doesn't have any subdisk with a
296 * larger offset. Insert it here.
298 plex
->sdnos
[i
] = sdno
;
299 sd
->plexsdno
= i
; /* note where we are in the subdisk */
300 sd
->plexno
= plex
->plexno
; /* and who we belong to */
305 * Add a subdisk to drive if possible. The
306 * pointer to the drive must already be stored in
307 * the sd structure, but the drive doesn't know
308 * about the subdisk yet.
311 give_sd_to_drive(int sdno
)
313 struct sd
*sd
; /* pointer to subdisk */
314 struct drive
*drive
; /* and drive */
315 int fe
; /* index in free list */
316 int sfe
; /* and index of subdisk when assigning max */
318 sd
= &SD
[sdno
]; /* point to sd */
319 drive
= &DRIVE
[sd
->driveno
]; /* and drive */
321 if (drive
->state
!= drive_up
) {
322 update_sd_state(sdno
); /* that crashes the subdisk */
325 if (drive
->flags
& VF_HOTSPARE
) /* the drive is a hot spare, */
326 throw_rude_remark(ENOSPC
,
327 "Can't place %s on hot spare drive %s",
330 if ((drive
->sectors_available
== 0) /* no space left */
331 ||(sd
->sectors
> drive
->sectors_available
)) { /* or too big, */
332 sd
->driveoffset
= -1; /* don't be confusing */
334 throw_rude_remark(ENOSPC
, "No space for %s on %s", sd
->name
, drive
->label
.name
);
335 return; /* in case we come back here */
337 drive
->subdisks_used
++; /* one more subdisk */
339 if (sd
->sectors
== 0) { /* take the largest chunk */
340 sfe
= 0; /* to keep the compiler happy */
341 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
342 if (drive
->freelist
[fe
].sectors
>= sd
->sectors
) { /* more space here */
343 sd
->sectors
= drive
->freelist
[fe
].sectors
; /* take it */
344 sd
->driveoffset
= drive
->freelist
[fe
].offset
;
345 sfe
= fe
; /* and note the index for later */
348 if (sd
->sectors
== 0) { /* no luck, */
349 sd
->driveoffset
= -1; /* don't be confusing */
351 throw_rude_remark(ENOSPC
, /* give up */
352 "No space for %s on %s",
356 if (sfe
< (drive
->freelist_entries
- 1)) /* not the last one, */
357 bcopy(&drive
->freelist
[sfe
+ 1],
358 &drive
->freelist
[sfe
],
359 (drive
->freelist_entries
- sfe
) * sizeof(struct drive_freelist
));
360 drive
->freelist_entries
--; /* one less entry */
361 drive
->sectors_available
-= sd
->sectors
; /* and note how much less space we have */
362 } else if (sd
->driveoffset
< 0) { /* no offset specified, find one */
363 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
364 if (drive
->freelist
[fe
].sectors
>= sd
->sectors
) { /* it'll fit here */
365 sd
->driveoffset
= drive
->freelist
[fe
].offset
;
366 if (sd
->sectors
== drive
->freelist
[fe
].sectors
) { /* used up the entire entry */
367 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one, */
368 bcopy(&drive
->freelist
[fe
+ 1],
369 &drive
->freelist
[fe
],
370 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
371 drive
->freelist_entries
--; /* one less entry */
373 drive
->freelist
[fe
].sectors
-= sd
->sectors
; /* this much less space */
374 drive
->freelist
[fe
].offset
+= sd
->sectors
; /* this much further on */
376 drive
->sectors_available
-= sd
->sectors
; /* and note how much less space we have */
380 if (sd
->driveoffset
< 0)
382 * Didn't find anything. Although the drive has
383 * enough space, it's too fragmented
387 throw_rude_remark(ENOSPC
, "No space for %s on %s", sd
->name
, drive
->label
.name
);
389 } else { /* specific offset */
391 * For a specific offset to work, the space must be
392 * entirely in a single freelist entry. Look for it.
394 u_int64_t sdend
= sd
->driveoffset
+ sd
->sectors
; /* end of our subdisk */
395 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
396 u_int64_t dend
= drive
->freelist
[fe
].offset
+ drive
->freelist
[fe
].sectors
; /* end of entry */
397 if (dend
>= sdend
) { /* fits before here */
398 if (drive
->freelist
[fe
].offset
> sd
->driveoffset
) { /* starts after the beginning of sd area */
399 sd
->driveoffset
= -1; /* don't be confusing */
400 set_sd_state(sd
->sdno
, sd_down
, setstate_force
);
401 throw_rude_remark(ENOSPC
,
402 "No space for %s on drive %s at offset %lld",
409 * We've found the space, and we can allocate it.
410 * We don't need to say that to the subdisk, which
411 * already knows about it. We need to tell it to
412 * the free list, though. We have four possibilities:
414 * 1. The subdisk exactly eats up the entry. That's the
416 * 2. The subdisk starts at the beginning and leaves space
418 * 3. The subdisk starts after the beginning and leaves
419 * space at the end as well: we end up with another
421 * 4. The subdisk leaves space at the beginning and finishes
424 drive
->sectors_available
-= sd
->sectors
; /* note how much less space we have */
425 if (sd
->driveoffset
== drive
->freelist
[fe
].offset
) { /* 1 or 2 */
426 if (sd
->sectors
== drive
->freelist
[fe
].sectors
) { /* 1: used up the entire entry */
427 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one, */
428 bcopy(&drive
->freelist
[fe
+ 1],
429 &drive
->freelist
[fe
],
430 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
431 drive
->freelist_entries
--; /* one less entry */
432 } else { /* 2: space at the end */
433 drive
->freelist
[fe
].sectors
-= sd
->sectors
; /* this much less space */
434 drive
->freelist
[fe
].offset
+= sd
->sectors
; /* this much further on */
436 } else { /* 3 or 4 */
437 drive
->freelist
[fe
].sectors
= sd
->driveoffset
- drive
->freelist
[fe
].offset
;
438 if (dend
> sdend
) { /* 3: space at the end as well */
439 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one */
440 bcopy(&drive
->freelist
[fe
], /* move the rest down */
441 &drive
->freelist
[fe
+ 1],
442 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
443 drive
->freelist_entries
++; /* one less entry */
444 drive
->freelist
[fe
+ 1].offset
= sdend
; /* second entry starts after sd */
445 drive
->freelist
[fe
+ 1].sectors
= dend
- sdend
; /* and is this long */
452 drive
->opencount
++; /* one more subdisk attached */
455 /* Get an empty drive entry from the drive table */
457 get_empty_drive(void)
462 /* first see if we have one which has been deallocated */
463 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
464 if (DRIVE
[driveno
].state
== drive_unallocated
) /* bingo */
468 if (driveno
>= vinum_conf
.drives_allocated
) /* we've used all our allocation */
469 EXPAND(DRIVE
, struct drive
, vinum_conf
.drives_allocated
, INITIAL_DRIVES
);
471 /* got a drive entry. Make it pretty */
472 drive
= &DRIVE
[driveno
];
473 bzero(drive
, sizeof(struct drive
));
474 drive
->driveno
= driveno
; /* put number in structure */
475 drive
->flags
|= VF_NEWBORN
; /* newly born drive */
476 strcpy(drive
->devicename
, "unknown"); /* and make the name ``unknown'' */
477 return driveno
; /* return the index */
481 * Find the named drive in vinum_conf.drive, return a pointer
482 * return the index in vinum_conf.drive.
483 * Don't mark the drive as allocated (XXX SMP)
484 * If create != 0, create an entry if it doesn't exist
486 /* XXX check if we have it open from attach */
488 find_drive(const char *name
, int create
)
494 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
495 drive
= &DRIVE
[driveno
]; /* point to drive */
496 if ((drive
->label
.name
[0] != '\0') /* it has a name */
497 &&(strcmp(drive
->label
.name
, name
) == 0) /* and it's this one */
498 &&(drive
->state
> drive_unallocated
)) /* and it's a real one: found */
502 /* the drive isn't in the list. Add it if he wants */
503 if (create
== 0) /* don't want to create */
504 return -1; /* give up */
506 driveno
= get_empty_drive();
507 drive
= &DRIVE
[driveno
];
509 bcopy(name
, /* put in its name */
511 min(sizeof(drive
->label
.name
),
513 drive
->state
= drive_referenced
; /* in use, nothing worthwhile there */
514 return driveno
; /* return the index */
518 * Find a drive given its device name.
519 * devname must be valid.
520 * Otherwise the same as find_drive above
523 find_drive_by_dev(const char *devname
, int create
)
528 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
529 drive
= &DRIVE
[driveno
]; /* point to drive */
530 if ((strcmp(drive
->devicename
, devname
) == 0) /* it's this device */
531 &&(drive
->state
> drive_unallocated
)) /* and it's a real one: found */
535 /* the drive isn't in the list. Add it if he wants */
536 if (create
== 0) /* don't want to create */
537 return -1; /* give up */
539 driveno
= get_empty_drive();
540 drive
= &DRIVE
[driveno
];
541 bcopy(devname
, /* put in its name */
543 min(sizeof(drive
->devicename
),
545 drive
->state
= drive_referenced
; /* in use, nothing worthwhile there */
546 return driveno
; /* return the index */
549 /* Find an empty subdisk in the subdisk table */
556 /* first see if we have one which has been deallocated */
557 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
558 if (SD
[sdno
].state
== sd_unallocated
) /* bingo */
561 if (sdno
>= vinum_conf
.subdisks_allocated
)
563 * We've run out of space. sdno is pointing
564 * where we want it, but at the moment we
565 * don't have the space. Get it.
567 EXPAND(SD
, struct sd
, vinum_conf
.subdisks_allocated
, INITIAL_SUBDISKS
);
569 /* initialize some things */
570 sd
= &SD
[sdno
]; /* point to it */
571 bzero(sd
, sizeof(struct sd
)); /* initialize */
572 sd
->flags
|= VF_NEWBORN
; /* newly born subdisk */
573 sd
->plexno
= -1; /* no plex */
574 sd
->sectors
= -1; /* no space */
575 sd
->driveno
= -1; /* no drive */
576 sd
->plexoffset
= -1; /* and no offsets */
577 sd
->driveoffset
= -1;
578 return sdno
; /* return the index */
581 /* return a drive to the free pool */
583 free_drive(struct drive
*drive
)
585 if ((drive
->state
> drive_referenced
) /* real drive */
586 ||(drive
->flags
& VF_OPEN
)) { /* how can it be open without a state? */
588 if (drive
->flags
& VF_OPEN
) { /* it's open, */
589 close_locked_drive(drive
); /* close it */
590 drive
->state
= drive_down
; /* and note the fact */
593 Free(drive
->freelist
);
594 bzero(drive
, sizeof(struct drive
)); /* this also sets drive_unallocated */
600 * Find the named subdisk in vinum_conf.sd.
602 * If create != 0, create an entry if it doesn't exist
604 * Return index in vinum_conf.sd
607 find_subdisk(const char *name
, int create
)
612 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
613 if (strcmp(SD
[sdno
].name
, name
) == 0) /* found it */
617 /* the subdisk isn't in the list. Add it if he wants */
618 if (create
== 0) /* don't want to create */
619 return -1; /* give up */
621 /* Allocate one and insert the name */
622 sdno
= get_empty_sd();
624 bcopy(name
, sd
->name
, min(sizeof(sd
->name
), strlen(name
))); /* put in its name */
625 return sdno
; /* return the pointer */
628 /* Return space to a drive */
630 return_drive_space(int driveno
, int64_t offset
, int length
)
633 int fe
; /* free list entry */
634 u_int64_t sdend
; /* end of our subdisk */
635 u_int64_t dend
; /* end of our freelist entry */
637 drive
= &DRIVE
[driveno
];
638 if (drive
->state
== drive_up
) {
639 sdend
= offset
+ length
; /* end of our subdisk */
641 /* Look for where to return the sd address space */
643 (fe
< drive
->freelist_entries
) && (drive
->freelist
[fe
].offset
< offset
);
646 * Now we are pointing to the last entry, the first
647 * with a higher offset than the subdisk, or both.
649 if ((fe
> 1) /* not the first entry */
650 &&((fe
== drive
->freelist_entries
) /* gone past the end */
651 ||(drive
->freelist
[fe
].offset
> offset
))) /* or past the block were looking for */
652 fe
--; /* point to the block before */
653 dend
= drive
->freelist
[fe
].offset
+ drive
->freelist
[fe
].sectors
; /* end of the entry */
656 * At this point, we are pointing to the correct
657 * place in the free list. A number of possibilities
660 * 1. The block to be freed starts at the end of the
661 * block to which we are pointing. This has two
664 * a. The block to be freed ends at the beginning
665 * of the following block. Merge the three
666 * areas into a single block.
668 * b. The block is shorter than the space between
669 * the current block and the next one. Enlarge
672 * 2. The block to be freed starts after the end
673 * of the block. Again, we have two cases:
675 * a. It ends before the start of the following block.
676 * Create a new free block.
678 * b. It ends at the start of the following block.
679 * Enlarge the following block downwards.
681 * When there is only one free space block, and the
682 * space to be returned is before it, the pointer is
683 * to a non-existent zeroth block. XXX check this
685 if (offset
== dend
) { /* Case 1: it starts at the end of this block */
686 if ((fe
< drive
->freelist_entries
- 1) /* we're not the last block in the free list */
687 /* and the subdisk ends at the start of the next block */
688 &&(sdend
== drive
->freelist
[fe
+ 1].offset
)) {
689 drive
->freelist
[fe
].sectors
/* 1a: merge all three blocks */
690 = drive
->freelist
[fe
+ 1].sectors
;
691 if (fe
< drive
->freelist_entries
- 2) /* still more blocks after next */
692 bcopy(&drive
->freelist
[fe
+ 2], /* move down one */
693 &drive
->freelist
[fe
+ 1],
694 (drive
->freelist_entries
- 2 - fe
)
695 * sizeof(struct drive_freelist
));
696 drive
->freelist_entries
--; /* one less entry in the free list */
697 } else /* 1b: just enlarge this block */
698 drive
->freelist
[fe
].sectors
+= length
;
699 } else { /* Case 2 */
700 if (offset
> dend
) /* it starts after this block */
701 fe
++; /* so look at the next block */
702 if ((fe
< drive
->freelist_entries
) /* we're not the last block in the free list */
703 /* and the subdisk ends at the start of this block: case 4 */
704 &&(sdend
== drive
->freelist
[fe
].offset
)) {
705 drive
->freelist
[fe
].offset
= offset
; /* it starts where the sd was */
706 drive
->freelist
[fe
].sectors
+= length
; /* and it's this much bigger */
707 } else { /* case 3: non-contiguous */
708 if (fe
< drive
->freelist_entries
) /* not after the last block, */
709 bcopy(&drive
->freelist
[fe
], /* move the rest up one entry */
710 &drive
->freelist
[fe
+ 1],
711 (drive
->freelist_entries
- fe
)
712 * sizeof(struct drive_freelist
));
713 drive
->freelist_entries
++; /* one less entry */
714 drive
->freelist
[fe
].offset
= offset
; /* this entry represents the sd */
715 drive
->freelist
[fe
].sectors
= length
;
718 drive
->sectors_available
+= length
; /* the sectors are now available */
723 * Free an allocated sd entry.
724 * This performs memory management only. remove()
725 * is responsible for checking relationships.
733 if ((sd
->driveno
>= 0) /* we have a drive, */
734 &&(sd
->sectors
> 0)) /* and some space on it */
735 return_drive_space(sd
->driveno
, /* return the space */
739 PLEX
[sd
->plexno
].subdisks
--; /* one less subdisk */
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 bzero(plex
, sizeof(struct plex
)); /* and clear it out */
818 plex
->state
= plex_unallocated
;
821 /* Find an empty volume in the volume table */
823 get_empty_volume(void)
829 /* first see if we have one which has been deallocated */
830 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
831 if (VOL
[volno
].state
== volume_unallocated
) /* bingo */
835 if (volno
>= vinum_conf
.volumes_allocated
)
836 EXPAND(VOL
, struct volume
, vinum_conf
.volumes_allocated
, INITIAL_VOLUMES
);
838 /* Now initialize fields */
840 bzero(vol
, sizeof(struct volume
));
841 vol
->flags
|= VF_NEWBORN
| VF_CREATED
; /* newly born volume */
842 vol
->preferred_plex
= ROUND_ROBIN_READPOL
; /* round robin */
843 for (i
= 0; i
< MAXPLEX
; i
++) /* mark the plexes missing */
845 return volno
; /* return the index */
849 * Find the named volume in vinum_conf.volume.
851 * If create != 0, create an entry if it doesn't exist
852 * return the index in vinum_conf
855 find_volume(const char *name
, int create
)
860 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
861 if (strcmp(VOL
[volno
].name
, name
) == 0) /* found it */
865 /* the volume isn't in the list. Add it if he wants */
866 if (create
== 0) /* don't want to create */
867 return -1; /* give up */
869 /* Allocate one and insert the name */
870 volno
= get_empty_volume();
872 bcopy(name
, vol
->name
, min(sizeof(vol
->name
), strlen(name
))); /* put in its name */
873 vol
->blocksize
= DEV_BSIZE
; /* block size of this volume */
874 return volno
; /* return the pointer */
878 * Free an allocated volume entry
879 * and its associated memory areas
882 free_volume(int volno
)
887 bzero(vol
, sizeof(struct volume
)); /* and clear it out */
888 vol
->state
= volume_unallocated
;
892 * Handle a drive definition. We store the information in the global variable
893 * drive, so we don't need to allocate.
895 * If we find an error, print a message and return
898 config_drive(int update
)
900 enum drive_label_info partition_status
; /* info about the partition */
902 int driveno
; /* index of drive in vinum_conf */
903 struct drive
*drive
; /* and pointer to it */
904 int otherdriveno
; /* index of possible second drive */
907 if (tokens
< 2) /* not enough tokens */
908 throw_rude_remark(EINVAL
, "Drive has no name\n");
909 driveno
= find_drive(token
[1], 1); /* allocate a drive to initialize */
910 drive
= &DRIVE
[driveno
]; /* and get a pointer */
911 if (update
&& ((drive
->flags
& VF_NEWBORN
) == 0)) /* this drive exists already */
912 return; /* don't do anything */
913 drive
->flags
&= ~VF_NEWBORN
; /* no longer newly born */
915 if (drive
->state
!= drive_referenced
) { /* we already know this drive */
917 * XXX Check which definition is more up-to-date. Give
918 * preference for the definition on its own drive.
922 for (parameter
= 2; parameter
< tokens
; parameter
++) { /* look at the other tokens */
923 switch (get_keyword(token
[parameter
], &keyword_set
)) {
926 otherdriveno
= find_drive_by_dev(token
[parameter
], 0); /* see if it exists already */
927 if (otherdriveno
>= 0) { /* yup, */
928 drive
->state
= drive_unallocated
; /* deallocate the drive */
929 throw_rude_remark(EEXIST
, /* and complain */
930 "Drive %s would have same device as drive %s",
932 DRIVE
[otherdriveno
].label
.name
);
934 if (drive
->devicename
[0] == '/') { /* we know this drive... */
935 if (strcmp(drive
->devicename
, token
[parameter
])) /* different name */
936 close_drive(drive
); /* close it if it's open */
940 /* open the device and get the configuration */
941 bcopy(token
[parameter
], /* insert device information */
943 min(sizeof(drive
->devicename
),
944 strlen(token
[parameter
])));
945 partition_status
= read_drive_label(drive
, 1);
946 switch (partition_status
) {
947 case DL_CANT_OPEN
: /* not our kind */
949 if (drive
->lasterror
== EFTYPE
) /* wrong kind of partition */
950 throw_rude_remark(drive
->lasterror
,
951 "Drive %s has invalid partition type",
953 else /* I/O error of some kind */
954 throw_rude_remark(drive
->lasterror
,
955 "Can't initialize drive %s",
959 case DL_WRONG_DRIVE
: /* valid drive, not the name we expected */
960 if (vinum_conf
.flags
& VF_FORCECONFIG
) { /* but we'll accept that */
961 bcopy(token
[1], drive
->label
.name
, sizeof(drive
->label
.name
));
966 * There's a potential race condition here:
967 * the rude remark refers to a field in an
968 * unallocated drive, which potentially could
969 * be reused. This works because we're the only
970 * thread accessing the config at the moment.
972 drive
->state
= drive_unallocated
; /* throw it away completely */
973 throw_rude_remark(drive
->lasterror
,
974 "Incorrect drive name %s specified for drive %s",
979 case DL_DELETED_LABEL
: /* it was a drive, but we deleted it */
980 case DL_NOT_OURS
: /* nothing to do with the rest */
985 * read_drive_label overwrites the device name.
986 * If we get here, we can have the drive,
987 * so put it back again
989 bcopy(token
[parameter
],
991 min(sizeof(drive
->devicename
),
992 strlen(token
[parameter
])));
996 parameter
++; /* skip the keyword */
997 if (vinum_conf
.flags
& VF_READING_CONFIG
)
998 drive
->state
= DriveState(token
[parameter
]); /* set the state */
1001 case kw_hotspare
: /* this drive is a hot spare */
1002 drive
->flags
|= VF_HOTSPARE
;
1007 throw_rude_remark(EINVAL
,
1008 "Drive %s, invalid keyword: %s",
1014 if (drive
->devicename
[0] != '/') {
1015 drive
->state
= drive_unallocated
; /* deallocate the drive */
1016 throw_rude_remark(EINVAL
, "No device name for %s", drive
->label
.name
);
1018 vinum_conf
.drives_used
++; /* passed all hurdles: one more in use */
1020 * If we're replacing a drive, it could be that
1021 * we already have subdisks referencing this
1022 * drive. Note where they should be and change
1023 * their state to obsolete.
1025 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
1026 if ((SD
[sdno
].state
> sd_referenced
)
1027 && (SD
[sdno
].driveno
== driveno
)) {
1028 give_sd_to_drive(sdno
);
1029 if (SD
[sdno
].state
> sd_stale
)
1030 SD
[sdno
].state
= sd_stale
;
1036 * Handle a subdisk definition. We store the information in the global variable
1037 * sd, so we don't need to allocate.
1039 * If we find an error, print a message and return
1042 config_subdisk(int update
)
1045 int sdno
; /* index of sd in vinum_conf */
1046 struct sd
*sd
; /* and pointer to it */
1048 int detached
= 0; /* set to 1 if this is a detached subdisk */
1049 int sdindex
= -1; /* index in plexes subdisk table */
1050 enum sdstate state
= sd_unallocated
; /* state to set, if specified */
1051 int autosize
= 0; /* set if we autosize in give_sd_to_drive */
1052 int namedsdno
; /* index of another with this name */
1053 char partition
= 0; /* partition of external subdisk */
1055 sdno
= get_empty_sd(); /* allocate an SD to initialize */
1056 sd
= &SD
[sdno
]; /* and get a pointer */
1058 for (parameter
= 1; parameter
< tokens
; parameter
++) { /* look at the other tokens */
1059 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1061 * If we have a 'name' parameter, it must
1062 * come first, because we're too lazy to tidy
1063 * up dangling refs if it comes later.
1066 namedsdno
= find_subdisk(token
[++parameter
], 0); /* find an existing sd with this name */
1067 if (namedsdno
>= 0) { /* got one */
1068 if (SD
[namedsdno
].state
== sd_referenced
) { /* we've been told about this one */
1070 throw_rude_remark(EINVAL
,
1071 "sd %s: name parameter must come first\n", /* no go */
1075 struct plex
*plex
; /* for tidying up dangling references */
1077 *sd
= SD
[namedsdno
]; /* copy from the referenced one */
1078 SD
[namedsdno
].state
= sd_unallocated
; /* and deallocate the referenced one */
1079 plex
= &PLEX
[sd
->plexno
]; /* now take a look at our plex */
1080 for (i
= 0; i
< plex
->subdisks
; i
++) { /* look for the pointer */
1081 if (plex
->sdnos
[i
] == namedsdno
) /* pointing to the old subdisk */
1082 plex
->sdnos
[i
] = sdno
; /* bend it to point here */
1086 if (update
) /* are we updating? */
1087 return; /* that's OK, nothing more to do */
1089 throw_rude_remark(EINVAL
, "Duplicate subdisk %s", token
[parameter
]);
1091 bcopy(token
[parameter
],
1093 min(sizeof(sd
->name
), strlen(token
[parameter
])));
1101 size
= sizespec(token
[++parameter
]);
1102 if ((size
== -1) /* unallocated */
1103 &&(vinum_conf
.flags
& VF_READING_CONFIG
)) /* reading from disk */
1104 break; /* invalid sd; just ignore it */
1105 if ((size
% DEV_BSIZE
) != 0)
1106 throw_rude_remark(EINVAL
,
1107 "sd %s, bad plex offset alignment: %lld",
1111 sd
->plexoffset
= size
/ DEV_BSIZE
;
1114 case kw_driveoffset
:
1115 size
= sizespec(token
[++parameter
]);
1116 if ((size
== -1) /* unallocated */
1117 &&(vinum_conf
.flags
& VF_READING_CONFIG
)) /* reading from disk */
1118 break; /* invalid sd; just ignore it */
1119 if ((size
% DEV_BSIZE
) != 0)
1120 throw_rude_remark(EINVAL
,
1121 "sd %s, bad drive offset alignment: %lld",
1125 sd
->driveoffset
= size
/ DEV_BSIZE
;
1129 if (get_keyword(token
[++parameter
], &keyword_set
) == kw_max
) /* select maximum size from drive */
1130 size
= 0; /* this is how we say it :-) */
1132 size
= sizespec(token
[parameter
]);
1133 if ((size
% DEV_BSIZE
) != 0)
1134 throw_rude_remark(EINVAL
, "sd %s, length %d not multiple of sector size", sd
->name
, size
);
1136 sd
->sectors
= size
/ DEV_BSIZE
;
1138 * We have a problem with autosizing: we need to
1139 * give the drive to the plex before we give it
1140 * to the drive, in order to be clean if we give
1141 * up in the middle, but at this time the size hasn't
1142 * been set. Note that we have to fix up after
1143 * giving the subdisk to the drive.
1146 autosize
= 1; /* note that we're autosizing */
1150 sd
->driveno
= find_drive(token
[++parameter
], 1); /* insert drive information */
1154 sd
->plexno
= find_plex(token
[++parameter
], 1); /* insert plex information */
1158 * Set the state. We can't do this directly,
1159 * because give_sd_to_plex may change it
1162 parameter
++; /* skip the keyword */
1163 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1164 state
= SdState(token
[parameter
]); /* set the state */
1168 parameter
++; /* skip the keyword */
1169 if ((strlen(token
[parameter
]) != 1)
1170 || (token
[parameter
][0] < 'a')
1171 || (token
[parameter
][0] > 'p'))
1172 throw_rude_remark(EINVAL
,
1173 "%s: invalid partition %c",
1175 token
[parameter
][0]);
1177 partition
= token
[parameter
][0];
1180 case kw_retryerrors
:
1181 sd
->flags
|= VF_RETRYERRORS
;
1185 throw_rude_remark(EINVAL
, "%s: invalid keyword: %s", sd
->name
, token
[parameter
]);
1189 /* Check we have a drive name */
1190 if (sd
->driveno
< 0) { /* didn't specify a drive */
1191 sd
->driveno
= current_drive
; /* set to the current drive */
1192 if (sd
->driveno
< 0) /* no current drive? */
1193 throw_rude_remark(EINVAL
, "Subdisk %s is not associated with a drive", sd
->name
);
1196 * This is tacky. If something goes wrong
1197 * with the checks, we may end up losing drive
1200 if (autosize
!= 0) /* need to find a size, */
1201 give_sd_to_drive(sdno
); /* do it before the plex */
1203 /* Check for a plex name */
1204 if ((sd
->plexno
< 0) /* didn't specify a plex */
1205 &&(!detached
)) /* and didn't say not to, */
1206 sd
->plexno
= current_plex
; /* set to the current plex */
1208 if (sd
->plexno
>= 0)
1209 sdindex
= give_sd_to_plex(sd
->plexno
, sdno
); /* now tell the plex that it has this sd */
1211 sd
->sdno
= sdno
; /* point to our entry in the table */
1213 /* Does the subdisk have a name? If not, give it one */
1214 if (sd
->name
[0] == '\0') { /* no name */
1215 char sdsuffix
[8]; /* form sd name suffix here */
1217 /* Do we have a plex name? */
1218 if (sdindex
>= 0) /* we have a plex */
1219 strcpy(sd
->name
, PLEX
[sd
->plexno
].name
); /* take it from there */
1221 throw_rude_remark(EINVAL
, "Unnamed sd is not associated with a plex");
1222 ksprintf(sdsuffix
, ".s%d", sdindex
); /* form the suffix */
1223 strcat(sd
->name
, sdsuffix
); /* and add it to the name */
1225 /* do we have complete info for this subdisk? */
1226 if (sd
->sectors
< 0)
1227 throw_rude_remark(EINVAL
, "sd %s has no length spec", sd
->name
);
1229 if (state
!= sd_unallocated
) /* we had a specific state to set */
1230 sd
->state
= state
; /* do it now */
1231 else if (sd
->state
== sd_unallocated
) /* no, nothing set yet, */
1232 sd
->state
= sd_empty
; /* must be empty */
1233 if (autosize
== 0) /* no autoconfig, do the drive now */
1234 give_sd_to_drive(sdno
);
1235 vinum_conf
.subdisks_used
++; /* one more in use */
1239 * Handle a plex definition.
1242 config_plex(int update
)
1245 int plexno
; /* index of plex in vinum_conf */
1246 struct plex
*plex
; /* and pointer to it */
1247 int pindex
= MAXPLEX
; /* index in volume's plex list */
1248 int detached
= 0; /* don't give it to a volume */
1250 enum plexstate state
= plex_init
; /* state to set at end */
1252 current_plex
= -1; /* forget the previous plex */
1253 plexno
= get_empty_plex(); /* allocate a plex */
1254 plex
= &PLEX
[plexno
]; /* and point to it */
1255 plex
->plexno
= plexno
; /* and back to the config */
1257 for (parameter
= 1; parameter
< tokens
; parameter
++) { /* look at the other tokens */
1258 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1260 * If we have a 'name' parameter, it must
1261 * come first, because we're too lazy to tidy
1262 * up dangling refs if it comes later.
1265 namedplexno
= find_plex(token
[++parameter
], 0); /* find an existing plex with this name */
1266 if (namedplexno
>= 0) { /* plex exists already, */
1267 if (PLEX
[namedplexno
].state
== plex_referenced
) { /* we've been told about this one */
1268 if (parameter
> 2) /* we've done other things first, */
1269 throw_rude_remark(EINVAL
,
1270 "plex %s: name parameter must come first\n", /* no go */
1274 struct volume
*vol
; /* for tidying up dangling references */
1276 *plex
= PLEX
[namedplexno
]; /* get the info */
1277 PLEX
[namedplexno
].state
= plex_unallocated
; /* and deallocate the other one */
1278 vol
= &VOL
[plex
->volno
]; /* point to the volume */
1279 for (i
= 0; i
< MAXPLEX
; i
++) { /* for each plex */
1280 if (vol
->plex
[i
] == namedplexno
)
1281 vol
->plex
[i
] = plexno
; /* bend the pointer */
1284 break; /* use this one */
1286 if (update
) /* are we updating? */
1287 return; /* yes: that's OK, just return */
1289 throw_rude_remark(EINVAL
, "Duplicate plex %s", token
[parameter
]);
1291 bcopy(token
[parameter
], /* put in the name */
1293 min(MAXPLEXNAME
, strlen(token
[parameter
])));
1300 case kw_org
: /* plex organization */
1301 switch (get_keyword(token
[++parameter
], &keyword_set
)) {
1303 plex
->organization
= plex_concat
;
1308 int stripesize
= sizespec(token
[++parameter
]);
1310 plex
->organization
= plex_striped
;
1311 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1312 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1316 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1322 int stripesize
= sizespec(token
[++parameter
]);
1324 plex
->organization
= plex_raid4
;
1325 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1326 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1330 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1336 int stripesize
= sizespec(token
[++parameter
]);
1338 plex
->organization
= plex_raid5
;
1339 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1340 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1344 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1349 throw_rude_remark(EINVAL
, "Invalid plex organization");
1352 && (plex
->stripesize
== 0)) /* didn't specify a valid stripe size */
1353 throw_rude_remark(EINVAL
, "Need a stripe size parameter");
1357 plex
->volno
= find_volume(token
[++parameter
], 1); /* insert a pointer to the volume */
1360 case kw_sd
: /* add a subdisk */
1364 sdno
= find_subdisk(token
[++parameter
], 1); /* find a subdisk */
1365 SD
[sdno
].plexoffset
= sizespec(token
[++parameter
]); /* get the offset */
1366 give_sd_to_plex(plexno
, sdno
); /* and insert it there */
1371 parameter
++; /* skip the keyword */
1372 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1373 state
= PlexState(token
[parameter
]); /* set the state */
1377 throw_rude_remark(EINVAL
, "plex %s, invalid keyword: %s",
1383 if (plex
->organization
== plex_disorg
)
1384 throw_rude_remark(EINVAL
, "No plex organization specified");
1386 if ((plex
->volno
< 0) /* we don't have a volume */
1387 &&(!detached
)) /* and we wouldn't object */
1388 plex
->volno
= current_volume
;
1390 if (plex
->volno
>= 0)
1391 pindex
= give_plex_to_volume(plex
->volno
, plexno
); /* Now tell the volume that it has this plex */
1393 /* Does the plex have a name? If not, give it one */
1394 if (plex
->name
[0] == '\0') { /* no name */
1395 char plexsuffix
[8]; /* form plex name suffix here */
1396 /* Do we have a volume name? */
1397 if (plex
->volno
>= 0) /* we have a volume */
1398 strcpy(plex
->name
, /* take it from there */
1399 VOL
[plex
->volno
].name
);
1401 throw_rude_remark(EINVAL
, "Unnamed plex is not associated with a volume");
1402 ksprintf(plexsuffix
, ".p%d", pindex
); /* form the suffix */
1403 strcat(plex
->name
, plexsuffix
); /* and add it to the name */
1405 if (isstriped(plex
)) {
1406 plex
->lock
= (struct rangelock
*)
1407 Malloc(PLEX_LOCKS
* sizeof(struct rangelock
));
1408 CHECKALLOC(plex
->lock
, "vinum: Can't allocate lock table\n");
1409 bzero((char *) plex
->lock
, PLEX_LOCKS
* sizeof(struct rangelock
));
1411 /* Note the last plex we configured */
1412 current_plex
= plexno
;
1413 plex
->state
= state
; /* set whatever state we chose */
1414 vinum_conf
.plexes_used
++; /* one more in use */
1418 * Handle a volume definition.
1419 * If we find an error, print a message, deallocate the nascent volume, and return
1422 config_volume(int update
)
1426 struct volume
*vol
; /* collect volume info here */
1429 if (tokens
< 2) /* not enough tokens */
1430 throw_rude_remark(EINVAL
, "Volume has no name");
1431 current_volume
= -1; /* forget the previous volume */
1432 volno
= find_volume(token
[1], 1); /* allocate a volume to initialize */
1433 vol
= &VOL
[volno
]; /* and get a pointer */
1434 if (update
&& ((vol
->flags
& VF_CREATED
) == 0)) /* this volume exists already */
1435 return; /* don't do anything */
1436 vol
->flags
&= ~VF_CREATED
; /* it exists now */
1438 for (parameter
= 2; parameter
< tokens
; parameter
++) { /* look at all tokens */
1439 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1442 int plexno
; /* index of this plex */
1443 int myplexno
; /* and index if it's already ours */
1445 plexno
= find_plex(token
[++parameter
], 1); /* find a plex */
1446 if (plexno
< 0) /* couldn't */
1447 break; /* we've already had an error message */
1448 myplexno
= my_plex(volno
, plexno
); /* does it already belong to us? */
1449 if (myplexno
> 0) /* yes, shouldn't get it again */
1450 throw_rude_remark(EINVAL
,
1451 "Plex %s already belongs to volume %s",
1454 else if (vol
->plexes
+ 1 > 8) /* another entry */
1455 throw_rude_remark(EINVAL
,
1456 "Too many plexes for volume %s",
1458 vol
->plex
[vol
->plexes
] = plexno
;
1460 PLEX
[plexno
].state
= plex_referenced
; /* we know something about it */
1461 PLEX
[plexno
].volno
= volno
; /* and this volume references it */
1466 switch (get_keyword(token
[++parameter
], &keyword_set
)) { /* decide what to do */
1468 vol
->preferred_plex
= ROUND_ROBIN_READPOL
; /* default */
1473 int myplexno
; /* index of this plex */
1475 myplexno
= find_plex(token
[++parameter
], 1); /* find a plex */
1476 if (myplexno
< 0) /* couldn't */
1477 break; /* we've already had an error message */
1478 myplexno
= my_plex(volno
, myplexno
); /* does it already belong to us? */
1479 if (myplexno
> 0) /* yes */
1480 vol
->preferred_plex
= myplexno
; /* just note the index */
1481 else if (++vol
->plexes
> 8) /* another entry */
1482 throw_rude_remark(EINVAL
, "Too many plexes");
1483 else { /* space for the new plex */
1484 vol
->plex
[vol
->plexes
- 1] = myplexno
; /* add it to our list */
1485 vol
->preferred_plex
= vol
->plexes
- 1; /* and note the index */
1491 throw_rude_remark(EINVAL
, "Invalid read policy");
1495 vol
->flags
|= VF_CONFIG_SETUPSTATE
; /* set the volume up later on */
1499 parameter
++; /* skip the keyword */
1500 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1501 vol
->state
= VolState(token
[parameter
]); /* set the state */
1505 * XXX experimental ideas. These are not
1506 * documented, and will not be until I
1507 * decide they're worth keeping
1509 case kw_writethrough
: /* set writethrough mode */
1510 vol
->flags
|= VF_WRITETHROUGH
;
1513 case kw_writeback
: /* set writeback mode */
1514 vol
->flags
&= ~VF_WRITETHROUGH
;
1518 vol
->flags
|= VF_RAW
; /* raw volume (no label) */
1522 throw_rude_remark(EINVAL
, "volume %s, invalid keyword: %s",
1527 current_volume
= volno
; /* note last referred volume */
1528 vol
->volno
= volno
; /* also note in volume */
1531 * Before we can actually use the volume, we need
1532 * a volume label. We could start to fake one here,
1533 * but it will be a lot easier when we have some
1534 * to copy from the drives, so defer it until we
1535 * set up the configuration. XXX
1537 if (vol
->state
== volume_unallocated
)
1538 vol
->state
= volume_down
; /* now ready to bring up at the end */
1540 /* Find out how big our volume is */
1541 for (i
= 0; i
< vol
->plexes
; i
++)
1542 vol
->size
= max(vol
->size
, PLEX
[vol
->plex
[i
]].length
);
1543 vinum_conf
.volumes_used
++; /* one more in use */
1547 * Parse a config entry. CARE! This destroys the original contents of the
1548 * config entry, which we don't really need after this. More specifically, it
1549 * places \0 characters at the end of each token.
1551 * Return 0 if all is well, otherwise EINVAL for invalid keyword,
1552 * or ENOENT if 'read' command doesn't find any drives.
1555 parse_config(char *cptr
, struct keywordset
*keyset
, int update
)
1559 status
= 0; /* until proven otherwise */
1560 tokens
= tokenize(cptr
, token
); /* chop up into tokens */
1562 if (tokens
<= 0) /* screwed up or empty line */
1563 return tokens
; /* give up */
1565 if (token
[0][0] == '#') /* comment line */
1568 switch (get_keyword(token
[0], keyset
)) { /* decide what to do */
1569 case kw_read
: /* read config from a specified drive */
1570 status
= vinum_scandisk(&token
[1], tokens
- 1); /* read the config from disk */
1574 config_drive(update
);
1578 config_subdisk(update
);
1582 config_plex(update
);
1586 config_volume(update
);
1589 /* Anything else is invalid in this context */
1591 throw_rude_remark(EINVAL
, /* should we die? */
1592 "Invalid configuration information: %s",
1599 * parse a line handed in from userland via ioctl.
1600 * This differs only by the error reporting mechanism:
1601 * we return the error indication in the reply to the
1602 * ioctl, so we need to set a global static pointer in
1603 * this file. This technique works because we have
1604 * ensured that configuration is performed in a single-
1608 parse_user_config(char *cptr
, struct keywordset
*keyset
)
1612 ioctl_reply
= (struct _ioctl_reply
*) cptr
;
1613 status
= parse_config(cptr
, keyset
, 0);
1614 if (status
== ENOENT
) /* from scandisk, but it can't tell us */
1615 strcpy(ioctl_reply
->msg
, "no drives found");
1616 ioctl_reply
= NULL
; /* don't do this again */
1620 /* Remove an object */
1622 remove(struct vinum_ioctl_msg
*msg
)
1624 struct vinum_ioctl_msg message
= *msg
; /* make a copy to hand on */
1626 ioctl_reply
= (struct _ioctl_reply
*) msg
; /* reinstate the address to reply to */
1627 ioctl_reply
->error
= 0; /* no error, */
1628 ioctl_reply
->msg
[0] = '\0'; /* no message */
1630 switch (message
.type
) {
1632 remove_drive_entry(message
.index
, message
.force
);
1637 remove_sd_entry(message
.index
, message
.force
, message
.recurse
);
1642 remove_plex_entry(message
.index
, message
.force
, message
.recurse
);
1647 remove_volume_entry(message
.index
, message
.force
, message
.recurse
);
1652 ioctl_reply
->error
= EINVAL
;
1653 strcpy(ioctl_reply
->msg
, "Invalid object type");
1657 /* Remove a drive. */
1659 remove_drive_entry(int driveno
, int force
)
1661 struct drive
*drive
= &DRIVE
[driveno
];
1664 if ((driveno
> vinum_conf
.drives_allocated
) /* not a valid drive */
1665 ||(drive
->state
== drive_unallocated
)) { /* or nothing there */
1666 ioctl_reply
->error
= EINVAL
;
1667 strcpy(ioctl_reply
->msg
, "No such drive");
1668 } else if (drive
->opencount
> 0) { /* we have subdisks */
1669 if (force
) { /* do it at any cost */
1670 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
1671 if ((SD
[sdno
].state
!= sd_unallocated
) /* subdisk is allocated */
1672 &&(SD
[sdno
].driveno
== driveno
)) /* and it belongs to this drive */
1673 remove_sd_entry(sdno
, force
, 0);
1675 remove_drive(driveno
); /* now remove it */
1676 vinum_conf
.drives_used
--; /* one less drive */
1678 ioctl_reply
->error
= EBUSY
; /* can't do that */
1680 remove_drive(driveno
); /* just remove it */
1681 vinum_conf
.drives_used
--; /* one less drive */
1685 /* remove a subdisk */
1687 remove_sd_entry(int sdno
, int force
, int recurse
)
1689 struct sd
*sd
= &SD
[sdno
];
1691 if ((sdno
> vinum_conf
.subdisks_allocated
) /* not a valid sd */
1692 ||(sd
->state
== sd_unallocated
)) { /* or nothing there */
1693 ioctl_reply
->error
= EINVAL
;
1694 strcpy(ioctl_reply
->msg
, "No such subdisk");
1695 } else if (sd
->flags
& VF_OPEN
) { /* we're open */
1696 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1698 } else if (sd
->plexno
>= 0) { /* we have a plex */
1699 if (force
) { /* do it at any cost */
1700 struct plex
*plex
= &PLEX
[sd
->plexno
]; /* point to our plex */
1703 for (mysdno
= 0; /* look for ourselves */
1704 mysdno
< plex
->subdisks
&& &SD
[plex
->sdnos
[mysdno
]] != sd
;
1706 if (mysdno
== plex
->subdisks
) /* didn't find it */
1708 "Error removing subdisk %s: not found in plex %s\n",
1711 else { /* remove the subdisk from plex */
1712 if (mysdno
< (plex
->subdisks
- 1)) /* not the last subdisk */
1713 bcopy(&plex
->sdnos
[mysdno
+ 1],
1714 &plex
->sdnos
[mysdno
],
1715 (plex
->subdisks
- 1 - mysdno
) * sizeof(int));
1717 sd
->plexno
= -1; /* disown the subdisk */
1721 * Removing a subdisk from a striped or
1722 * RAID-4 or RAID-5 plex really tears the
1723 * hell out of the structure, and it needs
1724 * to be reinitialized.
1726 if (plex
->organization
!= plex_concat
) /* not concatenated, */
1727 set_plex_state(plex
->plexno
, plex_faulty
, setstate_force
); /* need to reinitialize */
1728 log(LOG_INFO
, "vinum: removing %s\n", sd
->name
);
1731 ioctl_reply
->error
= EBUSY
; /* can't do that */
1733 log(LOG_INFO
, "vinum: removing %s\n", sd
->name
);
1740 remove_plex_entry(int plexno
, int force
, int recurse
)
1742 struct plex
*plex
= &PLEX
[plexno
];
1745 if ((plexno
> vinum_conf
.plexes_allocated
) /* not a valid plex */
1746 ||(plex
->state
== plex_unallocated
)) { /* or nothing there */
1747 ioctl_reply
->error
= EINVAL
;
1748 strcpy(ioctl_reply
->msg
, "No such plex");
1749 } else if (plex
->flags
& VF_OPEN
) { /* we're open */
1750 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1753 if (plex
->subdisks
) {
1754 if (force
) { /* do it anyway */
1755 if (recurse
) { /* remove all below */
1756 int sds
= plex
->subdisks
;
1757 for (sdno
= 0; sdno
< sds
; sdno
++)
1758 free_sd(plex
->sdnos
[sdno
]); /* free all subdisks */
1759 } else { /* just tear them out */
1760 int sds
= plex
->subdisks
;
1761 for (sdno
= 0; sdno
< sds
; sdno
++)
1762 SD
[plex
->sdnos
[sdno
]].plexno
= -1; /* no plex any more */
1764 } else { /* can't do it without force */
1765 ioctl_reply
->error
= EBUSY
; /* can't do that */
1769 if (plex
->volno
>= 0) { /* we are part of a volume */
1770 if (force
) { /* do it at any cost */
1771 struct volume
*vol
= &VOL
[plex
->volno
];
1774 for (myplexno
= 0; myplexno
< vol
->plexes
; myplexno
++)
1775 if (vol
->plex
[myplexno
] == plexno
) /* found it */
1777 if (myplexno
== vol
->plexes
) /* didn't find it. Huh? */
1779 "Error removing plex %s: not found in volume %s\n",
1782 if (myplexno
< (vol
->plexes
- 1)) /* not the last plex in the list */
1783 bcopy(&vol
->plex
[myplexno
+ 1],
1784 &vol
->plex
[myplexno
],
1785 vol
->plexes
- 1 - myplexno
);
1788 ioctl_reply
->error
= EBUSY
; /* can't do that */
1792 log(LOG_INFO
, "vinum: removing %s\n", plex
->name
);
1794 vinum_conf
.plexes_used
--; /* one less plex */
1797 /* remove a volume */
1799 remove_volume_entry(int volno
, int force
, int recurse
)
1801 struct volume
*vol
= &VOL
[volno
];
1804 if ((volno
> vinum_conf
.volumes_allocated
) /* not a valid volume */
1805 ||(vol
->state
== volume_unallocated
)) { /* or nothing there */
1806 ioctl_reply
->error
= EINVAL
;
1807 strcpy(ioctl_reply
->msg
, "No such volume");
1808 } else if (vol
->flags
& VF_OPEN
) /* we're open */
1809 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1810 else if (vol
->plexes
) {
1811 if (recurse
&& force
) { /* remove all below */
1812 int plexes
= vol
->plexes
;
1814 /* for (plexno = plexes - 1; plexno >= 0; plexno--) */
1815 for (plexno
= 0; plexno
< plexes
; plexno
++)
1816 remove_plex_entry(vol
->plex
[plexno
], force
, recurse
);
1817 log(LOG_INFO
, "vinum: removing %s\n", vol
->name
);
1819 vinum_conf
.volumes_used
--; /* one less volume */
1821 ioctl_reply
->error
= EBUSY
; /* can't do that */
1823 log(LOG_INFO
, "vinum: removing %s\n", vol
->name
);
1825 vinum_conf
.volumes_used
--; /* one less volume */
1829 /* Currently called only from ioctl */
1831 update_sd_config(int sdno
, int diskconfig
)
1834 set_sd_state(sdno
, sd_up
, setstate_configuring
);
1835 SD
[sdno
].flags
&= ~VF_NEWBORN
;
1839 update_plex_config(int plexno
, int diskconfig
)
1843 struct plex
*plex
= &PLEX
[plexno
];
1844 enum plexstate state
= plex_up
; /* state we want the plex in */
1845 int remainder
; /* size of fractional stripe at end */
1846 int added_plex
; /* set if we add a plex to a volume */
1847 int required_sds
; /* number of subdisks we need */
1850 int data_sds
= 0; /* number of sds carrying data */
1852 if (plex
->state
< plex_init
) /* not a real plex, */
1855 if (plex
->volno
>= 0) { /* we have a volume */
1856 vol
= &VOL
[plex
->volno
];
1859 * If we're newly born,
1860 * and the volume isn't,
1861 * and it has other plexes,
1862 * and we didn't read this mess from disk,
1863 * we were added later.
1865 if ((plex
->flags
& VF_NEWBORN
)
1866 && ((vol
->flags
& VF_NEWBORN
) == 0)
1867 && (vol
->plexes
> 0)
1868 && (diskconfig
== 0)) {
1870 state
= plex_down
; /* so take ourselves down */
1874 * Check that our subdisks make sense. For
1875 * striped, RAID-4 and RAID-5 plexes, we need at
1876 * least two subdisks, and they must all be the
1879 if (plex
->organization
== plex_striped
) {
1880 data_sds
= plex
->subdisks
;
1882 } else if (isparity(plex
)) { /* RAID 4 or 5 */
1883 data_sds
= plex
->subdisks
- 1;
1887 if (required_sds
> 0) { /* striped, RAID-4 or RAID-5 */
1888 if (plex
->subdisks
< required_sds
) {
1890 "vinum: plex %s does not have at least %d subdisks\n",
1893 state
= plex_faulty
;
1896 * Now see if the plex size is a multiple of
1897 * the stripe size. If not, trim off the end
1898 * of each subdisk and return it to the drive.
1900 if (plex
->length
> 0) {
1902 if (plex
->stripesize
> 0) {
1903 remainder
= (int) (plex
->length
/* are we exact? */
1904 % ((u_int64_t
) plex
->stripesize
* data_sds
));
1905 if (remainder
) { /* no */
1906 log(LOG_INFO
, "vinum: removing %d blocks of partial stripe at the end of %s\n",
1909 plex
->length
-= remainder
; /* shorten the plex */
1910 remainder
/= data_sds
; /* spread the remainder amongst the sds */
1911 for (sdno
= 0; sdno
< plex
->subdisks
; sdno
++) {
1912 sd
= &SD
[plex
->sdnos
[sdno
]]; /* point to the subdisk */
1913 return_drive_space(sd
->driveno
, /* return the space */
1914 sd
->driveoffset
+ sd
->sectors
- remainder
,
1916 sd
->sectors
-= remainder
; /* and shorten it */
1919 } else /* no data sds, */
1920 plex
->length
= 0; /* reset length */
1925 for (sdno
= 0; sdno
< plex
->subdisks
; sdno
++) {
1926 sd
= &SD
[plex
->sdnos
[sdno
]];
1929 && (sd
->sectors
!= SD
[plex
->sdnos
[sdno
- 1]].sectors
)) {
1930 log(LOG_ERR
, "vinum: %s must have equal sized subdisks\n", plex
->name
);
1933 size
+= sd
->sectors
;
1934 if (added_plex
) /* we were added later */
1935 sd
->state
= sd_stale
; /* stale until proven otherwise */
1938 if (plex
->subdisks
) { /* plex has subdisks, calculate size */
1940 * XXX We shouldn't need to calculate the size any
1941 * more. Check this some time
1944 size
= size
/ plex
->subdisks
* (plex
->subdisks
- 1); /* less space for RAID-4 and RAID-5 */
1945 if (plex
->length
!= size
)
1947 "Correcting length of %s: was %lld, is %lld\n",
1949 (long long) plex
->length
,
1951 plex
->length
= size
;
1952 } else { /* no subdisks, */
1953 plex
->length
= 0; /* no size */
1954 state
= plex_down
; /* take it down */
1956 update_plex_state(plexno
); /* set the state */
1957 plex
->flags
&= ~VF_NEWBORN
;
1961 update_volume_config(int volno
, int diskconfig
)
1963 struct volume
*vol
= &VOL
[volno
];
1967 if (vol
->state
!= volume_unallocated
)
1969 * Recalculate the size of the volume,
1970 * which might change if the original
1971 * plexes were not a multiple of the
1976 for (plexno
= 0; plexno
< vol
->plexes
; plexno
++) {
1977 plex
= &PLEX
[vol
->plex
[plexno
]];
1978 vol
->size
= max(plex
->length
, vol
->size
); /* maximum size */
1979 plex
->volplexno
= plexno
; /* note it in the plex */
1982 vol
->flags
&= ~VF_NEWBORN
; /* no longer newly born */
1986 * Update the global configuration.
1987 * diskconfig is != 0 if we're reading in a config
1988 * from disk. In this case, we don't try to
1989 * bring the devices up, though we will bring
1990 * them down if there's some error which got
1991 * missed when writing to disk.
1994 updateconfig(int diskconfig
)
1999 for (plexno
= 0; plexno
< vinum_conf
.plexes_allocated
; plexno
++)
2000 update_plex_config(plexno
, diskconfig
);
2002 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
2003 if (VOL
[volno
].state
> volume_uninit
) {
2004 VOL
[volno
].flags
&= ~VF_CONFIG_SETUPSTATE
; /* no more setupstate */
2005 update_volume_state(volno
);
2006 update_volume_config(volno
, diskconfig
);
2013 * Start manual changes to the configuration and lock out
2014 * others who may wish to do so.
2015 * XXX why do we need this and lock_config too?
2018 start_config(int force
)
2022 current_drive
= -1; /* note the last drive we mention, for
2024 current_plex
= -1; /* and the same for the last plex */
2025 current_volume
= -1; /* and the last volume */
2026 while ((vinum_conf
.flags
& VF_CONFIGURING
) != 0) {
2027 vinum_conf
.flags
|= VF_WILL_CONFIGURE
;
2028 if ((error
= tsleep(&vinum_conf
, PCATCH
, "vincfg", 0)) != 0)
2032 * We need two flags here: VF_CONFIGURING
2033 * tells other processes to hold off (this
2034 * function), and VF_CONFIG_INCOMPLETE
2035 * tells the state change routines not to
2036 * propagate incrememntal state changes
2038 vinum_conf
.flags
|= VF_CONFIGURING
| VF_CONFIG_INCOMPLETE
;
2040 vinum_conf
.flags
|= VF_FORCECONFIG
; /* overwrite differently named drives */
2041 current_drive
= -1; /* reset the defaults */
2042 current_plex
= -1; /* and the same for the last plex */
2043 current_volume
= -1; /* and the last volme */
2048 * Update the config if update is 1, and unlock
2049 * it. We won't update the configuration if we
2050 * are called in a recursive loop via throw_rude_remark.
2053 finish_config(int update
)
2055 /* we've finished our config */
2056 vinum_conf
.flags
&= ~(VF_CONFIG_INCOMPLETE
| VF_READING_CONFIG
| VF_FORCECONFIG
);
2058 updateconfig(0); /* so update things */
2060 updateconfig(1); /* do some updates only */
2061 vinum_conf
.flags
&= ~VF_CONFIGURING
; /* and now other people can take a turn */
2062 if ((vinum_conf
.flags
& VF_WILL_CONFIGURE
) != 0) {
2063 vinum_conf
.flags
&= ~VF_WILL_CONFIGURE
;
2064 wakeup_one(&vinum_conf
);
2067 /* Local Variables: */
2068 /* fill-column: 50 */