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