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 */
73 static void made_sd(struct sd
*sd
);
74 static void made_vol(struct volume
*vol
);
75 static void made_plex(struct plex
*plex
);
77 /* These values are used by most of these routines, so set them as globals */
78 static char *token
[MAXTOKEN
]; /* pointers to individual tokens */
79 static int tokens
; /* number of tokens */
90 #define MSG_MAX 1024 /* maximum length of a formatted message */
92 * Format an error message and return to the user in the reply.
93 * CARE: This routine is designed to be called only from the
94 * configuration routines, so it assumes it's the owner of
95 * the configuration lock, and unlocks it on exit
98 throw_rude_remark(int error
, char *msg
,...)
102 static int finishing
; /* don't recurse */
105 if ((vinum_conf
.flags
& VF_LOCKED
) == 0) /* bug catcher */
106 panic ("throw_rude_remark: called without config lock");
108 if ((ioctl_reply
!= NULL
) /* we're called from the user */
109 &&(!(vinum_conf
.flags
& VF_READING_CONFIG
))) { /* and not reading from disk: return msg */
111 * We can't just format to ioctl_reply, since it
112 * may contain our input parameters
114 kvasnrprintf(&text
, MSG_MAX
, 10, msg
, ap
);
115 strcpy(ioctl_reply
->msg
, text
);
116 ioctl_reply
->error
= error
; /* first byte is the error number */
120 kvprintf(msg
, ap
); /* print to the console */
125 if (vinum_conf
.flags
& VF_READING_CONFIG
) { /* go through to the bitter end, */
126 if ((vinum_conf
.flags
& VF_READING_CONFIG
) /* we're reading from disk, */
127 &&((daemon_options
& daemon_noupdate
) == 0)) {
128 log(LOG_NOTICE
, "Disabling configuration updates\n");
129 daemon_options
|= daemon_noupdate
;
134 * We have a problem here: we want to unlock the
135 * configuration, which implies tidying up, but
136 * if we find an error while tidying up, we could
137 * recurse for ever. Use this kludge to only try
140 was_finishing
= finishing
;
142 finish_config(was_finishing
); /* unlock anything we may be holding */
143 finishing
= was_finishing
;
144 longjmp(command_fail
, error
);
148 * Check a volume to see if the plex is already assigned to it.
149 * Return index in volume->plex, or -1 if not assigned
152 my_plex(int volno
, int plexno
)
157 vol
= &VOL
[volno
]; /* point to volno */
158 for (i
= 0; i
< vol
->plexes
; i
++)
159 if (vol
->plex
[i
] == plexno
)
161 return -1; /* not found */
165 * Check a plex to see if the subdisk is already assigned to it.
166 * Return index in plex->sd, or -1 if not assigned
169 my_sd(int plexno
, int sdno
)
174 plex
= &PLEX
[plexno
];
175 for (i
= 0; i
< plex
->subdisks
; i
++)
176 if (plex
->sdnos
[i
] == sdno
)
178 return -1; /* not found */
181 /* Add plex to the volume if possible */
183 give_plex_to_volume(int volno
, int plexno
)
189 * It's not an error for the plex to already
190 * belong to the volume, but we need to check a
191 * number of things to make sure it's done right.
194 if (my_plex(volno
, plexno
) >= 0)
195 return plexno
; /* that's it */
197 vol
= &VOL
[volno
]; /* point to volume */
198 if (vol
->plexes
== MAXPLEX
) /* all plexes allocated */
199 throw_rude_remark(ENOSPC
,
200 "Too many plexes for volume %s",
202 else if ((vol
->plexes
> 0) /* we have other plexes */
203 &&((vol
->flags
& VF_CONFIG_SETUPSTATE
) == 0)) /* and we're not setting up state */
204 invalidate_subdisks(&PLEX
[plexno
], sd_stale
); /* make the subdisks invalid */
205 vol
->plex
[vol
->plexes
] = plexno
; /* this one */
206 vol
->plexes
++; /* add another plex */
207 PLEX
[plexno
].volno
= volno
; /* note the number of our volume */
209 /* Find out how big our volume is */
210 for (i
= 0; i
< vol
->plexes
; i
++)
211 vol
->size
= u64max(vol
->size
, PLEX
[vol
->plex
[i
]].length
);
212 return vol
->plexes
- 1; /* and return its index */
216 * Add subdisk to a plex if possible
219 give_sd_to_plex(int plexno
, int sdno
)
226 * It's not an error for the sd to already
227 * belong to the plex, but we need to check a
228 * number of things to make sure it's done right.
231 i
= my_sd(plexno
, sdno
);
232 if (i
>= 0) /* does it already belong to us? */
233 return i
; /* that's it */
235 plex
= &PLEX
[plexno
]; /* point to the plex */
236 sd
= &SD
[sdno
]; /* and the subdisk */
238 /* Do we have an offset? Otherwise put it after the last one */
239 if (sd
->plexoffset
< 0) { /* no offset specified */
240 if (plex
->subdisks
> 0) {
241 struct sd
*lastsd
= &SD
[plex
->sdnos
[plex
->subdisks
- 1]]; /* last subdisk */
243 if (plex
->organization
== plex_concat
) /* concat, */
244 sd
->plexoffset
= lastsd
->sectors
+ lastsd
->plexoffset
; /* starts here */
245 else /* striped, RAID-4 or RAID-5 */
246 sd
->plexoffset
= plex
->stripesize
* plex
->subdisks
; /* starts here */
247 } else /* first subdisk */
248 sd
->plexoffset
= 0; /* start at the beginning */
250 if (plex
->subdisks
== MAXSD
) /* we already have our maximum */
251 throw_rude_remark(ENOSPC
, /* crap out */
252 "Can't add %s to %s: plex full",
256 plex
->subdisks
++; /* another entry */
257 if (plex
->subdisks
>= plex
->subdisks_allocated
) /* need more space */
258 EXPAND(plex
->sdnos
, int, plex
->subdisks_allocated
, INITIAL_SUBDISKS_IN_PLEX
);
260 /* Adjust size of plex and volume. */
261 if (isparity(plex
)) /* RAID-4 or RAID-5 */
262 plex
->length
= (plex
->subdisks
- 1) * sd
->sectors
; /* size is one disk short */
264 plex
->length
+= sd
->sectors
; /* plex gets this much bigger */
265 if (plex
->volno
>= 0) /* we have a volume */
266 VOL
[plex
->volno
].size
= u64max(VOL
[plex
->volno
].size
, plex
->length
); /* adjust its size */
269 * We need to check that the subdisks don't overlap,
270 * but we can't do that until a point where we *must*
271 * know the size of all the subdisks. That's not
272 * here. But we need to sort them by offset
274 for (i
= 0; i
< plex
->subdisks
- 1; i
++) {
275 if (sd
->plexoffset
< SD
[plex
->sdnos
[i
]].plexoffset
) { /* it fits before this one */
276 /* First move any remaining subdisks by one */
279 for (j
= plex
->subdisks
- 1; j
> i
; j
--) /* move up one at a time */
280 plex
->sdnos
[j
] = plex
->sdnos
[j
- 1];
281 plex
->sdnos
[i
] = sdno
;
282 sd
->plexsdno
= i
; /* note where we are in the subdisk */
288 * The plex doesn't have any subdisk with a
289 * larger offset. Insert it here.
291 plex
->sdnos
[i
] = sdno
;
292 sd
->plexsdno
= i
; /* note where we are in the subdisk */
293 sd
->plexno
= plex
->plexno
; /* and who we belong to */
298 * Add a subdisk to drive if possible. The
299 * pointer to the drive must already be stored in
300 * the sd structure, but the drive doesn't know
301 * about the subdisk yet.
304 give_sd_to_drive(int sdno
)
306 struct sd
*sd
; /* pointer to subdisk */
307 struct drive
*drive
; /* and drive */
308 int fe
; /* index in free list */
309 int sfe
; /* and index of subdisk when assigning max */
311 sd
= &SD
[sdno
]; /* point to sd */
312 drive
= &DRIVE
[sd
->driveno
]; /* and drive */
314 if (drive
->state
!= drive_up
) {
315 update_sd_state(sdno
); /* that crashes the subdisk */
318 if (drive
->flags
& VF_HOTSPARE
) /* the drive is a hot spare, */
319 throw_rude_remark(ENOSPC
,
320 "Can't place %s on hot spare drive %s",
323 if ((drive
->sectors_available
== 0) /* no space left */
324 ||(sd
->sectors
> drive
->sectors_available
)) { /* or too big, */
325 sd
->driveoffset
= -1; /* don't be confusing */
327 throw_rude_remark(ENOSPC
, "No space for %s on %s", sd
->name
, drive
->label
.name
);
328 return; /* in case we come back here */
330 drive
->subdisks_used
++; /* one more subdisk */
332 if (sd
->sectors
== 0) { /* take the largest chunk */
333 sfe
= 0; /* to keep the compiler happy */
334 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
335 if (drive
->freelist
[fe
].sectors
>= sd
->sectors
) { /* more space here */
336 sd
->sectors
= drive
->freelist
[fe
].sectors
; /* take it */
337 sd
->driveoffset
= drive
->freelist
[fe
].offset
;
338 sfe
= fe
; /* and note the index for later */
341 if (sd
->sectors
== 0) { /* no luck, */
342 sd
->driveoffset
= -1; /* don't be confusing */
344 throw_rude_remark(ENOSPC
, /* give up */
345 "No space for %s on %s",
349 if (sfe
< (drive
->freelist_entries
- 1)) /* not the last one, */
350 bcopy(&drive
->freelist
[sfe
+ 1],
351 &drive
->freelist
[sfe
],
352 (drive
->freelist_entries
- sfe
) * sizeof(struct drive_freelist
));
353 drive
->freelist_entries
--; /* one less entry */
354 drive
->sectors_available
-= sd
->sectors
; /* and note how much less space we have */
355 } else if (sd
->driveoffset
< 0) { /* no offset specified, find one */
356 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
357 if (drive
->freelist
[fe
].sectors
>= sd
->sectors
) { /* it'll fit here */
358 sd
->driveoffset
= drive
->freelist
[fe
].offset
;
359 if (sd
->sectors
== drive
->freelist
[fe
].sectors
) { /* used up the entire entry */
360 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one, */
361 bcopy(&drive
->freelist
[fe
+ 1],
362 &drive
->freelist
[fe
],
363 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
364 drive
->freelist_entries
--; /* one less entry */
366 drive
->freelist
[fe
].sectors
-= sd
->sectors
; /* this much less space */
367 drive
->freelist
[fe
].offset
+= sd
->sectors
; /* this much further on */
369 drive
->sectors_available
-= sd
->sectors
; /* and note how much less space we have */
373 if (sd
->driveoffset
< 0)
375 * Didn't find anything. Although the drive has
376 * enough space, it's too fragmented
380 throw_rude_remark(ENOSPC
, "No space for %s on %s", sd
->name
, drive
->label
.name
);
382 } else { /* specific offset */
384 * For a specific offset to work, the space must be
385 * entirely in a single freelist entry. Look for it.
387 u_int64_t sdend
= sd
->driveoffset
+ sd
->sectors
; /* end of our subdisk */
388 for (fe
= 0; fe
< drive
->freelist_entries
; fe
++) {
389 u_int64_t dend
= drive
->freelist
[fe
].offset
+ drive
->freelist
[fe
].sectors
; /* end of entry */
390 if (dend
>= sdend
) { /* fits before here */
391 if (drive
->freelist
[fe
].offset
> sd
->driveoffset
) { /* starts after the beginning of sd area */
392 sd
->driveoffset
= -1; /* don't be confusing */
393 set_sd_state(sd
->sdno
, sd_down
, setstate_force
);
394 throw_rude_remark(ENOSPC
,
395 "No space for %s on drive %s at offset %lld",
402 * We've found the space, and we can allocate it.
403 * We don't need to say that to the subdisk, which
404 * already knows about it. We need to tell it to
405 * the free list, though. We have four possibilities:
407 * 1. The subdisk exactly eats up the entry. That's the
409 * 2. The subdisk starts at the beginning and leaves space
411 * 3. The subdisk starts after the beginning and leaves
412 * space at the end as well: we end up with another
414 * 4. The subdisk leaves space at the beginning and finishes
417 drive
->sectors_available
-= sd
->sectors
; /* note how much less space we have */
418 if (sd
->driveoffset
== drive
->freelist
[fe
].offset
) { /* 1 or 2 */
419 if (sd
->sectors
== drive
->freelist
[fe
].sectors
) { /* 1: used up the entire entry */
420 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one, */
421 bcopy(&drive
->freelist
[fe
+ 1],
422 &drive
->freelist
[fe
],
423 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
424 drive
->freelist_entries
--; /* one less entry */
425 } else { /* 2: space at the end */
426 drive
->freelist
[fe
].sectors
-= sd
->sectors
; /* this much less space */
427 drive
->freelist
[fe
].offset
+= sd
->sectors
; /* this much further on */
429 } else { /* 3 or 4 */
430 drive
->freelist
[fe
].sectors
= sd
->driveoffset
- drive
->freelist
[fe
].offset
;
431 if (dend
> sdend
) { /* 3: space at the end as well */
432 if (fe
< (drive
->freelist_entries
- 1)) /* not the last one */
433 bcopy(&drive
->freelist
[fe
], /* move the rest down */
434 &drive
->freelist
[fe
+ 1],
435 (drive
->freelist_entries
- fe
) * sizeof(struct drive_freelist
));
436 drive
->freelist_entries
++; /* one less entry */
437 drive
->freelist
[fe
+ 1].offset
= sdend
; /* second entry starts after sd */
438 drive
->freelist
[fe
+ 1].sectors
= dend
- sdend
; /* and is this long */
445 drive
->opencount
++; /* one more subdisk attached */
448 /* Get an empty drive entry from the drive table */
450 get_empty_drive(void)
455 /* first see if we have one which has been deallocated */
456 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
457 if (DRIVE
[driveno
].state
== drive_unallocated
) /* bingo */
461 if (driveno
>= vinum_conf
.drives_allocated
) /* we've used all our allocation */
462 EXPAND(DRIVE
, struct drive
, vinum_conf
.drives_allocated
, INITIAL_DRIVES
);
464 /* got a drive entry. Make it pretty */
465 drive
= &DRIVE
[driveno
];
466 bzero(drive
, sizeof(struct drive
));
467 drive
->driveno
= driveno
; /* put number in structure */
468 drive
->flags
|= VF_NEWBORN
; /* newly born drive */
469 strcpy(drive
->devicename
, "unknown"); /* and make the name ``unknown'' */
470 return driveno
; /* return the index */
474 * Find the named drive in vinum_conf.drive, return a pointer
475 * return the index in vinum_conf.drive.
476 * Don't mark the drive as allocated (XXX SMP)
477 * If create != 0, create an entry if it doesn't exist
479 /* XXX check if we have it open from attach */
481 find_drive(const char *name
, int create
)
487 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
488 drive
= &DRIVE
[driveno
]; /* point to drive */
489 if ((drive
->label
.name
[0] != '\0') /* it has a name */
490 &&(strcmp(drive
->label
.name
, name
) == 0) /* and it's this one */
491 &&(drive
->state
> drive_unallocated
)) /* and it's a real one: found */
495 /* the drive isn't in the list. Add it if he wants */
496 if (create
== 0) /* don't want to create */
497 return -1; /* give up */
499 driveno
= get_empty_drive();
500 drive
= &DRIVE
[driveno
];
502 ksnprintf(drive
->label
.name
, sizeof(drive
->label
.name
), "%s", name
);
503 drive
->state
= drive_referenced
; /* in use, nothing worthwhile there */
504 return driveno
; /* return the index */
508 * Find a drive given its device name.
509 * devname must be valid.
510 * Otherwise the same as find_drive above
513 find_drive_by_dev(const char *devname
, int create
)
518 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
519 drive
= &DRIVE
[driveno
];
520 if (strcmp(drive
->devicename
, devname
) == 0 &&
521 drive
->state
> drive_unallocated
530 driveno
= get_empty_drive();
531 drive
= &DRIVE
[driveno
];
532 ksnprintf(drive
->devicename
, sizeof(drive
->devicename
), "%s", devname
);
533 /* in use, nothing worthwhile there */
534 drive
->state
= drive_referenced
;
538 /* Find an empty subdisk in the subdisk table */
545 /* first see if we have one which has been deallocated */
546 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
547 if (SD
[sdno
].state
== sd_unallocated
) /* bingo */
550 if (sdno
>= vinum_conf
.subdisks_allocated
)
552 * We've run out of space. sdno is pointing
553 * where we want it, but at the moment we
554 * don't have the space. Get it.
556 EXPAND(SD
, struct sd
, vinum_conf
.subdisks_allocated
, INITIAL_SUBDISKS
);
558 /* initialize some things */
559 sd
= &SD
[sdno
]; /* point to it */
560 bzero(sd
, sizeof(struct sd
)); /* initialize */
561 sd
->flags
|= VF_NEWBORN
; /* newly born subdisk */
562 sd
->plexno
= -1; /* no plex */
563 sd
->sectors
= -1; /* no space */
564 sd
->driveno
= -1; /* no drive */
565 sd
->plexoffset
= -1; /* and no offsets */
566 sd
->driveoffset
= -1;
567 return sdno
; /* return the index */
570 /* return a drive to the free pool */
572 free_drive(struct drive
*drive
)
574 if ((drive
->state
> drive_referenced
) /* real drive */
575 ||(drive
->flags
& VF_OPEN
)) { /* how can it be open without a state? */
577 if (drive
->flags
& VF_OPEN
) { /* it's open, */
578 close_locked_drive(drive
); /* close it */
579 drive
->state
= drive_down
; /* and note the fact */
582 Free(drive
->freelist
);
583 bzero(drive
, sizeof(struct drive
)); /* this also sets drive_unallocated */
589 * Find the named subdisk in vinum_conf.sd.
591 * If create != 0, create an entry if it doesn't exist
593 * Return index in vinum_conf.sd
596 find_subdisk(const char *name
, int create
)
601 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
602 if (strcmp(SD
[sdno
].name
, name
) == 0) /* found it */
606 /* the subdisk isn't in the list. Add it if he wants */
607 if (create
== 0) /* don't want to create */
608 return -1; /* give up */
610 /* Allocate one and insert the name */
611 sdno
= get_empty_sd();
613 ksnprintf(sd
->name
, sizeof(sd
->name
), "%s", name
);
614 return sdno
; /* return the pointer */
617 /* Return space to a drive */
619 return_drive_space(int driveno
, int64_t offset
, int length
)
622 int fe
; /* free list entry */
623 u_int64_t sdend
; /* end of our subdisk */
624 u_int64_t dend
; /* end of our freelist entry */
626 drive
= &DRIVE
[driveno
];
627 if (drive
->state
== drive_up
) {
628 sdend
= offset
+ length
; /* end of our subdisk */
630 /* Look for where to return the sd address space */
632 (fe
< drive
->freelist_entries
) && (drive
->freelist
[fe
].offset
< offset
);
635 * Now we are pointing to the last entry, the first
636 * with a higher offset than the subdisk, or both.
638 if ((fe
> 1) /* not the first entry */
639 &&((fe
== drive
->freelist_entries
) /* gone past the end */
640 ||(drive
->freelist
[fe
].offset
> offset
))) /* or past the block were looking for */
641 fe
--; /* point to the block before */
642 dend
= drive
->freelist
[fe
].offset
+ drive
->freelist
[fe
].sectors
; /* end of the entry */
645 * At this point, we are pointing to the correct
646 * place in the free list. A number of possibilities
649 * 1. The block to be freed starts at the end of the
650 * block to which we are pointing. This has two
653 * a. The block to be freed ends at the beginning
654 * of the following block. Merge the three
655 * areas into a single block.
657 * b. The block is shorter than the space between
658 * the current block and the next one. Enlarge
661 * 2. The block to be freed starts after the end
662 * of the block. Again, we have two cases:
664 * a. It ends before the start of the following block.
665 * Create a new free block.
667 * b. It ends at the start of the following block.
668 * Enlarge the following block downwards.
670 * When there is only one free space block, and the
671 * space to be returned is before it, the pointer is
672 * to a non-existent zeroth block. XXX check this
674 if (offset
== dend
) { /* Case 1: it starts at the end of this block */
675 if ((fe
< drive
->freelist_entries
- 1) /* we're not the last block in the free list */
676 /* and the subdisk ends at the start of the next block */
677 &&(sdend
== drive
->freelist
[fe
+ 1].offset
)) {
678 drive
->freelist
[fe
].sectors
/* 1a: merge all three blocks */
679 = drive
->freelist
[fe
+ 1].sectors
;
680 if (fe
< drive
->freelist_entries
- 2) /* still more blocks after next */
681 bcopy(&drive
->freelist
[fe
+ 2], /* move down one */
682 &drive
->freelist
[fe
+ 1],
683 (drive
->freelist_entries
- 2 - fe
)
684 * sizeof(struct drive_freelist
));
685 drive
->freelist_entries
--; /* one less entry in the free list */
686 } else /* 1b: just enlarge this block */
687 drive
->freelist
[fe
].sectors
+= length
;
688 } else { /* Case 2 */
689 if (offset
> dend
) /* it starts after this block */
690 fe
++; /* so look at the next block */
691 if ((fe
< drive
->freelist_entries
) /* we're not the last block in the free list */
692 /* and the subdisk ends at the start of this block: case 4 */
693 &&(sdend
== drive
->freelist
[fe
].offset
)) {
694 drive
->freelist
[fe
].offset
= offset
; /* it starts where the sd was */
695 drive
->freelist
[fe
].sectors
+= length
; /* and it's this much bigger */
696 } else { /* case 3: non-contiguous */
697 if (fe
< drive
->freelist_entries
) /* not after the last block, */
698 bcopy(&drive
->freelist
[fe
], /* move the rest up one entry */
699 &drive
->freelist
[fe
+ 1],
700 (drive
->freelist_entries
- fe
)
701 * sizeof(struct drive_freelist
));
702 drive
->freelist_entries
++; /* one less entry */
703 drive
->freelist
[fe
].offset
= offset
; /* this entry represents the sd */
704 drive
->freelist
[fe
].sectors
= length
;
707 drive
->sectors_available
+= length
; /* the sectors are now available */
712 * Free an allocated sd entry.
713 * This performs memory management only. remove()
714 * is responsible for checking relationships.
722 if ((sd
->driveno
>= 0) /* we have a drive, */
723 &&(sd
->sectors
> 0)) /* and some space on it */
724 return_drive_space(sd
->driveno
, /* return the space */
728 PLEX
[sd
->plexno
].subdisks
--; /* one less subdisk */
729 sd
->state
= sd_unallocated
;
731 bzero(sd
, sizeof(struct sd
)); /* and clear it out */
732 sd
->state
= sd_unallocated
;
733 vinum_conf
.subdisks_used
--; /* one less sd */
737 made_sd(struct sd
*sd
)
739 if (sd
->sd_dev
== NULL
&& sd
->state
!= sd_unallocated
) {
740 sd
->sd_dev
= make_dev(&vinum_ops
, VINUM_SD(sd
->sdno
),
741 UID_ROOT
, GID_OPERATOR
, 0640,
742 VINUM_BASE
"sd/%s", sd
->name
);
744 if (sd
->plexno
>= 0 && PLEX
[sd
->plexno
].volno
>= 0) {
745 make_dev_alias(sd
->sd_dev
, "vol/%s.plex/%s",
746 VOL
[PLEX
[sd
->plexno
].volno
].name
,
747 plex
->name
, VOL
[plex
->volno
].name
);
751 if (sd
->sd_dev
&& sd
->state
== sd_unallocated
) {
752 destroy_dev(sd
->sd_dev
);
758 made_vol(struct volume
*vol
)
760 if (vol
->vol_dev
== NULL
&& vol
->state
!= volume_unallocated
) {
761 vol
->vol_dev
= make_dev(&vinum_ops
,
762 VINUMDEV(vol
->volno
, 0, 0, VINUM_VOLUME_TYPE
),
763 UID_ROOT
, GID_OPERATOR
, 0640,
764 VINUM_BASE
"vol/%s", vol
->name
);
766 if (vol
->vol_dev
&& vol
->state
== volume_unallocated
) {
767 destroy_dev(vol
->vol_dev
);
773 made_plex(struct plex
*plex
)
775 if (plex
->plex_dev
== NULL
&& plex
->state
!= plex_unallocated
) {
776 plex
->plex_dev
= make_dev(&vinum_ops
, VINUM_PLEX(plex
->plexno
),
777 UID_ROOT
, GID_OPERATOR
, 0640,
778 VINUM_BASE
"plex/%s", plex
->name
);
779 if (plex
->volno
>= 0) {
780 make_dev_alias(plex
->plex_dev
, "vol/%s.plex/%s",
781 plex
->name
, VOL
[plex
->volno
].name
);
784 if (plex
->plex_dev
&& plex
->state
== plex_unallocated
) {
785 destroy_dev(plex
->plex_dev
);
786 plex
->plex_dev
= NULL
;
790 /* Find an empty plex in the plex table */
795 struct plex
*plex
; /* if we allocate one */
797 /* first see if we have one which has been deallocated */
798 for (plexno
= 0; plexno
< vinum_conf
.plexes_allocated
; plexno
++) {
799 if (PLEX
[plexno
].state
== plex_unallocated
) /* bingo */
800 break; /* and get out of here */
803 if (plexno
>= vinum_conf
.plexes_allocated
)
804 EXPAND(PLEX
, struct plex
, vinum_conf
.plexes_allocated
, INITIAL_PLEXES
);
806 /* Found a plex. Give it an sd structure */
807 plex
= &PLEX
[plexno
]; /* this one is ours */
808 bzero(plex
, sizeof(struct plex
)); /* polish it up */
809 plex
->sdnos
= (int *) Malloc(sizeof(int) * INITIAL_SUBDISKS_IN_PLEX
); /* allocate sd table */
810 CHECKALLOC(plex
->sdnos
, "vinum: Can't allocate plex subdisk table");
811 bzero(plex
->sdnos
, (sizeof(int) * INITIAL_SUBDISKS_IN_PLEX
)); /* do we need this? */
812 plex
->flags
|= VF_NEWBORN
; /* newly born plex */
813 plex
->subdisks
= 0; /* no subdisks in use */
814 plex
->subdisks_allocated
= INITIAL_SUBDISKS_IN_PLEX
; /* and we have space for this many */
815 plex
->organization
= plex_disorg
; /* and it's not organized */
816 plex
->volno
= -1; /* no volume yet */
817 return plexno
; /* return the index */
821 * Find the named plex in vinum_conf.plex
823 * If create != 0, create an entry if it doesn't exist
824 * return index in vinum_conf.plex
827 find_plex(const char *name
, int create
)
832 for (plexno
= 0; plexno
< vinum_conf
.plexes_allocated
; plexno
++) {
833 if (strcmp(PLEX
[plexno
].name
, name
) == 0) /* found it */
837 /* the plex isn't in the list. Add it if he wants */
838 if (create
== 0) /* don't want to create */
839 return -1; /* give up */
841 /* Allocate one and insert the name */
842 plexno
= get_empty_plex();
843 plex
= &PLEX
[plexno
]; /* point to it */
844 ksnprintf(plex
->name
, sizeof(plex
->name
), "%s", name
);
845 return plexno
; /* return the pointer */
849 * Free an allocated plex entry
850 * and its associated memory areas
853 free_plex(int plexno
)
857 plex
= &PLEX
[plexno
];
862 plex
->state
= plex_unallocated
;
864 bzero(plex
, sizeof(struct plex
)); /* and clear it out */
865 plex
->state
= plex_unallocated
;
868 /* Find an empty volume in the volume table */
870 get_empty_volume(void)
876 /* first see if we have one which has been deallocated */
877 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
878 if (VOL
[volno
].state
== volume_unallocated
) /* bingo */
882 if (volno
>= vinum_conf
.volumes_allocated
)
883 EXPAND(VOL
, struct volume
, vinum_conf
.volumes_allocated
, INITIAL_VOLUMES
);
885 /* Now initialize fields */
887 bzero(vol
, sizeof(struct volume
));
888 vol
->flags
|= VF_NEWBORN
| VF_CREATED
; /* newly born volume */
889 vol
->preferred_plex
= ROUND_ROBIN_READPOL
; /* round robin */
890 for (i
= 0; i
< MAXPLEX
; i
++) /* mark the plexes missing */
892 return volno
; /* return the index */
896 * Find the named volume in vinum_conf.volume.
898 * If create != 0, create an entry if it doesn't exist
899 * return the index in vinum_conf
902 find_volume(const char *name
, int create
)
907 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
908 if (strcmp(VOL
[volno
].name
, name
) == 0) /* found it */
912 /* the volume isn't in the list. Add it if he wants */
913 if (create
== 0) /* don't want to create */
914 return -1; /* give up */
916 /* Allocate one and insert the name */
917 volno
= get_empty_volume();
919 ksnprintf(vol
->name
, sizeof(vol
->name
), "%s", name
);
920 vol
->blocksize
= DEV_BSIZE
; /* block size of this volume */
921 return volno
; /* return the pointer */
925 * Free an allocated volume entry
926 * and its associated memory areas
929 free_volume(int volno
)
934 vol
->state
= volume_unallocated
;
936 bzero(vol
, sizeof(struct volume
)); /* and clear it out */
937 vol
->state
= volume_unallocated
;
941 * Handle a drive definition. We store the information in the global variable
942 * drive, so we don't need to allocate.
944 * If we find an error, print a message and return
947 config_drive(int update
)
949 enum drive_label_info partition_status
; /* info about the partition */
951 int driveno
; /* index of drive in vinum_conf */
952 struct drive
*drive
; /* and pointer to it */
953 int otherdriveno
; /* index of possible second drive */
956 if (tokens
< 2) /* not enough tokens */
957 throw_rude_remark(EINVAL
, "Drive has no name\n");
958 driveno
= find_drive(token
[1], 1); /* allocate a drive to initialize */
959 drive
= &DRIVE
[driveno
]; /* and get a pointer */
960 if (update
&& ((drive
->flags
& VF_NEWBORN
) == 0)) /* this drive exists already */
961 return; /* don't do anything */
962 drive
->flags
&= ~VF_NEWBORN
; /* no longer newly born */
964 if (drive
->state
!= drive_referenced
) { /* we already know this drive */
966 * XXX Check which definition is more up-to-date. Give
967 * preference for the definition on its own drive.
971 for (parameter
= 2; parameter
< tokens
; parameter
++) { /* look at the other tokens */
972 switch (get_keyword(token
[parameter
], &keyword_set
)) {
975 otherdriveno
= find_drive_by_dev(token
[parameter
], 0); /* see if it exists already */
976 if (otherdriveno
>= 0) { /* yup, */
977 drive
->state
= drive_unallocated
; /* deallocate the drive */
978 throw_rude_remark(EEXIST
, /* and complain */
979 "Drive %s would have same device as drive %s",
981 DRIVE
[otherdriveno
].label
.name
);
983 if (drive
->devicename
[0] == '/') { /* we know this drive... */
984 if (strcmp(drive
->devicename
, token
[parameter
])) /* different name */
985 close_drive(drive
); /* close it if it's open */
991 * open the device and get the configuration
993 ksnprintf(drive
->devicename
, sizeof(drive
->devicename
),
994 "%s", token
[parameter
]);
995 partition_status
= read_drive_label(drive
, 1);
997 switch (partition_status
) {
998 case DL_CANT_OPEN
: /* not our kind */
1000 if (drive
->lasterror
== EFTYPE
) /* wrong kind of partition */
1001 throw_rude_remark(drive
->lasterror
,
1002 "Drive %s has invalid partition type",
1004 else /* I/O error of some kind */
1005 throw_rude_remark(drive
->lasterror
,
1006 "Can't initialize drive %s",
1010 case DL_WRONG_DRIVE
: /* valid drive, not the name we expected */
1011 if (vinum_conf
.flags
& VF_FORCECONFIG
) { /* but we'll accept that */
1012 bcopy(token
[1], drive
->label
.name
, sizeof(drive
->label
.name
));
1017 * There's a potential race condition here:
1018 * the rude remark refers to a field in an
1019 * unallocated drive, which potentially could
1020 * be reused. This works because we're the only
1021 * thread accessing the config at the moment.
1023 drive
->state
= drive_unallocated
; /* throw it away completely */
1024 throw_rude_remark(drive
->lasterror
,
1025 "Incorrect drive name %s specified for drive %s",
1030 case DL_DELETED_LABEL
: /* it was a drive, but we deleted it */
1031 case DL_NOT_OURS
: /* nothing to do with the rest */
1036 * read_drive_label overwrites the device name.
1037 * If we get here, we can have the drive,
1038 * so put it back again
1040 ksnprintf(drive
->devicename
, sizeof(drive
->devicename
),
1041 "%s", token
[parameter
]);
1045 parameter
++; /* skip the keyword */
1046 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1047 drive
->state
= DriveState(token
[parameter
]); /* set the state */
1050 case kw_hotspare
: /* this drive is a hot spare */
1051 drive
->flags
|= VF_HOTSPARE
;
1056 throw_rude_remark(EINVAL
,
1057 "Drive %s, invalid keyword: %s",
1063 if (drive
->devicename
[0] != '/') {
1064 drive
->state
= drive_unallocated
; /* deallocate the drive */
1065 throw_rude_remark(EINVAL
, "No device name for %s", drive
->label
.name
);
1067 vinum_conf
.drives_used
++; /* passed all hurdles: one more in use */
1069 * If we're replacing a drive, it could be that
1070 * we already have subdisks referencing this
1071 * drive. Note where they should be and change
1072 * their state to obsolete.
1074 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
1075 if ((SD
[sdno
].state
> sd_referenced
)
1076 && (SD
[sdno
].driveno
== driveno
)) {
1077 give_sd_to_drive(sdno
);
1078 if (SD
[sdno
].state
> sd_stale
)
1079 SD
[sdno
].state
= sd_stale
;
1085 * Handle a subdisk definition. We store the information in the global variable
1086 * sd, so we don't need to allocate.
1088 * If we find an error, print a message and return
1091 config_subdisk(int update
)
1094 int sdno
; /* index of sd in vinum_conf */
1095 struct sd
*sd
; /* and pointer to it */
1097 int detached
= 0; /* set to 1 if this is a detached subdisk */
1098 int sdindex
= -1; /* index in plexes subdisk table */
1099 enum sdstate state
= sd_unallocated
; /* state to set, if specified */
1100 int autosize
= 0; /* set if we autosize in give_sd_to_drive */
1101 int namedsdno
; /* index of another with this name */
1102 char partition
= 0; /* partition of external subdisk */
1104 sdno
= get_empty_sd(); /* allocate an SD to initialize */
1105 sd
= &SD
[sdno
]; /* and get a pointer */
1107 for (parameter
= 1; parameter
< tokens
; parameter
++) { /* look at the other tokens */
1108 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1110 * If we have a 'name' parameter, it must
1111 * come first, because we're too lazy to tidy
1112 * up dangling refs if it comes later.
1115 namedsdno
= find_subdisk(token
[++parameter
], 0); /* find an existing sd with this name */
1116 if (namedsdno
>= 0) { /* got one */
1117 if (SD
[namedsdno
].state
== sd_referenced
) { /* we've been told about this one */
1119 throw_rude_remark(EINVAL
,
1120 "sd %s: name parameter must come first\n", /* no go */
1124 struct plex
*plex
; /* for tidying up dangling references */
1126 *sd
= SD
[namedsdno
]; /* copy from the referenced one */
1129 SD
[namedsdno
].state
= sd_unallocated
; /* and deallocate the referenced one */
1130 made_sd(&SD
[namedsdno
]);
1131 plex
= &PLEX
[sd
->plexno
]; /* now take a look at our plex */
1132 for (i
= 0; i
< plex
->subdisks
; i
++) { /* look for the pointer */
1133 if (plex
->sdnos
[i
] == namedsdno
) /* pointing to the old subdisk */
1134 plex
->sdnos
[i
] = sdno
; /* bend it to point here */
1138 if (update
) /* are we updating? */
1139 return; /* that's OK, nothing more to do */
1141 throw_rude_remark(EINVAL
, "Duplicate subdisk %s", token
[parameter
]);
1143 ksnprintf(sd
->name
, sizeof(sd
->name
),
1144 "%s", token
[parameter
]);
1153 size
= sizespec(token
[++parameter
]);
1154 if ((size
== -1) /* unallocated */
1155 &&(vinum_conf
.flags
& VF_READING_CONFIG
)) /* reading from disk */
1156 break; /* invalid sd; just ignore it */
1157 if ((size
% DEV_BSIZE
) != 0)
1158 throw_rude_remark(EINVAL
,
1159 "sd %s, bad plex offset alignment: %lld",
1163 sd
->plexoffset
= size
/ DEV_BSIZE
;
1166 case kw_driveoffset
:
1167 size
= sizespec(token
[++parameter
]);
1168 if ((size
== -1) /* unallocated */
1169 &&(vinum_conf
.flags
& VF_READING_CONFIG
)) /* reading from disk */
1170 break; /* invalid sd; just ignore it */
1171 if ((size
% DEV_BSIZE
) != 0)
1172 throw_rude_remark(EINVAL
,
1173 "sd %s, bad drive offset alignment: %lld",
1177 sd
->driveoffset
= size
/ DEV_BSIZE
;
1181 if (get_keyword(token
[++parameter
], &keyword_set
) == kw_max
) /* select maximum size from drive */
1182 size
= 0; /* this is how we say it :-) */
1184 size
= sizespec(token
[parameter
]);
1185 if ((size
% DEV_BSIZE
) != 0)
1186 throw_rude_remark(EINVAL
, "sd %s, length %d not multiple of sector size", sd
->name
, size
);
1188 sd
->sectors
= size
/ DEV_BSIZE
;
1190 * We have a problem with autosizing: we need to
1191 * give the drive to the plex before we give it
1192 * to the drive, in order to be clean if we give
1193 * up in the middle, but at this time the size hasn't
1194 * been set. Note that we have to fix up after
1195 * giving the subdisk to the drive.
1198 autosize
= 1; /* note that we're autosizing */
1202 sd
->driveno
= find_drive(token
[++parameter
], 1); /* insert drive information */
1206 sd
->plexno
= find_plex(token
[++parameter
], 1); /* insert plex information */
1210 * Set the state. We can't do this directly,
1211 * because give_sd_to_plex may change it
1214 parameter
++; /* skip the keyword */
1215 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1216 state
= SdState(token
[parameter
]); /* set the state */
1220 parameter
++; /* skip the keyword */
1221 if ((strlen(token
[parameter
]) != 1)
1222 || (token
[parameter
][0] < 'a')
1223 || (token
[parameter
][0] > 'p'))
1224 throw_rude_remark(EINVAL
,
1225 "%s: invalid partition %c",
1227 token
[parameter
][0]);
1229 partition
= token
[parameter
][0];
1232 case kw_retryerrors
:
1233 sd
->flags
|= VF_RETRYERRORS
;
1237 throw_rude_remark(EINVAL
, "%s: invalid keyword: %s", sd
->name
, token
[parameter
]);
1241 /* Check we have a drive name */
1242 if (sd
->driveno
< 0) { /* didn't specify a drive */
1243 sd
->driveno
= current_drive
; /* set to the current drive */
1244 if (sd
->driveno
< 0) /* no current drive? */
1245 throw_rude_remark(EINVAL
, "Subdisk %s is not associated with a drive", sd
->name
);
1248 * This is tacky. If something goes wrong
1249 * with the checks, we may end up losing drive
1252 if (autosize
!= 0) /* need to find a size, */
1253 give_sd_to_drive(sdno
); /* do it before the plex */
1255 /* Check for a plex name */
1256 if ((sd
->plexno
< 0) /* didn't specify a plex */
1257 &&(!detached
)) /* and didn't say not to, */
1258 sd
->plexno
= current_plex
; /* set to the current plex */
1260 if (sd
->plexno
>= 0)
1261 sdindex
= give_sd_to_plex(sd
->plexno
, sdno
); /* now tell the plex that it has this sd */
1263 sd
->sdno
= sdno
; /* point to our entry in the table */
1265 /* Does the subdisk have a name? If not, give it one */
1266 if (sd
->name
[0] == '\0') { /* no name */
1267 char sdsuffix
[8]; /* form sd name suffix here */
1269 /* Do we have a plex name? */
1270 if (sdindex
>= 0) /* we have a plex */
1271 strcpy(sd
->name
, PLEX
[sd
->plexno
].name
); /* take it from there */
1273 throw_rude_remark(EINVAL
, "Unnamed sd is not associated with a plex");
1274 ksprintf(sdsuffix
, ".s%d", sdindex
); /* form the suffix */
1275 strcat(sd
->name
, sdsuffix
); /* and add it to the name */
1277 /* do we have complete info for this subdisk? */
1278 if (sd
->sectors
< 0)
1279 throw_rude_remark(EINVAL
, "sd %s has no length spec", sd
->name
);
1281 if (state
!= sd_unallocated
) { /* we had a specific state to set */
1282 sd
->state
= state
; /* do it now */
1284 } else if (sd
->state
== sd_unallocated
) { /* no, nothing set yet, */
1285 sd
->state
= sd_empty
; /* must be empty */
1288 if (autosize
== 0) /* no autoconfig, do the drive now */
1289 give_sd_to_drive(sdno
);
1290 vinum_conf
.subdisks_used
++; /* one more in use */
1294 * Handle a plex definition.
1297 config_plex(int update
)
1300 int plexno
; /* index of plex in vinum_conf */
1301 struct plex
*plex
; /* and pointer to it */
1302 int pindex
= MAXPLEX
; /* index in volume's plex list */
1303 int detached
= 0; /* don't give it to a volume */
1305 enum plexstate state
= plex_init
; /* state to set at end */
1307 current_plex
= -1; /* forget the previous plex */
1308 plexno
= get_empty_plex(); /* allocate a plex */
1309 plex
= &PLEX
[plexno
]; /* and point to it */
1310 plex
->plexno
= plexno
; /* and back to the config */
1312 for (parameter
= 1; parameter
< tokens
; parameter
++) { /* look at the other tokens */
1313 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1315 * If we have a 'name' parameter, it must
1316 * come first, because we're too lazy to tidy
1317 * up dangling refs if it comes later.
1320 namedplexno
= find_plex(token
[++parameter
], 0); /* find an existing plex with this name */
1321 if (namedplexno
>= 0) { /* plex exists already, */
1322 if (PLEX
[namedplexno
].state
== plex_referenced
) { /* we've been told about this one */
1323 if (parameter
> 2) /* we've done other things first, */
1324 throw_rude_remark(EINVAL
,
1325 "plex %s: name parameter must come first\n", /* no go */
1329 struct volume
*vol
; /* for tidying up dangling references */
1331 *plex
= PLEX
[namedplexno
]; /* get the info */
1332 plex
->plex_dev
= NULL
;
1334 PLEX
[namedplexno
].state
= plex_unallocated
; /* and deallocate the other one */
1335 made_plex(&PLEX
[namedplexno
]);
1336 vol
= &VOL
[plex
->volno
]; /* point to the volume */
1337 for (i
= 0; i
< MAXPLEX
; i
++) { /* for each plex */
1338 if (vol
->plex
[i
] == namedplexno
)
1339 vol
->plex
[i
] = plexno
; /* bend the pointer */
1342 break; /* use this one */
1344 if (update
) /* are we updating? */
1345 return; /* yes: that's OK, just return */
1347 throw_rude_remark(EINVAL
, "Duplicate plex %s", token
[parameter
]);
1349 ksnprintf(plex
->name
, sizeof(plex
->name
),
1350 "%s", token
[parameter
]);
1358 case kw_org
: /* plex organization */
1359 switch (get_keyword(token
[++parameter
], &keyword_set
)) {
1361 plex
->organization
= plex_concat
;
1366 int stripesize
= sizespec(token
[++parameter
]);
1368 plex
->organization
= plex_striped
;
1369 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1370 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1374 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1380 int stripesize
= sizespec(token
[++parameter
]);
1382 plex
->organization
= plex_raid4
;
1383 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1384 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1388 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1394 int stripesize
= sizespec(token
[++parameter
]);
1396 plex
->organization
= plex_raid5
;
1397 if (stripesize
% DEV_BSIZE
!= 0) /* not a multiple of block size, */
1398 throw_rude_remark(EINVAL
, "plex %s: stripe size %d not a multiple of sector size",
1402 plex
->stripesize
= stripesize
/ DEV_BSIZE
;
1407 throw_rude_remark(EINVAL
, "Invalid plex organization");
1410 && (plex
->stripesize
== 0)) /* didn't specify a valid stripe size */
1411 throw_rude_remark(EINVAL
, "Need a stripe size parameter");
1415 plex
->volno
= find_volume(token
[++parameter
], 1); /* insert a pointer to the volume */
1418 case kw_sd
: /* add a subdisk */
1422 sdno
= find_subdisk(token
[++parameter
], 1); /* find a subdisk */
1423 SD
[sdno
].plexoffset
= sizespec(token
[++parameter
]); /* get the offset */
1424 give_sd_to_plex(plexno
, sdno
); /* and insert it there */
1429 parameter
++; /* skip the keyword */
1430 if (vinum_conf
.flags
& VF_READING_CONFIG
)
1431 state
= PlexState(token
[parameter
]); /* set the state */
1435 throw_rude_remark(EINVAL
, "plex %s, invalid keyword: %s",
1441 if (plex
->organization
== plex_disorg
)
1442 throw_rude_remark(EINVAL
, "No plex organization specified");
1444 if ((plex
->volno
< 0) /* we don't have a volume */
1445 &&(!detached
)) /* and we wouldn't object */
1446 plex
->volno
= current_volume
;
1448 if (plex
->volno
>= 0)
1449 pindex
= give_plex_to_volume(plex
->volno
, plexno
); /* Now tell the volume that it has this plex */
1451 /* Does the plex have a name? If not, give it one */
1452 if (plex
->name
[0] == '\0') { /* no name */
1453 char plexsuffix
[8]; /* form plex name suffix here */
1454 /* Do we have a volume name? */
1455 if (plex
->volno
>= 0) /* we have a volume */
1456 strcpy(plex
->name
, /* take it from there */
1457 VOL
[plex
->volno
].name
);
1459 throw_rude_remark(EINVAL
, "Unnamed plex is not associated with a volume");
1460 ksprintf(plexsuffix
, ".p%d", pindex
); /* form the suffix */
1461 strcat(plex
->name
, plexsuffix
); /* and add it to the name */
1463 if (isstriped(plex
)) {
1464 plex
->lock
= (struct rangelock
*)
1465 Malloc(PLEX_LOCKS
* sizeof(struct rangelock
));
1466 CHECKALLOC(plex
->lock
, "vinum: Can't allocate lock table\n");
1467 bzero((char *) plex
->lock
, PLEX_LOCKS
* sizeof(struct rangelock
));
1469 /* Note the last plex we configured */
1470 current_plex
= plexno
;
1471 plex
->state
= state
; /* set whatever state we chose */
1473 vinum_conf
.plexes_used
++; /* one more in use */
1477 * Handle a volume definition.
1478 * If we find an error, print a message, deallocate the nascent volume, and return
1481 config_volume(int update
)
1485 struct volume
*vol
; /* collect volume info here */
1488 if (tokens
< 2) /* not enough tokens */
1489 throw_rude_remark(EINVAL
, "Volume has no name");
1490 current_volume
= -1; /* forget the previous volume */
1491 volno
= find_volume(token
[1], 1); /* allocate a volume to initialize */
1492 vol
= &VOL
[volno
]; /* and get a pointer */
1493 if (update
&& ((vol
->flags
& VF_CREATED
) == 0)) /* this volume exists already */
1494 return; /* don't do anything */
1495 vol
->flags
&= ~VF_CREATED
; /* it exists now */
1497 for (parameter
= 2; parameter
< tokens
; parameter
++) { /* look at all tokens */
1498 switch (get_keyword(token
[parameter
], &keyword_set
)) {
1501 int plexno
; /* index of this plex */
1502 int myplexno
; /* and index if it's already ours */
1504 plexno
= find_plex(token
[++parameter
], 1); /* find a plex */
1505 if (plexno
< 0) /* couldn't */
1506 break; /* we've already had an error message */
1507 myplexno
= my_plex(volno
, plexno
); /* does it already belong to us? */
1508 if (myplexno
> 0) /* yes, shouldn't get it again */
1509 throw_rude_remark(EINVAL
,
1510 "Plex %s already belongs to volume %s",
1513 else if (vol
->plexes
+ 1 > 8) /* another entry */
1514 throw_rude_remark(EINVAL
,
1515 "Too many plexes for volume %s",
1517 vol
->plex
[vol
->plexes
] = plexno
;
1519 PLEX
[plexno
].state
= plex_referenced
; /* we know something about it */
1520 PLEX
[plexno
].volno
= volno
; /* and this volume references it */
1525 switch (get_keyword(token
[++parameter
], &keyword_set
)) { /* decide what to do */
1527 vol
->preferred_plex
= ROUND_ROBIN_READPOL
; /* default */
1532 int myplexno
; /* index of this plex */
1534 myplexno
= find_plex(token
[++parameter
], 1); /* find a plex */
1535 if (myplexno
< 0) /* couldn't */
1536 break; /* we've already had an error message */
1537 myplexno
= my_plex(volno
, myplexno
); /* does it already belong to us? */
1538 if (myplexno
> 0) /* yes */
1539 vol
->preferred_plex
= myplexno
; /* just note the index */
1540 else if (++vol
->plexes
> 8) /* another entry */
1541 throw_rude_remark(EINVAL
, "Too many plexes");
1542 else { /* space for the new plex */
1543 vol
->plex
[vol
->plexes
- 1] = myplexno
; /* add it to our list */
1544 vol
->preferred_plex
= vol
->plexes
- 1; /* and note the index */
1550 throw_rude_remark(EINVAL
, "Invalid read policy");
1554 vol
->flags
|= VF_CONFIG_SETUPSTATE
; /* set the volume up later on */
1558 parameter
++; /* skip the keyword */
1559 if (vinum_conf
.flags
& VF_READING_CONFIG
) {
1560 vol
->state
= VolState(token
[parameter
]); /* set the state */
1566 * XXX experimental ideas. These are not
1567 * documented, and will not be until I
1568 * decide they're worth keeping
1570 case kw_writethrough
: /* set writethrough mode */
1571 vol
->flags
|= VF_WRITETHROUGH
;
1574 case kw_writeback
: /* set writeback mode */
1575 vol
->flags
&= ~VF_WRITETHROUGH
;
1579 vol
->flags
|= VF_RAW
; /* raw volume (no label) */
1583 throw_rude_remark(EINVAL
, "volume %s, invalid keyword: %s",
1588 current_volume
= volno
; /* note last referred volume */
1589 vol
->volno
= volno
; /* also note in volume */
1592 * Before we can actually use the volume, we need
1593 * a volume label. We could start to fake one here,
1594 * but it will be a lot easier when we have some
1595 * to copy from the drives, so defer it until we
1596 * set up the configuration. XXX
1598 if (vol
->state
== volume_unallocated
) {
1599 vol
->state
= volume_down
; /* now ready to bring up at the end */
1603 /* Find out how big our volume is */
1604 for (i
= 0; i
< vol
->plexes
; i
++)
1605 vol
->size
= u64max(vol
->size
, PLEX
[vol
->plex
[i
]].length
);
1606 vinum_conf
.volumes_used
++; /* one more in use */
1610 * Parse a config entry. CARE! This destroys the original contents of the
1611 * config entry, which we don't really need after this. More specifically, it
1612 * places \0 characters at the end of each token.
1614 * Return 0 if all is well, otherwise EINVAL for invalid keyword,
1615 * or ENOENT if 'read' command doesn't find any drives.
1618 parse_config(char *cptr
, struct keywordset
*keyset
, int update
)
1622 status
= 0; /* until proven otherwise */
1623 tokens
= tokenize(cptr
, token
); /* chop up into tokens */
1625 if (tokens
<= 0) /* screwed up or empty line */
1626 return tokens
; /* give up */
1628 if (token
[0][0] == '#') /* comment line */
1631 switch (get_keyword(token
[0], keyset
)) { /* decide what to do */
1632 case kw_read
: /* read config from a specified drive */
1633 status
= vinum_scandisk(&token
[1], tokens
- 1); /* read the config from disk */
1637 config_drive(update
);
1641 config_subdisk(update
);
1645 config_plex(update
);
1649 config_volume(update
);
1652 /* Anything else is invalid in this context */
1654 throw_rude_remark(EINVAL
, /* should we die? */
1655 "Invalid configuration information: %s",
1662 * parse a line handed in from userland via ioctl.
1663 * This differs only by the error reporting mechanism:
1664 * we return the error indication in the reply to the
1665 * ioctl, so we need to set a global static pointer in
1666 * this file. This technique works because we have
1667 * ensured that configuration is performed in a single-
1671 parse_user_config(char *cptr
, struct keywordset
*keyset
)
1675 ioctl_reply
= (struct _ioctl_reply
*) cptr
;
1676 status
= parse_config(cptr
, keyset
, 0);
1677 if (status
== ENOENT
) /* from scandisk, but it can't tell us */
1678 strcpy(ioctl_reply
->msg
, "no drives found");
1679 ioctl_reply
= NULL
; /* don't do this again */
1683 /* Remove an object */
1685 remove(struct vinum_ioctl_msg
*msg
)
1687 struct vinum_ioctl_msg message
= *msg
; /* make a copy to hand on */
1689 ioctl_reply
= (struct _ioctl_reply
*) msg
; /* reinstate the address to reply to */
1690 ioctl_reply
->error
= 0; /* no error, */
1691 ioctl_reply
->msg
[0] = '\0'; /* no message */
1693 switch (message
.type
) {
1695 remove_drive_entry(message
.index
, message
.force
);
1700 remove_sd_entry(message
.index
, message
.force
, message
.recurse
);
1705 remove_plex_entry(message
.index
, message
.force
, message
.recurse
);
1710 remove_volume_entry(message
.index
, message
.force
, message
.recurse
);
1715 ioctl_reply
->error
= EINVAL
;
1716 strcpy(ioctl_reply
->msg
, "Invalid object type");
1720 /* Remove a drive. */
1722 remove_drive_entry(int driveno
, int force
)
1724 struct drive
*drive
= &DRIVE
[driveno
];
1727 if ((driveno
> vinum_conf
.drives_allocated
) /* not a valid drive */
1728 ||(drive
->state
== drive_unallocated
)) { /* or nothing there */
1729 ioctl_reply
->error
= EINVAL
;
1730 strcpy(ioctl_reply
->msg
, "No such drive");
1731 } else if (drive
->opencount
> 0) { /* we have subdisks */
1732 if (force
) { /* do it at any cost */
1733 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) {
1734 if ((SD
[sdno
].state
!= sd_unallocated
) /* subdisk is allocated */
1735 &&(SD
[sdno
].driveno
== driveno
)) /* and it belongs to this drive */
1736 remove_sd_entry(sdno
, force
, 0);
1738 remove_drive(driveno
); /* now remove it */
1739 vinum_conf
.drives_used
--; /* one less drive */
1741 ioctl_reply
->error
= EBUSY
; /* can't do that */
1743 remove_drive(driveno
); /* just remove it */
1744 vinum_conf
.drives_used
--; /* one less drive */
1748 /* remove a subdisk */
1750 remove_sd_entry(int sdno
, int force
, int recurse
)
1752 struct sd
*sd
= &SD
[sdno
];
1754 if ((sdno
> vinum_conf
.subdisks_allocated
) /* not a valid sd */
1755 ||(sd
->state
== sd_unallocated
)) { /* or nothing there */
1756 ioctl_reply
->error
= EINVAL
;
1757 strcpy(ioctl_reply
->msg
, "No such subdisk");
1758 } else if (sd
->flags
& VF_OPEN
) { /* we're open */
1759 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1761 } else if (sd
->plexno
>= 0) { /* we have a plex */
1762 if (force
) { /* do it at any cost */
1763 struct plex
*plex
= &PLEX
[sd
->plexno
]; /* point to our plex */
1766 for (mysdno
= 0; /* look for ourselves */
1767 mysdno
< plex
->subdisks
&& &SD
[plex
->sdnos
[mysdno
]] != sd
;
1769 if (mysdno
== plex
->subdisks
) /* didn't find it */
1771 "Error removing subdisk %s: not found in plex %s\n",
1774 else { /* remove the subdisk from plex */
1775 if (mysdno
< (plex
->subdisks
- 1)) /* not the last subdisk */
1776 bcopy(&plex
->sdnos
[mysdno
+ 1],
1777 &plex
->sdnos
[mysdno
],
1778 (plex
->subdisks
- 1 - mysdno
) * sizeof(int));
1780 sd
->plexno
= -1; /* disown the subdisk */
1784 * Removing a subdisk from a striped or
1785 * RAID-4 or RAID-5 plex really tears the
1786 * hell out of the structure, and it needs
1787 * to be reinitialized.
1789 if (plex
->organization
!= plex_concat
) /* not concatenated, */
1790 set_plex_state(plex
->plexno
, plex_faulty
, setstate_force
); /* need to reinitialize */
1791 log(LOG_INFO
, "vinum: removing %s\n", sd
->name
);
1794 ioctl_reply
->error
= EBUSY
; /* can't do that */
1796 log(LOG_INFO
, "vinum: removing %s\n", sd
->name
);
1803 remove_plex_entry(int plexno
, int force
, int recurse
)
1805 struct plex
*plex
= &PLEX
[plexno
];
1808 if ((plexno
> vinum_conf
.plexes_allocated
) /* not a valid plex */
1809 ||(plex
->state
== plex_unallocated
)) { /* or nothing there */
1810 ioctl_reply
->error
= EINVAL
;
1811 strcpy(ioctl_reply
->msg
, "No such plex");
1812 } else if (plex
->flags
& VF_OPEN
) { /* we're open */
1813 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1816 if (plex
->subdisks
) {
1817 if (force
) { /* do it anyway */
1818 if (recurse
) { /* remove all below */
1819 int sds
= plex
->subdisks
;
1820 for (sdno
= 0; sdno
< sds
; sdno
++)
1821 free_sd(plex
->sdnos
[sdno
]); /* free all subdisks */
1822 } else { /* just tear them out */
1823 int sds
= plex
->subdisks
;
1824 for (sdno
= 0; sdno
< sds
; sdno
++)
1825 SD
[plex
->sdnos
[sdno
]].plexno
= -1; /* no plex any more */
1827 } else { /* can't do it without force */
1828 ioctl_reply
->error
= EBUSY
; /* can't do that */
1832 if (plex
->volno
>= 0) { /* we are part of a volume */
1833 if (force
) { /* do it at any cost */
1834 struct volume
*vol
= &VOL
[plex
->volno
];
1837 for (myplexno
= 0; myplexno
< vol
->plexes
; myplexno
++)
1838 if (vol
->plex
[myplexno
] == plexno
) /* found it */
1840 if (myplexno
== vol
->plexes
) /* didn't find it. Huh? */
1842 "Error removing plex %s: not found in volume %s\n",
1845 if (myplexno
< (vol
->plexes
- 1)) /* not the last plex in the list */
1846 bcopy(&vol
->plex
[myplexno
+ 1],
1847 &vol
->plex
[myplexno
],
1848 vol
->plexes
- 1 - myplexno
);
1851 ioctl_reply
->error
= EBUSY
; /* can't do that */
1855 log(LOG_INFO
, "vinum: removing %s\n", plex
->name
);
1857 vinum_conf
.plexes_used
--; /* one less plex */
1860 /* remove a volume */
1862 remove_volume_entry(int volno
, int force
, int recurse
)
1864 struct volume
*vol
= &VOL
[volno
];
1867 if ((volno
> vinum_conf
.volumes_allocated
) /* not a valid volume */
1868 ||(vol
->state
== volume_unallocated
)) { /* or nothing there */
1869 ioctl_reply
->error
= EINVAL
;
1870 strcpy(ioctl_reply
->msg
, "No such volume");
1871 } else if (vol
->flags
& VF_OPEN
) /* we're open */
1872 ioctl_reply
->error
= EBUSY
; /* no getting around that */
1873 else if (vol
->plexes
) {
1874 if (recurse
&& force
) { /* remove all below */
1875 int plexes
= vol
->plexes
;
1877 /* for (plexno = plexes - 1; plexno >= 0; plexno--) */
1878 for (plexno
= 0; plexno
< plexes
; plexno
++)
1879 remove_plex_entry(vol
->plex
[plexno
], force
, recurse
);
1880 log(LOG_INFO
, "vinum: removing %s\n", vol
->name
);
1882 vinum_conf
.volumes_used
--; /* one less volume */
1884 ioctl_reply
->error
= EBUSY
; /* can't do that */
1886 log(LOG_INFO
, "vinum: removing %s\n", vol
->name
);
1888 vinum_conf
.volumes_used
--; /* one less volume */
1892 /* Currently called only from ioctl */
1894 update_sd_config(int sdno
, int diskconfig
)
1897 set_sd_state(sdno
, sd_up
, setstate_configuring
);
1898 SD
[sdno
].flags
&= ~VF_NEWBORN
;
1902 update_plex_config(int plexno
, int diskconfig
)
1906 struct plex
*plex
= &PLEX
[plexno
];
1907 enum plexstate state
= plex_up
; /* state we want the plex in */
1908 int remainder
; /* size of fractional stripe at end */
1909 int added_plex
; /* set if we add a plex to a volume */
1910 int required_sds
; /* number of subdisks we need */
1913 int data_sds
= 0; /* number of sds carrying data */
1915 if (plex
->state
< plex_init
) /* not a real plex, */
1918 if (plex
->volno
>= 0) { /* we have a volume */
1919 vol
= &VOL
[plex
->volno
];
1922 * If we're newly born,
1923 * and the volume isn't,
1924 * and it has other plexes,
1925 * and we didn't read this mess from disk,
1926 * we were added later.
1928 if ((plex
->flags
& VF_NEWBORN
)
1929 && ((vol
->flags
& VF_NEWBORN
) == 0)
1930 && (vol
->plexes
> 0)
1931 && (diskconfig
== 0)) {
1933 state
= plex_down
; /* so take ourselves down */
1937 * Check that our subdisks make sense. For
1938 * striped, RAID-4 and RAID-5 plexes, we need at
1939 * least two subdisks, and they must all be the
1942 if (plex
->organization
== plex_striped
) {
1943 data_sds
= plex
->subdisks
;
1945 } else if (isparity(plex
)) { /* RAID 4 or 5 */
1946 data_sds
= plex
->subdisks
- 1;
1950 if (required_sds
> 0) { /* striped, RAID-4 or RAID-5 */
1951 if (plex
->subdisks
< required_sds
) {
1953 "vinum: plex %s does not have at least %d subdisks\n",
1956 state
= plex_faulty
;
1959 * Now see if the plex size is a multiple of
1960 * the stripe size. If not, trim off the end
1961 * of each subdisk and return it to the drive.
1963 if (plex
->length
> 0) {
1965 if (plex
->stripesize
> 0) {
1966 remainder
= (int) (plex
->length
/* are we exact? */
1967 % ((u_int64_t
) plex
->stripesize
* data_sds
));
1968 if (remainder
) { /* no */
1969 log(LOG_INFO
, "vinum: removing %d blocks of partial stripe at the end of %s\n",
1972 plex
->length
-= remainder
; /* shorten the plex */
1973 remainder
/= data_sds
; /* spread the remainder amongst the sds */
1974 for (sdno
= 0; sdno
< plex
->subdisks
; sdno
++) {
1975 sd
= &SD
[plex
->sdnos
[sdno
]]; /* point to the subdisk */
1976 return_drive_space(sd
->driveno
, /* return the space */
1977 sd
->driveoffset
+ sd
->sectors
- remainder
,
1979 sd
->sectors
-= remainder
; /* and shorten it */
1982 } else /* no data sds, */
1983 plex
->length
= 0; /* reset length */
1988 for (sdno
= 0; sdno
< plex
->subdisks
; sdno
++) {
1989 sd
= &SD
[plex
->sdnos
[sdno
]];
1992 && (sd
->sectors
!= SD
[plex
->sdnos
[sdno
- 1]].sectors
)) {
1993 log(LOG_ERR
, "vinum: %s must have equal sized subdisks\n", plex
->name
);
1996 size
+= sd
->sectors
;
1997 if (added_plex
) { /* we were added later */
1998 sd
->state
= sd_stale
; /* stale until proven otherwise */
2003 if (plex
->subdisks
) { /* plex has subdisks, calculate size */
2005 * XXX We shouldn't need to calculate the size any
2006 * more. Check this some time
2009 size
= size
/ plex
->subdisks
* (plex
->subdisks
- 1); /* less space for RAID-4 and RAID-5 */
2010 if (plex
->length
!= size
)
2012 "Correcting length of %s: was %lld, is %lld\n",
2014 (long long) plex
->length
,
2016 plex
->length
= size
;
2017 } else { /* no subdisks, */
2018 plex
->length
= 0; /* no size */
2019 state
= plex_down
; /* take it down */
2021 update_plex_state(plexno
); /* set the state */
2022 plex
->flags
&= ~VF_NEWBORN
;
2026 update_volume_config(int volno
, int diskconfig
)
2028 struct volume
*vol
= &VOL
[volno
];
2032 if (vol
->state
!= volume_unallocated
)
2034 * Recalculate the size of the volume,
2035 * which might change if the original
2036 * plexes were not a multiple of the
2041 for (plexno
= 0; plexno
< vol
->plexes
; plexno
++) {
2042 plex
= &PLEX
[vol
->plex
[plexno
]];
2043 vol
->size
= u64max(plex
->length
, vol
->size
);
2044 plex
->volplexno
= plexno
; /* note it in the plex */
2047 vol
->flags
&= ~VF_NEWBORN
; /* no longer newly born */
2051 * Update the global configuration.
2052 * diskconfig is != 0 if we're reading in a config
2053 * from disk. In this case, we don't try to
2054 * bring the devices up, though we will bring
2055 * them down if there's some error which got
2056 * missed when writing to disk.
2059 updateconfig(int diskconfig
)
2064 for (plexno
= 0; plexno
< vinum_conf
.plexes_allocated
; plexno
++)
2065 update_plex_config(plexno
, diskconfig
);
2067 for (volno
= 0; volno
< vinum_conf
.volumes_allocated
; volno
++) {
2068 if (VOL
[volno
].state
> volume_uninit
) {
2069 VOL
[volno
].flags
&= ~VF_CONFIG_SETUPSTATE
; /* no more setupstate */
2070 update_volume_state(volno
);
2071 update_volume_config(volno
, diskconfig
);
2078 * Start manual changes to the configuration and lock out
2079 * others who may wish to do so.
2080 * XXX why do we need this and lock_config too?
2083 start_config(int force
)
2087 current_drive
= -1; /* note the last drive we mention, for
2089 current_plex
= -1; /* and the same for the last plex */
2090 current_volume
= -1; /* and the last volume */
2091 while ((vinum_conf
.flags
& VF_CONFIGURING
) != 0) {
2092 vinum_conf
.flags
|= VF_WILL_CONFIGURE
;
2093 if ((error
= tsleep(&vinum_conf
, PCATCH
, "vincfg", 0)) != 0)
2097 * We need two flags here: VF_CONFIGURING
2098 * tells other processes to hold off (this
2099 * function), and VF_CONFIG_INCOMPLETE
2100 * tells the state change routines not to
2101 * propagate incrememntal state changes
2103 vinum_conf
.flags
|= VF_CONFIGURING
| VF_CONFIG_INCOMPLETE
;
2105 vinum_conf
.flags
|= VF_FORCECONFIG
; /* overwrite differently named drives */
2106 current_drive
= -1; /* reset the defaults */
2107 current_plex
= -1; /* and the same for the last plex */
2108 current_volume
= -1; /* and the last volme */
2113 * Update the config if update is 1, and unlock
2114 * it. We won't update the configuration if we
2115 * are called in a recursive loop via throw_rude_remark.
2118 finish_config(int update
)
2120 /* we've finished our config */
2121 vinum_conf
.flags
&= ~(VF_CONFIG_INCOMPLETE
| VF_READING_CONFIG
| VF_FORCECONFIG
);
2123 updateconfig(0); /* so update things */
2125 updateconfig(1); /* do some updates only */
2126 vinum_conf
.flags
&= ~VF_CONFIGURING
; /* and now other people can take a turn */
2127 if ((vinum_conf
.flags
& VF_WILL_CONFIGURE
) != 0) {
2128 vinum_conf
.flags
&= ~VF_WILL_CONFIGURE
;
2129 wakeup_one(&vinum_conf
);
2132 /* Local Variables: */
2133 /* fill-column: 50 */