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