rf_configure.c revision 1.1 1 1.1 oster /*
2 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
3 1.1 oster * All rights reserved.
4 1.1 oster *
5 1.1 oster * Author: Mark Holland
6 1.1 oster *
7 1.1 oster * Permission to use, copy, modify and distribute this software and
8 1.1 oster * its documentation is hereby granted, provided that both the copyright
9 1.1 oster * notice and this permission notice appear in all copies of the
10 1.1 oster * software, derivative works or modified versions, and any portions
11 1.1 oster * thereof, and that both notices appear in supporting documentation.
12 1.1 oster *
13 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
15 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 1.1 oster *
17 1.1 oster * Carnegie Mellon requests users of this software to return to
18 1.1 oster *
19 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
20 1.1 oster * School of Computer Science
21 1.1 oster * Carnegie Mellon University
22 1.1 oster * Pittsburgh PA 15213-3890
23 1.1 oster *
24 1.1 oster * any improvements or extensions that they make and grant Carnegie the
25 1.1 oster * rights to redistribute these changes.
26 1.1 oster */
27 1.1 oster
28 1.1 oster /***************************************************************
29 1.1 oster *
30 1.1 oster * rf_configure.c -- code related to configuring the raidframe system
31 1.1 oster *
32 1.1 oster * configuration is complicated by the fact that we want the same
33 1.1 oster * driver to work both in the kernel and at user level. In the
34 1.1 oster * kernel, we can't read the configuration file, so we configure
35 1.1 oster * by running a user-level program that reads the config file,
36 1.1 oster * creates a data structure describing the configuration and
37 1.1 oster * passes it into the kernel via an ioctl. Since we want the config
38 1.1 oster * code to be common between the two versions of the driver, we
39 1.1 oster * configure using the same two-step process when running at
40 1.1 oster * user level. Of course, at user level, the config structure is
41 1.1 oster * passed directly to the config routine, rather than via ioctl.
42 1.1 oster *
43 1.1 oster * This file is not compiled into the kernel, so we have no
44 1.1 oster * need for KERNEL ifdefs.
45 1.1 oster *
46 1.1 oster **************************************************************/
47 1.1 oster
48 1.1 oster /* $Locker: $
49 1.1 oster * $Log: rf_configure.c,v $
50 1.1 oster * Revision 1.1 1998/11/13 04:34:02 oster
51 1.1 oster * RAIDframe, version 1.1, from the Parallel Data Laboratory at
52 1.1 oster * Carnegie Mellon University. Full RAID implementation, including
53 1.1 oster * levels 0, 1, 4, 5, 6, parity logging, and a few other goodies.
54 1.1 oster * Ported to NetBSD by Greg Oster.
55 1.1 oster *
56 1.1 oster * raidctl is our userland configuration tool for RAIDframe.
57 1.1 oster *
58 1.1 oster * Revision 1.42 1996/08/09 18:47:47 jimz
59 1.1 oster * major -> dev_major
60 1.1 oster *
61 1.1 oster * Revision 1.41 1996/07/29 14:05:12 jimz
62 1.1 oster * fix numPUs/numRUs confusion (everything is now numRUs)
63 1.1 oster * clean up some commenting, return values
64 1.1 oster *
65 1.1 oster * Revision 1.40 1996/07/27 23:36:08 jimz
66 1.1 oster * Solaris port of simulator
67 1.1 oster *
68 1.1 oster * Revision 1.39 1996/07/27 18:39:45 jimz
69 1.1 oster * cleanup sweep
70 1.1 oster *
71 1.1 oster * Revision 1.38 1996/07/18 22:57:14 jimz
72 1.1 oster * port simulator to AIX
73 1.1 oster *
74 1.1 oster * Revision 1.37 1996/06/19 14:58:02 jimz
75 1.1 oster * move layout-specific config parsing hooks into RF_LayoutSW_t
76 1.1 oster * table in rf_layout.c
77 1.1 oster *
78 1.1 oster * Revision 1.36 1996/06/17 14:38:33 jimz
79 1.1 oster * properly #if out RF_DEMO code
80 1.1 oster * fix bug in MakeConfig that was causing weird behavior
81 1.1 oster * in configuration routines (config was not zeroed at start)
82 1.1 oster * clean up genplot handling of stacks
83 1.1 oster *
84 1.1 oster * Revision 1.35 1996/06/05 19:38:32 jimz
85 1.1 oster * fixed up disk queueing types config
86 1.1 oster * added sstf disk queueing
87 1.1 oster * fixed exit bug on diskthreads (ref-ing bad mem)
88 1.1 oster *
89 1.1 oster * Revision 1.34 1996/06/03 23:28:26 jimz
90 1.1 oster * more bugfixes
91 1.1 oster * check in tree to sync for IPDS runs with current bugfixes
92 1.1 oster * there still may be a problem with threads in the script test
93 1.1 oster * getting I/Os stuck- not trivially reproducible (runs ~50 times
94 1.1 oster * in a row without getting stuck)
95 1.1 oster *
96 1.1 oster * Revision 1.33 1996/06/02 17:31:48 jimz
97 1.1 oster * Moved a lot of global stuff into array structure, where it belongs.
98 1.1 oster * Fixed up paritylogging, pss modules in this manner. Some general
99 1.1 oster * code cleanup. Removed lots of dead code, some dead files.
100 1.1 oster *
101 1.1 oster * Revision 1.32 1996/05/30 23:22:16 jimz
102 1.1 oster * bugfixes of serialization, timing problems
103 1.1 oster * more cleanup
104 1.1 oster *
105 1.1 oster * Revision 1.31 1996/05/30 11:29:41 jimz
106 1.1 oster * Numerous bug fixes. Stripe lock release code disagreed with the taking code
107 1.1 oster * about when stripes should be locked (I made it consistent: no parity, no lock)
108 1.1 oster * There was a lot of extra serialization of I/Os which I've removed- a lot of
109 1.1 oster * it was to calculate values for the cache code, which is no longer with us.
110 1.1 oster * More types, function, macro cleanup. Added code to properly quiesce the array
111 1.1 oster * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
112 1.1 oster * before. Fixed memory allocation, freeing bugs.
113 1.1 oster *
114 1.1 oster * Revision 1.30 1996/05/27 18:56:37 jimz
115 1.1 oster * more code cleanup
116 1.1 oster * better typing
117 1.1 oster * compiles in all 3 environments
118 1.1 oster *
119 1.1 oster * Revision 1.29 1996/05/24 01:59:45 jimz
120 1.1 oster * another checkpoint in code cleanup for release
121 1.1 oster * time to sync kernel tree
122 1.1 oster *
123 1.1 oster * Revision 1.28 1996/05/18 19:51:34 jimz
124 1.1 oster * major code cleanup- fix syntax, make some types consistent,
125 1.1 oster * add prototypes, clean out dead code, et cetera
126 1.1 oster *
127 1.1 oster * Revision 1.27 1995/12/12 18:10:06 jimz
128 1.1 oster * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
129 1.1 oster * fix 80-column brain damage in comments
130 1.1 oster *
131 1.1 oster * Revision 1.26 1995/12/01 15:16:36 root
132 1.1 oster * added copyright info
133 1.1 oster *
134 1.1 oster */
135 1.1 oster
136 1.1 oster
137 1.1 oster #include <stdio.h>
138 1.1 oster #include <sys/types.h>
139 1.1 oster #include <sys/stat.h>
140 1.1 oster #include "rf_raid.h"
141 1.1 oster #include "rf_raidframe.h"
142 1.1 oster #include "rf_utils.h"
143 1.1 oster #include "rf_general.h"
144 1.1 oster #include "rf_decluster.h"
145 1.1 oster #include "rf_configure.h"
146 1.1 oster #include "rf_sys.h"
147 1.1 oster
148 1.1 oster /*
149 1.1 oster
150 1.1 oster XXX we include this here so we don't need to drag rf_debugMem.c into
151 1.1 oster the picture... This is userland, afterall...
152 1.1 oster
153 1.1 oster */
154 1.1 oster
155 1.1 oster /* XXX sucky hack to override the defn. of RF_Malloc as given in
156 1.1 oster rf_debugMem.c... but I *really* don't want (nor need) to link with
157 1.1 oster that file here in userland.. GO
158 1.1 oster */
159 1.1 oster
160 1.1 oster #undef RF_Malloc
161 1.1 oster #define RF_Malloc(_p_, _size_, _cast_) \
162 1.1 oster { \
163 1.1 oster _p_ = _cast_ malloc((u_long)_size_); \
164 1.1 oster bzero((char *)_p_, _size_); \
165 1.1 oster }
166 1.1 oster
167 1.1 oster
168 1.1 oster
169 1.1 oster #ifndef SIMULATE
170 1.1 oster static unsigned int dev_name2num(char *s);
171 1.1 oster static unsigned int osf_dev_name2num(char *s);
172 1.1 oster #endif
173 1.1 oster static int rf_search_file_for_start_of(char *string, char *buf, int len,
174 1.1 oster FILE *fp);
175 1.1 oster static int rf_get_next_nonblank_line(char *buf, int len, FILE *fp,
176 1.1 oster char *errmsg);
177 1.1 oster
178 1.1 oster /* called from user level to read the configuration file and create
179 1.1 oster * a configuration control structure. This is used in the user-level
180 1.1 oster * version of the driver, and in the user-level program that configures
181 1.1 oster * the system via ioctl.
182 1.1 oster */
183 1.1 oster int rf_MakeConfig(configname, cfgPtr)
184 1.1 oster char *configname;
185 1.1 oster RF_Config_t *cfgPtr;
186 1.1 oster {
187 1.1 oster int numscanned, val, r, c, retcode, aa, bb, cc;
188 1.1 oster char buf[256], buf1[256], *cp;
189 1.1 oster RF_LayoutSW_t *lp;
190 1.1 oster FILE *fp;
191 1.1 oster
192 1.1 oster bzero((char *)cfgPtr, sizeof(RF_Config_t));
193 1.1 oster
194 1.1 oster fp = fopen(configname, "r");
195 1.1 oster if (!fp) {
196 1.1 oster RF_ERRORMSG1("Can't open config file %s\n",configname);
197 1.1 oster return(-1);
198 1.1 oster }
199 1.1 oster
200 1.1 oster rewind(fp);
201 1.1 oster if (rf_search_file_for_start_of("array", buf, 256, fp)) {
202 1.1 oster RF_ERRORMSG1("Unable to find start of \"array\" params in config file %s\n",configname);
203 1.1 oster retcode = -1; goto out;
204 1.1 oster }
205 1.1 oster rf_get_next_nonblank_line(buf, 256, fp, "Config file error (\"array\" section): unable to get numRow and numCol\n");
206 1.1 oster /*
207 1.1 oster * wackiness with aa, bb, cc to get around size problems on different platforms
208 1.1 oster */
209 1.1 oster numscanned = sscanf(buf,"%d %d %d", &aa, &bb, &cc);
210 1.1 oster if (numscanned != 3) {
211 1.1 oster RF_ERRORMSG("Config file error (\"array\" section): unable to get numRow, numCol, numSpare\n");
212 1.1 oster retcode = -1; goto out;
213 1.1 oster }
214 1.1 oster cfgPtr->numRow = (RF_RowCol_t)aa;
215 1.1 oster cfgPtr->numCol = (RF_RowCol_t)bb;
216 1.1 oster cfgPtr->numSpare = (RF_RowCol_t)cc;
217 1.1 oster
218 1.1 oster /* debug section is optional */
219 1.1 oster for (c=0; c<RF_MAXDBGV; c++)
220 1.1 oster cfgPtr->debugVars[c][0] = '\0';
221 1.1 oster rewind(fp);
222 1.1 oster if (!rf_search_file_for_start_of("debug", buf, 256, fp)) {
223 1.1 oster for (c=0; c < RF_MAXDBGV; c++) {
224 1.1 oster if (rf_get_next_nonblank_line(buf, 256, fp, NULL)) break;
225 1.1 oster cp = rf_find_non_white(buf);
226 1.1 oster if (!strncmp(cp, "START", strlen("START"))) break;
227 1.1 oster (void) strcpy(&cfgPtr->debugVars[c][0], cp);
228 1.1 oster }
229 1.1 oster }
230 1.1 oster
231 1.1 oster rewind(fp);
232 1.1 oster strcpy(cfgPtr->diskQueueType,"fifo");
233 1.1 oster cfgPtr->maxOutstandingDiskReqs = 1;
234 1.1 oster /* scan the file for the block related to disk queues */
235 1.1 oster if (rf_search_file_for_start_of("queue",buf,256,fp)) {
236 1.1 oster RF_ERRORMSG2("[No disk queue discipline specified in config file %s. Using %s.]\n",configname, cfgPtr->diskQueueType);
237 1.1 oster } else {
238 1.1 oster if (rf_get_next_nonblank_line(buf, 256, fp, NULL)) {
239 1.1 oster RF_ERRORMSG2("[No disk queue discipline specified in config file %s. Using %s.]\n",configname, cfgPtr->diskQueueType);
240 1.1 oster }
241 1.1 oster }
242 1.1 oster
243 1.1 oster /* the queue specifier line contains two entries:
244 1.1 oster * 1st char of first word specifies queue to be used
245 1.1 oster * 2nd word specifies max num reqs that can be outstanding on the disk itself (typically 1)
246 1.1 oster */
247 1.1 oster if (sscanf(buf,"%s %d",buf1,&val)!=2) {
248 1.1 oster RF_ERRORMSG1("Can't determine queue type and/or max outstanding reqs from line: %s",buf);
249 1.1 oster RF_ERRORMSG2("Using %s-%d\n", cfgPtr->diskQueueType, cfgPtr->maxOutstandingDiskReqs);
250 1.1 oster } else {
251 1.1 oster char *c;
252 1.1 oster bcopy(buf1, cfgPtr->diskQueueType, RF_MIN(sizeof(cfgPtr->diskQueueType), strlen(buf1)+1));
253 1.1 oster for(c=buf1;*c;c++) {
254 1.1 oster if (*c == ' ') {
255 1.1 oster *c = '\0';
256 1.1 oster break;
257 1.1 oster }
258 1.1 oster }
259 1.1 oster cfgPtr->maxOutstandingDiskReqs = val;
260 1.1 oster }
261 1.1 oster
262 1.1 oster rewind(fp);
263 1.1 oster
264 1.1 oster
265 1.1 oster if (rf_search_file_for_start_of("disks",buf,256,fp)) {
266 1.1 oster RF_ERRORMSG1("Can't find \"disks\" section in config file %s\n",configname);
267 1.1 oster retcode = -1; goto out;
268 1.1 oster }
269 1.1 oster
270 1.1 oster for (r=0; r<cfgPtr->numRow; r++) {
271 1.1 oster for (c=0; c<cfgPtr->numCol; c++) {
272 1.1 oster if (rf_get_next_nonblank_line(&cfgPtr->devnames[r][c][0], 50, fp, NULL)) {
273 1.1 oster RF_ERRORMSG2("Config file error: unable to get device file for disk at row %d col %d\n",r,c);
274 1.1 oster retcode = -1; goto out;
275 1.1 oster }
276 1.1 oster #ifndef SIMULATE
277 1.1 oster val = dev_name2num(&cfgPtr->devnames[r][c][0]);
278 1.1 oster
279 1.1 oster if (val < 0) {
280 1.1 oster RF_ERRORMSG3("Config file error: can't get dev num (dev file '%s') for disk at row %d c %d\n",
281 1.1 oster &cfgPtr->devnames[r][c][0],r,c);
282 1.1 oster retcode = -1; goto out;
283 1.1 oster } else cfgPtr->devs[r][c] = val;
284 1.1 oster #endif /* !SIMULATE */
285 1.1 oster }
286 1.1 oster }
287 1.1 oster
288 1.1 oster /* "spare" section is optional */
289 1.1 oster rewind(fp);
290 1.1 oster if (rf_search_file_for_start_of("spare",buf,256,fp)) cfgPtr->numSpare =0;
291 1.1 oster for (c = 0; c < cfgPtr->numSpare; c++) {
292 1.1 oster if (rf_get_next_nonblank_line(&cfgPtr->spare_names[c][0], 256, fp, NULL)) {
293 1.1 oster RF_ERRORMSG1("Config file error: unable to get device file for spare disk %d\n",c);
294 1.1 oster retcode = -1; goto out;
295 1.1 oster }
296 1.1 oster #ifndef SIMULATE
297 1.1 oster val = dev_name2num(&cfgPtr->spare_names[c][0]);
298 1.1 oster if (val < 0) {
299 1.1 oster RF_ERRORMSG2("Config file error: can't get dev num (dev file '%s') for spare disk %d\n",
300 1.1 oster &cfgPtr->spare_names[c][0],c);
301 1.1 oster retcode = -1; goto out;
302 1.1 oster } else cfgPtr->spare_devs[c] = val;
303 1.1 oster #endif /* !SIMULATE */
304 1.1 oster }
305 1.1 oster
306 1.1 oster /* scan the file for the block related to layout */
307 1.1 oster rewind(fp);
308 1.1 oster if (rf_search_file_for_start_of("layout",buf,256,fp)) {
309 1.1 oster RF_ERRORMSG1("Can't find \"layout\" section in configuration file %s\n",configname);
310 1.1 oster retcode = -1; goto out;
311 1.1 oster }
312 1.1 oster if (rf_get_next_nonblank_line(buf, 256, fp, NULL)) {
313 1.1 oster RF_ERRORMSG("Config file error (\"layout\" section): unable to find common layout param line\n");
314 1.1 oster retcode = -1; goto out;
315 1.1 oster }
316 1.1 oster c = sscanf(buf,"%d %d %d %c", &aa, &bb, &cc, &cfgPtr->parityConfig);
317 1.1 oster cfgPtr->sectPerSU = (RF_SectorNum_t)aa;
318 1.1 oster cfgPtr->SUsPerPU = (RF_StripeNum_t)bb;
319 1.1 oster cfgPtr->SUsPerRU = (RF_StripeNum_t)cc;
320 1.1 oster if (c != 4) {
321 1.1 oster RF_ERRORMSG("Unable to scan common layout line\n");
322 1.1 oster retcode = -1; goto out;
323 1.1 oster }
324 1.1 oster lp = rf_GetLayout(cfgPtr->parityConfig);
325 1.1 oster if (lp == NULL) {
326 1.1 oster RF_ERRORMSG1("Unknown parity config '%c'\n", cfgPtr->parityConfig);
327 1.1 oster retcode = -1;
328 1.1 oster goto out;
329 1.1 oster }
330 1.1 oster
331 1.1 oster /* XXX who cares.. it's not going into the kernel, so we should ignore this... */
332 1.1 oster #ifndef KERNEL
333 1.1 oster retcode = lp->MakeLayoutSpecific(fp, cfgPtr, lp->makeLayoutSpecificArg);
334 1.1 oster #endif
335 1.1 oster out:
336 1.1 oster fclose(fp);
337 1.1 oster if (retcode < 0)
338 1.1 oster retcode = errno = EINVAL;
339 1.1 oster else
340 1.1 oster errno = retcode;
341 1.1 oster return(retcode);
342 1.1 oster }
343 1.1 oster
344 1.1 oster
345 1.1 oster /* used in architectures such as RAID0 where there is no layout-specific
346 1.1 oster * information to be passed into the configuration code.
347 1.1 oster */
348 1.1 oster int rf_MakeLayoutSpecificNULL(fp, cfgPtr, ignored)
349 1.1 oster FILE *fp;
350 1.1 oster RF_Config_t *cfgPtr;
351 1.1 oster void *ignored;
352 1.1 oster {
353 1.1 oster cfgPtr->layoutSpecificSize = 0;
354 1.1 oster cfgPtr->layoutSpecific = NULL;
355 1.1 oster return(0);
356 1.1 oster }
357 1.1 oster
358 1.1 oster int rf_MakeLayoutSpecificDeclustered(configfp, cfgPtr, arg)
359 1.1 oster FILE *configfp;
360 1.1 oster RF_Config_t *cfgPtr;
361 1.1 oster void *arg;
362 1.1 oster {
363 1.1 oster int b, v, k, r, lambda, norotate, i, val, distSpare;
364 1.1 oster char *cfgBuf, *bdfile, *p, *smname;
365 1.1 oster char buf[256], smbuf[256];
366 1.1 oster FILE *fp;
367 1.1 oster
368 1.1 oster distSpare = *((int *)arg);
369 1.1 oster
370 1.1 oster /* get the block design file name */
371 1.1 oster if (rf_get_next_nonblank_line(buf,256,configfp,"Can't find block design file name in config file\n"))
372 1.1 oster return(EINVAL);
373 1.1 oster bdfile = rf_find_non_white(buf);
374 1.1 oster if (bdfile[strlen(bdfile)-1] == '\n') {
375 1.1 oster /* strip newline char */
376 1.1 oster bdfile[strlen(bdfile)-1] = '\0';
377 1.1 oster }
378 1.1 oster
379 1.1 oster /* open bd file, check validity of configuration */
380 1.1 oster if ((fp = fopen(bdfile,"r"))==NULL) {
381 1.1 oster RF_ERRORMSG1("RAID: config error: Can't open layout table file %s\n",bdfile);
382 1.1 oster return(EINVAL);
383 1.1 oster }
384 1.1 oster
385 1.1 oster fgets(buf,256,fp);
386 1.1 oster i = sscanf(buf,"%u %u %u %u %u %u",&b,&v,&k,&r,&lambda,&norotate);
387 1.1 oster if (i == 5)
388 1.1 oster norotate = 0; /* no-rotate flag is optional */
389 1.1 oster else if (i != 6) {
390 1.1 oster RF_ERRORMSG("Unable to parse header line in block design file\n");
391 1.1 oster return(EINVAL);
392 1.1 oster }
393 1.1 oster
394 1.1 oster /* set the sparemap directory. In the in-kernel version, there's a daemon
395 1.1 oster * that's responsible for finding the sparemaps
396 1.1 oster */
397 1.1 oster if (distSpare) {
398 1.1 oster if (rf_get_next_nonblank_line(smbuf,256,configfp,"Can't find sparemap file name in config file\n"))
399 1.1 oster return(EINVAL);
400 1.1 oster smname = rf_find_non_white(smbuf);
401 1.1 oster if (smname[strlen(smname)-1] == '\n') {
402 1.1 oster /* strip newline char */
403 1.1 oster smname[strlen(smname)-1] = '\0';
404 1.1 oster }
405 1.1 oster }
406 1.1 oster else {
407 1.1 oster smbuf[0] = '\0';
408 1.1 oster smname = smbuf;
409 1.1 oster }
410 1.1 oster
411 1.1 oster /* allocate a buffer to hold the configuration info */
412 1.1 oster cfgPtr->layoutSpecificSize = RF_SPAREMAP_NAME_LEN + 6 * sizeof(int) + b * k;
413 1.1 oster /* can't use RF_Malloc here b/c debugMem module not yet init'd */
414 1.1 oster cfgBuf = (char *) malloc(cfgPtr->layoutSpecificSize);
415 1.1 oster cfgPtr->layoutSpecific = (void *) cfgBuf;
416 1.1 oster p = cfgBuf;
417 1.1 oster
418 1.1 oster /* install name of sparemap file */
419 1.1 oster for (i=0; smname[i]; i++)
420 1.1 oster *p++ = smname[i];
421 1.1 oster /* pad with zeros */
422 1.1 oster while (i<RF_SPAREMAP_NAME_LEN) {
423 1.1 oster *p++ = '\0';
424 1.1 oster i++;
425 1.1 oster }
426 1.1 oster
427 1.1 oster /*
428 1.1 oster * fill in the buffer with the block design parameters
429 1.1 oster * and then the block design itself
430 1.1 oster */
431 1.1 oster *( (int *) p) = b; p += sizeof(int);
432 1.1 oster *( (int *) p) = v; p += sizeof(int);
433 1.1 oster *( (int *) p) = k; p += sizeof(int);
434 1.1 oster *( (int *) p) = r; p += sizeof(int);
435 1.1 oster *( (int *) p) = lambda; p += sizeof(int);
436 1.1 oster *( (int *) p) = norotate; p += sizeof(int);
437 1.1 oster
438 1.1 oster while (fscanf(fp,"%d",&val) == 1)
439 1.1 oster *p++ = (char) val;
440 1.1 oster fclose(fp);
441 1.1 oster if (p - cfgBuf != cfgPtr->layoutSpecificSize) {
442 1.1 oster RF_ERRORMSG2("Size mismatch creating layout specific data: is %d sb %d bytes\n",(p-cfgBuf),6*sizeof(int)+b*k);
443 1.1 oster return(EINVAL);
444 1.1 oster }
445 1.1 oster return(0);
446 1.1 oster }
447 1.1 oster
448 1.1 oster
449 1.1 oster /****************************************************************************
450 1.1 oster *
451 1.1 oster * utilities
452 1.1 oster *
453 1.1 oster ***************************************************************************/
454 1.1 oster #ifndef SIMULATE
455 1.1 oster /* convert a device file name to a device number */
456 1.1 oster static unsigned int dev_name2num(s)
457 1.1 oster char *s;
458 1.1 oster {
459 1.1 oster struct stat buf;
460 1.1 oster
461 1.1 oster if (stat(s, &buf) < 0) return(osf_dev_name2num(s));
462 1.1 oster else return(buf.st_rdev);
463 1.1 oster }
464 1.1 oster
465 1.1 oster /* converts an osf/1 style device name to a device number. We use this
466 1.1 oster * only if the stat of the device file fails.
467 1.1 oster */
468 1.1 oster static unsigned int osf_dev_name2num(s)
469 1.1 oster char *s;
470 1.1 oster {
471 1.1 oster int num;
472 1.1 oster char part_ch, lun_ch;
473 1.1 oster unsigned int bus, target, lun, part, dev_major;
474 1.1 oster
475 1.1 oster dev_major = RF_SCSI_DISK_MAJOR;
476 1.1 oster if (sscanf(s,"/dev/rrz%d%c", &num, &part_ch) == 2) {
477 1.1 oster bus = num>>3;
478 1.1 oster target = num & 0x7;
479 1.1 oster part = part_ch - 'a';
480 1.1 oster lun = 0;
481 1.1 oster } else if (sscanf(s,"/dev/rrz%c%d%c", &lun_ch, &num, &part_ch) == 3) {
482 1.1 oster bus = num>>3;
483 1.1 oster target = num & 0x7;
484 1.1 oster part = part_ch - 'a';
485 1.1 oster lun = lun_ch - 'a' + 1;
486 1.1 oster } else {
487 1.1 oster RF_ERRORMSG1("Unable to parse disk dev file name %s\n",s);
488 1.1 oster return(-1);
489 1.1 oster }
490 1.1 oster
491 1.1 oster return( (dev_major<<20) | (bus<<14) | (target<<10) | (lun<<6) | part );
492 1.1 oster }
493 1.1 oster #endif
494 1.1 oster
495 1.1 oster /* searches a file for a line that says "START string", where string is
496 1.1 oster * specified as a parameter
497 1.1 oster */
498 1.1 oster static int rf_search_file_for_start_of(string, buf, len, fp)
499 1.1 oster char *string;
500 1.1 oster char *buf;
501 1.1 oster int len;
502 1.1 oster FILE *fp;
503 1.1 oster {
504 1.1 oster char *p;
505 1.1 oster
506 1.1 oster while (1) {
507 1.1 oster if (fgets(buf, len, fp) == NULL) return(-1);
508 1.1 oster p = rf_find_non_white(buf);
509 1.1 oster if (!strncmp(p, "START", strlen("START"))) {
510 1.1 oster p = rf_find_white(p);
511 1.1 oster p = rf_find_non_white(p);
512 1.1 oster if (!strncmp(p, string, strlen(string))) return(0);
513 1.1 oster }
514 1.1 oster }
515 1.1 oster }
516 1.1 oster
517 1.1 oster /* reads from file fp into buf until it finds an interesting line */
518 1.1 oster int rf_get_next_nonblank_line(buf, len, fp, errmsg)
519 1.1 oster char *buf;
520 1.1 oster int len;
521 1.1 oster FILE *fp;
522 1.1 oster char *errmsg;
523 1.1 oster {
524 1.1 oster char *p;
525 1.1 oster
526 1.1 oster while (fgets(buf,256,fp) != NULL) {
527 1.1 oster p = rf_find_non_white(buf);
528 1.1 oster if (*p == '\n' || *p == '\0' || *p == '#') continue;
529 1.1 oster return(0);
530 1.1 oster }
531 1.1 oster if (errmsg) RF_ERRORMSG(errmsg);
532 1.1 oster return(1);
533 1.1 oster }
534 1.1 oster
535 1.1 oster /* Allocates an array for the spare table, and initializes it from a file.
536 1.1 oster * In the user-level version, this is called when recon is initiated.
537 1.1 oster * When/if I move recon into the kernel, there'll be a daemon that does
538 1.1 oster * an ioctl into raidframe which will block until a spare table is needed.
539 1.1 oster * When it returns, it will read a spare table from the file system,
540 1.1 oster * pass it into the kernel via a different ioctl, and then block again
541 1.1 oster * on the original ioctl.
542 1.1 oster *
543 1.1 oster * This is specific to the declustered layout, but doesn't belong in
544 1.1 oster * rf_decluster.c because it uses stuff that can't be compiled into
545 1.1 oster * the kernel, and it needs to be compiled into the user-level sparemap daemon.
546 1.1 oster *
547 1.1 oster */
548 1.1 oster void *rf_ReadSpareTable(req, fname)
549 1.1 oster RF_SparetWait_t *req;
550 1.1 oster char *fname;
551 1.1 oster {
552 1.1 oster int i, j, numFound, linecount, tableNum, tupleNum, spareDisk, spareBlkOffset;
553 1.1 oster char buf[1024], targString[100], errString[100];
554 1.1 oster RF_SpareTableEntry_t **table;
555 1.1 oster FILE *fp;
556 1.1 oster
557 1.1 oster /* allocate and initialize the table */
558 1.1 oster RF_Malloc(table, req->TablesPerSpareRegion * sizeof(RF_SpareTableEntry_t *), (RF_SpareTableEntry_t **));
559 1.1 oster for (i=0; i<req->TablesPerSpareRegion; i++) {
560 1.1 oster RF_Malloc(table[i], req->BlocksPerTable * sizeof(RF_SpareTableEntry_t), (RF_SpareTableEntry_t *));
561 1.1 oster for (j=0; j<req->BlocksPerTable; j++) table[i][j].spareDisk = table[i][j].spareBlockOffsetInSUs = -1;
562 1.1 oster }
563 1.1 oster
564 1.1 oster /* 2. open sparemap file, sanity check */
565 1.1 oster if ((fp = fopen(fname,"r"))==NULL) {
566 1.1 oster fprintf(stderr,"rf_ReadSpareTable: Can't open sparemap file %s\n",fname); return(NULL);
567 1.1 oster }
568 1.1 oster if (rf_get_next_nonblank_line(buf,1024,fp,"Invalid sparemap file: can't find header line\n"))
569 1.1 oster return(NULL);
570 1.1 oster if (buf[strlen(buf)-1] == '\n')
571 1.1 oster buf[strlen(buf)-1] = '\0';
572 1.1 oster
573 1.1 oster sprintf(targString, "fdisk %d\n", req->fcol);
574 1.1 oster sprintf(errString, "Invalid sparemap file: can't find \"fdisk %d\" line\n",req->fcol);
575 1.1 oster while (1) {
576 1.1 oster rf_get_next_nonblank_line(buf,1024,fp,errString);
577 1.1 oster if (!strncmp(buf,targString,strlen(targString))) break;
578 1.1 oster }
579 1.1 oster
580 1.1 oster /* no more blank lines or comments allowed now */
581 1.1 oster linecount = req->TablesPerSpareRegion * req->TableDepthInPUs;
582 1.1 oster for (i=0; i<linecount; i++) {
583 1.1 oster numFound = fscanf(fp," %d %d %d %d",&tableNum, &tupleNum, &spareDisk, &spareBlkOffset);
584 1.1 oster if (numFound != 4) {
585 1.1 oster fprintf(stderr,"Sparemap file prematurely exhausted after %d of %d lines\n",i,linecount); return(NULL);
586 1.1 oster }
587 1.1 oster RF_ASSERT(tableNum >= 0 && tableNum < req->TablesPerSpareRegion);
588 1.1 oster RF_ASSERT(tupleNum >= 0 && tupleNum < req->BlocksPerTable);
589 1.1 oster RF_ASSERT(spareDisk >= 0 && spareDisk < req->C);
590 1.1 oster RF_ASSERT(spareBlkOffset >= 0 && spareBlkOffset < req->SpareSpaceDepthPerRegionInSUs / req->SUsPerPU);
591
592 table[tableNum][tupleNum].spareDisk = spareDisk;
593 table[tableNum][tupleNum].spareBlockOffsetInSUs = spareBlkOffset * req->SUsPerPU;
594 }
595
596 fclose(fp);
597 return((void *) table);
598 }
599