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