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