]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
94e314b78040190faec3d6c1fd9d20546123f1d3
[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 num_devs = select_devices(devlist, ident, &st, &content, c,
1223 inargv, auto_assem);
1224 if (num_devs < 0)
1225 return 1;
1226
1227 if (!st || !st->sb || !content)
1228 return 2;
1229
1230 /* We have a full set of devices - we now need to find the
1231 * array device.
1232 * However there is a risk that we are racing with "mdadm -I"
1233 * and the array is already partially assembled - we will have
1234 * rejected any devices already in this address.
1235 * So we take a lock on the map file - to prevent further races -
1236 * and look for the uuid in there. If found and the array is
1237 * active, we abort. If found and the array is not active
1238 * we commit to that md device and add all the contained devices
1239 * to our list. We flag them so that we don't try to re-add,
1240 * but can remove if they turn out to not be wanted.
1241 */
1242 if (map_lock(&map))
1243 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1244 mp = map_by_uuid(&map, content->uuid);
1245 if (mp) {
1246 struct mdinfo *dv;
1247 /* array already exists. */
1248 pre_exist = sysfs_read(-1, mp->devnum, GET_LEVEL|GET_DEVS);
1249 if (pre_exist->array.level != UnSet) {
1250 pr_err("Found some drive for an array that is already active: %s\n",
1251 mp->path);
1252 pr_err("giving up.\n");
1253 return 1;
1254 }
1255 for (dv = pre_exist->devs; dv; dv = dv->next) {
1256 /* We want to add this device to our list,
1257 * but it could already be there if "mdadm -I"
1258 * started *after* we checked for O_EXCL.
1259 * If we add it to the top of the list
1260 * it will be preferred over later copies.
1261 */
1262 struct mddev_dev *newdev;
1263 char *devname = map_dev(dv->disk.major,
1264 dv->disk.minor,
1265 0);
1266 if (!devname)
1267 continue;
1268 newdev = xmalloc(sizeof(*newdev));
1269 newdev->devname = devname;
1270 newdev->disposition = 'I';
1271 newdev->used = 1;
1272 newdev->next = devlist;
1273 devlist = newdev;
1274 num_devs++;
1275 }
1276 strcpy(chosen_name, mp->path);
1277 if (c->verbose > 0 || mddev == NULL ||
1278 strcmp(mddev, chosen_name) != 0)
1279 pr_err("Merging with already-assembled %s\n",
1280 chosen_name);
1281 mdfd = open_dev_excl(mp->devnum);
1282 } else {
1283 int trustworthy = FOREIGN;
1284 name = content->name;
1285 switch (st->ss->match_home(st, c->homehost)
1286 ?: st->ss->match_home(st, "any")) {
1287 case 1:
1288 trustworthy = LOCAL;
1289 name = strchr(content->name, ':');
1290 if (name)
1291 name++;
1292 else
1293 name = content->name;
1294 break;
1295 }
1296 if (!auto_assem)
1297 /* If the array is listed in mdadm.conf or on
1298 * command line, then we trust the name
1299 * even if the array doesn't look local
1300 */
1301 trustworthy = LOCAL;
1302
1303 if (name[0] == 0 &&
1304 content->array.level == LEVEL_CONTAINER) {
1305 name = content->text_version;
1306 trustworthy = METADATA;
1307 }
1308
1309 if (name[0] && trustworthy != LOCAL &&
1310 ! c->require_homehost &&
1311 conf_name_is_free(name))
1312 trustworthy = LOCAL;
1313
1314 if (trustworthy == LOCAL &&
1315 strchr(name, ':'))
1316 /* Ignore 'host:' prefix of name */
1317 name = strchr(name, ':')+1;
1318
1319 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
1320 chosen_name);
1321 }
1322 if (mdfd < 0) {
1323 st->ss->free_super(st);
1324 if (auto_assem)
1325 goto try_again;
1326 return 1;
1327 }
1328 mddev = chosen_name;
1329 if (get_linux_version() < 2004000 ||
1330 md_get_version(mdfd) < 9000) {
1331 pr_err("Assemble requires Linux 2.4 or later, and\n"
1332 " md driver version 0.90.0 or later.\n"
1333 " Upgrade your kernel or try --build\n");
1334 close(mdfd);
1335 return 1;
1336 }
1337 if (pre_exist == NULL) {
1338 if (mddev_busy(fd2devnum(mdfd))) {
1339 pr_err("%s already active, cannot restart it!\n",
1340 mddev);
1341 for (tmpdev = devlist ;
1342 tmpdev && tmpdev->used != 1;
1343 tmpdev = tmpdev->next)
1344 ;
1345 if (tmpdev && auto_assem)
1346 pr_err("%s needed for %s...\n",
1347 mddev, tmpdev->devname);
1348 close(mdfd);
1349 mdfd = -3;
1350 st->ss->free_super(st);
1351 if (auto_assem)
1352 goto try_again;
1353 return 1;
1354 }
1355 /* just incase it was started but has no content */
1356 ioctl(mdfd, STOP_ARRAY, NULL);
1357 }
1358
1359 #ifndef MDASSEMBLE
1360 if (content != &info) {
1361 /* This is a member of a container. Try starting the array. */
1362 int err;
1363 err = assemble_container_content(st, mdfd, content, c,
1364 chosen_name);
1365 close(mdfd);
1366 return err;
1367 }
1368 #endif
1369 /* Ok, no bad inconsistancy, we can try updating etc */
1370 devices = xcalloc(num_devs, sizeof(*devices));
1371 devmap = xcalloc(num_devs, content->array.raid_disks);
1372 devcnt = load_devices(devices, devmap, ident, st, devlist,
1373 c, content, mdfd, mddev,
1374 &most_recent, &bestcnt, &best, inargv);
1375 if (devcnt < 0)
1376 return 1;
1377
1378 if (devcnt == 0) {
1379 pr_err("no devices found for %s\n",
1380 mddev);
1381 if (st)
1382 st->ss->free_super(st);
1383 close(mdfd);
1384 free(devices);
1385 free(devmap);
1386 return 1;
1387 }
1388
1389 if (c->update && strcmp(c->update, "byteorder")==0)
1390 st->minor_version = 90;
1391
1392 st->ss->getinfo_super(st, content, NULL);
1393 clean = content->array.state & 1;
1394
1395 /* now we have some devices that might be suitable.
1396 * I wonder how many
1397 */
1398 avail = xcalloc(content->array.raid_disks, 1);
1399 okcnt = 0;
1400 replcnt = 0;
1401 sparecnt=0;
1402 rebuilding_cnt=0;
1403 for (i=0; i< bestcnt; i++) {
1404 int j = best[i];
1405 int event_margin = 1; /* always allow a difference of '1'
1406 * like the kernel does
1407 */
1408 if (j < 0) continue;
1409 /* note: we ignore error flags in multipath arrays
1410 * as they don't make sense
1411 */
1412 if (content->array.level != LEVEL_MULTIPATH)
1413 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
1414 if (!(devices[j].i.disk.state
1415 & (1<<MD_DISK_FAULTY))) {
1416 devices[j].uptodate = 1;
1417 sparecnt++;
1418 }
1419 continue;
1420 }
1421 /* If this device thinks that 'most_recent' has failed, then
1422 * we must reject this device.
1423 */
1424 if (j != most_recent &&
1425 content->array.raid_disks > 0 &&
1426 devices[most_recent].i.disk.raid_disk >= 0 &&
1427 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
1428 if (c->verbose > -1)
1429 pr_err("ignoring %s as it reports %s as failed\n",
1430 devices[j].devname, devices[most_recent].devname);
1431 best[i] = -1;
1432 continue;
1433 }
1434 if (devices[j].i.events+event_margin >=
1435 devices[most_recent].i.events) {
1436 devices[j].uptodate = 1;
1437 if (i < content->array.raid_disks * 2) {
1438 if (devices[j].i.recovery_start == MaxSector ||
1439 (content->reshape_active &&
1440 ((i >= content->array.raid_disks - content->delta_disks) ||
1441 (i >= content->array.raid_disks - content->delta_disks - 1
1442 && content->array.level == 4)))) {
1443 if (!avail[i/2]) {
1444 okcnt++;
1445 avail[i/2]=1;
1446 } else
1447 replcnt++;
1448 } else
1449 rebuilding_cnt++;
1450 } else
1451 sparecnt++;
1452 }
1453 }
1454 free(devmap);
1455 if (c->force)
1456 okcnt += force_array(content, devices, best, bestcnt,
1457 avail, most_recent, st, c);
1458
1459 /* Now we want to look at the superblock which the kernel will base things on
1460 * and compare the devices that we think are working with the devices that the
1461 * superblock thinks are working.
1462 * If there are differences and --force is given, then update this chosen
1463 * superblock.
1464 */
1465 chosen_drive = -1;
1466 st->ss->free_super(st);
1467 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
1468 int j = best[i];
1469 int fd;
1470
1471 if (j<0)
1472 continue;
1473 if (!devices[j].uptodate)
1474 continue;
1475 if (devices[j].i.events < devices[most_recent].i.events)
1476 continue;
1477 chosen_drive = j;
1478 if ((fd=dev_open(devices[j].devname,
1479 devices[j].included ? O_RDONLY
1480 : (O_RDONLY|O_EXCL)))< 0) {
1481 pr_err("Cannot open %s: %s\n",
1482 devices[j].devname, strerror(errno));
1483 close(mdfd);
1484 free(devices);
1485 return 1;
1486 }
1487 if (st->ss->load_super(st,fd, NULL)) {
1488 close(fd);
1489 pr_err("RAID superblock has disappeared from %s\n",
1490 devices[j].devname);
1491 close(mdfd);
1492 free(devices);
1493 return 1;
1494 }
1495 close(fd);
1496 }
1497 if (st->sb == NULL) {
1498 pr_err("No suitable drives found for %s\n", mddev);
1499 close(mdfd);
1500 free(devices);
1501 return 1;
1502 }
1503 st->ss->getinfo_super(st, content, NULL);
1504 #ifndef MDASSEMBLE
1505 sysfs_init(content, mdfd, 0);
1506 #endif
1507 for (i=0; i<bestcnt; i++) {
1508 int j = best[i];
1509 unsigned int desired_state;
1510
1511 if (i >= content->array.raid_disks * 2)
1512 desired_state = 0;
1513 else if (i & 1)
1514 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_REPLACEMENT);
1515 else
1516 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1517
1518 if (j<0)
1519 continue;
1520 if (!devices[j].uptodate)
1521 continue;
1522
1523 devices[j].i.disk.state = desired_state;
1524 if (!(devices[j].i.array.state & 1))
1525 clean = 0;
1526
1527 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
1528 c->verbose, 0, NULL)) {
1529 if (c->force) {
1530 if (c->verbose >= 0)
1531 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1532 j, mddev, devices[j].devname);
1533 change = 1;
1534 } else {
1535 if (c->verbose >= -1)
1536 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1537 i, mddev, devices[j].devname);
1538 }
1539 }
1540 #if 0
1541 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
1542 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1543 i, mddev);
1544 }
1545 #endif
1546 }
1547 if (c->force && !clean &&
1548 !enough(content->array.level, content->array.raid_disks,
1549 content->array.layout, clean,
1550 avail)) {
1551 change += st->ss->update_super(st, content, "force-array",
1552 devices[chosen_drive].devname, c->verbose,
1553 0, NULL);
1554 clean = 1;
1555 }
1556
1557 if (change) {
1558 int fd;
1559 fd = dev_open(devices[chosen_drive].devname,
1560 devices[chosen_drive].included ?
1561 O_RDWR : (O_RDWR|O_EXCL));
1562 if (fd < 0) {
1563 pr_err("Could not open %s for write - cannot Assemble array.\n",
1564 devices[chosen_drive].devname);
1565 close(mdfd);
1566 free(devices);
1567 return 1;
1568 }
1569 if (st->ss->store_super(st, fd)) {
1570 close(fd);
1571 pr_err("Could not re-write superblock on %s\n",
1572 devices[chosen_drive].devname);
1573 close(mdfd);
1574 free(devices);
1575 return 1;
1576 }
1577 if (c->verbose >= 0)
1578 pr_err("Marking array %s as 'clean'\n",
1579 mddev);
1580 close(fd);
1581 }
1582
1583 /* If we are in the middle of a reshape we may need to restore saved data
1584 * that was moved aside due to the reshape overwriting live data
1585 * The code of doing this lives in Grow.c
1586 */
1587 #ifndef MDASSEMBLE
1588 if (content->reshape_active &&
1589 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
1590 int err = 0;
1591 int *fdlist = xmalloc(sizeof(int)* bestcnt);
1592 if (c->verbose > 0)
1593 pr_err(":%s has an active reshape - checking "
1594 "if critical section needs to be restored\n",
1595 chosen_name);
1596 for (i=0; i<bestcnt; i++) {
1597 int j = best[i];
1598 if (j >= 0) {
1599 fdlist[i] = dev_open(devices[j].devname,
1600 devices[j].included
1601 ? O_RDWR : (O_RDWR|O_EXCL));
1602 if (fdlist[i] < 0) {
1603 pr_err("Could not open %s for write - cannot Assemble array.\n",
1604 devices[j].devname);
1605 err = 1;
1606 break;
1607 }
1608 } else
1609 fdlist[i] = -1;
1610 }
1611 if (!err) {
1612 if (st->ss->external && st->ss->recover_backup)
1613 err = st->ss->recover_backup(st, content);
1614 else
1615 err = Grow_restart(st, content, fdlist, bestcnt,
1616 c->backup_file, c->verbose > 0);
1617 if (err && c->invalid_backup) {
1618 if (c->verbose > 0)
1619 pr_err("continuing"
1620 " without restoring backup\n");
1621 err = 0;
1622 }
1623 }
1624 while (i>0) {
1625 i--;
1626 if (fdlist[i]>=0) close(fdlist[i]);
1627 }
1628 free(fdlist);
1629 if (err) {
1630 pr_err("Failed to restore critical section for reshape, sorry.\n");
1631 if (c->backup_file == NULL)
1632 cont_err("Possibly you needed to specify the --backup-file\n");
1633 close(mdfd);
1634 free(devices);
1635 return err;
1636 }
1637 }
1638 #endif
1639
1640 /* Almost ready to actually *do* something */
1641 /* First, fill in the map, so that udev can find our name
1642 * as soon as we become active.
1643 */
1644 map_update(&map, fd2devnum(mdfd), content->text_version,
1645 content->uuid, chosen_name);
1646
1647 rv = start_array(mdfd, mddev, content,
1648 st, ident, best, bestcnt,
1649 chosen_drive, devices, okcnt, sparecnt,
1650 rebuilding_cnt,
1651 c,
1652 clean, avail, start_partial_ok);
1653 if (rv == 1 && !pre_exist)
1654 ioctl(mdfd, STOP_ARRAY, NULL);
1655 close(mdfd);
1656 free(devices);
1657 map_unlock(&map);
1658 if (rv == 0) {
1659 wait_for(chosen_name, mdfd);
1660 if (auto_assem) {
1661 int usecs = 1;
1662 /* There is a nasty race with 'mdadm --monitor'.
1663 * If it opens this device before we close it,
1664 * it gets an incomplete open on which IO
1665 * doesn't work and the capacity is
1666 * wrong.
1667 * If we reopen (to check for layered devices)
1668 * before --monitor closes, we loose.
1669 *
1670 * So: wait upto 1 second for there to be
1671 * a non-zero capacity.
1672 */
1673 while (usecs < 1000) {
1674 mdfd = open(mddev, O_RDONLY);
1675 if (mdfd >= 0) {
1676 unsigned long long size;
1677 if (get_dev_size(mdfd, NULL, &size) &&
1678 size > 0)
1679 break;
1680 close(mdfd);
1681 }
1682 usleep(usecs);
1683 usecs <<= 1;
1684 }
1685 }
1686 }
1687 /* '2' means 'OK, but not started yet' */
1688 return rv == 2 ? 0 : rv;
1689 }
1690
1691 #ifndef MDASSEMBLE
1692 int assemble_container_content(struct supertype *st, int mdfd,
1693 struct mdinfo *content, struct context *c,
1694 char *chosen_name)
1695 {
1696 struct mdinfo *dev, *sra;
1697 int working = 0, preexist = 0;
1698 int expansion = 0;
1699 struct map_ent *map = NULL;
1700 int old_raid_disks;
1701 int start_reshape;
1702
1703 sysfs_init(content, mdfd, 0);
1704
1705 sra = sysfs_read(mdfd, 0, GET_VERSION);
1706 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
1707 if (content->array.major_version == -1 &&
1708 content->array.minor_version == -2 &&
1709 c->readonly &&
1710 content->text_version[0] == '/')
1711 content->text_version[0] = '-';
1712 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1713 if (sra)
1714 sysfs_free(sra);
1715 return 1;
1716 }
1717 }
1718
1719 /* There are two types of reshape: container wide or sub-array specific
1720 * Check if metadata requests blocking container wide reshapes
1721 */
1722 start_reshape = (content->reshape_active &&
1723 !((content->reshape_active == CONTAINER_RESHAPE) &&
1724 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1725
1726 /* Block subarray here if it is under reshape now
1727 * Do not allow for any changes in this array
1728 */
1729 if (st->ss->external && content->recovery_blocked && start_reshape)
1730 block_subarray(content);
1731
1732 if (sra)
1733 sysfs_free(sra);
1734 old_raid_disks = content->array.raid_disks - content->delta_disks;
1735 for (dev = content->devs; dev; dev = dev->next)
1736 if (sysfs_add_disk(content, dev, 1) == 0) {
1737 if (dev->disk.raid_disk >= old_raid_disks &&
1738 content->reshape_active)
1739 expansion++;
1740 else
1741 working++;
1742 } else if (errno == EEXIST)
1743 preexist++;
1744 if (working + expansion == 0)
1745 return 1;/* Nothing new, don't try to start */
1746
1747 map_update(&map, fd2devnum(mdfd),
1748 content->text_version,
1749 content->uuid, chosen_name);
1750
1751 if (c->runstop > 0 ||
1752 (working + preexist + expansion) >=
1753 content->array.working_disks) {
1754 int err;
1755
1756 if (start_reshape) {
1757 int spare = content->array.raid_disks + expansion;
1758 if (restore_backup(st, content,
1759 working,
1760 spare, c->backup_file, c->verbose) == 1)
1761 return 1;
1762
1763 err = sysfs_set_str(content, NULL,
1764 "array_state", "readonly");
1765 if (err)
1766 return 1;
1767
1768 if (st->ss->external) {
1769 if (!mdmon_running(st->container_dev))
1770 start_mdmon(st->container_dev);
1771 ping_monitor_by_id(st->container_dev);
1772 if (mdmon_running(st->container_dev) &&
1773 st->update_tail == NULL)
1774 st->update_tail = &st->updates;
1775 }
1776
1777 err = Grow_continue(mdfd, st, content, c->backup_file,
1778 c->freeze_reshape);
1779 } else switch(content->array.level) {
1780 case LEVEL_LINEAR:
1781 case LEVEL_MULTIPATH:
1782 case 0:
1783 err = sysfs_set_str(content, NULL, "array_state",
1784 c->readonly ? "readonly" : "active");
1785 break;
1786 default:
1787 err = sysfs_set_str(content, NULL, "array_state",
1788 "readonly");
1789 /* start mdmon if needed. */
1790 if (!err) {
1791 if (!mdmon_running(st->container_dev))
1792 start_mdmon(st->container_dev);
1793 ping_monitor_by_id(st->container_dev);
1794 }
1795 break;
1796 }
1797 if (!err)
1798 sysfs_set_safemode(content, content->safe_mode_delay);
1799
1800 /* Block subarray here if it is not reshaped now
1801 * It has be blocked a little later to allow mdmon to switch in
1802 * in to R/W state
1803 */
1804 if (st->ss->external && content->recovery_blocked &&
1805 !start_reshape)
1806 block_subarray(content);
1807
1808 if (c->verbose >= 0) {
1809 if (err)
1810 pr_err("array %s now has %d device%s",
1811 chosen_name, working + preexist,
1812 working + preexist == 1 ? "":"s");
1813 else
1814 pr_err("Started %s with %d device%s",
1815 chosen_name, working + preexist,
1816 working + preexist == 1 ? "":"s");
1817 if (preexist)
1818 fprintf(stderr, " (%d new)", working);
1819 if (expansion)
1820 fprintf(stderr, " ( + %d for expansion)",
1821 expansion);
1822 fprintf(stderr, "\n");
1823 }
1824 if (!err)
1825 wait_for(chosen_name, mdfd);
1826 return err;
1827 /* FIXME should have an O_EXCL and wait for read-auto */
1828 } else {
1829 if (c->verbose >= 0) {
1830 pr_err("%s assembled with %d device%s",
1831 chosen_name, preexist + working,
1832 preexist + working == 1 ? "":"s");
1833 if (preexist)
1834 fprintf(stderr, " (%d new)", working);
1835 fprintf(stderr, " but not started\n");
1836 }
1837 return 1;
1838 }
1839 }
1840 #endif
1841