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