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