xref: /netbsd-src/sbin/raidctl/rf_configure.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*	$NetBSD: rf_configure.c,v 1.24 2009/04/06 12:47:20 lukem 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@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.24 2009/04/06 12:47:20 lukem 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 		fclose(fp);
333 		return (EINVAL);
334 	}
335 	i = sscanf(buf, "%u %u %u %u %u %u", &b, &v, &k, &r, &lambda, &norotate);
336 	if (i == 5)
337 		norotate = 0;	/* no-rotate flag is optional */
338 	else if (i != 6) {
339 		RF_ERRORMSG("Unable to parse header line in block design file\n");
340 		fclose(fp);
341 		return (EINVAL);
342 	}
343 	/* set the sparemap directory.  In the in-kernel version, there's a
344 	 * daemon that's responsible for finding the sparemaps */
345 	if (distSpare) {
346 		if (rf_get_next_nonblank_line(smbuf, 256, configfp,
347 		    "Can't find sparemap file name in config file\n")) {
348 			fclose(fp);
349 			return (EINVAL);
350 		}
351 		smname = rf_find_non_white(smbuf);
352 		if (smname[strlen(smname) - 1] == '\n') {
353 			/* strip newline char */
354 			smname[strlen(smname) - 1] = '\0';
355 		}
356 	} else {
357 		smbuf[0] = '\0';
358 		smname = smbuf;
359 	}
360 
361 	/* allocate a buffer to hold the configuration info */
362 	cfgPtr->layoutSpecificSize = RF_SPAREMAP_NAME_LEN +
363 	    6 * sizeof(int) + b * k;
364 
365 	cfgBuf = (char *) malloc(cfgPtr->layoutSpecificSize);
366 	if (cfgBuf == NULL) {
367 		fclose(fp);
368 		return (ENOMEM);
369 	}
370 	cfgPtr->layoutSpecific = (void *) cfgBuf;
371 	p = cfgBuf;
372 
373 	/* install name of sparemap file */
374 	for (i = 0; smname[i]; i++)
375 		*p++ = smname[i];
376 	/* pad with zeros */
377 	while (i < RF_SPAREMAP_NAME_LEN) {
378 		*p++ = '\0';
379 		i++;
380 	}
381 
382 	/*
383          * fill in the buffer with the block design parameters
384          * and then the block design itself
385          */
386 	*((int *) p) = b;
387 	p += sizeof(int);
388 	*((int *) p) = v;
389 	p += sizeof(int);
390 	*((int *) p) = k;
391 	p += sizeof(int);
392 	*((int *) p) = r;
393 	p += sizeof(int);
394 	*((int *) p) = lambda;
395 	p += sizeof(int);
396 	*((int *) p) = norotate;
397 	p += sizeof(int);
398 
399 	while (fscanf(fp, "%d", &val) == 1)
400 		*p++ = (char) val;
401 	fclose(fp);
402 	if ((unsigned int)(p - cfgBuf) != cfgPtr->layoutSpecificSize) {
403 		RF_ERRORMSG2("Size mismatch creating layout specific data: is %d sb %d bytes\n", (int) (p - cfgBuf), (int) (6 * sizeof(int) + b * k));
404 		return (EINVAL);
405 	}
406 	return (0);
407 }
408 
409 /****************************************************************************
410  *
411  * utilities
412  *
413  ***************************************************************************/
414 
415 /* finds a non-white character in the line */
416 char *
417 rf_find_non_white(char *p)
418 {
419 	for (; *p != '\0' && (*p == ' ' || *p == '\t'); p++);
420 	return (p);
421 }
422 
423 /* finds a white character in the line */
424 char *
425 rf_find_white(char *p)
426 {
427 	for (; *p != '\0' && (*p != ' ' && *p != '\t'); p++);
428 	return (p);
429 }
430 
431 /*
432  * searches a file for a line that says "START string", where string is
433  * specified as a parameter
434  */
435 static int
436 rf_search_file_for_start_of(const char *string, char *buf, int len, FILE *fp)
437 {
438 	char *p;
439 
440 	while (1) {
441 		if (fgets(buf, len, fp) == NULL)
442 			return (-1);
443 		p = rf_find_non_white(buf);
444 		if (!strncmp(p, "START", strlen("START"))) {
445 			p = rf_find_white(p);
446 			p = rf_find_non_white(p);
447 			if (!strncmp(p, string, strlen(string)))
448 				return (0);
449 		}
450 	}
451 }
452 
453 /* reads from file fp into buf until it finds an interesting line */
454 int
455 rf_get_next_nonblank_line(char *buf, int len, FILE *fp, const char *errmsg)
456 {
457 	char *p;
458 	int l;
459 
460 	while (fgets(buf, len, fp) != NULL) {
461 		p = rf_find_non_white(buf);
462 		if (*p == '\n' || *p == '\0' || *p == '#')
463 			continue;
464 		l = strlen(buf)-1;
465 		while (l>=0 && (buf[l]==' ' || buf[l]=='\n')) {
466 			buf[l]='\0';
467 			l--;
468 		}
469 		return (0);
470 	}
471 	if (errmsg)
472 		RF_ERRORMSG1("%s", errmsg);
473 	return (1);
474 }
475 
476 /*
477  * Allocates an array for the spare table, and initializes it from a file.
478  * In the user-level version, this is called when recon is initiated.
479  * When/if I move recon into the kernel, there'll be a daemon that does
480  * an ioctl into raidframe which will block until a spare table is needed.
481  * When it returns, it will read a spare table from the file system,
482  * pass it into the kernel via a different ioctl, and then block again
483  * on the original ioctl.
484  *
485  * This is specific to the declustered layout, but doesn't belong in
486  * rf_decluster.c because it uses stuff that can't be compiled into
487  * the kernel, and it needs to be compiled into the user-level sparemap daemon.
488  *
489  */
490 void *
491 rf_ReadSpareTable(RF_SparetWait_t *req, char *fname)
492 {
493 	int i, j, numFound, linecount, tableNum, tupleNum,
494 	    spareDisk, spareBlkOffset;
495 	char buf[1024], targString[100], errString[100];
496 	RF_SpareTableEntry_t **table;
497 	FILE *fp;
498 
499 	/* allocate and initialize the table */
500 	table = malloc(req->TablesPerSpareRegion *
501 		       sizeof(RF_SpareTableEntry_t *));
502 	if (table == NULL) {
503 		fprintf(stderr,
504 			"rf_ReadSpareTable: Unable to allocate table\n");
505 		return (NULL);
506 	}
507 	for (i = 0; i < req->TablesPerSpareRegion; i++) {
508 		table[i] = malloc(req->BlocksPerTable *
509 				  sizeof(RF_SpareTableEntry_t));
510 		if (table[i] == NULL) {
511 			fprintf(stderr,
512 				"rf_ReadSpareTable: Unable to allocate table\n");
513 			return (NULL);  /* XXX should cleanup too! */
514 		}
515 		for (j = 0; j < req->BlocksPerTable; j++)
516 			table[i][j].spareDisk =
517 			    table[i][j].spareBlockOffsetInSUs = -1;
518 	}
519 
520 	/* 2.  open sparemap file, sanity check */
521 	if ((fp = fopen(fname, "r")) == NULL) {
522 		fprintf(stderr,
523 		    "rf_ReadSpareTable:  Can't open sparemap file %s\n", fname);
524 		return (NULL);
525 	}
526 	if (rf_get_next_nonblank_line(buf, 1024, fp,
527 	    "Invalid sparemap file:  can't find header line\n")) {
528 		fclose(fp);
529 		return (NULL);
530 	}
531 	if (buf[strlen(buf) - 1] == '\n')
532 		buf[strlen(buf) - 1] = '\0';
533 
534 	snprintf(targString, sizeof(targString), "fdisk %d\n", req->fcol);
535 	snprintf(errString, sizeof(errString),
536 	    "Invalid sparemap file:  can't find \"fdisk %d\" line\n",
537 	    req->fcol);
538 	while (1) {
539 		rf_get_next_nonblank_line(buf, 1024, fp, errString);
540 		if (!strncmp(buf, targString, strlen(targString)))
541 			break;
542 	}
543 
544 	/* no more blank lines or comments allowed now */
545 	linecount = req->TablesPerSpareRegion * req->TableDepthInPUs;
546 	for (i = 0; i < linecount; i++) {
547 		numFound = fscanf(fp, " %d %d %d %d", &tableNum, &tupleNum,
548 		    &spareDisk, &spareBlkOffset);
549 		if (numFound != 4) {
550 			fprintf(stderr, "Sparemap file prematurely exhausted after %d of %d lines\n", i, linecount);
551 			fclose(fp);
552 			return (NULL);
553 		}
554 
555 		table[tableNum][tupleNum].spareDisk = spareDisk;
556 		table[tableNum][tupleNum].spareBlockOffsetInSUs =
557 		    spareBlkOffset * req->SUsPerPU;
558 	}
559 
560 	fclose(fp);
561 	return ((void *) table);
562 }
563