rf_disks.c revision 1.93.4.2 1 1.93.4.2 martin /* $NetBSD: rf_disks.c,v 1.93.4.2 2024/05/04 12:04:56 martin 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 *
18 1.6 oster * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.6 oster * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.6 oster * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.6 oster * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.6 oster * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.6 oster * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.6 oster * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.6 oster * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.6 oster * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.6 oster * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.6 oster * POSSIBILITY OF SUCH DAMAGE.
29 1.6 oster */
30 1.6 oster
31 1.1 oster /*
32 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
33 1.1 oster * All rights reserved.
34 1.1 oster *
35 1.1 oster * Author: Mark Holland
36 1.1 oster *
37 1.1 oster * Permission to use, copy, modify and distribute this software and
38 1.1 oster * its documentation is hereby granted, provided that both the copyright
39 1.1 oster * notice and this permission notice appear in all copies of the
40 1.1 oster * software, derivative works or modified versions, and any portions
41 1.1 oster * thereof, and that both notices appear in supporting documentation.
42 1.1 oster *
43 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
45 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 1.1 oster *
47 1.1 oster * Carnegie Mellon requests users of this software to return to
48 1.1 oster *
49 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
50 1.1 oster * School of Computer Science
51 1.1 oster * Carnegie Mellon University
52 1.1 oster * Pittsburgh PA 15213-3890
53 1.1 oster *
54 1.1 oster * any improvements or extensions that they make and grant Carnegie the
55 1.1 oster * rights to redistribute these changes.
56 1.1 oster */
57 1.1 oster
58 1.1 oster /***************************************************************
59 1.1 oster * rf_disks.c -- code to perform operations on the actual disks
60 1.1 oster ***************************************************************/
61 1.37 lukem
62 1.37 lukem #include <sys/cdefs.h>
63 1.93.4.2 martin __KERNEL_RCSID(0, "$NetBSD: rf_disks.c,v 1.93.4.2 2024/05/04 12:04:56 martin Exp $");
64 1.1 oster
65 1.35 oster #include <dev/raidframe/raidframevar.h>
66 1.35 oster
67 1.1 oster #include "rf_raid.h"
68 1.1 oster #include "rf_alloclist.h"
69 1.93.4.1 martin #include "rf_driver.h"
70 1.1 oster #include "rf_utils.h"
71 1.1 oster #include "rf_general.h"
72 1.1 oster #include "rf_options.h"
73 1.14 oster #include "rf_kintf.h"
74 1.15 oster #include "rf_netbsd.h"
75 1.1 oster
76 1.1 oster #include <sys/param.h>
77 1.1 oster #include <sys/systm.h>
78 1.1 oster #include <sys/proc.h>
79 1.1 oster #include <sys/ioctl.h>
80 1.1 oster #include <sys/fcntl.h>
81 1.1 oster #include <sys/vnode.h>
82 1.75 dholland #include <sys/namei.h> /* for pathbuf */
83 1.60 elad #include <sys/kauth.h>
84 1.86 hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
85 1.1 oster
86 1.15 oster static int rf_AllocDiskStructures(RF_Raid_t *, RF_Config_t *);
87 1.57 perry static void rf_print_label_status( RF_Raid_t *, int, char *,
88 1.26 oster RF_ComponentLabel_t *);
89 1.57 perry static int rf_check_label_vitals( RF_Raid_t *, int, int, char *,
90 1.26 oster RF_ComponentLabel_t *, int, int );
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.57 perry * We want the spare disks to have regular row,col numbers so that we can
100 1.57 perry * easily substitue a spare for a failed disk. But, the driver code assumes
101 1.57 perry * 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.57 perry * from raidSim. The quick and dirty fix is to make row zero bigger than the
104 1.57 perry * 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.57 perry int
110 1.64 christos rf_ConfigureDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
111 1.49 oster RF_Config_t *cfgPtr)
112 1.1 oster {
113 1.46 oster RF_RaidDisk_t *disks;
114 1.5 oster RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
115 1.46 oster RF_RowCol_t c;
116 1.6 oster int bs, ret;
117 1.5 oster unsigned i, count, foundone = 0, numFailuresThisRow;
118 1.7 oster int force;
119 1.5 oster
120 1.7 oster force = cfgPtr->force;
121 1.5 oster
122 1.22 oster ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
123 1.22 oster if (ret)
124 1.5 oster goto fail;
125 1.5 oster
126 1.22 oster disks = raidPtr->Disks;
127 1.22 oster
128 1.46 oster numFailuresThisRow = 0;
129 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
130 1.57 perry ret = rf_ConfigureDisk(raidPtr,
131 1.46 oster &cfgPtr->devnames[0][c][0],
132 1.46 oster &disks[c], c);
133 1.57 perry
134 1.46 oster if (ret)
135 1.46 oster goto fail;
136 1.57 perry
137 1.46 oster if (disks[c].status == rf_ds_optimal) {
138 1.73 jld ret = raidfetch_component_label(raidPtr, c);
139 1.73 jld if (ret)
140 1.73 jld goto fail;
141 1.80 oster
142 1.80 oster /* mark it as failed if the label looks bogus... */
143 1.80 oster if (!rf_reasonable_label(&raidPtr->raid_cinfo[c].ci_label,0) && !force) {
144 1.80 oster disks[c].status = rf_ds_failed;
145 1.80 oster }
146 1.46 oster }
147 1.7 oster
148 1.46 oster if (disks[c].status != rf_ds_optimal) {
149 1.46 oster numFailuresThisRow++;
150 1.46 oster } else {
151 1.46 oster if (disks[c].numBlocks < min_numblks)
152 1.46 oster min_numblks = disks[c].numBlocks;
153 1.71 sborrill DPRINTF6("Disk at col %d: dev %s numBlocks %" PRIu64 " blockSize %d (%ld MB)\n",
154 1.46 oster c, disks[c].devname,
155 1.71 sborrill disks[c].numBlocks,
156 1.46 oster disks[c].blockSize,
157 1.46 oster (long int) disks[c].numBlocks *
158 1.46 oster disks[c].blockSize / 1024 / 1024);
159 1.5 oster }
160 1.5 oster }
161 1.46 oster /* XXX fix for n-fault tolerant */
162 1.46 oster /* XXX this should probably check to see how many failures
163 1.46 oster we can handle for this configuration! */
164 1.46 oster if (numFailuresThisRow > 0)
165 1.46 oster raidPtr->status = rf_rs_degraded;
166 1.6 oster
167 1.5 oster /* all disks must be the same size & have the same block size, bs must
168 1.5 oster * be a power of 2 */
169 1.5 oster bs = 0;
170 1.46 oster foundone = 0;
171 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
172 1.46 oster if (disks[c].status == rf_ds_optimal) {
173 1.46 oster bs = disks[c].blockSize;
174 1.46 oster foundone = 1;
175 1.46 oster break;
176 1.5 oster }
177 1.5 oster }
178 1.5 oster if (!foundone) {
179 1.5 oster RF_ERRORMSG("RAIDFRAME: Did not find any live disks in the array.\n");
180 1.5 oster ret = EINVAL;
181 1.5 oster goto fail;
182 1.5 oster }
183 1.5 oster for (count = 0, i = 1; i; i <<= 1)
184 1.5 oster if (bs & i)
185 1.5 oster count++;
186 1.5 oster if (count != 1) {
187 1.5 oster RF_ERRORMSG1("Error: block size on disks (%d) must be a power of 2\n", bs);
188 1.5 oster ret = EINVAL;
189 1.5 oster goto fail;
190 1.5 oster }
191 1.6 oster
192 1.6 oster if (rf_CheckLabels( raidPtr, cfgPtr )) {
193 1.7 oster printf("raid%d: There were fatal errors\n", raidPtr->raidid);
194 1.7 oster if (force != 0) {
195 1.7 oster printf("raid%d: Fatal errors being ignored.\n",
196 1.7 oster raidPtr->raidid);
197 1.7 oster } else {
198 1.7 oster ret = EINVAL;
199 1.7 oster goto fail;
200 1.57 perry }
201 1.6 oster }
202 1.7 oster
203 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
204 1.46 oster if (disks[c].status == rf_ds_optimal) {
205 1.46 oster if (disks[c].blockSize != bs) {
206 1.46 oster RF_ERRORMSG1("Error: block size of disk at c %d different from disk at c 0\n", c);
207 1.46 oster ret = EINVAL;
208 1.46 oster goto fail;
209 1.46 oster }
210 1.46 oster if (disks[c].numBlocks != min_numblks) {
211 1.46 oster RF_ERRORMSG2("WARNING: truncating disk at c %d to %d blocks\n",
212 1.46 oster c, (int) min_numblks);
213 1.46 oster disks[c].numBlocks = min_numblks;
214 1.5 oster }
215 1.5 oster }
216 1.5 oster }
217 1.5 oster
218 1.5 oster raidPtr->sectorsPerDisk = min_numblks;
219 1.5 oster raidPtr->logBytesPerSector = ffs(bs) - 1;
220 1.5 oster raidPtr->bytesPerSector = bs;
221 1.5 oster raidPtr->sectorMask = bs - 1;
222 1.5 oster return (0);
223 1.1 oster
224 1.1 oster fail:
225 1.57 perry
226 1.6 oster rf_UnconfigureVnodes( raidPtr );
227 1.1 oster
228 1.5 oster return (ret);
229 1.1 oster }
230 1.1 oster
231 1.1 oster
232 1.6 oster /****************************************************************************
233 1.1 oster * set up the data structures describing the spare disks in the array
234 1.1 oster * recall from the above comment that the spare disk descriptors are stored
235 1.1 oster * in row zero, which is specially expanded to hold them.
236 1.6 oster ****************************************************************************/
237 1.57 perry int
238 1.64 christos rf_ConfigureSpareDisks(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
239 1.49 oster RF_Config_t *cfgPtr)
240 1.1 oster {
241 1.6 oster int i, ret;
242 1.6 oster unsigned int bs;
243 1.5 oster RF_RaidDisk_t *disks;
244 1.5 oster int num_spares_done;
245 1.1 oster
246 1.5 oster num_spares_done = 0;
247 1.5 oster
248 1.5 oster /* The space for the spares should have already been allocated by
249 1.5 oster * ConfigureDisks() */
250 1.5 oster
251 1.46 oster disks = &raidPtr->Disks[raidPtr->numCol];
252 1.5 oster for (i = 0; i < raidPtr->numSpare; i++) {
253 1.5 oster ret = rf_ConfigureDisk(raidPtr, &cfgPtr->spare_names[i][0],
254 1.46 oster &disks[i], raidPtr->numCol + i);
255 1.5 oster if (ret)
256 1.5 oster goto fail;
257 1.5 oster if (disks[i].status != rf_ds_optimal) {
258 1.57 perry RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
259 1.6 oster &cfgPtr->spare_names[i][0]);
260 1.5 oster } else {
261 1.5 oster disks[i].status = rf_ds_spare; /* change status to
262 1.5 oster * spare */
263 1.71 sborrill DPRINTF6("Spare Disk %d: dev %s numBlocks %" PRIu64 " blockSize %d (%ld MB)\n", i,
264 1.5 oster disks[i].devname,
265 1.71 sborrill disks[i].numBlocks, disks[i].blockSize,
266 1.57 perry (long int) disks[i].numBlocks *
267 1.6 oster disks[i].blockSize / 1024 / 1024);
268 1.5 oster }
269 1.5 oster num_spares_done++;
270 1.5 oster }
271 1.1 oster
272 1.5 oster /* check sizes and block sizes on spare disks */
273 1.5 oster bs = 1 << raidPtr->logBytesPerSector;
274 1.5 oster for (i = 0; i < raidPtr->numSpare; i++) {
275 1.5 oster if (disks[i].blockSize != bs) {
276 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);
277 1.5 oster ret = EINVAL;
278 1.5 oster goto fail;
279 1.5 oster }
280 1.5 oster if (disks[i].numBlocks < raidPtr->sectorsPerDisk) {
281 1.71 sborrill RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %" PRIu64 " blocks)\n",
282 1.57 perry disks[i].devname, disks[i].blockSize,
283 1.71 sborrill raidPtr->sectorsPerDisk);
284 1.5 oster ret = EINVAL;
285 1.5 oster goto fail;
286 1.5 oster } else
287 1.5 oster if (disks[i].numBlocks > raidPtr->sectorsPerDisk) {
288 1.71 sborrill RF_ERRORMSG3("Warning: truncating spare disk %s to %" PRIu64 " blocks (from %" PRIu64 ")\n",
289 1.56 tron disks[i].devname,
290 1.71 sborrill raidPtr->sectorsPerDisk,
291 1.71 sborrill disks[i].numBlocks);
292 1.5 oster
293 1.5 oster disks[i].numBlocks = raidPtr->sectorsPerDisk;
294 1.5 oster }
295 1.5 oster }
296 1.5 oster
297 1.5 oster return (0);
298 1.1 oster
299 1.1 oster fail:
300 1.2 oster
301 1.5 oster /* Release the hold on the main components. We've failed to allocate
302 1.57 perry * a spare, and since we're failing, we need to free things..
303 1.57 perry
304 1.57 perry XXX failing to allocate a spare is *not* that big of a deal...
305 1.6 oster We *can* survive without it, if need be, esp. if we get hot
306 1.57 perry adding working.
307 1.1 oster
308 1.57 perry If we don't fail out here, then we need a way to remove this spare...
309 1.57 perry that should be easier to do here than if we are "live"...
310 1.1 oster
311 1.6 oster */
312 1.1 oster
313 1.6 oster rf_UnconfigureVnodes( raidPtr );
314 1.57 perry
315 1.5 oster return (ret);
316 1.1 oster }
317 1.1 oster
318 1.15 oster static int
319 1.64 christos rf_AllocDiskStructures(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
320 1.15 oster {
321 1.15 oster int ret;
322 1.93 mrg size_t entries = raidPtr->numCol + RF_MAXSPARE;
323 1.1 oster
324 1.46 oster /* We allocate RF_MAXSPARE on the first row so that we
325 1.46 oster have room to do hot-swapping of spares */
326 1.93 mrg raidPtr->Disks = RF_MallocAndAdd(
327 1.93 mrg entries * sizeof(*raidPtr->Disks), raidPtr->cleanupList);
328 1.46 oster if (raidPtr->Disks == NULL) {
329 1.15 oster ret = ENOMEM;
330 1.15 oster goto fail;
331 1.15 oster }
332 1.46 oster
333 1.46 oster /* get space for device specific stuff.. */
334 1.91 christos raidPtr->raid_cinfo = RF_MallocAndAdd(
335 1.93 mrg entries * sizeof(*raidPtr->raid_cinfo), raidPtr->cleanupList);
336 1.15 oster if (raidPtr->raid_cinfo == NULL) {
337 1.15 oster ret = ENOMEM;
338 1.15 oster goto fail;
339 1.15 oster }
340 1.15 oster
341 1.93.4.1 martin raidPtr->abortRecon = RF_MallocAndAdd(
342 1.93.4.1 martin entries * sizeof(int), raidPtr->cleanupList);
343 1.93.4.1 martin if (raidPtr->abortRecon == NULL) {
344 1.93.4.1 martin ret = ENOMEM;
345 1.93.4.1 martin goto fail;
346 1.93.4.1 martin }
347 1.93.4.1 martin
348 1.93.4.1 martin
349 1.15 oster return(0);
350 1.57 perry fail:
351 1.15 oster rf_UnconfigureVnodes( raidPtr );
352 1.15 oster
353 1.15 oster return(ret);
354 1.15 oster }
355 1.15 oster
356 1.15 oster
357 1.15 oster /* configure a single disk during auto-configuration at boot */
358 1.15 oster int
359 1.49 oster rf_AutoConfigureDisks(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr,
360 1.49 oster RF_AutoConfig_t *auto_config)
361 1.15 oster {
362 1.46 oster RF_RaidDisk_t *disks;
363 1.15 oster RF_RaidDisk_t *diskPtr;
364 1.57 perry RF_RowCol_t c;
365 1.15 oster RF_SectorCount_t min_numblks = (RF_SectorCount_t) 0x7FFFFFFFFFFFLL;
366 1.15 oster int bs, ret;
367 1.15 oster int numFailuresThisRow;
368 1.15 oster RF_AutoConfig_t *ac;
369 1.17 oster int parity_good;
370 1.20 oster int mod_counter;
371 1.30 oster int mod_counter_found;
372 1.15 oster
373 1.15 oster #if DEBUG
374 1.15 oster printf("Starting autoconfiguration of RAID set...\n");
375 1.15 oster #endif
376 1.15 oster
377 1.15 oster ret = rf_AllocDiskStructures(raidPtr, cfgPtr);
378 1.15 oster if (ret)
379 1.15 oster goto fail;
380 1.15 oster
381 1.15 oster disks = raidPtr->Disks;
382 1.15 oster
383 1.17 oster /* assume the parity will be fine.. */
384 1.17 oster parity_good = RF_RAID_CLEAN;
385 1.17 oster
386 1.20 oster /* Check for mod_counters that are too low */
387 1.30 oster mod_counter_found = 0;
388 1.34 oster mod_counter = 0;
389 1.20 oster ac = auto_config;
390 1.20 oster while(ac!=NULL) {
391 1.30 oster if (mod_counter_found==0) {
392 1.20 oster mod_counter = ac->clabel->mod_counter;
393 1.30 oster mod_counter_found = 1;
394 1.30 oster } else {
395 1.20 oster if (ac->clabel->mod_counter > mod_counter) {
396 1.20 oster mod_counter = ac->clabel->mod_counter;
397 1.20 oster }
398 1.20 oster }
399 1.30 oster ac->flag = 0; /* clear the general purpose flag */
400 1.30 oster ac = ac->next;
401 1.20 oster }
402 1.20 oster
403 1.34 oster bs = 0;
404 1.15 oster
405 1.46 oster numFailuresThisRow = 0;
406 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
407 1.46 oster diskPtr = &disks[c];
408 1.57 perry
409 1.46 oster /* find this row/col in the autoconfig */
410 1.15 oster #if DEBUG
411 1.46 oster printf("Looking for %d in autoconfig\n",c);
412 1.15 oster #endif
413 1.46 oster ac = auto_config;
414 1.46 oster while(ac!=NULL) {
415 1.46 oster if (ac->clabel==NULL) {
416 1.46 oster /* big-time bad news. */
417 1.46 oster goto fail;
418 1.46 oster }
419 1.46 oster if ((ac->clabel->column == c) &&
420 1.46 oster (ac->clabel->mod_counter == mod_counter)) {
421 1.46 oster /* it's this one... */
422 1.46 oster /* flag it as 'used', so we don't
423 1.46 oster free it later. */
424 1.46 oster ac->flag = 1;
425 1.46 oster #if DEBUG
426 1.46 oster printf("Found: %s at %d\n",
427 1.46 oster ac->devname,c);
428 1.46 oster #endif
429 1.57 perry
430 1.46 oster break;
431 1.46 oster }
432 1.46 oster ac=ac->next;
433 1.46 oster }
434 1.57 perry
435 1.46 oster if (ac==NULL) {
436 1.46 oster /* we didn't find an exact match with a
437 1.46 oster correct mod_counter above... can we find
438 1.46 oster one with an incorrect mod_counter to use
439 1.46 oster instead? (this one, if we find it, will be
440 1.57 perry marked as failed once the set configures)
441 1.46 oster */
442 1.46 oster
443 1.15 oster ac = auto_config;
444 1.15 oster while(ac!=NULL) {
445 1.15 oster if (ac->clabel==NULL) {
446 1.15 oster /* big-time bad news. */
447 1.15 oster goto fail;
448 1.15 oster }
449 1.46 oster if (ac->clabel->column == c) {
450 1.57 perry /* it's this one...
451 1.57 perry flag it as 'used', so we
452 1.46 oster don't free it later. */
453 1.29 oster ac->flag = 1;
454 1.15 oster #if DEBUG
455 1.46 oster printf("Found(low mod_counter): %s at %d\n",
456 1.46 oster ac->devname,c);
457 1.15 oster #endif
458 1.57 perry
459 1.15 oster break;
460 1.15 oster }
461 1.15 oster ac=ac->next;
462 1.15 oster }
463 1.46 oster }
464 1.29 oster
465 1.29 oster
466 1.29 oster
467 1.46 oster if (ac!=NULL) {
468 1.46 oster /* Found it. Configure it.. */
469 1.46 oster diskPtr->blockSize = ac->clabel->blockSize;
470 1.78 enami diskPtr->numBlocks =
471 1.78 enami rf_component_label_numblocks(ac->clabel);
472 1.57 perry /* Note: rf_protectedSectors is already
473 1.46 oster factored into numBlocks here */
474 1.46 oster raidPtr->raid_cinfo[c].ci_vp = ac->vp;
475 1.46 oster raidPtr->raid_cinfo[c].ci_dev = ac->dev;
476 1.57 perry
477 1.72 jld memcpy(raidget_component_label(raidPtr, c),
478 1.52 itojun ac->clabel, sizeof(*ac->clabel));
479 1.52 itojun snprintf(diskPtr->devname, sizeof(diskPtr->devname),
480 1.52 itojun "/dev/%s", ac->devname);
481 1.57 perry
482 1.46 oster /* note the fact that this component was
483 1.46 oster autoconfigured. You'll need this info
484 1.46 oster later. Trust me :) */
485 1.46 oster diskPtr->auto_configured = 1;
486 1.46 oster diskPtr->dev = ac->dev;
487 1.57 perry
488 1.57 perry /*
489 1.46 oster * we allow the user to specify that
490 1.46 oster * only a fraction of the disks should
491 1.46 oster * be used this is just for debug: it
492 1.57 perry * speeds up the parity scan
493 1.46 oster */
494 1.57 perry
495 1.57 perry diskPtr->numBlocks = diskPtr->numBlocks *
496 1.46 oster rf_sizePercentage / 100;
497 1.57 perry
498 1.57 perry /* XXX these will get set multiple times,
499 1.46 oster but since we're autoconfiguring, they'd
500 1.46 oster better be always the same each time!
501 1.46 oster If not, this is the least of your worries */
502 1.46 oster
503 1.46 oster bs = diskPtr->blockSize;
504 1.46 oster min_numblks = diskPtr->numBlocks;
505 1.57 perry
506 1.46 oster /* this gets done multiple times, but that's
507 1.46 oster fine -- the serial number will be the same
508 1.46 oster for all components, guaranteed */
509 1.46 oster raidPtr->serial_number = ac->clabel->serial_number;
510 1.46 oster /* check the last time the label was modified */
511 1.46 oster
512 1.46 oster if (ac->clabel->mod_counter != mod_counter) {
513 1.46 oster /* Even though we've filled in all of
514 1.46 oster the above, we don't trust this
515 1.87 snj component since its modification
516 1.46 oster counter is not in sync with the
517 1.46 oster rest, and we really consider it to
518 1.46 oster be failed. */
519 1.46 oster disks[c].status = rf_ds_failed;
520 1.46 oster numFailuresThisRow++;
521 1.46 oster } else {
522 1.46 oster if (ac->clabel->clean != RF_RAID_CLEAN) {
523 1.46 oster parity_good = RF_RAID_DIRTY;
524 1.17 oster }
525 1.15 oster }
526 1.46 oster } else {
527 1.46 oster /* Didn't find it at all!! Component must
528 1.46 oster really be dead */
529 1.46 oster disks[c].status = rf_ds_failed;
530 1.52 itojun snprintf(disks[c].devname, sizeof(disks[c].devname),
531 1.52 itojun "component%d", c);
532 1.46 oster numFailuresThisRow++;
533 1.15 oster }
534 1.29 oster }
535 1.46 oster /* XXX fix for n-fault tolerant */
536 1.46 oster /* XXX this should probably check to see how many failures
537 1.46 oster we can handle for this configuration! */
538 1.51 oster if (numFailuresThisRow > 0) {
539 1.46 oster raidPtr->status = rf_rs_degraded;
540 1.51 oster raidPtr->numFailures = numFailuresThisRow;
541 1.51 oster }
542 1.57 perry
543 1.29 oster /* close the device for the ones that didn't get used */
544 1.29 oster
545 1.29 oster ac = auto_config;
546 1.29 oster while(ac!=NULL) {
547 1.29 oster if (ac->flag == 0) {
548 1.32 oster vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
549 1.68 pooka VOP_CLOSE(ac->vp, FREAD | FWRITE, NOCRED);
550 1.29 oster vput(ac->vp);
551 1.29 oster ac->vp = NULL;
552 1.57 perry #if DEBUG
553 1.29 oster printf("Released %s from auto-config set.\n",
554 1.29 oster ac->devname);
555 1.29 oster #endif
556 1.29 oster }
557 1.29 oster ac = ac->next;
558 1.15 oster }
559 1.20 oster
560 1.20 oster raidPtr->mod_counter = mod_counter;
561 1.15 oster
562 1.17 oster /* note the state of the parity, if any */
563 1.17 oster raidPtr->parity_good = parity_good;
564 1.15 oster raidPtr->sectorsPerDisk = min_numblks;
565 1.15 oster raidPtr->logBytesPerSector = ffs(bs) - 1;
566 1.15 oster raidPtr->bytesPerSector = bs;
567 1.15 oster raidPtr->sectorMask = bs - 1;
568 1.15 oster return (0);
569 1.15 oster
570 1.15 oster fail:
571 1.57 perry
572 1.15 oster rf_UnconfigureVnodes( raidPtr );
573 1.15 oster
574 1.15 oster return (ret);
575 1.15 oster
576 1.15 oster }
577 1.1 oster
578 1.1 oster /* configure a single disk in the array */
579 1.57 perry int
580 1.58 christos rf_ConfigureDisk(RF_Raid_t *raidPtr, char *bf, RF_RaidDisk_t *diskPtr,
581 1.49 oster RF_RowCol_t col)
582 1.1 oster {
583 1.5 oster char *p;
584 1.75 dholland struct pathbuf *pb;
585 1.1 oster struct vnode *vp;
586 1.5 oster int error;
587 1.1 oster
588 1.58 christos p = rf_find_non_white(bf);
589 1.5 oster if (p[strlen(p) - 1] == '\n') {
590 1.5 oster /* strip off the newline */
591 1.5 oster p[strlen(p) - 1] = '\0';
592 1.5 oster }
593 1.5 oster (void) strcpy(diskPtr->devname, p);
594 1.1 oster
595 1.5 oster /* Let's start by claiming the component is fine and well... */
596 1.5 oster diskPtr->status = rf_ds_optimal;
597 1.5 oster
598 1.46 oster raidPtr->raid_cinfo[col].ci_vp = NULL;
599 1.46 oster raidPtr->raid_cinfo[col].ci_dev = 0;
600 1.5 oster
601 1.53 oster if (!strcmp("absent", diskPtr->devname)) {
602 1.53 oster printf("Ignoring missing component at column %d\n", col);
603 1.85 christos snprintf(diskPtr->devname, sizeof(diskPtr->devname),
604 1.85 christos "component%d", col);
605 1.53 oster diskPtr->status = rf_ds_failed;
606 1.53 oster return (0);
607 1.53 oster }
608 1.53 oster
609 1.75 dholland pb = pathbuf_create(diskPtr->devname);
610 1.75 dholland if (pb == NULL) {
611 1.75 dholland printf("pathbuf_create for device: %s failed!\n",
612 1.75 dholland diskPtr->devname);
613 1.75 dholland return ENOMEM;
614 1.75 dholland }
615 1.92 mlelstv error = vn_bdev_openpath(pb, &vp, curlwp);
616 1.75 dholland pathbuf_destroy(pb);
617 1.5 oster if (error) {
618 1.93 mrg printf("open device: '%s' failed: %d\n", diskPtr->devname, error);
619 1.5 oster if (error == ENXIO) {
620 1.6 oster /* the component isn't there... must be dead :-( */
621 1.5 oster diskPtr->status = rf_ds_failed;
622 1.76 mrg return 0;
623 1.5 oster } else {
624 1.5 oster return (error);
625 1.5 oster }
626 1.5 oster }
627 1.76 mrg
628 1.81 oster if ((error = rf_getdisksize(vp, diskPtr)) != 0)
629 1.76 mrg return (error);
630 1.76 mrg
631 1.76 mrg /*
632 1.76 mrg * If this raidPtr's bytesPerSector is zero, fill it in with this
633 1.76 mrg * components blockSize. This will give us something to work with
634 1.76 mrg * initially, and if it is wrong, we'll get errors later.
635 1.76 mrg */
636 1.76 mrg if (raidPtr->bytesPerSector == 0)
637 1.76 mrg raidPtr->bytesPerSector = diskPtr->blockSize;
638 1.76 mrg
639 1.5 oster if (diskPtr->status == rf_ds_optimal) {
640 1.46 oster raidPtr->raid_cinfo[col].ci_vp = vp;
641 1.86 hannken raidPtr->raid_cinfo[col].ci_dev = vp->v_rdev;
642 1.57 perry
643 1.16 oster /* This component was not automatically configured */
644 1.16 oster diskPtr->auto_configured = 0;
645 1.86 hannken diskPtr->dev = vp->v_rdev;
646 1.57 perry
647 1.6 oster /* we allow the user to specify that only a fraction of the
648 1.6 oster * disks should be used this is just for debug: it speeds up
649 1.6 oster * the parity scan */
650 1.57 perry diskPtr->numBlocks = diskPtr->numBlocks *
651 1.6 oster rf_sizePercentage / 100;
652 1.6 oster }
653 1.6 oster return (0);
654 1.6 oster }
655 1.6 oster
656 1.7 oster static void
657 1.49 oster rf_print_label_status(RF_Raid_t *raidPtr, int column, char *dev_name,
658 1.49 oster RF_ComponentLabel_t *ci_label)
659 1.7 oster {
660 1.7 oster
661 1.57 perry printf("raid%d: Component %s being configured at col: %d\n",
662 1.46 oster raidPtr->raidid, dev_name, column );
663 1.46 oster printf(" Column: %d Num Columns: %d\n",
664 1.57 perry ci_label->column,
665 1.46 oster ci_label->num_columns);
666 1.7 oster printf(" Version: %d Serial Number: %d Mod Counter: %d\n",
667 1.7 oster ci_label->version, ci_label->serial_number,
668 1.7 oster ci_label->mod_counter);
669 1.11 oster printf(" Clean: %s Status: %d\n",
670 1.11 oster ci_label->clean ? "Yes" : "No", ci_label->status );
671 1.7 oster }
672 1.7 oster
673 1.49 oster static int rf_check_label_vitals(RF_Raid_t *raidPtr, int row, int column,
674 1.49 oster char *dev_name, RF_ComponentLabel_t *ci_label,
675 1.49 oster int serial_number, int mod_counter)
676 1.7 oster {
677 1.7 oster int fatal_error = 0;
678 1.7 oster
679 1.7 oster if (serial_number != ci_label->serial_number) {
680 1.57 perry printf("%s has a different serial number: %d %d\n",
681 1.7 oster dev_name, serial_number, ci_label->serial_number);
682 1.7 oster fatal_error = 1;
683 1.7 oster }
684 1.7 oster if (mod_counter != ci_label->mod_counter) {
685 1.65 christos printf("%s has a different modification count: %d %d\n",
686 1.7 oster dev_name, mod_counter, ci_label->mod_counter);
687 1.7 oster }
688 1.57 perry
689 1.7 oster if (row != ci_label->row) {
690 1.57 perry printf("Row out of alignment for: %s\n", dev_name);
691 1.7 oster fatal_error = 1;
692 1.7 oster }
693 1.7 oster if (column != ci_label->column) {
694 1.7 oster printf("Column out of alignment for: %s\n", dev_name);
695 1.7 oster fatal_error = 1;
696 1.7 oster }
697 1.7 oster if (raidPtr->numCol != ci_label->num_columns) {
698 1.7 oster printf("Number of columns do not match for: %s\n", dev_name);
699 1.7 oster fatal_error = 1;
700 1.7 oster }
701 1.7 oster if (ci_label->clean == 0) {
702 1.7 oster /* it's not clean, but that's not fatal */
703 1.7 oster printf("%s is not clean!\n", dev_name);
704 1.7 oster }
705 1.7 oster return(fatal_error);
706 1.7 oster }
707 1.7 oster
708 1.7 oster
709 1.88 christos static void
710 1.89 christos rf_handle_hosed(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, int hosed_column,
711 1.89 christos int again)
712 1.88 christos {
713 1.88 christos printf("Hosed component: %s\n", &cfgPtr->devnames[0][hosed_column][0]);
714 1.90 christos if (cfgPtr->force)
715 1.88 christos return;
716 1.88 christos
717 1.88 christos /* we'll fail this component, as if there are
718 1.89 christos other major errors, we aren't forcing things
719 1.88 christos and we'll abort the config anyways */
720 1.89 christos if (again && raidPtr->Disks[hosed_column].status == rf_ds_failed)
721 1.88 christos return;
722 1.88 christos
723 1.88 christos raidPtr->Disks[hosed_column].status = rf_ds_failed;
724 1.88 christos raidPtr->numFailures++;
725 1.88 christos raidPtr->status = rf_rs_degraded;
726 1.88 christos }
727 1.88 christos
728 1.57 perry /*
729 1.6 oster
730 1.6 oster rf_CheckLabels() - check all the component labels for consistency.
731 1.6 oster Return an error if there is anything major amiss.
732 1.6 oster
733 1.6 oster */
734 1.1 oster
735 1.57 perry int
736 1.49 oster rf_CheckLabels(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr)
737 1.6 oster {
738 1.46 oster int c;
739 1.6 oster char *dev_name;
740 1.6 oster RF_ComponentLabel_t *ci_label;
741 1.6 oster int serial_number = 0;
742 1.7 oster int mod_number = 0;
743 1.6 oster int fatal_error = 0;
744 1.7 oster int mod_values[4];
745 1.7 oster int mod_count[4];
746 1.7 oster int ser_values[4];
747 1.7 oster int ser_count[4];
748 1.7 oster int num_ser;
749 1.7 oster int num_mod;
750 1.7 oster int i;
751 1.7 oster int found;
752 1.7 oster int hosed_column;
753 1.7 oster int too_fatal;
754 1.7 oster int parity_good;
755 1.7 oster
756 1.7 oster hosed_column = -1;
757 1.7 oster too_fatal = 0;
758 1.7 oster
759 1.57 perry /*
760 1.57 perry We're going to try to be a little intelligent here. If one
761 1.7 oster component's label is bogus, and we can identify that it's the
762 1.7 oster *only* one that's gone, we'll mark it as "failed" and allow
763 1.7 oster the configuration to proceed. This will be the *only* case
764 1.7 oster that we'll proceed if there would be (otherwise) fatal errors.
765 1.57 perry
766 1.7 oster Basically we simply keep a count of how many components had
767 1.7 oster what serial number. If all but one agree, we simply mark
768 1.57 perry the disagreeing component as being failed, and allow
769 1.7 oster things to come up "normally".
770 1.57 perry
771 1.7 oster We do this first for serial numbers, and then for "mod_counter".
772 1.6 oster
773 1.7 oster */
774 1.7 oster
775 1.7 oster num_ser = 0;
776 1.7 oster num_mod = 0;
777 1.46 oster
778 1.80 oster ser_values[0] = ser_values[1] = ser_values[2] = ser_values[3] = 0;
779 1.80 oster ser_count[0] = ser_count[1] = ser_count[2] = ser_count[3] = 0;
780 1.83 pooka mod_values[0] = mod_values[1] = mod_values[2] = mod_values[3] = 0;
781 1.83 pooka mod_count[0] = mod_count[1] = mod_count[2] = mod_count[3] = 0;
782 1.80 oster
783 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
784 1.80 oster if (raidPtr->Disks[c].status != rf_ds_optimal)
785 1.80 oster continue;
786 1.72 jld ci_label = raidget_component_label(raidPtr, c);
787 1.46 oster found=0;
788 1.46 oster for(i=0;i<num_ser;i++) {
789 1.46 oster if (ser_values[i] == ci_label->serial_number) {
790 1.46 oster ser_count[i]++;
791 1.46 oster found=1;
792 1.46 oster break;
793 1.7 oster }
794 1.46 oster }
795 1.46 oster if (!found) {
796 1.46 oster ser_values[num_ser] = ci_label->serial_number;
797 1.46 oster ser_count[num_ser] = 1;
798 1.46 oster num_ser++;
799 1.46 oster if (num_ser>2) {
800 1.46 oster fatal_error = 1;
801 1.46 oster break;
802 1.7 oster }
803 1.46 oster }
804 1.46 oster found=0;
805 1.46 oster for(i=0;i<num_mod;i++) {
806 1.46 oster if (mod_values[i] == ci_label->mod_counter) {
807 1.46 oster mod_count[i]++;
808 1.46 oster found=1;
809 1.46 oster break;
810 1.7 oster }
811 1.46 oster }
812 1.46 oster if (!found) {
813 1.46 oster mod_values[num_mod] = ci_label->mod_counter;
814 1.46 oster mod_count[num_mod] = 1;
815 1.46 oster num_mod++;
816 1.46 oster if (num_mod>2) {
817 1.46 oster fatal_error = 1;
818 1.46 oster break;
819 1.7 oster }
820 1.7 oster }
821 1.7 oster }
822 1.7 oster #if DEBUG
823 1.7 oster printf("raid%d: Summary of serial numbers:\n", raidPtr->raidid);
824 1.7 oster for(i=0;i<num_ser;i++) {
825 1.7 oster printf("%d %d\n", ser_values[i], ser_count[i]);
826 1.7 oster }
827 1.7 oster printf("raid%d: Summary of mod counters:\n", raidPtr->raidid);
828 1.7 oster for(i=0;i<num_mod;i++) {
829 1.7 oster printf("%d %d\n", mod_values[i], mod_count[i]);
830 1.7 oster }
831 1.7 oster #endif
832 1.7 oster serial_number = ser_values[0];
833 1.7 oster if (num_ser == 2) {
834 1.7 oster if ((ser_count[0] == 1) || (ser_count[1] == 1)) {
835 1.7 oster /* Locate the maverick component */
836 1.7 oster if (ser_count[1] > ser_count[0]) {
837 1.7 oster serial_number = ser_values[1];
838 1.57 perry }
839 1.46 oster
840 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
841 1.80 oster if (raidPtr->Disks[c].status != rf_ds_optimal)
842 1.80 oster continue;
843 1.72 jld ci_label = raidget_component_label(raidPtr, c);
844 1.46 oster if (serial_number != ci_label->serial_number) {
845 1.46 oster hosed_column = c;
846 1.46 oster break;
847 1.7 oster }
848 1.7 oster }
849 1.88 christos if (hosed_column != -1)
850 1.89 christos rf_handle_hosed(raidPtr, cfgPtr, hosed_column,
851 1.89 christos 0);
852 1.7 oster } else {
853 1.7 oster too_fatal = 1;
854 1.7 oster }
855 1.7 oster if (cfgPtr->parityConfig == '0') {
856 1.57 perry /* We've identified two different serial numbers.
857 1.7 oster RAID 0 can't cope with that, so we'll punt */
858 1.7 oster too_fatal = 1;
859 1.7 oster }
860 1.7 oster
861 1.57 perry }
862 1.7 oster
863 1.7 oster /* record the serial number for later. If we bail later, setting
864 1.57 perry this doesn't matter, otherwise we've got the best guess at the
865 1.7 oster correct serial number */
866 1.7 oster raidPtr->serial_number = serial_number;
867 1.7 oster
868 1.7 oster mod_number = mod_values[0];
869 1.7 oster if (num_mod == 2) {
870 1.7 oster if ((mod_count[0] == 1) || (mod_count[1] == 1)) {
871 1.7 oster /* Locate the maverick component */
872 1.7 oster if (mod_count[1] > mod_count[0]) {
873 1.7 oster mod_number = mod_values[1];
874 1.8 oster } else if (mod_count[1] < mod_count[0]) {
875 1.8 oster mod_number = mod_values[0];
876 1.8 oster } else {
877 1.8 oster /* counts of different modification values
878 1.57 perry are the same. Assume greater value is
879 1.57 perry the correct one, all other things
880 1.8 oster considered */
881 1.8 oster if (mod_values[0] > mod_values[1]) {
882 1.8 oster mod_number = mod_values[0];
883 1.8 oster } else {
884 1.8 oster mod_number = mod_values[1];
885 1.8 oster }
886 1.57 perry
887 1.8 oster }
888 1.46 oster
889 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
890 1.80 oster if (raidPtr->Disks[c].status != rf_ds_optimal)
891 1.80 oster continue;
892 1.80 oster
893 1.72 jld ci_label = raidget_component_label(raidPtr, c);
894 1.46 oster if (mod_number != ci_label->mod_counter) {
895 1.46 oster if (hosed_column == c) {
896 1.46 oster /* same one. Can
897 1.46 oster deal with it. */
898 1.46 oster } else {
899 1.46 oster hosed_column = c;
900 1.46 oster if (num_ser != 1) {
901 1.46 oster too_fatal = 1;
902 1.46 oster break;
903 1.7 oster }
904 1.7 oster }
905 1.7 oster }
906 1.7 oster }
907 1.88 christos if (hosed_column != -1)
908 1.89 christos rf_handle_hosed(raidPtr, cfgPtr, hosed_column,
909 1.89 christos 1);
910 1.7 oster } else {
911 1.7 oster too_fatal = 1;
912 1.7 oster }
913 1.7 oster if (cfgPtr->parityConfig == '0') {
914 1.7 oster /* We've identified two different mod counters.
915 1.7 oster RAID 0 can't cope with that, so we'll punt */
916 1.7 oster too_fatal = 1;
917 1.7 oster }
918 1.57 perry }
919 1.7 oster
920 1.7 oster raidPtr->mod_counter = mod_number;
921 1.7 oster
922 1.7 oster if (too_fatal) {
923 1.7 oster /* we've had both a serial number mismatch, and a mod_counter
924 1.7 oster mismatch -- and they involved two different components!!
925 1.7 oster Bail -- make things fail so that the user must force
926 1.7 oster the issue... */
927 1.7 oster hosed_column = -1;
928 1.54 oster fatal_error = 1;
929 1.7 oster }
930 1.7 oster
931 1.7 oster if (num_ser > 2) {
932 1.57 perry printf("raid%d: Too many different serial numbers!\n",
933 1.7 oster raidPtr->raidid);
934 1.54 oster fatal_error = 1;
935 1.7 oster }
936 1.7 oster
937 1.7 oster if (num_mod > 2) {
938 1.57 perry printf("raid%d: Too many different mod counters!\n",
939 1.7 oster raidPtr->raidid);
940 1.54 oster fatal_error = 1;
941 1.7 oster }
942 1.7 oster
943 1.80 oster for (c = 0; c < raidPtr->numCol; c++) {
944 1.80 oster if (raidPtr->Disks[c].status != rf_ds_optimal) {
945 1.80 oster hosed_column = c;
946 1.80 oster break;
947 1.80 oster }
948 1.80 oster }
949 1.80 oster
950 1.7 oster /* we start by assuming the parity will be good, and flee from
951 1.7 oster that notion at the slightest sign of trouble */
952 1.7 oster
953 1.7 oster parity_good = RF_RAID_CLEAN;
954 1.46 oster
955 1.46 oster for (c = 0; c < raidPtr->numCol; c++) {
956 1.46 oster dev_name = &cfgPtr->devnames[0][c][0];
957 1.72 jld ci_label = raidget_component_label(raidPtr, c);
958 1.57 perry
959 1.46 oster if (c == hosed_column) {
960 1.46 oster printf("raid%d: Ignoring %s\n",
961 1.46 oster raidPtr->raidid, dev_name);
962 1.57 perry } else {
963 1.46 oster rf_print_label_status( raidPtr, c, dev_name, ci_label);
964 1.57 perry if (rf_check_label_vitals( raidPtr, 0, c,
965 1.46 oster dev_name, ci_label,
966 1.57 perry serial_number,
967 1.46 oster mod_number )) {
968 1.46 oster fatal_error = 1;
969 1.46 oster }
970 1.46 oster if (ci_label->clean != RF_RAID_CLEAN) {
971 1.46 oster parity_good = RF_RAID_DIRTY;
972 1.6 oster }
973 1.6 oster }
974 1.6 oster }
975 1.57 perry
976 1.7 oster if (fatal_error) {
977 1.7 oster parity_good = RF_RAID_DIRTY;
978 1.7 oster }
979 1.7 oster
980 1.7 oster /* we note the state of the parity */
981 1.7 oster raidPtr->parity_good = parity_good;
982 1.6 oster
983 1.57 perry return(fatal_error);
984 1.6 oster }
985 1.6 oster
986 1.6 oster int
987 1.49 oster rf_add_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
988 1.6 oster {
989 1.9 oster RF_DiskQueue_t *spareQueues;
990 1.93.4.1 martin RF_RaidDisk_t *disks;
991 1.6 oster int ret;
992 1.6 oster unsigned int bs;
993 1.6 oster int spare_number;
994 1.6 oster
995 1.40 oster ret=0;
996 1.40 oster
997 1.6 oster if (raidPtr->numSpare >= RF_MAXSPARE) {
998 1.6 oster RF_ERRORMSG1("Too many spares: %d\n", raidPtr->numSpare);
999 1.6 oster return(EINVAL);
1000 1.6 oster }
1001 1.9 oster
1002 1.79 mrg rf_lock_mutex2(raidPtr->mutex);
1003 1.93.4.1 martin while (raidPtr->changing_components == 1) {
1004 1.93.4.1 martin rf_wait_cond2(raidPtr->changing_components_cv, raidPtr->mutex);
1005 1.40 oster }
1006 1.93.4.1 martin raidPtr->changing_components = 1;
1007 1.79 mrg rf_unlock_mutex2(raidPtr->mutex);
1008 1.10 oster
1009 1.6 oster /* the beginning of the spares... */
1010 1.46 oster disks = &raidPtr->Disks[raidPtr->numCol];
1011 1.6 oster
1012 1.6 oster spare_number = raidPtr->numSpare;
1013 1.1 oster
1014 1.7 oster ret = rf_ConfigureDisk(raidPtr, sparePtr->component_name,
1015 1.46 oster &disks[spare_number],
1016 1.6 oster raidPtr->numCol + spare_number);
1017 1.1 oster
1018 1.6 oster if (ret)
1019 1.6 oster goto fail;
1020 1.6 oster if (disks[spare_number].status != rf_ds_optimal) {
1021 1.57 perry RF_ERRORMSG1("Warning: spare disk %s failed TUR\n",
1022 1.7 oster sparePtr->component_name);
1023 1.46 oster rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1024 1.6 oster ret=EINVAL;
1025 1.6 oster goto fail;
1026 1.6 oster } else {
1027 1.6 oster disks[spare_number].status = rf_ds_spare;
1028 1.71 sborrill DPRINTF6("Spare Disk %d: dev %s numBlocks %" PRIu64 " blockSize %d (%ld MB)\n",
1029 1.71 sborrill spare_number,
1030 1.6 oster disks[spare_number].devname,
1031 1.71 sborrill disks[spare_number].numBlocks,
1032 1.6 oster disks[spare_number].blockSize,
1033 1.57 perry (long int) disks[spare_number].numBlocks *
1034 1.6 oster disks[spare_number].blockSize / 1024 / 1024);
1035 1.6 oster }
1036 1.57 perry
1037 1.1 oster
1038 1.6 oster /* check sizes and block sizes on the spare disk */
1039 1.6 oster bs = 1 << raidPtr->logBytesPerSector;
1040 1.6 oster if (disks[spare_number].blockSize != bs) {
1041 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);
1042 1.46 oster rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1043 1.6 oster ret = EINVAL;
1044 1.6 oster goto fail;
1045 1.6 oster }
1046 1.6 oster if (disks[spare_number].numBlocks < raidPtr->sectorsPerDisk) {
1047 1.71 sborrill RF_ERRORMSG3("Spare disk %s (%d blocks) is too small to serve as a spare (need %" PRIu64 " blocks)\n",
1048 1.57 perry disks[spare_number].devname,
1049 1.57 perry disks[spare_number].blockSize,
1050 1.71 sborrill raidPtr->sectorsPerDisk);
1051 1.46 oster rf_close_component(raidPtr, raidPtr->raid_cinfo[raidPtr->numCol+spare_number].ci_vp, 0);
1052 1.6 oster ret = EINVAL;
1053 1.6 oster goto fail;
1054 1.6 oster } else {
1055 1.57 perry if (disks[spare_number].numBlocks >
1056 1.6 oster raidPtr->sectorsPerDisk) {
1057 1.71 sborrill RF_ERRORMSG3("Warning: truncating spare disk %s to %" PRIu64 " blocks (from %" PRIu64 ")\n",
1058 1.57 perry disks[spare_number].devname,
1059 1.71 sborrill raidPtr->sectorsPerDisk,
1060 1.71 sborrill disks[spare_number].numBlocks);
1061 1.57 perry
1062 1.6 oster disks[spare_number].numBlocks = raidPtr->sectorsPerDisk;
1063 1.6 oster }
1064 1.5 oster }
1065 1.6 oster
1066 1.93.4.1 martin /*
1067 1.93.4.1 martin * We only grow one initialized diskQueue at a time
1068 1.93.4.1 martin * spare_number can be lower than raidPtr->maxQueue (update)
1069 1.93.4.1 martin * or they can be equal (initialize new queue)
1070 1.93.4.1 martin */
1071 1.93.4.1 martin RF_ASSERT(spare_number <= raidPtr->maxQueue);
1072 1.57 perry
1073 1.93.4.1 martin spareQueues = &raidPtr->Queues[raidPtr->numCol];
1074 1.93.4.1 martin if (spare_number == raidPtr->maxQueue) {
1075 1.93.4.1 martin ret = rf_ConfigureDiskQueue(raidPtr, &spareQueues[spare_number],
1076 1.93.4.1 martin raidPtr->numCol + spare_number,
1077 1.93.4.1 martin raidPtr->qType,
1078 1.93.4.1 martin raidPtr->sectorsPerDisk,
1079 1.93.4.1 martin raidPtr->Disks[raidPtr->numCol +
1080 1.93.4.1 martin spare_number].dev,
1081 1.93.4.1 martin raidPtr->maxOutstanding,
1082 1.93.4.1 martin &raidPtr->shutdownList,
1083 1.93.4.1 martin raidPtr->cleanupList);
1084 1.93.4.1 martin if (ret)
1085 1.93.4.1 martin goto fail;
1086 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1087 1.93.4.1 martin raidPtr->maxQueue++;
1088 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1089 1.93.4.1 martin } else {
1090 1.93.4.1 martin (void)rf_UpdateDiskQueue(&spareQueues[spare_number],
1091 1.93.4.1 martin &disks[spare_number]);
1092 1.93.4.1 martin }
1093 1.6 oster
1094 1.6 oster fail:
1095 1.79 mrg rf_lock_mutex2(raidPtr->mutex);
1096 1.93.4.1 martin
1097 1.93.4.1 martin if (ret == 0) {
1098 1.93.4.1 martin raidPtr->numSpare++;
1099 1.93.4.1 martin }
1100 1.93.4.1 martin
1101 1.93.4.1 martin raidPtr->changing_components = 0;
1102 1.93.4.1 martin rf_signal_cond2(raidPtr->changing_components_cv);
1103 1.79 mrg rf_unlock_mutex2(raidPtr->mutex);
1104 1.40 oster
1105 1.6 oster return(ret);
1106 1.6 oster }
1107 1.6 oster
1108 1.6 oster int
1109 1.49 oster rf_remove_hot_spare(RF_Raid_t *raidPtr, RF_SingleComponent_t *sparePtr)
1110 1.6 oster {
1111 1.6 oster int spare_number;
1112 1.93.4.1 martin int i;
1113 1.93.4.1 martin RF_RaidDisk_t *disk;
1114 1.93.4.1 martin struct vnode *vp;
1115 1.93.4.1 martin int ret = EINVAL;
1116 1.6 oster
1117 1.93.4.1 martin spare_number = sparePtr->column - raidPtr->numCol;
1118 1.93.4.1 martin if (spare_number < 0 || spare_number > raidPtr->numSpare)
1119 1.93.4.1 martin return(ret);
1120 1.93.4.1 martin
1121 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1122 1.93.4.1 martin while (raidPtr->changing_components == 1) {
1123 1.93.4.1 martin rf_wait_cond2(raidPtr->changing_components_cv, raidPtr->mutex);
1124 1.6 oster }
1125 1.93.4.1 martin raidPtr->changing_components = 1;
1126 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1127 1.6 oster
1128 1.93.4.1 martin rf_SuspendNewRequestsAndWait(raidPtr);
1129 1.6 oster
1130 1.93.4.1 martin disk = &raidPtr->Disks[raidPtr->numCol + spare_number];
1131 1.93.4.1 martin if (disk->status != rf_ds_spare &&
1132 1.93.4.1 martin disk->status != rf_ds_failed) {
1133 1.93.4.1 martin printf("Spare is in use %d\n", disk->status);
1134 1.93.4.1 martin ret = EBUSY;
1135 1.93.4.1 martin goto out;
1136 1.93.4.1 martin }
1137 1.93.4.1 martin
1138 1.93.4.1 martin vp = raidPtr->raid_cinfo[raidPtr->numCol + spare_number].ci_vp;
1139 1.93.4.1 martin raidPtr->raid_cinfo[raidPtr->numCol + spare_number].ci_vp = NULL;
1140 1.93.4.1 martin raidPtr->raid_cinfo[raidPtr->numCol + spare_number].ci_dev = 0;
1141 1.93.4.1 martin
1142 1.93.4.1 martin /* This component was not automatically configured */
1143 1.93.4.1 martin disk->auto_configured = 0;
1144 1.93.4.1 martin disk->dev = 0;
1145 1.93.4.1 martin disk->numBlocks = 0;
1146 1.93.4.1 martin disk->status = rf_ds_failed;
1147 1.93.4.1 martin snprintf(disk->devname, sizeof(disk->devname),
1148 1.93.4.1 martin "absent_spare%d", spare_number);
1149 1.93.4.1 martin rf_close_component(raidPtr, vp, 0);
1150 1.6 oster
1151 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1152 1.6 oster
1153 1.93.4.1 martin /* at this point we know spare_number is to be pushed all the way to the end of the array... */
1154 1.6 oster
1155 1.93.4.1 martin for (i = raidPtr->numCol + spare_number; i < raidPtr->numCol+raidPtr->numSpare-1; i++) {
1156 1.93.4.1 martin /* now we work our way up the spare array, swaping the current one for the next one */
1157 1.93.4.1 martin rf_swap_components(raidPtr, i, i+1);
1158 1.93.4.1 martin }
1159 1.93.4.1 martin
1160 1.93.4.1 martin raidPtr->numSpare--;
1161 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1162 1.6 oster
1163 1.93.4.1 martin rf_ResumeNewRequests(raidPtr);
1164 1.6 oster
1165 1.93.4.1 martin ret = 0;
1166 1.6 oster
1167 1.93.4.1 martin out:
1168 1.6 oster
1169 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1170 1.93.4.1 martin raidPtr->changing_components = 0;
1171 1.93.4.1 martin rf_signal_cond2(raidPtr->changing_components_cv);
1172 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1173 1.6 oster
1174 1.93.4.1 martin return(ret);
1175 1.93.4.1 martin }
1176 1.6 oster
1177 1.93.4.1 martin /*
1178 1.93.4.1 martin * Delete a non hot spare component
1179 1.93.4.1 martin */
1180 1.25 oster int
1181 1.49 oster rf_delete_component(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
1182 1.25 oster {
1183 1.93.4.1 martin RF_RaidDisk_t *disk;
1184 1.93.4.1 martin RF_RowCol_t col = component->column;
1185 1.93.4.1 martin struct vnode *vp;
1186 1.93.4.1 martin int ret = EINVAL;
1187 1.25 oster
1188 1.93.4.1 martin if (col < 0 || col >= raidPtr->numCol)
1189 1.93.4.1 martin return(ret);
1190 1.93.4.1 martin
1191 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1192 1.93.4.1 martin while (raidPtr->changing_components == 1) {
1193 1.93.4.1 martin rf_wait_cond2(raidPtr->changing_components_cv, raidPtr->mutex);
1194 1.25 oster }
1195 1.93.4.1 martin raidPtr->changing_components = 1;
1196 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1197 1.25 oster
1198 1.93.4.1 martin disk = &raidPtr->Disks[col];
1199 1.25 oster
1200 1.93.4.1 martin /* 1. This component must be marked as failed or spared */
1201 1.93.4.1 martin switch (disk->status) {
1202 1.93.4.1 martin case rf_ds_failed:
1203 1.93.4.1 martin case rf_ds_dist_spared:
1204 1.93.4.1 martin case rf_ds_spared:
1205 1.93.4.1 martin break;
1206 1.93.4.1 martin default:
1207 1.93.4.1 martin ret = EBUSY;
1208 1.93.4.1 martin goto out;
1209 1.93.4.1 martin }
1210 1.25 oster
1211 1.93.4.1 martin vp = raidPtr->raid_cinfo[col].ci_vp;
1212 1.93.4.1 martin raidPtr->raid_cinfo[col].ci_vp = NULL;
1213 1.93.4.1 martin raidPtr->raid_cinfo[col].ci_dev = 0;
1214 1.93.4.1 martin
1215 1.93.4.1 martin /* This component was not automatically configured */
1216 1.93.4.1 martin disk->auto_configured = 0;
1217 1.93.4.1 martin disk->dev = 0;
1218 1.93.4.1 martin disk->numBlocks = 0;
1219 1.93.4.1 martin snprintf(disk->devname, sizeof(disk->devname), "component%d", col);
1220 1.93.4.1 martin
1221 1.93.4.1 martin rf_close_component(raidPtr, vp, 0);
1222 1.93.4.1 martin
1223 1.93.4.1 martin ret = 0;
1224 1.93.4.1 martin out:
1225 1.93.4.1 martin rf_lock_mutex2(raidPtr->mutex);
1226 1.93.4.1 martin raidPtr->changing_components = 0;
1227 1.93.4.1 martin rf_signal_cond2(raidPtr->changing_components_cv);
1228 1.93.4.1 martin rf_unlock_mutex2(raidPtr->mutex);
1229 1.93.4.1 martin
1230 1.93.4.1 martin return(ret);
1231 1.93.4.1 martin }
1232 1.93.4.1 martin
1233 1.93.4.1 martin int
1234 1.93.4.1 martin rf_remove_component(RF_Raid_t *raidPtr, RF_SingleComponent_t *component)
1235 1.93.4.1 martin {
1236 1.93.4.1 martin RF_RowCol_t col = component->column;
1237 1.93.4.1 martin
1238 1.93.4.1 martin if (col < 0 || col >= raidPtr->numCol + raidPtr->numSpare)
1239 1.93.4.1 martin return(EINVAL);
1240 1.93.4.1 martin
1241 1.93.4.1 martin if (col >= raidPtr->numCol)
1242 1.93.4.1 martin return rf_remove_hot_spare(raidPtr, component);
1243 1.93.4.1 martin else
1244 1.93.4.1 martin return rf_delete_component(raidPtr, component);
1245 1.25 oster }
1246 1.25 oster
1247 1.25 oster int
1248 1.64 christos rf_incorporate_hot_spare(RF_Raid_t *raidPtr,
1249 1.64 christos RF_SingleComponent_t *component)
1250 1.25 oster {
1251 1.25 oster
1252 1.57 perry /* Issues here include how to 'move' this in if there is IO
1253 1.25 oster taking place (e.g. component queues and such) */
1254 1.25 oster
1255 1.25 oster return(EINVAL); /* Not implemented yet. */
1256 1.25 oster }
1257 1.93.4.1 martin
1258 1.93.4.1 martin void
1259 1.93.4.1 martin rf_swap_components(RF_Raid_t *raidPtr, int a, int b)
1260 1.93.4.1 martin {
1261 1.93.4.1 martin char tmpdevname[56]; /* 56 is from raidframevar.h */
1262 1.93.4.1 martin RF_ComponentLabel_t tmp_ci_label;
1263 1.93.4.1 martin dev_t tmp_ci_dev, tmp_dev;
1264 1.93.4.1 martin int tmp_status;
1265 1.93.4.1 martin struct vnode *tmp_ci_vp;
1266 1.93.4.1 martin
1267 1.93.4.1 martin
1268 1.93.4.1 martin /* This function *MUST* be called with all IO suspended. */
1269 1.93.4.1 martin RF_ASSERT(raidPtr->accesses_suspended == 0);
1270 1.93.4.1 martin
1271 1.93.4.1 martin /* Swap the component names... */
1272 1.93.4.2 martin snprintf(tmpdevname, sizeof(tmpdevname), "%s", raidPtr->Disks[a].devname);
1273 1.93.4.2 martin snprintf(raidPtr->Disks[a].devname, sizeof(raidPtr->Disks[a].devname), "%s", raidPtr->Disks[b].devname);
1274 1.93.4.2 martin snprintf(raidPtr->Disks[b].devname, sizeof(raidPtr->Disks[b].devname), "%s", tmpdevname);
1275 1.93.4.1 martin
1276 1.93.4.1 martin /* and the vp */
1277 1.93.4.1 martin tmp_ci_vp = raidPtr->raid_cinfo[a].ci_vp;
1278 1.93.4.1 martin raidPtr->raid_cinfo[a].ci_vp = raidPtr->raid_cinfo[b].ci_vp;
1279 1.93.4.1 martin raidPtr->raid_cinfo[b].ci_vp = tmp_ci_vp;
1280 1.93.4.1 martin
1281 1.93.4.1 martin /* and the ci dev */
1282 1.93.4.1 martin tmp_ci_dev = raidPtr->raid_cinfo[a].ci_dev;
1283 1.93.4.1 martin raidPtr->raid_cinfo[a].ci_dev = raidPtr->raid_cinfo[b].ci_dev;
1284 1.93.4.1 martin raidPtr->raid_cinfo[b].ci_dev = tmp_ci_dev;
1285 1.93.4.1 martin
1286 1.93.4.1 martin /* the dev itself */
1287 1.93.4.1 martin tmp_dev = raidPtr->Disks[a].dev;
1288 1.93.4.1 martin raidPtr->Disks[a].dev = raidPtr->Disks[b].dev;
1289 1.93.4.1 martin raidPtr->Disks[b].dev = tmp_dev;
1290 1.93.4.1 martin
1291 1.93.4.1 martin /* the component label */
1292 1.93.4.1 martin tmp_ci_label = raidPtr->raid_cinfo[a].ci_label;
1293 1.93.4.1 martin raidPtr->raid_cinfo[a].ci_label = raidPtr->raid_cinfo[b].ci_label;
1294 1.93.4.1 martin raidPtr->raid_cinfo[b].ci_label = tmp_ci_label;
1295 1.93.4.1 martin
1296 1.93.4.1 martin /* and the status */
1297 1.93.4.1 martin tmp_status = raidPtr->Disks[a].status;
1298 1.93.4.1 martin raidPtr->Disks[a].status = raidPtr->Disks[b].status;
1299 1.93.4.1 martin raidPtr->Disks[b].status = tmp_status;
1300 1.93.4.1 martin }
1301 1.93.4.1 martin
1302