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