]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Fix compare_super to take supertype instead of a superblock.
[thirdparty/mdadm.git] / Assemble.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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 #include <ctype.h>
32
33 static int name_matches(char *found, char *required, char *homehost)
34 {
35 /* See if the name found matches the required name, possibly
36 * prefixed with 'homehost'
37 */
38 char fnd[33];
39
40 strncpy(fnd, found, 32);
41 fnd[32] = 0;
42 if (strcmp(found, required)==0)
43 return 1;
44 if (homehost) {
45 int l = strlen(homehost);
46 if (l < 32 && fnd[l] == ':' &&
47 strcmp(fnd+l+1, required)==0)
48 return 1;
49 }
50 return 0;
51 }
52
53 int Assemble(struct supertype *st, char *mddev, int mdfd,
54 mddev_ident_t ident,
55 mddev_dev_t devlist, char *backup_file,
56 int readonly, int runstop,
57 char *update, char *homehost,
58 int verbose, int force)
59 {
60 /*
61 * The task of Assemble is to find a collection of
62 * devices that should (according to their superblocks)
63 * form an array, and to give this collection to the MD driver.
64 * In Linux-2.4 and later, this involves submitting a
65 * SET_ARRAY_INFO ioctl with no arg - to prepare
66 * the array - and then submit a number of
67 * ADD_NEW_DISK ioctls to add disks into
68 * the array. Finally RUN_ARRAY might
69 * be submitted to start the array.
70 *
71 * Much of the work of Assemble is in finding and/or
72 * checking the disks to make sure they look right.
73 *
74 * If mddev is not set, then scan must be set and we
75 * read through the config file for dev+uuid mapping
76 * We recurse, setting mddev, for each device that
77 * - isn't running
78 * - has a valid uuid (or any uuid if !uuidset)
79 *
80 * If mddev is set, we try to determine state of md.
81 * check version - must be at least 0.90.0
82 * check kernel version. must be at least 2.4.
83 * If not, we can possibly fall back on START_ARRAY
84 * Try to GET_ARRAY_INFO.
85 * If possible, give up
86 * If not, try to STOP_ARRAY just to make sure
87 *
88 * If !uuidset and scan, look in conf-file for uuid
89 * If not found, give up
90 * If !devlist and scan and uuidset, get list of devs from conf-file
91 *
92 * For each device:
93 * Check superblock - discard if bad
94 * Check uuid (set if we don't have one) - discard if no match
95 * Check superblock similarity if we have a superblock - discard if different
96 * Record events, devicenum
97 * This should give us a list of devices for the array
98 * We should collect the most recent event number
99 *
100 * Count disks with recent enough event count
101 * While force && !enough disks
102 * Choose newest rejected disks, update event count
103 * mark clean and rewrite superblock
104 * If recent kernel:
105 * SET_ARRAY_INFO
106 * foreach device with recent events : ADD_NEW_DISK
107 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
108 * If old kernel:
109 * Check the device numbers in superblock are right
110 * update superblock if any changes
111 * START_ARRAY
112 *
113 */
114 int clean = 0;
115 int must_close = 0;
116 int old_linux = 0;
117 int vers = 0; /* Keep gcc quite - it really is initialised */
118 void *first_super = NULL, *super = NULL;
119 struct {
120 char *devname;
121 unsigned int major, minor;
122 unsigned int oldmajor, oldminor;
123 long long events;
124 int uptodate;
125 int state;
126 int raid_disk;
127 int disk_nr;
128 } *devices;
129 int *best = NULL; /* indexed by raid_disk */
130 unsigned int bestcnt = 0;
131 int devcnt = 0;
132 unsigned int okcnt, sparecnt;
133 unsigned int req_cnt;
134 unsigned int i;
135 int most_recent = 0;
136 int chosen_drive;
137 int change = 0;
138 int inargv = 0;
139 int bitmap_done;
140 int start_partial_ok = (runstop >= 0) && (force || devlist==NULL || mdfd < 0);
141 unsigned int num_devs;
142 mddev_dev_t tmpdev;
143 struct mdinfo info;
144 char *avail;
145 int nextspare = 0;
146
147 if (get_linux_version() < 2004000)
148 old_linux = 1;
149
150 if (mdfd >= 0) {
151 vers = md_get_version(mdfd);
152 if (vers <= 0) {
153 fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
154 return 1;
155 }
156 if (vers < 9000) {
157 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
158 " Upgrade your kernel or try --build\n");
159 return 1;
160 }
161
162 if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
163 fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
164 mddev);
165 return 1;
166 }
167 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
168 }
169 /*
170 * If any subdevs are listed, then any that don't
171 * match ident are discarded. Remainder must all match and
172 * become the array.
173 * If no subdevs, then we scan all devices in the config file, but
174 * there must be something in the identity
175 */
176
177 if (!devlist &&
178 ident->uuid_set == 0 &&
179 ident->super_minor < 0 &&
180 ident->devices == NULL) {
181 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
182 mddev ? mddev : "further assembly");
183 return 1;
184 }
185 if (devlist == NULL)
186 devlist = conf_get_devs();
187 else if (mdfd >= 0)
188 inargv = 1;
189
190 try_again:
191
192 tmpdev = devlist; num_devs = 0;
193 while (tmpdev) {
194 if (tmpdev->used)
195 tmpdev->used = 2;
196 else
197 num_devs++;
198 tmpdev = tmpdev->next;
199 }
200 devices = malloc(num_devs * sizeof(*devices));
201
202 if (!st && ident->st) st = ident->st;
203
204 if (verbose>0)
205 fprintf(stderr, Name ": looking for devices for %s\n",
206 mddev ? mddev : "further assembly");
207
208 /* first walk the list of devices to find a consistent set
209 * that match the criterea, if that is possible.
210 * We flag the one we like with 'used'.
211 */
212 for (tmpdev = devlist;
213 tmpdev;
214 tmpdev = tmpdev->next) {
215 char *devname = tmpdev->devname;
216 int dfd;
217 struct stat stb;
218 struct supertype *tst = st;
219
220 if (tmpdev->used > 1) continue;
221
222 if (ident->devices &&
223 !match_oneof(ident->devices, devname)) {
224 if ((inargv && verbose>=0) || verbose > 0)
225 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
226 continue;
227 }
228
229 dfd = dev_open(devname, O_RDONLY|O_EXCL);
230 if (dfd < 0) {
231 if ((inargv && verbose >= 0) || verbose > 0)
232 fprintf(stderr, Name ": cannot open device %s: %s\n",
233 devname, strerror(errno));
234 tmpdev->used = 2;
235 } else if (fstat(dfd, &stb)< 0) {
236 /* Impossible! */
237 fprintf(stderr, Name ": fstat failed for %s: %s\n",
238 devname, strerror(errno));
239 tmpdev->used = 2;
240 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
241 fprintf(stderr, Name ": %s is not a block device.\n",
242 devname);
243 tmpdev->used = 2;
244 } else if (!tst && (tst = guess_super(dfd)) == NULL) {
245 if ((inargv && verbose >= 0) || verbose > 0)
246 fprintf(stderr, Name ": no recogniseable superblock on %s\n",
247 devname);
248 tmpdev->used = 2;
249 } else if (tst->ss->load_super(tst,dfd, &super, NULL)) {
250 if ((inargv && verbose >= 0) || verbose > 0)
251 fprintf( stderr, Name ": no RAID superblock on %s\n",
252 devname);
253 } else {
254 tst->ss->getinfo_super(tst, &info, super);
255 }
256 if (dfd >= 0) close(dfd);
257
258 if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
259 (!super || same_uuid(info.uuid, ident->uuid, tst->ss->swapuuid)==0)) {
260 if ((inargv && verbose >= 0) || verbose > 0)
261 fprintf(stderr, Name ": %s has wrong uuid.\n",
262 devname);
263 goto loop;
264 }
265 if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
266 (!super || name_matches(info.name, ident->name, homehost)==0)) {
267 if ((inargv && verbose >= 0) || verbose > 0)
268 fprintf(stderr, Name ": %s has wrong name.\n",
269 devname);
270 goto loop;
271 }
272 if (ident->super_minor != UnSet &&
273 (!super || ident->super_minor != info.array.md_minor)) {
274 if ((inargv && verbose >= 0) || verbose > 0)
275 fprintf(stderr, Name ": %s has wrong super-minor.\n",
276 devname);
277 goto loop;
278 }
279 if (ident->level != UnSet &&
280 (!super|| ident->level != info.array.level)) {
281 if ((inargv && verbose >= 0) || verbose > 0)
282 fprintf(stderr, Name ": %s has wrong raid level.\n",
283 devname);
284 goto loop;
285 }
286 if (ident->raid_disks != UnSet &&
287 (!super || ident->raid_disks!= info.array.raid_disks)) {
288 if ((inargv && verbose >= 0) || verbose > 0)
289 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
290 devname);
291 goto loop;
292 }
293 if (mdfd < 0) {
294 if (tst == NULL || super == NULL)
295 continue;
296 if (update == NULL &&
297 tst->ss->match_home(tst, super, homehost)==0) {
298 if ((inargv && verbose >= 0) || verbose > 0)
299 fprintf(stderr, Name ": %s is not built for host %s.\n",
300 devname, homehost);
301 /* Auto-assemble, and this is not a usable host */
302 /* if update != NULL, we are updating the host
303 * name... */
304 goto loop;
305 }
306 }
307 /* If we are this far, then we are nearly commited to this device.
308 * If the super_block doesn't exist, or doesn't match others,
309 * then we probably cannot continue
310 * However if one of the arrays is for the homehost, and
311 * the other isn't that can disambiguate.
312 */
313
314 if (!super) {
315 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
316 devname);
317 st->ss->free_super(st, first_super);
318 return 1;
319 }
320
321 if (st == NULL)
322 st = tst;
323 if (st->ss != tst->ss ||
324 st->minor_version != tst->minor_version ||
325 st->ss->compare_super(st, tst) != 0) {
326 /* Some mismatch. If exactly one array matches this host,
327 * we can resolve on that one.
328 * Or, if we are auto assembling, we just ignore the second
329 * for now.
330 */
331 if (mdfd < 0)
332 goto loop;
333 if (homehost) {
334 int first = st->ss->match_home(st, first_super,
335 homehost);
336 int last = tst->ss->match_home(tst, super,
337 homehost);
338 if (first+last == 1) {
339 /* We can do something */
340 if (first) {/* just ignore this one */
341 if ((inargv && verbose >= 0) || verbose > 0)
342 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
343 devname);
344 goto loop;
345 } else { /* reject all those sofar */
346 mddev_dev_t td;
347 if ((inargv && verbose >= 0) || verbose > 0)
348 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
349 devname);
350 for (td=devlist; td != tmpdev; td=td->next)
351 if (td->used == 1)
352 td->used = 0;
353 tmpdev->used = 1;
354 goto loop;
355 }
356 }
357 }
358 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
359 devname);
360 tst->ss->free_super(tst, super);
361 st->ss->free_super(st, first_super);
362 return 1;
363 }
364
365 tmpdev->used = 1;
366
367 loop:
368 if (super)
369 tst->ss->free_super(tst, super);
370 super = NULL;
371 }
372
373 if (mdfd < 0) {
374 /* So... it is up to me to open the device.
375 * We create a name '/dev/md/XXX' based on the info in the
376 * superblock, and call open_mddev on that
377 */
378 mdu_array_info_t inf;
379 char *c;
380 if (!first_super) {
381 return 2;
382 }
383 st->ss->getinfo_super(st, &info, first_super);
384 c = strchr(info.name, ':');
385 if (c) c++; else c= info.name;
386 if (isdigit(*c) && ((ident->autof & 7)==4 || (ident->autof&7)==6))
387 /* /dev/md/d0 style for partitionable */
388 asprintf(&mddev, "/dev/md/d%s", c);
389 else
390 asprintf(&mddev, "/dev/md/%s", c);
391 mdfd = open_mddev(mddev, ident->autof);
392 if (mdfd < 0) {
393 st->ss->free_super(st, first_super);
394 free(devices);
395 first_super = NULL;
396 goto try_again;
397 }
398 vers = md_get_version(mdfd);
399 if (ioctl(mdfd, GET_ARRAY_INFO, &inf)==0) {
400 for (tmpdev = devlist ;
401 tmpdev && tmpdev->used != 1;
402 tmpdev = tmpdev->next)
403 ;
404 fprintf(stderr, Name ": %s already active, cannot restart it!\n", mddev);
405 if (tmpdev)
406 fprintf(stderr, Name ": %s needed for %s...\n",
407 mddev, tmpdev->devname);
408 close(mdfd);
409 mdfd = -1;
410 st->ss->free_super(st, first_super);
411 free(devices);
412 first_super = NULL;
413 goto try_again;
414 }
415 must_close = 1;
416 }
417
418 /* Ok, no bad inconsistancy, we can try updating etc */
419 bitmap_done = 0;
420 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
421 char *devname = tmpdev->devname;
422 struct stat stb;
423 /* looks like a good enough match to update the super block if needed */
424 #ifndef MDASSEMBLE
425 if (update) {
426 int dfd;
427 /* prepare useful information in info structures */
428 struct stat stb2;
429 fstat(mdfd, &stb2);
430
431 if (strcmp(update, "uuid")==0 &&
432 !ident->uuid_set) {
433 int rfd;
434 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
435 read(rfd, ident->uuid, 16) != 16) {
436 *(__u32*)(ident->uuid) = random();
437 *(__u32*)(ident->uuid+1) = random();
438 *(__u32*)(ident->uuid+2) = random();
439 *(__u32*)(ident->uuid+3) = random();
440 }
441 if (rfd >= 0) close(rfd);
442 }
443 dfd = dev_open(devname, O_RDWR|O_EXCL);
444
445 remove_partitions(dfd);
446
447 st->ss->load_super(st, dfd, &super, NULL);
448 st->ss->getinfo_super(st, &info, super);
449
450 memcpy(info.uuid, ident->uuid, 16);
451 strcpy(info.name, ident->name);
452 info.array.md_minor = minor(stb2.st_rdev);
453
454 st->ss->update_super(st, &info, super, update,
455 devname, verbose,
456 ident->uuid_set, homehost);
457 if (strcmp(update, "uuid")==0 &&
458 !ident->uuid_set) {
459 ident->uuid_set = 1;
460 memcpy(ident->uuid, info.uuid, 16);
461 }
462 if (dfd < 0)
463 fprintf(stderr, Name ": Cannot open %s for superblock update\n",
464 devname);
465 else if (st->ss->store_super(st, dfd, super))
466 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
467 devname);
468 if (dfd >= 0)
469 close(dfd);
470
471 if (strcmp(update, "uuid")==0 &&
472 ident->bitmap_fd >= 0 && !bitmap_done) {
473 if (bitmap_update_uuid(ident->bitmap_fd, info.uuid, st->ss->swapuuid) != 0)
474 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
475 else
476 bitmap_done = 1;
477 }
478 } else
479 #endif
480 {
481 int dfd;
482 dfd = dev_open(devname, O_RDWR|O_EXCL);
483
484 remove_partitions(dfd);
485
486 st->ss->load_super(st, dfd, &super, NULL);
487 st->ss->getinfo_super(st, &info, super);
488 close(dfd);
489 }
490
491 stat(devname, &stb);
492
493 if (verbose > 0)
494 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
495 devname, mddev, info.disk.raid_disk);
496 devices[devcnt].devname = devname;
497 devices[devcnt].major = major(stb.st_rdev);
498 devices[devcnt].minor = minor(stb.st_rdev);
499 devices[devcnt].oldmajor = info.disk.major;
500 devices[devcnt].oldminor = info.disk.minor;
501 devices[devcnt].events = info.events;
502 devices[devcnt].raid_disk = info.disk.raid_disk;
503 devices[devcnt].disk_nr = info.disk.number;
504 devices[devcnt].uptodate = 0;
505 devices[devcnt].state = info.disk.state;
506 if (most_recent < devcnt) {
507 if (devices[devcnt].events
508 > devices[most_recent].events)
509 most_recent = devcnt;
510 }
511 if (info.array.level == -4)
512 /* with multipath, the raid_disk from the superblock is meaningless */
513 i = devcnt;
514 else
515 i = devices[devcnt].raid_disk;
516 if (i+1 == 0) {
517 if (nextspare < info.array.raid_disks)
518 nextspare = info.array.raid_disks;
519 i = nextspare++;
520 } else {
521 if (i >= info.array.raid_disks &&
522 i >= nextspare)
523 nextspare = i+1;
524 }
525 if (i < 10000) {
526 if (i >= bestcnt) {
527 unsigned int newbestcnt = i+10;
528 int *newbest = malloc(sizeof(int)*newbestcnt);
529 unsigned int c;
530 for (c=0; c < newbestcnt; c++)
531 if (c < bestcnt)
532 newbest[c] = best[c];
533 else
534 newbest[c] = -1;
535 if (best)free(best);
536 best = newbest;
537 bestcnt = newbestcnt;
538 }
539 if (best[i] >=0 &&
540 devices[best[i]].events == devices[devcnt].events &&
541 devices[best[i]].minor != devices[devcnt].minor &&
542 st->ss->major == 0 &&
543 info.array.level != -4) {
544 /* two different devices with identical superblock.
545 * Could be a mis-detection caused by overlapping
546 * partitions. fail-safe.
547 */
548 fprintf(stderr, Name ": WARNING %s and %s appear"
549 " to have very similar superblocks.\n"
550 " If they are really different, "
551 "please --zero the superblock on one\n"
552 " If they are the same or overlap,"
553 " please remove one from %s.\n",
554 devices[best[i]].devname, devname,
555 inargv ? "the list" :
556 "the\n DEVICE list in mdadm.conf"
557 );
558 if (must_close) close(mdfd);
559 return 1;
560 }
561 if (best[i] == -1
562 || devices[best[i]].events < devices[devcnt].events)
563 best[i] = devcnt;
564 }
565 devcnt++;
566
567 if (super)
568 st->ss->free_super(st, super);
569 super = NULL;
570 }
571
572 if (update && strcmp(update, "byteorder")==0)
573 st->minor_version = 90;
574
575 if (devcnt == 0) {
576 fprintf(stderr, Name ": no devices found for %s\n",
577 mddev);
578 st->ss->free_super(st, first_super);
579 if (must_close) close(mdfd);
580 return 1;
581 }
582
583 st->ss->getinfo_super(st, &info, first_super);
584 clean = info.array.state & 1;
585
586 /* now we have some devices that might be suitable.
587 * I wonder how many
588 */
589 avail = malloc(info.array.raid_disks);
590 memset(avail, 0, info.array.raid_disks);
591 okcnt = 0;
592 sparecnt=0;
593 for (i=0; i< bestcnt ;i++) {
594 int j = best[i];
595 int event_margin = 1; /* always allow a difference of '1'
596 * like the kernel does
597 */
598 if (j < 0) continue;
599 /* note: we ignore error flags in multipath arrays
600 * as they don't make sense
601 */
602 if (info.array.level != -4)
603 if (!(devices[j].state & (1<<MD_DISK_SYNC))) {
604 if (!(devices[j].state & (1<<MD_DISK_FAULTY)))
605 sparecnt++;
606 continue;
607 }
608 if (devices[j].events+event_margin >=
609 devices[most_recent].events) {
610 devices[j].uptodate = 1;
611 if (i < info.array.raid_disks) {
612 okcnt++;
613 avail[i]=1;
614 } else
615 sparecnt++;
616 }
617 }
618 while (force && !enough(info.array.level, info.array.raid_disks,
619 info.array.layout, 1,
620 avail, okcnt)) {
621 /* Choose the newest best drive which is
622 * not up-to-date, update the superblock
623 * and add it.
624 */
625 int fd;
626 long long current_events;
627 chosen_drive = -1;
628 for (i=0; i<info.array.raid_disks && i < bestcnt; i++) {
629 int j = best[i];
630 if (j>=0 &&
631 !devices[j].uptodate &&
632 devices[j].events > 0 &&
633 (chosen_drive < 0 ||
634 devices[j].events > devices[chosen_drive].events))
635 chosen_drive = j;
636 }
637 if (chosen_drive < 0)
638 break;
639 current_events = devices[chosen_drive].events;
640 add_another:
641 if (verbose >= 0)
642 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
643 devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
644 (int)(devices[chosen_drive].events),
645 (int)(devices[most_recent].events));
646 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
647 if (fd < 0) {
648 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
649 devices[chosen_drive].devname);
650 devices[chosen_drive].events = 0;
651 continue;
652 }
653 if (st->ss->load_super(st,fd, &super, NULL)) {
654 close(fd);
655 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
656 devices[chosen_drive].devname);
657 devices[chosen_drive].events = 0;
658 continue;
659 }
660 info.events = devices[most_recent].events;
661 st->ss->update_super(st, &info, super, "force-one",
662 devices[chosen_drive].devname, verbose,
663 0, NULL);
664
665 if (st->ss->store_super(st, fd, super)) {
666 close(fd);
667 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
668 devices[chosen_drive].devname);
669 devices[chosen_drive].events = 0;
670 st->ss->free_super(st, super);
671 continue;
672 }
673 close(fd);
674 devices[chosen_drive].events = devices[most_recent].events;
675 devices[chosen_drive].uptodate = 1;
676 avail[chosen_drive] = 1;
677 okcnt++;
678 st->ss->free_super(st, super);
679
680 /* If there are any other drives of the same vintage,
681 * add them in as well. We can't lose and we might gain
682 */
683 for (i=0; i<info.array.raid_disks && i < bestcnt ; i++) {
684 int j = best[i];
685 if (j >= 0 &&
686 !devices[j].uptodate &&
687 devices[j].events > 0 &&
688 devices[j].events == current_events) {
689 chosen_drive = j;
690 goto add_another;
691 }
692 }
693 }
694
695 /* Now we want to look at the superblock which the kernel will base things on
696 * and compare the devices that we think are working with the devices that the
697 * superblock thinks are working.
698 * If there are differences and --force is given, then update this chosen
699 * superblock.
700 */
701 chosen_drive = -1;
702 super = NULL;
703 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
704 int j = best[i];
705 int fd;
706
707 if (j<0)
708 continue;
709 if (!devices[j].uptodate)
710 continue;
711 chosen_drive = j;
712 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
713 fprintf(stderr, Name ": Cannot open %s: %s\n",
714 devices[j].devname, strerror(errno));
715 if (must_close) close(mdfd);
716 return 1;
717 }
718 if (st->ss->load_super(st,fd, &super, NULL)) {
719 close(fd);
720 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
721 devices[j].devname);
722 if (must_close) close(mdfd);
723 return 1;
724 }
725 close(fd);
726 }
727 if (super == NULL) {
728 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
729 if (must_close) close(mdfd);
730 return 1;
731 }
732 st->ss->getinfo_super(st, &info, super);
733 for (i=0; i<bestcnt; i++) {
734 int j = best[i];
735 unsigned int desired_state;
736
737 if (i < info.array.raid_disks)
738 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
739 else
740 desired_state = 0;
741
742 if (j<0)
743 continue;
744 if (!devices[j].uptodate)
745 continue;
746 info.disk.number = devices[j].disk_nr;
747 info.disk.raid_disk = i;
748 info.disk.state = desired_state;
749
750 if (devices[j].uptodate &&
751 st->ss->update_super(st, &info, super, "assemble", NULL,
752 verbose, 0, NULL)) {
753 if (force) {
754 if (verbose >= 0)
755 fprintf(stderr, Name ": "
756 "clearing FAULTY flag for device %d in %s for %s\n",
757 j, mddev, devices[j].devname);
758 change = 1;
759 } else {
760 if (verbose >= -1)
761 fprintf(stderr, Name ": "
762 "device %d in %s has wrong state in superblock, but %s seems ok\n",
763 i, mddev, devices[j].devname);
764 }
765 }
766 #if 0
767 if (!devices[j].uptodate &&
768 !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
769 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
770 i, mddev);
771 }
772 #endif
773 }
774 if (force && !clean &&
775 !enough(info.array.level, info.array.raid_disks,
776 info.array.layout, clean,
777 avail, okcnt)) {
778 change += st->ss->update_super(st, &info, super, "force-array",
779 devices[chosen_drive].devname, verbose,
780 0, NULL);
781 clean = 1;
782 }
783
784 if (change) {
785 int fd;
786 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
787 if (fd < 0) {
788 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
789 devices[chosen_drive].devname);
790 if (must_close) close(mdfd);
791 return 1;
792 }
793 if (st->ss->store_super(st, fd, super)) {
794 close(fd);
795 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
796 devices[chosen_drive].devname);
797 if (must_close) close(mdfd);
798 return 1;
799 }
800 close(fd);
801 }
802
803 /* If we are in the middle of a reshape we may need to restore saved data
804 * that was moved aside due to the reshape overwriting live data
805 * The code of doing this lives in Grow.c
806 */
807 #ifndef MDASSEMBLE
808 if (info.reshape_active) {
809 int err = 0;
810 int *fdlist = malloc(sizeof(int)* bestcnt);
811 for (i=0; i<bestcnt; i++) {
812 int j = best[i];
813 if (j >= 0) {
814 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
815 if (fdlist[i] < 0) {
816 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
817 devices[j].devname);
818 err = 1;
819 break;
820 }
821 } else
822 fdlist[i] = -1;
823 }
824 if (!err)
825 err = Grow_restart(st, &info, fdlist, bestcnt, backup_file);
826 while (i>0) {
827 i--;
828 if (fdlist[i]>=0) close(fdlist[i]);
829 }
830 if (err) {
831 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
832 if (must_close) close(mdfd);
833 return err;
834 }
835 }
836 #endif
837 /* count number of in-sync devices according to the superblock.
838 * We must have this number to start the array without -s or -R
839 */
840 req_cnt = info.array.working_disks;
841
842 /* Almost ready to actually *do* something */
843 if (!old_linux) {
844 int rv;
845 if ((vers % 100) >= 1) { /* can use different versions */
846 mdu_array_info_t inf;
847 memset(&inf, 0, sizeof(inf));
848 inf.major_version = st->ss->major;
849 inf.minor_version = st->minor_version;
850 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
851 } else
852 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
853
854 if (rv) {
855 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
856 mddev, strerror(errno));
857 if (must_close) close(mdfd);
858 return 1;
859 }
860 if (ident->bitmap_fd >= 0) {
861 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
862 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
863 if (must_close) close(mdfd);
864 return 1;
865 }
866 } else if (ident->bitmap_file) {
867 /* From config file */
868 int bmfd = open(ident->bitmap_file, O_RDWR);
869 if (bmfd < 0) {
870 fprintf(stderr, Name ": Could not open bitmap file %s\n",
871 ident->bitmap_file);
872 if (must_close) close(mdfd);
873 return 1;
874 }
875 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
876 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
877 close(bmfd);
878 if (must_close) close(mdfd);
879 return 1;
880 }
881 close(bmfd);
882 }
883
884 /* First, add the raid disks, but add the chosen one last */
885 for (i=0; i<= bestcnt; i++) {
886 int j;
887 if (i < bestcnt) {
888 j = best[i];
889 if (j == chosen_drive)
890 continue;
891 } else
892 j = chosen_drive;
893
894 if (j >= 0 /* && devices[j].uptodate */) {
895 mdu_disk_info_t disk;
896 memset(&disk, 0, sizeof(disk));
897 disk.major = devices[j].major;
898 disk.minor = devices[j].minor;
899 if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) {
900 fprintf(stderr, Name ": failed to add %s to %s: %s\n",
901 devices[j].devname,
902 mddev,
903 strerror(errno));
904 if (i < info.array.raid_disks || i == bestcnt)
905 okcnt--;
906 else
907 sparecnt--;
908 } else if (verbose > 0)
909 fprintf(stderr, Name ": added %s to %s as %d\n",
910 devices[j].devname, mddev, devices[j].raid_disk);
911 } else if (verbose > 0 && i < info.array.raid_disks)
912 fprintf(stderr, Name ": no uptodate device for slot %d of %s\n",
913 i, mddev);
914 }
915
916 if (runstop == 1 ||
917 (runstop <= 0 &&
918 ( enough(info.array.level, info.array.raid_disks,
919 info.array.layout, clean, avail, okcnt) &&
920 (okcnt >= req_cnt || start_partial_ok)
921 ))) {
922 if (ioctl(mdfd, RUN_ARRAY, NULL)==0) {
923 if (verbose >= 0) {
924 fprintf(stderr, Name ": %s has been started with %d drive%s",
925 mddev, okcnt, okcnt==1?"":"s");
926 if (okcnt < info.array.raid_disks)
927 fprintf(stderr, " (out of %d)", info.array.raid_disks);
928 if (sparecnt)
929 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
930 fprintf(stderr, ".\n");
931 }
932 if (must_close) {
933 int usecs = 1;
934 close(mdfd);
935 /* There is a nasty race with 'mdadm --monitor'.
936 * If it opens this device before we close it,
937 * it gets an incomplete open on which IO
938 * doesn't work and the capacity if wrong.
939 * If we reopen (to check for layered devices)
940 * before --monitor closes, we loose.
941 *
942 * So: wait upto 1 second for there to be
943 * a non-zero capacity.
944 */
945 while (usecs < 1000) {
946 mdfd = open(mddev, O_RDONLY);
947 if (mdfd >= 0) {
948 unsigned long long size;
949 if (get_dev_size(mdfd, NULL, &size) &&
950 size > 0)
951 break;
952 close(mdfd);
953 }
954 usleep(usecs);
955 usecs <<= 1;
956 }
957 }
958 return 0;
959 }
960 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
961 mddev, strerror(errno));
962
963 if (!enough(info.array.level, info.array.raid_disks,
964 info.array.layout, 1, avail, okcnt))
965 fprintf(stderr, Name ": Not enough devices to "
966 "start the array.\n");
967 else if (!enough(info.array.level,
968 info.array.raid_disks,
969 info.array.layout, clean,
970 avail, okcnt))
971 fprintf(stderr, Name ": Not enough devices to "
972 "start the array while not clean "
973 "- consider --force.\n");
974
975 if (must_close) close(mdfd);
976 return 1;
977 }
978 if (runstop == -1) {
979 fprintf(stderr, Name ": %s assembled from %d drive%s",
980 mddev, okcnt, okcnt==1?"":"s");
981 if (okcnt != info.array.raid_disks)
982 fprintf(stderr, " (out of %d)", info.array.raid_disks);
983 fprintf(stderr, ", but not started.\n");
984 if (must_close) close(mdfd);
985 return 0;
986 }
987 if (verbose >= -1) {
988 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
989 if (sparecnt)
990 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
991 if (!enough(info.array.level, info.array.raid_disks,
992 info.array.layout, 1, avail, okcnt))
993 fprintf(stderr, " - not enough to start the array.\n");
994 else if (!enough(info.array.level,
995 info.array.raid_disks,
996 info.array.layout, clean,
997 avail, okcnt))
998 fprintf(stderr, " - not enough to start the "
999 "array while not clean - consider "
1000 "--force.\n");
1001 else {
1002 if (req_cnt == info.array.raid_disks)
1003 fprintf(stderr, " - need all %d to start it", req_cnt);
1004 else
1005 fprintf(stderr, " - need %d of %d to start", req_cnt, info.array.raid_disks);
1006 fprintf(stderr, " (use --run to insist).\n");
1007 }
1008 }
1009 if (must_close) close(mdfd);
1010 return 1;
1011 } else {
1012 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1013 * been updated to point to the current locations of devices.
1014 * so we can just start the array
1015 */
1016 unsigned long dev;
1017 dev = makedev(devices[chosen_drive].major,
1018 devices[chosen_drive].minor);
1019 if (ioctl(mdfd, START_ARRAY, dev)) {
1020 fprintf(stderr, Name ": Cannot start array: %s\n",
1021 strerror(errno));
1022 }
1023
1024 }
1025 if (must_close) close(mdfd);
1026 return 0;
1027 }