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