2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * This software is distributed under the so-called ``Berkeley
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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 $
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
54 open_drive(struct drive
*drive
, struct proc
*p
, int verbose
)
56 struct nlookupdata nd
;
61 * Fail if already open
63 if (drive
->flags
& VF_OPEN
)
65 dname
= drive
->devicename
;
68 * Severe hack to disallow opening partition 'c'
70 if (strncmp(dname
, "/dev", 4) == 0 && dname
[strlen(dname
)-1] == 'c')
75 * Open via filesystem (future)
77 error
= nlookup_init(&nd
, drive
->devicename
, UIO_SYSSPACE
, NLC_FOLLOW
);
80 error
= vn_open(&nd
, NULL
, FREAD
|FWRITE
, 0);
81 drive
->vp
= nd
.nl_open_vp
;
86 * Open via synthesized vnode backed by disk device
88 error
= vn_opendisk(drive
->devicename
, FREAD
|FWRITE
, &drive
->vp
);
93 if (error
== 0 && drive
->vp
== NULL
)
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
);
106 drive
->state
= drive_down
;
109 "vinum open_drive %s: failed with error %d\n",
110 drive
->devicename
, error
);
113 drive
->dev
= drive
->vp
->v_rdev
;
114 drive
->flags
|= VF_OPEN
;
116 drive
->lasterror
= 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
;
136 if (debug
& DEBUG_BIGDRIVE
) /* pretend we're 100 times as big */
137 drive
->label
.drive_size
*= 100;
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
;
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 */
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
;
168 * Initialize a drive: open the device and add device
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");
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
) {
189 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
193 return drive
->lasterror
;
195 if (drive
->partinfo
.fstype
!= FS_VINUM
&&
196 !kuuid_is_vinum(&drive
->partinfo
.fstype_uuid
)
198 drive
->lasterror
= EFTYPE
;
201 "vinum open_drive %s: Wrong partition type for vinum\n",
206 return set_drive_parms(drive
); /* set various odds and ends */
209 /* Close a drive if it's open. */
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 */
222 * Real drive close code, called with drive already locked.
223 * We have also checked that the drive is open. No errors.
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.
234 drive
->lasterror
= vn_close(drive
->vp
, FREAD
|FWRITE
);
237 drive
->flags
&= ~VF_OPEN
;
241 * Remove drive from the configuration.
242 * Caller must ensure that it isn't active.
245 remove_drive(int driveno
)
247 struct drive
*drive
= &vinum_conf
.drive
[driveno
];
248 struct vinum_hdr
*vhdr
; /* buffer for header */
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
);
257 drive
->lasterror
= error
;
259 vhdr
->magic
= VINUM_NOMAGIC
; /* obliterate the magic, but leave the rest */
260 write_drive(drive
, (void *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
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
)
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 */
292 bp
->b_bio1
.bio_offset
= offset
; /* disk offset */
293 saveaddr
= bp
->b_data
;
296 vn_strategy(drive
->vp
, &bp
->b_bio1
);
298 bp
->b_data
= saveaddr
;
299 bp
->b_flags
|= B_INVAL
| B_AGE
;
300 bp
->b_flags
&= ~B_ERROR
;
304 length
-= len
; /* update pointers */
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
)
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 */
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 */
359 result
= DL_NOT_OURS
; /* we could have it, but we don't yet */
360 Free(vhdr
); /* that's all. */
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.
372 check_drive(char *devicename
)
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
395 drive
->lasterror
= EEXIST
;
397 } else { /* it's just a place holder, */
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 */
412 if (drive
->lasterror
== 0)
413 drive
->lasterror
= ENODEV
;
415 drive
->state
= drive_down
;
421 sappend(char *txt
, char *s
)
423 while ((*s
++ = *txt
++) != 0);
428 format_config(char *config
, int len
)
433 char *configend
= &config
[len
];
437 /* First write the volume configuration */
438 for (i
= 0; i
< vinum_conf
.volumes_allocated
; i
++) {
441 vol
= &vinum_conf
.volume
[i
];
442 if ((vol
->state
> volume_uninit
)
443 && (vol
->name
[0] != '\0')) { /* paranoia */
446 "volume %s state %s",
448 volume_state(vol
->state
));
450 s
++; /* find the end */
451 if (vol
->preferred_plex
>= 0) /* preferences, */
454 " readpol prefer %s",
455 vinum_conf
.plex
[vol
->preferred_plex
].name
);
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
++) {
466 plex
= &vinum_conf
.plex
[i
];
467 if ((plex
->state
> plex_referenced
)
468 && (plex
->name
[0] != '\0')) { /* paranoia */
471 "plex name %s state %s org %s ",
473 plex_state(plex
->state
),
474 plex_org(plex
->organization
));
476 s
++; /* find the end */
477 if (isstriped(plex
)) {
481 (int) plex
->stripesize
);
483 s
++; /* find the end */
485 if (plex
->volno
>= 0) /* we have a volume */
489 vinum_conf
.volume
[plex
->volno
].name
);
491 s
++; /* find the end */
492 for (j
= 0; j
< plex
->subdisks
; j
++) {
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
++) {
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*";
524 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
527 vinum_conf
.plex
[sd
->plexno
].name
,
528 (unsigned long long) sd
->sectors
,
529 (unsigned long long) sd
->driveoffset
,
530 sd_state(sd
->state
));
532 s
++; /* find the end */
537 (long long) sd
->plexoffset
);
539 ksnprintf(s
, configend
- s
, " detached");
541 s
++; /* find the end */
542 if (sd
->flags
& VF_RETRYERRORS
) {
543 ksnprintf(s
, configend
- s
, " retryerrors");
545 s
++; /* find the end */
547 ksnprintf(s
, configend
- s
, " \n");
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
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
571 daemon_save_config(void)
574 int written_config
; /* set when we first write the config to disk */
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
)
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')) {
609 free_drive(drive
); /* get rid of it */
612 if (((drive
->flags
& VF_OPEN
) == 0) /* drive not open */
613 &&(drive
->state
> drive_down
)) { /* and it thinks it's not down */
615 set_drive_state(driveno
, drive_down
, setstate_force
); /* tell it what's what */
618 if ((drive
->state
== drive_down
) /* it's down */
619 &&(drive
->flags
& VF_OPEN
)) { /* but open, */
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 */
632 error
= VOP_IOCTL(drive
->vp
, DIOCWLABEL
,
634 FREAD
|FWRITE
, proc0
.p_ucred
);
637 error
= write_drive(drive
, (char *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
639 error
= write_drive(drive
, config
, MAXCONFIG
, VINUM_CONFIG_OFFSET
); /* first config copy */
641 error
= write_drive(drive
, config
, MAXCONFIG
, VINUM_CONFIG_OFFSET
+ MAXCONFIG
); /* second copy */
642 wlabel_on
= 0; /* enable writing the label */
645 error
= VOP_IOCTL(drive
->vp
, DIOCWLABEL
,
647 FREAD
|FWRITE
, proc0
.p_ucred
);
653 "vinum: Can't write config to %s, error %d\n",
656 set_drive_state(drive
->driveno
, drive_down
, setstate_force
);
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 */
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 */
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 */
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 */
701 error
= setjmp(command_fail
); /* come back here on error */
702 if (error
) { /* longjmped out */
707 /* Open all drives and find which was modified most recently */
708 for (driveno
= 0; driveno
< drives
; driveno
++) {
709 char part
; /* UNIX partition */
711 int founddrive
; /* flag when we find a vinum drive */
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
723 if ((tmp
= rindex(devicename
[driveno
], '/')) == NULL
)
724 tmp
= devicename
[driveno
];
725 while (*tmp
&& (*tmp
< '0' || *tmp
> '9'))
727 while (*tmp
&& *tmp
>= '0' && *tmp
<= '9')
730 has_slice
= strtol(tmp
+ 1, &tmp
, 0);
731 if (*tmp
>= 'a' && *tmp
<= 'p')
735 * Scan slices if no slice was specified, only if no partition was
738 if (has_slice
== 0 && has_part
== 0)
739 for (slice
= 0; slice
< MAX_SLICES
; slice
++) {
740 if (has_slice
&& slice
!= has_slice
)
743 for (part
= 'a'; part
< 'a' + MAXPARTITIONS
; part
++) {
744 if (has_part
&& part
!= has_part
)
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, */
756 "vinum: already read config from %s\n", /* say so */
759 drivelist
[gooddrives
] = drive
->driveno
; /* keep the drive index */
760 drive
->flags
&= ~VF_NEWBORN
; /* which is no longer newly born */
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
)
773 ksnprintf(partname
, DRIVENAMELEN
,
774 "%s", devicename
[driveno
]);
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, */
785 "vinum: already read config from %s\n", /* say so */
788 drivelist
[gooddrives
] = drive
->driveno
; /* keep the drive index */
789 drive
->flags
&= ~VF_NEWBORN
; /* which is no longer newly born */
796 if (gooddrives
== 0) {
798 log(LOG_WARNING
, "vinum: no drives found\n");
800 log(LOG_INFO
, "vinum: no additional drives found\n");
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
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
);
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
);
828 kprintf("vinum_scandisk: %s is %s\n", drive
->devicename
, drive_state(drive
->state
));
832 log(LOG_ERR
, "vinum: Can't read device %s, error %d\n", drive
->devicename
, error
);
833 free_drive(drive
); /* give it back */
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.
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 */
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.
862 "vinum: Config error on %s, aborting integration\n",
864 free_drive(drive
); /* give it back */
868 while (*cptr
== '\n')
869 cptr
++; /* skip to next line */
872 drive
->flags
|= VF_CONFIGURED
; /* read this drive's configuration */
878 vinum_conf
.flags
&= ~VF_READING_CONFIG
; /* no longer reading from disk */
880 kprintf("vinum: couldn't read configuration");
882 updateconfig(VF_READING_CONFIG
); /* update from disk config */
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
))
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
)))
908 /* Local Variables: */
909 /* fill-column: 50 */