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