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