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