]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Assemble: remove some stray tracing.
[thirdparty/mdadm.git] / Assemble.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2012 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 pr_err("%s has wrong uuid.\n", devname);
85 return 0;
86 }
87 if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
88 name_matches(content->name, ident->name, homehost)==0) {
89 if (devname)
90 pr_err("%s has wrong name.\n", devname);
91 return 0;
92 }
93 if (ident->super_minor != UnSet &&
94 ident->super_minor != content->array.md_minor) {
95 if (devname)
96 pr_err("%s has wrong super-minor.\n",
97 devname);
98 return 0;
99 }
100 if (ident->level != UnSet &&
101 ident->level != content->array.level) {
102 if (devname)
103 pr_err("%s has wrong raid level.\n",
104 devname);
105 return 0;
106 }
107 if (ident->raid_disks != UnSet &&
108 ident->raid_disks!= content->array.raid_disks) {
109 if (devname)
110 pr_err("%s requires wrong number of drives.\n",
111 devname);
112 return 0;
113 }
114 if (ident->member && ident->member[0]) {
115 /* content->text_version must match */
116 char *s = strchr(content->text_version+1, '/');
117 if (s == NULL) {
118 if (devname)
119 pr_err("%s is not a container and one is required.\n",
120 devname);
121 return 0;
122 } else if (strcmp(ident->member, s+1) != 0) {
123 if (devname)
124 pr_err("skipping wrong member %s is %s\n",
125 content->text_version, devname);
126 return 0;
127 }
128 }
129 return 1;
130 }
131
132 static int select_devices(struct mddev_dev *devlist,
133 struct mddev_ident *ident,
134 struct supertype **stp,
135 struct mdinfo **contentp,
136 struct context *c,
137 int inargv, int auto_assem)
138 {
139 struct mddev_dev *tmpdev;
140 int num_devs;
141 struct supertype *st = *stp;
142 struct mdinfo *content = NULL;
143 int report_mismatch = ((inargv && c->verbose >= 0) || c->verbose > 0);
144 struct domainlist *domains = NULL;
145
146 tmpdev = devlist; num_devs = 0;
147 while (tmpdev) {
148 if (tmpdev->used)
149 tmpdev->used = 2;
150 else
151 num_devs++;
152 tmpdev->disposition = 0;
153 tmpdev = tmpdev->next;
154 }
155
156 /* first walk the list of devices to find a consistent set
157 * that match the criterea, if that is possible.
158 * We flag the ones we like with 'used'.
159 */
160 for (tmpdev = devlist;
161 tmpdev;
162 tmpdev = tmpdev ? tmpdev->next : NULL) {
163 char *devname = tmpdev->devname;
164 int dfd;
165 struct stat stb;
166 struct supertype *tst;
167 struct dev_policy *pol = NULL;
168 int found_container = 0;
169
170 if (tmpdev->used > 1)
171 continue;
172
173 if (ident->devices &&
174 !match_oneof(ident->devices, devname)) {
175 if (report_mismatch)
176 pr_err("%s is not one of %s\n", devname, ident->devices);
177 continue;
178 }
179
180 tst = dup_super(st);
181
182 dfd = dev_open(devname, O_RDONLY);
183 if (dfd < 0) {
184 if (report_mismatch)
185 pr_err("cannot open device %s: %s\n",
186 devname, strerror(errno));
187 tmpdev->used = 2;
188 } else if (fstat(dfd, &stb)< 0) {
189 /* Impossible! */
190 pr_err("fstat failed for %s: %s\n",
191 devname, strerror(errno));
192 tmpdev->used = 2;
193 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
194 pr_err("%s is not a block device.\n",
195 devname);
196 tmpdev->used = 2;
197 } else if (must_be_container(dfd)) {
198 if (st) {
199 /* already found some components, this cannot
200 * be another one.
201 */
202 if (report_mismatch)
203 pr_err("%s is a container, but we are looking for components\n",
204 devname);
205 tmpdev->used = 2;
206 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
207 } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
208 if (report_mismatch)
209 pr_err("not a recognisable container: %s\n",
210 devname);
211 tmpdev->used = 2;
212 #endif
213 } else if (!tst->ss->load_container
214 || tst->ss->load_container(tst, dfd, NULL)) {
215 if (report_mismatch)
216 pr_err("no correct container type: %s\n",
217 devname);
218 tmpdev->used = 2;
219 } else if (auto_assem &&
220 !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
221 tst->ss->match_home(tst, c->homehost) == 1)) {
222 if (report_mismatch)
223 pr_err("%s has metadata type %s for which "
224 "auto-assembly is disabled\n",
225 devname, tst->ss->name);
226 tmpdev->used = 2;
227 } else
228 found_container = 1;
229 } else {
230 if (!tst && (tst = guess_super(dfd)) == NULL) {
231 if (report_mismatch)
232 pr_err("no recogniseable superblock on %s\n",
233 devname);
234 tmpdev->used = 2;
235 } else if (tst->ss->load_super(tst,dfd, NULL)) {
236 if (report_mismatch)
237 pr_err("no RAID superblock on %s\n",
238 devname);
239 tmpdev->used = 2;
240 } else if (tst->ss->compare_super == NULL) {
241 if (report_mismatch)
242 pr_err("Cannot assemble %s metadata on %s\n",
243 tst->ss->name, devname);
244 tmpdev->used = 2;
245 } else if (auto_assem && st == NULL &&
246 !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
247 tst->ss->match_home(tst, c->homehost) == 1)) {
248 if (report_mismatch)
249 pr_err("%s has metadata type %s for which "
250 "auto-assembly is disabled\n",
251 devname, tst->ss->name);
252 tmpdev->used = 2;
253 }
254 }
255 if (dfd >= 0) close(dfd);
256 if (tmpdev->used == 2) {
257 if (auto_assem || !inargv)
258 /* Ignore unrecognised devices during auto-assembly */
259 goto loop;
260 if (ident->uuid_set || ident->name[0] ||
261 ident->super_minor != UnSet)
262 /* Ignore unrecognised device if looking for
263 * specific array */
264 goto loop;
265
266 pr_err("%s has no superblock - assembly aborted\n",
267 devname);
268 if (st)
269 st->ss->free_super(st);
270 dev_policy_free(pol);
271 domain_free(domains);
272 return -1;
273 }
274
275 if (found_container) {
276 /* tmpdev is a container. We need to be either
277 * looking for a member, or auto-assembling
278 */
279 /* should be safe to try an exclusive open now, we
280 * have rejected anything that some other mdadm might
281 * be looking at
282 */
283 dfd = dev_open(devname, O_RDONLY | O_EXCL);
284 if (dfd < 0) {
285 if (report_mismatch)
286 pr_err("%s is busy - skipping\n", devname);
287 goto loop;
288 }
289 close(dfd);
290
291 if (ident->container) {
292 if (ident->container[0] == '/' &&
293 !same_dev(ident->container, devname)) {
294 if (report_mismatch)
295 pr_err("%s is not the container required (%s)\n",
296 devname, ident->container);
297 goto loop;
298 }
299 if (ident->container[0] != '/') {
300 /* we have a uuid */
301 int uuid[4];
302
303 content = *contentp;
304 tst->ss->getinfo_super(tst, content, NULL);
305
306 if (!parse_uuid(ident->container, uuid) ||
307 !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) {
308 if (report_mismatch)
309 pr_err("%s has wrong UUID to be required container\n",
310 devname);
311 goto loop;
312 }
313 }
314 }
315 /* It is worth looking inside this container.
316 */
317 if (c->verbose > 0)
318 pr_err("looking in container %s\n",
319 devname);
320
321 for (content = tst->ss->container_content(tst, NULL);
322 content;
323 content = content->next) {
324
325 if (!ident_matches(ident, content, tst,
326 c->homehost, c->update,
327 report_mismatch ? devname : NULL))
328 /* message already printed */;
329 else if (is_member_busy(content->text_version)) {
330 if (report_mismatch)
331 pr_err("member %s in %s is already assembled\n",
332 content->text_version,
333 devname);
334 } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
335 /* do not assemble arrays with unsupported configurations */
336 pr_err("Cannot activate member %s in %s.\n",
337 content->text_version,
338 devname);
339 } else
340 break;
341 }
342 if (!content) {
343 tmpdev->used = 2;
344 goto loop; /* empty container */
345 }
346
347 st = tst; tst = NULL;
348 if (!auto_assem && inargv && tmpdev->next != NULL) {
349 pr_err("%s is a container, but is not "
350 "only device given: confused and aborting\n",
351 devname);
352 st->ss->free_super(st);
353 dev_policy_free(pol);
354 domain_free(domains);
355 return -1;
356 }
357 if (c->verbose > 0)
358 pr_err("found match on member %s in %s\n",
359 content->text_version, devname);
360
361 /* make sure we finished the loop */
362 tmpdev = NULL;
363 goto loop;
364 } else {
365 int rv = 0;
366 struct mddev_ident *match;
367
368 content = *contentp;
369 tst->ss->getinfo_super(tst, content, NULL);
370
371 if (!ident_matches(ident, content, tst,
372 c->homehost, c->update,
373 report_mismatch ? devname : NULL))
374 goto loop;
375
376 match = conf_match(tst, content, devname,
377 report_mismatch ? c->verbose : -1,
378 &rv);
379 if (!match && rv == 2)
380 goto loop;
381 if (match && match->devname &&
382 strcasecmp(match->devname, "<ignore>") == 0) {
383 if (report_mismatch)
384 pr_err("%s is a member of an explicitly ignored array\n",
385 devname);
386 goto loop;
387 }
388 if (match && !ident_matches(match, content, tst,
389 c->homehost, c->update,
390 report_mismatch ? devname : NULL))
391 /* Array exists in mdadm.conf but some
392 * details don't match, so reject it
393 */
394 goto loop;
395
396 /* should be safe to try an exclusive open now, we
397 * have rejected anything that some other mdadm might
398 * be looking at
399 */
400 dfd = dev_open(devname, O_RDONLY | O_EXCL);
401 if (dfd < 0) {
402 if (report_mismatch)
403 pr_err("%s is busy - skipping\n", devname);
404 goto loop;
405 }
406 close(dfd);
407
408 if (st == NULL)
409 st = dup_super(tst);
410 if (st->minor_version == -1)
411 st->minor_version = tst->minor_version;
412
413 if (memcmp(content->uuid, uuid_zero,
414 sizeof(int[4])) == 0) {
415 /* this is a floating spare. It cannot define
416 * an array unless there are no more arrays of
417 * this type to be found. It can be included
418 * in an array of this type though.
419 */
420 tmpdev->used = 3;
421 goto loop;
422 }
423
424 if (st->ss != tst->ss ||
425 st->minor_version != tst->minor_version ||
426 st->ss->compare_super(st, tst) != 0) {
427 /* Some mismatch. If exactly one array matches this host,
428 * we can resolve on that one.
429 * Or, if we are auto assembling, we just ignore the second
430 * for now.
431 */
432 if (auto_assem)
433 goto loop;
434 if (c->homehost) {
435 int first = st->ss->match_home(st, c->homehost);
436 int last = tst->ss->match_home(tst, c->homehost);
437 if (first != last &&
438 (first == 1 || last == 1)) {
439 /* We can do something */
440 if (first) {/* just ignore this one */
441 if (report_mismatch)
442 pr_err("%s misses out due to wrong homehost\n",
443 devname);
444 goto loop;
445 } else { /* reject all those sofar */
446 struct mddev_dev *td;
447 if (report_mismatch)
448 pr_err("%s overrides previous devices due to good homehost\n",
449 devname);
450 for (td=devlist; td != tmpdev; td=td->next)
451 if (td->used == 1)
452 td->used = 0;
453 tmpdev->used = 1;
454 goto loop;
455 }
456 }
457 }
458 pr_err("superblock on %s doesn't match others - assembly aborted\n",
459 devname);
460 tst->ss->free_super(tst);
461 st->ss->free_super(st);
462 dev_policy_free(pol);
463 domain_free(domains);
464 return -1;
465 }
466 tmpdev->used = 1;
467 }
468 loop:
469 /* Collect domain information from members only */
470 if (tmpdev && tmpdev->used == 1) {
471 if (!pol)
472 pol = devid_policy(stb.st_rdev);
473 domain_merge(&domains, pol, tst?tst->ss->name:NULL);
474 }
475 dev_policy_free(pol);
476 pol = NULL;
477 if (tst)
478 tst->ss->free_super(tst);
479 }
480
481 /* Check if we found some imsm spares but no members */
482 if ((auto_assem ||
483 (ident->uuid_set &&
484 memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) &&
485 (!st || !st->sb))
486 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
487 if (tmpdev->used != 3)
488 continue;
489 tmpdev->used = 1;
490 content = *contentp;
491
492 if (!st->sb) {
493 /* we need sb from one of the spares */
494 int dfd = dev_open(tmpdev->devname, O_RDONLY);
495 if (dfd < 0 ||
496 st->ss->load_super(st, dfd, NULL))
497 tmpdev->used = 2;
498 if (dfd > 0)
499 close(dfd);
500 }
501 }
502
503 /* Now reject spares that don't match domains of identified members */
504 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
505 struct stat stb;
506 if (tmpdev->used != 3)
507 continue;
508 if (stat(tmpdev->devname, &stb)< 0) {
509 pr_err("fstat failed for %s: %s\n",
510 tmpdev->devname, strerror(errno));
511 tmpdev->used = 2;
512 } else {
513 struct dev_policy *pol = devid_policy(stb.st_rdev);
514 int dt = domain_test(domains, pol, NULL);
515 if (inargv && dt != 0)
516 /* take this spare as domains match
517 * if there are any */
518 tmpdev->used = 1;
519 else if (!inargv && dt == 1)
520 /* device wasn't explicitly listed, so need
521 * explicit domain match - which we have */
522 tmpdev->used = 1;
523 else
524 /* if domains don't match mark as unused */
525 tmpdev->used = 0;
526 dev_policy_free(pol);
527 }
528 }
529 domain_free(domains);
530 *stp = st;
531 if (st && st->sb && content == *contentp)
532 st->ss->getinfo_super(st, content, NULL);
533 *contentp = content;
534
535 return num_devs;
536 }
537
538 struct devs {
539 char *devname;
540 int uptodate; /* set once we decide that this device is as
541 * recent as everything else in the array.
542 */
543 int included; /* set if the device is already in the array
544 * due to a previous '-I'
545 */
546 struct mdinfo i;
547 };
548
549 static int load_devices(struct devs *devices, char *devmap,
550 struct mddev_ident *ident, struct supertype *st,
551 struct mddev_dev *devlist, struct context *c,
552 struct mdinfo *content,
553 int mdfd, char *mddev,
554 int *most_recentp, int *bestcntp, int **bestp,
555 int inargv)
556 {
557 struct mddev_dev *tmpdev;
558 int devcnt = 0;
559 int nextspare = 0;
560 #ifndef MDASSEMBLE
561 int bitmap_done = 0;
562 #endif
563 int most_recent = 0;
564 int bestcnt = 0;
565 int *best = *bestp;
566
567 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) {
568 char *devname = tmpdev->devname;
569 struct stat stb;
570 int i;
571
572 if (tmpdev->used != 1)
573 continue;
574 /* looks like a good enough match to update the super block if needed */
575 #ifndef MDASSEMBLE
576 if (c->update) {
577 int dfd;
578 /* prepare useful information in info structures */
579 struct stat stb2;
580 struct supertype *tst;
581 int err;
582 fstat(mdfd, &stb2);
583
584 if (strcmp(c->update, "uuid")==0 &&
585 !ident->uuid_set) {
586 int rfd;
587 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
588 read(rfd, ident->uuid, 16) != 16) {
589 *(__u32*)(ident->uuid) = random();
590 *(__u32*)(ident->uuid+1) = random();
591 *(__u32*)(ident->uuid+2) = random();
592 *(__u32*)(ident->uuid+3) = random();
593 }
594 if (rfd >= 0) close(rfd);
595 }
596 dfd = dev_open(devname,
597 tmpdev->disposition == 'I'
598 ? O_RDWR : (O_RDWR|O_EXCL));
599
600 tst = dup_super(st);
601 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
602 pr_err("cannot re-read metadata from %s - aborting\n",
603 devname);
604 if (dfd >= 0)
605 close(dfd);
606 close(mdfd);
607 free(devices);
608 free(devmap);
609 return -1;
610 }
611 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
612
613 memcpy(content->uuid, ident->uuid, 16);
614 strcpy(content->name, ident->name);
615 content->array.md_minor = minor(stb2.st_rdev);
616
617 if (strcmp(c->update, "byteorder") == 0)
618 err = 0;
619 else
620 err = tst->ss->update_super(tst, content, c->update,
621 devname, c->verbose,
622 ident->uuid_set,
623 c->homehost);
624 if (err < 0) {
625 if (err == -1)
626 pr_err("--update=%s not understood"
627 " for %s metadata\n",
628 c->update, tst->ss->name);
629 tst->ss->free_super(tst);
630 free(tst);
631 close(mdfd);
632 close(dfd);
633 free(devices);
634 free(devmap);
635 return -1;
636 }
637 if (strcmp(c->update, "uuid")==0 &&
638 !ident->uuid_set) {
639 ident->uuid_set = 1;
640 memcpy(ident->uuid, content->uuid, 16);
641 }
642 if (tst->ss->store_super(tst, dfd))
643 pr_err("Could not re-write superblock on %s.\n",
644 devname);
645 close(dfd);
646
647 if (strcmp(c->update, "uuid")==0 &&
648 ident->bitmap_fd >= 0 && !bitmap_done) {
649 if (bitmap_update_uuid(ident->bitmap_fd,
650 content->uuid,
651 tst->ss->swapuuid) != 0)
652 pr_err("Could not update uuid on external bitmap.\n");
653 else
654 bitmap_done = 1;
655 }
656 tst->ss->free_super(tst);
657 } else
658 #endif
659 {
660 struct supertype *tst = dup_super(st);
661 int dfd;
662 dfd = dev_open(devname,
663 tmpdev->disposition == 'I'
664 ? O_RDWR : (O_RDWR|O_EXCL));
665
666 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
667 pr_err("cannot re-read metadata from %s - aborting\n",
668 devname);
669 if (dfd >= 0)
670 close(dfd);
671 close(mdfd);
672 free(devices);
673 free(devmap);
674 return -1;
675 }
676 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
677 tst->ss->free_super(tst);
678 close(dfd);
679 }
680
681 stat(devname, &stb);
682
683 if (c->verbose > 0)
684 pr_err("%s is identified as a member of %s, slot %d%s.\n",
685 devname, mddev, content->disk.raid_disk,
686 (content->disk.state & (1<<MD_DISK_REPLACEMENT)) ? " replacement":"");
687 devices[devcnt].devname = devname;
688 devices[devcnt].uptodate = 0;
689 devices[devcnt].included = (tmpdev->disposition == 'I');
690 devices[devcnt].i = *content;
691 devices[devcnt].i.disk.major = major(stb.st_rdev);
692 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
693
694 if (devices[devcnt].i.events
695 > devices[most_recent].i.events &&
696 devices[devcnt].i.disk.state == 6)
697 most_recent = devcnt;
698
699 if (content->array.level == LEVEL_MULTIPATH)
700 /* with multipath, the raid_disk from the superblock is meaningless */
701 i = devcnt;
702 else
703 i = devices[devcnt].i.disk.raid_disk;
704 if (i+1 == 0) {
705 if (nextspare < content->array.raid_disks*2)
706 nextspare = content->array.raid_disks*2;
707 i = nextspare++;
708 } else {
709 /* i is raid_disk - double it so there is room for
710 * replacements */
711 i *= 2;
712 if (devices[devcnt].i.disk.state & (1<<MD_DISK_REPLACEMENT))
713 i++;
714 if (i >= content->array.raid_disks*2 &&
715 i >= nextspare)
716 nextspare = i+1;
717 }
718 if (i < 10000) {
719 if (i >= bestcnt) {
720 int newbestcnt = i+10;
721 int *newbest = xmalloc(sizeof(int)*newbestcnt);
722 int c;
723 for (c=0; c < newbestcnt; c++)
724 if (c < bestcnt)
725 newbest[c] = best[c];
726 else
727 newbest[c] = -1;
728 if (best)free(best);
729 best = newbest;
730 bestcnt = newbestcnt;
731 }
732 if (best[i] >=0 &&
733 devices[best[i]].i.events
734 == devices[devcnt].i.events
735 && (devices[best[i]].i.disk.minor
736 != devices[devcnt].i.disk.minor)
737 && st->ss == &super0
738 && content->array.level != LEVEL_MULTIPATH) {
739 /* two different devices with identical superblock.
740 * Could be a mis-detection caused by overlapping
741 * partitions. fail-safe.
742 */
743 pr_err("WARNING %s and %s appear"
744 " to have very similar superblocks.\n"
745 " If they are really different, "
746 "please --zero the superblock on one\n"
747 " If they are the same or overlap,"
748 " please remove one from %s.\n",
749 devices[best[i]].devname, devname,
750 inargv ? "the list" :
751 "the\n DEVICE list in mdadm.conf"
752 );
753 close(mdfd);
754 free(devices);
755 free(devmap);
756 return -1;
757 }
758 if (best[i] == -1
759 || (devices[best[i]].i.events
760 < devices[devcnt].i.events))
761 best[i] = devcnt;
762 }
763 devcnt++;
764 }
765 *most_recentp = most_recent;
766 *bestcntp = bestcnt;
767 *bestp = best;
768 return devcnt;
769 }
770
771 static int force_array(struct mdinfo *content,
772 struct devs *devices,
773 int *best, int bestcnt, char *avail,
774 int most_recent,
775 struct supertype *st,
776 struct context *c)
777 {
778 int okcnt = 0;
779 while (!enough(content->array.level, content->array.raid_disks,
780 content->array.layout, 1,
781 avail)
782 ||
783 (content->reshape_active && content->delta_disks > 0 &&
784 !enough(content->array.level, (content->array.raid_disks
785 - content->delta_disks),
786 content->new_layout, 1,
787 avail)
788 )) {
789 /* Choose the newest best drive which is
790 * not up-to-date, update the superblock
791 * and add it.
792 */
793 int fd;
794 struct supertype *tst;
795 unsigned long long current_events;
796 int chosen_drive = -1;
797 int i;
798
799 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
800 int j = best[i];
801 if (j>=0 &&
802 !devices[j].uptodate &&
803 devices[j].i.recovery_start == MaxSector &&
804 (chosen_drive < 0 ||
805 devices[j].i.events
806 > devices[chosen_drive].i.events))
807 chosen_drive = j;
808 }
809 if (chosen_drive < 0)
810 break;
811 current_events = devices[chosen_drive].i.events;
812 add_another:
813 if (c->verbose >= 0)
814 pr_err("forcing event count in %s(%d) from %d upto %d\n",
815 devices[chosen_drive].devname,
816 devices[chosen_drive].i.disk.raid_disk,
817 (int)(devices[chosen_drive].i.events),
818 (int)(devices[most_recent].i.events));
819 fd = dev_open(devices[chosen_drive].devname,
820 devices[chosen_drive].included ? O_RDWR
821 : (O_RDWR|O_EXCL));
822 if (fd < 0) {
823 pr_err("Couldn't open %s for write - not updating\n",
824 devices[chosen_drive].devname);
825 devices[chosen_drive].i.events = 0;
826 continue;
827 }
828 tst = dup_super(st);
829 if (tst->ss->load_super(tst,fd, NULL)) {
830 close(fd);
831 pr_err("RAID superblock disappeared from %s - not updating.\n",
832 devices[chosen_drive].devname);
833 devices[chosen_drive].i.events = 0;
834 continue;
835 }
836 content->events = devices[most_recent].i.events;
837 tst->ss->update_super(tst, content, "force-one",
838 devices[chosen_drive].devname, c->verbose,
839 0, NULL);
840
841 if (tst->ss->store_super(tst, fd)) {
842 close(fd);
843 pr_err("Could not re-write superblock on %s\n",
844 devices[chosen_drive].devname);
845 devices[chosen_drive].i.events = 0;
846 tst->ss->free_super(tst);
847 continue;
848 }
849 close(fd);
850 devices[chosen_drive].i.events = devices[most_recent].i.events;
851 devices[chosen_drive].uptodate = 1;
852 avail[chosen_drive] = 1;
853 okcnt++;
854 tst->ss->free_super(tst);
855
856 /* If there are any other drives of the same vintage,
857 * add them in as well. We can't lose and we might gain
858 */
859 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
860 int j = best[i];
861 if (j >= 0 &&
862 !devices[j].uptodate &&
863 devices[j].i.recovery_start == MaxSector &&
864 devices[j].i.events == current_events) {
865 chosen_drive = j;
866 goto add_another;
867 }
868 }
869 }
870 return okcnt;
871 }
872
873 static int start_array(int mdfd,
874 char *mddev,
875 struct mdinfo *content,
876 struct supertype *st,
877 struct mddev_ident *ident,
878 int *best, int bestcnt,
879 int chosen_drive,
880 struct devs *devices,
881 unsigned int okcnt,
882 unsigned int sparecnt,
883 unsigned int rebuilding_cnt,
884 struct context *c,
885 int clean, char *avail,
886 int start_partial_ok,
887 int err_ok,
888 int was_forced
889 )
890 {
891 int rv;
892 int i;
893 unsigned int req_cnt;
894
895 rv = set_array_info(mdfd, st, content);
896 if (rv && !err_ok) {
897 pr_err("failed to set array info for %s: %s\n",
898 mddev, strerror(errno));
899 return 1;
900 }
901 if (ident->bitmap_fd >= 0) {
902 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
903 pr_err("SET_BITMAP_FILE failed.\n");
904 return 1;
905 }
906 } else if (ident->bitmap_file) {
907 /* From config file */
908 int bmfd = open(ident->bitmap_file, O_RDWR);
909 if (bmfd < 0) {
910 pr_err("Could not open bitmap file %s\n",
911 ident->bitmap_file);
912 return 1;
913 }
914 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
915 pr_err("Failed to set bitmapfile for %s\n", mddev);
916 close(bmfd);
917 return 1;
918 }
919 close(bmfd);
920 }
921
922 /* First, add the raid disks, but add the chosen one last */
923 for (i=0; i<= bestcnt; i++) {
924 int j;
925 if (i < bestcnt) {
926 j = best[i];
927 if (j == chosen_drive)
928 continue;
929 } else
930 j = chosen_drive;
931
932 if (j >= 0 && !devices[j].included) {
933 int dfd = dev_open(devices[j].devname,
934 O_RDWR|O_EXCL);
935 if (dfd >= 0) {
936 remove_partitions(dfd);
937 close(dfd);
938 }
939 rv = add_disk(mdfd, st, content, &devices[j].i);
940
941 if (rv) {
942 pr_err("failed to add "
943 "%s to %s: %s\n",
944 devices[j].devname,
945 mddev,
946 strerror(errno));
947 if (i < content->array.raid_disks * 2
948 || i == bestcnt)
949 okcnt--;
950 else
951 sparecnt--;
952 } else if (c->verbose > 0)
953 pr_err("added %s to %s as %d%s%s\n",
954 devices[j].devname, mddev,
955 devices[j].i.disk.raid_disk,
956 devices[j].uptodate?"":
957 " (possibly out of date)",
958 (devices[j].i.disk.state & (1<<MD_DISK_REPLACEMENT))?" replacement":"");
959 } else if (j >= 0) {
960 if (c->verbose > 0)
961 pr_err("%s is already in %s as %d\n",
962 devices[j].devname, mddev,
963 devices[j].i.disk.raid_disk);
964 } else if (c->verbose > 0 && i < content->array.raid_disks*2
965 && (i&1) == 0)
966 pr_err("no uptodate device for slot %d of %s\n",
967 i, mddev);
968 }
969
970 if (content->array.level == LEVEL_CONTAINER) {
971 if (c->verbose >= 0) {
972 pr_err("Container %s has been "
973 "assembled with %d drive%s",
974 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
975 if (okcnt < (unsigned)content->array.raid_disks)
976 fprintf(stderr, " (out of %d)",
977 content->array.raid_disks);
978 fprintf(stderr, "\n");
979 }
980 st->ss->free_super(st);
981 sysfs_uevent(content, "change");
982 return 0;
983 }
984
985 /* Get number of in-sync devices according to the superblock.
986 * We must have this number to start the array without -s or -R
987 */
988 req_cnt = content->array.working_disks;
989
990 if (c->runstop == 1 ||
991 (c->runstop <= 0 &&
992 ( enough(content->array.level, content->array.raid_disks,
993 content->array.layout, clean, avail) &&
994 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
995 ))) {
996 /* This array is good-to-go.
997 * If a reshape is in progress then we might need to
998 * continue monitoring it. In that case we start
999 * it read-only and let the grow code make it writable.
1000 */
1001 int rv;
1002 #ifndef MDASSEMBLE
1003 if (content->reshape_active &&
1004 !(content->reshape_active & RESHAPE_NO_BACKUP) &&
1005 content->delta_disks <= 0) {
1006 rv = sysfs_set_str(content, NULL,
1007 "array_state", "readonly");
1008 if (rv == 0)
1009 rv = Grow_continue(mdfd, st, content,
1010 c->backup_file,
1011 c->freeze_reshape);
1012 } else if (c->readonly &&
1013 sysfs_attribute_available(
1014 content, NULL, "array_state")) {
1015 rv = sysfs_set_str(content, NULL,
1016 "array_state", "readonly");
1017 } else
1018 #endif
1019 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1020 if (rv == 0) {
1021 if (c->verbose >= 0) {
1022 pr_err("%s has been started with %d drive%s",
1023 mddev, okcnt, okcnt==1?"":"s");
1024 if (okcnt < (unsigned)content->array.raid_disks)
1025 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1026 if (rebuilding_cnt)
1027 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1028 if (sparecnt)
1029 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1030 fprintf(stderr, ".\n");
1031 }
1032 if (content->reshape_active &&
1033 content->array.level >= 4 &&
1034 content->array.level <= 6) {
1035 /* might need to increase the size
1036 * of the stripe cache - default is 256
1037 */
1038 if (256 < 4 * (content->array.chunk_size/4096)) {
1039 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
1040 if (sra)
1041 sysfs_set_num(sra, NULL,
1042 "stripe_cache_size",
1043 (4 * content->array.chunk_size / 4096) + 1);
1044 sysfs_free(sra);
1045 }
1046 }
1047 if (okcnt < (unsigned)content->array.raid_disks) {
1048 /* If any devices did not get added
1049 * because the kernel rejected them based
1050 * on event count, try adding them
1051 * again providing the action policy is
1052 * 're-add' or greater. The bitmap
1053 * might allow them to be included, or
1054 * they will become spares.
1055 */
1056 for (i = 0; i < bestcnt; i++) {
1057 int j = best[i];
1058 if (j >= 0 && !devices[j].uptodate) {
1059 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1060 continue;
1061 rv = add_disk(mdfd, st, content,
1062 &devices[j].i);
1063 if (rv == 0 && c->verbose >= 0)
1064 pr_err("%s has been re-added.\n",
1065 devices[j].devname);
1066 }
1067 }
1068 }
1069 if (content->array.level == 6 &&
1070 okcnt + 1 == (unsigned)content->array.raid_disks &&
1071 was_forced) {
1072 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
1073 if (sra)
1074 sysfs_set_str(sra, NULL,
1075 "sync_action", "repair");
1076 sysfs_free(sra);
1077 }
1078 return 0;
1079 }
1080 pr_err("failed to RUN_ARRAY %s: %s\n",
1081 mddev, strerror(errno));
1082
1083 if (!enough(content->array.level, content->array.raid_disks,
1084 content->array.layout, 1, avail))
1085 pr_err("Not enough devices to "
1086 "start the array.\n");
1087 else if (!enough(content->array.level,
1088 content->array.raid_disks,
1089 content->array.layout, clean,
1090 avail))
1091 pr_err("Not enough devices to "
1092 "start the array while not clean "
1093 "- consider --force.\n");
1094
1095 return 1;
1096 }
1097 if (c->runstop == -1) {
1098 pr_err("%s assembled from %d drive%s",
1099 mddev, okcnt, okcnt==1?"":"s");
1100 if (okcnt != (unsigned)content->array.raid_disks)
1101 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1102 fprintf(stderr, ", but not started.\n");
1103 return 2;
1104 }
1105 if (c->verbose >= -1) {
1106 pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1107 if (rebuilding_cnt)
1108 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1109 if (sparecnt)
1110 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1111 if (!enough(content->array.level, content->array.raid_disks,
1112 content->array.layout, 1, avail))
1113 fprintf(stderr, " - not enough to start the array.\n");
1114 else if (!enough(content->array.level,
1115 content->array.raid_disks,
1116 content->array.layout, clean,
1117 avail))
1118 fprintf(stderr, " - not enough to start the "
1119 "array while not clean - consider "
1120 "--force.\n");
1121 else {
1122 if (req_cnt == (unsigned)content->array.raid_disks)
1123 fprintf(stderr, " - need all %d to start it", req_cnt);
1124 else
1125 fprintf(stderr, " - need %d to start", req_cnt);
1126 fprintf(stderr, " (use --run to insist).\n");
1127 }
1128 }
1129 return 1;
1130 }
1131
1132 int Assemble(struct supertype *st, char *mddev,
1133 struct mddev_ident *ident,
1134 struct mddev_dev *devlist,
1135 struct context *c)
1136 {
1137 /*
1138 * The task of Assemble is to find a collection of
1139 * devices that should (according to their superblocks)
1140 * form an array, and to give this collection to the MD driver.
1141 * In Linux-2.4 and later, this involves submitting a
1142 * SET_ARRAY_INFO ioctl with no arg - to prepare
1143 * the array - and then submit a number of
1144 * ADD_NEW_DISK ioctls to add disks into
1145 * the array. Finally RUN_ARRAY might
1146 * be submitted to start the array.
1147 *
1148 * Much of the work of Assemble is in finding and/or
1149 * checking the disks to make sure they look right.
1150 *
1151 * If mddev is not set, then scan must be set and we
1152 * read through the config file for dev+uuid mapping
1153 * We recurse, setting mddev, for each device that
1154 * - isn't running
1155 * - has a valid uuid (or any uuid if !uuidset)
1156 *
1157 * If mddev is set, we try to determine state of md.
1158 * check version - must be at least 0.90.0
1159 * check kernel version. must be at least 2.4.
1160 * If not, we can possibly fall back on START_ARRAY
1161 * Try to GET_ARRAY_INFO.
1162 * If possible, give up
1163 * If not, try to STOP_ARRAY just to make sure
1164 *
1165 * If !uuidset and scan, look in conf-file for uuid
1166 * If not found, give up
1167 * If !devlist and scan and uuidset, get list of devs from conf-file
1168 *
1169 * For each device:
1170 * Check superblock - discard if bad
1171 * Check uuid (set if we don't have one) - discard if no match
1172 * Check superblock similarity if we have a superblock - discard if different
1173 * Record events, devicenum
1174 * This should give us a list of devices for the array
1175 * We should collect the most recent event number
1176 *
1177 * Count disks with recent enough event count
1178 * While force && !enough disks
1179 * Choose newest rejected disks, update event count
1180 * mark clean and rewrite superblock
1181 * If recent kernel:
1182 * SET_ARRAY_INFO
1183 * foreach device with recent events : ADD_NEW_DISK
1184 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1185 * If old kernel:
1186 * Check the device numbers in superblock are right
1187 * update superblock if any changes
1188 * START_ARRAY
1189 *
1190 */
1191 int rv;
1192 int mdfd;
1193 int clean;
1194 int auto_assem = (mddev == NULL && !ident->uuid_set &&
1195 ident->super_minor == UnSet && ident->name[0] == 0
1196 && (ident->container == NULL || ident->member == NULL));
1197 struct devs *devices;
1198 char *devmap;
1199 int *best = NULL; /* indexed by raid_disk */
1200 int bestcnt = 0;
1201 int devcnt;
1202 unsigned int okcnt, sparecnt, rebuilding_cnt, replcnt;
1203 int i;
1204 int was_forced = 0;
1205 int most_recent = 0;
1206 int chosen_drive;
1207 int change = 0;
1208 int inargv = 0;
1209 int start_partial_ok = (c->runstop >= 0) &&
1210 (c->force || devlist==NULL || auto_assem);
1211 int num_devs;
1212 struct mddev_dev *tmpdev;
1213 struct mdinfo info;
1214 struct mdinfo *content = NULL;
1215 struct mdinfo *pre_exist = NULL;
1216 char *avail;
1217 char *name = NULL;
1218 char chosen_name[1024];
1219 struct map_ent *map = NULL;
1220 struct map_ent *mp;
1221
1222 /*
1223 * If any subdevs are listed, then any that don't
1224 * match ident are discarded. Remainder must all match and
1225 * become the array.
1226 * If no subdevs, then we scan all devices in the config file, but
1227 * there must be something in the identity
1228 */
1229
1230 if (!devlist &&
1231 ident->uuid_set == 0 &&
1232 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
1233 ident->name[0] == 0 &&
1234 (ident->container == NULL || ident->member == NULL) &&
1235 ident->devices == NULL) {
1236 pr_err("No identity information available for %s - cannot assemble.\n",
1237 mddev ? mddev : "further assembly");
1238 return 1;
1239 }
1240
1241 if (devlist == NULL)
1242 devlist = conf_get_devs();
1243 else if (mddev)
1244 inargv = 1;
1245
1246 try_again:
1247 /* We come back here when doing auto-assembly and attempting some
1248 * set of devices failed. Those are now marked as ->used==2 and
1249 * we ignore them and try again
1250 */
1251 if (!st && ident->st)
1252 st = ident->st;
1253 if (c->verbose>0)
1254 pr_err("looking for devices for %s\n",
1255 mddev ? mddev : "further assembly");
1256
1257 content = &info;
1258 if (st)
1259 st->ignore_hw_compat = 1;
1260 num_devs = select_devices(devlist, ident, &st, &content, c,
1261 inargv, auto_assem);
1262 if (num_devs < 0)
1263 return 1;
1264
1265 if (!st || !st->sb || !content)
1266 return 2;
1267
1268 /* We have a full set of devices - we now need to find the
1269 * array device.
1270 * However there is a risk that we are racing with "mdadm -I"
1271 * and the array is already partially assembled - we will have
1272 * rejected any devices already in this address.
1273 * So we take a lock on the map file - to prevent further races -
1274 * and look for the uuid in there. If found and the array is
1275 * active, we abort. If found and the array is not active
1276 * we commit to that md device and add all the contained devices
1277 * to our list. We flag them so that we don't try to re-add,
1278 * but can remove if they turn out to not be wanted.
1279 */
1280 if (map_lock(&map))
1281 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1282 mp = map_by_uuid(&map, content->uuid);
1283 if (mp) {
1284 struct mdinfo *dv;
1285 /* array already exists. */
1286 pre_exist = sysfs_read(-1, mp->devnm, GET_LEVEL|GET_DEVS);
1287 if (pre_exist->array.level != UnSet) {
1288 pr_err("Found some drive for an array that is already active: %s\n",
1289 mp->path);
1290 pr_err("giving up.\n");
1291 return 1;
1292 }
1293 for (dv = pre_exist->devs; dv; dv = dv->next) {
1294 /* We want to add this device to our list,
1295 * but it could already be there if "mdadm -I"
1296 * started *after* we checked for O_EXCL.
1297 * If we add it to the top of the list
1298 * it will be preferred over later copies.
1299 */
1300 struct mddev_dev *newdev;
1301 char *devname = map_dev(dv->disk.major,
1302 dv->disk.minor,
1303 0);
1304 if (!devname)
1305 continue;
1306 newdev = xmalloc(sizeof(*newdev));
1307 newdev->devname = devname;
1308 newdev->disposition = 'I';
1309 newdev->used = 1;
1310 newdev->next = devlist;
1311 devlist = newdev;
1312 num_devs++;
1313 }
1314 strcpy(chosen_name, mp->path);
1315 if (c->verbose > 0 || mddev == NULL ||
1316 strcmp(mddev, chosen_name) != 0)
1317 pr_err("Merging with already-assembled %s\n",
1318 chosen_name);
1319 mdfd = open_dev_excl(mp->devnm);
1320 } else {
1321 int trustworthy = FOREIGN;
1322 name = content->name;
1323 switch (st->ss->match_home(st, c->homehost)
1324 ?: st->ss->match_home(st, "any")) {
1325 case 1:
1326 trustworthy = LOCAL;
1327 name = strchr(content->name, ':');
1328 if (name)
1329 name++;
1330 else
1331 name = content->name;
1332 break;
1333 }
1334 if (!auto_assem)
1335 /* If the array is listed in mdadm.conf or on
1336 * command line, then we trust the name
1337 * even if the array doesn't look local
1338 */
1339 trustworthy = LOCAL;
1340
1341 if (name[0] == 0 &&
1342 content->array.level == LEVEL_CONTAINER) {
1343 name = content->text_version;
1344 trustworthy = METADATA;
1345 }
1346
1347 if (name[0] && trustworthy != LOCAL &&
1348 ! c->require_homehost &&
1349 conf_name_is_free(name))
1350 trustworthy = LOCAL;
1351
1352 if (trustworthy == LOCAL &&
1353 strchr(name, ':'))
1354 /* Ignore 'host:' prefix of name */
1355 name = strchr(name, ':')+1;
1356
1357 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
1358 chosen_name);
1359 }
1360 if (mdfd < 0) {
1361 st->ss->free_super(st);
1362 if (auto_assem)
1363 goto try_again;
1364 return 1;
1365 }
1366 mddev = chosen_name;
1367 if (get_linux_version() < 2004000 ||
1368 md_get_version(mdfd) < 9000) {
1369 pr_err("Assemble requires Linux 2.4 or later, and\n"
1370 " md driver version 0.90.0 or later.\n"
1371 " Upgrade your kernel or try --build\n");
1372 close(mdfd);
1373 return 1;
1374 }
1375 if (pre_exist == NULL) {
1376 if (mddev_busy(fd2devnm(mdfd))) {
1377 pr_err("%s already active, cannot restart it!\n",
1378 mddev);
1379 for (tmpdev = devlist ;
1380 tmpdev && tmpdev->used != 1;
1381 tmpdev = tmpdev->next)
1382 ;
1383 if (tmpdev && auto_assem)
1384 pr_err("%s needed for %s...\n",
1385 mddev, tmpdev->devname);
1386 close(mdfd);
1387 mdfd = -3;
1388 st->ss->free_super(st);
1389 if (auto_assem)
1390 goto try_again;
1391 return 1;
1392 }
1393 /* just incase it was started but has no content */
1394 ioctl(mdfd, STOP_ARRAY, NULL);
1395 }
1396
1397 #ifndef MDASSEMBLE
1398 if (content != &info) {
1399 /* This is a member of a container. Try starting the array. */
1400 int err;
1401 err = assemble_container_content(st, mdfd, content, c,
1402 chosen_name);
1403 close(mdfd);
1404 return err;
1405 }
1406 #endif
1407 /* Ok, no bad inconsistancy, we can try updating etc */
1408 devices = xcalloc(num_devs, sizeof(*devices));
1409 devmap = xcalloc(num_devs, content->array.raid_disks);
1410 devcnt = load_devices(devices, devmap, ident, st, devlist,
1411 c, content, mdfd, mddev,
1412 &most_recent, &bestcnt, &best, inargv);
1413 if (devcnt < 0)
1414 return 1;
1415
1416 if (devcnt == 0) {
1417 pr_err("no devices found for %s\n",
1418 mddev);
1419 if (st)
1420 st->ss->free_super(st);
1421 close(mdfd);
1422 free(devices);
1423 free(devmap);
1424 return 1;
1425 }
1426
1427 if (c->update && strcmp(c->update, "byteorder")==0)
1428 st->minor_version = 90;
1429
1430 st->ss->getinfo_super(st, content, NULL);
1431 clean = content->array.state & 1;
1432
1433 /* now we have some devices that might be suitable.
1434 * I wonder how many
1435 */
1436 avail = xcalloc(content->array.raid_disks, 1);
1437 okcnt = 0;
1438 replcnt = 0;
1439 sparecnt=0;
1440 rebuilding_cnt=0;
1441 for (i=0; i< bestcnt; i++) {
1442 int j = best[i];
1443 int event_margin = 1; /* always allow a difference of '1'
1444 * like the kernel does
1445 */
1446 if (j < 0) continue;
1447 /* note: we ignore error flags in multipath arrays
1448 * as they don't make sense
1449 */
1450 if (content->array.level != LEVEL_MULTIPATH)
1451 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
1452 if (!(devices[j].i.disk.state
1453 & (1<<MD_DISK_FAULTY))) {
1454 devices[j].uptodate = 1;
1455 sparecnt++;
1456 }
1457 continue;
1458 }
1459 /* If this device thinks that 'most_recent' has failed, then
1460 * we must reject this device.
1461 */
1462 if (j != most_recent &&
1463 content->array.raid_disks > 0 &&
1464 devices[most_recent].i.disk.raid_disk >= 0 &&
1465 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
1466 if (c->verbose > -1)
1467 pr_err("ignoring %s as it reports %s as failed\n",
1468 devices[j].devname, devices[most_recent].devname);
1469 best[i] = -1;
1470 continue;
1471 }
1472 /* Require event counter to be same as, or just less than,
1473 * most recent. If it is bigger, it must be a stray spare and
1474 * should be ignored.
1475 */
1476 if (devices[j].i.events+event_margin >=
1477 devices[most_recent].i.events &&
1478 devices[j].i.events <=
1479 devices[most_recent].i.events
1480 ) {
1481 devices[j].uptodate = 1;
1482 if (i < content->array.raid_disks * 2) {
1483 if (devices[j].i.recovery_start == MaxSector ||
1484 (content->reshape_active &&
1485 ((i >= content->array.raid_disks - content->delta_disks) ||
1486 (i >= content->array.raid_disks - content->delta_disks - 1
1487 && content->array.level == 4)))) {
1488 if (!avail[i/2]) {
1489 okcnt++;
1490 avail[i/2]=1;
1491 } else
1492 replcnt++;
1493 } else
1494 rebuilding_cnt++;
1495 } else
1496 sparecnt++;
1497 }
1498 }
1499 free(devmap);
1500 if (c->force) {
1501 int force_ok = force_array(content, devices, best, bestcnt,
1502 avail, most_recent, st, c);
1503 okcnt += force_ok;
1504 if (force_ok)
1505 was_forced = 1;
1506 }
1507 /* Now we want to look at the superblock which the kernel will base things on
1508 * and compare the devices that we think are working with the devices that the
1509 * superblock thinks are working.
1510 * If there are differences and --force is given, then update this chosen
1511 * superblock.
1512 */
1513 chosen_drive = -1;
1514 st->ss->free_super(st);
1515 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
1516 int j = best[i];
1517 int fd;
1518
1519 if (j<0)
1520 continue;
1521 if (!devices[j].uptodate)
1522 continue;
1523 if (devices[j].i.events < devices[most_recent].i.events)
1524 continue;
1525 chosen_drive = j;
1526 if ((fd=dev_open(devices[j].devname,
1527 devices[j].included ? O_RDONLY
1528 : (O_RDONLY|O_EXCL)))< 0) {
1529 pr_err("Cannot open %s: %s\n",
1530 devices[j].devname, strerror(errno));
1531 close(mdfd);
1532 free(devices);
1533 return 1;
1534 }
1535 if (st->ss->load_super(st,fd, NULL)) {
1536 close(fd);
1537 pr_err("RAID superblock has disappeared from %s\n",
1538 devices[j].devname);
1539 close(mdfd);
1540 free(devices);
1541 return 1;
1542 }
1543 close(fd);
1544 }
1545 if (st->sb == NULL) {
1546 pr_err("No suitable drives found for %s\n", mddev);
1547 close(mdfd);
1548 free(devices);
1549 return 1;
1550 }
1551 st->ss->getinfo_super(st, content, NULL);
1552 #ifndef MDASSEMBLE
1553 sysfs_init(content, mdfd, NULL);
1554 #endif
1555 for (i=0; i<bestcnt; i++) {
1556 int j = best[i];
1557 unsigned int desired_state;
1558
1559 if (i >= content->array.raid_disks * 2)
1560 desired_state = 0;
1561 else if (i & 1)
1562 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_REPLACEMENT);
1563 else
1564 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1565
1566 if (j<0)
1567 continue;
1568 if (!devices[j].uptodate)
1569 continue;
1570
1571 devices[j].i.disk.state = desired_state;
1572 if (!(devices[j].i.array.state & 1))
1573 clean = 0;
1574
1575 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
1576 c->verbose, 0, NULL)) {
1577 if (c->force) {
1578 if (c->verbose >= 0)
1579 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1580 j, mddev, devices[j].devname);
1581 change = 1;
1582 } else {
1583 if (c->verbose >= -1)
1584 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1585 i, mddev, devices[j].devname);
1586 }
1587 }
1588 #if 0
1589 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
1590 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1591 i, mddev);
1592 }
1593 #endif
1594 }
1595 if (c->force && !clean &&
1596 !enough(content->array.level, content->array.raid_disks,
1597 content->array.layout, clean,
1598 avail)) {
1599 change += st->ss->update_super(st, content, "force-array",
1600 devices[chosen_drive].devname, c->verbose,
1601 0, NULL);
1602 was_forced = 1;
1603 clean = 1;
1604 }
1605
1606 if (change) {
1607 int fd;
1608 fd = dev_open(devices[chosen_drive].devname,
1609 devices[chosen_drive].included ?
1610 O_RDWR : (O_RDWR|O_EXCL));
1611 if (fd < 0) {
1612 pr_err("Could not open %s for write - cannot Assemble array.\n",
1613 devices[chosen_drive].devname);
1614 close(mdfd);
1615 free(devices);
1616 return 1;
1617 }
1618 if (st->ss->store_super(st, fd)) {
1619 close(fd);
1620 pr_err("Could not re-write superblock on %s\n",
1621 devices[chosen_drive].devname);
1622 close(mdfd);
1623 free(devices);
1624 return 1;
1625 }
1626 if (c->verbose >= 0)
1627 pr_err("Marking array %s as 'clean'\n",
1628 mddev);
1629 close(fd);
1630 }
1631
1632 /* If we are in the middle of a reshape we may need to restore saved data
1633 * that was moved aside due to the reshape overwriting live data
1634 * The code of doing this lives in Grow.c
1635 */
1636 #ifndef MDASSEMBLE
1637 if (content->reshape_active &&
1638 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
1639 int err = 0;
1640 int *fdlist = xmalloc(sizeof(int)* bestcnt);
1641 if (c->verbose > 0)
1642 pr_err(":%s has an active reshape - checking "
1643 "if critical section needs to be restored\n",
1644 chosen_name);
1645 enable_fds(bestcnt/2);
1646 for (i = 0; i < bestcnt/2; i++) {
1647 int j = best[i*2];
1648 if (j >= 0) {
1649 fdlist[i] = dev_open(devices[j].devname,
1650 devices[j].included
1651 ? O_RDWR : (O_RDWR|O_EXCL));
1652 if (fdlist[i] < 0) {
1653 pr_err("Could not open %s for write - cannot Assemble array.\n",
1654 devices[j].devname);
1655 err = 1;
1656 break;
1657 }
1658 } else
1659 fdlist[i] = -1;
1660 }
1661 if (!err) {
1662 if (st->ss->external && st->ss->recover_backup)
1663 err = st->ss->recover_backup(st, content);
1664 else
1665 err = Grow_restart(st, content, fdlist, bestcnt/2,
1666 c->backup_file, c->verbose > 0);
1667 if (err && c->invalid_backup) {
1668 if (c->verbose > 0)
1669 pr_err("continuing"
1670 " without restoring backup\n");
1671 err = 0;
1672 }
1673 }
1674 while (i>0) {
1675 i--;
1676 if (fdlist[i]>=0) close(fdlist[i]);
1677 }
1678 free(fdlist);
1679 if (err) {
1680 pr_err("Failed to restore critical section for reshape, sorry.\n");
1681 if (c->backup_file == NULL)
1682 cont_err("Possibly you needed to specify the --backup-file\n");
1683 close(mdfd);
1684 free(devices);
1685 return err;
1686 }
1687 }
1688 #endif
1689
1690 /* Almost ready to actually *do* something */
1691 /* First, fill in the map, so that udev can find our name
1692 * as soon as we become active.
1693 */
1694 if (c->update && strcmp(c->update, "metadata")==0) {
1695 content->array.major_version = 1;
1696 content->array.minor_version = 0;
1697 strcpy(content->text_version, "1.0");
1698 }
1699
1700 map_update(&map, fd2devnm(mdfd), content->text_version,
1701 content->uuid, chosen_name);
1702
1703 rv = start_array(mdfd, mddev, content,
1704 st, ident, best, bestcnt,
1705 chosen_drive, devices, okcnt, sparecnt,
1706 rebuilding_cnt,
1707 c,
1708 clean, avail, start_partial_ok,
1709 pre_exist != NULL,
1710 was_forced);
1711 if (rv == 1 && !pre_exist)
1712 ioctl(mdfd, STOP_ARRAY, NULL);
1713 free(devices);
1714 map_unlock(&map);
1715 if (rv == 0) {
1716 wait_for(chosen_name, mdfd);
1717 close(mdfd);
1718 if (auto_assem) {
1719 int usecs = 1;
1720 /* There is a nasty race with 'mdadm --monitor'.
1721 * If it opens this device before we close it,
1722 * it gets an incomplete open on which IO
1723 * doesn't work and the capacity is
1724 * wrong.
1725 * If we reopen (to check for layered devices)
1726 * before --monitor closes, we loose.
1727 *
1728 * So: wait upto 1 second for there to be
1729 * a non-zero capacity.
1730 */
1731 while (usecs < 1000) {
1732 mdfd = open(mddev, O_RDONLY);
1733 if (mdfd >= 0) {
1734 unsigned long long size;
1735 if (get_dev_size(mdfd, NULL, &size) &&
1736 size > 0)
1737 break;
1738 close(mdfd);
1739 }
1740 usleep(usecs);
1741 usecs <<= 1;
1742 }
1743 }
1744 } else
1745 close(mdfd);
1746
1747 /* '2' means 'OK, but not started yet' */
1748 return rv == 2 ? 0 : rv;
1749 }
1750
1751 #ifndef MDASSEMBLE
1752 int assemble_container_content(struct supertype *st, int mdfd,
1753 struct mdinfo *content, struct context *c,
1754 char *chosen_name)
1755 {
1756 struct mdinfo *dev, *sra;
1757 int working = 0, preexist = 0;
1758 int expansion = 0;
1759 struct map_ent *map = NULL;
1760 int old_raid_disks;
1761 int start_reshape;
1762
1763 sysfs_init(content, mdfd, NULL);
1764
1765 sra = sysfs_read(mdfd, NULL, GET_VERSION);
1766 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
1767 if (content->array.major_version == -1 &&
1768 content->array.minor_version == -2 &&
1769 c->readonly &&
1770 content->text_version[0] == '/')
1771 content->text_version[0] = '-';
1772 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1773 if (sra)
1774 sysfs_free(sra);
1775 return 1;
1776 }
1777 }
1778
1779 /* There are two types of reshape: container wide or sub-array specific
1780 * Check if metadata requests blocking container wide reshapes
1781 */
1782 start_reshape = (content->reshape_active &&
1783 !((content->reshape_active == CONTAINER_RESHAPE) &&
1784 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1785
1786 /* Block subarray here if it is under reshape now
1787 * Do not allow for any changes in this array
1788 */
1789 if (st->ss->external && content->recovery_blocked && start_reshape)
1790 block_subarray(content);
1791
1792 if (sra)
1793 sysfs_free(sra);
1794 old_raid_disks = content->array.raid_disks - content->delta_disks;
1795 for (dev = content->devs; dev; dev = dev->next)
1796 if (sysfs_add_disk(content, dev, 1) == 0) {
1797 if (dev->disk.raid_disk >= old_raid_disks &&
1798 content->reshape_active)
1799 expansion++;
1800 else
1801 working++;
1802 } else if (errno == EEXIST)
1803 preexist++;
1804 if (working + expansion == 0)
1805 return 1;/* Nothing new, don't try to start */
1806
1807 map_update(&map, fd2devnm(mdfd),
1808 content->text_version,
1809 content->uuid, chosen_name);
1810
1811 if (c->runstop > 0 ||
1812 (working + preexist + expansion) >=
1813 content->array.working_disks) {
1814 int err;
1815
1816 if (start_reshape) {
1817 int spare = content->array.raid_disks + expansion;
1818 if (restore_backup(st, content,
1819 working,
1820 spare, c->backup_file, c->verbose) == 1)
1821 return 1;
1822
1823 err = sysfs_set_str(content, NULL,
1824 "array_state", "readonly");
1825 if (err)
1826 return 1;
1827
1828 if (st->ss->external) {
1829 if (!mdmon_running(st->container_devnm))
1830 start_mdmon(st->container_devnm);
1831 ping_monitor(st->container_devnm);
1832 if (mdmon_running(st->container_devnm) &&
1833 st->update_tail == NULL)
1834 st->update_tail = &st->updates;
1835 }
1836
1837 err = Grow_continue(mdfd, st, content, c->backup_file,
1838 c->freeze_reshape);
1839 } else switch(content->array.level) {
1840 case LEVEL_LINEAR:
1841 case LEVEL_MULTIPATH:
1842 case 0:
1843 err = sysfs_set_str(content, NULL, "array_state",
1844 c->readonly ? "readonly" : "active");
1845 break;
1846 default:
1847 err = sysfs_set_str(content, NULL, "array_state",
1848 "readonly");
1849 /* start mdmon if needed. */
1850 if (!err) {
1851 if (!mdmon_running(st->container_devnm))
1852 start_mdmon(st->container_devnm);
1853 ping_monitor(st->container_devnm);
1854 }
1855 break;
1856 }
1857 if (!err)
1858 sysfs_set_safemode(content, content->safe_mode_delay);
1859
1860 /* Block subarray here if it is not reshaped now
1861 * It has be blocked a little later to allow mdmon to switch in
1862 * in to R/W state
1863 */
1864 if (st->ss->external && content->recovery_blocked &&
1865 !start_reshape)
1866 block_subarray(content);
1867
1868 if (c->verbose >= 0) {
1869 if (err)
1870 pr_err("array %s now has %d device%s",
1871 chosen_name, working + preexist,
1872 working + preexist == 1 ? "":"s");
1873 else
1874 pr_err("Started %s with %d device%s",
1875 chosen_name, working + preexist,
1876 working + preexist == 1 ? "":"s");
1877 if (preexist)
1878 fprintf(stderr, " (%d new)", working);
1879 if (expansion)
1880 fprintf(stderr, " ( + %d for expansion)",
1881 expansion);
1882 fprintf(stderr, "\n");
1883 }
1884 if (!err)
1885 wait_for(chosen_name, mdfd);
1886 return err;
1887 /* FIXME should have an O_EXCL and wait for read-auto */
1888 } else {
1889 if (c->verbose >= 0) {
1890 pr_err("%s assembled with %d device%s",
1891 chosen_name, preexist + working,
1892 preexist + working == 1 ? "":"s");
1893 if (preexist)
1894 fprintf(stderr, " (%d new)", working);
1895 fprintf(stderr, " but not started\n");
1896 }
1897 return 1;
1898 }
1899 }
1900 #endif