rf_disks.c revision 1.13 1 1.13 oster /* $NetBSD: rf_disks.c,v 1.13 1999/08/14 03:10:03 oster Exp $ */
2 1.6 oster /*-
3 1.6 oster * Copyright (c) 1999 The NetBSD Foundation, Inc.
4 1.6 oster * All rights reserved.
5 1.6 oster *
6 1.6 oster * This code is derived from software contributed to The NetBSD Foundation
7 1.6 oster * by Greg Oster
8 1.6 oster *
9 1.6 oster * Redistribution and use in source and binary forms, with or without
10 1.6 oster * modification, are permitted provided that the following conditions
11 1.6 oster * are met:
12 1.6 oster * 1. Redistributions of source code must retain the above copyright
13 1.6 oster * notice, this list of conditions and the following disclaimer.
14 1.6 oster * 2. Redistributions in binary form must reproduce the above copyright
15 1.6 oster * notice, this list of conditions and the following disclaimer in the
16 1.6 oster * documentation and/or other materials provided with the distribution.
17 1.6 oster * 3. All advertising materials mentioning features or use of this software
18 1.6 oster * must display the following acknowledgement:
19 1.6 oster * This product includes software developed by the NetBSD
20 1.6 oster * Foundation, Inc. and its contributors.
21 1.6 oster * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.6 oster * contributors may be used to endorse or promote products derived
23 1.6 oster * from this software without specific prior written permission.
24 1.6 oster *
25 1.6 oster * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.6 oster * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.6 oster * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.6 oster * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.6 oster * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.6 oster * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.6 oster * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.6 oster * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.6 oster * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.6 oster * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.6 oster * POSSIBILITY OF SUCH DAMAGE.
36 1.6 oster */
37 1.6 oster
38 1.1 oster /*
39 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
40 1.1 oster * All rights reserved.
41 1.1 oster *
42 1.1 oster * Author: Mark Holland
43 1.1 oster *
44 1.1 oster * Permission to use, copy, modify and distribute this software and
45 1.1 oster * its documentation is hereby granted, provided that both the copyright
46 1.1 oster * notice and this permission notice appear in all copies of the
47 1.1 oster * software, derivative works or modified versions, and any portions
48 1.1 oster * thereof, and that both notices appear in supporting documentation.
49 1.1 oster *
50 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53 1.1 oster *
54 1.1 oster * Carnegie Mellon requests users of this software to return to
55 1.1 oster *
56 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
57 1.1 oster * School of Computer Science
58 1.1 oster * Carnegie Mellon University
59 1.1 oster * Pittsburgh PA 15213-3890
60 1.1 oster *
61 1.1 oster * any improvements or extensions that they make and grant Carnegie the
62 1.1 oster * rights to redistribute these changes.
63 1.1 oster */
64 1.1 oster
65 1.1 oster /***************************************************************
66 1.1 oster * rf_disks.c -- code to perform operations on the actual disks
67 1.1 oster ***************************************************************/
68 1.1 oster
69 1.1 oster #include "rf_types.h"
70 1.1 oster #include "rf_raid.h"
71 1.1 oster #include "rf_alloclist.h"
72 1.1 oster #include "rf_utils.h"
73 1.1 oster #include "rf_configure.h"
74 1.1 oster #include "rf_general.h"
75 1.1 oster #include "rf_options.h"
76 1.1 oster
77 1.1 oster #include <sys/types.h>
78 1.1 oster #include <sys/param.h>
79 1.1 oster #include <sys/systm.h>
80 1.1 oster #include <sys/proc.h>
81 1.1 oster #include <sys/ioctl.h>
82 1.1 oster #include <sys/fcntl.h>
83 1.1 oster #include <sys/vnode.h>
84 1.1 oster
85 1.6 oster /* XXX these should be in a header file somewhere */
86 1.5 oster int raidlookup __P((char *, struct proc * p, struct vnode **));
87 1.6 oster int raidwrite_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
88 1.6 oster int raidread_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
89 1.6 oster void rf_UnconfigureVnodes( RF_Raid_t * );
90 1.6 oster int rf_CheckLabels( RF_Raid_t *, RF_Config_t *);
91 1.1 oster
92 1.1 oster #define DPRINTF6(a,b,c,d,e,f) if (rf_diskDebug) printf(a,b,c,d,e,f)
93 1.1 oster #define DPRINTF7(a,b,c,d,e,f,g) if (rf_diskDebug) printf(a,b,c,d,e,f,g)
94 1.1 oster
95 1.6 oster /**************************************************************************
96 1.1 oster *
97 1.1 oster * initialize the disks comprising the array
98 1.1 oster *
99 1.6 oster * We want the spare disks to have regular row,col numbers so that we can
100 1.6 oster * easily substitue a spare for a failed disk. But, the driver code assumes
101 1.6 oster * throughout that the array contains numRow by numCol _non-spare_ disks, so
102 1.6 oster * it's not clear how to fit in the spares. This is an unfortunate holdover
103 1.6 oster * from raidSim. The quick and dirty fix is to make row zero bigger than the
104 1.6 oster * rest, and put all the spares in it. This probably needs to get changed
105 1.6 oster * eventually.
106 1.1 oster *
107 1.6 oster **************************************************************************/
108 1.6 oster
109 1.5 oster int
110 1.6 oster rf_ConfigureDisks( listp, raidPtr, cfgPtr )
111 1.6 oster RF_ShutdownList_t **listp;
112 1.6 oster RF_Raid_t *raidPtr;
113 1.6 oster RF_Config_t *cfgPtr;
114 1.1 oster {
115 1.5 oster RF_RaidDisk_t **disks;
116 1.5 oster RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
117 1.5 oster RF_RowCol_t r, c;
118 1.6 oster int bs, ret;
119 1.5 oster unsigned i, count, foundone = 0, numFailuresThisRow;
120 1.6 oster int num_rows_done, num_cols_done;
121 1.7 oster int force;
122 1.5 oster
123 1.5 oster num_rows_done = 0;
124 1.5 oster num_cols_done = 0;
125 1.7 oster force = cfgPtr->force;
126 1.5 oster
127 1.6 oster RF_CallocAndAdd(disks, raidPtr->numRow, sizeof(RF_RaidDisk_t *),
128 1.6 oster (RF_RaidDisk_t **), raidPtr->cleanupList);
129 1.5 oster if (disks == NULL) {
130 1.5 oster ret = ENOMEM;
131 1.5 oster goto fail;
132 1.5 oster }
133 1.5 oster raidPtr->Disks = disks;
134 1.5 oster
135 1.5 oster /* get space for the device-specific stuff... */
136 1.5 oster RF_CallocAndAdd(raidPtr->raid_cinfo, raidPtr->numRow,
137 1.5 oster sizeof(struct raidcinfo *), (struct raidcinfo **),
138 1.5 oster raidPtr->cleanupList);
139 1.5 oster if (raidPtr->raid_cinfo == NULL) {
140 1.5 oster ret = ENOMEM;
141 1.5 oster goto fail;
142 1.5 oster }
143 1.5 oster for (r = 0; r < raidPtr->numRow; r++) {
144 1.5 oster numFailuresThisRow = 0;
145 1.6 oster /* We allocate RF_MAXSPARE on the first row so that we
146 1.6 oster have room to do hot-swapping of spares */
147 1.6 oster RF_CallocAndAdd(disks[r], raidPtr->numCol
148 1.6 oster + ((r == 0) ? RF_MAXSPARE : 0),
149 1.6 oster sizeof(RF_RaidDisk_t), (RF_RaidDisk_t *),
150 1.6 oster raidPtr->cleanupList);
151 1.5 oster if (disks[r] == NULL) {
152 1.5 oster ret = ENOMEM;
153 1.5 oster goto fail;
154 1.5 oster }
155 1.5 oster /* get more space for device specific stuff.. */
156 1.5 oster RF_CallocAndAdd(raidPtr->raid_cinfo[r],
157 1.5 oster raidPtr->numCol + ((r == 0) ? raidPtr->numSpare : 0),
158 1.5 oster sizeof(struct raidcinfo), (struct raidcinfo *),
159 1.1 oster raidPtr->cleanupList);
160 1.5 oster if (raidPtr->raid_cinfo[r] == NULL) {
161 1.5 oster ret = ENOMEM;
162 1.5 oster goto fail;
163 1.5 oster }
164 1.5 oster for (c = 0; c < raidPtr->numCol; c++) {
165 1.6 oster ret = rf_ConfigureDisk(raidPtr,
166 1.6 oster &cfgPtr->devnames[r][c][0],
167 1.6 oster &disks[r][c], r, c);
168 1.5 oster if (ret)
169 1.5 oster goto fail;
170 1.7 oster
171 1.6 oster if (disks[r][c].status == rf_ds_optimal) {
172 1.6 oster raidread_component_label(
173 1.6 oster raidPtr->raid_cinfo[r][c].ci_dev,
174 1.6 oster raidPtr->raid_cinfo[r][c].ci_vp,
175 1.6 oster &raidPtr->raid_cinfo[r][c].ci_label);
176 1.6 oster }
177 1.7 oster
178 1.5 oster if (disks[r][c].status != rf_ds_optimal) {
179 1.5 oster numFailuresThisRow++;
180 1.5 oster } else {
181 1.5 oster if (disks[r][c].numBlocks < min_numblks)
182 1.5 oster min_numblks = disks[r][c].numBlocks;
183 1.5 oster DPRINTF7("Disk at row %d col %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n",
184 1.5 oster r, c, disks[r][c].devname,
185 1.5 oster (long int) disks[r][c].numBlocks,
186 1.5 oster disks[r][c].blockSize,
187 1.6 oster (long int) disks[r][c].numBlocks *
188 1.6 oster disks[r][c].blockSize / 1024 / 1024);
189 1.5 oster }
190 1.5 oster num_cols_done++;
191 1.5 oster }
192 1.5 oster /* XXX fix for n-fault tolerant */
193 1.6 oster /* XXX this should probably check to see how many failures
194 1.6 oster we can handle for this configuration! */
195 1.5 oster if (numFailuresThisRow > 0)
196 1.5 oster raidPtr->status[r] = rf_rs_degraded;
197 1.5 oster num_rows_done++;
198 1.5 oster }
199 1.6 oster
200 1.5 oster /* all disks must be the same size & have the same block size, bs must
201 1.5 oster * be a power of 2 */
202 1.5 oster bs = 0;
203 1.5 oster for (foundone = r = 0; !foundone && r < raidPtr->numRow; r++) {
204 1.5 oster for (c = 0; !foundone && c < raidPtr->numCol; c++) {
205 1.5 oster if (disks[r][c].status == rf_ds_optimal) {
206 1.5 oster bs = disks[r][c].blockSize;
207 1.5 oster foundone = 1;
208 1.5 oster }
209 1.5 oster }
210 1.5 oster }
211 1.5 oster if (!foundone) {
212 1.5 oster RF_ERRORMSG("RAIDFRAME: Did not find any live disks in the array.\n");
213 1.5 oster ret = EINVAL;
214 1.5 oster goto fail;
215 1.5 oster }
216 1.5 oster for (count = 0, i = 1; i; i <<= 1)
217 1.5 oster if (bs & i)
218 1.5 oster count++;
219 1.5 oster if (count != 1) {
220 1.5 oster RF_ERRORMSG1("Error: block size on disks (%d) must be a power of 2\n", bs);
221 1.5 oster ret = EINVAL;
222 1.5 oster goto fail;
223 1.5 oster }
224 1.6 oster
225 1.6 oster if (rf_CheckLabels( raidPtr, cfgPtr )) {
226 1.7 oster printf("raid%d: There were fatal errors\n", raidPtr->raidid);
227 1.7 oster if (force != 0) {
228 1.7 oster printf("raid%d: Fatal errors being ignored.\n",
229 1.7 oster raidPtr->raidid);
230 1.7 oster } else {
231 1.7 oster ret = EINVAL;
232 1.7 oster goto fail;
233 1.7 oster }
234 1.6 oster }
235 1.7 oster
236 1.5 oster for (r = 0; r < raidPtr->numRow; r++) {
237 1.5 oster for (c = 0; c < raidPtr->numCol; c++) {
238 1.5 oster if (disks[r][c].status == rf_ds_optimal) {
239 1.5 oster if (disks[r][c].blockSize != bs) {
240 1.5 oster RF_ERRORMSG2("Error: block size of disk at r %d c %d different from disk at r 0 c 0\n", r, c);
241 1.5 oster ret = EINVAL;
242 1.5 oster goto fail;
243 1.5 oster }
244 1.5 oster if (disks[r][c].numBlocks != min_numblks) {
245 1.5 oster RF_ERRORMSG3("WARNING: truncating disk at r %d c %d to %d blocks\n",
246 1.5 oster r, c, (int) min_numblks);
247 1.5 oster disks[r][c].numBlocks = min_numblks;
248 1.5 oster }
249 1.5 oster }
250 1.5 oster }
251 1.5 oster }
252 1.5 oster
253 1.5 oster raidPtr->sectorsPerDisk = min_numblks;
254 1.5 oster raidPtr->logBytesPerSector = ffs(bs) - 1;
255 1.5 oster raidPtr->bytesPerSector = bs;
256 1.5 oster raidPtr->sectorMask = bs - 1;
257 1.5 oster return (0);
258 1.1 oster
259 1.1 oster fail:
260 1.6 oster
261 1.6 oster rf_UnconfigureVnodes( raidPtr );
262 1.1 oster
263 1.5 oster return (ret);
264 1.1 oster }
265 1.1 oster
266 1.1 oster
267 1.6 oster /****************************************************************************
268 1.1 oster * set up the data structures describing the spare disks in the array
269 1.1 oster * recall from the above comment that the spare disk descriptors are stored
270 1.1 oster * in row zero, which is specially expanded to hold them.
271 1.6 oster ****************************************************************************/
272 1.5 oster int
273 1.6 oster rf_ConfigureSpareDisks( listp, raidPtr, cfgPtr )
274 1.6 oster RF_ShutdownList_t ** listp;
275 1.6 oster RF_Raid_t * raidPtr;
276 1.6 oster RF_Config_t * cfgPtr;
277 1.1 oster {
278 1.6 oster int i, ret;
279 1.6 oster unsigned int bs;
280 1.5 oster RF_RaidDisk_t *disks;
281 1.5 oster int num_spares_done;
282 1.1 oster
283 1.5 oster num_spares_done = 0;
284 1.5 oster
285 1.5 oster /* The space for the spares should have already been allocated by
286 1.5 oster * ConfigureDisks() */
287 1.5 oster
288 1.5 oster disks = &raidPtr->Disks[0][raidPtr->numCol];
289 1.5 oster for (i = 0; i < raidPtr->numSpare; i++) {
290 1.5 oster ret = rf_ConfigureDisk(raidPtr, &cfgPtr->spare_names[i][0],
291 1.6 oster &disks[i], 0, raidPtr->numCol + i);
292 1.5 oster if (ret)
293 1.5 oster goto fail;
294 1.5 oster if (disks[i].status != rf_ds_optimal) {
295 1.6 oster RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
296 1.6 oster &cfgPtr->spare_names[i][0]);
297 1.5 oster } else {
298 1.5 oster disks[i].status = rf_ds_spare; /* change status to
299 1.5 oster * spare */
300 1.5 oster DPRINTF6("Spare Disk %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n", i,
301 1.5 oster disks[i].devname,
302 1.5 oster (long int) disks[i].numBlocks, disks[i].blockSize,
303 1.6 oster (long int) disks[i].numBlocks *
304 1.6 oster disks[i].blockSize / 1024 / 1024);
305 1.5 oster }
306 1.5 oster num_spares_done++;
307 1.5 oster }
308 1.1 oster
309 1.5 oster /* check sizes and block sizes on spare disks */
310 1.5 oster bs = 1 << raidPtr->logBytesPerSector;
311 1.5 oster for (i = 0; i < raidPtr->numSpare; i++) {
312 1.5 oster if (disks[i].blockSize != bs) {
313 1.5 oster RF_ERRORMSG3("Block size of %d on spare disk %s is not the same as on other disks (%d)\n", disks[i].blockSize, disks[i].devname, bs);
314 1.5 oster ret = EINVAL;
315 1.5 oster goto fail;
316 1.5 oster }
317 1.5 oster if (disks[i].numBlocks < raidPtr->sectorsPerDisk) {
318 1.5 oster RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
319 1.6 oster disks[i].devname, disks[i].blockSize,
320 1.6 oster (long int) raidPtr->sectorsPerDisk);
321 1.5 oster ret = EINVAL;
322 1.5 oster goto fail;
323 1.5 oster } else
324 1.5 oster if (disks[i].numBlocks > raidPtr->sectorsPerDisk) {
325 1.5 oster RF_ERRORMSG2("Warning: truncating spare disk %s to %ld blocks\n", disks[i].devname, (long int) raidPtr->sectorsPerDisk);
326 1.5 oster
327 1.5 oster disks[i].numBlocks = raidPtr->sectorsPerDisk;
328 1.5 oster }
329 1.5 oster }
330 1.5 oster
331 1.5 oster return (0);
332 1.1 oster
333 1.1 oster fail:
334 1.2 oster
335 1.5 oster /* Release the hold on the main components. We've failed to allocate
336 1.6 oster * a spare, and since we're failing, we need to free things..
337 1.6 oster
338 1.6 oster XXX failing to allocate a spare is *not* that big of a deal...
339 1.6 oster We *can* survive without it, if need be, esp. if we get hot
340 1.6 oster adding working.
341 1.1 oster
342 1.6 oster If we don't fail out here, then we need a way to remove this spare...
343 1.6 oster that should be easier to do here than if we are "live"...
344 1.1 oster
345 1.6 oster */
346 1.1 oster
347 1.6 oster rf_UnconfigureVnodes( raidPtr );
348 1.1 oster
349 1.5 oster return (ret);
350 1.1 oster }
351 1.1 oster
352 1.1 oster
353 1.1 oster
354 1.1 oster /* configure a single disk in the array */
355 1.5 oster int
356 1.6 oster rf_ConfigureDisk(raidPtr, buf, diskPtr, row, col)
357 1.6 oster RF_Raid_t *raidPtr;
358 1.5 oster char *buf;
359 1.5 oster RF_RaidDisk_t *diskPtr;
360 1.5 oster RF_RowCol_t row;
361 1.5 oster RF_RowCol_t col;
362 1.1 oster {
363 1.5 oster char *p;
364 1.5 oster int retcode;
365 1.1 oster
366 1.1 oster struct partinfo dpart;
367 1.1 oster struct vnode *vp;
368 1.1 oster struct vattr va;
369 1.1 oster struct proc *proc;
370 1.5 oster int error;
371 1.1 oster
372 1.5 oster retcode = 0;
373 1.5 oster p = rf_find_non_white(buf);
374 1.5 oster if (p[strlen(p) - 1] == '\n') {
375 1.5 oster /* strip off the newline */
376 1.5 oster p[strlen(p) - 1] = '\0';
377 1.5 oster }
378 1.5 oster (void) strcpy(diskPtr->devname, p);
379 1.1 oster
380 1.13 oster proc = raidPtr->engine_thread;
381 1.5 oster
382 1.5 oster /* Let's start by claiming the component is fine and well... */
383 1.5 oster diskPtr->status = rf_ds_optimal;
384 1.5 oster
385 1.5 oster raidPtr->raid_cinfo[row][col].ci_vp = NULL;
386 1.5 oster raidPtr->raid_cinfo[row][col].ci_dev = NULL;
387 1.5 oster
388 1.5 oster error = raidlookup(diskPtr->devname, proc, &vp);
389 1.5 oster if (error) {
390 1.5 oster printf("raidlookup on device: %s failed!\n", diskPtr->devname);
391 1.5 oster if (error == ENXIO) {
392 1.6 oster /* the component isn't there... must be dead :-( */
393 1.5 oster diskPtr->status = rf_ds_failed;
394 1.5 oster } else {
395 1.5 oster return (error);
396 1.5 oster }
397 1.5 oster }
398 1.5 oster if (diskPtr->status == rf_ds_optimal) {
399 1.5 oster
400 1.5 oster if ((error = VOP_GETATTR(vp, &va, proc->p_ucred, proc)) != 0) {
401 1.5 oster return (error);
402 1.5 oster }
403 1.5 oster error = VOP_IOCTL(vp, DIOCGPART, (caddr_t) & dpart,
404 1.6 oster FREAD, proc->p_ucred, proc);
405 1.5 oster if (error) {
406 1.5 oster return (error);
407 1.5 oster }
408 1.6 oster
409 1.5 oster diskPtr->blockSize = dpart.disklab->d_secsize;
410 1.6 oster
411 1.5 oster diskPtr->numBlocks = dpart.part->p_size - rf_protectedSectors;
412 1.6 oster
413 1.5 oster raidPtr->raid_cinfo[row][col].ci_vp = vp;
414 1.5 oster raidPtr->raid_cinfo[row][col].ci_dev = va.va_rdev;
415 1.6 oster
416 1.6 oster diskPtr->dev = va.va_rdev;
417 1.6 oster
418 1.6 oster /* we allow the user to specify that only a fraction of the
419 1.6 oster * disks should be used this is just for debug: it speeds up
420 1.6 oster * the parity scan */
421 1.6 oster diskPtr->numBlocks = diskPtr->numBlocks *
422 1.6 oster rf_sizePercentage / 100;
423 1.6 oster }
424 1.6 oster return (0);
425 1.6 oster }
426 1.6 oster
427 1.7 oster static void rf_print_label_status( RF_Raid_t *, int, int, char *,
428 1.7 oster RF_ComponentLabel_t *);
429 1.7 oster
430 1.7 oster static void
431 1.7 oster rf_print_label_status( raidPtr, row, column, dev_name, ci_label )
432 1.7 oster RF_Raid_t *raidPtr;
433 1.7 oster int row;
434 1.7 oster int column;
435 1.7 oster char *dev_name;
436 1.7 oster RF_ComponentLabel_t *ci_label;
437 1.7 oster {
438 1.7 oster
439 1.7 oster printf("raid%d: Component %s being configured at row: %d col: %d\n",
440 1.7 oster raidPtr->raidid, dev_name, row, column );
441 1.7 oster printf(" Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
442 1.7 oster ci_label->row, ci_label->column,
443 1.7 oster ci_label->num_rows, ci_label->num_columns);
444 1.7 oster printf(" Version: %d Serial Number: %d Mod Counter: %d\n",
445 1.7 oster ci_label->version, ci_label->serial_number,
446 1.7 oster ci_label->mod_counter);
447 1.11 oster printf(" Clean: %s Status: %d\n",
448 1.11 oster ci_label->clean ? "Yes" : "No", ci_label->status );
449 1.7 oster }
450 1.7 oster
451 1.7 oster static int rf_check_label_vitals( RF_Raid_t *, int, int, char *,
452 1.7 oster RF_ComponentLabel_t *, int, int );
453 1.7 oster static int rf_check_label_vitals( raidPtr, row, column, dev_name, ci_label,
454 1.7 oster serial_number, mod_counter )
455 1.7 oster RF_Raid_t *raidPtr;
456 1.7 oster int row;
457 1.7 oster int column;
458 1.7 oster char *dev_name;
459 1.7 oster RF_ComponentLabel_t *ci_label;
460 1.7 oster int serial_number;
461 1.7 oster int mod_counter;
462 1.7 oster {
463 1.7 oster int fatal_error = 0;
464 1.7 oster
465 1.7 oster if (serial_number != ci_label->serial_number) {
466 1.7 oster printf("%s has a different serial number: %d %d\n",
467 1.7 oster dev_name, serial_number, ci_label->serial_number);
468 1.7 oster fatal_error = 1;
469 1.7 oster }
470 1.7 oster if (mod_counter != ci_label->mod_counter) {
471 1.7 oster printf("%s has a different modfication count: %d %d\n",
472 1.7 oster dev_name, mod_counter, ci_label->mod_counter);
473 1.7 oster }
474 1.7 oster
475 1.7 oster if (row != ci_label->row) {
476 1.7 oster printf("Row out of alignment for: %s\n", dev_name);
477 1.7 oster fatal_error = 1;
478 1.7 oster }
479 1.7 oster if (column != ci_label->column) {
480 1.7 oster printf("Column out of alignment for: %s\n", dev_name);
481 1.7 oster fatal_error = 1;
482 1.7 oster }
483 1.7 oster if (raidPtr->numRow != ci_label->num_rows) {
484 1.7 oster printf("Number of rows do not match for: %s\n", dev_name);
485 1.7 oster fatal_error = 1;
486 1.7 oster }
487 1.7 oster if (raidPtr->numCol != ci_label->num_columns) {
488 1.7 oster printf("Number of columns do not match for: %s\n", dev_name);
489 1.7 oster fatal_error = 1;
490 1.7 oster }
491 1.7 oster if (ci_label->clean == 0) {
492 1.7 oster /* it's not clean, but that's not fatal */
493 1.7 oster printf("%s is not clean!\n", dev_name);
494 1.7 oster }
495 1.7 oster return(fatal_error);
496 1.7 oster }
497 1.7 oster
498 1.7 oster
499 1.6 oster /*
500 1.6 oster
501 1.6 oster rf_CheckLabels() - check all the component labels for consistency.
502 1.6 oster Return an error if there is anything major amiss.
503 1.6 oster
504 1.6 oster */
505 1.1 oster
506 1.6 oster int
507 1.6 oster rf_CheckLabels( raidPtr, cfgPtr )
508 1.6 oster RF_Raid_t *raidPtr;
509 1.6 oster RF_Config_t *cfgPtr;
510 1.6 oster {
511 1.6 oster int r,c;
512 1.6 oster char *dev_name;
513 1.6 oster RF_ComponentLabel_t *ci_label;
514 1.6 oster int serial_number = 0;
515 1.7 oster int mod_number = 0;
516 1.6 oster int fatal_error = 0;
517 1.7 oster int mod_values[4];
518 1.7 oster int mod_count[4];
519 1.7 oster int ser_values[4];
520 1.7 oster int ser_count[4];
521 1.7 oster int num_ser;
522 1.7 oster int num_mod;
523 1.7 oster int i;
524 1.7 oster int found;
525 1.7 oster int hosed_row;
526 1.7 oster int hosed_column;
527 1.7 oster int too_fatal;
528 1.7 oster int parity_good;
529 1.7 oster int force;
530 1.7 oster
531 1.7 oster hosed_row = -1;
532 1.7 oster hosed_column = -1;
533 1.7 oster too_fatal = 0;
534 1.7 oster force = cfgPtr->force;
535 1.7 oster
536 1.7 oster /*
537 1.7 oster We're going to try to be a little intelligent here. If one
538 1.7 oster component's label is bogus, and we can identify that it's the
539 1.7 oster *only* one that's gone, we'll mark it as "failed" and allow
540 1.7 oster the configuration to proceed. This will be the *only* case
541 1.7 oster that we'll proceed if there would be (otherwise) fatal errors.
542 1.7 oster
543 1.7 oster Basically we simply keep a count of how many components had
544 1.7 oster what serial number. If all but one agree, we simply mark
545 1.7 oster the disagreeing component as being failed, and allow
546 1.7 oster things to come up "normally".
547 1.7 oster
548 1.7 oster We do this first for serial numbers, and then for "mod_counter".
549 1.6 oster
550 1.7 oster */
551 1.7 oster
552 1.7 oster num_ser = 0;
553 1.7 oster num_mod = 0;
554 1.7 oster for (r = 0; r < raidPtr->numRow && !fatal_error ; r++) {
555 1.7 oster for (c = 0; c < raidPtr->numCol; c++) {
556 1.7 oster ci_label = &raidPtr->raid_cinfo[r][c].ci_label;
557 1.7 oster found=0;
558 1.7 oster for(i=0;i<num_ser;i++) {
559 1.7 oster if (ser_values[i] == ci_label->serial_number) {
560 1.7 oster ser_count[i]++;
561 1.7 oster found=1;
562 1.7 oster break;
563 1.7 oster }
564 1.7 oster }
565 1.7 oster if (!found) {
566 1.7 oster ser_values[num_ser] = ci_label->serial_number;
567 1.7 oster ser_count[num_ser] = 1;
568 1.7 oster num_ser++;
569 1.7 oster if (num_ser>2) {
570 1.7 oster fatal_error = 1;
571 1.7 oster break;
572 1.7 oster }
573 1.7 oster }
574 1.7 oster found=0;
575 1.7 oster for(i=0;i<num_mod;i++) {
576 1.7 oster if (mod_values[i] == ci_label->mod_counter) {
577 1.7 oster mod_count[i]++;
578 1.7 oster found=1;
579 1.7 oster break;
580 1.7 oster }
581 1.7 oster }
582 1.7 oster if (!found) {
583 1.7 oster mod_values[num_mod] = ci_label->mod_counter;
584 1.7 oster mod_count[num_mod] = 1;
585 1.7 oster num_mod++;
586 1.7 oster if (num_mod>2) {
587 1.7 oster fatal_error = 1;
588 1.7 oster break;
589 1.7 oster }
590 1.7 oster }
591 1.7 oster }
592 1.7 oster }
593 1.7 oster #if DEBUG
594 1.7 oster printf("raid%d: Summary of serial numbers:\n", raidPtr->raidid);
595 1.7 oster for(i=0;i<num_ser;i++) {
596 1.7 oster printf("%d %d\n", ser_values[i], ser_count[i]);
597 1.7 oster }
598 1.7 oster printf("raid%d: Summary of mod counters:\n", raidPtr->raidid);
599 1.7 oster for(i=0;i<num_mod;i++) {
600 1.7 oster printf("%d %d\n", mod_values[i], mod_count[i]);
601 1.7 oster }
602 1.7 oster #endif
603 1.7 oster serial_number = ser_values[0];
604 1.7 oster if (num_ser == 2) {
605 1.7 oster if ((ser_count[0] == 1) || (ser_count[1] == 1)) {
606 1.7 oster /* Locate the maverick component */
607 1.7 oster if (ser_count[1] > ser_count[0]) {
608 1.7 oster serial_number = ser_values[1];
609 1.7 oster }
610 1.7 oster for (r = 0; r < raidPtr->numRow; r++) {
611 1.7 oster for (c = 0; c < raidPtr->numCol; c++) {
612 1.7 oster ci_label = &raidPtr->raid_cinfo[r][c].ci_label;
613 1.7 oster if (serial_number !=
614 1.7 oster ci_label->serial_number) {
615 1.7 oster hosed_row = r;
616 1.7 oster hosed_column = c;
617 1.7 oster break;
618 1.7 oster }
619 1.7 oster }
620 1.7 oster }
621 1.7 oster printf("Hosed component: %s\n",
622 1.7 oster &cfgPtr->devnames[hosed_row][hosed_column][0]);
623 1.7 oster if (!force) {
624 1.7 oster /* we'll fail this component, as if there are
625 1.7 oster other major errors, we arn't forcing things
626 1.7 oster and we'll abort the config anyways */
627 1.7 oster raidPtr->Disks[hosed_row][hosed_column].status
628 1.7 oster = rf_ds_failed;
629 1.7 oster raidPtr->numFailures++;
630 1.7 oster raidPtr->status[hosed_row] = rf_rs_degraded;
631 1.7 oster }
632 1.7 oster } else {
633 1.7 oster too_fatal = 1;
634 1.7 oster }
635 1.7 oster if (cfgPtr->parityConfig == '0') {
636 1.7 oster /* We've identified two different serial numbers.
637 1.7 oster RAID 0 can't cope with that, so we'll punt */
638 1.7 oster too_fatal = 1;
639 1.7 oster }
640 1.7 oster
641 1.7 oster }
642 1.7 oster
643 1.7 oster /* record the serial number for later. If we bail later, setting
644 1.7 oster this doesn't matter, otherwise we've got the best guess at the
645 1.7 oster correct serial number */
646 1.7 oster raidPtr->serial_number = serial_number;
647 1.7 oster
648 1.7 oster mod_number = mod_values[0];
649 1.7 oster if (num_mod == 2) {
650 1.7 oster if ((mod_count[0] == 1) || (mod_count[1] == 1)) {
651 1.7 oster /* Locate the maverick component */
652 1.7 oster if (mod_count[1] > mod_count[0]) {
653 1.7 oster mod_number = mod_values[1];
654 1.8 oster } else if (mod_count[1] < mod_count[0]) {
655 1.8 oster mod_number = mod_values[0];
656 1.8 oster } else {
657 1.8 oster /* counts of different modification values
658 1.8 oster are the same. Assume greater value is
659 1.8 oster the correct one, all other things
660 1.8 oster considered */
661 1.8 oster if (mod_values[0] > mod_values[1]) {
662 1.8 oster mod_number = mod_values[0];
663 1.8 oster } else {
664 1.8 oster mod_number = mod_values[1];
665 1.8 oster }
666 1.8 oster
667 1.8 oster }
668 1.7 oster for (r = 0; r < raidPtr->numRow && !too_fatal ; r++) {
669 1.7 oster for (c = 0; c < raidPtr->numCol; c++) {
670 1.7 oster ci_label = &raidPtr->raid_cinfo[r][c].ci_label;
671 1.7 oster if (mod_number !=
672 1.7 oster ci_label->mod_counter) {
673 1.7 oster if ( ( hosed_row == r ) &&
674 1.7 oster ( hosed_column == c )) {
675 1.7 oster /* same one. Can
676 1.7 oster deal with it. */
677 1.7 oster } else {
678 1.7 oster hosed_row = r;
679 1.7 oster hosed_column = c;
680 1.7 oster if (num_ser != 1) {
681 1.7 oster too_fatal = 1;
682 1.7 oster break;
683 1.7 oster }
684 1.7 oster }
685 1.7 oster }
686 1.7 oster }
687 1.7 oster }
688 1.7 oster printf("Hosed component: %s\n",
689 1.7 oster &cfgPtr->devnames[hosed_row][hosed_column][0]);
690 1.7 oster if (!force) {
691 1.7 oster /* we'll fail this component, as if there are
692 1.7 oster other major errors, we arn't forcing things
693 1.7 oster and we'll abort the config anyways */
694 1.9 oster if (raidPtr->Disks[hosed_row][hosed_column].status != rf_ds_failed) {
695 1.9 oster raidPtr->Disks[hosed_row][hosed_column].status
696 1.9 oster = rf_ds_failed;
697 1.9 oster raidPtr->numFailures++;
698 1.9 oster raidPtr->status[hosed_row] = rf_rs_degraded;
699 1.9 oster }
700 1.7 oster }
701 1.7 oster } else {
702 1.7 oster too_fatal = 1;
703 1.7 oster }
704 1.7 oster if (cfgPtr->parityConfig == '0') {
705 1.7 oster /* We've identified two different mod counters.
706 1.7 oster RAID 0 can't cope with that, so we'll punt */
707 1.7 oster too_fatal = 1;
708 1.7 oster }
709 1.7 oster }
710 1.7 oster
711 1.7 oster raidPtr->mod_counter = mod_number;
712 1.7 oster
713 1.7 oster if (too_fatal) {
714 1.7 oster /* we've had both a serial number mismatch, and a mod_counter
715 1.7 oster mismatch -- and they involved two different components!!
716 1.7 oster Bail -- make things fail so that the user must force
717 1.7 oster the issue... */
718 1.7 oster hosed_row = -1;
719 1.7 oster hosed_column = -1;
720 1.7 oster }
721 1.7 oster
722 1.7 oster if (num_ser > 2) {
723 1.7 oster printf("raid%d: Too many different serial numbers!\n",
724 1.7 oster raidPtr->raidid);
725 1.7 oster }
726 1.7 oster
727 1.7 oster if (num_mod > 2) {
728 1.7 oster printf("raid%d: Too many different mod counters!\n",
729 1.7 oster raidPtr->raidid);
730 1.7 oster }
731 1.7 oster
732 1.7 oster /* we start by assuming the parity will be good, and flee from
733 1.7 oster that notion at the slightest sign of trouble */
734 1.7 oster
735 1.7 oster parity_good = RF_RAID_CLEAN;
736 1.6 oster for (r = 0; r < raidPtr->numRow; r++) {
737 1.6 oster for (c = 0; c < raidPtr->numCol; c++) {
738 1.6 oster dev_name = &cfgPtr->devnames[r][c][0];
739 1.6 oster ci_label = &raidPtr->raid_cinfo[r][c].ci_label;
740 1.7 oster
741 1.7 oster if ((r == hosed_row) && (c == hosed_column)) {
742 1.7 oster printf("raid%d: Ignoring %s\n",
743 1.7 oster raidPtr->raidid, dev_name);
744 1.7 oster } else {
745 1.7 oster rf_print_label_status( raidPtr, r, c,
746 1.7 oster dev_name, ci_label );
747 1.7 oster if (rf_check_label_vitals( raidPtr, r, c,
748 1.7 oster dev_name, ci_label,
749 1.7 oster serial_number,
750 1.7 oster mod_number )) {
751 1.6 oster fatal_error = 1;
752 1.6 oster }
753 1.7 oster if (ci_label->clean != RF_RAID_CLEAN) {
754 1.7 oster parity_good = RF_RAID_DIRTY;
755 1.6 oster }
756 1.6 oster }
757 1.6 oster }
758 1.6 oster }
759 1.7 oster if (fatal_error) {
760 1.7 oster parity_good = RF_RAID_DIRTY;
761 1.7 oster }
762 1.7 oster
763 1.7 oster /* we note the state of the parity */
764 1.7 oster raidPtr->parity_good = parity_good;
765 1.6 oster
766 1.6 oster return(fatal_error);
767 1.6 oster }
768 1.6 oster
769 1.9 oster int config_disk_queue(RF_Raid_t *, RF_DiskQueue_t *, RF_RowCol_t,
770 1.9 oster RF_RowCol_t, RF_DiskQueueSW_t *,
771 1.9 oster RF_SectorCount_t, dev_t, int,
772 1.9 oster RF_ShutdownList_t **,
773 1.9 oster RF_AllocListElem_t *);
774 1.7 oster int rf_add_hot_spare(RF_Raid_t *, RF_SingleComponent_t *);
775 1.6 oster int
776 1.6 oster rf_add_hot_spare(raidPtr, sparePtr)
777 1.6 oster RF_Raid_t *raidPtr;
778 1.7 oster RF_SingleComponent_t *sparePtr;
779 1.6 oster {
780 1.6 oster RF_RaidDisk_t *disks;
781 1.9 oster RF_DiskQueue_t *spareQueues;
782 1.6 oster int ret;
783 1.6 oster unsigned int bs;
784 1.6 oster int spare_number;
785 1.6 oster
786 1.6 oster printf("Just in rf_add_hot_spare: %d\n",raidPtr->numSpare);
787 1.6 oster printf("Num col: %d\n",raidPtr->numCol);
788 1.6 oster if (raidPtr->numSpare >= RF_MAXSPARE) {
789 1.6 oster RF_ERRORMSG1("Too many spares: %d\n", raidPtr->numSpare);
790 1.6 oster return(EINVAL);
791 1.6 oster }
792 1.9 oster
793 1.10 oster RF_LOCK_MUTEX(raidPtr->mutex);
794 1.10 oster
795 1.6 oster /* the beginning of the spares... */
796 1.6 oster disks = &raidPtr->Disks[0][raidPtr->numCol];
797 1.6 oster
798 1.6 oster spare_number = raidPtr->numSpare;
799 1.1 oster
800 1.7 oster ret = rf_ConfigureDisk(raidPtr, sparePtr->component_name,
801 1.6 oster &disks[spare_number], 0,
802 1.6 oster raidPtr->numCol + spare_number);
803 1.1 oster
804 1.6 oster if (ret)
805 1.6 oster goto fail;
806 1.6 oster if (disks[spare_number].status != rf_ds_optimal) {
807 1.6 oster RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
808 1.7 oster sparePtr->component_name);
809 1.6 oster ret=EINVAL;
810 1.6 oster goto fail;
811 1.6 oster } else {
812 1.6 oster disks[spare_number].status = rf_ds_spare;
813 1.6 oster DPRINTF6("Spare Disk %d: dev %s numBlocks %ld blockSize %d (%ld MB)\n", spare_number,
814 1.6 oster disks[spare_number].devname,
815 1.6 oster (long int) disks[spare_number].numBlocks,
816 1.6 oster disks[spare_number].blockSize,
817 1.6 oster (long int) disks[spare_number].numBlocks *
818 1.6 oster disks[spare_number].blockSize / 1024 / 1024);
819 1.6 oster }
820 1.6 oster
821 1.1 oster
822 1.6 oster /* check sizes and block sizes on the spare disk */
823 1.6 oster bs = 1 << raidPtr->logBytesPerSector;
824 1.6 oster if (disks[spare_number].blockSize != bs) {
825 1.6 oster RF_ERRORMSG3("Block size of %d on spare disk %s is not the same as on other disks (%d)\n", disks[spare_number].blockSize, disks[spare_number].devname, bs);
826 1.6 oster ret = EINVAL;
827 1.6 oster goto fail;
828 1.6 oster }
829 1.6 oster if (disks[spare_number].numBlocks < raidPtr->sectorsPerDisk) {
830 1.6 oster RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %ld blocks)\n",
831 1.6 oster disks[spare_number].devname,
832 1.6 oster disks[spare_number].blockSize,
833 1.6 oster (long int) raidPtr->sectorsPerDisk);
834 1.6 oster ret = EINVAL;
835 1.6 oster goto fail;
836 1.6 oster } else {
837 1.6 oster if (disks[spare_number].numBlocks >
838 1.6 oster raidPtr->sectorsPerDisk) {
839 1.6 oster RF_ERRORMSG2("Warning: truncating spare disk %s to %ld blocks\n", disks[spare_number].devname,
840 1.6 oster (long int) raidPtr->sectorsPerDisk);
841 1.6 oster
842 1.6 oster disks[spare_number].numBlocks = raidPtr->sectorsPerDisk;
843 1.6 oster }
844 1.5 oster }
845 1.6 oster
846 1.9 oster spareQueues = &raidPtr->Queues[0][raidPtr->numCol];
847 1.9 oster ret = config_disk_queue( raidPtr, &spareQueues[spare_number],
848 1.9 oster 0, raidPtr->numCol + spare_number,
849 1.9 oster raidPtr->Queues[0][0].qPtr, /* XXX */
850 1.9 oster raidPtr->sectorsPerDisk,
851 1.9 oster raidPtr->Disks[0][raidPtr->numCol + spare_number].dev,
852 1.9 oster raidPtr->Queues[0][0].maxOutstanding, /* XXX */
853 1.9 oster &raidPtr->shutdownList,
854 1.9 oster raidPtr->cleanupList);
855 1.9 oster
856 1.9 oster
857 1.6 oster raidPtr->numSpare++;
858 1.10 oster RF_UNLOCK_MUTEX(raidPtr->mutex);
859 1.5 oster return (0);
860 1.6 oster
861 1.6 oster fail:
862 1.10 oster RF_UNLOCK_MUTEX(raidPtr->mutex);
863 1.6 oster return(ret);
864 1.6 oster }
865 1.6 oster
866 1.6 oster int
867 1.6 oster rf_remove_hot_spare(raidPtr,sparePtr)
868 1.6 oster RF_Raid_t *raidPtr;
869 1.7 oster RF_SingleComponent_t *sparePtr;
870 1.6 oster {
871 1.6 oster int spare_number;
872 1.6 oster
873 1.6 oster
874 1.6 oster if (raidPtr->numSpare==0) {
875 1.6 oster printf("No spares to remove!\n");
876 1.6 oster return(EINVAL);
877 1.6 oster }
878 1.6 oster
879 1.7 oster spare_number = sparePtr->column;
880 1.6 oster
881 1.6 oster return(EINVAL); /* XXX not implemented yet */
882 1.6 oster #if 0
883 1.6 oster if (spare_number < 0 || spare_number > raidPtr->numSpare) {
884 1.6 oster return(EINVAL);
885 1.6 oster }
886 1.6 oster
887 1.6 oster /* verify that this spare isn't in use... */
888 1.6 oster
889 1.6 oster
890 1.6 oster
891 1.6 oster
892 1.6 oster /* it's gone.. */
893 1.6 oster
894 1.6 oster raidPtr->numSpare--;
895 1.6 oster
896 1.6 oster return(0);
897 1.6 oster #endif
898 1.1 oster }
899 1.6 oster
900 1.6 oster
901