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