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