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.32 2001/05/23 23:03:45 grog Exp grog $
40 #include <dev/vinum/vinumhdr.h>
41 #include <dev/vinum/request.h>
43 static char *sappend(char *txt, char *s);
44 static int drivecmp(const void *va, const void *vb);
47 * Open the device associated with the drive, and set drive's vp.
48 * Return an error number
51 open_drive(struct drive *drive, struct thread *td, int verbose)
53 int devmajor; /* major devs for disk device */
54 int devminor; /* minor devs for disk device */
57 struct cdevsw *dsw; /* pointer to cdevsw entry */
59 if (bcmp(drive->devicename, "/dev/", 5)) /* device name doesn't start with /dev */
60 return ENOENT; /* give up */
61 if (drive->flags & VF_OPEN) /* open already, */
62 return EBUSY; /* don't do it again */
65 * Yes, Bruce, I know this is horrible, but we
66 * don't have a root filesystem when we first
67 * try to do this. If you can come up with a
68 * better solution, I'd really like it. I'm
69 * just putting it in now to add ammuntion to
70 * moving the system to devfs.
72 dname = &drive->devicename[5];
73 drive->dev = NULL; /* no device yet */
76 if (bcmp(dname, "ad", 2) == 0) /* IDE disk */
78 else if (bcmp(dname, "wd", 2) == 0) /* IDE disk */
80 else if (bcmp(dname, "da", 2) == 0)
82 else if (bcmp(dname, "vn", 2) == 0)
84 else if (bcmp(dname, "md", 2) == 0)
86 else if (bcmp(dname, "ar", 2) == 0)
88 else if (bcmp(dname, "amrd", 4) == 0) {
91 } else if (bcmp(dname, "mlxd", 4) == 0) {
94 } else if (bcmp(dname, "idad", 4) == 0) {
97 } else if (bcmp(dname, "twed", 4) == 0) { /* 3ware raid */
102 dname += 2; /* point past */
105 * Found the device. We can expect one of
106 * two formats for the rest: a unit number,
107 * then either a partition letter for the
108 * compatiblity partition (e.g. h) or a
109 * slice ID and partition (e.g. s2e).
110 * Create a minor number for each of them.
113 while ((*dname >= '0') /* unit number */
115 unit = unit * 10 + *dname - '0';
119 if (*dname == 's') { /* slice */
120 if (((dname[1] < '1') || (dname[1] > '4')) /* invalid slice */
121 ||((dname[2] < 'a') || (dname[2] > 'h'))) /* or invalid partition */
123 devminor = ((unit & 31) << 3) /* unit */
124 +(dname[2] - 'a') /* partition */
125 +((dname[1] - '0' + 1) << 16) /* slice */
126 +((unit & ~31) << 16); /* high-order unit bits */
127 } else { /* compatibility partition */
128 if ((*dname < 'a') || (*dname > 'h')) /* or invalid partition */
130 devminor = (*dname - 'a') /* partition */
131 +((unit & 31) << 3) /* unit */
132 +((unit & ~31) << 16); /* high-order unit bits */
135 if ((devminor & 7) == 2) /* partition c */
136 return ENOTTY; /* not buying that */
138 drive->dev = makedev(devmajor, devminor); /* find the device */
139 if (drive->dev == NULL) /* didn't find anything */
142 drive->dev->si_iosize_max = DFLTPHYS;
143 dsw = devsw(drive->dev);
145 drive->lasterror = ENOENT;
147 drive->lasterror = (dsw->d_open) (drive->dev, FWRITE, 0, NULL);
149 if (drive->lasterror != 0) { /* failed */
150 drive->state = drive_down; /* just force it down */
153 "vinum open_drive %s: failed with error %d\n",
154 drive->devicename, drive->lasterror);
156 drive->flags |= VF_OPEN; /* we're open now */
158 return drive->lasterror;
162 * Set some variables in the drive struct
163 * in more convenient form. Return error indication
166 set_drive_parms(struct drive *drive)
168 drive->blocksize = BLKDEV_IOSIZE; /* do we need this? */
169 drive->secsperblock = drive->blocksize /* number of sectors per block */
172 /* Now update the label part */
173 bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN); /* put in host name */
174 getmicrotime(&drive->label.date_of_birth); /* and current time */
175 drive->label.drive_size = drive->mediasize;
177 if (debug & DEBUG_BIGDRIVE) /* pretend we're 100 times as big */
178 drive->label.drive_size *= 100;
181 /* number of sectors available for subdisks */
182 drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
185 * Bug in 3.0 as of January 1998: you can open
186 * non-existent slices. They have a length of 0.
188 if (drive->label.drive_size < MINVINUMSLICE) { /* too small to worry about */
189 set_drive_state(drive->driveno, drive_down, setstate_force);
190 drive->lasterror = ENOSPC;
193 drive->freelist_size = INITIAL_DRIVE_FREELIST; /* initial number of entries */
194 drive->freelist = (struct drive_freelist *)
195 Malloc(INITIAL_DRIVE_FREELIST * sizeof(struct drive_freelist));
196 if (drive->freelist == NULL) /* can't malloc, dammit */
198 drive->freelist_entries = 1; /* just (almost) the complete drive */
199 drive->freelist[0].offset = DATASTART; /* starts here */
200 drive->freelist[0].sectors = (drive->label.drive_size >> DEV_BSHIFT) - DATASTART; /* and it's this long */
201 if (drive->label.name[0] != '\0') /* got a name */
202 set_drive_state(drive->driveno, drive_up, setstate_force); /* our drive is accessible */
203 else /* we know about it, but that's all */
204 drive->state = drive_referenced;
209 * Initialize a drive: open the device and add device
213 init_drive(struct drive *drive, int verbose)
215 if (drive->devicename[0] != '/') {
216 drive->lasterror = EINVAL;
217 log(LOG_ERR, "vinum: Can't open drive without drive name\n");
220 drive->lasterror = open_drive(drive, curthread, verbose); /* open the drive */
221 if (drive->lasterror)
222 return drive->lasterror;
224 drive->lasterror = (*devsw(drive->dev)->d_ioctl) (drive->dev,
226 (caddr_t) & drive->sectorsize,
229 if (drive->lasterror == 0)
230 drive->lasterror = (*devsw(drive->dev)->d_ioctl) (drive->dev,
232 (caddr_t) & drive->mediasize,
235 if (drive->lasterror) {
238 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
242 return drive->lasterror;
246 * XXX: this check is bogus and needs to be rewitten, we cannot guarantee
247 * XXX: that there will be a label with a typefield on all platforms.
249 if (drive->partinfo.part->p_fstype != FS_VINUM) { /* not Vinum */
250 drive->lasterror = EFTYPE;
253 "vinum open_drive %s: Wrong partition type for vinum\n",
259 return set_drive_parms(drive); /* set various odds and ends */
262 /* Close a drive if it's open. */
264 close_drive(struct drive *drive)
266 LOCKDRIVE(drive); /* keep the daemon out */
267 if (drive->flags & VF_OPEN)
268 close_locked_drive(drive); /* and close it */
269 if (drive->state > drive_down) /* if it's up */
270 drive->state = drive_down; /* make sure it's down */
275 * Real drive close code, called with drive already locked.
276 * We have also checked that the drive is open. No errors.
279 close_locked_drive(struct drive *drive)
282 * If we can't access the drive, we can't flush
283 * the queues, which spec_close() will try to
284 * do. Get rid of them here first.
286 drive->lasterror = (*devsw(drive->dev)->d_close) (drive->dev, 0, 0, NULL);
287 drive->flags &= ~VF_OPEN; /* no longer open */
291 * Remove drive from the configuration.
292 * Caller must ensure that it isn't active.
295 remove_drive(int driveno)
297 struct drive *drive = &vinum_conf.drive[driveno];
298 struct vinum_hdr *vhdr; /* buffer for header */
301 if (drive->state > drive_referenced) { /* real drive */
302 if (drive->state == drive_up) {
303 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffer */
304 CHECKALLOC(vhdr, "Can't allocate memory");
305 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
307 drive->lasterror = error;
309 vhdr->magic = VINUM_NOMAGIC; /* obliterate the magic, but leave the rest */
310 write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
314 free_drive(drive); /* close it and free resources */
315 save_config(); /* and save the updated configuration */
320 * Transfer drive data. Usually called from one of these defines;
321 * #define read_drive(a, b, c, d) driveio (a, b, c, d, B_READ)
322 * #define write_drive(a, b, c, d) driveio (a, b, c, d, B_WRITE)
324 * length and offset are in bytes, but must be multiples of sector
325 * size. The function *does not check* for this condition, and
326 * truncates ruthlessly.
327 * Return error number
330 driveio(struct drive *drive, char *buf, size_t length, off_t offset, int flag)
335 error = 0; /* to keep the compiler happy */
336 while (length) { /* divide into small enough blocks */
337 int len = min(length, MAXBSIZE); /* maximum block device transfer is MAXBSIZE */
339 bp = geteblk(len); /* get a buffer header */
342 bp->b_dev = drive->dev; /* device */
343 bp->b_blkno = offset / drive->sectorsize; /* block number */
344 bp->b_saveaddr = bp->b_data;
347 DEV_STRATEGY(bp, 0); /* initiate the transfer */
349 bp->b_data = bp->b_saveaddr;
350 bp->b_flags |= B_INVAL | B_AGE;
351 bp->b_ioflags &= ~BIO_ERROR;
355 length -= len; /* update pointers */
363 * Check a drive for a vinum header. If found,
364 * update the drive information. We come here
365 * with a partially populated drive structure
366 * which includes the device name.
368 * Return information on what we found.
370 * This function is called from two places: check_drive,
371 * which wants to find out whether the drive is a
372 * Vinum drive, and config_drive, which asserts that
373 * it is a vinum drive. In the first case, we don't
374 * print error messages (verbose==0), in the second
375 * we do (verbose==1).
377 enum drive_label_info
378 read_drive_label(struct drive *drive, int verbose)
381 int result; /* result of our search */
382 struct vinum_hdr *vhdr; /* and as header */
384 error = init_drive(drive, 0); /* find the drive */
385 if (error) /* find the drive */
386 return DL_CANT_OPEN; /* not ours */
388 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffers */
389 CHECKALLOC(vhdr, "Can't allocate memory");
391 drive->state = drive_up; /* be optimistic */
392 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
393 if (vhdr->magic == VINUM_MAGIC) { /* ours! */
394 if (drive->label.name[0] /* we have a name for this drive */
395 &&(strcmp(drive->label.name, vhdr->label.name))) { /* but it doesn't match the real name */
396 drive->lasterror = EINVAL;
397 result = DL_WRONG_DRIVE; /* it's the wrong drive */
398 drive->state = drive_unallocated; /* put it back, it's not ours */
402 * We copy the drive anyway so that we have
403 * the correct name in the drive info. This
404 * may not be the name specified
406 drive->label = vhdr->label; /* put in the label information */
407 } else if (vhdr->magic == VINUM_NOMAGIC) /* was ours, but we gave it away */
408 result = DL_DELETED_LABEL; /* and return the info */
410 result = DL_NOT_OURS; /* we could have it, but we don't yet */
411 Free(vhdr); /* that's all. */
416 * Check a drive for a vinum header. If found,
417 * read configuration information from the drive and
418 * incorporate the data into the configuration.
420 * Return drive number.
423 check_drive(char *devicename)
429 driveno = find_drive_by_dev(devicename, 1); /* if entry doesn't exist, create it */
430 drive = &vinum_conf.drive[driveno]; /* and get a pointer */
432 if (read_drive_label(drive, 0) == DL_OURS) { /* one of ours */
433 for (i = 0; i < vinum_conf.drives_allocated; i++) { /* see if the name already exists */
434 if ((i != driveno) /* not this drive */
435 &&(DRIVE[i].state != drive_unallocated) /* and it's allocated */
436 &&(strcmp(DRIVE[i].label.name,
437 DRIVE[driveno].label.name) == 0)) { /* and it has the same name */
438 struct drive *mydrive = &DRIVE[i];
440 if (mydrive->devicename[0] == '/') { /* we know a device name for it */
442 * set an error, but don't take the
443 * drive down: that would cause unneeded
446 drive->lasterror = EEXIST;
448 } else { /* it's just a place holder, */
451 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) { /* look at each subdisk */
452 if ((SD[sdno].driveno == i) /* it's pointing to this one, */
453 &&(SD[sdno].state != sd_unallocated)) { /* and it's a real subdisk */
454 SD[sdno].driveno = drive->driveno; /* point to the one we found */
455 update_sd_state(sdno); /* and update its state */
458 bzero(mydrive, sizeof(struct drive)); /* don't deallocate it, just remove it */
463 if (drive->lasterror == 0)
464 drive->lasterror = ENODEV;
466 drive->state = drive_down;
472 sappend(char *txt, char *s)
474 while ((*s++ = *txt++) != 0);
479 format_config(char *config, int len)
484 char *configend = &config[len];
488 /* First write the volume configuration */
489 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
492 vol = &vinum_conf.volume[i];
493 if ((vol->state > volume_uninit)
494 && (vol->name[0] != '\0')) { /* paranoia */
497 "volume %s state %s",
499 volume_state(vol->state));
501 s++; /* find the end */
502 if (vol->preferred_plex >= 0) /* preferences, */
505 " readpol prefer %s",
506 vinum_conf.plex[vol->preferred_plex].name);
508 s++; /* find the end */
509 s = sappend("\n", s);
513 /* Then the plex configuration */
514 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
517 plex = &vinum_conf.plex[i];
518 if ((plex->state > plex_referenced)
519 && (plex->name[0] != '\0')) { /* paranoia */
522 "plex name %s state %s org %s ",
524 plex_state(plex->state),
525 plex_org(plex->organization));
527 s++; /* find the end */
528 if (isstriped(plex)) {
532 (int) plex->stripesize);
534 s++; /* find the end */
536 if (plex->volno >= 0) /* we have a volume */
540 vinum_conf.volume[plex->volno].name);
542 s++; /* find the end */
543 for (j = 0; j < plex->subdisks; j++) {
547 vinum_conf.sd[plex->sdnos[j]].name);
549 s = sappend("\n", s);
553 /* And finally the subdisk configuration */
554 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
559 if ((sd->state != sd_referenced)
560 && (sd->state != sd_unallocated)
561 && (sd->name[0] != '\0')) { /* paranoia */
562 drivename = vinum_conf.drive[sd->driveno].label.name;
564 * XXX We've seen cases of dead subdisks
565 * which don't have a drive. If we let them
566 * through here, the drive name is null, so
567 * they get the drive named 'plex'.
569 * This is a breakage limiter, not a fix.
571 if (drivename[0] == '\0')
572 drivename = "*invalid*";
575 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
578 vinum_conf.plex[sd->plexno].name,
579 (unsigned long long) sd->sectors,
580 (unsigned long long) sd->driveoffset,
581 sd_state(sd->state));
583 s++; /* find the end */
588 (long long) sd->plexoffset);
590 snprintf(s, configend - s, " detached");
592 s++; /* find the end */
593 if (sd->flags & VF_RETRYERRORS) {
594 snprintf(s, configend - s, " retryerrors");
596 s++; /* find the end */
598 snprintf(s, configend - s, " \n");
600 s++; /* find the end */
603 if (s > &config[len - 2])
604 panic("vinum: configuration data overflow");
608 * issue a save config request to the dæmon. The actual work
609 * is done in process context by daemon_save_config
614 queue_daemon_request(daemonrq_saveconfig, (union daemoninfo) NULL);
618 * Write the configuration to all vinum slices. This
619 * is performed by the dæmon only
622 daemon_save_config(void)
625 int written_config; /* set when we first write the config to disk */
627 struct drive *drive; /* point to current drive info */
628 struct vinum_hdr *vhdr; /* and as header */
629 char *config; /* point to config data */
630 int wlabel_on; /* to set writing label on/off */
632 /* don't save the configuration while we're still working on it */
633 if (vinum_conf.flags & VF_CONFIGURING)
635 written_config = 0; /* no config written yet */
636 /* Build a volume header */
637 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* get space for the config data */
638 CHECKALLOC(vhdr, "Can't allocate config data");
639 vhdr->magic = VINUM_MAGIC; /* magic number */
640 vhdr->config_length = MAXCONFIG; /* length of following config info */
642 config = Malloc(MAXCONFIG); /* get space for the config data */
643 CHECKALLOC(config, "Can't allocate config data");
645 format_config(config, MAXCONFIG);
646 error = 0; /* no errors yet */
647 for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
648 drive = &vinum_conf.drive[driveno]; /* point to drive */
649 if (drive->state > drive_referenced) {
650 LOCKDRIVE(drive); /* don't let it change */
653 * First, do some drive consistency checks. Some
654 * of these are kludges, others require a process
655 * context and couldn't be done before
657 if ((drive->devicename[0] == '\0')
658 || (drive->label.name[0] == '\0')) {
660 free_drive(drive); /* get rid of it */
663 if (((drive->flags & VF_OPEN) == 0) /* drive not open */
664 &&(drive->state > drive_down)) { /* and it thinks it's not down */
666 set_drive_state(driveno, drive_down, setstate_force); /* tell it what's what */
669 if ((drive->state == drive_down) /* it's down */
670 &&(drive->flags & VF_OPEN)) { /* but open, */
672 close_drive(drive); /* close it */
673 } else if (drive->state > drive_down) {
674 getmicrotime(&drive->label.last_update); /* time of last update is now */
675 bcopy((char *) &drive->label, /* and the label info from the drive structure */
676 (char *) &vhdr->label,
677 sizeof(vhdr->label));
678 if ((drive->state != drive_unallocated)
679 && (drive->state != drive_referenced)) { /* and it's a real drive */
680 wlabel_on = 1; /* enable writing the label */
681 error = (*devsw(drive->dev)->d_ioctl) (drive->dev, /* make the label writeable */
683 (caddr_t) & wlabel_on,
687 error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
689 error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET); /* first config copy */
691 error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG); /* second copy */
692 wlabel_on = 0; /* enable writing the label */
694 error = (*devsw(drive->dev)->d_ioctl) (drive->dev, /* make the label non-writeable again */
696 (caddr_t) & wlabel_on,
702 "vinum: Can't write config to %s, error %d\n",
705 set_drive_state(drive->driveno, drive_down, setstate_force);
707 written_config = 1; /* we've written it on at least one drive */
709 } else /* not worth looking at, */
710 unlockdrive(drive); /* just unlock it again */
718 * Disk labels are a mess. The correct way to
719 * access them is with the DIOC[GSW]DINFO ioctls,
720 * but some programs, such as newfs, access the
721 * disk directly, so we have to write things
722 * there. We do this only on request. If a user
723 * request tries to read it directly, we fake up
728 * get_volume_label returns a label structure to lp, which
729 * is allocated by the caller
732 get_volume_label(char *name, int plexes, u_int64_t size, struct disklabel *lp)
734 bzero(lp, sizeof(struct disklabel));
736 strncpy(lp->d_typename, "vinum", sizeof(lp->d_typename));
737 lp->d_type = DTYPE_VINUM;
738 strncpy(lp->d_packname, name, min(sizeof(lp->d_packname), sizeof(name)));
739 lp->d_rpm = 14400 * plexes; /* to keep them guessing */
740 lp->d_interleave = 1;
744 * A Vinum volume has a single track with all
747 lp->d_secsize = DEV_BSIZE; /* bytes per sector */
748 lp->d_nsectors = size; /* data sectors per track */
749 lp->d_ntracks = 1; /* tracks per cylinder */
750 lp->d_ncylinders = 1; /* data cylinders per unit */
751 lp->d_secpercyl = size; /* data sectors per cylinder */
752 lp->d_secperunit = size; /* data sectors per unit */
754 lp->d_bbsize = BBSIZE;
757 lp->d_magic = DISKMAGIC;
758 lp->d_magic2 = DISKMAGIC;
761 * Set up partitions a, b and c to be identical
762 * and the size of the volume. a is UFS, b is
763 * swap, c is nothing.
765 lp->d_partitions[0].p_size = size;
766 lp->d_partitions[0].p_fsize = 1024;
767 lp->d_partitions[0].p_fstype = FS_BSDFFS; /* FreeBSD File System :-) */
768 lp->d_partitions[0].p_fsize = 1024; /* FS fragment size */
769 lp->d_partitions[0].p_frag = 8; /* and fragments per block */
770 lp->d_partitions[SWAP_PART].p_size = size;
771 lp->d_partitions[SWAP_PART].p_fstype = FS_SWAP; /* swap partition */
772 lp->d_partitions[LABEL_PART].p_size = size;
773 lp->d_npartitions = LABEL_PART + 1;
774 strncpy(lp->d_packname, name, min(sizeof(lp->d_packname), sizeof(name)));
775 lp->d_checksum = dkcksum(lp);
778 /* Write a volume label. This implements the VINUM_LABEL ioctl. */
780 write_volume_label(int volno)
782 struct disklabel *lp;
784 struct disklabel *dlp;
788 lp = (struct disklabel *) Malloc((sizeof(struct disklabel) + (DEV_BSIZE - 1)) & (DEV_BSIZE - 1));
792 if ((unsigned) (volno) >= (unsigned) vinum_conf.volumes_allocated) /* invalid volume */
795 vol = &VOL[volno]; /* volume in question */
796 if (vol->state <= volume_uninit) /* nothing there */
798 else if (vol->state < volume_up) /* not accessible */
799 return EIO; /* I/O error */
801 get_volume_label(vol->name, vol->plexes, vol->size, lp); /* get the label */
804 * Now write to disk. This code is derived from the
805 * system writedisklabel (), which does silly things
806 * like reading the label and refusing to write
807 * unless it's already there.
809 bp = geteblk((int) lp->d_secsize); /* get a buffer */
810 bp->b_dev = makedev(VINUM_CDEV_MAJOR, vol->volno); /* our own raw volume */
811 bp->b_blkno = LABELSECTOR * ((int) lp->d_secsize / DEV_BSIZE);
812 bp->b_bcount = lp->d_secsize;
813 bzero(bp->b_data, lp->d_secsize);
814 dlp = (struct disklabel *) bp->b_data;
816 bp->b_flags &= ~B_INVAL;
817 bp->b_iocmd = BIO_WRITE;
822 * vinumstrategy (bp);
824 * Negotiate with phk to get it fixed.
828 bp->b_flags |= B_INVAL | B_AGE;
829 bp->b_ioflags &= ~BIO_ERROR;
835 /* Look at all disks on the system for vinum slices */
837 vinum_scandisk(char *devicename[], int drives)
839 struct drive *volatile drive;
840 volatile int driveno;
841 int firstdrive; /* first drive in this list */
842 volatile int gooddrives; /* number of usable drives found */
843 int firsttime; /* set if we have never configured before */
845 char *config_text; /* read the config info from disk into here */
846 char *volatile cptr; /* pointer into config information */
847 char *eptr; /* end pointer into config information */
848 char *config_line; /* copy the config line to */
850 int *volatile drivelist; /* list of drive indices */
851 #define DRIVENAMELEN 64
852 #define DRIVEPARTS 35 /* max partitions per drive, excluding c */
853 char partname[DRIVENAMELEN]; /* for creating partition names */
855 status = 0; /* success indication */
856 vinum_conf.flags |= VF_READING_CONFIG; /* reading config from disk */
858 gooddrives = 0; /* number of usable drives found */
859 firstdrive = vinum_conf.drives_used; /* the first drive */
860 firsttime = vinum_conf.drives_used == 0; /* are we a virgin? */
862 /* allocate a drive pointer list */
863 drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
864 CHECKALLOC(drivelist, "Can't allocate memory");
866 /* Open all drives and find which was modified most recently */
867 for (driveno = 0; driveno < drives; driveno++) {
868 char part; /* UNIX partition */
870 int founddrive; /* flag when we find a vinum drive */
872 founddrive = 0; /* no vinum drive found yet on this spindle */
873 /* first try the partition table */
874 for (slice = 1; slice < 5; slice++)
875 for (part = 'a'; part < 'i'; part++) {
876 if (part != 'c') { /* don't do the c partition */
883 drive = check_drive(partname); /* try to open it */
884 if ((drive->lasterror != 0) /* didn't work, */
885 ||(drive->state != drive_up))
886 free_drive(drive); /* get rid of it */
887 else if (drive->flags & VF_CONFIGURED) /* already read this config, */
889 "vinum: already read config from %s\n", /* say so */
892 drivelist[gooddrives] = drive->driveno; /* keep the drive index */
893 drive->flags &= ~VF_NEWBORN; /* which is no longer newly born */
899 if (founddrive == 0) { /* didn't find anything, */
900 for (part = 'a'; part < 'i'; part++) /* try the compatibility partition */
901 if (part != 'c') { /* don't do the c partition */
902 snprintf(partname, /* /dev/sd0a */
907 drive = check_drive(partname); /* try to open it */
908 if ((drive->lasterror != 0) /* didn't work, */
909 ||(drive->state != drive_up))
910 free_drive(drive); /* get rid of it */
911 else if (drive->flags & VF_CONFIGURED) /* already read this config, */
913 "vinum: already read config from %s\n", /* say so */
916 drivelist[gooddrives] = drive->driveno; /* keep the drive index */
917 drive->flags &= ~VF_NEWBORN; /* which is no longer newly born */
924 if (gooddrives == 0) {
926 log(LOG_WARNING, "vinum: no drives found\n");
928 log(LOG_INFO, "vinum: no additional drives found\n");
932 * We now have at least one drive
933 * open. Sort them in order of config time
934 * and merge the config info with what we
937 qsort(drivelist, gooddrives, sizeof(int), drivecmp);
938 config_text = (char *) Malloc(MAXCONFIG * 2); /* allocate buffers */
939 CHECKALLOC(config_text, "Can't allocate memory");
940 config_line = (char *) Malloc(MAXCONFIGLINE * 2); /* allocate buffers */
941 CHECKALLOC(config_line, "Can't allocate memory");
942 for (driveno = 0; driveno < gooddrives; driveno++) { /* now include the config */
943 drive = &DRIVE[drivelist[driveno]]; /* point to the drive */
945 if (firsttime && (driveno == 0)) /* we've never configured before, */
946 log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
948 log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
950 if (drive->state == drive_up)
951 /* Read in both copies of the configuration information */
952 error = read_drive(drive, config_text, MAXCONFIG * 2, VINUM_CONFIG_OFFSET);
955 printf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
959 log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
960 free_drive(drive); /* give it back */
964 * At this point, check that the two copies
965 * are the same, and do something useful if
966 * not. In particular, consider which is
967 * newer, and what this means for the
968 * integrity of the data on the drive.
971 vinum_conf.drives_used++; /* another drive in use */
972 /* Parse the configuration, and add it to the global configuration */
973 for (cptr = config_text; *cptr != '\0';) { /* love this style(9) */
974 volatile int parse_status; /* return value from parse_config */
976 for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');) /* until the end of the line */
978 *eptr = '\0'; /* and delimit */
979 if (setjmp(command_fail) == 0) { /* come back here on error and continue */
980 parse_status = parse_config(config_line, &keyword_set, 1); /* parse the config line */
981 if (parse_status < 0) { /* error in config */
983 * This config should have been parsed in user
984 * space. If we run into problems here, something
985 * serious is afoot. Complain and let the user
986 * snarf the config to see what's wrong.
989 "vinum: Config error on %s, aborting integration\n",
991 free_drive(drive); /* give it back */
995 while (*cptr == '\n')
996 cptr++; /* skip to next line */
999 drive->flags |= VF_CONFIGURED; /* read this drive's configuration */
1004 vinum_conf.flags &= ~VF_READING_CONFIG; /* no longer reading from disk */
1006 printf("vinum: couldn't read configuration");
1008 updateconfig(VF_READING_CONFIG); /* update from disk config */
1013 * Compare the modification dates of the drives, for qsort.
1014 * Return 1 if a < b, 0 if a == b, 01 if a > b: in other
1015 * words, sort backwards.
1018 drivecmp(const void *va, const void *vb)
1020 const struct drive *a = &DRIVE[*(const int *) va];
1021 const struct drive *b = &DRIVE[*(const int *) vb];
1023 if ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
1024 && (a->label.last_update.tv_usec == b->label.last_update.tv_usec))
1026 else if ((a->label.last_update.tv_sec > b->label.last_update.tv_sec)
1027 || ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
1028 && (a->label.last_update.tv_usec > b->label.last_update.tv_usec)))
1033 /* Local Variables: */
1034 /* fill-column: 50 */