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