Home | History | Annotate | Line # | Download | only in raidctl
rf_configure.c revision 1.29
      1  1.29       kre /*	$NetBSD: rf_configure.c,v 1.29 2017/11/20 22:16:23 kre 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.17       agc #include <sys/cdefs.h>
     50  1.17       agc 
     51  1.17       agc #ifndef lint
     52  1.29       kre __RCSID("$NetBSD: rf_configure.c,v 1.29 2017/11/20 22:16:23 kre Exp $");
     53  1.17       agc #endif
     54  1.17       agc 
     55   1.1     oster 
     56   1.1     oster #include <stdio.h>
     57   1.4     oster #include <stdlib.h>
     58   1.3    mjacob #include <errno.h>
     59   1.4     oster #include <strings.h>
     60  1.25  christos #include <err.h>
     61  1.27    kardel #include <util.h>
     62   1.1     oster #include <sys/types.h>
     63   1.1     oster #include <sys/stat.h>
     64  1.15     oster 
     65  1.15     oster #include <dev/raidframe/raidframevar.h>
     66  1.15     oster #include <dev/raidframe/raidframeio.h>
     67   1.1     oster #include "rf_configure.h"
     68   1.1     oster 
     69  1.28  christos static char   *rf_find_non_white(char *, int);
     70  1.28  christos static char   *rf_find_white(char *);
     71  1.28  christos static int rf_search_file_for_start_of(const char *, char *, int, FILE *);
     72  1.28  christos static int rf_get_next_nonblank_line(char *, int, FILE *, const char *);
     73  1.28  christos 
     74  1.15     oster #define RF_MIN(a,b) (((a) < (b)) ? (a) : (b))
     75  1.15     oster 
     76  1.28  christos static int     distSpareYes = 1;
     77  1.28  christos static int     distSpareNo = 0;
     78  1.13     oster 
     79  1.29       kre /*
     80  1.29       kre  * The mapsw[] table below contains all the various RAID types that might
     81  1.29       kre  * be supported by the kernel.  The actual supported types are found
     82  1.29       kre  * in sys/dev/raidframe/rf_layout.c.
     83  1.29       kre  */
     84  1.13     oster 
     85  1.28  christos static const RF_LayoutSW_t mapsw[] = {
     86  1.13     oster 	/* parity declustering */
     87  1.13     oster 	{'T', "Parity declustering",
     88  1.13     oster 	 rf_MakeLayoutSpecificDeclustered, &distSpareNo},
     89  1.13     oster 	/* parity declustering with distributed sparing */
     90  1.13     oster 	{'D', "Distributed sparing parity declustering",
     91  1.13     oster 	 rf_MakeLayoutSpecificDeclustered, &distSpareYes},
     92  1.13     oster 	/* declustered P+Q */
     93  1.13     oster 	{'Q', "Declustered P+Q",
     94  1.13     oster 	 rf_MakeLayoutSpecificDeclustered, &distSpareNo},
     95  1.13     oster 	/* RAID 5 with rotated sparing */
     96  1.13     oster 	{'R', "RAID Level 5 rotated sparing", rf_MakeLayoutSpecificNULL, NULL},
     97  1.13     oster 	/* Chained Declustering */
     98  1.13     oster 	{'C', "Chained Declustering", rf_MakeLayoutSpecificNULL, NULL},
     99  1.13     oster 	/* Interleaved Declustering */
    100  1.13     oster 	{'I', "Interleaved Declustering", rf_MakeLayoutSpecificNULL, NULL},
    101  1.13     oster 	/* RAID level 0 */
    102  1.13     oster 	{'0', "RAID Level 0", rf_MakeLayoutSpecificNULL, NULL},
    103  1.13     oster 	/* RAID level 1 */
    104  1.13     oster 	{'1', "RAID Level 1", rf_MakeLayoutSpecificNULL, NULL},
    105  1.13     oster 	/* RAID level 4 */
    106  1.13     oster 	{'4', "RAID Level 4", rf_MakeLayoutSpecificNULL, NULL},
    107  1.13     oster 	/* RAID level 5 */
    108  1.13     oster 	{'5', "RAID Level 5", rf_MakeLayoutSpecificNULL, NULL},
    109  1.13     oster 	/* Evenodd */
    110  1.13     oster 	{'E', "EvenOdd", rf_MakeLayoutSpecificNULL, NULL},
    111  1.13     oster 	/* Declustered Evenodd */
    112  1.13     oster 	{'e', "Declustered EvenOdd",
    113  1.13     oster 	 rf_MakeLayoutSpecificDeclustered, &distSpareNo},
    114  1.13     oster 	/* parity logging */
    115  1.13     oster 	{'L', "Parity logging", rf_MakeLayoutSpecificNULL, NULL},
    116  1.13     oster 	/* end-of-list marker */
    117  1.13     oster 	{'\0', NULL, NULL, NULL}
    118  1.13     oster };
    119  1.28  christos 
    120  1.28  christos static const RF_LayoutSW_t *
    121  1.13     oster rf_GetLayout(RF_ParityConfig_t parityConfig)
    122  1.13     oster {
    123  1.28  christos 	const RF_LayoutSW_t *p;
    124  1.13     oster 
    125  1.13     oster 	/* look up the specific layout */
    126  1.13     oster 	for (p = &mapsw[0]; p->parityConfig; p++)
    127  1.13     oster 		if (p->parityConfig == parityConfig)
    128  1.13     oster 			break;
    129  1.13     oster 	if (!p->parityConfig)
    130  1.28  christos 		return NULL;
    131  1.28  christos 	return p;
    132  1.13     oster }
    133   1.7     oster 
    134  1.10   thorpej /*
    135  1.10   thorpej  * called from user level to read the configuration file and create
    136   1.1     oster  * a configuration control structure.  This is used in the user-level
    137   1.1     oster  * version of the driver, and in the user-level program that configures
    138   1.1     oster  * the system via ioctl.
    139   1.1     oster  */
    140  1.29       kre int
    141  1.22   xtraeme rf_MakeConfig(char *configname, RF_Config_t *cfgPtr)
    142   1.1     oster {
    143  1.10   thorpej 	int numscanned, val, r, c, retcode, aa, bb, cc;
    144  1.28  christos 	char buf[BUFSIZ], buf1[BUFSIZ], *cp;
    145  1.28  christos 	const RF_LayoutSW_t *lp;
    146  1.10   thorpej 	FILE *fp;
    147  1.10   thorpej 
    148  1.28  christos 	memset(cfgPtr, 0, sizeof(*cfgPtr));
    149  1.10   thorpej 
    150  1.10   thorpej 	fp = fopen(configname, "r");
    151  1.10   thorpej 	if (!fp) {
    152  1.28  christos 		warnx("Can't open config file %s", configname);
    153  1.28  christos 		return -1;
    154  1.10   thorpej 	}
    155  1.10   thorpej 	rewind(fp);
    156  1.28  christos 	if (rf_search_file_for_start_of("array", buf, sizeof(buf), fp)) {
    157  1.28  christos 		warnx("Unable to find start of \"array\" params in config "
    158  1.28  christos 		    "file %s", configname);
    159  1.10   thorpej 		retcode = -1;
    160  1.10   thorpej 		goto out;
    161  1.10   thorpej 	}
    162  1.28  christos 	rf_get_next_nonblank_line(buf, sizeof(buf), fp,
    163  1.28  christos 	    "Config file error (\"array\" section):  unable to get numRow "
    164  1.28  christos 	    "and numCol");
    165  1.10   thorpej 
    166  1.10   thorpej 	/*
    167  1.29       kre 	 * wackiness with aa, bb, cc to get around size problems on
    168  1.29       kre 	 * different platforms
    169  1.29       kre 	 */
    170  1.10   thorpej 	numscanned = sscanf(buf, "%d %d %d", &aa, &bb, &cc);
    171  1.10   thorpej 	if (numscanned != 3) {
    172  1.28  christos 		warnx("Config file error (\"array\" section): unable to get "
    173  1.28  christos 		    "numRow, numCol, numSpare");
    174  1.10   thorpej 		retcode = -1;
    175  1.10   thorpej 		goto out;
    176  1.10   thorpej 	}
    177  1.10   thorpej 	cfgPtr->numRow = (RF_RowCol_t) aa;
    178  1.10   thorpej 	cfgPtr->numCol = (RF_RowCol_t) bb;
    179  1.10   thorpej 	cfgPtr->numSpare = (RF_RowCol_t) cc;
    180  1.10   thorpej 
    181  1.10   thorpej 	/* debug section is optional */
    182  1.10   thorpej 	for (c = 0; c < RF_MAXDBGV; c++)
    183  1.10   thorpej 		cfgPtr->debugVars[c][0] = '\0';
    184  1.10   thorpej 	rewind(fp);
    185  1.28  christos 	if (!rf_search_file_for_start_of("debug", buf, sizeof(buf), fp)) {
    186  1.10   thorpej 		for (c = 0; c < RF_MAXDBGV; c++) {
    187  1.28  christos 			if (rf_get_next_nonblank_line(buf, sizeof(buf), fp,
    188  1.28  christos 			    NULL))
    189  1.10   thorpej 				break;
    190  1.28  christos 			cp = rf_find_non_white(buf, 0);
    191  1.28  christos 			if (!strncmp(cp, "START", sizeof("START") - 1))
    192  1.10   thorpej 				break;
    193  1.18    itojun 			(void) strlcpy(&cfgPtr->debugVars[c][0], cp,
    194  1.18    itojun 			    sizeof(cfgPtr->debugVars[c]));
    195  1.10   thorpej 		}
    196  1.10   thorpej 	}
    197  1.10   thorpej 	rewind(fp);
    198  1.18    itojun 	strlcpy(cfgPtr->diskQueueType, "fifo", sizeof(cfgPtr->diskQueueType));
    199  1.10   thorpej 	cfgPtr->maxOutstandingDiskReqs = 1;
    200  1.29       kre 
    201  1.10   thorpej 	/* scan the file for the block related to disk queues */
    202  1.28  christos 	if (rf_search_file_for_start_of("queue", buf, sizeof(buf), fp) ||
    203  1.28  christos 	    rf_get_next_nonblank_line(buf, sizeof(buf), fp, NULL)) {
    204  1.28  christos 		warnx("[No disk queue discipline specified in config file %s. "
    205  1.28  christos 		    "Using %s.]", configname, cfgPtr->diskQueueType);
    206  1.10   thorpej 	}
    207  1.10   thorpej 
    208  1.29       kre 	/*
    209  1.29       kre 	 * the queue specifier line contains two entries: 1st char of first
    210  1.10   thorpej 	 * word specifies queue to be used 2nd word specifies max num reqs
    211  1.29       kre 	 * that can be outstanding on the disk itself (typically 1)
    212  1.29       kre 	 */
    213  1.28  christos 	if (sscanf(buf, "%s %d", buf1, &val) != 2) {
    214  1.28  christos 		warnx("Can't determine queue type and/or max outstanding "
    215  1.28  christos 		    "reqs from line: %*s", (int)(sizeof(buf) - 1), buf);
    216  1.28  christos 		warnx("Using %s-%d", cfgPtr->diskQueueType,
    217  1.28  christos 		    cfgPtr->maxOutstandingDiskReqs);
    218  1.10   thorpej 	} else {
    219  1.10   thorpej 		char *ch;
    220  1.28  christos 		memcpy(cfgPtr->diskQueueType, buf1,
    221  1.10   thorpej 		    RF_MIN(sizeof(cfgPtr->diskQueueType), strlen(buf1) + 1));
    222  1.10   thorpej 		for (ch = buf1; *ch; ch++) {
    223  1.10   thorpej 			if (*ch == ' ') {
    224  1.10   thorpej 				*ch = '\0';
    225  1.10   thorpej 				break;
    226  1.10   thorpej 			}
    227  1.10   thorpej 		}
    228  1.10   thorpej 		cfgPtr->maxOutstandingDiskReqs = val;
    229  1.10   thorpej 	}
    230  1.10   thorpej 
    231  1.10   thorpej 	rewind(fp);
    232  1.10   thorpej 
    233  1.28  christos 	if (rf_search_file_for_start_of("disks", buf, sizeof(buf), fp)) {
    234  1.28  christos 		warnx("Can't find \"disks\" section in config file %s",
    235  1.28  christos 		    configname);
    236  1.10   thorpej 		retcode = -1;
    237  1.10   thorpej 		goto out;
    238  1.10   thorpej 	}
    239  1.10   thorpej 	for (r = 0; r < cfgPtr->numRow; r++) {
    240  1.10   thorpej 		for (c = 0; c < cfgPtr->numCol; c++) {
    241  1.28  christos 			char b1[MAXPATHLEN];
    242  1.27    kardel 			const char *b;
    243  1.27    kardel 
    244  1.10   thorpej 			if (rf_get_next_nonblank_line(
    245  1.27    kardel 			    buf, sizeof(buf), fp, NULL)) {
    246  1.28  christos 				warnx("Config file error: unable to get device "
    247  1.28  christos 				    "file for disk at row %d col %d", r, c);
    248  1.10   thorpej 				retcode = -1;
    249  1.10   thorpej 				goto out;
    250  1.10   thorpej 			}
    251  1.27    kardel 
    252  1.29       kre 			b = getfsspecname(b1, sizeof(b1), buf);
    253  1.29       kre 			if (b == NULL) {
    254  1.28  christos 				warnx("Config file error: warning: unable to "
    255  1.28  christos 				    "get device file for disk at row %d col "
    256  1.28  christos 				    "%d: %s", r, c, b1);
    257  1.27    kardel 				b = buf;
    258  1.29       kre 			}
    259  1.27    kardel 
    260  1.28  christos 			strlcpy(&cfgPtr->devnames[r][c][0], b,
    261  1.28  christos 			    sizeof(cfgPtr->devnames[r][c][0]));
    262  1.10   thorpej 		}
    263  1.10   thorpej 	}
    264  1.10   thorpej 
    265  1.10   thorpej 	/* "spare" section is optional */
    266  1.10   thorpej 	rewind(fp);
    267  1.28  christos 	if (rf_search_file_for_start_of("spare", buf, sizeof(buf), fp))
    268  1.10   thorpej 		cfgPtr->numSpare = 0;
    269  1.10   thorpej 	for (c = 0; c < cfgPtr->numSpare; c++) {
    270  1.28  christos 		char b1[MAXPATHLEN];
    271  1.27    kardel 		const char *b;
    272  1.27    kardel 
    273  1.28  christos 		if (rf_get_next_nonblank_line(buf, sizeof(buf), fp, NULL)) {
    274  1.28  christos 			warnx("Config file error: unable to get device file "
    275  1.28  christos 			    "for spare disk %d", c);
    276  1.10   thorpej 			retcode = -1;
    277  1.10   thorpej 			goto out;
    278  1.10   thorpej 		}
    279  1.27    kardel 
    280  1.27    kardel 		b = getfsspecname(b1, sizeof(b1), buf);
    281  1.27    kardel 		if (b == NULL) {
    282  1.28  christos 			warnx("Config file error: warning: unable to get "
    283  1.28  christos 			    "device file for spare disk %d: %s", c, b);
    284  1.27    kardel 			b = buf;
    285  1.27    kardel 		}
    286  1.27    kardel 
    287  1.29       kre 	        strlcpy(&cfgPtr->spare_names[r][0], b,
    288  1.28  christos 		    sizeof(cfgPtr->spare_names[r][0]));
    289  1.10   thorpej 	}
    290  1.10   thorpej 
    291  1.10   thorpej 	/* scan the file for the block related to layout */
    292  1.10   thorpej 	rewind(fp);
    293  1.28  christos 	if (rf_search_file_for_start_of("layout", buf, sizeof(buf), fp)) {
    294  1.28  christos 		warnx("Can't find \"layout\" section in configuration file %s",
    295  1.28  christos 		    configname);
    296  1.10   thorpej 		retcode = -1;
    297  1.10   thorpej 		goto out;
    298  1.10   thorpej 	}
    299  1.28  christos 	if (rf_get_next_nonblank_line(buf, sizeof(buf), fp, NULL)) {
    300  1.28  christos 		warnx("Config file error (\"layout\" section): unable to find "
    301  1.28  christos 		    "common layout param line");
    302  1.10   thorpej 		retcode = -1;
    303  1.10   thorpej 		goto out;
    304  1.10   thorpej 	}
    305  1.10   thorpej 	c = sscanf(buf, "%d %d %d %c", &aa, &bb, &cc, &cfgPtr->parityConfig);
    306  1.10   thorpej 	cfgPtr->sectPerSU = (RF_SectorNum_t) aa;
    307  1.10   thorpej 	cfgPtr->SUsPerPU = (RF_StripeNum_t) bb;
    308  1.10   thorpej 	cfgPtr->SUsPerRU = (RF_StripeNum_t) cc;
    309  1.10   thorpej 	if (c != 4) {
    310  1.28  christos 		warnx("Unable to scan common layout line");
    311  1.10   thorpej 		retcode = -1;
    312  1.10   thorpej 		goto out;
    313  1.10   thorpej 	}
    314  1.10   thorpej 	lp = rf_GetLayout(cfgPtr->parityConfig);
    315  1.10   thorpej 	if (lp == NULL) {
    316  1.28  christos 		warnx("Unknown parity config '%c'",
    317  1.10   thorpej 		    cfgPtr->parityConfig);
    318  1.10   thorpej 		retcode = -1;
    319  1.10   thorpej 		goto out;
    320  1.10   thorpej 	}
    321  1.10   thorpej 
    322  1.28  christos 	retcode = lp->MakeLayoutSpecific(fp, cfgPtr,
    323  1.28  christos 	    lp->makeLayoutSpecificArg);
    324   1.1     oster out:
    325  1.10   thorpej 	fclose(fp);
    326  1.10   thorpej 	if (retcode < 0)
    327  1.10   thorpej 		retcode = errno = EINVAL;
    328  1.10   thorpej 	else
    329  1.10   thorpej 		errno = retcode;
    330  1.28  christos 	return retcode;
    331   1.1     oster }
    332   1.1     oster 
    333   1.1     oster 
    334  1.29       kre /*
    335  1.29       kre  * used in architectures such as RAID0 where there is no layout-specific
    336   1.1     oster  * information to be passed into the configuration code.
    337   1.1     oster  */
    338  1.29       kre int
    339  1.22   xtraeme rf_MakeLayoutSpecificNULL(FILE *fp, RF_Config_t *cfgPtr, void *ignored)
    340   1.1     oster {
    341  1.10   thorpej 	cfgPtr->layoutSpecificSize = 0;
    342  1.10   thorpej 	cfgPtr->layoutSpecific = NULL;
    343  1.28  christos 	return 0;
    344   1.1     oster }
    345   1.1     oster 
    346  1.29       kre int
    347  1.22   xtraeme rf_MakeLayoutSpecificDeclustered(FILE *configfp, RF_Config_t *cfgPtr, void *arg)
    348   1.1     oster {
    349  1.10   thorpej 	int b, v, k, r, lambda, norotate, i, val, distSpare;
    350  1.10   thorpej 	char *cfgBuf, *bdfile, *p, *smname;
    351  1.28  christos 	char buf[BUFSIZ], smbuf[BUFSIZ];
    352  1.10   thorpej 	FILE *fp;
    353  1.10   thorpej 
    354  1.10   thorpej 	distSpare = *((int *) arg);
    355  1.10   thorpej 
    356  1.10   thorpej 	/* get the block design file name */
    357  1.28  christos 	if (rf_get_next_nonblank_line(buf, sizeof(buf), configfp,
    358  1.28  christos 	    "Can't find block design file name in config file"))
    359  1.28  christos 		return EINVAL;
    360  1.28  christos 	bdfile = rf_find_non_white(buf, 1);
    361  1.10   thorpej 	/* open bd file, check validity of configuration */
    362  1.10   thorpej 	if ((fp = fopen(bdfile, "r")) == NULL) {
    363  1.28  christos 		warn("RAID: config error: Can't open layout table file %s",
    364  1.28  christos 		    bdfile);
    365  1.28  christos 		return EINVAL;
    366  1.28  christos 	}
    367  1.28  christos 	if (fgets(buf, sizeof(buf), fp) == NULL) {
    368  1.28  christos 		warnx("RAID: config error: Can't read layout from layout "
    369  1.28  christos 		    "table file %s", bdfile);
    370  1.23       dan 		fclose(fp);
    371  1.28  christos 		return EINVAL;
    372  1.12       wiz 	}
    373  1.28  christos 	i = sscanf(buf, "%u %u %u %u %u %u",
    374  1.28  christos 	    &b, &v, &k, &r, &lambda, &norotate);
    375  1.10   thorpej 	if (i == 5)
    376  1.10   thorpej 		norotate = 0;	/* no-rotate flag is optional */
    377  1.10   thorpej 	else if (i != 6) {
    378  1.28  christos 		warnx("Unable to parse header line in block design file");
    379  1.23       dan 		fclose(fp);
    380  1.28  christos 		return EINVAL;
    381  1.10   thorpej 	}
    382  1.29       kre 	/*
    383  1.29       kre 	 * set the sparemap directory.  In the in-kernel version, there's a
    384  1.29       kre 	 * daemon that's responsible for finding the sparemaps
    385  1.29       kre 	 */
    386  1.10   thorpej 	if (distSpare) {
    387  1.28  christos 		if (rf_get_next_nonblank_line(smbuf, sizeof(buf), configfp,
    388  1.28  christos 		    "Can't find sparemap file name in config file")) {
    389  1.23       dan 			fclose(fp);
    390  1.28  christos 			return EINVAL;
    391  1.10   thorpej 		}
    392  1.28  christos 		smname = rf_find_non_white(smbuf, 1);
    393  1.10   thorpej 	} else {
    394  1.10   thorpej 		smbuf[0] = '\0';
    395  1.10   thorpej 		smname = smbuf;
    396  1.10   thorpej 	}
    397  1.10   thorpej 
    398  1.10   thorpej 	/* allocate a buffer to hold the configuration info */
    399  1.10   thorpej 	cfgPtr->layoutSpecificSize = RF_SPAREMAP_NAME_LEN +
    400  1.10   thorpej 	    6 * sizeof(int) + b * k;
    401  1.21     oster 
    402  1.10   thorpej 	cfgBuf = (char *) malloc(cfgPtr->layoutSpecificSize);
    403  1.23       dan 	if (cfgBuf == NULL) {
    404  1.23       dan 		fclose(fp);
    405  1.28  christos 		return ENOMEM;
    406  1.23       dan 	}
    407  1.10   thorpej 	cfgPtr->layoutSpecific = (void *) cfgBuf;
    408  1.10   thorpej 	p = cfgBuf;
    409  1.10   thorpej 
    410  1.10   thorpej 	/* install name of sparemap file */
    411  1.10   thorpej 	for (i = 0; smname[i]; i++)
    412  1.10   thorpej 		*p++ = smname[i];
    413  1.10   thorpej 	/* pad with zeros */
    414  1.10   thorpej 	while (i < RF_SPAREMAP_NAME_LEN) {
    415  1.10   thorpej 		*p++ = '\0';
    416  1.10   thorpej 		i++;
    417  1.10   thorpej 	}
    418  1.10   thorpej 
    419  1.10   thorpej 	/*
    420  1.29       kre 	 * fill in the buffer with the block design parameters
    421  1.29       kre 	 * and then the block design itself
    422  1.29       kre 	 */
    423  1.10   thorpej 	*((int *) p) = b;
    424  1.10   thorpej 	p += sizeof(int);
    425  1.10   thorpej 	*((int *) p) = v;
    426  1.10   thorpej 	p += sizeof(int);
    427  1.10   thorpej 	*((int *) p) = k;
    428  1.10   thorpej 	p += sizeof(int);
    429  1.10   thorpej 	*((int *) p) = r;
    430  1.10   thorpej 	p += sizeof(int);
    431  1.10   thorpej 	*((int *) p) = lambda;
    432  1.10   thorpej 	p += sizeof(int);
    433  1.10   thorpej 	*((int *) p) = norotate;
    434  1.10   thorpej 	p += sizeof(int);
    435  1.10   thorpej 
    436  1.10   thorpej 	while (fscanf(fp, "%d", &val) == 1)
    437  1.10   thorpej 		*p++ = (char) val;
    438  1.10   thorpej 	fclose(fp);
    439  1.24     lukem 	if ((unsigned int)(p - cfgBuf) != cfgPtr->layoutSpecificSize) {
    440  1.28  christos 		warnx("Size mismatch creating layout specific data: is %tu sb "
    441  1.28  christos 		    "%zu bytes", p - cfgBuf, 6 * sizeof(int) + b * k);
    442  1.28  christos 		return EINVAL;
    443  1.10   thorpej 	}
    444  1.28  christos 	return 0;
    445   1.1     oster }
    446   1.1     oster 
    447   1.1     oster /****************************************************************************
    448   1.1     oster  *
    449   1.1     oster  * utilities
    450   1.1     oster  *
    451   1.1     oster  ***************************************************************************/
    452   1.7     oster 
    453   1.7     oster /* finds a non-white character in the line */
    454  1.28  christos static char *
    455  1.28  christos rf_find_non_white(char *p, int eatnl)
    456   1.7     oster {
    457  1.28  christos 	for (; *p != '\0' && (*p == ' ' || *p == '\t'); p++)
    458  1.28  christos 		continue;
    459  1.28  christos 	if (*p == '\n' && eatnl)
    460  1.28  christos 		*p = '\0';
    461  1.28  christos 	return p;
    462   1.7     oster }
    463   1.7     oster 
    464   1.7     oster /* finds a white character in the line */
    465  1.28  christos static char *
    466   1.7     oster rf_find_white(char *p)
    467   1.7     oster {
    468  1.28  christos 	for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
    469  1.28  christos 		continue;
    470  1.28  christos 	return p;
    471   1.7     oster }
    472   1.1     oster 
    473  1.10   thorpej /*
    474  1.10   thorpej  * searches a file for a line that says "START string", where string is
    475   1.1     oster  * specified as a parameter
    476   1.1     oster  */
    477  1.29       kre static int
    478  1.22   xtraeme rf_search_file_for_start_of(const char *string, char *buf, int len, FILE *fp)
    479   1.1     oster {
    480  1.10   thorpej 	char *p;
    481   1.1     oster 
    482  1.10   thorpej 	while (1) {
    483  1.10   thorpej 		if (fgets(buf, len, fp) == NULL)
    484  1.28  christos 			return -1;
    485  1.28  christos 		p = rf_find_non_white(buf, 0);
    486  1.10   thorpej 		if (!strncmp(p, "START", strlen("START"))) {
    487  1.10   thorpej 			p = rf_find_white(p);
    488  1.28  christos 			p = rf_find_non_white(p, 0);
    489  1.10   thorpej 			if (!strncmp(p, string, strlen(string)))
    490  1.28  christos 				return 0;
    491  1.10   thorpej 		}
    492  1.10   thorpej 	}
    493   1.1     oster }
    494   1.1     oster 
    495   1.1     oster /* reads from file fp into buf until it finds an interesting line */
    496  1.29       kre static int
    497  1.22   xtraeme rf_get_next_nonblank_line(char *buf, int len, FILE *fp, const char *errmsg)
    498   1.1     oster {
    499  1.10   thorpej 	char *p;
    500  1.20     oster 	int l;
    501   1.1     oster 
    502  1.19     oster 	while (fgets(buf, len, fp) != NULL) {
    503  1.28  christos 		p = rf_find_non_white(buf, 0);
    504  1.10   thorpej 		if (*p == '\n' || *p == '\0' || *p == '#')
    505  1.10   thorpej 			continue;
    506  1.28  christos 		l = strlen(buf) - 1;
    507  1.28  christos 		while (l >= 0 && (buf[l] == ' ' || buf[l] == '\n')) {
    508  1.28  christos 			buf[l] = '\0';
    509  1.20     oster 			l--;
    510  1.20     oster 		}
    511  1.28  christos 		return 0;
    512  1.10   thorpej 	}
    513  1.10   thorpej 	if (errmsg)
    514  1.28  christos 		warnx("%s", errmsg);
    515  1.28  christos 	return 1;
    516   1.1     oster }
    517   1.1     oster 
    518  1.10   thorpej /*
    519  1.10   thorpej  * Allocates an array for the spare table, and initializes it from a file.
    520   1.1     oster  * In the user-level version, this is called when recon is initiated.
    521   1.1     oster  * When/if I move recon into the kernel, there'll be a daemon that does
    522   1.1     oster  * an ioctl into raidframe which will block until a spare table is needed.
    523   1.1     oster  * When it returns, it will read a spare table from the file system,
    524   1.1     oster  * pass it into the kernel via a different ioctl, and then block again
    525   1.1     oster  * on the original ioctl.
    526   1.1     oster  *
    527   1.1     oster  * This is specific to the declustered layout, but doesn't belong in
    528   1.1     oster  * rf_decluster.c because it uses stuff that can't be compiled into
    529   1.1     oster  * the kernel, and it needs to be compiled into the user-level sparemap daemon.
    530   1.1     oster  */
    531  1.10   thorpej void *
    532  1.22   xtraeme rf_ReadSpareTable(RF_SparetWait_t *req, char *fname)
    533   1.1     oster {
    534  1.10   thorpej 	int i, j, numFound, linecount, tableNum, tupleNum,
    535  1.10   thorpej 	    spareDisk, spareBlkOffset;
    536  1.28  christos 	char buf[BUFSIZ], targString[BUFSIZ], errString[BUFSIZ];
    537  1.10   thorpej 	RF_SpareTableEntry_t **table;
    538  1.26  christos 	FILE *fp = NULL;
    539  1.10   thorpej 
    540  1.10   thorpej 	/* allocate and initialize the table */
    541  1.26  christos 	table = calloc(req->TablesPerSpareRegion, sizeof(*table));
    542  1.21     oster 	if (table == NULL) {
    543  1.26  christos 		warn("%s: Unable to allocate table", __func__);
    544  1.26  christos 		return NULL;
    545  1.21     oster 	}
    546  1.10   thorpej 	for (i = 0; i < req->TablesPerSpareRegion; i++) {
    547  1.26  christos 		table[i] = calloc(req->BlocksPerTable, sizeof(**table));
    548  1.21     oster 		if (table[i] == NULL) {
    549  1.26  christos 			warn("%s: Unable to allocate table", __func__);
    550  1.26  christos 			goto out;
    551  1.21     oster 		}
    552  1.10   thorpej 		for (j = 0; j < req->BlocksPerTable; j++)
    553  1.10   thorpej 			table[i][j].spareDisk =
    554  1.10   thorpej 			    table[i][j].spareBlockOffsetInSUs = -1;
    555  1.10   thorpej 	}
    556  1.10   thorpej 
    557  1.10   thorpej 	/* 2.  open sparemap file, sanity check */
    558  1.10   thorpej 	if ((fp = fopen(fname, "r")) == NULL) {
    559  1.26  christos 		warn("%s: Can't open sparemap file %s", __func__, fname);
    560  1.26  christos 		goto out;
    561  1.10   thorpej 	}
    562  1.10   thorpej 	if (rf_get_next_nonblank_line(buf, 1024, fp,
    563  1.28  christos 	    "Invalid sparemap file:  can't find header line"))
    564  1.26  christos 		goto out;
    565  1.26  christos 
    566  1.26  christos 	size_t len = strlen(buf);
    567  1.26  christos 	if (len != 0 && buf[len - 1] == '\n')
    568  1.26  christos 		buf[len - 1] = '\0';
    569  1.10   thorpej 
    570  1.18    itojun 	snprintf(targString, sizeof(targString), "fdisk %d\n", req->fcol);
    571  1.18    itojun 	snprintf(errString, sizeof(errString),
    572  1.28  christos 	    "Invalid sparemap file: Can't find \"fdisk %d\" line", req->fcol);
    573  1.26  christos 	for (;;) {
    574  1.28  christos 		rf_get_next_nonblank_line(buf, sizeof(buf), fp, errString);
    575  1.10   thorpej 		if (!strncmp(buf, targString, strlen(targString)))
    576  1.10   thorpej 			break;
    577  1.10   thorpej 	}
    578  1.10   thorpej 
    579  1.10   thorpej 	/* no more blank lines or comments allowed now */
    580  1.10   thorpej 	linecount = req->TablesPerSpareRegion * req->TableDepthInPUs;
    581  1.10   thorpej 	for (i = 0; i < linecount; i++) {
    582  1.10   thorpej 		numFound = fscanf(fp, " %d %d %d %d", &tableNum, &tupleNum,
    583  1.10   thorpej 		    &spareDisk, &spareBlkOffset);
    584  1.10   thorpej 		if (numFound != 4) {
    585  1.25  christos 			warnx("Sparemap file prematurely exhausted after %d "
    586  1.25  christos 			    "of %d lines", i, linecount);
    587  1.26  christos 			goto out;
    588  1.10   thorpej 		}
    589  1.10   thorpej 
    590  1.10   thorpej 		table[tableNum][tupleNum].spareDisk = spareDisk;
    591  1.10   thorpej 		table[tableNum][tupleNum].spareBlockOffsetInSUs =
    592  1.10   thorpej 		    spareBlkOffset * req->SUsPerPU;
    593  1.10   thorpej 	}
    594   1.1     oster 
    595  1.10   thorpej 	fclose(fp);
    596  1.28  christos 	return (void *) table;
    597  1.26  christos out:
    598  1.26  christos 	if (fp)
    599  1.26  christos 		fclose(fp);
    600  1.26  christos 	for (i = 0; i < req->TablesPerSpareRegion; i++)
    601  1.26  christos 		free(table[i]);
    602  1.26  christos 	free(table);
    603  1.26  christos 	return NULL;
    604   1.1     oster }
    605