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