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