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