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