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