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