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