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