724e4f69ef16e29f3c9add84912d61e00c5bcf0e
[dragonfly.git] / sys / dev / raid / vinum / vinumio.c
1 /*-
2  * Copyright (c) 1997, 1998
3  *      Nan Yang Computer Services Limited.  All rights reserved.
4  *
5  *  This software is distributed under the so-called ``Berkeley
6  *  License'':
7  *
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.
23  *
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.
35  *
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 $
39  */
40
41 #include "vinumhdr.h"
42 #include "request.h"
43 #include <vm/vm_zone.h>
44
45 static char *sappend(char *txt, char *s);
46 static int drivecmp(const void *va, const void *vb);
47
48 /*
49  * Open the device associated with the drive, and set drive's vp.
50  * Return an error number
51  */
52 int
53 open_drive(struct drive *drive, struct proc *p, int verbose)
54 {
55     int devmajor;                                           /* major devs for disk device */
56     int devminor;                                           /* minor devs for disk device */
57     int unit;
58     char *dname;
59
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 */
64
65     /*
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.
72      */
73     dname = &drive->devicename[5];
74     drive->dev = NULL;                                      /* no device yet */
75
76     /* Find the device */
77     if (bcmp(dname, "ad", 2) == 0)                          /* IDE disk */
78         devmajor = 116;
79     else if (bcmp(dname, "wd", 2) == 0)                     /* IDE disk */
80         devmajor = 3;
81     else if (bcmp(dname, "da", 2) == 0)
82         devmajor = 13;
83     else if (bcmp(dname, "vn", 2) == 0)
84         devmajor = 43;
85     else if (bcmp(dname, "md", 2) == 0)
86         devmajor = 95;
87     else if (bcmp(dname, "vkd", 3) == 0) {
88         devmajor = 97;
89         dname += 1;
90     } else if (bcmp(dname, "amrd", 4) == 0) {
91         devmajor = 133;
92         dname += 2;
93     } else if (bcmp(dname, "mlxd", 4) == 0) {
94         devmajor = 131;
95         dname += 2;
96     } else if (bcmp(dname, "idad", 4) == 0) {
97         devmajor = 109;
98         dname += 2;
99     } else if (bcmp(dname, "twed", 4) == 0) {               /* 3ware raid */
100       devmajor = 147;
101       dname += 2;
102     } else if (bcmp(dname, "ar", 2) == 0) {
103         devmajor = 157;
104     } else
105         return ENODEV;
106     dname += 2;                                             /* point past */
107
108     /*
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.
115      */
116     unit = 0;
117     while ((*dname >= '0')                                  /* unit number */
118     &&(*dname <= '9')) {
119         unit = unit * 10 + *dname - '0';
120         dname++;
121     }
122
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 */
126             return ENODEV;
127         devminor = dkmakeminor(unit, dname[1] - '0' + 1, (dname[2] - 'a'));
128     } else {                                                /* compatibility partition */
129         if ((*dname < 'a') || (*dname > 'p'))               /* or invalid partition */
130             return ENODEV;
131         devminor = dkmakeminor(unit, 0, (dname[0] - 'a'));
132     }
133
134     /*
135      * Disallow partition c
136      */
137     if ((((devminor >> 17) & 0x08) | (devminor & 7)) == 2)
138         return ENOTTY;                                      /* not buying that */
139
140     drive->dev = udev2dev(makeudev(devmajor, devminor), 0);
141
142     if (drive->dev == NULL)
143         return ENODEV;
144
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);
148     else
149         drive->lasterror = ENOENT;
150
151     if (drive->lasterror != 0) {                            /* failed */
152         drive->state = drive_down;                          /* just force it down */
153         if (verbose)
154             log(LOG_WARNING,
155                 "vinum open_drive %s: failed with error %d\n",
156                 drive->devicename, drive->lasterror);
157     } else
158         drive->flags |= VF_OPEN;                            /* we're open now */
159
160     return drive->lasterror;
161 }
162
163 /*
164  * Set some variables in the drive struct
165  * in more convenient form.  Return error indication
166  */
167 int
168 set_drive_parms(struct drive *drive)
169 {
170     drive->blocksize = BLKDEV_IOSIZE;                       /* do we need this? */
171     drive->secsperblock = drive->blocksize                  /* number of sectors per block */
172         / drive->partinfo.media_blksize;
173
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;
178 #if VINUMDEBUG
179     if (debug & DEBUG_BIGDRIVE)                             /* pretend we're 100 times as big */
180         drive->label.drive_size *= 100;
181 #endif
182
183     /* number of sectors available for subdisks */
184     drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
185
186     /*
187      * Bug in 3.0 as of January 1998: you can open
188      * non-existent slices.  They have a length of 0.
189      */
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;
193         return ENOSPC;
194     }
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 */
199         return ENOSPC;
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;
207     return 0;
208 }
209
210 /*
211  * Initialize a drive: open the device and add device
212  * information
213  */
214 int
215 init_drive(struct drive *drive, int verbose)
216 {
217     if (drive->devicename[0] != '/') {
218         drive->lasterror = EINVAL;
219         log(LOG_ERR, "vinum: Can't open drive without drive name\n");
220         return EINVAL;
221     }
222     drive->lasterror = open_drive(drive, curproc, verbose); /* open the drive */
223     if (drive->lasterror)
224         return drive->lasterror;
225
226     drive->lasterror = dev_dioctl(
227         drive->dev,
228         DIOCGPART,
229         (caddr_t) & drive->partinfo,
230         FREAD,
231         proc0.p_ucred);
232     if (drive->lasterror) {
233         if (verbose)
234             log(LOG_WARNING,
235                 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
236                 drive->devicename,
237                 drive->lasterror);
238         close_drive(drive);
239         return drive->lasterror;
240     }
241     if (drive->partinfo.fstype != FS_VINUM &&
242         !kuuid_is_vinum(&drive->partinfo.fstype_uuid)
243     ) {  
244         drive->lasterror = EFTYPE;
245         if (verbose)
246             log(LOG_WARNING,
247                 "vinum open_drive %s: Wrong partition type for vinum\n",
248                 drive->devicename);
249         close_drive(drive);
250         return EFTYPE;
251     }
252     return set_drive_parms(drive);                          /* set various odds and ends */
253 }
254
255 /* Close a drive if it's open. */
256 void
257 close_drive(struct drive *drive)
258 {
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 */
264     unlockdrive(drive);
265 }
266
267 /*
268  * Real drive close code, called with drive already locked.
269  * We have also checked that the drive is open.  No errors.
270  */
271 void
272 close_locked_drive(struct drive *drive)
273 {
274     /*
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.
278      */
279     drive->lasterror = dev_dclose(drive->dev, 0, 0);
280     drive->flags &= ~VF_OPEN;                               /* no longer open */
281 }
282
283 /*
284  * Remove drive from the configuration.
285  * Caller must ensure that it isn't active.
286  */
287 void
288 remove_drive(int driveno)
289 {
290     struct drive *drive = &vinum_conf.drive[driveno];
291     struct vinum_hdr *vhdr;                                 /* buffer for header */
292     int error;
293
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);
299             if (error)
300                 drive->lasterror = error;
301             else {
302                 vhdr->magic = VINUM_NOMAGIC;                /* obliterate the magic, but leave the rest */
303                 write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
304             }
305             Free(vhdr);
306         }
307         free_drive(drive);                                  /* close it and free resources */
308         save_config();                                      /* and save the updated configuration */
309     }
310 }
311
312 /*
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)
316  *
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
321  */
322 int
323 driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
324 {
325     int error;
326     struct buf *bp;
327     caddr_t saveaddr;
328
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 */
332
333         bp = geteblk(len);                                  /* get a buffer header */
334         bp->b_cmd = cmd;
335         bp->b_bio1.bio_offset = offset;                     /* disk offset */
336         saveaddr = bp->b_data;
337         bp->b_data = buf;
338         bp->b_bcount = len;
339         dev_dstrategy(drive->dev, &bp->b_bio1);
340         error = biowait(bp);
341         bp->b_data = saveaddr;
342         bp->b_flags |= B_INVAL | B_AGE;
343         bp->b_flags &= ~B_ERROR;
344         brelse(bp);
345         if (error)
346             break;
347         length -= len;                                      /* update pointers */
348         buf += len;
349         offset += len;
350     }
351     return error;
352 }
353
354 /*
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.
359  *
360  * Return information on what we found.
361  *
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).
368  */
369 enum drive_label_info
370 read_drive_label(struct drive *drive, int verbose)
371 {
372     int error;
373     int result;                                             /* result of our search */
374     struct vinum_hdr *vhdr;                                 /* and as header */
375
376     error = init_drive(drive, 0);                           /* find the drive */
377     if (error)                                              /* find the drive */
378         return DL_CANT_OPEN;                                /* not ours */
379
380     vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);     /* allocate buffers */
381     CHECKALLOC(vhdr, "Can't allocate memory");
382
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 */
391         } else
392             result = DL_OURS;
393         /*
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
397          */
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 */
401     else
402         result = DL_NOT_OURS;                               /* we could have it, but we don't yet */
403     Free(vhdr);                                             /* that's all. */
404     return result;
405 }
406
407 /*
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.
411  *
412  * Return drive number.
413  */
414 struct drive *
415 check_drive(char *devicename)
416 {
417     int driveno;
418     int i;
419     struct drive *drive;
420
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 */
423
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];
431
432                 if (mydrive->devicename[0] == '/') {        /* we know a device name for it */
433                     /*
434                      * set an error, but don't take the
435                      * drive down: that would cause unneeded
436                      * error messages.
437                      */
438                     drive->lasterror = EEXIST;
439                     break;
440                 } else {                                    /* it's just a place holder, */
441                     int sdno;
442
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 */
448                         }
449                     }
450                     bzero(mydrive, sizeof(struct drive));   /* don't deallocate it, just remove it */
451                 }
452             }
453         }
454     } else {
455         if (drive->lasterror == 0)
456             drive->lasterror = ENODEV;
457         close_drive(drive);
458         drive->state = drive_down;
459     }
460     return drive;
461 }
462
463 static char *
464 sappend(char *txt, char *s)
465 {
466     while ((*s++ = *txt++) != 0);
467     return s - 1;
468 }
469
470 void
471 format_config(char *config, int len)
472 {
473     int i;
474     int j;
475     char *s = config;
476     char *configend = &config[len];
477
478     bzero(config, len);
479
480     /* First write the volume configuration */
481     for (i = 0; i < vinum_conf.volumes_allocated; i++) {
482         struct volume *vol;
483
484         vol = &vinum_conf.volume[i];
485         if ((vol->state > volume_uninit)
486             && (vol->name[0] != '\0')) {                    /* paranoia */
487             ksnprintf(s,
488                 configend - s,
489                 "volume %s state %s",
490                 vol->name,
491                 volume_state(vol->state));
492             while (*s)
493                 s++;                                        /* find the end */
494             if (vol->preferred_plex >= 0)                   /* preferences, */
495                 ksnprintf(s,
496                     configend - s,
497                     " readpol prefer %s",
498                     vinum_conf.plex[vol->preferred_plex].name);
499             while (*s)
500                 s++;                                        /* find the end */
501             s = sappend("\n", s);
502         }
503     }
504
505     /* Then the plex configuration */
506     for (i = 0; i < vinum_conf.plexes_allocated; i++) {
507         struct plex *plex;
508
509         plex = &vinum_conf.plex[i];
510         if ((plex->state > plex_referenced)
511             && (plex->name[0] != '\0')) {                   /* paranoia */
512             ksnprintf(s,
513                 configend - s,
514                 "plex name %s state %s org %s ",
515                 plex->name,
516                 plex_state(plex->state),
517                 plex_org(plex->organization));
518             while (*s)
519                 s++;                                        /* find the end */
520             if (isstriped(plex)) {
521                 ksnprintf(s,
522                     configend - s,
523                     "%ds ",
524                     (int) plex->stripesize);
525                 while (*s)
526                     s++;                                    /* find the end */
527             }
528             if (plex->volno >= 0)                           /* we have a volume */
529                 ksnprintf(s,
530                     configend - s,
531                     "vol %s ",
532                     vinum_conf.volume[plex->volno].name);
533             while (*s)
534                 s++;                                        /* find the end */
535             for (j = 0; j < plex->subdisks; j++) {
536                 ksnprintf(s,
537                     configend - s,
538                     " sd %s",
539                     vinum_conf.sd[plex->sdnos[j]].name);
540             }
541             s = sappend("\n", s);
542         }
543     }
544
545     /* And finally the subdisk configuration */
546     for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
547         struct sd *sd;
548         char *drivename;
549
550         sd = &SD[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;
555             /*
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'.
560              *
561              * This is a breakage limiter, not a fix.
562              */
563             if (drivename[0] == '\0')
564                 drivename = "*invalid*";
565             ksnprintf(s,
566                 configend - s,
567                 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
568                 sd->name,
569                 drivename,
570                 vinum_conf.plex[sd->plexno].name,
571                 (unsigned long long) sd->sectors,
572                 (unsigned long long) sd->driveoffset,
573                 sd_state(sd->state));
574             while (*s)
575                 s++;                                        /* find the end */
576             if (sd->plexno >= 0)
577                 ksnprintf(s,
578                     configend - s,
579                     " plexoffset %llds",
580                     (long long) sd->plexoffset);
581             else
582                 ksnprintf(s, configend - s, " detached");
583             while (*s)
584                 s++;                                        /* find the end */
585             if (sd->flags & VF_RETRYERRORS) {
586                 ksnprintf(s, configend - s, " retryerrors");
587                 while (*s)
588                     s++;                                    /* find the end */
589             }
590             ksnprintf(s, configend - s, " \n");
591             while (*s)
592                 s++;                                        /* find the end */
593         }
594     }
595     if (s > &config[len - 2])
596         panic("vinum: configuration data overflow");
597 }
598
599 /*
600  * issue a save config request to the dæmon.  The actual work
601  * is done in process context by daemon_save_config
602  */
603 void
604 save_config(void)
605 {
606     queue_daemon_request(daemonrq_saveconfig, (union daemoninfo) NULL);
607 }
608
609 /*
610  * Write the configuration to all vinum slices.  This
611  * is performed by the dæmon only
612  */
613 void
614 daemon_save_config(void)
615 {
616     int error;
617     int written_config;                                     /* set when we first write the config to disk */
618     int driveno;
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 */
623
624     /* don't save the configuration while we're still working on it */
625     if (vinum_conf.flags & VF_CONFIGURING)
626         return;
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 */
633
634     config = Malloc(MAXCONFIG);                             /* get space for the config data */
635     CHECKALLOC(config, "Can't allocate config data");
636
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 */
643
644             /*
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
648              */
649             if ((drive->devicename[0] == '\0')
650                 || (drive->label.name[0] == '\0')) {
651                 unlockdrive(drive);
652                 free_drive(drive);                          /* get rid of it */
653                 break;
654             }
655             if (((drive->flags & VF_OPEN) == 0)             /* drive not open */
656             &&(drive->state > drive_down)) {                /* and it thinks it's not down */
657                 unlockdrive(drive);
658                 set_drive_state(driveno, drive_down, setstate_force); /* tell it what's what */
659                 continue;
660             }
661             if ((drive->state == drive_down)                /* it's down */
662             &&(drive->flags & VF_OPEN)) {                   /* but open, */
663                 unlockdrive(drive);
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 */
674                         DIOCWLABEL,
675                         (caddr_t) & wlabel_on,
676                         FWRITE,
677                         proc0.p_ucred);
678                     if (error == 0)
679                         error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
680                     if (error == 0)
681                         error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET); /* first config copy */
682                     if (error == 0)
683                         error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG); /* second copy */
684                     wlabel_on = 0;                          /* enable writing the label */
685                     if (error == 0)
686                         error = dev_dioctl(drive->dev, /* make the label non-writeable again */
687                             DIOCWLABEL,
688                             (caddr_t) & wlabel_on,
689                             FWRITE,
690                             proc0.p_ucred);
691                     unlockdrive(drive);
692                     if (error) {
693                         log(LOG_ERR,
694                             "vinum: Can't write config to %s, error %d\n",
695                             drive->devicename,
696                             error);
697                         set_drive_state(drive->driveno, drive_down, setstate_force);
698                     } else
699                         written_config = 1;                 /* we've written it on at least one drive */
700                 }
701             } else                                          /* not worth looking at, */
702                 unlockdrive(drive);                         /* just unlock it again */
703         }
704     }
705     Free(vhdr);
706     Free(config);
707 }
708
709 /* Look at all disks on the system for vinum slices */
710 int
711 vinum_scandisk(char *devicename[], int drives)
712 {
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 */
718     int error;
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 */
723     volatile int status;
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 */
728
729     status = 0;                                             /* success indication */
730     vinum_conf.flags |= VF_READING_CONFIG;                  /* reading config from disk */
731
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? */
735
736     /* allocate a drive pointer list */
737     drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
738     CHECKALLOC(drivelist, "Can't allocate memory");
739
740     /* Open all drives and find which was modified most recently */
741     for (driveno = 0; driveno < drives; driveno++) {
742         char part;                                          /* UNIX partition */
743         int slice;
744         int founddrive;                                     /* flag when we find a vinum drive */
745         int has_slice = 0;
746         int has_part = 0;
747         char *tmp;
748
749         founddrive = 0;                                     /* no vinum drive found yet on this spindle */
750
751         /*
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
754          * that partition.
755          */
756         if ((tmp = rindex(devicename[driveno], '/')) == NULL)
757             tmp = devicename[driveno];
758         while (*tmp && (*tmp < '0' || *tmp > '9'))
759             ++tmp;
760         while (*tmp && *tmp >= '0' && *tmp <= '9')
761             ++tmp;
762         if (*tmp == 's')
763             has_slice = strtol(tmp + 1, &tmp, 0);
764         if (*tmp >= 'a' && *tmp <= 'p')
765             has_part = *tmp;
766
767         /*
768          * Scan slices if no slice was specified, only if no partition was
769          * specified.
770          */
771         if (has_slice == 0 && has_part == 0)
772         for (slice = 1; slice < 5; slice++) {
773             if (has_slice && slice != has_slice)
774                 continue;
775
776             for (part = 'a'; part < 'a' + MAXPARTITIONS; part++) {
777                 if (has_part && part != has_part)
778                     continue;
779                 if (part == 'c')
780                     continue;
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, */
788                     log(LOG_WARNING,
789                         "vinum: already read config from %s\n", /* say so */
790                         drive->label.name);
791                 else {
792                     drivelist[gooddrives] = drive->driveno;     /* keep the drive index */
793                     drive->flags &= ~VF_NEWBORN;            /* which is no longer newly born */
794                     gooddrives++;
795                     founddrive++;
796                 }
797             }
798         }
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)
802                     continue;
803                 if (part == 'c')
804                     continue;
805                 if (has_part) {
806                     ksnprintf(partname, DRIVENAMELEN,
807                             "%s", devicename[driveno]);
808                 } else {
809                     ksnprintf(partname, DRIVENAMELEN,
810                             "%s%c", devicename[driveno], part);
811                 }
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, */
817                     log(LOG_WARNING,
818                         "vinum: already read config from %s\n", /* say so */
819                         drive->label.name);
820                 else {
821                     drivelist[gooddrives] = drive->driveno;     /* keep the drive index */
822                     drive->flags &= ~VF_NEWBORN;            /* which is no longer newly born */
823                     gooddrives++;
824                 }
825             }
826         }
827     }
828
829     if (gooddrives == 0) {
830         if (firsttime)
831             log(LOG_WARNING, "vinum: no drives found\n");
832         else
833             log(LOG_INFO, "vinum: no additional drives found\n");
834         return ENOENT;
835     }
836     /*
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
840      * have already.
841      */
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 */
849
850         if (firsttime && (driveno == 0))                    /* we've never configured before, */
851             log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
852         else
853             log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
854
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);
858         else {
859             error = EIO;
860             kprintf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
861         }
862
863         if (error != 0) {
864             log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
865             free_drive(drive);                              /* give it back */
866             status = error;
867         }
868         /*
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.
874          */
875         else {
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 */
880
881                 for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');) /* until the end of the line */
882                     *eptr++ = *cptr++;
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 */
887                         /*
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.
892                          */
893                         log(LOG_ERR,
894                             "vinum: Config error on %s, aborting integration\n",
895                             drive->devicename);
896                         free_drive(drive);                  /* give it back */
897                         status = EINVAL;
898                     }
899                 }
900                 while (*cptr == '\n')
901                     cptr++;                                 /* skip to next line */
902             }
903         }
904         drive->flags |= VF_CONFIGURED;                      /* read this drive's configuration */
905     }
906
907     Free(config_line);
908     Free(config_text);
909     Free(drivelist);
910     vinum_conf.flags &= ~VF_READING_CONFIG;                 /* no longer reading from disk */
911     if (status != 0)
912         kprintf("vinum: couldn't read configuration");
913     else
914         updateconfig(VF_READING_CONFIG);                    /* update from disk config */
915     return status;
916 }
917
918 /*
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.
922  */
923 int
924 drivecmp(const void *va, const void *vb)
925 {
926     const struct drive *a = &DRIVE[*(const int *) va];
927     const struct drive *b = &DRIVE[*(const int *) vb];
928
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))
931         return 0;
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)))
935         return -1;
936     else
937         return 1;
938 }
939 /* Local Variables: */
940 /* fill-column: 50 */
941 /* End: */