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