Home | History | Annotate | Line # | Download | only in raidctl
rf_configure.c revision 1.11
      1  1.11       is /*	$NetBSD: rf_configure.c,v 1.11 2000/10/10 20:24:53 is Exp $	*/
      2  1.10  thorpej 
      3   1.1    oster /*
      4   1.1    oster  * Copyright (c) 1995 Carnegie-Mellon University.
      5   1.1    oster  * All rights reserved.
      6   1.1    oster  *
      7   1.1    oster  * Author: Mark Holland
      8   1.1    oster  *
      9   1.1    oster  * Permission to use, copy, modify and distribute this software and
     10   1.1    oster  * its documentation is hereby granted, provided that both the copyright
     11   1.1    oster  * notice and this permission notice appear in all copies of the
     12   1.1    oster  * software, derivative works or modified versions, and any portions
     13   1.1    oster  * thereof, and that both notices appear in supporting documentation.
     14   1.1    oster  *
     15   1.1    oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1    oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1    oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1    oster  *
     19   1.1    oster  * Carnegie Mellon requests users of this software to return to
     20   1.1    oster  *
     21   1.1    oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1    oster  *  School of Computer Science
     23   1.1    oster  *  Carnegie Mellon University
     24   1.1    oster  *  Pittsburgh PA 15213-3890
     25   1.1    oster  *
     26   1.1    oster  * any improvements or extensions that they make and grant Carnegie the
     27   1.1    oster  * rights to redistribute these changes.
     28   1.1    oster  */
     29   1.1    oster 
     30   1.1    oster /***************************************************************
     31   1.1    oster  *
     32   1.1    oster  * rf_configure.c -- code related to configuring the raidframe system
     33   1.1    oster  *
     34   1.1    oster  * configuration is complicated by the fact that we want the same
     35   1.1    oster  * driver to work both in the kernel and at user level.  In the
     36   1.1    oster  * kernel, we can't read the configuration file, so we configure
     37   1.1    oster  * by running a user-level program that reads the config file,
     38   1.1    oster  * creates a data structure describing the configuration and
     39   1.1    oster  * passes it into the kernel via an ioctl.  Since we want the config
     40   1.1    oster  * code to be common between the two versions of the driver, we
     41   1.1    oster  * configure using the same two-step process when running at
     42   1.1    oster  * user level.  Of course, at user level, the config structure is
     43   1.1    oster  * passed directly to the config routine, rather than via ioctl.
     44   1.1    oster  *
     45   1.1    oster  * This file is not compiled into the kernel, so we have no
     46   1.1    oster  * need for KERNEL ifdefs.
     47   1.1    oster  *
     48   1.1    oster  **************************************************************/
     49   1.1    oster 
     50   1.1    oster #include <stdio.h>
     51   1.4    oster #include <stdlib.h>
     52   1.3   mjacob #include <errno.h>
     53   1.4    oster #include <strings.h>
     54   1.1    oster #include <sys/types.h>
     55   1.1    oster #include <sys/stat.h>
     56   1.1    oster #include "rf_raid.h"
     57   1.1    oster #include "rf_raidframe.h"
     58   1.1    oster #include "rf_general.h"
     59   1.1    oster #include "rf_decluster.h"
     60   1.1    oster #include "rf_configure.h"
     61   1.1    oster 
     62  1.10  thorpej /*
     63  1.10  thorpej  * XXX we include this here so we don't need to drag rf_debugMem.c into
     64  1.10  thorpej  * the picture...  This is userland, afterall...
     65  1.10  thorpej  */
     66   1.1    oster 
     67  1.10  thorpej /*
     68  1.10  thorpej  * XXX sucky hack to override the defn. of RF_Malloc as given in
     69  1.10  thorpej  * rf_debugMem.c...  but I *really* don't want (nor need) to link with
     70  1.10  thorpej  * that file here in userland..  GO
     71   1.1    oster  */
     72   1.1    oster 
     73   1.1    oster #undef RF_Malloc
     74   1.1    oster #define RF_Malloc(_p_, _size_, _cast_) \
     75   1.1    oster   { \
     76   1.1    oster      _p_ = _cast_ malloc((u_long)_size_); \
     77   1.1    oster      bzero((char *)_p_, _size_); \
     78   1.1    oster   }
     79   1.1    oster 
     80  1.10  thorpej char *rf_find_non_white(char *p);
     81  1.10  thorpej char *rf_find_white(char *p);
     82   1.7    oster 
     83  1.10  thorpej static int rf_search_file_for_start_of(const char *string, char *buf,
     84  1.10  thorpej     int len, FILE * fp);
     85  1.10  thorpej static int rf_get_next_nonblank_line(char *buf, int len, FILE * fp,
     86  1.10  thorpej     const char *errmsg);
     87   1.1    oster 
     88  1.10  thorpej /*
     89  1.10  thorpej  * called from user level to read the configuration file and create
     90   1.1    oster  * a configuration control structure.  This is used in the user-level
     91   1.1    oster  * version of the driver, and in the user-level program that configures
     92   1.1    oster  * the system via ioctl.
     93   1.1    oster  */
     94  1.10  thorpej int
     95  1.10  thorpej rf_MakeConfig(configname, cfgPtr)
     96  1.10  thorpej 	char *configname;
     97  1.10  thorpej 	RF_Config_t *cfgPtr;
     98   1.1    oster {
     99  1.10  thorpej 	int numscanned, val, r, c, retcode, aa, bb, cc;
    100  1.10  thorpej 	char buf[256], buf1[256], *cp;
    101  1.10  thorpej 	RF_LayoutSW_t *lp;
    102  1.10  thorpej 	FILE *fp;
    103  1.10  thorpej 
    104  1.10  thorpej 	bzero((char *) cfgPtr, sizeof(RF_Config_t));
    105  1.10  thorpej 
    106  1.10  thorpej 	fp = fopen(configname, "r");
    107  1.10  thorpej 	if (!fp) {
    108  1.10  thorpej 		RF_ERRORMSG1("Can't open config file %s\n", configname);
    109  1.10  thorpej 		return (-1);
    110  1.10  thorpej 	}
    111  1.10  thorpej 	rewind(fp);
    112  1.10  thorpej 	if (rf_search_file_for_start_of("array", buf, 256, fp)) {
    113  1.10  thorpej 		RF_ERRORMSG1("Unable to find start of \"array\" params in config file %s\n", configname);
    114  1.10  thorpej 		retcode = -1;
    115  1.10  thorpej 		goto out;
    116  1.10  thorpej 	}
    117  1.10  thorpej 	rf_get_next_nonblank_line(buf, 256, fp, "Config file error (\"array\" section):  unable to get numRow and numCol\n");
    118  1.10  thorpej 
    119  1.10  thorpej 	/*
    120  1.10  thorpej          * wackiness with aa, bb, cc to get around size problems on
    121  1.10  thorpej          * different platforms
    122  1.10  thorpej          */
    123  1.10  thorpej 	numscanned = sscanf(buf, "%d %d %d", &aa, &bb, &cc);
    124  1.10  thorpej 	if (numscanned != 3) {
    125  1.10  thorpej 		RF_ERRORMSG("Config file error (\"array\" section):  unable to get numRow, numCol, numSpare\n");
    126  1.10  thorpej 		retcode = -1;
    127  1.10  thorpej 		goto out;
    128  1.10  thorpej 	}
    129  1.10  thorpej 	cfgPtr->numRow = (RF_RowCol_t) aa;
    130  1.10  thorpej 	cfgPtr->numCol = (RF_RowCol_t) bb;
    131  1.10  thorpej 	cfgPtr->numSpare = (RF_RowCol_t) cc;
    132  1.10  thorpej 
    133  1.10  thorpej 	/* debug section is optional */
    134  1.10  thorpej 	for (c = 0; c < RF_MAXDBGV; c++)
    135  1.10  thorpej 		cfgPtr->debugVars[c][0] = '\0';
    136  1.10  thorpej 	rewind(fp);
    137  1.10  thorpej 	if (!rf_search_file_for_start_of("debug", buf, 256, fp)) {
    138  1.10  thorpej 		for (c = 0; c < RF_MAXDBGV; c++) {
    139  1.10  thorpej 			if (rf_get_next_nonblank_line(buf, 256, fp, NULL))
    140  1.10  thorpej 				break;
    141  1.10  thorpej 			cp = rf_find_non_white(buf);
    142  1.10  thorpej 			if (!strncmp(cp, "START", strlen("START")))
    143  1.10  thorpej 				break;
    144  1.10  thorpej 			(void) strcpy(&cfgPtr->debugVars[c][0], cp);
    145  1.10  thorpej 		}
    146  1.10  thorpej 	}
    147  1.10  thorpej 	rewind(fp);
    148  1.10  thorpej 	strcpy(cfgPtr->diskQueueType, "fifo");
    149  1.10  thorpej 	cfgPtr->maxOutstandingDiskReqs = 1;
    150  1.10  thorpej 	/* scan the file for the block related to disk queues */
    151  1.10  thorpej 	if (rf_search_file_for_start_of("queue", buf, 256, fp)) {
    152  1.10  thorpej 		RF_ERRORMSG2("[No disk queue discipline specified in config file %s.  Using %s.]\n", configname, cfgPtr->diskQueueType);
    153  1.10  thorpej 	} else {
    154  1.10  thorpej 		if (rf_get_next_nonblank_line(buf, 256, fp, NULL)) {
    155  1.10  thorpej 			RF_ERRORMSG2("[No disk queue discipline specified in config file %s.  Using %s.]\n", configname, cfgPtr->diskQueueType);
    156  1.10  thorpej 		}
    157  1.10  thorpej 	}
    158  1.10  thorpej 
    159  1.10  thorpej 	/* the queue specifier line contains two entries: 1st char of first
    160  1.10  thorpej 	 * word specifies queue to be used 2nd word specifies max num reqs
    161  1.10  thorpej 	 * that can be outstanding on the disk itself (typically 1) */
    162  1.10  thorpej 	if (sscanf(buf, "%s %d", buf1, &val) != 2) {
    163  1.10  thorpej 		RF_ERRORMSG1("Can't determine queue type and/or max outstanding reqs from line: %s", buf);
    164  1.10  thorpej 		RF_ERRORMSG2("Using %s-%d\n", cfgPtr->diskQueueType, cfgPtr->maxOutstandingDiskReqs);
    165  1.10  thorpej 	} else {
    166  1.10  thorpej 		char *ch;
    167  1.10  thorpej 		bcopy(buf1, cfgPtr->diskQueueType,
    168  1.10  thorpej 		    RF_MIN(sizeof(cfgPtr->diskQueueType), strlen(buf1) + 1));
    169  1.10  thorpej 		for (ch = buf1; *ch; ch++) {
    170  1.10  thorpej 			if (*ch == ' ') {
    171  1.10  thorpej 				*ch = '\0';
    172  1.10  thorpej 				break;
    173  1.10  thorpej 			}
    174  1.10  thorpej 		}
    175  1.10  thorpej 		cfgPtr->maxOutstandingDiskReqs = val;
    176  1.10  thorpej 	}
    177  1.10  thorpej 
    178  1.10  thorpej 	rewind(fp);
    179  1.10  thorpej 
    180  1.10  thorpej 	if (rf_search_file_for_start_of("disks", buf, 256, fp)) {
    181  1.10  thorpej 		RF_ERRORMSG1("Can't find \"disks\" section in config file %s\n", configname);
    182  1.10  thorpej 		retcode = -1;
    183  1.10  thorpej 		goto out;
    184  1.10  thorpej 	}
    185  1.10  thorpej 	for (r = 0; r < cfgPtr->numRow; r++) {
    186  1.10  thorpej 		for (c = 0; c < cfgPtr->numCol; c++) {
    187  1.10  thorpej 			if (rf_get_next_nonblank_line(
    188  1.10  thorpej 			    &cfgPtr->devnames[r][c][0], 50, fp, NULL)) {
    189  1.10  thorpej 				RF_ERRORMSG2("Config file error: unable to get device file for disk at row %d col %d\n", r, c);
    190  1.10  thorpej 				retcode = -1;
    191  1.10  thorpej 				goto out;
    192  1.10  thorpej 			}
    193  1.10  thorpej 		}
    194  1.10  thorpej 	}
    195  1.10  thorpej 
    196  1.10  thorpej 	/* "spare" section is optional */
    197  1.10  thorpej 	rewind(fp);
    198  1.10  thorpej 	if (rf_search_file_for_start_of("spare", buf, 256, fp))
    199  1.10  thorpej 		cfgPtr->numSpare = 0;
    200  1.10  thorpej 	for (c = 0; c < cfgPtr->numSpare; c++) {
    201  1.10  thorpej 		if (rf_get_next_nonblank_line(&cfgPtr->spare_names[c][0],
    202  1.10  thorpej 		    256, fp, NULL)) {
    203  1.10  thorpej 			RF_ERRORMSG1("Config file error: unable to get device file for spare disk %d\n", c);
    204  1.10  thorpej 			retcode = -1;
    205  1.10  thorpej 			goto out;
    206  1.10  thorpej 		}
    207  1.10  thorpej 	}
    208  1.10  thorpej 
    209  1.10  thorpej 	/* scan the file for the block related to layout */
    210  1.10  thorpej 	rewind(fp);
    211  1.10  thorpej 	if (rf_search_file_for_start_of("layout", buf, 256, fp)) {
    212  1.10  thorpej 		RF_ERRORMSG1("Can't find \"layout\" section in configuration file %s\n", configname);
    213  1.10  thorpej 		retcode = -1;
    214  1.10  thorpej 		goto out;
    215  1.10  thorpej 	}
    216  1.10  thorpej 	if (rf_get_next_nonblank_line(buf, 256, fp, NULL)) {
    217  1.10  thorpej 		RF_ERRORMSG("Config file error (\"layout\" section): unable to find common layout param line\n");
    218  1.10  thorpej 		retcode = -1;
    219  1.10  thorpej 		goto out;
    220  1.10  thorpej 	}
    221  1.10  thorpej 	c = sscanf(buf, "%d %d %d %c", &aa, &bb, &cc, &cfgPtr->parityConfig);
    222  1.10  thorpej 	cfgPtr->sectPerSU = (RF_SectorNum_t) aa;
    223  1.10  thorpej 	cfgPtr->SUsPerPU = (RF_StripeNum_t) bb;
    224  1.10  thorpej 	cfgPtr->SUsPerRU = (RF_StripeNum_t) cc;
    225  1.10  thorpej 	if (c != 4) {
    226  1.10  thorpej 		RF_ERRORMSG("Unable to scan common layout line\n");
    227  1.10  thorpej 		retcode = -1;
    228  1.10  thorpej 		goto out;
    229  1.10  thorpej 	}
    230  1.10  thorpej 	lp = rf_GetLayout(cfgPtr->parityConfig);
    231  1.10  thorpej 	if (lp == NULL) {
    232  1.10  thorpej 		RF_ERRORMSG1("Unknown parity config '%c'\n",
    233  1.10  thorpej 		    cfgPtr->parityConfig);
    234  1.10  thorpej 		retcode = -1;
    235  1.10  thorpej 		goto out;
    236  1.10  thorpej 	}
    237  1.10  thorpej 
    238  1.10  thorpej 	/*
    239  1.10  thorpej 	 * XXX who cares.. it's not going into the kernel, so we should ignore
    240  1.10  thorpej 	 * this...
    241  1.10  thorpej 	 */
    242  1.10  thorpej #ifndef _KERNEL
    243  1.10  thorpej 	retcode = lp->MakeLayoutSpecific(fp, cfgPtr, lp->makeLayoutSpecificArg);
    244   1.1    oster #endif
    245   1.1    oster out:
    246  1.10  thorpej 	fclose(fp);
    247  1.10  thorpej 	if (retcode < 0)
    248  1.10  thorpej 		retcode = errno = EINVAL;
    249  1.10  thorpej 	else
    250  1.10  thorpej 		errno = retcode;
    251  1.10  thorpej 	return (retcode);
    252   1.1    oster }
    253   1.1    oster 
    254   1.1    oster 
    255   1.1    oster /* used in architectures such as RAID0 where there is no layout-specific
    256   1.1    oster  * information to be passed into the configuration code.
    257   1.1    oster  */
    258  1.10  thorpej int
    259  1.10  thorpej rf_MakeLayoutSpecificNULL(fp, cfgPtr, ignored)
    260  1.10  thorpej 	FILE *fp;
    261  1.10  thorpej 	RF_Config_t *cfgPtr;
    262  1.10  thorpej 	void *ignored;
    263   1.1    oster {
    264  1.10  thorpej 	cfgPtr->layoutSpecificSize = 0;
    265  1.10  thorpej 	cfgPtr->layoutSpecific = NULL;
    266  1.10  thorpej 	return (0);
    267   1.1    oster }
    268   1.1    oster 
    269  1.10  thorpej int
    270  1.10  thorpej rf_MakeLayoutSpecificDeclustered(configfp, cfgPtr, arg)
    271  1.10  thorpej 	FILE *configfp;
    272  1.10  thorpej 	RF_Config_t *cfgPtr;
    273  1.10  thorpej 	void *arg;
    274   1.1    oster {
    275  1.10  thorpej 	int b, v, k, r, lambda, norotate, i, val, distSpare;
    276  1.10  thorpej 	char *cfgBuf, *bdfile, *p, *smname;
    277  1.10  thorpej 	char buf[256], smbuf[256];
    278  1.10  thorpej 	FILE *fp;
    279  1.10  thorpej 
    280  1.10  thorpej 	distSpare = *((int *) arg);
    281  1.10  thorpej 
    282  1.10  thorpej 	/* get the block design file name */
    283  1.10  thorpej 	if (rf_get_next_nonblank_line(buf, 256, configfp,
    284  1.10  thorpej 	    "Can't find block design file name in config file\n"))
    285  1.10  thorpej 		return (EINVAL);
    286  1.10  thorpej 	bdfile = rf_find_non_white(buf);
    287  1.10  thorpej 	if (bdfile[strlen(bdfile) - 1] == '\n') {
    288  1.10  thorpej 		/* strip newline char */
    289  1.10  thorpej 		bdfile[strlen(bdfile) - 1] = '\0';
    290  1.10  thorpej 	}
    291  1.10  thorpej 	/* open bd file, check validity of configuration */
    292  1.10  thorpej 	if ((fp = fopen(bdfile, "r")) == NULL) {
    293  1.10  thorpej 		RF_ERRORMSG1("RAID: config error: Can't open layout table file %s\n", bdfile);
    294  1.10  thorpej 		return (EINVAL);
    295  1.10  thorpej 	}
    296  1.10  thorpej 	fgets(buf, 256, fp);
    297  1.10  thorpej 	i = sscanf(buf, "%u %u %u %u %u %u", &b, &v, &k, &r, &lambda, &norotate);
    298  1.10  thorpej 	if (i == 5)
    299  1.10  thorpej 		norotate = 0;	/* no-rotate flag is optional */
    300  1.10  thorpej 	else if (i != 6) {
    301  1.10  thorpej 		RF_ERRORMSG("Unable to parse header line in block design file\n");
    302  1.10  thorpej 		return (EINVAL);
    303  1.10  thorpej 	}
    304  1.10  thorpej 	/* set the sparemap directory.  In the in-kernel version, there's a
    305  1.10  thorpej 	 * daemon that's responsible for finding the sparemaps */
    306  1.10  thorpej 	if (distSpare) {
    307  1.10  thorpej 		if (rf_get_next_nonblank_line(smbuf, 256, configfp,
    308  1.10  thorpej 		    "Can't find sparemap file name in config file\n"))
    309  1.10  thorpej 			return (EINVAL);
    310  1.10  thorpej 		smname = rf_find_non_white(smbuf);
    311  1.10  thorpej 		if (smname[strlen(smname) - 1] == '\n') {
    312  1.10  thorpej 			/* strip newline char */
    313  1.10  thorpej 			smname[strlen(smname) - 1] = '\0';
    314  1.10  thorpej 		}
    315  1.10  thorpej 	} else {
    316  1.10  thorpej 		smbuf[0] = '\0';
    317  1.10  thorpej 		smname = smbuf;
    318  1.10  thorpej 	}
    319  1.10  thorpej 
    320  1.10  thorpej 	/* allocate a buffer to hold the configuration info */
    321  1.10  thorpej 	cfgPtr->layoutSpecificSize = RF_SPAREMAP_NAME_LEN +
    322  1.10  thorpej 	    6 * sizeof(int) + b * k;
    323  1.10  thorpej 	/* can't use RF_Malloc here b/c debugMem module not yet init'd */
    324  1.10  thorpej 	cfgBuf = (char *) malloc(cfgPtr->layoutSpecificSize);
    325  1.10  thorpej 	cfgPtr->layoutSpecific = (void *) cfgBuf;
    326  1.10  thorpej 	p = cfgBuf;
    327  1.10  thorpej 
    328  1.10  thorpej 	/* install name of sparemap file */
    329  1.10  thorpej 	for (i = 0; smname[i]; i++)
    330  1.10  thorpej 		*p++ = smname[i];
    331  1.10  thorpej 	/* pad with zeros */
    332  1.10  thorpej 	while (i < RF_SPAREMAP_NAME_LEN) {
    333  1.10  thorpej 		*p++ = '\0';
    334  1.10  thorpej 		i++;
    335  1.10  thorpej 	}
    336  1.10  thorpej 
    337  1.10  thorpej 	/*
    338  1.10  thorpej          * fill in the buffer with the block design parameters
    339  1.10  thorpej          * and then the block design itself
    340  1.10  thorpej          */
    341  1.10  thorpej 	*((int *) p) = b;
    342  1.10  thorpej 	p += sizeof(int);
    343  1.10  thorpej 	*((int *) p) = v;
    344  1.10  thorpej 	p += sizeof(int);
    345  1.10  thorpej 	*((int *) p) = k;
    346  1.10  thorpej 	p += sizeof(int);
    347  1.10  thorpej 	*((int *) p) = r;
    348  1.10  thorpej 	p += sizeof(int);
    349  1.10  thorpej 	*((int *) p) = lambda;
    350  1.10  thorpej 	p += sizeof(int);
    351  1.10  thorpej 	*((int *) p) = norotate;
    352  1.10  thorpej 	p += sizeof(int);
    353  1.10  thorpej 
    354  1.10  thorpej 	while (fscanf(fp, "%d", &val) == 1)
    355  1.10  thorpej 		*p++ = (char) val;
    356  1.10  thorpej 	fclose(fp);
    357  1.10  thorpej 	if (p - cfgBuf != cfgPtr->layoutSpecificSize) {
    358  1.10  thorpej 		RF_ERRORMSG2("Size mismatch creating layout specific data: is %d sb %d bytes\n", (int) (p - cfgBuf), (int) (6 * sizeof(int) + b * k));
    359  1.10  thorpej 		return (EINVAL);
    360  1.10  thorpej 	}
    361  1.10  thorpej 	return (0);
    362   1.1    oster }
    363   1.1    oster 
    364   1.1    oster /****************************************************************************
    365   1.1    oster  *
    366   1.1    oster  * utilities
    367   1.1    oster  *
    368   1.1    oster  ***************************************************************************/
    369   1.7    oster 
    370   1.7    oster /* finds a non-white character in the line */
    371  1.10  thorpej char *
    372   1.7    oster rf_find_non_white(char *p)
    373   1.7    oster {
    374   1.7    oster 	for (; *p != '\0' && (*p == ' ' || *p == '\t'); p++);
    375   1.7    oster 	return (p);
    376   1.7    oster }
    377   1.7    oster 
    378   1.7    oster /* finds a white character in the line */
    379  1.10  thorpej char *
    380   1.7    oster rf_find_white(char *p)
    381   1.7    oster {
    382   1.7    oster 	for (; *p != '\0' && (*p != ' ' && *p != '\t'); p++);
    383   1.7    oster 	return (p);
    384   1.7    oster }
    385   1.1    oster 
    386  1.10  thorpej /*
    387  1.10  thorpej  * searches a file for a line that says "START string", where string is
    388   1.1    oster  * specified as a parameter
    389   1.1    oster  */
    390  1.10  thorpej static int
    391  1.10  thorpej rf_search_file_for_start_of(string, buf, len, fp)
    392  1.10  thorpej 	const char *string;
    393  1.10  thorpej 	char *buf;
    394  1.10  thorpej 	int len;
    395  1.10  thorpej 	FILE *fp;
    396   1.1    oster {
    397  1.10  thorpej 	char *p;
    398   1.1    oster 
    399  1.10  thorpej 	while (1) {
    400  1.10  thorpej 		if (fgets(buf, len, fp) == NULL)
    401  1.10  thorpej 			return (-1);
    402  1.10  thorpej 		p = rf_find_non_white(buf);
    403  1.10  thorpej 		if (!strncmp(p, "START", strlen("START"))) {
    404  1.10  thorpej 			p = rf_find_white(p);
    405  1.10  thorpej 			p = rf_find_non_white(p);
    406  1.10  thorpej 			if (!strncmp(p, string, strlen(string)))
    407  1.10  thorpej 				return (0);
    408  1.10  thorpej 		}
    409  1.10  thorpej 	}
    410   1.1    oster }
    411   1.1    oster 
    412   1.1    oster /* reads from file fp into buf until it finds an interesting line */
    413  1.10  thorpej int
    414  1.10  thorpej rf_get_next_nonblank_line(buf, len, fp, errmsg)
    415  1.10  thorpej 	char *buf;
    416  1.10  thorpej 	int len;
    417  1.10  thorpej 	FILE *fp;
    418  1.10  thorpej 	const char *errmsg;
    419   1.1    oster {
    420  1.10  thorpej 	char *p;
    421   1.1    oster 
    422  1.10  thorpej 	while (fgets(buf, 256, fp) != NULL) {
    423  1.10  thorpej 		p = rf_find_non_white(buf);
    424  1.10  thorpej 		if (*p == '\n' || *p == '\0' || *p == '#')
    425  1.10  thorpej 			continue;
    426  1.10  thorpej 		return (0);
    427  1.10  thorpej 	}
    428  1.10  thorpej 	if (errmsg)
    429  1.11       is 		RF_ERRORMSG1("%s", errmsg);
    430  1.10  thorpej 	return (1);
    431   1.1    oster }
    432   1.1    oster 
    433  1.10  thorpej /*
    434  1.10  thorpej  * Allocates an array for the spare table, and initializes it from a file.
    435   1.1    oster  * In the user-level version, this is called when recon is initiated.
    436   1.1    oster  * When/if I move recon into the kernel, there'll be a daemon that does
    437   1.1    oster  * an ioctl into raidframe which will block until a spare table is needed.
    438   1.1    oster  * When it returns, it will read a spare table from the file system,
    439   1.1    oster  * pass it into the kernel via a different ioctl, and then block again
    440   1.1    oster  * on the original ioctl.
    441   1.1    oster  *
    442   1.1    oster  * This is specific to the declustered layout, but doesn't belong in
    443   1.1    oster  * rf_decluster.c because it uses stuff that can't be compiled into
    444   1.1    oster  * the kernel, and it needs to be compiled into the user-level sparemap daemon.
    445   1.1    oster  *
    446   1.1    oster  */
    447  1.10  thorpej void *
    448  1.10  thorpej rf_ReadSpareTable(req, fname)
    449  1.10  thorpej 	RF_SparetWait_t *req;
    450  1.10  thorpej 	char *fname;
    451   1.1    oster {
    452  1.10  thorpej 	int i, j, numFound, linecount, tableNum, tupleNum,
    453  1.10  thorpej 	    spareDisk, spareBlkOffset;
    454  1.10  thorpej 	char buf[1024], targString[100], errString[100];
    455  1.10  thorpej 	RF_SpareTableEntry_t **table;
    456  1.10  thorpej 	FILE *fp;
    457  1.10  thorpej 
    458  1.10  thorpej 	/* allocate and initialize the table */
    459  1.10  thorpej 	RF_Malloc(table,
    460  1.10  thorpej 	    req->TablesPerSpareRegion * sizeof(RF_SpareTableEntry_t *),
    461  1.10  thorpej 	    (RF_SpareTableEntry_t **));
    462  1.10  thorpej 	for (i = 0; i < req->TablesPerSpareRegion; i++) {
    463  1.10  thorpej 		RF_Malloc(table[i],
    464  1.10  thorpej 		    req->BlocksPerTable * sizeof(RF_SpareTableEntry_t),
    465  1.10  thorpej 		    (RF_SpareTableEntry_t *));
    466  1.10  thorpej 		for (j = 0; j < req->BlocksPerTable; j++)
    467  1.10  thorpej 			table[i][j].spareDisk =
    468  1.10  thorpej 			    table[i][j].spareBlockOffsetInSUs = -1;
    469  1.10  thorpej 	}
    470  1.10  thorpej 
    471  1.10  thorpej 	/* 2.  open sparemap file, sanity check */
    472  1.10  thorpej 	if ((fp = fopen(fname, "r")) == NULL) {
    473  1.10  thorpej 		fprintf(stderr,
    474  1.10  thorpej 		    "rf_ReadSpareTable:  Can't open sparemap file %s\n", fname);
    475  1.10  thorpej 		return (NULL);
    476  1.10  thorpej 	}
    477  1.10  thorpej 	if (rf_get_next_nonblank_line(buf, 1024, fp,
    478  1.10  thorpej 	    "Invalid sparemap file:  can't find header line\n"))
    479  1.10  thorpej 		return (NULL);
    480  1.10  thorpej 	if (buf[strlen(buf) - 1] == '\n')
    481  1.10  thorpej 		buf[strlen(buf) - 1] = '\0';
    482  1.10  thorpej 
    483  1.10  thorpej 	sprintf(targString, "fdisk %d\n", req->fcol);
    484  1.10  thorpej 	sprintf(errString,
    485  1.10  thorpej 	    "Invalid sparemap file:  can't find \"fdisk %d\" line\n",
    486  1.10  thorpej 	    req->fcol);
    487  1.10  thorpej 	while (1) {
    488  1.10  thorpej 		rf_get_next_nonblank_line(buf, 1024, fp, errString);
    489  1.10  thorpej 		if (!strncmp(buf, targString, strlen(targString)))
    490  1.10  thorpej 			break;
    491  1.10  thorpej 	}
    492  1.10  thorpej 
    493  1.10  thorpej 	/* no more blank lines or comments allowed now */
    494  1.10  thorpej 	linecount = req->TablesPerSpareRegion * req->TableDepthInPUs;
    495  1.10  thorpej 	for (i = 0; i < linecount; i++) {
    496  1.10  thorpej 		numFound = fscanf(fp, " %d %d %d %d", &tableNum, &tupleNum,
    497  1.10  thorpej 		    &spareDisk, &spareBlkOffset);
    498  1.10  thorpej 		if (numFound != 4) {
    499  1.10  thorpej 			fprintf(stderr, "Sparemap file prematurely exhausted after %d of %d lines\n", i, linecount);
    500  1.10  thorpej 			return (NULL);
    501  1.10  thorpej 		}
    502  1.10  thorpej 		RF_ASSERT(tableNum >= 0 &&
    503  1.10  thorpej 		    tableNum < req->TablesPerSpareRegion);
    504  1.10  thorpej 		RF_ASSERT(tupleNum >= 0 && tupleNum < req->BlocksPerTable);
    505  1.10  thorpej 		RF_ASSERT(spareDisk >= 0 && spareDisk < req->C);
    506  1.10  thorpej 		RF_ASSERT(spareBlkOffset >= 0 && spareBlkOffset <
    507  1.10  thorpej 		    req->SpareSpaceDepthPerRegionInSUs / req->SUsPerPU);
    508  1.10  thorpej 
    509  1.10  thorpej 		table[tableNum][tupleNum].spareDisk = spareDisk;
    510  1.10  thorpej 		table[tableNum][tupleNum].spareBlockOffsetInSUs =
    511  1.10  thorpej 		    spareBlkOffset * req->SUsPerPU;
    512  1.10  thorpej 	}
    513   1.1    oster 
    514  1.10  thorpej 	fclose(fp);
    515  1.10  thorpej 	return ((void *) table);
    516   1.1    oster }
    517