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