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