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