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