Merge from vendor branch INTEL_ACPICA:
[games.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.20 2006/12/22 23:26:24 swildner 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, "amrd", 4) == 0) {
88         devmajor = 133;
89         dname += 2;
90     } else if (bcmp(dname, "mlxd", 4) == 0) {
91         devmajor = 131;
92         dname += 2;
93     } else if (bcmp(dname, "idad", 4) == 0) {
94         devmajor = 109;
95         dname += 2;
96     } else if (bcmp(dname, "twed", 4) == 0) {               /* 3ware raid */
97       devmajor = 147;
98       dname += 2;
99     } else if (bcmp(dname, "ar", 2) == 0) {
100         devmajor = 157;
101     } else
102         return ENODEV;
103     dname += 2;                                             /* point past */
104
105     /*
106      * Found the device.  We can expect one of
107      * two formats for the rest: a unit number,
108      * then either a partition letter for the
109      * compatiblity partition (e.g. h) or a
110      * slice ID and partition (e.g. s2e).
111      * Create a minor number for each of them.
112      */
113     unit = 0;
114     while ((*dname >= '0')                                  /* unit number */
115     &&(*dname <= '9')) {
116         unit = unit * 10 + *dname - '0';
117         dname++;
118     }
119
120     if (*dname == 's') {                                    /* slice */
121         if (((dname[1] < '1') || (dname[1] > '4'))          /* invalid slice */
122         ||((dname[2] < 'a') || (dname[2] > 'p')))           /* or invalid partition */
123             return ENODEV;
124         devminor = dkmakeminor(unit, dname[1] - '0' + 1, (dname[2] - 'a'));
125     } else {                                                /* compatibility partition */
126         if ((*dname < 'a') || (*dname > 'p'))               /* or invalid partition */
127             return ENODEV;
128         devminor = dkmakeminor(unit, 0, (dname[0] - 'a'));
129     }
130
131     /*
132      * Disallow partition c
133      */
134     if ((((devminor >> 17) & 0x08) | (devminor & 7)) == 2)
135         return ENOTTY;                                      /* not buying that */
136
137     drive->dev = udev2dev(makeudev(devmajor, devminor), 0);
138
139     if (drive->dev == NOCDEV)
140         return ENODEV;
141
142     drive->dev->si_iosize_max = DFLTPHYS;
143     if (dev_is_good(drive->dev))
144         drive->lasterror = dev_dopen(drive->dev, FWRITE, 0, proc0.p_ucred);
145     else
146         drive->lasterror = ENOENT;
147
148     if (drive->lasterror != 0) {                            /* failed */
149         drive->state = drive_down;                          /* just force it down */
150         if (verbose)
151             log(LOG_WARNING,
152                 "vinum open_drive %s: failed with error %d\n",
153                 drive->devicename, drive->lasterror);
154     } else
155         drive->flags |= VF_OPEN;                            /* we're open now */
156
157     return drive->lasterror;
158 }
159
160 /*
161  * Set some variables in the drive struct
162  * in more convenient form.  Return error indication
163  */
164 int
165 set_drive_parms(struct drive *drive)
166 {
167     drive->blocksize = BLKDEV_IOSIZE;                       /* do we need this? */
168     drive->secsperblock = drive->blocksize                  /* number of sectors per block */
169         / drive->partinfo.disklab->d_secsize;
170
171     /* Now update the label part */
172     bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN); /* put in host name */
173     getmicrotime(&drive->label.date_of_birth);              /* and current time */
174     drive->label.drive_size = ((u_int64_t) drive->partinfo.part->p_size) /* size of the drive in bytes */
175     *((u_int64_t) drive->partinfo.disklab->d_secsize);
176 #if VINUMDEBUG
177     if (debug & DEBUG_BIGDRIVE)                             /* pretend we're 100 times as big */
178         drive->label.drive_size *= 100;
179 #endif
180
181     /* number of sectors available for subdisks */
182     drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
183
184     /*
185      * Bug in 3.0 as of January 1998: you can open
186      * non-existent slices.  They have a length of 0.
187      */
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;
191         return ENOSPC;
192     }
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 */
197         return ENOSPC;
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;
205     return 0;
206 }
207
208 /*
209  * Initialize a drive: open the device and add device
210  * information
211  */
212 int
213 init_drive(struct drive *drive, int verbose)
214 {
215     if (drive->devicename[0] != '/') {
216         drive->lasterror = EINVAL;
217         log(LOG_ERR, "vinum: Can't open drive without drive name\n");
218         return EINVAL;
219     }
220     drive->lasterror = open_drive(drive, curproc, verbose); /* open the drive */
221     if (drive->lasterror)
222         return drive->lasterror;
223
224     drive->lasterror = dev_dioctl(
225         drive->dev,
226         DIOCGPART,
227         (caddr_t) & drive->partinfo,
228         FREAD,
229         proc0.p_ucred);
230     if (drive->lasterror) {
231         if (verbose)
232             log(LOG_WARNING,
233                 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
234                 drive->devicename,
235                 drive->lasterror);
236         close_drive(drive);
237         return drive->lasterror;
238     }
239     if (drive->partinfo.part->p_fstype != FS_VINUM) {       /* not Vinum */
240         drive->lasterror = EFTYPE;
241         if (verbose)
242             log(LOG_WARNING,
243                 "vinum open_drive %s: Wrong partition type for vinum\n",
244                 drive->devicename);
245         close_drive(drive);
246         return EFTYPE;
247     }
248     return set_drive_parms(drive);                          /* set various odds and ends */
249 }
250
251 /* Close a drive if it's open. */
252 void
253 close_drive(struct drive *drive)
254 {
255     LOCKDRIVE(drive);                                       /* keep the daemon out */
256     if (drive->flags & VF_OPEN)
257         close_locked_drive(drive);                          /* and close it */
258     if (drive->state > drive_down)                          /* if it's up */
259         drive->state = drive_down;                          /* make sure it's down */
260     unlockdrive(drive);
261 }
262
263 /*
264  * Real drive close code, called with drive already locked.
265  * We have also checked that the drive is open.  No errors.
266  */
267 void
268 close_locked_drive(struct drive *drive)
269 {
270     /*
271      * If we can't access the drive, we can't flush
272      * the queues, which spec_close() will try to
273      * do.  Get rid of them here first.
274      */
275     drive->lasterror = dev_dclose(drive->dev, 0, 0);
276     drive->flags &= ~VF_OPEN;                               /* no longer open */
277 }
278
279 /*
280  * Remove drive from the configuration.
281  * Caller must ensure that it isn't active.
282  */
283 void
284 remove_drive(int driveno)
285 {
286     struct drive *drive = &vinum_conf.drive[driveno];
287     struct vinum_hdr *vhdr;                                 /* buffer for header */
288     int error;
289
290     if (drive->state > drive_referenced) {                  /* real drive */
291         if (drive->state == drive_up) {
292             vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffer */
293             CHECKALLOC(vhdr, "Can't allocate memory");
294             error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
295             if (error)
296                 drive->lasterror = error;
297             else {
298                 vhdr->magic = VINUM_NOMAGIC;                /* obliterate the magic, but leave the rest */
299                 write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
300             }
301             Free(vhdr);
302         }
303         free_drive(drive);                                  /* close it and free resources */
304         save_config();                                      /* and save the updated configuration */
305     }
306 }
307
308 /*
309  * Transfer drive data.  Usually called from one of these defines;
310  * #define read_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_READ)
311  * #define write_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_WRITE)
312  *
313  * length and offset are in bytes, but must be multiples of sector
314  * size.  The function *does not check* for this condition, and
315  * truncates ruthlessly.
316  * Return error number
317  */
318 int
319 driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
320 {
321     int error;
322     struct buf *bp;
323     caddr_t saveaddr;
324
325     error = 0;                                              /* to keep the compiler happy */
326     while (length) {                                        /* divide into small enough blocks */
327         int len = min(length, MAXBSIZE);                    /* maximum block device transfer is MAXBSIZE */
328
329         bp = geteblk(len);                                  /* get a buffer header */
330         bp->b_cmd = cmd;
331         bp->b_bio1.bio_offset = offset;                     /* disk offset */
332         saveaddr = bp->b_data;
333         bp->b_data = buf;
334         bp->b_bcount = len;
335         dev_dstrategy(drive->dev, &bp->b_bio1);
336         error = biowait(bp);
337         bp->b_data = saveaddr;
338         bp->b_flags |= B_INVAL | B_AGE;
339         bp->b_flags &= ~B_ERROR;
340         brelse(bp);
341         if (error)
342             break;
343         length -= len;                                      /* update pointers */
344         buf += len;
345         offset += len;
346     }
347     return error;
348 }
349
350 /*
351  * Check a drive for a vinum header.  If found,
352  * update the drive information.  We come here
353  * with a partially populated drive structure
354  * which includes the device name.
355  *
356  * Return information on what we found.
357  *
358  * This function is called from two places: check_drive,
359  * which wants to find out whether the drive is a
360  * Vinum drive, and config_drive, which asserts that
361  * it is a vinum drive.  In the first case, we don't
362  * print error messages (verbose==0), in the second
363  * we do (verbose==1).
364  */
365 enum drive_label_info
366 read_drive_label(struct drive *drive, int verbose)
367 {
368     int error;
369     int result;                                             /* result of our search */
370     struct vinum_hdr *vhdr;                                 /* and as header */
371
372     error = init_drive(drive, 0);                           /* find the drive */
373     if (error)                                              /* find the drive */
374         return DL_CANT_OPEN;                                /* not ours */
375
376     vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);     /* allocate buffers */
377     CHECKALLOC(vhdr, "Can't allocate memory");
378
379     drive->state = drive_up;                                /* be optimistic */
380     error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
381     if (vhdr->magic == VINUM_MAGIC) {                       /* ours! */
382         if (drive->label.name[0]                            /* we have a name for this drive */
383         &&(strcmp(drive->label.name, vhdr->label.name))) {  /* but it doesn't match the real name */
384             drive->lasterror = EINVAL;
385             result = DL_WRONG_DRIVE;                        /* it's the wrong drive */
386             drive->state = drive_unallocated;               /* put it back, it's not ours */
387         } else
388             result = DL_OURS;
389         /*
390          * We copy the drive anyway so that we have
391          * the correct name in the drive info.  This
392          * may not be the name specified
393          */
394         drive->label = vhdr->label;                         /* put in the label information */
395     } else if (vhdr->magic == VINUM_NOMAGIC)                /* was ours, but we gave it away */
396         result = DL_DELETED_LABEL;                          /* and return the info */
397     else
398         result = DL_NOT_OURS;                               /* we could have it, but we don't yet */
399     Free(vhdr);                                             /* that's all. */
400     return result;
401 }
402
403 /*
404  * Check a drive for a vinum header.  If found,
405  * read configuration information from the drive and
406  * incorporate the data into the configuration.
407  *
408  * Return drive number.
409  */
410 struct drive *
411 check_drive(char *devicename)
412 {
413     int driveno;
414     int i;
415     struct drive *drive;
416
417     driveno = find_drive_by_dev(devicename, 1);             /* if entry doesn't exist, create it */
418     drive = &vinum_conf.drive[driveno];                     /* and get a pointer */
419
420     if (read_drive_label(drive, 0) == DL_OURS) {            /* one of ours */
421         for (i = 0; i < vinum_conf.drives_allocated; i++) { /* see if the name already exists */
422             if ((i != driveno)                              /* not this drive */
423             &&(DRIVE[i].state != drive_unallocated)         /* and it's allocated */
424             &&(strcmp(DRIVE[i].label.name,
425                         DRIVE[driveno].label.name) == 0)) { /* and it has the same name */
426                 struct drive *mydrive = &DRIVE[i];
427
428                 if (mydrive->devicename[0] == '/') {        /* we know a device name for it */
429                     /*
430                      * set an error, but don't take the
431                      * drive down: that would cause unneeded
432                      * error messages.
433                      */
434                     drive->lasterror = EEXIST;
435                     break;
436                 } else {                                    /* it's just a place holder, */
437                     int sdno;
438
439                     for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) { /* look at each subdisk */
440                         if ((SD[sdno].driveno == i)         /* it's pointing to this one, */
441                         &&(SD[sdno].state != sd_unallocated)) { /* and it's a real subdisk */
442                             SD[sdno].driveno = drive->driveno; /* point to the one we found */
443                             update_sd_state(sdno);          /* and update its state */
444                         }
445                     }
446                     bzero(mydrive, sizeof(struct drive));   /* don't deallocate it, just remove it */
447                 }
448             }
449         }
450     } else {
451         if (drive->lasterror == 0)
452             drive->lasterror = ENODEV;
453         close_drive(drive);
454         drive->state = drive_down;
455     }
456     return drive;
457 }
458
459 static char *
460 sappend(char *txt, char *s)
461 {
462     while ((*s++ = *txt++) != 0);
463     return s - 1;
464 }
465
466 void
467 format_config(char *config, int len)
468 {
469     int i;
470     int j;
471     char *s = config;
472     char *configend = &config[len];
473
474     bzero(config, len);
475
476     /* First write the volume configuration */
477     for (i = 0; i < vinum_conf.volumes_allocated; i++) {
478         struct volume *vol;
479
480         vol = &vinum_conf.volume[i];
481         if ((vol->state > volume_uninit)
482             && (vol->name[0] != '\0')) {                    /* paranoia */
483             ksnprintf(s,
484                 configend - s,
485                 "volume %s state %s",
486                 vol->name,
487                 volume_state(vol->state));
488             while (*s)
489                 s++;                                        /* find the end */
490             if (vol->preferred_plex >= 0)                   /* preferences, */
491                 ksnprintf(s,
492                     configend - s,
493                     " readpol prefer %s",
494                     vinum_conf.plex[vol->preferred_plex].name);
495             while (*s)
496                 s++;                                        /* find the end */
497             s = sappend("\n", s);
498         }
499     }
500
501     /* Then the plex configuration */
502     for (i = 0; i < vinum_conf.plexes_allocated; i++) {
503         struct plex *plex;
504
505         plex = &vinum_conf.plex[i];
506         if ((plex->state > plex_referenced)
507             && (plex->name[0] != '\0')) {                   /* paranoia */
508             ksnprintf(s,
509                 configend - s,
510                 "plex name %s state %s org %s ",
511                 plex->name,
512                 plex_state(plex->state),
513                 plex_org(plex->organization));
514             while (*s)
515                 s++;                                        /* find the end */
516             if (isstriped(plex)) {
517                 ksnprintf(s,
518                     configend - s,
519                     "%ds ",
520                     (int) plex->stripesize);
521                 while (*s)
522                     s++;                                    /* find the end */
523             }
524             if (plex->volno >= 0)                           /* we have a volume */
525                 ksnprintf(s,
526                     configend - s,
527                     "vol %s ",
528                     vinum_conf.volume[plex->volno].name);
529             while (*s)
530                 s++;                                        /* find the end */
531             for (j = 0; j < plex->subdisks; j++) {
532                 ksnprintf(s,
533                     configend - s,
534                     " sd %s",
535                     vinum_conf.sd[plex->sdnos[j]].name);
536             }
537             s = sappend("\n", s);
538         }
539     }
540
541     /* And finally the subdisk configuration */
542     for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
543         struct sd *sd;
544         char *drivename;
545
546         sd = &SD[i];
547         if ((sd->state != sd_referenced)
548             && (sd->state != sd_unallocated)
549             && (sd->name[0] != '\0')) {                     /* paranoia */
550             drivename = vinum_conf.drive[sd->driveno].label.name;
551             /*
552              * XXX We've seen cases of dead subdisks
553              * which don't have a drive.  If we let them
554              * through here, the drive name is null, so
555              * they get the drive named 'plex'.
556              *
557              * This is a breakage limiter, not a fix.
558              */
559             if (drivename[0] == '\0')
560                 drivename = "*invalid*";
561             ksnprintf(s,
562                 configend - s,
563                 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
564                 sd->name,
565                 drivename,
566                 vinum_conf.plex[sd->plexno].name,
567                 (unsigned long long) sd->sectors,
568                 (unsigned long long) sd->driveoffset,
569                 sd_state(sd->state));
570             while (*s)
571                 s++;                                        /* find the end */
572             if (sd->plexno >= 0)
573                 ksnprintf(s,
574                     configend - s,
575                     " plexoffset %llds",
576                     (long long) sd->plexoffset);
577             else
578                 ksnprintf(s, configend - s, " detached");
579             while (*s)
580                 s++;                                        /* find the end */
581             if (sd->flags & VF_RETRYERRORS) {
582                 ksnprintf(s, configend - s, " retryerrors");
583                 while (*s)
584                     s++;                                    /* find the end */
585             }
586             ksnprintf(s, configend - s, " \n");
587             while (*s)
588                 s++;                                        /* find the end */
589         }
590     }
591     if (s > &config[len - 2])
592         panic("vinum: configuration data overflow");
593 }
594
595 /*
596  * issue a save config request to the dæmon.  The actual work
597  * is done in process context by daemon_save_config
598  */
599 void
600 save_config(void)
601 {
602     queue_daemon_request(daemonrq_saveconfig, (union daemoninfo) NULL);
603 }
604
605 /*
606  * Write the configuration to all vinum slices.  This
607  * is performed by the dæmon only
608  */
609 void
610 daemon_save_config(void)
611 {
612     int error;
613     int written_config;                                     /* set when we first write the config to disk */
614     int driveno;
615     struct drive *drive;                                    /* point to current drive info */
616     struct vinum_hdr *vhdr;                                 /* and as header */
617     char *config;                                           /* point to config data */
618     int wlabel_on;                                          /* to set writing label on/off */
619
620     /* don't save the configuration while we're still working on it */
621     if (vinum_conf.flags & VF_CONFIGURING)
622         return;
623     written_config = 0;                                     /* no config written yet */
624     /* Build a volume header */
625     vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);     /* get space for the config data */
626     CHECKALLOC(vhdr, "Can't allocate config data");
627     vhdr->magic = VINUM_MAGIC;                              /* magic number */
628     vhdr->config_length = MAXCONFIG;                        /* length of following config info */
629
630     config = Malloc(MAXCONFIG);                             /* get space for the config data */
631     CHECKALLOC(config, "Can't allocate config data");
632
633     format_config(config, MAXCONFIG);
634     error = 0;                                              /* no errors yet */
635     for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
636         drive = &vinum_conf.drive[driveno];                 /* point to drive */
637         if (drive->state > drive_referenced) {
638             LOCKDRIVE(drive);                               /* don't let it change */
639
640             /*
641              * First, do some drive consistency checks.  Some
642              * of these are kludges, others require a process
643              * context and couldn't be done before
644              */
645             if ((drive->devicename[0] == '\0')
646                 || (drive->label.name[0] == '\0')) {
647                 unlockdrive(drive);
648                 free_drive(drive);                          /* get rid of it */
649                 break;
650             }
651             if (((drive->flags & VF_OPEN) == 0)             /* drive not open */
652             &&(drive->state > drive_down)) {                /* and it thinks it's not down */
653                 unlockdrive(drive);
654                 set_drive_state(driveno, drive_down, setstate_force); /* tell it what's what */
655                 continue;
656             }
657             if ((drive->state == drive_down)                /* it's down */
658             &&(drive->flags & VF_OPEN)) {                   /* but open, */
659                 unlockdrive(drive);
660                 close_drive(drive);                         /* close it */
661             } else if (drive->state > drive_down) {
662                 getmicrotime(&drive->label.last_update);    /* time of last update is now */
663                 bcopy((char *) &drive->label,               /* and the label info from the drive structure */
664                     (char *) &vhdr->label,
665                     sizeof(vhdr->label));
666                 if ((drive->state != drive_unallocated)
667                     && (drive->state != drive_referenced)) { /* and it's a real drive */
668                     wlabel_on = 1;                          /* enable writing the label */
669                     error = dev_dioctl(drive->dev, /* make the label writeable */
670                         DIOCWLABEL,
671                         (caddr_t) & wlabel_on,
672                         FWRITE,
673                         proc0.p_ucred);
674                     if (error == 0)
675                         error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
676                     if (error == 0)
677                         error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET); /* first config copy */
678                     if (error == 0)
679                         error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG); /* second copy */
680                     wlabel_on = 0;                          /* enable writing the label */
681                     if (error == 0)
682                         error = dev_dioctl(drive->dev, /* make the label non-writeable again */
683                             DIOCWLABEL,
684                             (caddr_t) & wlabel_on,
685                             FWRITE,
686                             proc0.p_ucred);
687                     unlockdrive(drive);
688                     if (error) {
689                         log(LOG_ERR,
690                             "vinum: Can't write config to %s, error %d\n",
691                             drive->devicename,
692                             error);
693                         set_drive_state(drive->driveno, drive_down, setstate_force);
694                     } else
695                         written_config = 1;                 /* we've written it on at least one drive */
696                 }
697             } else                                          /* not worth looking at, */
698                 unlockdrive(drive);                         /* just unlock it again */
699         }
700     }
701     Free(vhdr);
702     Free(config);
703 }
704
705 /*
706  * Disk labels are a mess.  The correct way to
707  * access them is with the DIOC[GSW]DINFO ioctls,
708  * but some programs, such as newfs, access the
709  * disk directly, so we have to write things
710  * there.  We do this only on request.  If a user
711  * request tries to read it directly, we fake up
712  * one on the fly.
713  */
714
715 /*
716  * get_volume_label returns a label structure to lp, which
717  * is allocated by the caller
718  */
719 void
720 get_volume_label(char *name, int plexes, u_int64_t size, struct disklabel *lp)
721 {
722     bzero(lp, sizeof(struct disklabel));
723
724     strncpy(lp->d_typename, "vinum", sizeof(lp->d_typename));
725     lp->d_type = DTYPE_VINUM;
726     strncpy(lp->d_packname, name, min(sizeof(lp->d_packname), sizeof(name)));
727     lp->d_rpm = 14400 * plexes;                             /* to keep them guessing */
728     lp->d_interleave = 1;
729     lp->d_flags = 0;
730
731     /*
732      * A Vinum volume has a single track with all
733      * its sectors.
734      */
735     lp->d_secsize = DEV_BSIZE;                              /* bytes per sector */
736     lp->d_nsectors = size;                                  /* data sectors per track */
737     lp->d_ntracks = 1;                                      /* tracks per cylinder */
738     lp->d_ncylinders = 1;                                   /* data cylinders per unit */
739     lp->d_secpercyl = size;                                 /* data sectors per cylinder */
740     lp->d_secperunit = size;                                /* data sectors per unit */
741
742     lp->d_bbsize = BBSIZE;
743     lp->d_sbsize = SBSIZE;
744
745     lp->d_magic = DISKMAGIC;
746     lp->d_magic2 = DISKMAGIC;
747
748     /*
749      * Set up partitions a, b and c to be identical
750      * and the size of the volume.  a is UFS, b is
751      * swap, c is nothing.
752      */
753     lp->d_partitions[0].p_size = size;
754     lp->d_partitions[0].p_fsize = 1024;
755     lp->d_partitions[0].p_fstype = FS_BSDFFS;               /* FreeBSD File System :-) */
756     lp->d_partitions[0].p_fsize = 1024;                     /* FS fragment size */
757     lp->d_partitions[0].p_frag = 8;                         /* and fragments per block */
758     lp->d_partitions[SWAP_PART].p_size = size;
759     lp->d_partitions[SWAP_PART].p_fstype = FS_SWAP;         /* swap partition */
760     lp->d_partitions[LABEL_PART].p_size = size;
761     lp->d_npartitions = LABEL_PART + 1;
762     strncpy(lp->d_packname, name, min(sizeof(lp->d_packname), sizeof(name)));
763     lp->d_checksum = dkcksum(lp);
764 }
765
766 /* Write a volume label.  This implements the VINUM_LABEL ioctl. */
767 int
768 write_volume_label(int volno)
769 {
770     struct disklabel *lp;
771     struct buf *bp;
772     struct disklabel *dlp;
773     struct volume *vol;
774     int error;
775     cdev_t dev;
776
777     lp = (struct disklabel *) Malloc((sizeof(struct disklabel) + (DEV_BSIZE - 1)) & (DEV_BSIZE - 1));
778     if (lp == 0)
779         return ENOMEM;
780
781     if ((unsigned) (volno) >= (unsigned) vinum_conf.volumes_allocated) /* invalid volume */
782         return ENOENT;
783
784     vol = &VOL[volno];                                      /* volume in question */
785     if (vol->state <= volume_uninit)                        /* nothing there */
786         return ENXIO;
787     else if (vol->state < volume_up)                        /* not accessible */
788         return EIO;                                         /* I/O error */
789
790     get_volume_label(vol->name, vol->plexes, vol->size, lp); /* get the label */
791
792     /*
793      * Now write to disk.  This code is derived from the
794      * system writedisklabel (), which does silly things
795      * like reading the label and refusing to write
796      * unless it's already there.
797      */
798     bp = geteblk((int) lp->d_secsize);                      /* get a buffer */
799     dev = make_adhoc_dev(&vinum_ops, vol->volno);
800     bp->b_bio1.bio_offset = (off_t)LABELSECTOR * lp->d_secsize;
801     bp->b_bcount = lp->d_secsize;
802     bzero(bp->b_data, lp->d_secsize);
803     dlp = (struct disklabel *) bp->b_data;
804     *dlp = *lp;
805     bp->b_flags &= ~B_INVAL;
806     bp->b_cmd = BUF_CMD_WRITE;
807
808     /*
809      * This should read:
810      *
811      *       vinumstrategy (bp);
812      *
813      * Negotiate with phk to get it fixed.
814      */
815     dev_dstrategy(dev, &bp->b_bio1);
816     error = biowait(bp);
817     bp->b_flags |= B_INVAL | B_AGE;
818     bp->b_flags &= ~B_ERROR;
819     brelse(bp);
820     return error;
821 }
822
823 /* Look at all disks on the system for vinum slices */
824 int
825 vinum_scandisk(char *devicename[], int drives)
826 {
827     struct drive *volatile drive;
828     volatile int driveno;
829     int firstdrive;                                         /* first drive in this list */
830     volatile int gooddrives;                                /* number of usable drives found */
831     int firsttime;                                          /* set if we have never configured before */
832     int error;
833     char *config_text;                                      /* read the config info from disk into here */
834     char *volatile cptr;                                    /* pointer into config information */
835     char *eptr;                                             /* end pointer into config information */
836     char *config_line;                                      /* copy the config line to */
837     volatile int status;
838     int *volatile drivelist;                                /* list of drive indices */
839 #define DRIVENAMELEN 64
840 #define DRIVEPARTS   35                                     /* max partitions per drive, excluding c */
841     char partname[DRIVENAMELEN];                            /* for creating partition names */
842
843     status = 0;                                             /* success indication */
844     vinum_conf.flags |= VF_READING_CONFIG;                  /* reading config from disk */
845
846     gooddrives = 0;                                         /* number of usable drives found */
847     firstdrive = vinum_conf.drives_used;                    /* the first drive */
848     firsttime = vinum_conf.drives_used == 0;                /* are we a virgin? */
849
850     /* allocate a drive pointer list */
851     drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
852     CHECKALLOC(drivelist, "Can't allocate memory");
853
854     /* Open all drives and find which was modified most recently */
855     for (driveno = 0; driveno < drives; driveno++) {
856         char part;                                          /* UNIX partition */
857         int slice;
858         int founddrive;                                     /* flag when we find a vinum drive */
859         int has_slice = 0;
860         int has_part = 0;
861         char *tmp;
862
863         founddrive = 0;                                     /* no vinum drive found yet on this spindle */
864
865         /*
866          * If the device path contains a slice we do not try to tack on
867          * another slice.  If the device path has a partition we only check
868          * that partition.
869          */
870         if ((tmp = rindex(devicename[driveno], '/')) == NULL)
871             tmp = devicename[driveno];
872         while (*tmp && (*tmp < '0' || *tmp > '9'))
873             ++tmp;
874         while (*tmp && *tmp >= '0' && *tmp <= '9')
875             ++tmp;
876         if (*tmp == 's')
877             has_slice = strtol(tmp + 1, &tmp, 0);
878         if (*tmp >= 'a' && *tmp <= 'p')
879             has_part = *tmp;
880
881         /*
882          * Scan slices if no slice was specified, only if no partition was
883          * specified.
884          */
885         if (has_slice == 0 && has_part == 0)
886         for (slice = 1; slice < 5; slice++) {
887             if (has_slice && slice != has_slice)
888                 continue;
889
890             for (part = 'a'; part <= 'p'; part++) {
891                 if (has_part && part != has_part)
892                     continue;
893                 if (part == 'c')
894                     continue;
895                 ksnprintf(partname, DRIVENAMELEN,
896                         "%ss%d%c", devicename[driveno], slice, part);
897                 drive = check_drive(partname);      /* try to open it */
898                 if ((drive->lasterror != 0)                 /* didn't work, */
899                     ||(drive->state != drive_up))
900                     free_drive(drive);              /* get rid of it */
901                 else if (drive->flags & VF_CONFIGURED)  /* already read this config, */
902                     log(LOG_WARNING,
903                         "vinum: already read config from %s\n", /* say so */
904                         drive->label.name);
905                 else {
906                     drivelist[gooddrives] = drive->driveno;     /* keep the drive index */
907                     drive->flags &= ~VF_NEWBORN;            /* which is no longer newly born */
908                     gooddrives++;
909                     founddrive++;
910                 }
911             }
912         }
913         if (founddrive == 0 && has_slice == 0) {            /* didn't find anything, */
914             for (part = 'a'; part <= 'p'; part++) {         /* try the compatibility partition */
915                 if (has_part && has_part != part)
916                     continue;
917                 if (part == 'c')
918                     continue;
919                 if (has_part) {
920                     ksnprintf(partname, DRIVENAMELEN,
921                             "%s", devicename[driveno]);
922                 } else {
923                     ksnprintf(partname, DRIVENAMELEN,
924                             "%s%c", devicename[driveno], part);
925                 }
926                 drive = check_drive(partname);      /* try to open it */
927                 if ((drive->lasterror != 0)                 /* didn't work, */
928                 ||(drive->state != drive_up))
929                     free_drive(drive);              /* get rid of it */
930                 else if (drive->flags & VF_CONFIGURED)  /* already read this config, */
931                     log(LOG_WARNING,
932                         "vinum: already read config from %s\n", /* say so */
933                         drive->label.name);
934                 else {
935                     drivelist[gooddrives] = drive->driveno;     /* keep the drive index */
936                     drive->flags &= ~VF_NEWBORN;            /* which is no longer newly born */
937                     gooddrives++;
938                 }
939             }
940         }
941     }
942
943     if (gooddrives == 0) {
944         if (firsttime)
945             log(LOG_WARNING, "vinum: no drives found\n");
946         else
947             log(LOG_INFO, "vinum: no additional drives found\n");
948         return ENOENT;
949     }
950     /*
951      * We now have at least one drive
952      * open.  Sort them in order of config time
953      * and merge the config info with what we
954      * have already.
955      */
956     kqsort(drivelist, gooddrives, sizeof(int), drivecmp);
957     config_text = (char *) Malloc(MAXCONFIG * 2);           /* allocate buffers */
958     CHECKALLOC(config_text, "Can't allocate memory");
959     config_line = (char *) Malloc(MAXCONFIGLINE * 2);       /* allocate buffers */
960     CHECKALLOC(config_line, "Can't allocate memory");
961     for (driveno = 0; driveno < gooddrives; driveno++) {    /* now include the config */
962         drive = &DRIVE[drivelist[driveno]];                 /* point to the drive */
963
964         if (firsttime && (driveno == 0))                    /* we've never configured before, */
965             log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
966         else
967             log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
968
969         if (drive->state == drive_up)
970             /* Read in both copies of the configuration information */
971             error = read_drive(drive, config_text, MAXCONFIG * 2, VINUM_CONFIG_OFFSET);
972         else {
973             error = EIO;
974             kprintf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
975         }
976
977         if (error != 0) {
978             log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
979             free_drive(drive);                              /* give it back */
980             status = error;
981         }
982         /*
983          * At this point, check that the two copies
984          * are the same, and do something useful if
985          * not.  In particular, consider which is
986          * newer, and what this means for the
987          * integrity of the data on the drive.
988          */
989         else {
990             vinum_conf.drives_used++;                       /* another drive in use */
991             /* Parse the configuration, and add it to the global configuration */
992             for (cptr = config_text; *cptr != '\0';) {      /* love this style(9) */
993                 volatile int parse_status;                  /* return value from parse_config */
994
995                 for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');) /* until the end of the line */
996                     *eptr++ = *cptr++;
997                 *eptr = '\0';                               /* and delimit */
998                 if (setjmp(command_fail) == 0) {            /* come back here on error and continue */
999                     parse_status = parse_config(config_line, &keyword_set, 1); /* parse the config line */
1000                     if (parse_status < 0) {                 /* error in config */
1001                         /*
1002                            * This config should have been parsed in user
1003                            * space.  If we run into problems here, something
1004                            * serious is afoot.  Complain and let the user
1005                            * snarf the config to see what's wrong.
1006                          */
1007                         log(LOG_ERR,
1008                             "vinum: Config error on %s, aborting integration\n",
1009                             drive->devicename);
1010                         free_drive(drive);                  /* give it back */
1011                         status = EINVAL;
1012                     }
1013                 }
1014                 while (*cptr == '\n')
1015                     cptr++;                                 /* skip to next line */
1016             }
1017         }
1018         drive->flags |= VF_CONFIGURED;                      /* read this drive's configuration */
1019     }
1020
1021     Free(config_line);
1022     Free(config_text);
1023     Free(drivelist);
1024     vinum_conf.flags &= ~VF_READING_CONFIG;                 /* no longer reading from disk */
1025     if (status != 0)
1026         kprintf("vinum: couldn't read configuration");
1027     else
1028         updateconfig(VF_READING_CONFIG);                    /* update from disk config */
1029     return status;
1030 }
1031
1032 /*
1033  * Compare the modification dates of the drives, for qsort.
1034  * Return 1 if a < b, 0 if a == b, 01 if a > b: in other
1035  * words, sort backwards.
1036  */
1037 int
1038 drivecmp(const void *va, const void *vb)
1039 {
1040     const struct drive *a = &DRIVE[*(const int *) va];
1041     const struct drive *b = &DRIVE[*(const int *) vb];
1042
1043     if ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
1044         && (a->label.last_update.tv_usec == b->label.last_update.tv_usec))
1045         return 0;
1046     else if ((a->label.last_update.tv_sec > b->label.last_update.tv_sec)
1047             || ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
1048             && (a->label.last_update.tv_usec > b->label.last_update.tv_usec)))
1049         return -1;
1050     else
1051         return 1;
1052 }
1053 /* Local Variables: */
1054 /* fill-column: 50 */
1055 /* End: */