xref: /dragonfly/sys/dev/raid/vinum/vinumio.c (revision a8ca8ac6)
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.31 2008/06/05 18:06:31 swildner Exp $
39  */
40 
41 #include "vinumhdr.h"
42 #include "request.h"
43 #include <vm/vm_zone.h>
44 #include <sys/nlookup.h>
45 
46 static char *sappend(char *txt, char *s);
47 static int drivecmp(const void *va, const void *vb);
48 
49 /*
50  * Open the device associated with the drive, and set drive's vp.
51  * Return an error number
52  */
53 int
54 open_drive(struct drive *drive, struct proc *p, int verbose)
55 {
56     struct nlookupdata nd;
57     int error;
58     const char *dname;
59 
60     /*
61      * Fail if already open
62      */
63     if (drive->flags & VF_OPEN)
64 	return EBUSY;
65     dname = drive->devicename;
66 
67     if (rootdev) {
68 	/*
69 	 * Open via filesystem (future)
70 	 */
71 	error = nlookup_init(&nd, drive->devicename, UIO_SYSSPACE, NLC_FOLLOW);
72 	if (error)
73 	    return error;
74 	error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
75 	drive->vp = nd.nl_open_vp;
76 	nd.nl_open_vp = NULL;
77 	nlookup_done(&nd);
78     } else {
79 	/*
80 	 * Open via synthesized vnode backed by disk device
81 	 */
82 	error = vn_opendisk(drive->devicename, FREAD|FWRITE, &drive->vp);
83 	if (error)
84 	    return error;
85     }
86 
87     if (error == 0 && drive->vp == NULL)
88 	error = ENODEV;
89 
90     /*
91      * A huge amount of pollution all over vinum requires that our low
92      * level drive be a device.
93      */
94     if (error == 0 && drive->vp->v_type != VCHR) {
95 	vn_close(drive->vp, FREAD|FWRITE);
96 	drive->vp = NULL;
97 	error = ENODEV;
98     }
99     if (error) {
100 	drive->state = drive_down;
101 	if (verbose) {
102 	    log(LOG_WARNING,
103 		"vinum open_drive %s: failed with error %d\n",
104 		drive->devicename, error);
105 	}
106     } else {
107 	drive->dev = drive->vp->v_rdev;
108 	drive->flags |= VF_OPEN;
109     }
110     drive->lasterror = error;
111     return error;
112 }
113 
114 /*
115  * Set some variables in the drive struct
116  * in more convenient form.  Return error indication
117  */
118 int
119 set_drive_parms(struct drive *drive)
120 {
121     drive->blocksize = BLKDEV_IOSIZE;			    /* do we need this? */
122     drive->secsperblock = drive->blocksize		    /* number of sectors per block */
123 	/ drive->partinfo.media_blksize;
124 
125     /* Now update the label part */
126     bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN); /* put in host name */
127     getmicrotime(&drive->label.date_of_birth);		    /* and current time */
128     drive->label.drive_size = drive->partinfo.media_size;
129 #if VINUMDEBUG
130     if (debug & DEBUG_BIGDRIVE)				    /* pretend we're 100 times as big */
131 	drive->label.drive_size *= 100;
132 #endif
133 
134     /* number of sectors available for subdisks */
135     drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
136 
137     /*
138      * Bug in 3.0 as of January 1998: you can open
139      * non-existent slices.  They have a length of 0.
140      */
141     if (drive->label.drive_size < MINVINUMSLICE) {	    /* too small to worry about */
142 	set_drive_state(drive->driveno, drive_down, setstate_force);
143 	drive->lasterror = ENOSPC;
144 	return ENOSPC;
145     }
146     drive->freelist_size = INITIAL_DRIVE_FREELIST;	    /* initial number of entries */
147     drive->freelist = (struct drive_freelist *)
148 	Malloc(INITIAL_DRIVE_FREELIST * sizeof(struct drive_freelist));
149     if (drive->freelist == NULL)			    /* can't malloc, dammit */
150 	return ENOSPC;
151     drive->freelist_entries = 1;			    /* just (almost) the complete drive */
152     drive->freelist[0].offset = DATASTART;		    /* starts here */
153     drive->freelist[0].sectors = (drive->label.drive_size >> DEV_BSHIFT) - DATASTART; /* and it's this long */
154     if (drive->label.name[0] != '\0')			    /* got a name */
155 	set_drive_state(drive->driveno, drive_up, setstate_force); /* our drive is accessible */
156     else						    /* we know about it, but that's all */
157 	drive->state = drive_referenced;
158     return 0;
159 }
160 
161 /*
162  * Initialize a drive: open the device and add device
163  * information
164  */
165 int
166 init_drive(struct drive *drive, int verbose)
167 {
168     if (drive->devicename[0] != '/') {
169 	drive->lasterror = EINVAL;
170 	log(LOG_ERR, "vinum: Can't open drive without drive name (%s)\n",
171 	    drive->devicename);
172 	return EINVAL;
173     }
174     drive->lasterror = open_drive(drive, curproc, verbose); /* open the drive */
175     if (drive->lasterror)
176 	return drive->lasterror;
177 
178     drive->lasterror = VOP_IOCTL(drive->vp, DIOCGPART,
179 				 (caddr_t)&drive->partinfo, FREAD|FWRITE,
180 				 proc0.p_ucred, NULL);
181     if (drive->lasterror) {
182 	if (verbose)
183 	    log(LOG_WARNING,
184 		"vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
185 		drive->devicename,
186 		drive->lasterror);
187 	close_drive(drive);
188 	return drive->lasterror;
189     }
190     if (drive->partinfo.fstype != FS_VINUM &&
191 	!kuuid_is_vinum(&drive->partinfo.fstype_uuid)
192     ) {
193 	drive->lasterror = EFTYPE;
194 	if (verbose)
195 	    log(LOG_WARNING,
196 		"vinum open_drive %s: Wrong partition type for vinum\n",
197 		drive->devicename);
198 	close_drive(drive);
199 	return EFTYPE;
200     }
201     return set_drive_parms(drive);			    /* set various odds and ends */
202 }
203 
204 /* Close a drive if it's open. */
205 void
206 close_drive(struct drive *drive)
207 {
208     LOCKDRIVE(drive);					    /* keep the daemon out */
209     if (drive->flags & VF_OPEN)
210 	close_locked_drive(drive);			    /* and close it */
211     if (drive->state > drive_down)			    /* if it's up */
212 	drive->state = drive_down;			    /* make sure it's down */
213     unlockdrive(drive);
214 }
215 
216 /*
217  * Real drive close code, called with drive already locked.
218  * We have also checked that the drive is open.  No errors.
219  */
220 void
221 close_locked_drive(struct drive *drive)
222 {
223     /*
224      * If we can't access the drive, we can't flush
225      * the queues, which spec_close() will try to
226      * do.  Get rid of them here first.
227      */
228     if (drive->vp) {
229 	drive->lasterror = vn_close(drive->vp, FREAD|FWRITE);
230 	drive->vp = NULL;
231     }
232     drive->flags &= ~VF_OPEN;
233 }
234 
235 /*
236  * Remove drive from the configuration.
237  * Caller must ensure that it isn't active.
238  */
239 void
240 remove_drive(int driveno)
241 {
242     struct drive *drive = &vinum_conf.drive[driveno];
243     struct vinum_hdr *vhdr;				    /* buffer for header */
244     int error;
245 
246     if (drive->state > drive_referenced) {		    /* real drive */
247 	if (drive->state == drive_up) {
248 	    vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);	/* allocate buffer */
249 	    CHECKALLOC(vhdr, "Can't allocate memory");
250 	    error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
251 	    if (error)
252 		drive->lasterror = error;
253 	    else {
254 		vhdr->magic = VINUM_NOMAGIC;		    /* obliterate the magic, but leave the rest */
255 		write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
256 	    }
257 	    Free(vhdr);
258 	}
259 	free_drive(drive);				    /* close it and free resources */
260 	save_config();					    /* and save the updated configuration */
261     }
262 }
263 
264 /*
265  * Transfer drive data.  Usually called from one of these defines;
266  * #define read_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_READ)
267  * #define write_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_WRITE)
268  *
269  * length and offset are in bytes, but must be multiples of sector
270  * size.  The function *does not check* for this condition, and
271  * truncates ruthlessly.
272  * Return error number
273  */
274 int
275 driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
276 {
277     int error;
278     struct buf *bp;
279     caddr_t saveaddr;
280 
281     error = 0;						    /* to keep the compiler happy */
282     while (length) {					    /* divide into small enough blocks */
283 	int len = umin(length, MAXBSIZE);		    /* maximum block device transfer is MAXBSIZE */
284 
285 	bp = geteblk(len);				    /* get a buffer header */
286 	bp->b_cmd = cmd;
287 	bp->b_bio1.bio_offset = offset;			    /* disk offset */
288 	bp->b_bio1.bio_done = biodone_sync;
289 	bp->b_bio1.bio_flags |= BIO_SYNC;
290 	saveaddr = bp->b_data;
291 	bp->b_data = buf;
292 	bp->b_bcount = len;
293 	vn_strategy(drive->vp, &bp->b_bio1);
294 	error = biowait(&bp->b_bio1, (cmd == BUF_CMD_READ ? "drvrd" : "drvwr"));
295 	bp->b_data = saveaddr;
296 	bp->b_flags |= B_INVAL | B_AGE;
297 	bp->b_flags &= ~B_ERROR;
298 	brelse(bp);
299 	if (error)
300 	    break;
301 	length -= len;					    /* update pointers */
302 	buf += len;
303 	offset += len;
304     }
305     return error;
306 }
307 
308 /*
309  * Check a drive for a vinum header.  If found,
310  * update the drive information.  We come here
311  * with a partially populated drive structure
312  * which includes the device name.
313  *
314  * Return information on what we found.
315  *
316  * This function is called from two places: check_drive,
317  * which wants to find out whether the drive is a
318  * Vinum drive, and config_drive, which asserts that
319  * it is a vinum drive.  In the first case, we don't
320  * print error messages (verbose==0), in the second
321  * we do (verbose==1).
322  */
323 enum drive_label_info
324 read_drive_label(struct drive *drive, int verbose)
325 {
326     int error;
327     int result;
328     struct vinum_hdr *vhdr;
329 
330     error = init_drive(drive, verbose);			    /* find the drive */
331     if (error)						    /* find the drive */
332 	return DL_CANT_OPEN;				    /* not ours */
333 
334     vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);	    /* allocate buffers */
335     CHECKALLOC(vhdr, "Can't allocate memory");
336 
337     drive->state = drive_up;				    /* be optimistic */
338     error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
339     if (vhdr->magic == VINUM_MAGIC) {			    /* ours! */
340 	if (drive->label.name[0]			    /* we have a name for this drive */
341 	&&(strcmp(drive->label.name, vhdr->label.name))) {  /* but it doesn't match the real name */
342 	    drive->lasterror = EINVAL;
343 	    result = DL_WRONG_DRIVE;			    /* it's the wrong drive */
344 	    drive->state = drive_unallocated;		    /* put it back, it's not ours */
345 	} else
346 	    result = DL_OURS;
347 	/*
348 	 * We copy the drive anyway so that we have
349 	 * the correct name in the drive info.  This
350 	 * may not be the name specified
351 	 */
352 	drive->label = vhdr->label;			    /* put in the label information */
353     } else if (vhdr->magic == VINUM_NOMAGIC)		    /* was ours, but we gave it away */
354 	result = DL_DELETED_LABEL;			    /* and return the info */
355     else
356 	result = DL_NOT_OURS;				    /* we could have it, but we don't yet */
357     Free(vhdr);						    /* that's all. */
358     return result;
359 }
360 
361 /*
362  * Check a drive for a vinum header.  If found,
363  * read configuration information from the drive and
364  * incorporate the data into the configuration.
365  *
366  * Return drive number.
367  */
368 struct drive *
369 check_drive(char *devicename)
370 {
371     int driveno;
372     int i;
373     struct drive *drive;
374 
375     driveno = find_drive_by_dev(devicename, 1);		    /* if entry doesn't exist, create it */
376     drive = &vinum_conf.drive[driveno];			    /* and get a pointer */
377 
378     if (read_drive_label(drive, 0) == DL_OURS) {	    /* one of ours */
379 	for (i = 0; i < vinum_conf.drives_allocated; i++) { /* see if the name already exists */
380 	    if ((i != driveno)				    /* not this drive */
381 	    &&(DRIVE[i].state != drive_unallocated)	    /* and it's allocated */
382 	    &&(strcmp(DRIVE[i].label.name,
383 			DRIVE[driveno].label.name) == 0)) { /* and it has the same name */
384 		struct drive *mydrive = &DRIVE[i];
385 
386 		if (mydrive->devicename[0] == '/') {	    /* we know a device name for it */
387 		    /*
388 		     * set an error, but don't take the
389 		     * drive down: that would cause unneeded
390 		     * error messages.
391 		     */
392 		    drive->lasterror = EEXIST;
393 		    break;
394 		} else {				    /* it's just a place holder, */
395 		    int sdno;
396 
397 		    for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) { /* look at each subdisk */
398 			if ((SD[sdno].driveno == i)	    /* it's pointing to this one, */
399 			&&(SD[sdno].state != sd_unallocated)) {	/* and it's a real subdisk */
400 			    SD[sdno].driveno = drive->driveno; /* point to the one we found */
401 			    update_sd_state(sdno);	    /* and update its state */
402 			}
403 		    }
404 		    bzero(mydrive, sizeof(struct drive));   /* don't deallocate it, just remove it */
405 		}
406 	    }
407 	}
408     } else {
409 	if (drive->lasterror == 0)
410 	    drive->lasterror = ENODEV;
411 	close_drive(drive);
412 	drive->state = drive_down;
413     }
414     return drive;
415 }
416 
417 static char *
418 sappend(char *txt, char *s)
419 {
420     while ((*s++ = *txt++) != 0);
421     return s - 1;
422 }
423 
424 void
425 format_config(char *config, int len)
426 {
427     int i;
428     int j;
429     char *s = config;
430     char *configend = &config[len];
431 
432     bzero(config, len);
433 
434     /* First write the volume configuration */
435     for (i = 0; i < vinum_conf.volumes_allocated; i++) {
436 	struct volume *vol;
437 
438 	vol = &vinum_conf.volume[i];
439 	if ((vol->state > volume_uninit)
440 	    && (vol->name[0] != '\0')) {		    /* paranoia */
441 	    ksnprintf(s,
442 		configend - s,
443 		"volume %s state %s",
444 		vol->name,
445 		volume_state(vol->state));
446 	    while (*s)
447 		s++;					    /* find the end */
448 	    if (vol->preferred_plex >= 0)		    /* preferences, */
449 		ksnprintf(s,
450 		    configend - s,
451 		    " readpol prefer %s",
452 		    vinum_conf.plex[vol->preferred_plex].name);
453 	    while (*s)
454 		s++;					    /* find the end */
455 	    s = sappend("\n", s);
456 	}
457     }
458 
459     /* Then the plex configuration */
460     for (i = 0; i < vinum_conf.plexes_allocated; i++) {
461 	struct plex *plex;
462 
463 	plex = &vinum_conf.plex[i];
464 	if ((plex->state > plex_referenced)
465 	    && (plex->name[0] != '\0')) {		    /* paranoia */
466 	    ksnprintf(s,
467 		configend - s,
468 		"plex name %s state %s org %s ",
469 		plex->name,
470 		plex_state(plex->state),
471 		plex_org(plex->organization));
472 	    while (*s)
473 		s++;					    /* find the end */
474 	    if (isstriped(plex)) {
475 		ksnprintf(s,
476 		    configend - s,
477 		    "%ds ",
478 		    (int) plex->stripesize);
479 		while (*s)
480 		    s++;				    /* find the end */
481 	    }
482 	    if (plex->volno >= 0)			    /* we have a volume */
483 		ksnprintf(s,
484 		    configend - s,
485 		    "vol %s ",
486 		    vinum_conf.volume[plex->volno].name);
487 	    while (*s)
488 		s++;					    /* find the end */
489 	    for (j = 0; j < plex->subdisks; j++) {
490 		ksnprintf(s,
491 		    configend - s,
492 		    " sd %s",
493 		    vinum_conf.sd[plex->sdnos[j]].name);
494 	    }
495 	    s = sappend("\n", s);
496 	}
497     }
498 
499     /* And finally the subdisk configuration */
500     for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
501 	struct sd *sd;
502 	char *drivename;
503 
504 	sd = &SD[i];
505 	if ((sd->state != sd_referenced)
506 	    && (sd->state != sd_unallocated)
507 	    && (sd->name[0] != '\0')) {			    /* paranoia */
508 	    drivename = vinum_conf.drive[sd->driveno].label.name;
509 	    /*
510 	     * XXX We've seen cases of dead subdisks
511 	     * which don't have a drive.  If we let them
512 	     * through here, the drive name is null, so
513 	     * they get the drive named 'plex'.
514 	     *
515 	     * This is a breakage limiter, not a fix.
516 	     */
517 	    if (drivename[0] == '\0')
518 		drivename = "*invalid*";
519 	    ksnprintf(s,
520 		configend - s,
521 		"sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
522 		sd->name,
523 		drivename,
524 		vinum_conf.plex[sd->plexno].name,
525 		(unsigned long long) sd->sectors,
526 		(unsigned long long) sd->driveoffset,
527 		sd_state(sd->state));
528 	    while (*s)
529 		s++;					    /* find the end */
530 	    if (sd->plexno >= 0)
531 		ksnprintf(s,
532 		    configend - s,
533 		    " plexoffset %llds",
534 		    (long long) sd->plexoffset);
535 	    else
536 		ksnprintf(s, configend - s, " detached");
537 	    while (*s)
538 		s++;					    /* find the end */
539 	    if (sd->flags & VF_RETRYERRORS) {
540 		ksnprintf(s, configend - s, " retryerrors");
541 		while (*s)
542 		    s++;				    /* find the end */
543 	    }
544 	    ksnprintf(s, configend - s, " \n");
545 	    while (*s)
546 		s++;					    /* find the end */
547 	}
548     }
549     if (s > &config[len - 2])
550 	panic("vinum: configuration data overflow");
551 }
552 
553 /*
554  * issue a save config request to the d�mon.  The actual work
555  * is done in process context by daemon_save_config
556  */
557 void
558 save_config(void)
559 {
560     queue_daemon_request(daemonrq_saveconfig, (union daemoninfo) 0);
561 }
562 
563 /*
564  * Write the configuration to all vinum slices.  This
565  * is performed by the d�mon only
566  */
567 void
568 daemon_save_config(void)
569 {
570     int error;
571     int written_config;					    /* set when we first write the config to disk */
572     int driveno;
573     struct drive *drive;				    /* point to current drive info */
574     struct vinum_hdr *vhdr;				    /* and as header */
575     char *config;					    /* point to config data */
576     int wlabel_on;					    /* to set writing label on/off */
577 
578     /* don't save the configuration while we're still working on it */
579     if (vinum_conf.flags & VF_CONFIGURING)
580 	return;
581     written_config = 0;					    /* no config written yet */
582     /* Build a volume header */
583     vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);	    /* get space for the config data */
584     CHECKALLOC(vhdr, "Can't allocate config data");
585     vhdr->magic = VINUM_MAGIC;				    /* magic number */
586     vhdr->config_length = MAXCONFIG;			    /* length of following config info */
587 
588     config = Malloc(MAXCONFIG);				    /* get space for the config data */
589     CHECKALLOC(config, "Can't allocate config data");
590 
591     format_config(config, MAXCONFIG);
592     error = 0;						    /* no errors yet */
593     for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
594 	drive = &vinum_conf.drive[driveno];		    /* point to drive */
595 	if (drive->state > drive_referenced) {
596 	    LOCKDRIVE(drive);				    /* don't let it change */
597 
598 	    /*
599 	     * First, do some drive consistency checks.  Some
600 	     * of these are kludges, others require a process
601 	     * context and couldn't be done before
602 	     */
603 	    if ((drive->devicename[0] == '\0')
604 		|| (drive->label.name[0] == '\0')) {
605 		unlockdrive(drive);
606 		free_drive(drive);			    /* get rid of it */
607 		break;
608 	    }
609 	    if (((drive->flags & VF_OPEN) == 0)		    /* drive not open */
610 	    &&(drive->state > drive_down)) {		    /* and it thinks it's not down */
611 		unlockdrive(drive);
612 		set_drive_state(driveno, drive_down, setstate_force); /* tell it what's what */
613 		continue;
614 	    }
615 	    if ((drive->state == drive_down)		    /* it's down */
616 	    &&(drive->flags & VF_OPEN)) {		    /* but open, */
617 		unlockdrive(drive);
618 		close_drive(drive);			    /* close it */
619 	    } else if (drive->state > drive_down) {
620 		getmicrotime(&drive->label.last_update);    /* time of last update is now */
621 		bcopy((char *) &drive->label,		    /* and the label info from the drive structure */
622 		    (char *) &vhdr->label,
623 		    sizeof(vhdr->label));
624 		if ((drive->state != drive_unallocated)
625 		    && (drive->state != drive_referenced)) { /* and it's a real drive */
626 		    wlabel_on = 1;			    /* enable writing the label */
627 		    error = 0;
628 #if 1
629 		    error = VOP_IOCTL(drive->vp, DIOCWLABEL,
630 				      (caddr_t)&wlabel_on, FREAD|FWRITE,
631 				      proc0.p_ucred, NULL);
632 #endif
633 		    if (error == 0)
634 			error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
635 		    if (error == 0)
636 			error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET); /* first config copy */
637 		    if (error == 0)
638 			error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG);	/* second copy */
639 		    wlabel_on = 0;			    /* enable writing the label */
640 #if 1
641 		    if (error == 0) {
642 			error = VOP_IOCTL(drive->vp, DIOCWLABEL,
643 					  (caddr_t)&wlabel_on, FREAD|FWRITE,
644 					  proc0.p_ucred, NULL);
645 		    }
646 #endif
647 		    unlockdrive(drive);
648 		    if (error) {
649 			log(LOG_ERR,
650 			    "vinum: Can't write config to %s, error %d\n",
651 			    drive->devicename,
652 			    error);
653 			set_drive_state(drive->driveno, drive_down, setstate_force);
654 		    } else
655 			written_config = 1;		    /* we've written it on at least one drive */
656 		}
657 	    } else					    /* not worth looking at, */
658 		unlockdrive(drive);			    /* just unlock it again */
659 	}
660     }
661     Free(vhdr);
662     Free(config);
663 }
664 
665 /* Look at all disks on the system for vinum slices */
666 int
667 vinum_scandisk(char *devicename[], int drives)
668 {
669     struct drive *volatile drive;
670     volatile int driveno;
671     int firstdrive;					    /* first drive in this list */
672     volatile int gooddrives;				    /* number of usable drives found */
673     int firsttime;					    /* set if we have never configured before */
674     int error;
675     char *config_text;					    /* read the config info from disk into here */
676     char *volatile cptr;				    /* pointer into config information */
677     char *eptr;						    /* end pointer into config information */
678     char *config_line;					    /* copy the config line to */
679     volatile int status;
680     int *volatile drivelist;				    /* list of drive indices */
681 #define DRIVENAMELEN 64
682 #define DRIVEPARTS   35					    /* max partitions per drive, excluding c */
683     char partname[DRIVENAMELEN];			    /* for creating partition names */
684 
685     status = 0;						    /* success indication */
686     vinum_conf.flags |= VF_READING_CONFIG;		    /* reading config from disk */
687 
688     gooddrives = 0;					    /* number of usable drives found */
689     firstdrive = vinum_conf.drives_used;		    /* the first drive */
690     firsttime = vinum_conf.drives_used == 0;		    /* are we a virgin? */
691 
692     /* allocate a drive pointer list */
693     drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
694     CHECKALLOC(drivelist, "Can't allocate memory");
695     error = setjmp(command_fail);			    /* come back here on error */
696     if (error) {					    /* longjmped out */
697 	return error;
698     }
699 
700     /* Open all drives and find which was modified most recently */
701     for (driveno = 0; driveno < drives; driveno++) {
702 	char part, has_part = 0;			    /* UNIX partition */
703 	int slice;
704 	int founddrive;					    /* flag when we find a vinum drive */
705 	int has_slice = -1;
706 	char *tmp;
707 
708 	founddrive = 0;					    /* no vinum drive found yet on this spindle */
709 
710 	/*
711 	 * If the device path contains a slice we do not try to tack on
712 	 * another slice.  If the device path has a partition we only check
713 	 * that partition.
714 	 */
715 	if ((tmp = rindex(devicename[driveno], '/')) == NULL)
716 	    tmp = devicename[driveno];
717 	else
718 		tmp++;
719 	ksscanf(tmp, "%*[a-z]%*d%*[s]%d%c", &has_slice, &has_part);
720 
721 	for (slice = 0; slice < MAX_SLICES; slice++) {
722 	    if (has_slice >= 0 && slice != has_slice)
723 		continue;
724 
725 	    for (part = 'a'; part < 'a' + MAXPARTITIONS; part++) {
726 		if (part == 'c')
727 		    continue;
728 		if (has_part && part != has_part)
729 		    continue;
730 		if (has_slice >= 0 && has_part)
731 			strncpy(partname, devicename[driveno], DRIVENAMELEN);
732 		else if (has_slice >= 0)
733 			ksnprintf(partname, DRIVENAMELEN,
734 				"%s%c", devicename[driveno], part);
735 		else
736 			ksnprintf(partname, DRIVENAMELEN,
737 				"%ss%d%c", devicename[driveno], slice, part);
738 		drive = check_drive(partname);	    /* try to open it */
739 		if ((drive->lasterror != 0)		    /* didn't work, */
740 		    ||(drive->state != drive_up))
741 		    free_drive(drive);		    /* get rid of it */
742 		else if (drive->flags & VF_CONFIGURED)  /* already read this config, */
743 		    log(LOG_WARNING,
744 			"vinum: already read config from %s\n", /* say so */
745 			drive->label.name);
746 		else {
747 		    drivelist[gooddrives] = drive->driveno;	/* keep the drive index */
748 		    drive->flags &= ~VF_NEWBORN;	    /* which is no longer newly born */
749 		    gooddrives++;
750 		    founddrive++;
751 		}
752 	    }
753 	}
754     }
755 
756     if (gooddrives == 0) {
757 	if (firsttime)
758 	    log(LOG_WARNING, "vinum: no drives found\n");
759 	else
760 	    log(LOG_INFO, "vinum: no additional drives found\n");
761 	return ENOENT;
762     }
763     /*
764      * We now have at least one drive
765      * open.  Sort them in order of config time
766      * and merge the config info with what we
767      * have already.
768      */
769     kqsort(drivelist, gooddrives, sizeof(int), drivecmp);
770     config_text = (char *) Malloc(MAXCONFIG * 2);	    /* allocate buffers */
771     CHECKALLOC(config_text, "Can't allocate memory");
772     config_line = (char *) Malloc(MAXCONFIGLINE * 2);	    /* allocate buffers */
773     CHECKALLOC(config_line, "Can't allocate memory");
774     for (driveno = 0; driveno < gooddrives; driveno++) {    /* now include the config */
775 	drive = &DRIVE[drivelist[driveno]];		    /* point to the drive */
776 
777 	if (firsttime && (driveno == 0))		    /* we've never configured before, */
778 	    log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
779 	else
780 	    log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
781 
782 	if (drive->state == drive_up)
783 	    /* Read in both copies of the configuration information */
784 	    error = read_drive(drive, config_text, MAXCONFIG * 2, VINUM_CONFIG_OFFSET);
785 	else {
786 	    error = EIO;
787 	    kprintf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
788 	}
789 
790 	if (error != 0) {
791 	    log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
792 	    free_drive(drive);				    /* give it back */
793 	    status = error;
794 	}
795 	/*
796 	 * At this point, check that the two copies
797 	 * are the same, and do something useful if
798 	 * not.  In particular, consider which is
799 	 * newer, and what this means for the
800 	 * integrity of the data on the drive.
801 	 */
802 	else {
803 	    vinum_conf.drives_used++;			    /* another drive in use */
804 	    /* Parse the configuration, and add it to the global configuration */
805 	    for (cptr = config_text; *cptr != '\0';) {	    /* love this style(9) */
806 		volatile int parse_status;		    /* return value from parse_config */
807 
808 		for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');) /* until the end of the line */
809 		    *eptr++ = *cptr++;
810 		*eptr = '\0';				    /* and delimit */
811 		if (setjmp(command_fail) == 0) {	    /* come back here on error and continue */
812 		    parse_status = parse_config(config_line, &keyword_set, 1); /* parse the config line */
813 		    if (parse_status < 0) {		    /* error in config */
814 			/*
815 			   * This config should have been parsed in user
816 			   * space.  If we run into problems here, something
817 			   * serious is afoot.  Complain and let the user
818 			   * snarf the config to see what's wrong.
819 			 */
820 			log(LOG_ERR,
821 			    "vinum: Config error on %s, aborting integration\n",
822 			    drive->devicename);
823 			free_drive(drive);		    /* give it back */
824 			status = EINVAL;
825 		    }
826 		}
827 		while (*cptr == '\n')
828 		    cptr++;				    /* skip to next line */
829 	    }
830 	}
831 	drive->flags |= VF_CONFIGURED;			    /* read this drive's configuration */
832     }
833 
834     Free(config_line);
835     Free(config_text);
836     Free(drivelist);
837     vinum_conf.flags &= ~VF_READING_CONFIG;		    /* no longer reading from disk */
838     if (status != 0)
839 	kprintf("vinum: couldn't read configuration");
840     else
841 	updateconfig(VF_READING_CONFIG);		    /* update from disk config */
842     return status;
843 }
844 
845 /*
846  * Compare the modification dates of the drives, for qsort.
847  * Return 1 if a < b, 0 if a == b, 01 if a > b: in other
848  * words, sort backwards.
849  */
850 int
851 drivecmp(const void *va, const void *vb)
852 {
853     const struct drive *a = &DRIVE[*(const int *) va];
854     const struct drive *b = &DRIVE[*(const int *) vb];
855 
856     if ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
857 	&& (a->label.last_update.tv_usec == b->label.last_update.tv_usec))
858 	return 0;
859     else if ((a->label.last_update.tv_sec > b->label.last_update.tv_sec)
860 	    || ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
861 	    && (a->label.last_update.tv_usec > b->label.last_update.tv_usec)))
862 	return -1;
863     else
864 	return 1;
865 }
866 /* Local Variables: */
867 /* fill-column: 50 */
868 /* End: */
869