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