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