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