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