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.25 2007/06/19 06:07:55 dillon Exp $
43 #include <vm/vm_zone.h>
45 static char *sappend(char *txt
, char *s
);
46 static int drivecmp(const void *va
, const void *vb
);
49 * Open the device associated with the drive, and set drive's vp.
50 * Return an error number
53 open_drive(struct drive
*drive
, struct proc
*p
, int verbose
)
55 int devmajor
; /* major devs for disk device */
56 int devminor
; /* minor devs for disk device */
60 if (bcmp(drive
->devicename
, "/dev/", 5)) /* device name doesn't start with /dev */
61 return ENOENT
; /* give up */
62 if (drive
->flags
& VF_OPEN
) /* open already, */
63 return EBUSY
; /* don't do it again */
66 * Yes, Bruce, I know this is horrible, but we
67 * don't have a root file system when we first
68 * try to do this. If you can come up with a
69 * better solution, I'd really like it. I'm
70 * just putting it in now to add ammuntion to
71 * moving the system to devfs.
73 dname
= &drive
->devicename
[5];
74 drive
->dev
= NULL
; /* no device yet */
77 if (bcmp(dname
, "ad", 2) == 0) /* IDE disk */
79 else if (bcmp(dname
, "wd", 2) == 0) /* IDE disk */
81 else if (bcmp(dname
, "da", 2) == 0)
83 else if (bcmp(dname
, "vn", 2) == 0)
85 else if (bcmp(dname
, "md", 2) == 0)
87 else if (bcmp(dname
, "vkd", 3) == 0) {
90 } else if (bcmp(dname
, "amrd", 4) == 0) {
93 } else if (bcmp(dname
, "mlxd", 4) == 0) {
96 } else if (bcmp(dname
, "idad", 4) == 0) {
99 } else if (bcmp(dname
, "twed", 4) == 0) { /* 3ware raid */
102 } else if (bcmp(dname
, "ar", 2) == 0) {
106 dname
+= 2; /* point past */
109 * Found the device. We can expect one of
110 * two formats for the rest: a unit number,
111 * then either a partition letter for the
112 * compatiblity partition (e.g. h) or a
113 * slice ID and partition (e.g. s2e).
114 * Create a minor number for each of them.
117 while ((*dname
>= '0') /* unit number */
119 unit
= unit
* 10 + *dname
- '0';
123 if (*dname
== 's') { /* slice */
124 if (((dname
[1] < '1') || (dname
[1] > '4')) /* invalid slice */
125 ||((dname
[2] < 'a') || (dname
[2] > 'p'))) /* or invalid partition */
127 devminor
= dkmakeminor(unit
, dname
[1] - '0' + 1, (dname
[2] - 'a'));
128 } else { /* compatibility partition */
129 if ((*dname
< 'a') || (*dname
> 'p')) /* or invalid partition */
131 devminor
= dkmakeminor(unit
, 0, (dname
[0] - 'a'));
135 * Disallow partition c
137 if ((((devminor
>> 17) & 0x08) | (devminor
& 7)) == 2)
138 return ENOTTY
; /* not buying that */
140 drive
->dev
= udev2dev(makeudev(devmajor
, devminor
), 0);
142 if (drive
->dev
== NULL
)
145 drive
->dev
->si_iosize_max
= DFLTPHYS
;
146 if (dev_is_good(drive
->dev
))
147 drive
->lasterror
= dev_dopen(drive
->dev
, FWRITE
, 0, proc0
.p_ucred
);
149 drive
->lasterror
= ENOENT
;
151 if (drive
->lasterror
!= 0) { /* failed */
152 drive
->state
= drive_down
; /* just force it down */
155 "vinum open_drive %s: failed with error %d\n",
156 drive
->devicename
, drive
->lasterror
);
158 drive
->flags
|= VF_OPEN
; /* we're open now */
160 return drive
->lasterror
;
164 * Set some variables in the drive struct
165 * in more convenient form. Return error indication
168 set_drive_parms(struct drive
*drive
)
170 drive
->blocksize
= BLKDEV_IOSIZE
; /* do we need this? */
171 drive
->secsperblock
= drive
->blocksize
/* number of sectors per block */
172 / drive
->partinfo
.media_blksize
;
174 /* Now update the label part */
175 bcopy(hostname
, drive
->label
.sysname
, VINUMHOSTNAMELEN
); /* put in host name */
176 getmicrotime(&drive
->label
.date_of_birth
); /* and current time */
177 drive
->label
.drive_size
= drive
->partinfo
.media_size
;
179 if (debug
& DEBUG_BIGDRIVE
) /* pretend we're 100 times as big */
180 drive
->label
.drive_size
*= 100;
183 /* number of sectors available for subdisks */
184 drive
->sectors_available
= drive
->label
.drive_size
/ DEV_BSIZE
- DATASTART
;
187 * Bug in 3.0 as of January 1998: you can open
188 * non-existent slices. They have a length of 0.
190 if (drive
->label
.drive_size
< MINVINUMSLICE
) { /* too small to worry about */
191 set_drive_state(drive
->driveno
, drive_down
, setstate_force
);
192 drive
->lasterror
= ENOSPC
;
195 drive
->freelist_size
= INITIAL_DRIVE_FREELIST
; /* initial number of entries */
196 drive
->freelist
= (struct drive_freelist
*)
197 Malloc(INITIAL_DRIVE_FREELIST
* sizeof(struct drive_freelist
));
198 if (drive
->freelist
== NULL
) /* can't malloc, dammit */
200 drive
->freelist_entries
= 1; /* just (almost) the complete drive */
201 drive
->freelist
[0].offset
= DATASTART
; /* starts here */
202 drive
->freelist
[0].sectors
= (drive
->label
.drive_size
>> DEV_BSHIFT
) - DATASTART
; /* and it's this long */
203 if (drive
->label
.name
[0] != '\0') /* got a name */
204 set_drive_state(drive
->driveno
, drive_up
, setstate_force
); /* our drive is accessible */
205 else /* we know about it, but that's all */
206 drive
->state
= drive_referenced
;
211 * Initialize a drive: open the device and add device
215 init_drive(struct drive
*drive
, int verbose
)
217 if (drive
->devicename
[0] != '/') {
218 drive
->lasterror
= EINVAL
;
219 log(LOG_ERR
, "vinum: Can't open drive without drive name\n");
222 drive
->lasterror
= open_drive(drive
, curproc
, verbose
); /* open the drive */
223 if (drive
->lasterror
)
224 return drive
->lasterror
;
226 drive
->lasterror
= dev_dioctl(
229 (caddr_t
) & drive
->partinfo
,
232 if (drive
->lasterror
) {
235 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
239 return drive
->lasterror
;
241 if (drive
->partinfo
.fstype
!= FS_VINUM
&&
242 !kuuid_is_vinum(&drive
->partinfo
.fstype_uuid
)
244 drive
->lasterror
= EFTYPE
;
247 "vinum open_drive %s: Wrong partition type for vinum\n",
252 return set_drive_parms(drive
); /* set various odds and ends */
255 /* Close a drive if it's open. */
257 close_drive(struct drive
*drive
)
259 LOCKDRIVE(drive
); /* keep the daemon out */
260 if (drive
->flags
& VF_OPEN
)
261 close_locked_drive(drive
); /* and close it */
262 if (drive
->state
> drive_down
) /* if it's up */
263 drive
->state
= drive_down
; /* make sure it's down */
268 * Real drive close code, called with drive already locked.
269 * We have also checked that the drive is open. No errors.
272 close_locked_drive(struct drive
*drive
)
275 * If we can't access the drive, we can't flush
276 * the queues, which spec_close() will try to
277 * do. Get rid of them here first.
279 drive
->lasterror
= dev_dclose(drive
->dev
, 0, 0);
280 drive
->flags
&= ~VF_OPEN
; /* no longer open */
284 * Remove drive from the configuration.
285 * Caller must ensure that it isn't active.
288 remove_drive(int driveno
)
290 struct drive
*drive
= &vinum_conf
.drive
[driveno
];
291 struct vinum_hdr
*vhdr
; /* buffer for header */
294 if (drive
->state
> drive_referenced
) { /* real drive */
295 if (drive
->state
== drive_up
) {
296 vhdr
= (struct vinum_hdr
*) Malloc(VINUMHEADERLEN
); /* allocate buffer */
297 CHECKALLOC(vhdr
, "Can't allocate memory");
298 error
= read_drive(drive
, (void *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
300 drive
->lasterror
= error
;
302 vhdr
->magic
= VINUM_NOMAGIC
; /* obliterate the magic, but leave the rest */
303 write_drive(drive
, (void *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
307 free_drive(drive
); /* close it and free resources */
308 save_config(); /* and save the updated configuration */
313 * Transfer drive data. Usually called from one of these defines;
314 * #define read_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_READ)
315 * #define write_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_WRITE)
317 * length and offset are in bytes, but must be multiples of sector
318 * size. The function *does not check* for this condition, and
319 * truncates ruthlessly.
320 * Return error number
323 driveio(struct drive
*drive
, char *buf
, size_t length
, off_t offset
, buf_cmd_t cmd
)
329 error
= 0; /* to keep the compiler happy */
330 while (length
) { /* divide into small enough blocks */
331 int len
= min(length
, MAXBSIZE
); /* maximum block device transfer is MAXBSIZE */
333 bp
= geteblk(len
); /* get a buffer header */
335 bp
->b_bio1
.bio_offset
= offset
; /* disk offset */
336 saveaddr
= bp
->b_data
;
339 dev_dstrategy(drive
->dev
, &bp
->b_bio1
);
341 bp
->b_data
= saveaddr
;
342 bp
->b_flags
|= B_INVAL
| B_AGE
;
343 bp
->b_flags
&= ~B_ERROR
;
347 length
-= len
; /* update pointers */
355 * Check a drive for a vinum header. If found,
356 * update the drive information. We come here
357 * with a partially populated drive structure
358 * which includes the device name.
360 * Return information on what we found.
362 * This function is called from two places: check_drive,
363 * which wants to find out whether the drive is a
364 * Vinum drive, and config_drive, which asserts that
365 * it is a vinum drive. In the first case, we don't
366 * print error messages (verbose==0), in the second
367 * we do (verbose==1).
369 enum drive_label_info
370 read_drive_label(struct drive
*drive
, int verbose
)
373 int result
; /* result of our search */
374 struct vinum_hdr
*vhdr
; /* and as header */
376 error
= init_drive(drive
, 0); /* find the drive */
377 if (error
) /* find the drive */
378 return DL_CANT_OPEN
; /* not ours */
380 vhdr
= (struct vinum_hdr
*) Malloc(VINUMHEADERLEN
); /* allocate buffers */
381 CHECKALLOC(vhdr
, "Can't allocate memory");
383 drive
->state
= drive_up
; /* be optimistic */
384 error
= read_drive(drive
, (void *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
385 if (vhdr
->magic
== VINUM_MAGIC
) { /* ours! */
386 if (drive
->label
.name
[0] /* we have a name for this drive */
387 &&(strcmp(drive
->label
.name
, vhdr
->label
.name
))) { /* but it doesn't match the real name */
388 drive
->lasterror
= EINVAL
;
389 result
= DL_WRONG_DRIVE
; /* it's the wrong drive */
390 drive
->state
= drive_unallocated
; /* put it back, it's not ours */
394 * We copy the drive anyway so that we have
395 * the correct name in the drive info. This
396 * may not be the name specified
398 drive
->label
= vhdr
->label
; /* put in the label information */
399 } else if (vhdr
->magic
== VINUM_NOMAGIC
) /* was ours, but we gave it away */
400 result
= DL_DELETED_LABEL
; /* and return the info */
402 result
= DL_NOT_OURS
; /* we could have it, but we don't yet */
403 Free(vhdr
); /* that's all. */
408 * Check a drive for a vinum header. If found,
409 * read configuration information from the drive and
410 * incorporate the data into the configuration.
412 * Return drive number.
415 check_drive(char *devicename
)
421 driveno
= find_drive_by_dev(devicename
, 1); /* if entry doesn't exist, create it */
422 drive
= &vinum_conf
.drive
[driveno
]; /* and get a pointer */
424 if (read_drive_label(drive
, 0) == DL_OURS
) { /* one of ours */
425 for (i
= 0; i
< vinum_conf
.drives_allocated
; i
++) { /* see if the name already exists */
426 if ((i
!= driveno
) /* not this drive */
427 &&(DRIVE
[i
].state
!= drive_unallocated
) /* and it's allocated */
428 &&(strcmp(DRIVE
[i
].label
.name
,
429 DRIVE
[driveno
].label
.name
) == 0)) { /* and it has the same name */
430 struct drive
*mydrive
= &DRIVE
[i
];
432 if (mydrive
->devicename
[0] == '/') { /* we know a device name for it */
434 * set an error, but don't take the
435 * drive down: that would cause unneeded
438 drive
->lasterror
= EEXIST
;
440 } else { /* it's just a place holder, */
443 for (sdno
= 0; sdno
< vinum_conf
.subdisks_allocated
; sdno
++) { /* look at each subdisk */
444 if ((SD
[sdno
].driveno
== i
) /* it's pointing to this one, */
445 &&(SD
[sdno
].state
!= sd_unallocated
)) { /* and it's a real subdisk */
446 SD
[sdno
].driveno
= drive
->driveno
; /* point to the one we found */
447 update_sd_state(sdno
); /* and update its state */
450 bzero(mydrive
, sizeof(struct drive
)); /* don't deallocate it, just remove it */
455 if (drive
->lasterror
== 0)
456 drive
->lasterror
= ENODEV
;
458 drive
->state
= drive_down
;
464 sappend(char *txt
, char *s
)
466 while ((*s
++ = *txt
++) != 0);
471 format_config(char *config
, int len
)
476 char *configend
= &config
[len
];
480 /* First write the volume configuration */
481 for (i
= 0; i
< vinum_conf
.volumes_allocated
; i
++) {
484 vol
= &vinum_conf
.volume
[i
];
485 if ((vol
->state
> volume_uninit
)
486 && (vol
->name
[0] != '\0')) { /* paranoia */
489 "volume %s state %s",
491 volume_state(vol
->state
));
493 s
++; /* find the end */
494 if (vol
->preferred_plex
>= 0) /* preferences, */
497 " readpol prefer %s",
498 vinum_conf
.plex
[vol
->preferred_plex
].name
);
500 s
++; /* find the end */
501 s
= sappend("\n", s
);
505 /* Then the plex configuration */
506 for (i
= 0; i
< vinum_conf
.plexes_allocated
; i
++) {
509 plex
= &vinum_conf
.plex
[i
];
510 if ((plex
->state
> plex_referenced
)
511 && (plex
->name
[0] != '\0')) { /* paranoia */
514 "plex name %s state %s org %s ",
516 plex_state(plex
->state
),
517 plex_org(plex
->organization
));
519 s
++; /* find the end */
520 if (isstriped(plex
)) {
524 (int) plex
->stripesize
);
526 s
++; /* find the end */
528 if (plex
->volno
>= 0) /* we have a volume */
532 vinum_conf
.volume
[plex
->volno
].name
);
534 s
++; /* find the end */
535 for (j
= 0; j
< plex
->subdisks
; j
++) {
539 vinum_conf
.sd
[plex
->sdnos
[j
]].name
);
541 s
= sappend("\n", s
);
545 /* And finally the subdisk configuration */
546 for (i
= 0; i
< vinum_conf
.subdisks_allocated
; i
++) {
551 if ((sd
->state
!= sd_referenced
)
552 && (sd
->state
!= sd_unallocated
)
553 && (sd
->name
[0] != '\0')) { /* paranoia */
554 drivename
= vinum_conf
.drive
[sd
->driveno
].label
.name
;
556 * XXX We've seen cases of dead subdisks
557 * which don't have a drive. If we let them
558 * through here, the drive name is null, so
559 * they get the drive named 'plex'.
561 * This is a breakage limiter, not a fix.
563 if (drivename
[0] == '\0')
564 drivename
= "*invalid*";
567 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
570 vinum_conf
.plex
[sd
->plexno
].name
,
571 (unsigned long long) sd
->sectors
,
572 (unsigned long long) sd
->driveoffset
,
573 sd_state(sd
->state
));
575 s
++; /* find the end */
580 (long long) sd
->plexoffset
);
582 ksnprintf(s
, configend
- s
, " detached");
584 s
++; /* find the end */
585 if (sd
->flags
& VF_RETRYERRORS
) {
586 ksnprintf(s
, configend
- s
, " retryerrors");
588 s
++; /* find the end */
590 ksnprintf(s
, configend
- s
, " \n");
592 s
++; /* find the end */
595 if (s
> &config
[len
- 2])
596 panic("vinum: configuration data overflow");
600 * issue a save config request to the dæmon. The actual work
601 * is done in process context by daemon_save_config
606 queue_daemon_request(daemonrq_saveconfig
, (union daemoninfo
) NULL
);
610 * Write the configuration to all vinum slices. This
611 * is performed by the dæmon only
614 daemon_save_config(void)
617 int written_config
; /* set when we first write the config to disk */
619 struct drive
*drive
; /* point to current drive info */
620 struct vinum_hdr
*vhdr
; /* and as header */
621 char *config
; /* point to config data */
622 int wlabel_on
; /* to set writing label on/off */
624 /* don't save the configuration while we're still working on it */
625 if (vinum_conf
.flags
& VF_CONFIGURING
)
627 written_config
= 0; /* no config written yet */
628 /* Build a volume header */
629 vhdr
= (struct vinum_hdr
*) Malloc(VINUMHEADERLEN
); /* get space for the config data */
630 CHECKALLOC(vhdr
, "Can't allocate config data");
631 vhdr
->magic
= VINUM_MAGIC
; /* magic number */
632 vhdr
->config_length
= MAXCONFIG
; /* length of following config info */
634 config
= Malloc(MAXCONFIG
); /* get space for the config data */
635 CHECKALLOC(config
, "Can't allocate config data");
637 format_config(config
, MAXCONFIG
);
638 error
= 0; /* no errors yet */
639 for (driveno
= 0; driveno
< vinum_conf
.drives_allocated
; driveno
++) {
640 drive
= &vinum_conf
.drive
[driveno
]; /* point to drive */
641 if (drive
->state
> drive_referenced
) {
642 LOCKDRIVE(drive
); /* don't let it change */
645 * First, do some drive consistency checks. Some
646 * of these are kludges, others require a process
647 * context and couldn't be done before
649 if ((drive
->devicename
[0] == '\0')
650 || (drive
->label
.name
[0] == '\0')) {
652 free_drive(drive
); /* get rid of it */
655 if (((drive
->flags
& VF_OPEN
) == 0) /* drive not open */
656 &&(drive
->state
> drive_down
)) { /* and it thinks it's not down */
658 set_drive_state(driveno
, drive_down
, setstate_force
); /* tell it what's what */
661 if ((drive
->state
== drive_down
) /* it's down */
662 &&(drive
->flags
& VF_OPEN
)) { /* but open, */
664 close_drive(drive
); /* close it */
665 } else if (drive
->state
> drive_down
) {
666 getmicrotime(&drive
->label
.last_update
); /* time of last update is now */
667 bcopy((char *) &drive
->label
, /* and the label info from the drive structure */
668 (char *) &vhdr
->label
,
669 sizeof(vhdr
->label
));
670 if ((drive
->state
!= drive_unallocated
)
671 && (drive
->state
!= drive_referenced
)) { /* and it's a real drive */
672 wlabel_on
= 1; /* enable writing the label */
673 error
= dev_dioctl(drive
->dev
, /* make the label writeable */
675 (caddr_t
) & wlabel_on
,
679 error
= write_drive(drive
, (char *) vhdr
, VINUMHEADERLEN
, VINUM_LABEL_OFFSET
);
681 error
= write_drive(drive
, config
, MAXCONFIG
, VINUM_CONFIG_OFFSET
); /* first config copy */
683 error
= write_drive(drive
, config
, MAXCONFIG
, VINUM_CONFIG_OFFSET
+ MAXCONFIG
); /* second copy */
684 wlabel_on
= 0; /* enable writing the label */
686 error
= dev_dioctl(drive
->dev
, /* make the label non-writeable again */
688 (caddr_t
) & wlabel_on
,
694 "vinum: Can't write config to %s, error %d\n",
697 set_drive_state(drive
->driveno
, drive_down
, setstate_force
);
699 written_config
= 1; /* we've written it on at least one drive */
701 } else /* not worth looking at, */
702 unlockdrive(drive
); /* just unlock it again */
709 /* Look at all disks on the system for vinum slices */
711 vinum_scandisk(char *devicename
[], int drives
)
713 struct drive
*volatile drive
;
714 volatile int driveno
;
715 int firstdrive
; /* first drive in this list */
716 volatile int gooddrives
; /* number of usable drives found */
717 int firsttime
; /* set if we have never configured before */
719 char *config_text
; /* read the config info from disk into here */
720 char *volatile cptr
; /* pointer into config information */
721 char *eptr
; /* end pointer into config information */
722 char *config_line
; /* copy the config line to */
724 int *volatile drivelist
; /* list of drive indices */
725 #define DRIVENAMELEN 64
726 #define DRIVEPARTS 35 /* max partitions per drive, excluding c */
727 char partname
[DRIVENAMELEN
]; /* for creating partition names */
729 status
= 0; /* success indication */
730 vinum_conf
.flags
|= VF_READING_CONFIG
; /* reading config from disk */
732 gooddrives
= 0; /* number of usable drives found */
733 firstdrive
= vinum_conf
.drives_used
; /* the first drive */
734 firsttime
= vinum_conf
.drives_used
== 0; /* are we a virgin? */
736 /* allocate a drive pointer list */
737 drivelist
= (int *) Malloc(drives
* DRIVEPARTS
* sizeof(int));
738 CHECKALLOC(drivelist
, "Can't allocate memory");
740 /* Open all drives and find which was modified most recently */
741 for (driveno
= 0; driveno
< drives
; driveno
++) {
742 char part
; /* UNIX partition */
744 int founddrive
; /* flag when we find a vinum drive */
749 founddrive
= 0; /* no vinum drive found yet on this spindle */
752 * If the device path contains a slice we do not try to tack on
753 * another slice. If the device path has a partition we only check
756 if ((tmp
= rindex(devicename
[driveno
], '/')) == NULL
)
757 tmp
= devicename
[driveno
];
758 while (*tmp
&& (*tmp
< '0' || *tmp
> '9'))
760 while (*tmp
&& *tmp
>= '0' && *tmp
<= '9')
763 has_slice
= strtol(tmp
+ 1, &tmp
, 0);
764 if (*tmp
>= 'a' && *tmp
<= 'p')
768 * Scan slices if no slice was specified, only if no partition was
771 if (has_slice
== 0 && has_part
== 0)
772 for (slice
= 1; slice
< 5; slice
++) {
773 if (has_slice
&& slice
!= has_slice
)
776 for (part
= 'a'; part
< 'a' + MAXPARTITIONS
; part
++) {
777 if (has_part
&& part
!= has_part
)
781 ksnprintf(partname
, DRIVENAMELEN
,
782 "%ss%d%c", devicename
[driveno
], slice
, part
);
783 drive
= check_drive(partname
); /* try to open it */
784 if ((drive
->lasterror
!= 0) /* didn't work, */
785 ||(drive
->state
!= drive_up
))
786 free_drive(drive
); /* get rid of it */
787 else if (drive
->flags
& VF_CONFIGURED
) /* already read this config, */
789 "vinum: already read config from %s\n", /* say so */
792 drivelist
[gooddrives
] = drive
->driveno
; /* keep the drive index */
793 drive
->flags
&= ~VF_NEWBORN
; /* which is no longer newly born */
799 if (founddrive
== 0 && has_slice
== 0) { /* didn't find anything, */
800 for (part
= 'a'; part
< 'a' + MAXPARTITIONS
; part
++) { /* try the compatibility partition */
801 if (has_part
&& has_part
!= part
)
806 ksnprintf(partname
, DRIVENAMELEN
,
807 "%s", devicename
[driveno
]);
809 ksnprintf(partname
, DRIVENAMELEN
,
810 "%s%c", devicename
[driveno
], part
);
812 drive
= check_drive(partname
); /* try to open it */
813 if ((drive
->lasterror
!= 0) /* didn't work, */
814 ||(drive
->state
!= drive_up
))
815 free_drive(drive
); /* get rid of it */
816 else if (drive
->flags
& VF_CONFIGURED
) /* already read this config, */
818 "vinum: already read config from %s\n", /* say so */
821 drivelist
[gooddrives
] = drive
->driveno
; /* keep the drive index */
822 drive
->flags
&= ~VF_NEWBORN
; /* which is no longer newly born */
829 if (gooddrives
== 0) {
831 log(LOG_WARNING
, "vinum: no drives found\n");
833 log(LOG_INFO
, "vinum: no additional drives found\n");
837 * We now have at least one drive
838 * open. Sort them in order of config time
839 * and merge the config info with what we
842 kqsort(drivelist
, gooddrives
, sizeof(int), drivecmp
);
843 config_text
= (char *) Malloc(MAXCONFIG
* 2); /* allocate buffers */
844 CHECKALLOC(config_text
, "Can't allocate memory");
845 config_line
= (char *) Malloc(MAXCONFIGLINE
* 2); /* allocate buffers */
846 CHECKALLOC(config_line
, "Can't allocate memory");
847 for (driveno
= 0; driveno
< gooddrives
; driveno
++) { /* now include the config */
848 drive
= &DRIVE
[drivelist
[driveno
]]; /* point to the drive */
850 if (firsttime
&& (driveno
== 0)) /* we've never configured before, */
851 log(LOG_INFO
, "vinum: reading configuration from %s\n", drive
->devicename
);
853 log(LOG_INFO
, "vinum: updating configuration from %s\n", drive
->devicename
);
855 if (drive
->state
== drive_up
)
856 /* Read in both copies of the configuration information */
857 error
= read_drive(drive
, config_text
, MAXCONFIG
* 2, VINUM_CONFIG_OFFSET
);
860 kprintf("vinum_scandisk: %s is %s\n", drive
->devicename
, drive_state(drive
->state
));
864 log(LOG_ERR
, "vinum: Can't read device %s, error %d\n", drive
->devicename
, error
);
865 free_drive(drive
); /* give it back */
869 * At this point, check that the two copies
870 * are the same, and do something useful if
871 * not. In particular, consider which is
872 * newer, and what this means for the
873 * integrity of the data on the drive.
876 vinum_conf
.drives_used
++; /* another drive in use */
877 /* Parse the configuration, and add it to the global configuration */
878 for (cptr
= config_text
; *cptr
!= '\0';) { /* love this style(9) */
879 volatile int parse_status
; /* return value from parse_config */
881 for (eptr
= config_line
; (*cptr
!= '\n') && (*cptr
!= '\0');) /* until the end of the line */
883 *eptr
= '\0'; /* and delimit */
884 if (setjmp(command_fail
) == 0) { /* come back here on error and continue */
885 parse_status
= parse_config(config_line
, &keyword_set
, 1); /* parse the config line */
886 if (parse_status
< 0) { /* error in config */
888 * This config should have been parsed in user
889 * space. If we run into problems here, something
890 * serious is afoot. Complain and let the user
891 * snarf the config to see what's wrong.
894 "vinum: Config error on %s, aborting integration\n",
896 free_drive(drive
); /* give it back */
900 while (*cptr
== '\n')
901 cptr
++; /* skip to next line */
904 drive
->flags
|= VF_CONFIGURED
; /* read this drive's configuration */
910 vinum_conf
.flags
&= ~VF_READING_CONFIG
; /* no longer reading from disk */
912 kprintf("vinum: couldn't read configuration");
914 updateconfig(VF_READING_CONFIG
); /* update from disk config */
919 * Compare the modification dates of the drives, for qsort.
920 * Return 1 if a < b, 0 if a == b, 01 if a > b: in other
921 * words, sort backwards.
924 drivecmp(const void *va
, const void *vb
)
926 const struct drive
*a
= &DRIVE
[*(const int *) va
];
927 const struct drive
*b
= &DRIVE
[*(const int *) vb
];
929 if ((a
->label
.last_update
.tv_sec
== b
->label
.last_update
.tv_sec
)
930 && (a
->label
.last_update
.tv_usec
== b
->label
.last_update
.tv_usec
))
932 else if ((a
->label
.last_update
.tv_sec
> b
->label
.last_update
.tv_sec
)
933 || ((a
->label
.last_update
.tv_sec
== b
->label
.last_update
.tv_sec
)
934 && (a
->label
.last_update
.tv_usec
> b
->label
.last_update
.tv_usec
)))
939 /* Local Variables: */
940 /* fill-column: 50 */