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