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