]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Assemble: don't ever consider a 'spare' to be the 'most recent'.
[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(0, 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 = -1;
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.disk.state == 6) {
699 if (most_recent < 0 ||
700 devices[devcnt].i.events
701 > devices[most_recent].i.events)
702 most_recent = devcnt;
703 }
704
705 if (content->array.level == LEVEL_MULTIPATH)
706 /* with multipath, the raid_disk from the superblock is meaningless */
707 i = devcnt;
708 else
709 i = devices[devcnt].i.disk.raid_disk;
710 if (i+1 == 0) {
711 if (nextspare < content->array.raid_disks*2)
712 nextspare = content->array.raid_disks*2;
713 i = nextspare++;
714 } else {
715 /* i is raid_disk - double it so there is room for
716 * replacements */
717 i *= 2;
718 if (devices[devcnt].i.disk.state & (1<<MD_DISK_REPLACEMENT))
719 i++;
720 if (i >= content->array.raid_disks*2 &&
721 i >= nextspare)
722 nextspare = i+1;
723 }
724 if (i < 10000) {
725 if (i >= bestcnt) {
726 int newbestcnt = i+10;
727 int *newbest = xmalloc(sizeof(int)*newbestcnt);
728 int c;
729 for (c=0; c < newbestcnt; c++)
730 if (c < bestcnt)
731 newbest[c] = best[c];
732 else
733 newbest[c] = -1;
734 if (best)free(best);
735 best = newbest;
736 bestcnt = newbestcnt;
737 }
738 if (best[i] >=0 &&
739 devices[best[i]].i.events
740 == devices[devcnt].i.events
741 && (devices[best[i]].i.disk.minor
742 != devices[devcnt].i.disk.minor)
743 && st->ss == &super0
744 && content->array.level != LEVEL_MULTIPATH) {
745 /* two different devices with identical superblock.
746 * Could be a mis-detection caused by overlapping
747 * partitions. fail-safe.
748 */
749 pr_err("WARNING %s and %s appear"
750 " to have very similar superblocks.\n"
751 " If they are really different, "
752 "please --zero the superblock on one\n"
753 " If they are the same or overlap,"
754 " please remove one from %s.\n",
755 devices[best[i]].devname, devname,
756 inargv ? "the list" :
757 "the\n DEVICE list in mdadm.conf"
758 );
759 close(mdfd);
760 free(devices);
761 free(devmap);
762 return -1;
763 }
764 if (best[i] == -1
765 || (devices[best[i]].i.events
766 < devices[devcnt].i.events))
767 best[i] = devcnt;
768 }
769 devcnt++;
770 }
771 if (most_recent >= 0)
772 *most_recentp = most_recent;
773 *bestcntp = bestcnt;
774 *bestp = best;
775 return devcnt;
776 }
777
778 static int force_array(struct mdinfo *content,
779 struct devs *devices,
780 int *best, int bestcnt, char *avail,
781 int most_recent,
782 struct supertype *st,
783 struct context *c)
784 {
785 int okcnt = 0;
786 while (!enough(content->array.level, content->array.raid_disks,
787 content->array.layout, 1,
788 avail)
789 ||
790 (content->reshape_active && content->delta_disks > 0 &&
791 !enough(content->array.level, (content->array.raid_disks
792 - content->delta_disks),
793 content->new_layout, 1,
794 avail)
795 )) {
796 /* Choose the newest best drive which is
797 * not up-to-date, update the superblock
798 * and add it.
799 */
800 int fd;
801 struct supertype *tst;
802 unsigned long long current_events;
803 int chosen_drive = -1;
804 int i;
805
806 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
807 int j = best[i];
808 if (j>=0 &&
809 !devices[j].uptodate &&
810 devices[j].i.recovery_start == MaxSector &&
811 (chosen_drive < 0 ||
812 devices[j].i.events
813 > devices[chosen_drive].i.events))
814 chosen_drive = j;
815 }
816 if (chosen_drive < 0)
817 break;
818 current_events = devices[chosen_drive].i.events;
819 add_another:
820 if (c->verbose >= 0)
821 pr_err("forcing event count in %s(%d) from %d upto %d\n",
822 devices[chosen_drive].devname,
823 devices[chosen_drive].i.disk.raid_disk,
824 (int)(devices[chosen_drive].i.events),
825 (int)(devices[most_recent].i.events));
826 fd = dev_open(devices[chosen_drive].devname,
827 devices[chosen_drive].included ? O_RDWR
828 : (O_RDWR|O_EXCL));
829 if (fd < 0) {
830 pr_err("Couldn't open %s for write - not updating\n",
831 devices[chosen_drive].devname);
832 devices[chosen_drive].i.events = 0;
833 continue;
834 }
835 tst = dup_super(st);
836 if (tst->ss->load_super(tst,fd, NULL)) {
837 close(fd);
838 pr_err("RAID superblock disappeared from %s - not updating.\n",
839 devices[chosen_drive].devname);
840 devices[chosen_drive].i.events = 0;
841 continue;
842 }
843 content->events = devices[most_recent].i.events;
844 tst->ss->update_super(tst, content, "force-one",
845 devices[chosen_drive].devname, c->verbose,
846 0, NULL);
847
848 if (tst->ss->store_super(tst, fd)) {
849 close(fd);
850 pr_err("Could not re-write superblock on %s\n",
851 devices[chosen_drive].devname);
852 devices[chosen_drive].i.events = 0;
853 tst->ss->free_super(tst);
854 continue;
855 }
856 close(fd);
857 devices[chosen_drive].i.events = devices[most_recent].i.events;
858 devices[chosen_drive].uptodate = 1;
859 avail[chosen_drive] = 1;
860 okcnt++;
861 tst->ss->free_super(tst);
862
863 /* If there are any other drives of the same vintage,
864 * add them in as well. We can't lose and we might gain
865 */
866 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
867 int j = best[i];
868 if (j >= 0 &&
869 !devices[j].uptodate &&
870 devices[j].i.recovery_start == MaxSector &&
871 devices[j].i.events == current_events) {
872 chosen_drive = j;
873 goto add_another;
874 }
875 }
876 }
877 return okcnt;
878 }
879
880 static int start_array(int mdfd,
881 char *mddev,
882 struct mdinfo *content,
883 struct supertype *st,
884 struct mddev_ident *ident,
885 int *best, int bestcnt,
886 int chosen_drive,
887 struct devs *devices,
888 unsigned int okcnt,
889 unsigned int sparecnt,
890 unsigned int rebuilding_cnt,
891 struct context *c,
892 int clean, char *avail,
893 int start_partial_ok,
894 int err_ok,
895 int was_forced
896 )
897 {
898 int rv;
899 int i;
900 unsigned int req_cnt;
901
902 rv = set_array_info(mdfd, st, content);
903 if (rv && !err_ok) {
904 pr_err("failed to set array info for %s: %s\n",
905 mddev, strerror(errno));
906 return 1;
907 }
908 if (ident->bitmap_fd >= 0) {
909 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
910 pr_err("SET_BITMAP_FILE failed.\n");
911 return 1;
912 }
913 } else if (ident->bitmap_file) {
914 /* From config file */
915 int bmfd = open(ident->bitmap_file, O_RDWR);
916 if (bmfd < 0) {
917 pr_err("Could not open bitmap file %s\n",
918 ident->bitmap_file);
919 return 1;
920 }
921 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
922 pr_err("Failed to set bitmapfile for %s\n", mddev);
923 close(bmfd);
924 return 1;
925 }
926 close(bmfd);
927 }
928
929 /* First, add the raid disks, but add the chosen one last */
930 for (i=0; i<= bestcnt; i++) {
931 int j;
932 if (i < bestcnt) {
933 j = best[i];
934 if (j == chosen_drive)
935 continue;
936 } else
937 j = chosen_drive;
938
939 if (j >= 0 && !devices[j].included) {
940 int dfd = dev_open(devices[j].devname,
941 O_RDWR|O_EXCL);
942 if (dfd >= 0) {
943 remove_partitions(dfd);
944 close(dfd);
945 }
946 rv = add_disk(mdfd, st, content, &devices[j].i);
947
948 if (rv) {
949 pr_err("failed to add "
950 "%s to %s: %s\n",
951 devices[j].devname,
952 mddev,
953 strerror(errno));
954 if (i < content->array.raid_disks * 2
955 || i == bestcnt)
956 okcnt--;
957 else
958 sparecnt--;
959 } else if (c->verbose > 0)
960 pr_err("added %s to %s as %d%s%s\n",
961 devices[j].devname, mddev,
962 devices[j].i.disk.raid_disk,
963 devices[j].uptodate?"":
964 " (possibly out of date)",
965 (devices[j].i.disk.state & (1<<MD_DISK_REPLACEMENT))?" replacement":"");
966 } else if (j >= 0) {
967 if (c->verbose > 0)
968 pr_err("%s is already in %s as %d\n",
969 devices[j].devname, mddev,
970 devices[j].i.disk.raid_disk);
971 } else if (c->verbose > 0 && i < content->array.raid_disks*2
972 && (i&1) == 0)
973 pr_err("no uptodate device for slot %d of %s\n",
974 i, mddev);
975 }
976
977 if (content->array.level == LEVEL_CONTAINER) {
978 if (c->verbose >= 0) {
979 pr_err("Container %s has been "
980 "assembled with %d drive%s",
981 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
982 if (okcnt < (unsigned)content->array.raid_disks)
983 fprintf(stderr, " (out of %d)",
984 content->array.raid_disks);
985 fprintf(stderr, "\n");
986 }
987 st->ss->free_super(st);
988 sysfs_uevent(content, "change");
989 return 0;
990 }
991
992 /* Get number of in-sync devices according to the superblock.
993 * We must have this number to start the array without -s or -R
994 */
995 req_cnt = content->array.working_disks;
996
997 if (c->runstop == 1 ||
998 (c->runstop <= 0 &&
999 ( enough(content->array.level, content->array.raid_disks,
1000 content->array.layout, clean, avail) &&
1001 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
1002 ))) {
1003 /* This array is good-to-go.
1004 * If a reshape is in progress then we might need to
1005 * continue monitoring it. In that case we start
1006 * it read-only and let the grow code make it writable.
1007 */
1008 int rv;
1009 #ifndef MDASSEMBLE
1010 if (content->reshape_active &&
1011 !(content->reshape_active & RESHAPE_NO_BACKUP) &&
1012 content->delta_disks <= 0) {
1013 if (!c->backup_file) {
1014 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1015 mddev);
1016 pr_err("Please provided one with \"--backup-file=...\"\n");
1017 if (c->update &&
1018 strcmp(c->update, "revert-reshape") == 0)
1019 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1020 return 1;
1021 }
1022 rv = sysfs_set_str(content, NULL,
1023 "array_state", "readonly");
1024 if (rv == 0)
1025 rv = Grow_continue(mdfd, st, content,
1026 c->backup_file,
1027 c->freeze_reshape);
1028 } else if (c->readonly &&
1029 sysfs_attribute_available(
1030 content, NULL, "array_state")) {
1031 rv = sysfs_set_str(content, NULL,
1032 "array_state", "readonly");
1033 } else
1034 #endif
1035 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1036 if (rv == 0) {
1037 if (c->verbose >= 0) {
1038 pr_err("%s has been started with %d drive%s",
1039 mddev, okcnt, okcnt==1?"":"s");
1040 if (okcnt < (unsigned)content->array.raid_disks)
1041 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1042 if (rebuilding_cnt)
1043 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1044 if (sparecnt)
1045 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1046 fprintf(stderr, ".\n");
1047 }
1048 if (content->reshape_active &&
1049 content->array.level >= 4 &&
1050 content->array.level <= 6) {
1051 /* might need to increase the size
1052 * of the stripe cache - default is 256
1053 */
1054 if (256 < 4 * (content->array.chunk_size/4096)) {
1055 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
1056 if (sra)
1057 sysfs_set_num(sra, NULL,
1058 "stripe_cache_size",
1059 (4 * content->array.chunk_size / 4096) + 1);
1060 sysfs_free(sra);
1061 }
1062 }
1063 if (okcnt < (unsigned)content->array.raid_disks) {
1064 /* If any devices did not get added
1065 * because the kernel rejected them based
1066 * on event count, try adding them
1067 * again providing the action policy is
1068 * 're-add' or greater. The bitmap
1069 * might allow them to be included, or
1070 * they will become spares.
1071 */
1072 for (i = 0; i < bestcnt; i++) {
1073 int j = best[i];
1074 if (j >= 0 && !devices[j].uptodate) {
1075 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1076 continue;
1077 rv = add_disk(mdfd, st, content,
1078 &devices[j].i);
1079 if (rv == 0 && c->verbose >= 0)
1080 pr_err("%s has been re-added.\n",
1081 devices[j].devname);
1082 }
1083 }
1084 }
1085 if (content->array.level == 6 &&
1086 okcnt + 1 == (unsigned)content->array.raid_disks &&
1087 was_forced) {
1088 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
1089 if (sra)
1090 sysfs_set_str(sra, NULL,
1091 "sync_action", "repair");
1092 sysfs_free(sra);
1093 }
1094 return 0;
1095 }
1096 pr_err("failed to RUN_ARRAY %s: %s\n",
1097 mddev, strerror(errno));
1098
1099 if (!enough(content->array.level, content->array.raid_disks,
1100 content->array.layout, 1, avail))
1101 pr_err("Not enough devices to "
1102 "start the array.\n");
1103 else if (!enough(content->array.level,
1104 content->array.raid_disks,
1105 content->array.layout, clean,
1106 avail))
1107 pr_err("Not enough devices to "
1108 "start the array while not clean "
1109 "- consider --force.\n");
1110
1111 return 1;
1112 }
1113 if (c->runstop == -1) {
1114 pr_err("%s assembled from %d drive%s",
1115 mddev, okcnt, okcnt==1?"":"s");
1116 if (okcnt != (unsigned)content->array.raid_disks)
1117 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1118 fprintf(stderr, ", but not started.\n");
1119 return 2;
1120 }
1121 if (c->verbose >= -1) {
1122 pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1123 if (rebuilding_cnt)
1124 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1125 if (sparecnt)
1126 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1127 if (!enough(content->array.level, content->array.raid_disks,
1128 content->array.layout, 1, avail))
1129 fprintf(stderr, " - not enough to start the array.\n");
1130 else if (!enough(content->array.level,
1131 content->array.raid_disks,
1132 content->array.layout, clean,
1133 avail))
1134 fprintf(stderr, " - not enough to start the "
1135 "array while not clean - consider "
1136 "--force.\n");
1137 else {
1138 if (req_cnt == (unsigned)content->array.raid_disks)
1139 fprintf(stderr, " - need all %d to start it", req_cnt);
1140 else
1141 fprintf(stderr, " - need %d to start", req_cnt);
1142 fprintf(stderr, " (use --run to insist).\n");
1143 }
1144 }
1145 return 1;
1146 }
1147
1148 int Assemble(struct supertype *st, char *mddev,
1149 struct mddev_ident *ident,
1150 struct mddev_dev *devlist,
1151 struct context *c)
1152 {
1153 /*
1154 * The task of Assemble is to find a collection of
1155 * devices that should (according to their superblocks)
1156 * form an array, and to give this collection to the MD driver.
1157 * In Linux-2.4 and later, this involves submitting a
1158 * SET_ARRAY_INFO ioctl with no arg - to prepare
1159 * the array - and then submit a number of
1160 * ADD_NEW_DISK ioctls to add disks into
1161 * the array. Finally RUN_ARRAY might
1162 * be submitted to start the array.
1163 *
1164 * Much of the work of Assemble is in finding and/or
1165 * checking the disks to make sure they look right.
1166 *
1167 * If mddev is not set, then scan must be set and we
1168 * read through the config file for dev+uuid mapping
1169 * We recurse, setting mddev, for each device that
1170 * - isn't running
1171 * - has a valid uuid (or any uuid if !uuidset)
1172 *
1173 * If mddev is set, we try to determine state of md.
1174 * check version - must be at least 0.90.0
1175 * check kernel version. must be at least 2.4.
1176 * If not, we can possibly fall back on START_ARRAY
1177 * Try to GET_ARRAY_INFO.
1178 * If possible, give up
1179 * If not, try to STOP_ARRAY just to make sure
1180 *
1181 * If !uuidset and scan, look in conf-file for uuid
1182 * If not found, give up
1183 * If !devlist and scan and uuidset, get list of devs from conf-file
1184 *
1185 * For each device:
1186 * Check superblock - discard if bad
1187 * Check uuid (set if we don't have one) - discard if no match
1188 * Check superblock similarity if we have a superblock - discard if different
1189 * Record events, devicenum
1190 * This should give us a list of devices for the array
1191 * We should collect the most recent event number
1192 *
1193 * Count disks with recent enough event count
1194 * While force && !enough disks
1195 * Choose newest rejected disks, update event count
1196 * mark clean and rewrite superblock
1197 * If recent kernel:
1198 * SET_ARRAY_INFO
1199 * foreach device with recent events : ADD_NEW_DISK
1200 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1201 * If old kernel:
1202 * Check the device numbers in superblock are right
1203 * update superblock if any changes
1204 * START_ARRAY
1205 *
1206 */
1207 int rv;
1208 int mdfd;
1209 int clean;
1210 int auto_assem = (mddev == NULL && !ident->uuid_set &&
1211 ident->super_minor == UnSet && ident->name[0] == 0
1212 && (ident->container == NULL || ident->member == NULL));
1213 struct devs *devices;
1214 char *devmap;
1215 int *best = NULL; /* indexed by raid_disk */
1216 int bestcnt = 0;
1217 int devcnt;
1218 unsigned int okcnt, sparecnt, rebuilding_cnt, replcnt;
1219 int i;
1220 int was_forced = 0;
1221 int most_recent = 0;
1222 int chosen_drive;
1223 int change = 0;
1224 int inargv = 0;
1225 int start_partial_ok = (c->runstop >= 0) &&
1226 (c->force || devlist==NULL || auto_assem);
1227 int num_devs;
1228 struct mddev_dev *tmpdev;
1229 struct mdinfo info;
1230 struct mdinfo *content = NULL;
1231 struct mdinfo *pre_exist = NULL;
1232 char *avail;
1233 char *name = NULL;
1234 char chosen_name[1024];
1235 struct map_ent *map = NULL;
1236 struct map_ent *mp;
1237
1238 /*
1239 * If any subdevs are listed, then any that don't
1240 * match ident are discarded. Remainder must all match and
1241 * become the array.
1242 * If no subdevs, then we scan all devices in the config file, but
1243 * there must be something in the identity
1244 */
1245
1246 if (!devlist &&
1247 ident->uuid_set == 0 &&
1248 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
1249 ident->name[0] == 0 &&
1250 (ident->container == NULL || ident->member == NULL) &&
1251 ident->devices == NULL) {
1252 pr_err("No identity information available for %s - cannot assemble.\n",
1253 mddev ? mddev : "further assembly");
1254 return 1;
1255 }
1256
1257 if (devlist == NULL)
1258 devlist = conf_get_devs();
1259 else if (mddev)
1260 inargv = 1;
1261
1262 try_again:
1263 /* We come back here when doing auto-assembly and attempting some
1264 * set of devices failed. Those are now marked as ->used==2 and
1265 * we ignore them and try again
1266 */
1267 if (!st && ident->st)
1268 st = ident->st;
1269 if (c->verbose>0)
1270 pr_err("looking for devices for %s\n",
1271 mddev ? mddev : "further assembly");
1272
1273 content = &info;
1274 if (st)
1275 st->ignore_hw_compat = 1;
1276 num_devs = select_devices(devlist, ident, &st, &content, c,
1277 inargv, auto_assem);
1278 if (num_devs < 0)
1279 return 1;
1280
1281 if (!st || !st->sb || !content)
1282 return 2;
1283
1284 /* We have a full set of devices - we now need to find the
1285 * array device.
1286 * However there is a risk that we are racing with "mdadm -I"
1287 * and the array is already partially assembled - we will have
1288 * rejected any devices already in this address.
1289 * So we take a lock on the map file - to prevent further races -
1290 * and look for the uuid in there. If found and the array is
1291 * active, we abort. If found and the array is not active
1292 * we commit to that md device and add all the contained devices
1293 * to our list. We flag them so that we don't try to re-add,
1294 * but can remove if they turn out to not be wanted.
1295 */
1296 if (map_lock(&map))
1297 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1298 mp = map_by_uuid(&map, content->uuid);
1299 if (mp) {
1300 struct mdinfo *dv;
1301 /* array already exists. */
1302 pre_exist = sysfs_read(-1, mp->devnm, GET_LEVEL|GET_DEVS);
1303 if (pre_exist->array.level != UnSet) {
1304 pr_err("Found some drive for an array that is already active: %s\n",
1305 mp->path);
1306 pr_err("giving up.\n");
1307 return 1;
1308 }
1309 for (dv = pre_exist->devs; dv; dv = dv->next) {
1310 /* We want to add this device to our list,
1311 * but it could already be there if "mdadm -I"
1312 * started *after* we checked for O_EXCL.
1313 * If we add it to the top of the list
1314 * it will be preferred over later copies.
1315 */
1316 struct mddev_dev *newdev;
1317 char *devname = map_dev(dv->disk.major,
1318 dv->disk.minor,
1319 0);
1320 if (!devname)
1321 continue;
1322 newdev = xmalloc(sizeof(*newdev));
1323 newdev->devname = devname;
1324 newdev->disposition = 'I';
1325 newdev->used = 1;
1326 newdev->next = devlist;
1327 devlist = newdev;
1328 num_devs++;
1329 }
1330 strcpy(chosen_name, mp->path);
1331 if (c->verbose > 0 || mddev == NULL ||
1332 strcmp(mddev, chosen_name) != 0)
1333 pr_err("Merging with already-assembled %s\n",
1334 chosen_name);
1335 mdfd = open_dev_excl(mp->devnm);
1336 } else {
1337 int trustworthy = FOREIGN;
1338 name = content->name;
1339 switch (st->ss->match_home(st, c->homehost)
1340 ?: st->ss->match_home(st, "any")) {
1341 case 1:
1342 trustworthy = LOCAL;
1343 name = strchr(content->name, ':');
1344 if (name)
1345 name++;
1346 else
1347 name = content->name;
1348 break;
1349 }
1350 if (!auto_assem)
1351 /* If the array is listed in mdadm.conf or on
1352 * command line, then we trust the name
1353 * even if the array doesn't look local
1354 */
1355 trustworthy = LOCAL;
1356
1357 if (name[0] == 0 &&
1358 content->array.level == LEVEL_CONTAINER) {
1359 name = content->text_version;
1360 trustworthy = METADATA;
1361 }
1362
1363 if (name[0] && trustworthy != LOCAL &&
1364 ! c->require_homehost &&
1365 conf_name_is_free(name))
1366 trustworthy = LOCAL;
1367
1368 if (trustworthy == LOCAL &&
1369 strchr(name, ':'))
1370 /* Ignore 'host:' prefix of name */
1371 name = strchr(name, ':')+1;
1372
1373 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
1374 chosen_name);
1375 }
1376 if (mdfd < 0) {
1377 st->ss->free_super(st);
1378 if (auto_assem)
1379 goto try_again;
1380 return 1;
1381 }
1382 mddev = chosen_name;
1383 if (get_linux_version() < 2004000 ||
1384 md_get_version(mdfd) < 9000) {
1385 pr_err("Assemble requires Linux 2.4 or later, and\n"
1386 " md driver version 0.90.0 or later.\n"
1387 " Upgrade your kernel or try --build\n");
1388 close(mdfd);
1389 return 1;
1390 }
1391 if (pre_exist == NULL) {
1392 if (mddev_busy(fd2devnm(mdfd))) {
1393 pr_err("%s already active, cannot restart it!\n",
1394 mddev);
1395 for (tmpdev = devlist ;
1396 tmpdev && tmpdev->used != 1;
1397 tmpdev = tmpdev->next)
1398 ;
1399 if (tmpdev && auto_assem)
1400 pr_err("%s needed for %s...\n",
1401 mddev, tmpdev->devname);
1402 close(mdfd);
1403 mdfd = -3;
1404 st->ss->free_super(st);
1405 if (auto_assem)
1406 goto try_again;
1407 return 1;
1408 }
1409 /* just incase it was started but has no content */
1410 ioctl(mdfd, STOP_ARRAY, NULL);
1411 }
1412
1413 #ifndef MDASSEMBLE
1414 if (content != &info) {
1415 /* This is a member of a container. Try starting the array. */
1416 int err;
1417 err = assemble_container_content(st, mdfd, content, c,
1418 chosen_name);
1419 close(mdfd);
1420 return err;
1421 }
1422 #endif
1423 /* Ok, no bad inconsistancy, we can try updating etc */
1424 devices = xcalloc(num_devs, sizeof(*devices));
1425 devmap = xcalloc(num_devs, content->array.raid_disks);
1426 devcnt = load_devices(devices, devmap, ident, st, devlist,
1427 c, content, mdfd, mddev,
1428 &most_recent, &bestcnt, &best, inargv);
1429 if (devcnt < 0)
1430 return 1;
1431
1432 if (devcnt == 0) {
1433 pr_err("no devices found for %s\n",
1434 mddev);
1435 if (st)
1436 st->ss->free_super(st);
1437 close(mdfd);
1438 free(devices);
1439 free(devmap);
1440 return 1;
1441 }
1442
1443 if (c->update && strcmp(c->update, "byteorder")==0)
1444 st->minor_version = 90;
1445
1446 st->ss->getinfo_super(st, content, NULL);
1447 clean = content->array.state & 1;
1448
1449 /* now we have some devices that might be suitable.
1450 * I wonder how many
1451 */
1452 avail = xcalloc(content->array.raid_disks, 1);
1453 okcnt = 0;
1454 replcnt = 0;
1455 sparecnt=0;
1456 rebuilding_cnt=0;
1457 for (i=0; i< bestcnt; i++) {
1458 int j = best[i];
1459 int event_margin = 1; /* always allow a difference of '1'
1460 * like the kernel does
1461 */
1462 if (j < 0) continue;
1463 /* note: we ignore error flags in multipath arrays
1464 * as they don't make sense
1465 */
1466 if (content->array.level != LEVEL_MULTIPATH)
1467 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
1468 if (!(devices[j].i.disk.state
1469 & (1<<MD_DISK_FAULTY))) {
1470 devices[j].uptodate = 1;
1471 sparecnt++;
1472 }
1473 continue;
1474 }
1475 /* If this device thinks that 'most_recent' has failed, then
1476 * we must reject this device.
1477 */
1478 if (j != most_recent && !c->force &&
1479 content->array.raid_disks > 0 &&
1480 devices[most_recent].i.disk.raid_disk >= 0 &&
1481 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
1482 if (c->verbose > -1)
1483 pr_err("ignoring %s as it reports %s as failed\n",
1484 devices[j].devname, devices[most_recent].devname);
1485 best[i] = -1;
1486 continue;
1487 }
1488 /* Require event counter to be same as, or just less than,
1489 * most recent. If it is bigger, it must be a stray spare and
1490 * should be ignored.
1491 */
1492 if (devices[j].i.events+event_margin >=
1493 devices[most_recent].i.events &&
1494 devices[j].i.events <=
1495 devices[most_recent].i.events
1496 ) {
1497 devices[j].uptodate = 1;
1498 if (i < content->array.raid_disks * 2) {
1499 if (devices[j].i.recovery_start == MaxSector ||
1500 (content->reshape_active &&
1501 ((i >= content->array.raid_disks - content->delta_disks) ||
1502 (i >= content->array.raid_disks - content->delta_disks - 1
1503 && content->array.level == 4)))) {
1504 if (!avail[i/2]) {
1505 okcnt++;
1506 avail[i/2]=1;
1507 } else
1508 replcnt++;
1509 } else
1510 rebuilding_cnt++;
1511 } else
1512 sparecnt++;
1513 }
1514 }
1515 free(devmap);
1516 if (c->force) {
1517 int force_ok = force_array(content, devices, best, bestcnt,
1518 avail, most_recent, st, c);
1519 okcnt += force_ok;
1520 if (force_ok)
1521 was_forced = 1;
1522 }
1523 /* Now we want to look at the superblock which the kernel will base things on
1524 * and compare the devices that we think are working with the devices that the
1525 * superblock thinks are working.
1526 * If there are differences and --force is given, then update this chosen
1527 * superblock.
1528 */
1529 chosen_drive = -1;
1530 st->ss->free_super(st);
1531 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
1532 int j = best[i];
1533 int fd;
1534
1535 if (j<0)
1536 continue;
1537 if (!devices[j].uptodate)
1538 continue;
1539 if (devices[j].i.events < devices[most_recent].i.events)
1540 continue;
1541 chosen_drive = j;
1542 if ((fd=dev_open(devices[j].devname,
1543 devices[j].included ? O_RDONLY
1544 : (O_RDONLY|O_EXCL)))< 0) {
1545 pr_err("Cannot open %s: %s\n",
1546 devices[j].devname, strerror(errno));
1547 close(mdfd);
1548 free(devices);
1549 return 1;
1550 }
1551 if (st->ss->load_super(st,fd, NULL)) {
1552 close(fd);
1553 pr_err("RAID superblock has disappeared from %s\n",
1554 devices[j].devname);
1555 close(mdfd);
1556 free(devices);
1557 return 1;
1558 }
1559 close(fd);
1560 }
1561 if (st->sb == NULL) {
1562 pr_err("No suitable drives found for %s\n", mddev);
1563 close(mdfd);
1564 free(devices);
1565 return 1;
1566 }
1567 st->ss->getinfo_super(st, content, NULL);
1568 #ifndef MDASSEMBLE
1569 sysfs_init(content, mdfd, NULL);
1570 #endif
1571 for (i=0; i<bestcnt; i++) {
1572 int j = best[i];
1573 unsigned int desired_state;
1574
1575 if (i >= content->array.raid_disks * 2)
1576 desired_state = 0;
1577 else if (i & 1)
1578 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_REPLACEMENT);
1579 else
1580 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1581
1582 if (j<0)
1583 continue;
1584 if (!devices[j].uptodate)
1585 continue;
1586
1587 devices[j].i.disk.state = desired_state;
1588 if (!(devices[j].i.array.state & 1))
1589 clean = 0;
1590
1591 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
1592 c->verbose, 0, NULL)) {
1593 if (c->force) {
1594 if (c->verbose >= 0)
1595 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
1596 j, mddev, devices[j].devname);
1597 change = 1;
1598 } else {
1599 if (c->verbose >= -1)
1600 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
1601 i, mddev, devices[j].devname);
1602 }
1603 }
1604 #if 0
1605 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
1606 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1607 i, mddev);
1608 }
1609 #endif
1610 }
1611 if (c->force && !clean &&
1612 !enough(content->array.level, content->array.raid_disks,
1613 content->array.layout, clean,
1614 avail)) {
1615 change += st->ss->update_super(st, content, "force-array",
1616 devices[chosen_drive].devname, c->verbose,
1617 0, NULL);
1618 was_forced = 1;
1619 clean = 1;
1620 }
1621
1622 if (change) {
1623 int fd;
1624 fd = dev_open(devices[chosen_drive].devname,
1625 devices[chosen_drive].included ?
1626 O_RDWR : (O_RDWR|O_EXCL));
1627 if (fd < 0) {
1628 pr_err("Could not open %s for write - cannot Assemble array.\n",
1629 devices[chosen_drive].devname);
1630 close(mdfd);
1631 free(devices);
1632 return 1;
1633 }
1634 if (st->ss->store_super(st, fd)) {
1635 close(fd);
1636 pr_err("Could not re-write superblock on %s\n",
1637 devices[chosen_drive].devname);
1638 close(mdfd);
1639 free(devices);
1640 return 1;
1641 }
1642 if (c->verbose >= 0)
1643 pr_err("Marking array %s as 'clean'\n",
1644 mddev);
1645 close(fd);
1646 }
1647
1648 /* If we are in the middle of a reshape we may need to restore saved data
1649 * that was moved aside due to the reshape overwriting live data
1650 * The code of doing this lives in Grow.c
1651 */
1652 #ifndef MDASSEMBLE
1653 if (content->reshape_active &&
1654 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
1655 int err = 0;
1656 int *fdlist = xmalloc(sizeof(int)* bestcnt);
1657 if (c->verbose > 0)
1658 pr_err(":%s has an active reshape - checking "
1659 "if critical section needs to be restored\n",
1660 chosen_name);
1661 enable_fds(bestcnt/2);
1662 for (i = 0; i < bestcnt/2; i++) {
1663 int j = best[i*2];
1664 if (j >= 0) {
1665 fdlist[i] = dev_open(devices[j].devname,
1666 devices[j].included
1667 ? O_RDWR : (O_RDWR|O_EXCL));
1668 if (fdlist[i] < 0) {
1669 pr_err("Could not open %s for write - cannot Assemble array.\n",
1670 devices[j].devname);
1671 err = 1;
1672 break;
1673 }
1674 } else
1675 fdlist[i] = -1;
1676 }
1677 if (!err) {
1678 if (st->ss->external && st->ss->recover_backup)
1679 err = st->ss->recover_backup(st, content);
1680 else
1681 err = Grow_restart(st, content, fdlist, bestcnt/2,
1682 c->backup_file, c->verbose > 0);
1683 if (err && c->invalid_backup) {
1684 if (c->verbose > 0)
1685 pr_err("continuing"
1686 " without restoring backup\n");
1687 err = 0;
1688 }
1689 }
1690 while (i>0) {
1691 i--;
1692 if (fdlist[i]>=0) close(fdlist[i]);
1693 }
1694 free(fdlist);
1695 if (err) {
1696 pr_err("Failed to restore critical section for reshape, sorry.\n");
1697 if (c->backup_file == NULL)
1698 cont_err("Possibly you needed to specify the --backup-file\n");
1699 close(mdfd);
1700 free(devices);
1701 return err;
1702 }
1703 }
1704 #endif
1705
1706 /* Almost ready to actually *do* something */
1707 /* First, fill in the map, so that udev can find our name
1708 * as soon as we become active.
1709 */
1710 if (c->update && strcmp(c->update, "metadata")==0) {
1711 content->array.major_version = 1;
1712 content->array.minor_version = 0;
1713 strcpy(content->text_version, "1.0");
1714 }
1715
1716 map_update(&map, fd2devnm(mdfd), content->text_version,
1717 content->uuid, chosen_name);
1718
1719 rv = start_array(mdfd, mddev, content,
1720 st, ident, best, bestcnt,
1721 chosen_drive, devices, okcnt, sparecnt,
1722 rebuilding_cnt,
1723 c,
1724 clean, avail, start_partial_ok,
1725 pre_exist != NULL,
1726 was_forced);
1727 if (rv == 1 && !pre_exist)
1728 ioctl(mdfd, STOP_ARRAY, NULL);
1729 free(devices);
1730 map_unlock(&map);
1731 if (rv == 0) {
1732 wait_for(chosen_name, mdfd);
1733 close(mdfd);
1734 if (auto_assem) {
1735 int usecs = 1;
1736 /* There is a nasty race with 'mdadm --monitor'.
1737 * If it opens this device before we close it,
1738 * it gets an incomplete open on which IO
1739 * doesn't work and the capacity is
1740 * wrong.
1741 * If we reopen (to check for layered devices)
1742 * before --monitor closes, we loose.
1743 *
1744 * So: wait upto 1 second for there to be
1745 * a non-zero capacity.
1746 */
1747 while (usecs < 1000) {
1748 mdfd = open(mddev, O_RDONLY);
1749 if (mdfd >= 0) {
1750 unsigned long long size;
1751 if (get_dev_size(mdfd, NULL, &size) &&
1752 size > 0)
1753 break;
1754 close(mdfd);
1755 }
1756 usleep(usecs);
1757 usecs <<= 1;
1758 }
1759 }
1760 } else
1761 close(mdfd);
1762
1763 /* '2' means 'OK, but not started yet' */
1764 return rv == 2 ? 0 : rv;
1765 }
1766
1767 #ifndef MDASSEMBLE
1768 int assemble_container_content(struct supertype *st, int mdfd,
1769 struct mdinfo *content, struct context *c,
1770 char *chosen_name)
1771 {
1772 struct mdinfo *dev, *sra;
1773 int working = 0, preexist = 0;
1774 int expansion = 0;
1775 struct map_ent *map = NULL;
1776 int old_raid_disks;
1777 int start_reshape;
1778
1779 sysfs_init(content, mdfd, NULL);
1780
1781 sra = sysfs_read(mdfd, NULL, GET_VERSION);
1782 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
1783 if (content->array.major_version == -1 &&
1784 content->array.minor_version == -2 &&
1785 c->readonly &&
1786 content->text_version[0] == '/')
1787 content->text_version[0] = '-';
1788 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1789 if (sra)
1790 sysfs_free(sra);
1791 return 1;
1792 }
1793 }
1794
1795 /* There are two types of reshape: container wide or sub-array specific
1796 * Check if metadata requests blocking container wide reshapes
1797 */
1798 start_reshape = (content->reshape_active &&
1799 !((content->reshape_active == CONTAINER_RESHAPE) &&
1800 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1801
1802 /* Block subarray here if it is under reshape now
1803 * Do not allow for any changes in this array
1804 */
1805 if (st->ss->external && content->recovery_blocked && start_reshape)
1806 block_subarray(content);
1807
1808 if (sra)
1809 sysfs_free(sra);
1810 old_raid_disks = content->array.raid_disks - content->delta_disks;
1811 for (dev = content->devs; dev; dev = dev->next)
1812 if (sysfs_add_disk(content, dev, 1) == 0) {
1813 if (dev->disk.raid_disk >= old_raid_disks &&
1814 content->reshape_active)
1815 expansion++;
1816 else
1817 working++;
1818 } else if (errno == EEXIST)
1819 preexist++;
1820 if (working + expansion == 0)
1821 return 1;/* Nothing new, don't try to start */
1822
1823 map_update(&map, fd2devnm(mdfd),
1824 content->text_version,
1825 content->uuid, chosen_name);
1826
1827 if (c->runstop > 0 ||
1828 (working + preexist + expansion) >=
1829 content->array.working_disks) {
1830 int err;
1831
1832 if (start_reshape) {
1833 int spare = content->array.raid_disks + expansion;
1834 if (restore_backup(st, content,
1835 working,
1836 spare, c->backup_file, c->verbose) == 1)
1837 return 1;
1838
1839 err = sysfs_set_str(content, NULL,
1840 "array_state", "readonly");
1841 if (err)
1842 return 1;
1843
1844 if (st->ss->external) {
1845 if (!mdmon_running(st->container_devnm))
1846 start_mdmon(st->container_devnm);
1847 ping_monitor(st->container_devnm);
1848 if (mdmon_running(st->container_devnm) &&
1849 st->update_tail == NULL)
1850 st->update_tail = &st->updates;
1851 }
1852
1853 err = Grow_continue(mdfd, st, content, c->backup_file,
1854 c->freeze_reshape);
1855 } else switch(content->array.level) {
1856 case LEVEL_LINEAR:
1857 case LEVEL_MULTIPATH:
1858 case 0:
1859 err = sysfs_set_str(content, NULL, "array_state",
1860 c->readonly ? "readonly" : "active");
1861 break;
1862 default:
1863 err = sysfs_set_str(content, NULL, "array_state",
1864 "readonly");
1865 /* start mdmon if needed. */
1866 if (!err) {
1867 if (!mdmon_running(st->container_devnm))
1868 start_mdmon(st->container_devnm);
1869 ping_monitor(st->container_devnm);
1870 }
1871 break;
1872 }
1873 if (!err)
1874 sysfs_set_safemode(content, content->safe_mode_delay);
1875
1876 /* Block subarray here if it is not reshaped now
1877 * It has be blocked a little later to allow mdmon to switch in
1878 * in to R/W state
1879 */
1880 if (st->ss->external && content->recovery_blocked &&
1881 !start_reshape)
1882 block_subarray(content);
1883
1884 if (c->verbose >= 0) {
1885 if (err)
1886 pr_err("array %s now has %d device%s",
1887 chosen_name, working + preexist,
1888 working + preexist == 1 ? "":"s");
1889 else
1890 pr_err("Started %s with %d device%s",
1891 chosen_name, working + preexist,
1892 working + preexist == 1 ? "":"s");
1893 if (preexist)
1894 fprintf(stderr, " (%d new)", working);
1895 if (expansion)
1896 fprintf(stderr, " ( + %d for expansion)",
1897 expansion);
1898 fprintf(stderr, "\n");
1899 }
1900 if (!err)
1901 wait_for(chosen_name, mdfd);
1902 return err;
1903 /* FIXME should have an O_EXCL and wait for read-auto */
1904 } else {
1905 if (c->verbose >= 0) {
1906 pr_err("%s assembled with %d device%s",
1907 chosen_name, preexist + working,
1908 preexist + working == 1 ? "":"s");
1909 if (preexist)
1910 fprintf(stderr, " (%d new)", working);
1911 fprintf(stderr, " but not started\n");
1912 }
1913 return 1;
1914 }
1915 }
1916 #endif