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