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