]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Various compile fixes.
[thirdparty/mdadm.git] / Assemble.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include <ctype.h>
27
28 static int name_matches(char *found, char *required, char *homehost)
29 {
30 /* See if the name found matches the required name, possibly
31 * prefixed with 'homehost'
32 */
33 char fnd[33];
34
35 strncpy(fnd, found, 32);
36 fnd[32] = 0;
37 if (strcmp(found, required)==0)
38 return 1;
39 if (homehost) {
40 int l = strlen(homehost);
41 if (l < 32 && fnd[l] == ':' &&
42 strcmp(fnd+l+1, required)==0)
43 return 1;
44 }
45 return 0;
46 }
47
48 static int is_member_busy(char *metadata_version)
49 {
50 /* check if the given member array is active */
51 struct mdstat_ent *mdstat = mdstat_read(1, 0);
52 struct mdstat_ent *ent;
53 int busy = 0;
54
55 for (ent = mdstat; ent; ent = ent->next) {
56 if (ent->metadata_version == NULL)
57 continue;
58 if (strncmp(ent->metadata_version, "external:", 9) != 0)
59 continue;
60 if (!is_subarray(&ent->metadata_version[9]))
61 continue;
62 /* Skip first char - it can be '/' or '-' */
63 if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) {
64 busy = 1;
65 break;
66 }
67 }
68 free_mdstat(mdstat);
69
70 return busy;
71 }
72
73 static int ident_matches(struct mddev_ident *ident,
74 struct mdinfo *content,
75 struct supertype *tst,
76 char *homehost,
77 char *update, char *devname)
78 {
79
80 if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
81 same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 &&
82 memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) {
83 if (devname)
84 fprintf(stderr, Name ": %s has wrong uuid.\n",
85 devname);
86 return 0;
87 }
88 if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
89 name_matches(content->name, ident->name, homehost)==0) {
90 if (devname)
91 fprintf(stderr, Name ": %s has wrong name.\n",
92 devname);
93 return 0;
94 }
95 if (ident->super_minor != UnSet &&
96 ident->super_minor != content->array.md_minor) {
97 if (devname)
98 fprintf(stderr, Name ": %s has wrong super-minor.\n",
99 devname);
100 return 0;
101 }
102 if (ident->level != UnSet &&
103 ident->level != content->array.level) {
104 if (devname)
105 fprintf(stderr, Name ": %s has wrong raid level.\n",
106 devname);
107 return 0;
108 }
109 if (ident->raid_disks != UnSet &&
110 ident->raid_disks!= content->array.raid_disks) {
111 if (devname)
112 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
113 devname);
114 return 0;
115 }
116 if (ident->member && ident->member[0]) {
117 /* content->text_version must match */
118 char *s = strchr(content->text_version+1, '/');
119 if (s == NULL) {
120 if (devname)
121 fprintf(stderr, Name ": %s is not a container and one is required.\n",
122 devname);
123 return 0;
124 } else if (strcmp(ident->member, s+1) != 0) {
125 if (devname)
126 fprintf(stderr, Name ": skipping wrong member %s is %s\n",
127 content->text_version, devname);
128 return 0;
129 }
130 }
131 return 1;
132 }
133
134
135 int Assemble(struct supertype *st, char *mddev,
136 struct mddev_ident *ident,
137 struct mddev_dev *devlist,
138 char *backup_file, int invalid_backup,
139 int readonly, int runstop,
140 char *update, char *homehost, int require_homehost,
141 int verbose, int force)
142 {
143 /*
144 * The task of Assemble is to find a collection of
145 * devices that should (according to their superblocks)
146 * form an array, and to give this collection to the MD driver.
147 * In Linux-2.4 and later, this involves submitting a
148 * SET_ARRAY_INFO ioctl with no arg - to prepare
149 * the array - and then submit a number of
150 * ADD_NEW_DISK ioctls to add disks into
151 * the array. Finally RUN_ARRAY might
152 * be submitted to start the array.
153 *
154 * Much of the work of Assemble is in finding and/or
155 * checking the disks to make sure they look right.
156 *
157 * If mddev is not set, then scan must be set and we
158 * read through the config file for dev+uuid mapping
159 * We recurse, setting mddev, for each device that
160 * - isn't running
161 * - has a valid uuid (or any uuid if !uuidset)
162 *
163 * If mddev is set, we try to determine state of md.
164 * check version - must be at least 0.90.0
165 * check kernel version. must be at least 2.4.
166 * If not, we can possibly fall back on START_ARRAY
167 * Try to GET_ARRAY_INFO.
168 * If possible, give up
169 * If not, try to STOP_ARRAY just to make sure
170 *
171 * If !uuidset and scan, look in conf-file for uuid
172 * If not found, give up
173 * If !devlist and scan and uuidset, get list of devs from conf-file
174 *
175 * For each device:
176 * Check superblock - discard if bad
177 * Check uuid (set if we don't have one) - discard if no match
178 * Check superblock similarity if we have a superblock - discard if different
179 * Record events, devicenum
180 * This should give us a list of devices for the array
181 * We should collect the most recent event number
182 *
183 * Count disks with recent enough event count
184 * While force && !enough disks
185 * Choose newest rejected disks, update event count
186 * mark clean and rewrite superblock
187 * If recent kernel:
188 * SET_ARRAY_INFO
189 * foreach device with recent events : ADD_NEW_DISK
190 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
191 * If old kernel:
192 * Check the device numbers in superblock are right
193 * update superblock if any changes
194 * START_ARRAY
195 *
196 */
197 int mdfd;
198 int clean;
199 int auto_assem = (mddev == NULL && !ident->uuid_set &&
200 ident->super_minor == UnSet && ident->name[0] == 0
201 && (ident->container == NULL || ident->member == NULL));
202 int old_linux = 0;
203 int vers = vers; /* Keep gcc quite - it really is initialised */
204 struct {
205 char *devname;
206 int uptodate; /* set once we decide that this device is as
207 * recent as everything else in the array.
208 */
209 struct mdinfo i;
210 } *devices;
211 char *devmap;
212 int *best = NULL; /* indexed by raid_disk */
213 int bestcnt = 0;
214 int devcnt = 0;
215 unsigned int okcnt, sparecnt, rebuilding_cnt;
216 unsigned int req_cnt;
217 int i;
218 int most_recent = 0;
219 int chosen_drive;
220 int change = 0;
221 int inargv = 0;
222 int report_missmatch;
223 int bitmap_done;
224 int start_partial_ok = (runstop >= 0) &&
225 (force || devlist==NULL || auto_assem);
226 unsigned int num_devs;
227 struct mddev_dev *tmpdev;
228 struct mdinfo info;
229 struct mdinfo *content = NULL;
230 char *avail;
231 int nextspare = 0;
232 char *name = NULL;
233 int trustworthy;
234 char chosen_name[1024];
235 struct domainlist *domains = NULL;
236
237 if (get_linux_version() < 2004000)
238 old_linux = 1;
239
240 /*
241 * If any subdevs are listed, then any that don't
242 * match ident are discarded. Remainder must all match and
243 * become the array.
244 * If no subdevs, then we scan all devices in the config file, but
245 * there must be something in the identity
246 */
247
248 if (!devlist &&
249 ident->uuid_set == 0 &&
250 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
251 ident->name[0] == 0 &&
252 (ident->container == NULL || ident->member == NULL) &&
253 ident->devices == NULL) {
254 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
255 mddev ? mddev : "further assembly");
256 return 1;
257 }
258
259 if (devlist == NULL)
260 devlist = conf_get_devs();
261 else if (mddev)
262 inargv = 1;
263
264 report_missmatch = ((inargv && verbose >= 0) || verbose > 0);
265 try_again:
266 /* We come back here when doing auto-assembly and attempting some
267 * set of devices failed. Those are now marked as ->used==2 and
268 * we ignore them and try again
269 */
270
271 tmpdev = devlist; num_devs = 0;
272 while (tmpdev) {
273 if (tmpdev->used)
274 tmpdev->used = 2;
275 else
276 num_devs++;
277 tmpdev = tmpdev->next;
278 }
279
280 if (!st && ident->st) st = ident->st;
281
282 if (verbose>0)
283 fprintf(stderr, Name ": looking for devices for %s\n",
284 mddev ? mddev : "further assembly");
285
286 /* first walk the list of devices to find a consistent set
287 * that match the criterea, if that is possible.
288 * We flag the ones we like with 'used'.
289 */
290 for (tmpdev = devlist;
291 tmpdev;
292 tmpdev = tmpdev ? tmpdev->next : NULL) {
293 char *devname = tmpdev->devname;
294 int dfd;
295 struct stat stb;
296 struct supertype *tst = dup_super(st);
297 struct dev_policy *pol = NULL;
298 int found_container = 0;
299
300 if (tmpdev->used > 1) continue;
301
302 if (ident->devices &&
303 !match_oneof(ident->devices, devname)) {
304 if (report_missmatch)
305 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
306 continue;
307 }
308
309 dfd = dev_open(devname, O_RDONLY|O_EXCL);
310 if (dfd < 0) {
311 if (report_missmatch)
312 fprintf(stderr, Name ": cannot open device %s: %s\n",
313 devname, strerror(errno));
314 tmpdev->used = 2;
315 } else if (fstat(dfd, &stb)< 0) {
316 /* Impossible! */
317 fprintf(stderr, Name ": fstat failed for %s: %s\n",
318 devname, strerror(errno));
319 tmpdev->used = 2;
320 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
321 fprintf(stderr, Name ": %s is not a block device.\n",
322 devname);
323 tmpdev->used = 2;
324 } else if (must_be_container(dfd)) {
325 if (st) {
326 /* already found some components, this cannot
327 * be another one.
328 */
329 if (report_missmatch)
330 fprintf(stderr, Name ": %s is a container, but we are looking for components\n",
331 devname);
332 tmpdev->used = 2;
333 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
334 } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
335 if (report_missmatch)
336 fprintf(stderr, Name ": not a recognisable container: %s\n",
337 devname);
338 tmpdev->used = 2;
339 #endif
340 } else if (!tst->ss->load_container
341 || tst->ss->load_container(tst, dfd, NULL)) {
342 if (report_missmatch)
343 fprintf(stderr, Name ": no correct container type: %s\n",
344 devname);
345 tmpdev->used = 2;
346 } else if (auto_assem &&
347 !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)),
348 tst->ss->match_home(tst, homehost) == 1)) {
349 if (report_missmatch)
350 fprintf(stderr, Name ": %s has metadata type %s for which "
351 "auto-assembly is disabled\n",
352 devname, tst->ss->name);
353 tmpdev->used = 2;
354 } else
355 found_container = 1;
356 } else {
357 if (!tst && (tst = guess_super(dfd)) == NULL) {
358 if (report_missmatch)
359 fprintf(stderr, Name ": no recogniseable superblock on %s\n",
360 devname);
361 tmpdev->used = 2;
362 } else if (tst->ss->load_super(tst,dfd, NULL)) {
363 if (report_missmatch)
364 fprintf(stderr, Name ": no RAID superblock on %s\n",
365 devname);
366 tmpdev->used = 2;
367 } else if (tst->ss->compare_super == NULL) {
368 if (report_missmatch)
369 fprintf(stderr, Name ": Cannot assemble %s metadata on %s\n",
370 tst->ss->name, devname);
371 tmpdev->used = 2;
372 } else if (auto_assem && st == NULL &&
373 !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)),
374 tst->ss->match_home(tst, homehost) == 1)) {
375 if (report_missmatch)
376 fprintf(stderr, Name ": %s has metadata type %s for which "
377 "auto-assembly is disabled\n",
378 devname, tst->ss->name);
379 tmpdev->used = 2;
380 }
381 }
382 if (dfd >= 0) close(dfd);
383 if (tmpdev->used == 2) {
384 if (auto_assem || !inargv)
385 /* Ignore unrecognised devices during auto-assembly */
386 goto loop;
387 if (ident->uuid_set || ident->name[0] ||
388 ident->super_minor != UnSet)
389 /* Ignore unrecognised device if looking for
390 * specific array */
391 goto loop;
392
393
394 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
395 devname);
396 if (st)
397 st->ss->free_super(st);
398 dev_policy_free(pol);
399 domain_free(domains);
400 return 1;
401 }
402
403 if (found_container) {
404 /* tmpdev is a container. We need to be either
405 * looking for a member, or auto-assembling
406 */
407
408 if (ident->container) {
409 if (ident->container[0] == '/' &&
410 !same_dev(ident->container, devname)) {
411 if (report_missmatch)
412 fprintf(stderr, Name ": %s is not the container required (%s)\n",
413 devname, ident->container);
414 goto loop;
415 }
416 if (ident->container[0] != '/') {
417 /* we have a uuid */
418 int uuid[4];
419
420 content = &info;
421 memset(content, 0, sizeof(*content));
422 tst->ss->getinfo_super(tst, content, NULL);
423
424 if (!parse_uuid(ident->container, uuid) ||
425 !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) {
426 if (report_missmatch)
427 fprintf(stderr, Name ": %s has wrong UUID to be required container\n",
428 devname);
429 goto loop;
430 }
431 }
432 }
433 /* It is worth looking inside this container.
434 */
435 if (verbose > 0)
436 fprintf(stderr, Name ": looking in container %s\n",
437 devname);
438
439 for (content = tst->ss->container_content(tst, NULL);
440 content;
441 content = content->next) {
442
443 /* do not assemble arrays that might have bad blocks */
444 if (content->array.state & (1<<MD_SB_BBM_ERRORS)) {
445 fprintf(stderr, Name ": BBM log found in metadata. "
446 "Cannot activate array(s).\n");
447 tmpdev->used = 2;
448 goto loop;
449 }
450 if (!ident_matches(ident, content, tst,
451 homehost, update,
452 report_missmatch ? devname : NULL))
453 /* message already printed */;
454 else if (is_member_busy(content->text_version)) {
455 if (report_missmatch)
456 fprintf(stderr, Name ": member %s in %s is already assembled\n",
457 content->text_version,
458 devname);
459 } else
460 break;
461 }
462 if (!content) {
463 tmpdev->used = 2;
464 goto loop; /* empty container */
465 }
466
467 st = tst; tst = NULL;
468 if (!auto_assem && inargv && tmpdev->next != NULL) {
469 fprintf(stderr, Name ": %s is a container, but is not "
470 "only device given: confused and aborting\n",
471 devname);
472 st->ss->free_super(st);
473 dev_policy_free(pol);
474 domain_free(domains);
475 return 1;
476 }
477 if (verbose > 0)
478 fprintf(stderr, Name ": found match on member %s in %s\n",
479 content->text_version, devname);
480
481 /* make sure we finished the loop */
482 tmpdev = NULL;
483 goto loop;
484 } else {
485
486 content = &info;
487 memset(content, 0, sizeof(*content));
488 tst->ss->getinfo_super(tst, content, NULL);
489
490 if (!ident_matches(ident, content, tst,
491 homehost, update,
492 report_missmatch ? devname : NULL))
493 goto loop;
494
495 if (st == NULL)
496 st = dup_super(tst);
497 if (st->minor_version == -1)
498 st->minor_version = tst->minor_version;
499
500 if (memcmp(content->uuid, uuid_zero,
501 sizeof(int[4])) == 0) {
502 /* this is a floating spare. It cannot define
503 * an array unless there are no more arrays of
504 * this type to be found. It can be included
505 * in an array of this type though.
506 */
507 tmpdev->used = 3;
508 goto loop;
509 }
510
511 if (st->ss != tst->ss ||
512 st->minor_version != tst->minor_version ||
513 st->ss->compare_super(st, tst) != 0) {
514 /* Some mismatch. If exactly one array matches this host,
515 * we can resolve on that one.
516 * Or, if we are auto assembling, we just ignore the second
517 * for now.
518 */
519 if (auto_assem)
520 goto loop;
521 if (homehost) {
522 int first = st->ss->match_home(st, homehost);
523 int last = tst->ss->match_home(tst, homehost);
524 if (first != last &&
525 (first == 1 || last == 1)) {
526 /* We can do something */
527 if (first) {/* just ignore this one */
528 if (report_missmatch)
529 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
530 devname);
531 goto loop;
532 } else { /* reject all those sofar */
533 struct mddev_dev *td;
534 if (report_missmatch)
535 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
536 devname);
537 for (td=devlist; td != tmpdev; td=td->next)
538 if (td->used == 1)
539 td->used = 0;
540 tmpdev->used = 1;
541 goto loop;
542 }
543 }
544 }
545 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
546 devname);
547 tst->ss->free_super(tst);
548 st->ss->free_super(st);
549 dev_policy_free(pol);
550 domain_free(domains);
551 return 1;
552 }
553 tmpdev->used = 1;
554 }
555 loop:
556 /* Collect domain information from members only */
557 if (tmpdev && tmpdev->used == 1) {
558 if (!pol)
559 pol = devnum_policy(stb.st_rdev);
560 domain_merge(&domains, pol, tst?tst->ss->name:NULL);
561 }
562 dev_policy_free(pol);
563 pol = NULL;
564 if (tst)
565 tst->ss->free_super(tst);
566 }
567
568 /* Check if we found some imsm spares but no members */
569 if ((auto_assem ||
570 (ident->uuid_set &&
571 memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) &&
572 (!st || !st->sb))
573 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
574 if (tmpdev->used != 3)
575 continue;
576 tmpdev->used = 1;
577 content = &info;
578
579 if (!st->sb) {
580 /* we need sb from one of the spares */
581 int dfd = dev_open(tmpdev->devname, O_RDONLY);
582 if (dfd < 0 ||
583 st->ss->load_super(st, dfd, NULL))
584 tmpdev->used = 2;
585 if (dfd > 0)
586 close(dfd);
587 }
588 }
589
590 /* Now reject spares that don't match domains of identified members */
591 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
592 struct stat stb;
593 if (tmpdev->used != 3)
594 continue;
595 if (stat(tmpdev->devname, &stb)< 0) {
596 fprintf(stderr, Name ": fstat failed for %s: %s\n",
597 tmpdev->devname, strerror(errno));
598 tmpdev->used = 2;
599 } else {
600 struct dev_policy *pol = devnum_policy(stb.st_rdev);
601 int dt = domain_test(domains, pol, NULL);
602 if (inargv && dt != 0)
603 /* take this spare as domains match
604 * if there are any */
605 tmpdev->used = 1;
606 else if (!inargv && dt == 1)
607 /* device wasn't explicitly listed, so need
608 * explicit domain match - which we have */
609 tmpdev->used = 1;
610 else
611 /* if domains don't match mark as unused */
612 tmpdev->used = 0;
613 dev_policy_free(pol);
614 }
615 }
616 domain_free(domains);
617
618 if (!st || !st->sb || !content)
619 return 2;
620
621 /* Now need to open the array device. Use create_mddev */
622 if (content == &info)
623 st->ss->getinfo_super(st, content, NULL);
624
625 trustworthy = FOREIGN;
626 name = content->name;
627 switch (st->ss->match_home(st, homehost)
628 ?: st->ss->match_home(st, "any")) {
629 case 1:
630 trustworthy = LOCAL;
631 name = strchr(content->name, ':');
632 if (name)
633 name++;
634 else
635 name = content->name;
636 break;
637 }
638 if (!auto_assem)
639 /* If the array is listed in mdadm.conf or on
640 * command line, then we trust the name
641 * even if the array doesn't look local
642 */
643 trustworthy = LOCAL;
644
645 if (name[0] == 0 &&
646 content->array.level == LEVEL_CONTAINER) {
647 name = content->text_version;
648 trustworthy = METADATA;
649 }
650
651 if (name[0] && trustworthy != LOCAL &&
652 ! require_homehost &&
653 conf_name_is_free(name))
654 trustworthy = LOCAL;
655
656 if (trustworthy == LOCAL &&
657 strchr(name, ':'))
658 /* Ignore 'host:' prefix of name */
659 name = strchr(name, ':')+1;
660
661 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
662 chosen_name);
663 if (mdfd < 0) {
664 st->ss->free_super(st);
665 if (auto_assem)
666 goto try_again;
667 return 1;
668 }
669 mddev = chosen_name;
670 vers = md_get_version(mdfd);
671 if (vers < 9000) {
672 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
673 " Upgrade your kernel or try --build\n");
674 close(mdfd);
675 return 1;
676 }
677 if (mddev_busy(fd2devnum(mdfd))) {
678 fprintf(stderr, Name ": %s already active, cannot restart it!\n",
679 mddev);
680 for (tmpdev = devlist ;
681 tmpdev && tmpdev->used != 1;
682 tmpdev = tmpdev->next)
683 ;
684 if (tmpdev && auto_assem)
685 fprintf(stderr, Name ": %s needed for %s...\n",
686 mddev, tmpdev->devname);
687 close(mdfd);
688 mdfd = -3;
689 st->ss->free_super(st);
690 if (auto_assem)
691 goto try_again;
692 return 1;
693 }
694 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
695
696 #ifndef MDASSEMBLE
697 if (content != &info) {
698 /* This is a member of a container. Try starting the array. */
699 return assemble_container_content(st, mdfd, content, runstop,
700 chosen_name, verbose);
701 }
702 #endif
703 /* Ok, no bad inconsistancy, we can try updating etc */
704 bitmap_done = 0;
705 content->update_private = NULL;
706 devices = malloc(num_devs * sizeof(*devices));
707 devmap = calloc(num_devs * content->array.raid_disks, 1);
708 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
709 char *devname = tmpdev->devname;
710 struct stat stb;
711 /* looks like a good enough match to update the super block if needed */
712 #ifndef MDASSEMBLE
713 if (update) {
714 int dfd;
715 /* prepare useful information in info structures */
716 struct stat stb2;
717 struct supertype *tst;
718 int err;
719 fstat(mdfd, &stb2);
720
721 if (strcmp(update, "uuid")==0 &&
722 !ident->uuid_set) {
723 int rfd;
724 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
725 read(rfd, ident->uuid, 16) != 16) {
726 *(__u32*)(ident->uuid) = random();
727 *(__u32*)(ident->uuid+1) = random();
728 *(__u32*)(ident->uuid+2) = random();
729 *(__u32*)(ident->uuid+3) = random();
730 }
731 if (rfd >= 0) close(rfd);
732 }
733 dfd = dev_open(devname, O_RDWR|O_EXCL);
734
735 tst = dup_super(st);
736 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
737 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
738 devname);
739 if (dfd >= 0)
740 close(dfd);
741 close(mdfd);
742 free(devices);
743 free(devmap);
744 return 1;
745 }
746 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
747
748 memcpy(content->uuid, ident->uuid, 16);
749 strcpy(content->name, ident->name);
750 content->array.md_minor = minor(stb2.st_rdev);
751
752 if (strcmp(update, "byteorder") == 0)
753 err = 0;
754 else
755 err = tst->ss->update_super(tst, content, update,
756 devname, verbose,
757 ident->uuid_set,
758 homehost);
759 if (err < 0) {
760 fprintf(stderr,
761 Name ": --update=%s not understood"
762 " for %s metadata\n",
763 update, tst->ss->name);
764 tst->ss->free_super(tst);
765 free(tst);
766 close(mdfd);
767 close(dfd);
768 free(devices);
769 free(devmap);
770 return 1;
771 }
772 if (strcmp(update, "uuid")==0 &&
773 !ident->uuid_set) {
774 ident->uuid_set = 1;
775 memcpy(ident->uuid, content->uuid, 16);
776 }
777 if (tst->ss->store_super(tst, dfd))
778 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
779 devname);
780 close(dfd);
781
782 if (strcmp(update, "uuid")==0 &&
783 ident->bitmap_fd >= 0 && !bitmap_done) {
784 if (bitmap_update_uuid(ident->bitmap_fd,
785 content->uuid,
786 tst->ss->swapuuid) != 0)
787 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
788 else
789 bitmap_done = 1;
790 }
791 tst->ss->free_super(tst);
792 } else
793 #endif
794 {
795 struct supertype *tst = dup_super(st);
796 int dfd;
797 dfd = dev_open(devname, O_RDWR|O_EXCL);
798
799 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
800 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
801 devname);
802 if (dfd >= 0)
803 close(dfd);
804 close(mdfd);
805 free(devices);
806 free(devmap);
807 return 1;
808 }
809 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
810 tst->ss->free_super(tst);
811 close(dfd);
812 }
813
814 stat(devname, &stb);
815
816 if (verbose > 0)
817 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
818 devname, mddev, content->disk.raid_disk);
819 devices[devcnt].devname = devname;
820 devices[devcnt].uptodate = 0;
821 devices[devcnt].i = *content;
822 devices[devcnt].i.disk.major = major(stb.st_rdev);
823 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
824 if (most_recent < devcnt) {
825 if (devices[devcnt].i.events
826 > devices[most_recent].i.events)
827 most_recent = devcnt;
828 }
829 if (content->array.level == LEVEL_MULTIPATH)
830 /* with multipath, the raid_disk from the superblock is meaningless */
831 i = devcnt;
832 else
833 i = devices[devcnt].i.disk.raid_disk;
834 if (i+1 == 0) {
835 if (nextspare < content->array.raid_disks)
836 nextspare = content->array.raid_disks;
837 i = nextspare++;
838 } else {
839 if (i >= content->array.raid_disks &&
840 i >= nextspare)
841 nextspare = i+1;
842 }
843 if (i < 10000) {
844 if (i >= bestcnt) {
845 int newbestcnt = i+10;
846 int *newbest = malloc(sizeof(int)*newbestcnt);
847 int c;
848 for (c=0; c < newbestcnt; c++)
849 if (c < bestcnt)
850 newbest[c] = best[c];
851 else
852 newbest[c] = -1;
853 if (best)free(best);
854 best = newbest;
855 bestcnt = newbestcnt;
856 }
857 if (best[i] >=0 &&
858 devices[best[i]].i.events
859 == devices[devcnt].i.events
860 && (devices[best[i]].i.disk.minor
861 != devices[devcnt].i.disk.minor)
862 && st->ss == &super0
863 && content->array.level != LEVEL_MULTIPATH) {
864 /* two different devices with identical superblock.
865 * Could be a mis-detection caused by overlapping
866 * partitions. fail-safe.
867 */
868 fprintf(stderr, Name ": WARNING %s and %s appear"
869 " to have very similar superblocks.\n"
870 " If they are really different, "
871 "please --zero the superblock on one\n"
872 " If they are the same or overlap,"
873 " please remove one from %s.\n",
874 devices[best[i]].devname, devname,
875 inargv ? "the list" :
876 "the\n DEVICE list in mdadm.conf"
877 );
878 close(mdfd);
879 free(devices);
880 free(devmap);
881 return 1;
882 }
883 if (best[i] == -1
884 || (devices[best[i]].i.events
885 < devices[devcnt].i.events))
886 best[i] = devcnt;
887 }
888 devcnt++;
889 }
890 free(content->update_private);
891 content->update_private = NULL;
892
893 if (devcnt == 0) {
894 fprintf(stderr, Name ": no devices found for %s\n",
895 mddev);
896 if (st)
897 st->ss->free_super(st);
898 close(mdfd);
899 free(devices);
900 free(devmap);
901 return 1;
902 }
903
904 if (update && strcmp(update, "byteorder")==0)
905 st->minor_version = 90;
906
907 st->ss->getinfo_super(st, content, NULL);
908 clean = content->array.state & 1;
909
910 /* now we have some devices that might be suitable.
911 * I wonder how many
912 */
913 avail = malloc(content->array.raid_disks);
914 memset(avail, 0, content->array.raid_disks);
915 okcnt = 0;
916 sparecnt=0;
917 rebuilding_cnt=0;
918 for (i=0; i< bestcnt; i++) {
919 int j = best[i];
920 int event_margin = 1; /* always allow a difference of '1'
921 * like the kernel does
922 */
923 if (j < 0) continue;
924 /* note: we ignore error flags in multipath arrays
925 * as they don't make sense
926 */
927 if (content->array.level != LEVEL_MULTIPATH)
928 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
929 if (!(devices[j].i.disk.state
930 & (1<<MD_DISK_FAULTY))) {
931 devices[j].uptodate = 1;
932 sparecnt++;
933 }
934 continue;
935 }
936 /* If this devices thinks that 'most_recent' has failed, then
937 * we must reject this device.
938 */
939 if (j != most_recent &&
940 content->array.raid_disks > 0 &&
941 devices[most_recent].i.disk.raid_disk >= 0 &&
942 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
943 if (verbose > -1)
944 fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n",
945 devices[j].devname, devices[most_recent].devname);
946 best[i] = -1;
947 continue;
948 }
949 if (devices[j].i.events+event_margin >=
950 devices[most_recent].i.events) {
951 devices[j].uptodate = 1;
952 if (i < content->array.raid_disks) {
953 if (devices[j].i.recovery_start == MaxSector ||
954 (content->reshape_active &&
955 j >= content->array.raid_disks - content->delta_disks)) {
956 okcnt++;
957 avail[i]=1;
958 } else
959 rebuilding_cnt++;
960 } else
961 sparecnt++;
962 }
963 }
964 free(devmap);
965 while (force && !enough(content->array.level, content->array.raid_disks,
966 content->array.layout, 1,
967 avail, okcnt)) {
968 /* Choose the newest best drive which is
969 * not up-to-date, update the superblock
970 * and add it.
971 */
972 int fd;
973 struct supertype *tst;
974 unsigned long long current_events;
975 chosen_drive = -1;
976 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
977 int j = best[i];
978 if (j>=0 &&
979 !devices[j].uptodate &&
980 devices[j].i.recovery_start == MaxSector &&
981 (chosen_drive < 0 ||
982 devices[j].i.events
983 > devices[chosen_drive].i.events))
984 chosen_drive = j;
985 }
986 if (chosen_drive < 0)
987 break;
988 current_events = devices[chosen_drive].i.events;
989 add_another:
990 if (verbose >= 0)
991 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
992 devices[chosen_drive].devname,
993 devices[chosen_drive].i.disk.raid_disk,
994 (int)(devices[chosen_drive].i.events),
995 (int)(devices[most_recent].i.events));
996 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
997 if (fd < 0) {
998 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
999 devices[chosen_drive].devname);
1000 devices[chosen_drive].i.events = 0;
1001 continue;
1002 }
1003 tst = dup_super(st);
1004 if (tst->ss->load_super(tst,fd, NULL)) {
1005 close(fd);
1006 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
1007 devices[chosen_drive].devname);
1008 devices[chosen_drive].i.events = 0;
1009 continue;
1010 }
1011 content->events = devices[most_recent].i.events;
1012 tst->ss->update_super(tst, content, "force-one",
1013 devices[chosen_drive].devname, verbose,
1014 0, NULL);
1015
1016 if (tst->ss->store_super(tst, fd)) {
1017 close(fd);
1018 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1019 devices[chosen_drive].devname);
1020 devices[chosen_drive].i.events = 0;
1021 tst->ss->free_super(tst);
1022 continue;
1023 }
1024 close(fd);
1025 devices[chosen_drive].i.events = devices[most_recent].i.events;
1026 devices[chosen_drive].uptodate = 1;
1027 avail[chosen_drive] = 1;
1028 okcnt++;
1029 tst->ss->free_super(tst);
1030
1031 /* If there are any other drives of the same vintage,
1032 * add them in as well. We can't lose and we might gain
1033 */
1034 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
1035 int j = best[i];
1036 if (j >= 0 &&
1037 !devices[j].uptodate &&
1038 devices[j].i.events == current_events) {
1039 chosen_drive = j;
1040 goto add_another;
1041 }
1042 }
1043 }
1044
1045 /* Now we want to look at the superblock which the kernel will base things on
1046 * and compare the devices that we think are working with the devices that the
1047 * superblock thinks are working.
1048 * If there are differences and --force is given, then update this chosen
1049 * superblock.
1050 */
1051 chosen_drive = -1;
1052 st->ss->free_super(st);
1053 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
1054 int j = best[i];
1055 int fd;
1056
1057 if (j<0)
1058 continue;
1059 if (!devices[j].uptodate)
1060 continue;
1061 chosen_drive = j;
1062 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
1063 fprintf(stderr, Name ": Cannot open %s: %s\n",
1064 devices[j].devname, strerror(errno));
1065 close(mdfd);
1066 free(devices);
1067 return 1;
1068 }
1069 if (st->ss->load_super(st,fd, NULL)) {
1070 close(fd);
1071 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
1072 devices[j].devname);
1073 close(mdfd);
1074 free(devices);
1075 return 1;
1076 }
1077 close(fd);
1078 }
1079 if (st->sb == NULL) {
1080 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
1081 close(mdfd);
1082 free(devices);
1083 return 1;
1084 }
1085 st->ss->getinfo_super(st, content, NULL);
1086 #ifndef MDASSEMBLE
1087 sysfs_init(content, mdfd, 0);
1088 #endif
1089 for (i=0; i<bestcnt; i++) {
1090 int j = best[i];
1091 unsigned int desired_state;
1092
1093 if (i < content->array.raid_disks)
1094 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1095 else
1096 desired_state = 0;
1097
1098 if (j<0)
1099 continue;
1100 if (!devices[j].uptodate)
1101 continue;
1102
1103 devices[j].i.disk.state = desired_state;
1104 if (!(devices[j].i.array.state & 1))
1105 clean = 0;
1106
1107 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
1108 verbose, 0, NULL)) {
1109 if (force) {
1110 if (verbose >= 0)
1111 fprintf(stderr, Name ": "
1112 "clearing FAULTY flag for device %d in %s for %s\n",
1113 j, mddev, devices[j].devname);
1114 change = 1;
1115 } else {
1116 if (verbose >= -1)
1117 fprintf(stderr, Name ": "
1118 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1119 i, mddev, devices[j].devname);
1120 }
1121 }
1122 #if 0
1123 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
1124 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1125 i, mddev);
1126 }
1127 #endif
1128 }
1129 if (force && !clean &&
1130 !enough(content->array.level, content->array.raid_disks,
1131 content->array.layout, clean,
1132 avail, okcnt)) {
1133 change += st->ss->update_super(st, content, "force-array",
1134 devices[chosen_drive].devname, verbose,
1135 0, NULL);
1136 clean = 1;
1137 }
1138
1139 if (change) {
1140 int fd;
1141 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
1142 if (fd < 0) {
1143 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1144 devices[chosen_drive].devname);
1145 close(mdfd);
1146 free(devices);
1147 return 1;
1148 }
1149 if (st->ss->store_super(st, fd)) {
1150 close(fd);
1151 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1152 devices[chosen_drive].devname);
1153 close(mdfd);
1154 free(devices);
1155 return 1;
1156 }
1157 close(fd);
1158 }
1159
1160 /* If we are in the middle of a reshape we may need to restore saved data
1161 * that was moved aside due to the reshape overwriting live data
1162 * The code of doing this lives in Grow.c
1163 */
1164 #ifndef MDASSEMBLE
1165 if (content->reshape_active) {
1166 int err = 0;
1167 int *fdlist = malloc(sizeof(int)* bestcnt);
1168 if (verbose > 0)
1169 fprintf(stderr, Name ":%s has an active reshape - checking "
1170 "if critical section needs to be restored\n",
1171 chosen_name);
1172 for (i=0; i<bestcnt; i++) {
1173 int j = best[i];
1174 if (j >= 0) {
1175 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
1176 if (fdlist[i] < 0) {
1177 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1178 devices[j].devname);
1179 err = 1;
1180 break;
1181 }
1182 } else
1183 fdlist[i] = -1;
1184 }
1185 if (!err) {
1186 err = Grow_restart(st, content, fdlist, bestcnt,
1187 backup_file, verbose > 0);
1188 if (err && invalid_backup) {
1189 if (verbose > 0)
1190 fprintf(stderr, Name ": continuing"
1191 " without restoring backup\n");
1192 err = 0;
1193 }
1194 }
1195 while (i>0) {
1196 i--;
1197 if (fdlist[i]>=0) close(fdlist[i]);
1198 }
1199 if (err) {
1200 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
1201 if (backup_file == NULL)
1202 fprintf(stderr," Possibly you needed to specify the --backup-file\n");
1203 close(mdfd);
1204 free(devices);
1205 return err;
1206 }
1207 }
1208 #endif
1209 /* count number of in-sync devices according to the superblock.
1210 * We must have this number to start the array without -s or -R
1211 */
1212 req_cnt = content->array.working_disks;
1213
1214 /* Almost ready to actually *do* something */
1215 if (!old_linux) {
1216 int rv;
1217
1218 /* First, fill in the map, so that udev can find our name
1219 * as soon as we become active.
1220 */
1221 map_update(NULL, fd2devnum(mdfd), content->text_version,
1222 content->uuid, chosen_name);
1223
1224 rv = set_array_info(mdfd, st, content);
1225 if (rv) {
1226 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
1227 mddev, strerror(errno));
1228 ioctl(mdfd, STOP_ARRAY, NULL);
1229 close(mdfd);
1230 free(devices);
1231 return 1;
1232 }
1233 if (ident->bitmap_fd >= 0) {
1234 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
1235 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
1236 ioctl(mdfd, STOP_ARRAY, NULL);
1237 close(mdfd);
1238 free(devices);
1239 return 1;
1240 }
1241 } else if (ident->bitmap_file) {
1242 /* From config file */
1243 int bmfd = open(ident->bitmap_file, O_RDWR);
1244 if (bmfd < 0) {
1245 fprintf(stderr, Name ": Could not open bitmap file %s\n",
1246 ident->bitmap_file);
1247 ioctl(mdfd, STOP_ARRAY, NULL);
1248 close(mdfd);
1249 free(devices);
1250 return 1;
1251 }
1252 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
1253 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
1254 close(bmfd);
1255 ioctl(mdfd, STOP_ARRAY, NULL);
1256 close(mdfd);
1257 free(devices);
1258 return 1;
1259 }
1260 close(bmfd);
1261 }
1262
1263 /* First, add the raid disks, but add the chosen one last */
1264 for (i=0; i<= bestcnt; i++) {
1265 int j;
1266 if (i < bestcnt) {
1267 j = best[i];
1268 if (j == chosen_drive)
1269 continue;
1270 } else
1271 j = chosen_drive;
1272
1273 if (j >= 0 /* && devices[j].uptodate */) {
1274 int dfd = dev_open(devices[j].devname,
1275 O_RDWR|O_EXCL);
1276 if (dfd >= 0) {
1277 remove_partitions(dfd);
1278 close(dfd);
1279 }
1280 rv = add_disk(mdfd, st, content, &devices[j].i);
1281
1282 if (rv) {
1283 fprintf(stderr, Name ": failed to add "
1284 "%s to %s: %s\n",
1285 devices[j].devname,
1286 mddev,
1287 strerror(errno));
1288 if (i < content->array.raid_disks
1289 || i == bestcnt)
1290 okcnt--;
1291 else
1292 sparecnt--;
1293 } else if (verbose > 0)
1294 fprintf(stderr, Name ": added %s "
1295 "to %s as %d\n",
1296 devices[j].devname, mddev,
1297 devices[j].i.disk.raid_disk);
1298 } else if (verbose > 0 && i < content->array.raid_disks)
1299 fprintf(stderr, Name ": no uptodate device for "
1300 "slot %d of %s\n",
1301 i, mddev);
1302 }
1303
1304 if (content->array.level == LEVEL_CONTAINER) {
1305 if (verbose >= 0) {
1306 fprintf(stderr, Name ": Container %s has been "
1307 "assembled with %d drive%s",
1308 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
1309 if (okcnt < (unsigned)content->array.raid_disks)
1310 fprintf(stderr, " (out of %d)",
1311 content->array.raid_disks);
1312 fprintf(stderr, "\n");
1313 }
1314 st->ss->free_super(st);
1315 sysfs_uevent(content, "change");
1316 wait_for(chosen_name, mdfd);
1317 close(mdfd);
1318 free(devices);
1319 return 0;
1320 }
1321
1322 if (runstop == 1 ||
1323 (runstop <= 0 &&
1324 ( enough(content->array.level, content->array.raid_disks,
1325 content->array.layout, clean, avail, okcnt) &&
1326 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
1327 ))) {
1328 /* This array is good-to-go.
1329 * If a reshape is in progress then we might need to
1330 * continue monitoring it. In that case we start
1331 * it read-only and let the grow code make it writable.
1332 */
1333 int rv;
1334 #ifndef MDASSEMBLE
1335 if (content->reshape_active &&
1336 content->delta_disks <= 0)
1337 rv = Grow_continue(mdfd, st, content, backup_file);
1338 else
1339 #endif
1340 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1341 if (rv == 0) {
1342 if (verbose >= 0) {
1343 fprintf(stderr, Name ": %s has been started with %d drive%s",
1344 mddev, okcnt, okcnt==1?"":"s");
1345 if (okcnt < (unsigned)content->array.raid_disks)
1346 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1347 if (rebuilding_cnt)
1348 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1349 if (sparecnt)
1350 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1351 fprintf(stderr, ".\n");
1352 }
1353 if (content->reshape_active &&
1354 content->array.level >= 4 &&
1355 content->array.level <= 6) {
1356 /* might need to increase the size
1357 * of the stripe cache - default is 256
1358 */
1359 if (256 < 4 * (content->array.chunk_size/4096)) {
1360 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1361 if (sra)
1362 sysfs_set_num(sra, NULL,
1363 "stripe_cache_size",
1364 (4 * content->array.chunk_size / 4096) + 1);
1365 }
1366 }
1367 if (okcnt < (unsigned)content->array.raid_disks) {
1368 /* If any devices did not get added
1369 * because the kernel rejected them based
1370 * on event count, try adding them
1371 * again providing the action policy is
1372 * 're-add' or greater. The bitmap
1373 * might allow them to be included, or
1374 * they will become spares.
1375 */
1376 for (i = 0; i <= bestcnt; i++) {
1377 int j = best[i];
1378 if (j >= 0 && !devices[j].uptodate) {
1379 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1380 continue;
1381 rv = add_disk(mdfd, st, content,
1382 &devices[j].i);
1383 if (rv == 0 && verbose >= 0)
1384 fprintf(stderr,
1385 Name ": %s has been re-added.\n",
1386 devices[j].devname);
1387 }
1388 }
1389 }
1390 wait_for(mddev, mdfd);
1391 close(mdfd);
1392 if (auto_assem) {
1393 int usecs = 1;
1394 /* There is a nasty race with 'mdadm --monitor'.
1395 * If it opens this device before we close it,
1396 * it gets an incomplete open on which IO
1397 * doesn't work and the capacity is
1398 * wrong.
1399 * If we reopen (to check for layered devices)
1400 * before --monitor closes, we loose.
1401 *
1402 * So: wait upto 1 second for there to be
1403 * a non-zero capacity.
1404 */
1405 while (usecs < 1000) {
1406 mdfd = open(mddev, O_RDONLY);
1407 if (mdfd >= 0) {
1408 unsigned long long size;
1409 if (get_dev_size(mdfd, NULL, &size) &&
1410 size > 0)
1411 break;
1412 close(mdfd);
1413 }
1414 usleep(usecs);
1415 usecs <<= 1;
1416 }
1417 }
1418 free(devices);
1419 return 0;
1420 }
1421 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
1422 mddev, strerror(errno));
1423
1424 if (!enough(content->array.level, content->array.raid_disks,
1425 content->array.layout, 1, avail, okcnt))
1426 fprintf(stderr, Name ": Not enough devices to "
1427 "start the array.\n");
1428 else if (!enough(content->array.level,
1429 content->array.raid_disks,
1430 content->array.layout, clean,
1431 avail, okcnt))
1432 fprintf(stderr, Name ": Not enough devices to "
1433 "start the array while not clean "
1434 "- consider --force.\n");
1435
1436 if (auto_assem)
1437 ioctl(mdfd, STOP_ARRAY, NULL);
1438 close(mdfd);
1439 free(devices);
1440 return 1;
1441 }
1442 if (runstop == -1) {
1443 fprintf(stderr, Name ": %s assembled from %d drive%s",
1444 mddev, okcnt, okcnt==1?"":"s");
1445 if (okcnt != (unsigned)content->array.raid_disks)
1446 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1447 fprintf(stderr, ", but not started.\n");
1448 close(mdfd);
1449 free(devices);
1450 return 0;
1451 }
1452 if (verbose >= -1) {
1453 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1454 if (rebuilding_cnt)
1455 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
1456 if (sparecnt)
1457 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1458 if (!enough(content->array.level, content->array.raid_disks,
1459 content->array.layout, 1, avail, okcnt))
1460 fprintf(stderr, " - not enough to start the array.\n");
1461 else if (!enough(content->array.level,
1462 content->array.raid_disks,
1463 content->array.layout, clean,
1464 avail, okcnt))
1465 fprintf(stderr, " - not enough to start the "
1466 "array while not clean - consider "
1467 "--force.\n");
1468 else {
1469 if (req_cnt == (unsigned)content->array.raid_disks)
1470 fprintf(stderr, " - need all %d to start it", req_cnt);
1471 else
1472 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
1473 fprintf(stderr, " (use --run to insist).\n");
1474 }
1475 }
1476 if (auto_assem)
1477 ioctl(mdfd, STOP_ARRAY, NULL);
1478 close(mdfd);
1479 free(devices);
1480 return 1;
1481 } else {
1482 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1483 * been updated to point to the current locations of devices.
1484 * so we can just start the array
1485 */
1486 unsigned long dev;
1487 dev = makedev(devices[chosen_drive].i.disk.major,
1488 devices[chosen_drive].i.disk.minor);
1489 if (ioctl(mdfd, START_ARRAY, dev)) {
1490 fprintf(stderr, Name ": Cannot start array: %s\n",
1491 strerror(errno));
1492 }
1493
1494 }
1495 close(mdfd);
1496 free(devices);
1497 return 0;
1498 }
1499
1500 #ifndef MDASSEMBLE
1501 int assemble_container_content(struct supertype *st, int mdfd,
1502 struct mdinfo *content, int runstop,
1503 char *chosen_name, int verbose)
1504 {
1505 struct mdinfo *dev, *sra;
1506 int working = 0, preexist = 0;
1507 struct map_ent *map = NULL;
1508
1509 sysfs_init(content, mdfd, 0);
1510
1511 sra = sysfs_read(mdfd, 0, GET_VERSION);
1512 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
1513 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1514 close(mdfd);
1515 return 1;
1516 }
1517 if (sra)
1518 sysfs_free(sra);
1519
1520 for (dev = content->devs; dev; dev = dev->next)
1521 if (sysfs_add_disk(content, dev, 1) == 0)
1522 working++;
1523 else if (errno == EEXIST)
1524 preexist++;
1525 if (working == 0) {
1526 close(mdfd);
1527 return 1;/* Nothing new, don't try to start */
1528 }
1529
1530 map_update(&map, fd2devnum(mdfd),
1531 content->text_version,
1532 content->uuid, chosen_name);
1533
1534 if (runstop > 0 ||
1535 (working + preexist) >= content->array.working_disks) {
1536 int err;
1537
1538 switch(content->array.level) {
1539 case LEVEL_LINEAR:
1540 case LEVEL_MULTIPATH:
1541 case 0:
1542 err = sysfs_set_str(content, NULL, "array_state",
1543 "active");
1544 break;
1545 default:
1546 err = sysfs_set_str(content, NULL, "array_state",
1547 "readonly");
1548 /* start mdmon if needed. */
1549 if (!err) {
1550 if (!mdmon_running(st->container_dev))
1551 start_mdmon(st->container_dev);
1552 ping_monitor(devnum2devname(st->container_dev));
1553 }
1554 break;
1555 }
1556 if (!err)
1557 sysfs_set_safemode(content, content->safe_mode_delay);
1558 if (verbose >= 0) {
1559 if (err)
1560 fprintf(stderr, Name
1561 ": array %s now has %d devices",
1562 chosen_name, working + preexist);
1563 else
1564 fprintf(stderr, Name
1565 ": Started %s with %d devices",
1566 chosen_name, working + preexist);
1567 if (preexist)
1568 fprintf(stderr, " (%d new)", working);
1569 fprintf(stderr, "\n");
1570 }
1571 if (!err)
1572 wait_for(chosen_name, mdfd);
1573 close(mdfd);
1574 return 0;
1575 /* FIXME should have an O_EXCL and wait for read-auto */
1576 } else {
1577 if (verbose >= 0)
1578 fprintf(stderr, Name
1579 ": %s assembled with %d devices but "
1580 "not started\n",
1581 chosen_name, working);
1582 close(mdfd);
1583 return 1;
1584 }
1585 }
1586 #endif
1587