]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Fix assembling of raid10 in the face of missing devices.
[thirdparty/mdadm.git] / Assemble.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31
32 int Assemble(struct supertype *st, char *mddev, int mdfd,
33 mddev_ident_t ident, char *conffile,
34 mddev_dev_t devlist,
35 int readonly, int runstop,
36 char *update,
37 int verbose, int force)
38 {
39 /*
40 * The task of Assemble is to find a collection of
41 * devices that should (according to their superblocks)
42 * form an array, and to give this collection to the MD driver.
43 * In Linux-2.4 and later, this involves submitting a
44 * SET_ARRAY_INFO ioctl with no arg - to prepare
45 * the array - and then submit a number of
46 * ADD_NEW_DISK ioctls to add disks into
47 * the array. Finally RUN_ARRAY might
48 * be submitted to start the array.
49 *
50 * Much of the work of Assemble is in finding and/or
51 * checking the disks to make sure they look right.
52 *
53 * If mddev is not set, then scan must be set and we
54 * read through the config file for dev+uuid mapping
55 * We recurse, setting mddev, for each device that
56 * - isn't running
57 * - has a valid uuid (or any uuid if !uuidset)
58 *
59 * If mddev is set, we try to determine state of md.
60 * check version - must be at least 0.90.0
61 * check kernel version. must be at least 2.4.
62 * If not, we can possibly fall back on START_ARRAY
63 * Try to GET_ARRAY_INFO.
64 * If possible, give up
65 * If not, try to STOP_ARRAY just to make sure
66 *
67 * If !uuidset and scan, look in conf-file for uuid
68 * If not found, give up
69 * If !devlist and scan and uuidset, get list of devs from conf-file
70 *
71 * For each device:
72 * Check superblock - discard if bad
73 * Check uuid (set if we don't have one) - discard if no match
74 * Check superblock similarity if we have a superblock - discard if different
75 * Record events, devicenum
76 * This should give us a list of devices for the array
77 * We should collect the most recent event number
78 *
79 * Count disks with recent enough event count
80 * While force && !enough disks
81 * Choose newest rejected disks, update event count
82 * mark clean and rewrite superblock
83 * If recent kernel:
84 * SET_ARRAY_INFO
85 * foreach device with recent events : ADD_NEW_DISK
86 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
87 * If old kernel:
88 * Check the device numbers in superblock are right
89 * update superblock if any changes
90 * START_ARRAY
91 *
92 */
93 int old_linux = 0;
94 int vers;
95 void *first_super = NULL, *super = NULL;
96 struct {
97 char *devname;
98 unsigned int major, minor;
99 unsigned int oldmajor, oldminor;
100 long long events;
101 int uptodate;
102 int state;
103 int raid_disk;
104 int disk_nr;
105 } *devices;
106 int *best = NULL; /* indexed by raid_disk */
107 unsigned int bestcnt = 0;
108 int devcnt = 0;
109 unsigned int okcnt, sparecnt;
110 unsigned int req_cnt;
111 unsigned int i;
112 int most_recent = 0;
113 int chosen_drive;
114 int change = 0;
115 int inargv = 0;
116 int start_partial_ok = force || devlist==NULL;
117 unsigned int num_devs;
118 mddev_dev_t tmpdev;
119 struct mdinfo info;
120 struct mddev_ident_s ident2;
121 char *avail;
122
123 vers = md_get_version(mdfd);
124 if (vers <= 0) {
125 fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
126 return 1;
127 }
128 if (vers < 9000) {
129 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
130 " Upgrade your kernel or try --build\n");
131 return 1;
132 }
133 if (get_linux_version() < 2004000)
134 old_linux = 1;
135
136 if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
137 fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
138 mddev);
139 return 1;
140 }
141 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
142
143 /*
144 * If any subdevs are listed, then any that don't
145 * match ident are discarded. Remainder must all match and
146 * become the array.
147 * If no subdevs, then we scan all devices in the config file, but
148 * there must be something in the identity
149 */
150
151 if (!devlist &&
152 ident->uuid_set == 0 &&
153 ident->super_minor < 0 &&
154 ident->devices == NULL) {
155 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
156 mddev);
157 return 1;
158 }
159 if (devlist == NULL)
160 devlist = conf_get_devs(conffile);
161 else inargv = 1;
162
163 tmpdev = devlist; num_devs = 0;
164 while (tmpdev) {
165 num_devs++;
166 tmpdev = tmpdev->next;
167 }
168 devices = malloc(num_devs * sizeof(*devices));
169
170 if (!st && ident->st) st = ident->st;
171
172 if (verbose>0)
173 fprintf(stderr, Name ": looking for devices for %s\n",
174 mddev);
175
176 while ( devlist) {
177 char *devname;
178 int dfd;
179 struct stat stb;
180 struct supertype *tst = st;
181
182 devname = devlist->devname;
183 devlist = devlist->next;
184
185 if (ident->devices &&
186 !match_oneof(ident->devices, devname)) {
187 if ((inargv && verbose>=0) || verbose > 0)
188 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
189 continue;
190 }
191
192 if (super) {
193 free(super);
194 super = NULL;
195 }
196
197 dfd = open(devname, O_RDONLY|O_EXCL, 0);
198 if (dfd < 0) {
199 if ((inargv && verbose >= 0) || verbose > 0)
200 fprintf(stderr, Name ": cannot open device %s: %s\n",
201 devname, strerror(errno));
202 } else if (fstat(dfd, &stb)< 0) {
203 /* Impossible! */
204 fprintf(stderr, Name ": fstat failed for %s: %s\n",
205 devname, strerror(errno));
206 close(dfd);
207 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
208 fprintf(stderr, Name ": %s is not a block device.\n",
209 devname);
210 close(dfd);
211 } else if (!tst && (tst = guess_super(dfd)) == NULL) {
212 if ((inargv && verbose >= 0) || verbose > 0)
213 fprintf(stderr, Name ": no recogniseable superblock\n");
214 } else if (tst->ss->load_super(tst,dfd, &super, NULL)) {
215 if ((inargv && verbose >= 0) || verbose > 0)
216 fprintf( stderr, Name ": no RAID superblock on %s\n",
217 devname);
218 close(dfd);
219 } else {
220 tst->ss->getinfo_super(&info, &ident2, super);
221 close(dfd);
222 }
223
224 if (ident->uuid_set &&
225 (!super || same_uuid(info.uuid, ident->uuid, tst->ss->swapuuid)==0)) {
226 if ((inargv && verbose >= 0) || verbose > 0)
227 fprintf(stderr, Name ": %s has wrong uuid.\n",
228 devname);
229 continue;
230 }
231 if (ident->name[0] &&
232 (!super || strncmp(ident2.name, ident->name, 32)!=0)) {
233 if ((inargv && verbose >= 0) || verbose > 0)
234 fprintf(stderr, Name ": %s has wrong name.\n",
235 devname);
236 continue;
237 }
238 if (ident->super_minor != UnSet &&
239 (!super || ident->super_minor != info.array.md_minor)) {
240 if ((inargv && verbose >= 0) || verbose > 0)
241 fprintf(stderr, Name ": %s has wrong super-minor.\n",
242 devname);
243 continue;
244 }
245 if (ident->level != UnSet &&
246 (!super|| ident->level != info.array.level)) {
247 if ((inargv && verbose >= 0) || verbose > 0)
248 fprintf(stderr, Name ": %s has wrong raid level.\n",
249 devname);
250 continue;
251 }
252 if (ident->raid_disks != UnSet &&
253 (!super || ident->raid_disks!= info.array.raid_disks)) {
254 if ((inargv && verbose >= 0) || verbose > 0)
255 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
256 devname);
257 continue;
258 }
259
260 /* If we are this far, then we are commited to this device.
261 * If the super_block doesn't exist, or doesn't match others,
262 * then we cannot continue
263 */
264
265 if (!super) {
266 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
267 devname);
268 free(first_super);
269 return 1;
270 }
271 st = tst; /* commit to this format, if haven't already */
272 if (st->ss->compare_super(&first_super, super)) {
273 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
274 devname);
275 free(super);
276 free(first_super);
277 return 1;
278 }
279
280 /* looks like a good enough match to update the super block if needed */
281 if (update) {
282 /* prepare useful information in info structures */
283 struct stat stb2;
284 fstat(mdfd, &stb2);
285 info.array.md_minor = minor(stb2.st_rdev);
286
287 st->ss->update_super(&info, super, update, devname, verbose);
288
289 dfd = open(devname, O_RDWR|O_EXCL, 0);
290 if (dfd < 0)
291 fprintf(stderr, Name ": Cannot open %s for superblock update\n",
292 devname);
293 else if (st->ss->store_super(st, dfd, super))
294 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
295 devname);
296 if (dfd >= 0)
297 close(dfd);
298 }
299
300 if (verbose > 0)
301 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
302 devname, mddev, info.disk.raid_disk);
303 devices[devcnt].devname = devname;
304 devices[devcnt].major = major(stb.st_rdev);
305 devices[devcnt].minor = minor(stb.st_rdev);
306 devices[devcnt].oldmajor = info.disk.major;
307 devices[devcnt].oldminor = info.disk.minor;
308 devices[devcnt].events = info.events;
309 devices[devcnt].raid_disk = info.disk.raid_disk;
310 devices[devcnt].disk_nr = info.disk.number;
311 devices[devcnt].uptodate = 0;
312 devices[devcnt].state = info.disk.state;
313 if (most_recent < devcnt) {
314 if (devices[devcnt].events
315 > devices[most_recent].events)
316 most_recent = devcnt;
317 }
318 if (info.array.level == -4)
319 /* with multipath, the raid_disk from the superblock is meaningless */
320 i = devcnt;
321 else
322 i = devices[devcnt].raid_disk;
323 if (i < 10000) {
324 if (i >= bestcnt) {
325 unsigned int newbestcnt = i+10;
326 int *newbest = malloc(sizeof(int)*newbestcnt);
327 unsigned int c;
328 for (c=0; c < newbestcnt; c++)
329 if (c < bestcnt)
330 newbest[c] = best[c];
331 else
332 newbest[c] = -1;
333 if (best)free(best);
334 best = newbest;
335 bestcnt = newbestcnt;
336 }
337 if (best[i] == -1
338 || devices[best[i]].events < devices[devcnt].events)
339 best[i] = devcnt;
340 }
341 devcnt++;
342 }
343
344 if (super)
345 free(super);
346 super = NULL;
347
348 if (update && strcmp(update, "byteorder")==0)
349 st->minor_version = 90;
350
351 if (devcnt == 0) {
352 fprintf(stderr, Name ": no devices found for %s\n",
353 mddev);
354 free(first_super);
355 return 1;
356 }
357
358 st->ss->getinfo_super(&info, &ident2, first_super);
359
360 /* now we have some devices that might be suitable.
361 * I wonder how many
362 */
363 avail = malloc(info.array.raid_disks);
364 memset(avail, 0, info.array.raid_disks);
365 okcnt = 0;
366 sparecnt=0;
367 for (i=0; i< bestcnt ;i++) {
368 int j = best[i];
369 int event_margin = !force;
370 if (j < 0) continue;
371 /* note: we ignore error flags in multipath arrays
372 * as they don't make sense
373 */
374 if (info.array.level != -4)
375 if (!(devices[j].state & (1<<MD_DISK_SYNC))) {
376 if (!(devices[j].state & (1<<MD_DISK_FAULTY)))
377 sparecnt++;
378 continue;
379 }
380 if (devices[j].events+event_margin >=
381 devices[most_recent].events) {
382 devices[j].uptodate = 1;
383 if (i < info.array.raid_disks) {
384 okcnt++;
385 avail[i]=1;
386 } else
387 sparecnt++;
388 }
389 }
390 while (force && !enough(info.array.level, info.array.raid_disks,
391 info.array.layout,
392 avail, okcnt)) {
393 /* Choose the newest best drive which is
394 * not up-to-date, update the superblock
395 * and add it.
396 */
397 int fd;
398 chosen_drive = -1;
399 for (i=0; i<info.array.raid_disks && i < bestcnt; i++) {
400 int j = best[i];
401 if (j>=0 &&
402 !devices[j].uptodate &&
403 devices[j].events > 0 &&
404 (chosen_drive < 0 ||
405 devices[j].events > devices[chosen_drive].events))
406 chosen_drive = j;
407 }
408 if (chosen_drive < 0)
409 break;
410 if (verbose >= 0)
411 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
412 devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
413 (int)(devices[chosen_drive].events),
414 (int)(devices[most_recent].events));
415 fd = open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
416 if (fd < 0) {
417 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
418 devices[chosen_drive].devname);
419 devices[chosen_drive].events = 0;
420 continue;
421 }
422 if (st->ss->load_super(st,fd, &super, NULL)) {
423 close(fd);
424 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
425 devices[chosen_drive].devname);
426 devices[chosen_drive].events = 0;
427 continue;
428 }
429 info.events = devices[most_recent].events;
430 st->ss->update_super(&info, super, "force", devices[chosen_drive].devname, verbose);
431
432 if (st->ss->store_super(st, fd, super)) {
433 close(fd);
434 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
435 devices[chosen_drive].devname);
436 devices[chosen_drive].events = 0;
437 free(super);
438 continue;
439 }
440 close(fd);
441 devices[chosen_drive].events = devices[most_recent].events;
442 devices[chosen_drive].uptodate = 1;
443 avail[chosen_drive] = 1;
444 okcnt++;
445 free(super);
446 }
447
448 /* Now we want to look at the superblock which the kernel will base things on
449 * and compare the devices that we think are working with the devices that the
450 * superblock thinks are working.
451 * If there are differences and --force is given, then update this chosen
452 * superblock.
453 */
454 chosen_drive = -1;
455 super = NULL;
456 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
457 int j = best[i];
458 int fd;
459
460 if (j<0)
461 continue;
462 if (!devices[j].uptodate)
463 continue;
464 chosen_drive = j;
465 if ((fd=open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
466 fprintf(stderr, Name ": Cannot open %s: %s\n",
467 devices[j].devname, strerror(errno));
468 return 1;
469 }
470 if (st->ss->load_super(st,fd, &super, NULL)) {
471 close(fd);
472 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
473 devices[j].devname);
474 return 1;
475 }
476 close(fd);
477 }
478 if (super == NULL) {
479 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
480 return 1;
481 }
482 st->ss->getinfo_super(&info, &ident2, super);
483 for (i=0; i<bestcnt; i++) {
484 int j = best[i];
485 unsigned int desired_state;
486
487 if (i < info.array.raid_disks)
488 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
489 else
490 desired_state = 0;
491
492 if (j<0)
493 continue;
494 if (!devices[j].uptodate)
495 continue;
496 info.disk.number = devices[j].disk_nr;
497 info.disk.raid_disk = i;
498 info.disk.state = desired_state;
499
500 if (devices[j].uptodate &&
501 st->ss->update_super(&info, super, "assemble", NULL, verbose)) {
502 if (force) {
503 if (verbose >= 0)
504 fprintf(stderr, Name ": "
505 "clearing FAULTY flag for device %d in %s for %s\n",
506 j, mddev, devices[j].devname);
507 change = 1;
508 } else {
509 if (verbose >= -1)
510 fprintf(stderr, Name ": "
511 "device %d in %s has wrong state in superblock, but %s seems ok\n",
512 i, mddev, devices[j].devname);
513 }
514 }
515 #if 0
516 if (!devices[j].uptodate &&
517 !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
518 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
519 i, mddev);
520 }
521 #endif
522 }
523 if (force && okcnt == info.array.raid_disks-1) {
524 /* FIXME check event count */
525 change += st->ss->update_super(&info, super, "force",
526 devices[chosen_drive].devname, verbose);
527 }
528
529 if (change) {
530 int fd;
531 fd = open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
532 if (fd < 0) {
533 fprintf(stderr, Name ": Could open %s for write - cannot Assemble array.\n",
534 devices[chosen_drive].devname);
535 return 1;
536 }
537 if (st->ss->store_super(st, fd, super)) {
538 close(fd);
539 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
540 devices[chosen_drive].devname);
541 return 1;
542 }
543 close(fd);
544 }
545
546 /* count number of in-sync devices according to the superblock.
547 * We must have this number to start the array without -s or -R
548 */
549 req_cnt = info.array.working_disks;
550
551 /* Almost ready to actually *do* something */
552 if (!old_linux) {
553 int rv;
554 if ((vers % 100) >= 1) { /* can use different versions */
555 mdu_array_info_t inf;
556 memset(&inf, 0, sizeof(inf));
557 inf.major_version = st->ss->major;
558 inf.minor_version = st->minor_version;
559 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
560 } else
561 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
562
563 if (rv) {
564 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
565 mddev, strerror(errno));
566 return 1;
567 }
568 if (ident->bitmap_fd >= 0) {
569 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
570 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
571 return 1;
572 }
573 }
574
575 /* First, add the raid disks, but add the chosen one last */
576 for (i=0; i<= bestcnt; i++) {
577 int j;
578 if (i < bestcnt) {
579 j = best[i];
580 if (j == chosen_drive)
581 continue;
582 } else
583 j = chosen_drive;
584
585 if (j >= 0 /* && devices[j].uptodate */) {
586 mdu_disk_info_t disk;
587 memset(&disk, 0, sizeof(disk));
588 disk.major = devices[j].major;
589 disk.minor = devices[j].minor;
590 if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) {
591 fprintf(stderr, Name ": failed to add %s to %s: %s\n",
592 devices[j].devname,
593 mddev,
594 strerror(errno));
595 if (i < info.array.raid_disks || i == bestcnt)
596 okcnt--;
597 else
598 sparecnt--;
599 } else if (verbose > 0)
600 fprintf(stderr, Name ": added %s to %s as %d\n",
601 devices[j].devname, mddev, devices[j].raid_disk);
602 } else if (verbose > 0 && i < info.array.raid_disks)
603 fprintf(stderr, Name ": no uptodate device for slot %d of %s\n",
604 i, mddev);
605 }
606
607 if (runstop == 1 ||
608 (runstop == 0 &&
609 ( enough(info.array.level, info.array.raid_disks, info.array.layout, avail, okcnt) &&
610 (okcnt >= req_cnt || start_partial_ok)
611 ))) {
612 if (ioctl(mdfd, RUN_ARRAY, NULL)==0) {
613 if (verbose >= 0) {
614 fprintf(stderr, Name ": %s has been started with %d drive%s",
615 mddev, okcnt, okcnt==1?"":"s");
616 if (okcnt < info.array.raid_disks)
617 fprintf(stderr, " (out of %d)", info.array.raid_disks);
618 if (sparecnt)
619 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
620 fprintf(stderr, ".\n");
621 }
622 return 0;
623 }
624 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
625 mddev, strerror(errno));
626 return 1;
627 }
628 if (runstop == -1) {
629 fprintf(stderr, Name ": %s assembled from %d drive%s, but not started.\n",
630 mddev, okcnt, okcnt==1?"":"s");
631 return 0;
632 }
633 if (verbose >= 0) {
634 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
635 if (sparecnt)
636 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
637 if (!enough(info.array.level, info.array.raid_disks, info.array.layout, avail, okcnt))
638 fprintf(stderr, " - not enough to start the array.\n");
639 else {
640 if (req_cnt == info.array.raid_disks)
641 fprintf(stderr, " - need all %d to start it", req_cnt);
642 else
643 fprintf(stderr, " - need %d of %d to start", req_cnt, info.array.raid_disks);
644 fprintf(stderr, " (use --run to insist).\n");
645 }
646 }
647 return 1;
648 } else {
649 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
650 * been updated to point to the current locations of devices.
651 * so we can just start the array
652 */
653 unsigned long dev;
654 dev = makedev(devices[chosen_drive].major,
655 devices[chosen_drive].minor);
656 if (ioctl(mdfd, START_ARRAY, dev)) {
657 fprintf(stderr, Name ": Cannot start array: %s\n",
658 strerror(errno));
659 }
660
661 }
662 return 0;
663 }