* Fix some cases where NULL was used but 0 was meant (and vice versa).
[dragonfly.git] / sys / dev / raid / vinum / vinumio.c
blob3702fae139bf037962ef5f30f5c27d9dd08092ef
1 /*-
2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * This software is distributed under the so-called ``Berkeley
6 * License'':
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Nan Yang Computer
19 * Services Limited.
20 * 4. Neither the name of the Company nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * This software is provided ``as is'', and any express or implied
25 * warranties, including, but not limited to, the implied warranties of
26 * merchantability and fitness for a particular purpose are disclaimed.
27 * In no event shall the company or contributors be liable for any
28 * direct, indirect, incidental, special, exemplary, or consequential
29 * damages (including, but not limited to, procurement of substitute
30 * goods or services; loss of use, data, or profits; or business
31 * interruption) however caused and on any theory of liability, whether
32 * in contract, strict liability, or tort (including negligence or
33 * otherwise) arising in any way out of the use of this software, even if
34 * advised of the possibility of such damage.
36 * $Id: vinumio.c,v 1.30 2000/05/10 23:23:30 grog Exp grog $
37 * $FreeBSD: src/sys/dev/vinum/vinumio.c,v 1.52.2.6 2002/05/02 08:43:44 grog Exp $
38 * $DragonFly: src/sys/dev/raid/vinum/vinumio.c,v 1.31 2008/06/05 18:06:31 swildner Exp $
41 #include "vinumhdr.h"
42 #include "request.h"
43 #include <vm/vm_zone.h>
44 #include <sys/nlookup.h>
46 static char *sappend(char *txt, char *s);
47 static int drivecmp(const void *va, const void *vb);
50 * Open the device associated with the drive, and set drive's vp.
51 * Return an error number
53 int
54 open_drive(struct drive *drive, struct proc *p, int verbose)
56 struct nlookupdata nd;
57 int error;
58 const char *dname;
61 * Fail if already open
63 if (drive->flags & VF_OPEN)
64 return EBUSY;
65 dname = drive->devicename;
68 * Severe hack to disallow opening partition 'c'
70 if (strncmp(dname, "/dev", 4) == 0 && dname[strlen(dname)-1] == 'c')
71 return ENOTTY;
73 if (rootdev) {
75 * Open via filesystem (future)
77 error = nlookup_init(&nd, drive->devicename, UIO_SYSSPACE, NLC_FOLLOW);
78 if (error)
79 return error;
80 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
81 drive->vp = nd.nl_open_vp;
82 nd.nl_open_vp = NULL;
83 nlookup_done(&nd);
84 } else {
86 * Open via synthesized vnode backed by disk device
88 error = vn_opendisk(drive->devicename, FREAD|FWRITE, &drive->vp);
89 if (error)
90 return error;
93 if (error == 0 && drive->vp == NULL)
94 error = ENODEV;
97 * A huge amount of pollution all over vinum requires that our low
98 * level drive be a device.
100 if (error == 0 && drive->vp->v_type != VCHR) {
101 vn_close(drive->vp, FREAD|FWRITE);
102 drive->vp = NULL;
103 error = ENODEV;
105 if (error) {
106 drive->state = drive_down;
107 if (verbose) {
108 log(LOG_WARNING,
109 "vinum open_drive %s: failed with error %d\n",
110 drive->devicename, error);
112 } else {
113 drive->dev = drive->vp->v_rdev;
114 drive->flags |= VF_OPEN;
116 drive->lasterror = error;
117 return error;
121 * Set some variables in the drive struct
122 * in more convenient form. Return error indication
125 set_drive_parms(struct drive *drive)
127 drive->blocksize = BLKDEV_IOSIZE; /* do we need this? */
128 drive->secsperblock = drive->blocksize /* number of sectors per block */
129 / drive->partinfo.media_blksize;
131 /* Now update the label part */
132 bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN); /* put in host name */
133 getmicrotime(&drive->label.date_of_birth); /* and current time */
134 drive->label.drive_size = drive->partinfo.media_size;
135 #if VINUMDEBUG
136 if (debug & DEBUG_BIGDRIVE) /* pretend we're 100 times as big */
137 drive->label.drive_size *= 100;
138 #endif
140 /* number of sectors available for subdisks */
141 drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
144 * Bug in 3.0 as of January 1998: you can open
145 * non-existent slices. They have a length of 0.
147 if (drive->label.drive_size < MINVINUMSLICE) { /* too small to worry about */
148 set_drive_state(drive->driveno, drive_down, setstate_force);
149 drive->lasterror = ENOSPC;
150 return ENOSPC;
152 drive->freelist_size = INITIAL_DRIVE_FREELIST; /* initial number of entries */
153 drive->freelist = (struct drive_freelist *)
154 Malloc(INITIAL_DRIVE_FREELIST * sizeof(struct drive_freelist));
155 if (drive->freelist == NULL) /* can't malloc, dammit */
156 return ENOSPC;
157 drive->freelist_entries = 1; /* just (almost) the complete drive */
158 drive->freelist[0].offset = DATASTART; /* starts here */
159 drive->freelist[0].sectors = (drive->label.drive_size >> DEV_BSHIFT) - DATASTART; /* and it's this long */
160 if (drive->label.name[0] != '\0') /* got a name */
161 set_drive_state(drive->driveno, drive_up, setstate_force); /* our drive is accessible */
162 else /* we know about it, but that's all */
163 drive->state = drive_referenced;
164 return 0;
168 * Initialize a drive: open the device and add device
169 * information
172 init_drive(struct drive *drive, int verbose)
174 if (drive->devicename[0] != '/') {
175 drive->lasterror = EINVAL;
176 log(LOG_ERR, "vinum: Can't open drive without drive name\n");
177 return EINVAL;
179 drive->lasterror = open_drive(drive, curproc, verbose); /* open the drive */
180 if (drive->lasterror)
181 return drive->lasterror;
183 drive->lasterror = VOP_IOCTL(drive->vp, DIOCGPART,
184 (caddr_t)&drive->partinfo,
185 FREAD|FWRITE, proc0.p_ucred);
186 if (drive->lasterror) {
187 if (verbose)
188 log(LOG_WARNING,
189 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
190 drive->devicename,
191 drive->lasterror);
192 close_drive(drive);
193 return drive->lasterror;
195 if (drive->partinfo.fstype != FS_VINUM &&
196 !kuuid_is_vinum(&drive->partinfo.fstype_uuid)
197 ) {
198 drive->lasterror = EFTYPE;
199 if (verbose)
200 log(LOG_WARNING,
201 "vinum open_drive %s: Wrong partition type for vinum\n",
202 drive->devicename);
203 close_drive(drive);
204 return EFTYPE;
206 return set_drive_parms(drive); /* set various odds and ends */
209 /* Close a drive if it's open. */
210 void
211 close_drive(struct drive *drive)
213 LOCKDRIVE(drive); /* keep the daemon out */
214 if (drive->flags & VF_OPEN)
215 close_locked_drive(drive); /* and close it */
216 if (drive->state > drive_down) /* if it's up */
217 drive->state = drive_down; /* make sure it's down */
218 unlockdrive(drive);
222 * Real drive close code, called with drive already locked.
223 * We have also checked that the drive is open. No errors.
225 void
226 close_locked_drive(struct drive *drive)
229 * If we can't access the drive, we can't flush
230 * the queues, which spec_close() will try to
231 * do. Get rid of them here first.
233 if (drive->vp) {
234 drive->lasterror = vn_close(drive->vp, FREAD|FWRITE);
235 drive->vp = NULL;
237 drive->flags &= ~VF_OPEN;
241 * Remove drive from the configuration.
242 * Caller must ensure that it isn't active.
244 void
245 remove_drive(int driveno)
247 struct drive *drive = &vinum_conf.drive[driveno];
248 struct vinum_hdr *vhdr; /* buffer for header */
249 int error;
251 if (drive->state > drive_referenced) { /* real drive */
252 if (drive->state == drive_up) {
253 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffer */
254 CHECKALLOC(vhdr, "Can't allocate memory");
255 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
256 if (error)
257 drive->lasterror = error;
258 else {
259 vhdr->magic = VINUM_NOMAGIC; /* obliterate the magic, but leave the rest */
260 write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
262 Free(vhdr);
264 free_drive(drive); /* close it and free resources */
265 save_config(); /* and save the updated configuration */
270 * Transfer drive data. Usually called from one of these defines;
271 * #define read_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_READ)
272 * #define write_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_WRITE)
274 * length and offset are in bytes, but must be multiples of sector
275 * size. The function *does not check* for this condition, and
276 * truncates ruthlessly.
277 * Return error number
280 driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
282 int error;
283 struct buf *bp;
284 caddr_t saveaddr;
286 error = 0; /* to keep the compiler happy */
287 while (length) { /* divide into small enough blocks */
288 int len = min(length, MAXBSIZE); /* maximum block device transfer is MAXBSIZE */
290 bp = geteblk(len); /* get a buffer header */
291 bp->b_cmd = cmd;
292 bp->b_bio1.bio_offset = offset; /* disk offset */
293 saveaddr = bp->b_data;
294 bp->b_data = buf;
295 bp->b_bcount = len;
296 vn_strategy(drive->vp, &bp->b_bio1);
297 error = biowait(bp);
298 bp->b_data = saveaddr;
299 bp->b_flags |= B_INVAL | B_AGE;
300 bp->b_flags &= ~B_ERROR;
301 brelse(bp);
302 if (error)
303 break;
304 length -= len; /* update pointers */
305 buf += len;
306 offset += len;
308 return error;
312 * Check a drive for a vinum header. If found,
313 * update the drive information. We come here
314 * with a partially populated drive structure
315 * which includes the device name.
317 * Return information on what we found.
319 * This function is called from two places: check_drive,
320 * which wants to find out whether the drive is a
321 * Vinum drive, and config_drive, which asserts that
322 * it is a vinum drive. In the first case, we don't
323 * print error messages (verbose==0), in the second
324 * we do (verbose==1).
326 enum drive_label_info
327 read_drive_label(struct drive *drive, int verbose)
329 int error;
330 int result; /* result of our search */
331 struct vinum_hdr *vhdr; /* and as header */
333 error = init_drive(drive, 0); /* find the drive */
334 if (error) /* find the drive */
335 return DL_CANT_OPEN; /* not ours */
337 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffers */
338 CHECKALLOC(vhdr, "Can't allocate memory");
340 drive->state = drive_up; /* be optimistic */
341 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
342 if (vhdr->magic == VINUM_MAGIC) { /* ours! */
343 if (drive->label.name[0] /* we have a name for this drive */
344 &&(strcmp(drive->label.name, vhdr->label.name))) { /* but it doesn't match the real name */
345 drive->lasterror = EINVAL;
346 result = DL_WRONG_DRIVE; /* it's the wrong drive */
347 drive->state = drive_unallocated; /* put it back, it's not ours */
348 } else
349 result = DL_OURS;
351 * We copy the drive anyway so that we have
352 * the correct name in the drive info. This
353 * may not be the name specified
355 drive->label = vhdr->label; /* put in the label information */
356 } else if (vhdr->magic == VINUM_NOMAGIC) /* was ours, but we gave it away */
357 result = DL_DELETED_LABEL; /* and return the info */
358 else
359 result = DL_NOT_OURS; /* we could have it, but we don't yet */
360 Free(vhdr); /* that's all. */
361 return result;
365 * Check a drive for a vinum header. If found,
366 * read configuration information from the drive and
367 * incorporate the data into the configuration.
369 * Return drive number.
371 struct drive *
372 check_drive(char *devicename)
374 int driveno;
375 int i;
376 struct drive *drive;
378 driveno = find_drive_by_dev(devicename, 1); /* if entry doesn't exist, create it */
379 drive = &vinum_conf.drive[driveno]; /* and get a pointer */
381 if (read_drive_label(drive, 0) == DL_OURS) { /* one of ours */
382 for (i = 0; i < vinum_conf.drives_allocated; i++) { /* see if the name already exists */
383 if ((i != driveno) /* not this drive */
384 &&(DRIVE[i].state != drive_unallocated) /* and it's allocated */
385 &&(strcmp(DRIVE[i].label.name,
386 DRIVE[driveno].label.name) == 0)) { /* and it has the same name */
387 struct drive *mydrive = &DRIVE[i];
389 if (mydrive->devicename[0] == '/') { /* we know a device name for it */
391 * set an error, but don't take the
392 * drive down: that would cause unneeded
393 * error messages.
395 drive->lasterror = EEXIST;
396 break;
397 } else { /* it's just a place holder, */
398 int sdno;
400 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) { /* look at each subdisk */
401 if ((SD[sdno].driveno == i) /* it's pointing to this one, */
402 &&(SD[sdno].state != sd_unallocated)) { /* and it's a real subdisk */
403 SD[sdno].driveno = drive->driveno; /* point to the one we found */
404 update_sd_state(sdno); /* and update its state */
407 bzero(mydrive, sizeof(struct drive)); /* don't deallocate it, just remove it */
411 } else {
412 if (drive->lasterror == 0)
413 drive->lasterror = ENODEV;
414 close_drive(drive);
415 drive->state = drive_down;
417 return drive;
420 static char *
421 sappend(char *txt, char *s)
423 while ((*s++ = *txt++) != 0);
424 return s - 1;
427 void
428 format_config(char *config, int len)
430 int i;
431 int j;
432 char *s = config;
433 char *configend = &config[len];
435 bzero(config, len);
437 /* First write the volume configuration */
438 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
439 struct volume *vol;
441 vol = &vinum_conf.volume[i];
442 if ((vol->state > volume_uninit)
443 && (vol->name[0] != '\0')) { /* paranoia */
444 ksnprintf(s,
445 configend - s,
446 "volume %s state %s",
447 vol->name,
448 volume_state(vol->state));
449 while (*s)
450 s++; /* find the end */
451 if (vol->preferred_plex >= 0) /* preferences, */
452 ksnprintf(s,
453 configend - s,
454 " readpol prefer %s",
455 vinum_conf.plex[vol->preferred_plex].name);
456 while (*s)
457 s++; /* find the end */
458 s = sappend("\n", s);
462 /* Then the plex configuration */
463 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
464 struct plex *plex;
466 plex = &vinum_conf.plex[i];
467 if ((plex->state > plex_referenced)
468 && (plex->name[0] != '\0')) { /* paranoia */
469 ksnprintf(s,
470 configend - s,
471 "plex name %s state %s org %s ",
472 plex->name,
473 plex_state(plex->state),
474 plex_org(plex->organization));
475 while (*s)
476 s++; /* find the end */
477 if (isstriped(plex)) {
478 ksnprintf(s,
479 configend - s,
480 "%ds ",
481 (int) plex->stripesize);
482 while (*s)
483 s++; /* find the end */
485 if (plex->volno >= 0) /* we have a volume */
486 ksnprintf(s,
487 configend - s,
488 "vol %s ",
489 vinum_conf.volume[plex->volno].name);
490 while (*s)
491 s++; /* find the end */
492 for (j = 0; j < plex->subdisks; j++) {
493 ksnprintf(s,
494 configend - s,
495 " sd %s",
496 vinum_conf.sd[plex->sdnos[j]].name);
498 s = sappend("\n", s);
502 /* And finally the subdisk configuration */
503 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
504 struct sd *sd;
505 char *drivename;
507 sd = &SD[i];
508 if ((sd->state != sd_referenced)
509 && (sd->state != sd_unallocated)
510 && (sd->name[0] != '\0')) { /* paranoia */
511 drivename = vinum_conf.drive[sd->driveno].label.name;
513 * XXX We've seen cases of dead subdisks
514 * which don't have a drive. If we let them
515 * through here, the drive name is null, so
516 * they get the drive named 'plex'.
518 * This is a breakage limiter, not a fix.
520 if (drivename[0] == '\0')
521 drivename = "*invalid*";
522 ksnprintf(s,
523 configend - s,
524 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
525 sd->name,
526 drivename,
527 vinum_conf.plex[sd->plexno].name,
528 (unsigned long long) sd->sectors,
529 (unsigned long long) sd->driveoffset,
530 sd_state(sd->state));
531 while (*s)
532 s++; /* find the end */
533 if (sd->plexno >= 0)
534 ksnprintf(s,
535 configend - s,
536 " plexoffset %llds",
537 (long long) sd->plexoffset);
538 else
539 ksnprintf(s, configend - s, " detached");
540 while (*s)
541 s++; /* find the end */
542 if (sd->flags & VF_RETRYERRORS) {
543 ksnprintf(s, configend - s, " retryerrors");
544 while (*s)
545 s++; /* find the end */
547 ksnprintf(s, configend - s, " \n");
548 while (*s)
549 s++; /* find the end */
552 if (s > &config[len - 2])
553 panic("vinum: configuration data overflow");
557 * issue a save config request to the dæmon. The actual work
558 * is done in process context by daemon_save_config
560 void
561 save_config(void)
563 queue_daemon_request(daemonrq_saveconfig, (union daemoninfo) 0);
567 * Write the configuration to all vinum slices. This
568 * is performed by the dæmon only
570 void
571 daemon_save_config(void)
573 int error;
574 int written_config; /* set when we first write the config to disk */
575 int driveno;
576 struct drive *drive; /* point to current drive info */
577 struct vinum_hdr *vhdr; /* and as header */
578 char *config; /* point to config data */
579 int wlabel_on; /* to set writing label on/off */
581 /* don't save the configuration while we're still working on it */
582 if (vinum_conf.flags & VF_CONFIGURING)
583 return;
584 written_config = 0; /* no config written yet */
585 /* Build a volume header */
586 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* get space for the config data */
587 CHECKALLOC(vhdr, "Can't allocate config data");
588 vhdr->magic = VINUM_MAGIC; /* magic number */
589 vhdr->config_length = MAXCONFIG; /* length of following config info */
591 config = Malloc(MAXCONFIG); /* get space for the config data */
592 CHECKALLOC(config, "Can't allocate config data");
594 format_config(config, MAXCONFIG);
595 error = 0; /* no errors yet */
596 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
597 drive = &vinum_conf.drive[driveno]; /* point to drive */
598 if (drive->state > drive_referenced) {
599 LOCKDRIVE(drive); /* don't let it change */
602 * First, do some drive consistency checks. Some
603 * of these are kludges, others require a process
604 * context and couldn't be done before
606 if ((drive->devicename[0] == '\0')
607 || (drive->label.name[0] == '\0')) {
608 unlockdrive(drive);
609 free_drive(drive); /* get rid of it */
610 break;
612 if (((drive->flags & VF_OPEN) == 0) /* drive not open */
613 &&(drive->state > drive_down)) { /* and it thinks it's not down */
614 unlockdrive(drive);
615 set_drive_state(driveno, drive_down, setstate_force); /* tell it what's what */
616 continue;
618 if ((drive->state == drive_down) /* it's down */
619 &&(drive->flags & VF_OPEN)) { /* but open, */
620 unlockdrive(drive);
621 close_drive(drive); /* close it */
622 } else if (drive->state > drive_down) {
623 getmicrotime(&drive->label.last_update); /* time of last update is now */
624 bcopy((char *) &drive->label, /* and the label info from the drive structure */
625 (char *) &vhdr->label,
626 sizeof(vhdr->label));
627 if ((drive->state != drive_unallocated)
628 && (drive->state != drive_referenced)) { /* and it's a real drive */
629 wlabel_on = 1; /* enable writing the label */
630 error = 0;
631 #if 1
632 error = VOP_IOCTL(drive->vp, DIOCWLABEL,
633 (caddr_t)&wlabel_on,
634 FREAD|FWRITE, proc0.p_ucred);
635 #endif
636 if (error == 0)
637 error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
638 if (error == 0)
639 error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET); /* first config copy */
640 if (error == 0)
641 error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG); /* second copy */
642 wlabel_on = 0; /* enable writing the label */
643 #if 1
644 if (error == 0) {
645 error = VOP_IOCTL(drive->vp, DIOCWLABEL,
646 (caddr_t)&wlabel_on,
647 FREAD|FWRITE, proc0.p_ucred);
649 #endif
650 unlockdrive(drive);
651 if (error) {
652 log(LOG_ERR,
653 "vinum: Can't write config to %s, error %d\n",
654 drive->devicename,
655 error);
656 set_drive_state(drive->driveno, drive_down, setstate_force);
657 } else
658 written_config = 1; /* we've written it on at least one drive */
660 } else /* not worth looking at, */
661 unlockdrive(drive); /* just unlock it again */
664 Free(vhdr);
665 Free(config);
668 /* Look at all disks on the system for vinum slices */
670 vinum_scandisk(char *devicename[], int drives)
672 struct drive *volatile drive;
673 volatile int driveno;
674 int firstdrive; /* first drive in this list */
675 volatile int gooddrives; /* number of usable drives found */
676 int firsttime; /* set if we have never configured before */
677 int error;
678 char *config_text; /* read the config info from disk into here */
679 char *volatile cptr; /* pointer into config information */
680 char *eptr; /* end pointer into config information */
681 char *config_line; /* copy the config line to */
682 volatile int status;
683 int *volatile drivelist; /* list of drive indices */
684 #define DRIVENAMELEN 64
685 #define DRIVEPARTS 35 /* max partitions per drive, excluding c */
686 char partname[DRIVENAMELEN]; /* for creating partition names */
688 status = 0; /* success indication */
689 vinum_conf.flags |= VF_READING_CONFIG; /* reading config from disk */
691 gooddrives = 0; /* number of usable drives found */
692 firstdrive = vinum_conf.drives_used; /* the first drive */
693 firsttime = vinum_conf.drives_used == 0; /* are we a virgin? */
695 /* allocate a drive pointer list */
696 drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
697 CHECKALLOC(drivelist, "Can't allocate memory");
698 error = lock_config(); /* make sure we're alone here */
699 if (error)
700 return error;
701 error = setjmp(command_fail); /* come back here on error */
702 if (error) { /* longjmped out */
703 unlock_config();
704 return error;
707 /* Open all drives and find which was modified most recently */
708 for (driveno = 0; driveno < drives; driveno++) {
709 char part; /* UNIX partition */
710 int slice;
711 int founddrive; /* flag when we find a vinum drive */
712 int has_slice = 0;
713 int has_part = 0;
714 char *tmp;
716 founddrive = 0; /* no vinum drive found yet on this spindle */
719 * If the device path contains a slice we do not try to tack on
720 * another slice. If the device path has a partition we only check
721 * that partition.
723 if ((tmp = rindex(devicename[driveno], '/')) == NULL)
724 tmp = devicename[driveno];
725 while (*tmp && (*tmp < '0' || *tmp > '9'))
726 ++tmp;
727 while (*tmp && *tmp >= '0' && *tmp <= '9')
728 ++tmp;
729 if (*tmp == 's')
730 has_slice = strtol(tmp + 1, &tmp, 0);
731 if (*tmp >= 'a' && *tmp <= 'p')
732 has_part = *tmp;
735 * Scan slices if no slice was specified, only if no partition was
736 * specified.
738 if (has_slice == 0 && has_part == 0)
739 for (slice = 0; slice < MAX_SLICES; slice++) {
740 if (has_slice && slice != has_slice)
741 continue;
743 for (part = 'a'; part < 'a' + MAXPARTITIONS; part++) {
744 if (has_part && part != has_part)
745 continue;
746 if (part == 'c')
747 continue;
748 ksnprintf(partname, DRIVENAMELEN,
749 "%ss%d%c", devicename[driveno], slice, part);
750 drive = check_drive(partname); /* try to open it */
751 if ((drive->lasterror != 0) /* didn't work, */
752 ||(drive->state != drive_up))
753 free_drive(drive); /* get rid of it */
754 else if (drive->flags & VF_CONFIGURED) /* already read this config, */
755 log(LOG_WARNING,
756 "vinum: already read config from %s\n", /* say so */
757 drive->label.name);
758 else {
759 drivelist[gooddrives] = drive->driveno; /* keep the drive index */
760 drive->flags &= ~VF_NEWBORN; /* which is no longer newly born */
761 gooddrives++;
762 founddrive++;
766 if (founddrive == 0 && has_slice == 0) { /* didn't find anything, */
767 for (part = 'a'; part < 'a' + MAXPARTITIONS; part++) { /* try the compatibility partition */
768 if (has_part && has_part != part)
769 continue;
770 if (part == 'c')
771 continue;
772 if (has_part) {
773 ksnprintf(partname, DRIVENAMELEN,
774 "%s", devicename[driveno]);
775 } else {
776 ksnprintf(partname, DRIVENAMELEN,
777 "%s%c", devicename[driveno], part);
779 drive = check_drive(partname); /* try to open it */
780 if ((drive->lasterror != 0) /* didn't work, */
781 ||(drive->state != drive_up))
782 free_drive(drive); /* get rid of it */
783 else if (drive->flags & VF_CONFIGURED) /* already read this config, */
784 log(LOG_WARNING,
785 "vinum: already read config from %s\n", /* say so */
786 drive->label.name);
787 else {
788 drivelist[gooddrives] = drive->driveno; /* keep the drive index */
789 drive->flags &= ~VF_NEWBORN; /* which is no longer newly born */
790 gooddrives++;
796 if (gooddrives == 0) {
797 if (firsttime)
798 log(LOG_WARNING, "vinum: no drives found\n");
799 else
800 log(LOG_INFO, "vinum: no additional drives found\n");
801 unlock_config();
802 return ENOENT;
805 * We now have at least one drive
806 * open. Sort them in order of config time
807 * and merge the config info with what we
808 * have already.
810 kqsort(drivelist, gooddrives, sizeof(int), drivecmp);
811 config_text = (char *) Malloc(MAXCONFIG * 2); /* allocate buffers */
812 CHECKALLOC(config_text, "Can't allocate memory");
813 config_line = (char *) Malloc(MAXCONFIGLINE * 2); /* allocate buffers */
814 CHECKALLOC(config_line, "Can't allocate memory");
815 for (driveno = 0; driveno < gooddrives; driveno++) { /* now include the config */
816 drive = &DRIVE[drivelist[driveno]]; /* point to the drive */
818 if (firsttime && (driveno == 0)) /* we've never configured before, */
819 log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
820 else
821 log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
823 if (drive->state == drive_up)
824 /* Read in both copies of the configuration information */
825 error = read_drive(drive, config_text, MAXCONFIG * 2, VINUM_CONFIG_OFFSET);
826 else {
827 error = EIO;
828 kprintf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
831 if (error != 0) {
832 log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
833 free_drive(drive); /* give it back */
834 status = error;
837 * At this point, check that the two copies
838 * are the same, and do something useful if
839 * not. In particular, consider which is
840 * newer, and what this means for the
841 * integrity of the data on the drive.
843 else {
844 vinum_conf.drives_used++; /* another drive in use */
845 /* Parse the configuration, and add it to the global configuration */
846 for (cptr = config_text; *cptr != '\0';) { /* love this style(9) */
847 volatile int parse_status; /* return value from parse_config */
849 for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');) /* until the end of the line */
850 *eptr++ = *cptr++;
851 *eptr = '\0'; /* and delimit */
852 if (setjmp(command_fail) == 0) { /* come back here on error and continue */
853 parse_status = parse_config(config_line, &keyword_set, 1); /* parse the config line */
854 if (parse_status < 0) { /* error in config */
856 * This config should have been parsed in user
857 * space. If we run into problems here, something
858 * serious is afoot. Complain and let the user
859 * snarf the config to see what's wrong.
861 log(LOG_ERR,
862 "vinum: Config error on %s, aborting integration\n",
863 drive->devicename);
864 free_drive(drive); /* give it back */
865 status = EINVAL;
868 while (*cptr == '\n')
869 cptr++; /* skip to next line */
872 drive->flags |= VF_CONFIGURED; /* read this drive's configuration */
875 Free(config_line);
876 Free(config_text);
877 Free(drivelist);
878 vinum_conf.flags &= ~VF_READING_CONFIG; /* no longer reading from disk */
879 if (status != 0)
880 kprintf("vinum: couldn't read configuration");
881 else
882 updateconfig(VF_READING_CONFIG); /* update from disk config */
883 unlock_config();
884 return status;
888 * Compare the modification dates of the drives, for qsort.
889 * Return 1 if a < b, 0 if a == b, 01 if a > b: in other
890 * words, sort backwards.
893 drivecmp(const void *va, const void *vb)
895 const struct drive *a = &DRIVE[*(const int *) va];
896 const struct drive *b = &DRIVE[*(const int *) vb];
898 if ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
899 && (a->label.last_update.tv_usec == b->label.last_update.tv_usec))
900 return 0;
901 else if ((a->label.last_update.tv_sec > b->label.last_update.tv_sec)
902 || ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
903 && (a->label.last_update.tv_usec > b->label.last_update.tv_usec)))
904 return -1;
905 else
906 return 1;
908 /* Local Variables: */
909 /* fill-column: 50 */
910 /* End: */