]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / Assemble.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
c61b1c0b 4 * Copyright (C) 2001-2016 Neil Brown <neilb@suse.com>
64c4757e
NB
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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
41a3b72a 26#include <ctype.h>
64c4757e 27
92a647c8
MT
28mapping_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 */
45struct 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 */
61static 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;
5f21d674 66 char *status_msg = map_num_s(assemble_statuses, status);
92a647c8
MT
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);
5f21d674 80 fprintf(stderr, " %s.\n", status_msg);
92a647c8
MT
81}
82
86b77ddf 83static int name_matches(char *found, char *required, char *homehost, int require_homehost)
624920bb
NB
84{
85 /* See if the name found matches the required name, possibly
86 * prefixed with 'homehost'
87 */
00f23a88
N
88 char *sep;
89 unsigned int l;
624920bb 90
624920bb
NB
91 if (strcmp(found, required)==0)
92 return 1;
00f23a88
N
93 sep = strchr(found, ':');
94 if (!sep)
95 return 0;
96 l = sep - found;
97 if (strncmp(found, "any:", 4) == 0 ||
98 (homehost && strcmp(homehost, "any") == 0) ||
86b77ddf 99 !require_homehost ||
00f23a88
N
100 (homehost && strlen(homehost) == l &&
101 strncmp(found, homehost, l) == 0)) {
102 /* matching homehost */
103 if (strcmp(sep+1, required) == 0)
624920bb
NB
104 return 1;
105 }
106 return 0;
107}
108
9008ed1c 109static int is_member_busy(char *metadata_version)
2de8884f
DW
110{
111 /* check if the given member array is active */
b4924f46 112 struct mdstat_ent *mdstat = mdstat_read(0, 0);
2de8884f
DW
113 struct mdstat_ent *ent;
114 int busy = 0;
115
116 for (ent = mdstat; ent; ent = ent->next) {
117 if (ent->metadata_version == NULL)
118 continue;
119 if (strncmp(ent->metadata_version, "external:", 9) != 0)
120 continue;
121 if (!is_subarray(&ent->metadata_version[9]))
122 continue;
123 /* Skip first char - it can be '/' or '-' */
124 if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) {
125 busy = 1;
126 break;
127 }
128 }
129 free_mdstat(mdstat);
130
131 return busy;
132}
133
fa56eddb 134static int ident_matches(struct mddev_ident *ident,
08fb91a3
N
135 struct mdinfo *content,
136 struct supertype *tst,
86b77ddf 137 char *homehost, int require_homehost,
3a87fa67 138 enum update_opt update, char *devname)
08fb91a3
N
139{
140
3a87fa67 141 if (ident->uuid_set && update != UOPT_UUID &&
cbeeb0e5
AC
142 same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 &&
143 memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) {
08fb91a3 144 if (devname)
e7b84f9d 145 pr_err("%s has wrong uuid.\n", devname);
08fb91a3
N
146 return 0;
147 }
3a87fa67 148 if (ident->name[0] && update != UOPT_NAME &&
86b77ddf 149 name_matches(content->name, ident->name, homehost, require_homehost)==0) {
08fb91a3 150 if (devname)
e7b84f9d 151 pr_err("%s has wrong name.\n", devname);
08fb91a3
N
152 return 0;
153 }
154 if (ident->super_minor != UnSet &&
155 ident->super_minor != content->array.md_minor) {
156 if (devname)
e7b84f9d
N
157 pr_err("%s has wrong super-minor.\n",
158 devname);
08fb91a3
N
159 return 0;
160 }
161 if (ident->level != UnSet &&
162 ident->level != content->array.level) {
163 if (devname)
e7b84f9d
N
164 pr_err("%s has wrong raid level.\n",
165 devname);
08fb91a3
N
166 return 0;
167 }
168 if (ident->raid_disks != UnSet &&
babb8dd4 169 content->array.raid_disks != 0 && /* metadata doesn't know how many to expect */
08fb91a3
N
170 ident->raid_disks!= content->array.raid_disks) {
171 if (devname)
e7b84f9d
N
172 pr_err("%s requires wrong number of drives.\n",
173 devname);
08fb91a3
N
174 return 0;
175 }
805d30b2
N
176 if (ident->member && ident->member[0]) {
177 /* content->text_version must match */
178 char *s = strchr(content->text_version+1, '/');
179 if (s == NULL) {
180 if (devname)
e7b84f9d
N
181 pr_err("%s is not a container and one is required.\n",
182 devname);
805d30b2
N
183 return 0;
184 } else if (strcmp(ident->member, s+1) != 0) {
185 if (devname)
e7b84f9d
N
186 pr_err("skipping wrong member %s is %s\n",
187 content->text_version, devname);
805d30b2
N
188 return 0;
189 }
190 }
08fb91a3
N
191 return 1;
192}
08fb91a3 193
95425a89
N
194static int select_devices(struct mddev_dev *devlist,
195 struct mddev_ident *ident,
196 struct supertype **stp,
197 struct mdinfo **contentp,
198 struct context *c,
199 int inargv, int auto_assem)
64c4757e 200{
a655e550 201 struct mddev_dev *tmpdev;
95425a89
N
202 int num_devs;
203 struct supertype *st = *stp;
98dbd966 204 struct mdinfo *content = NULL;
8cf2eb96 205 int report_mismatch = ((inargv && c->verbose >= 0) || c->verbose > 0);
cbeeb0e5 206 struct domainlist *domains = NULL;
0a6bff09 207 dev_t rdev;
60e1bc1a 208
5787fa49
NB
209 tmpdev = devlist; num_devs = 0;
210 while (tmpdev) {
da6b5ca9
NB
211 if (tmpdev->used)
212 tmpdev->used = 2;
213 else
214 num_devs++;
0431869c 215 tmpdev->disposition = 0;
5787fa49
NB
216 tmpdev = tmpdev->next;
217 }
5787fa49 218
811e6cbe
NB
219 /* first walk the list of devices to find a consistent set
220 * that match the criterea, if that is possible.
c30e5369 221 * We flag the ones we like with 'used'.
811e6cbe
NB
222 */
223 for (tmpdev = devlist;
224 tmpdev;
5083d66b 225 tmpdev = tmpdev ? tmpdev->next : NULL) {
811e6cbe 226 char *devname = tmpdev->devname;
64c4757e 227 int dfd;
518a60f3 228 struct supertype *tst;
4e8d9f0a 229 struct dev_policy *pol = NULL;
5083d66b 230 int found_container = 0;
52826846 231
95425a89
N
232 if (tmpdev->used > 1)
233 continue;
da6b5ca9 234
c39b2e63
N
235 if (ident->container) {
236 if (ident->container[0] == '/' &&
237 !same_dev(ident->container, devname)) {
238 if (report_mismatch)
239 pr_err("%s is not the container required (%s)\n",
240 devname, ident->container);
241 continue;
242 }
243 } else if (ident->devices &&
244 !match_oneof(ident->devices, devname)) {
245 /* Note that we ignore the "device=" identifier if a
246 * "container=" is given. Checking both is unnecessarily
247 * complicated.
248 */
8cf2eb96 249 if (report_mismatch)
e7b84f9d 250 pr_err("%s is not one of %s\n", devname, ident->devices);
52826846 251 continue;
56eedc1a 252 }
4b1ac34b 253
518a60f3
JS
254 tst = dup_super(st);
255
56d18859 256 dfd = dev_open(devname, O_RDONLY);
64c4757e 257 if (dfd < 0) {
8cf2eb96 258 if (report_mismatch)
e7b84f9d
N
259 pr_err("cannot open device %s: %s\n",
260 devname, strerror(errno));
da6b5ca9 261 tmpdev->used = 2;
0a6bff09 262 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
da6b5ca9 263 tmpdev->used = 2;
5083d66b
N
264 } else if (must_be_container(dfd)) {
265 if (st) {
266 /* already found some components, this cannot
267 * be another one.
268 */
8cf2eb96 269 if (report_mismatch)
e7b84f9d
N
270 pr_err("%s is a container, but we are looking for components\n",
271 devname);
5083d66b
N
272 tmpdev->used = 2;
273 } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
8cf2eb96 274 if (report_mismatch)
e7b84f9d
N
275 pr_err("not a recognisable container: %s\n",
276 devname);
5083d66b 277 tmpdev->used = 2;
d7be7d87
JS
278 } else if (!tst->ss->load_container ||
279 tst->ss->load_container(tst, dfd, NULL)) {
8cf2eb96 280 if (report_mismatch)
e7b84f9d
N
281 pr_err("no correct container type: %s\n",
282 devname);
5083d66b
N
283 tmpdev->used = 2;
284 } else if (auto_assem &&
0a6bff09
ZL
285 !conf_test_metadata(tst->ss->name,
286 (pol = devid_policy(rdev)),
4977146a 287 tst->ss->match_home(tst, c->homehost) == 1)) {
8cf2eb96 288 if (report_mismatch)
7a862a02 289 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
e7b84f9d 290 devname, tst->ss->name);
5083d66b
N
291 tmpdev->used = 2;
292 } else
293 found_container = 1;
52826846 294 } else {
5083d66b 295 if (!tst && (tst = guess_super(dfd)) == NULL) {
8cf2eb96 296 if (report_mismatch)
e7b84f9d
N
297 pr_err("no recogniseable superblock on %s\n",
298 devname);
5083d66b 299 tmpdev->used = 2;
83607604
N
300 } else if ((tst->ignore_hw_compat = 0),
301 tst->ss->load_super(tst, dfd,
302 report_mismatch ? devname : NULL)) {
8cf2eb96 303 if (report_mismatch)
e7b84f9d
N
304 pr_err("no RAID superblock on %s\n",
305 devname);
5083d66b
N
306 tmpdev->used = 2;
307 } else if (tst->ss->compare_super == NULL) {
8cf2eb96 308 if (report_mismatch)
e7b84f9d
N
309 pr_err("Cannot assemble %s metadata on %s\n",
310 tst->ss->name, devname);
5083d66b
N
311 tmpdev->used = 2;
312 } else if (auto_assem && st == NULL &&
0a6bff09
ZL
313 !conf_test_metadata(tst->ss->name,
314 (pol = devid_policy(rdev)),
4977146a 315 tst->ss->match_home(tst, c->homehost) == 1)) {
8cf2eb96 316 if (report_mismatch)
7a862a02 317 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
e7b84f9d 318 devname, tst->ss->name);
5083d66b
N
319 tmpdev->used = 2;
320 }
64c4757e 321 }
c06487ce 322 if (dfd >= 0) close(dfd);
d68ea4d7 323 if (tmpdev->used == 2) {
d4386799 324 if (auto_assem || !inargv)
d68ea4d7
N
325 /* Ignore unrecognised devices during auto-assembly */
326 goto loop;
5cfb79de 327 if (ident->name[0] ||
d68ea4d7
N
328 ident->super_minor != UnSet)
329 /* Ignore unrecognised device if looking for
330 * specific array */
331 goto loop;
e6878148
SP
332 if (ident->uuid_set)
333 /* ignore unrecognized device if looking for
334 * specific uuid
335 */
336 goto loop;
d68ea4d7 337
e7b84f9d 338 pr_err("%s has no superblock - assembly aborted\n",
79f9f56d 339 devname);
d68ea4d7
N
340 if (st)
341 st->ss->free_super(st);
342 dev_policy_free(pol);
cbeeb0e5 343 domain_free(domains);
e9fb93af 344 if (tst) {
4a670aab 345 tst->ss->free_super(tst);
e9fb93af
GM
346 free(tst);
347 }
95425a89 348 return -1;
d68ea4d7 349 }
52826846 350
5083d66b 351 if (found_container) {
9008ed1c
N
352 /* tmpdev is a container. We need to be either
353 * looking for a member, or auto-assembling
354 */
56d18859
N
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) {
8cf2eb96 361 if (report_mismatch)
e7b84f9d 362 pr_err("%s is busy - skipping\n", devname);
56d18859
N
363 goto loop;
364 }
365 close(dfd);
9008ed1c 366
c39b2e63
N
367 if (ident->container && ident->container[0] != '/') {
368 /* we have a uuid */
369 int uuid[4];
87477e6d 370
c39b2e63
N
371 content = *contentp;
372 tst->ss->getinfo_super(tst, content, NULL);
87477e6d 373
c39b2e63
N
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;
9008ed1c
N
380 }
381 }
382 /* It is worth looking inside this container.
383 */
4977146a 384 if (c->verbose > 0)
e7b84f9d 385 pr_err("looking in container %s\n",
79f9f56d 386 devname);
1415fe4b 387
88cef9b3
N
388 for (content = tst->ss->container_content(tst, NULL);
389 content;
390 content = content->next) {
1415fe4b 391
88cef9b3 392 if (!ident_matches(ident, content, tst,
86b77ddf
N
393 c->homehost, c->require_homehost,
394 c->update,
8cf2eb96 395 report_mismatch ? devname : NULL))
88cef9b3
N
396 /* message already printed */;
397 else if (is_member_busy(content->text_version)) {
8cf2eb96 398 if (report_mismatch)
e7b84f9d 399 pr_err("member %s in %s is already assembled\n",
79f9f56d
N
400 content->text_version,
401 devname);
81219e70
LM
402 } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
403 /* do not assemble arrays with unsupported configurations */
e7b84f9d 404 pr_err("Cannot activate member %s in %s.\n",
79f9f56d
N
405 content->text_version,
406 devname);
88cef9b3
N
407 } else
408 break;
409 }
410 if (!content) {
f7ad3ccc 411 tmpdev->used = 2;
88cef9b3 412 goto loop; /* empty container */
fa031239 413 }
88cef9b3 414
9008ed1c 415 st = tst; tst = NULL;
4c1c3ad8 416 if (!auto_assem && inargv && tmpdev->next != NULL) {
7a862a02 417 pr_err("%s is a container, but is not only device given: confused and aborting\n",
79f9f56d 418 devname);
9008ed1c 419 st->ss->free_super(st);
4e8d9f0a 420 dev_policy_free(pol);
cbeeb0e5 421 domain_free(domains);
e9fb93af 422 free(st);
95425a89 423 return -1;
9008ed1c 424 }
4977146a 425 if (c->verbose > 0)
e7b84f9d 426 pr_err("found match on member %s in %s\n",
79f9f56d 427 content->text_version, devname);
bac0d92e 428
5083d66b
N
429 /* make sure we finished the loop */
430 tmpdev = NULL;
bac0d92e 431 goto loop;
5083d66b 432 } else {
95425a89 433 content = *contentp;
5083d66b
N
434 tst->ss->getinfo_super(tst, content, NULL);
435
436 if (!ident_matches(ident, content, tst,
86b77ddf
N
437 c->homehost, c->require_homehost,
438 c->update,
8cf2eb96 439 report_mismatch ? devname : NULL))
df37ffc0 440 goto loop;
66eb2c93 441
5141638c
N
442 if (auto_assem) {
443 /* Never auto-assemble things that conflict
444 * with mdadm.conf in some way
1d04e275 445 */
5141638c
N
446 struct mddev_ident *match;
447 int rv = 0;
448
449 match = conf_match(tst, content, devname,
450 report_mismatch ? c->verbose : -1,
451 &rv);
452 if (!match && rv == 2)
453 goto loop;
454 if (match && match->devname &&
455 strcasecmp(match->devname, "<ignore>") == 0) {
456 if (report_mismatch)
457 pr_err("%s is a member of an explicitly ignored array\n",
458 devname);
459 goto loop;
460 }
461 if (match && !ident_matches(match, content, tst,
86b77ddf
N
462 c->homehost, c->require_homehost,
463 c->update,
5141638c
N
464 report_mismatch ? devname : NULL))
465 /* Array exists in mdadm.conf but some
466 * details don't match, so reject it
467 */
468 goto loop;
469 }
66eb2c93 470
56d18859
N
471 /* should be safe to try an exclusive open now, we
472 * have rejected anything that some other mdadm might
473 * be looking at
474 */
475 dfd = dev_open(devname, O_RDONLY | O_EXCL);
476 if (dfd < 0) {
8cf2eb96 477 if (report_mismatch)
e7b84f9d 478 pr_err("%s is busy - skipping\n", devname);
56d18859
N
479 goto loop;
480 }
481 close(dfd);
482
5083d66b
N
483 if (st == NULL)
484 st = dup_super(tst);
485 if (st->minor_version == -1)
486 st->minor_version = tst->minor_version;
ed7fc6b4
AC
487
488 if (memcmp(content->uuid, uuid_zero,
489 sizeof(int[4])) == 0) {
490 /* this is a floating spare. It cannot define
491 * an array unless there are no more arrays of
492 * this type to be found. It can be included
493 * in an array of this type though.
494 */
495 tmpdev->used = 3;
496 goto loop;
497 }
498
5083d66b
N
499 if (st->ss != tst->ss ||
500 st->minor_version != tst->minor_version ||
c7b8547c 501 st->ss->compare_super(st, tst, 1) != 0) {
5083d66b
N
502 /* Some mismatch. If exactly one array matches this host,
503 * we can resolve on that one.
504 * Or, if we are auto assembling, we just ignore the second
505 * for now.
506 */
507 if (auto_assem)
508 goto loop;
4977146a
N
509 if (c->homehost) {
510 int first = st->ss->match_home(st, c->homehost);
511 int last = tst->ss->match_home(tst, c->homehost);
5083d66b
N
512 if (first != last &&
513 (first == 1 || last == 1)) {
514 /* We can do something */
515 if (first) {/* just ignore this one */
8cf2eb96 516 if (report_mismatch)
e7b84f9d 517 pr_err("%s misses out due to wrong homehost\n",
79f9f56d 518 devname);
5083d66b
N
519 goto loop;
520 } else { /* reject all those sofar */
521 struct mddev_dev *td;
8cf2eb96 522 if (report_mismatch)
e7b84f9d 523 pr_err("%s overrides previous devices due to good homehost\n",
79f9f56d 524 devname);
5083d66b
N
525 for (td=devlist; td != tmpdev; td=td->next)
526 if (td->used == 1)
527 td->used = 0;
528 tmpdev->used = 1;
529 goto loop;
530 }
83b6208e
NB
531 }
532 }
e7b84f9d 533 pr_err("superblock on %s doesn't match others - assembly aborted\n",
79f9f56d 534 devname);
5083d66b
N
535 tst->ss->free_super(tst);
536 st->ss->free_super(st);
537 dev_policy_free(pol);
cbeeb0e5 538 domain_free(domains);
e9fb93af 539 free(tst);
95425a89 540 return -1;
83b6208e 541 }
5083d66b 542 tmpdev->used = 1;
64c4757e 543 }
df37ffc0 544 loop:
cbeeb0e5 545 /* Collect domain information from members only */
26b05aea
AC
546 if (tmpdev && tmpdev->used == 1) {
547 if (!pol)
0a6bff09 548 pol = devid_policy(rdev);
cbeeb0e5 549 domain_merge(&domains, pol, tst?tst->ss->name:NULL);
26b05aea 550 }
4e8d9f0a
N
551 dev_policy_free(pol);
552 pol = NULL;
e9fb93af 553 if (tst) {
3d2b16e7 554 tst->ss->free_super(tst);
e9fb93af
GM
555 free(tst);
556 }
811e6cbe
NB
557 }
558
ed7fc6b4 559 /* Check if we found some imsm spares but no members */
3c7b4a25
CA
560 if ((auto_assem ||
561 (ident->uuid_set &&
562 memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) &&
563 (!st || !st->sb))
ed7fc6b4
AC
564 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
565 if (tmpdev->used != 3)
566 continue;
567 tmpdev->used = 1;
95425a89 568 content = *contentp;
ed7fc6b4
AC
569
570 if (!st->sb) {
571 /* we need sb from one of the spares */
572 int dfd = dev_open(tmpdev->devname, O_RDONLY);
573 if (dfd < 0 ||
574 st->ss->load_super(st, dfd, NULL))
575 tmpdev->used = 2;
b71de056 576 close_fd(&dfd);
ed7fc6b4
AC
577 }
578 }
579
cbeeb0e5
AC
580 /* Now reject spares that don't match domains of identified members */
581 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
cbeeb0e5
AC
582 if (tmpdev->used != 3)
583 continue;
9e04ac1c 584 if (!stat_is_blkdev(tmpdev->devname, &rdev)) {
cbeeb0e5
AC
585 tmpdev->used = 2;
586 } else {
9e04ac1c 587 struct dev_policy *pol = devid_policy(rdev);
a5d10dce
N
588 int dt = domain_test(domains, pol, NULL);
589 if (inargv && dt != 0)
590 /* take this spare as domains match
591 * if there are any */
592 tmpdev->used = 1;
593 else if (!inargv && dt == 1)
594 /* device wasn't explicitly listed, so need
595 * explicit domain match - which we have */
cbeeb0e5
AC
596 tmpdev->used = 1;
597 else
598 /* if domains don't match mark as unused */
599 tmpdev->used = 0;
600 dev_policy_free(pol);
601 }
602 }
603 domain_free(domains);
95425a89
N
604 *stp = st;
605 if (st && st->sb && content == *contentp)
606 st->ss->getinfo_super(st, content, NULL);
607 *contentp = content;
608
609 return num_devs;
610}
611
2c355c22
N
612struct devs {
613 char *devname;
614 int uptodate; /* set once we decide that this device is as
615 * recent as everything else in the array.
616 */
617 int included; /* set if the device is already in the array
618 * due to a previous '-I'
619 */
620 struct mdinfo i;
621};
622
623static int load_devices(struct devs *devices, char *devmap,
df842e69 624 struct mddev_ident *ident, struct supertype **stp,
2c355c22
N
625 struct mddev_dev *devlist, struct context *c,
626 struct mdinfo *content,
627 int mdfd, char *mddev,
628 int *most_recentp, int *bestcntp, int **bestp,
629 int inargv)
630{
631 struct mddev_dev *tmpdev;
632 int devcnt = 0;
633 int nextspare = 0;
2c355c22 634 int bitmap_done = 0;
a792ece6 635 int most_recent = -1;
2c355c22
N
636 int bestcnt = 0;
637 int *best = *bestp;
df842e69 638 struct supertype *st = *stp;
2c355c22
N
639
640 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) {
641 char *devname = tmpdev->devname;
642 struct stat stb;
9ee314da 643 struct supertype *tst;
2c355c22 644 int i;
e9e6894d 645 int dfd;
563ac108 646 int disk_state;
2c355c22
N
647
648 if (tmpdev->used != 1)
649 continue;
650 /* looks like a good enough match to update the super block if needed */
2c355c22 651 if (c->update) {
2c355c22
N
652 /* prepare useful information in info structures */
653 struct stat stb2;
2c355c22
N
654 int err;
655 fstat(mdfd, &stb2);
656
3a87fa67 657 if (c->update == UOPT_UUID && !ident->uuid_set)
c5f71c24
JS
658 random_uuid((__u8 *)ident->uuid);
659
3a87fa67 660 if (c->update == UOPT_PPL && ident->bitmap_fd >= 0) {
e6e9dd3f 661 pr_err("PPL is not compatible with bitmap\n");
d8b01738
GJ
662 close(mdfd);
663 free(devices);
664 free(devmap);
e6e9dd3f
AP
665 return -1;
666 }
667
2c355c22
N
668 dfd = dev_open(devname,
669 tmpdev->disposition == 'I'
670 ? O_RDWR : (O_RDWR|O_EXCL));
671
672 tst = dup_super(st);
673 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
674 pr_err("cannot re-read metadata from %s - aborting\n",
675 devname);
676 if (dfd >= 0)
677 close(dfd);
678 close(mdfd);
679 free(devices);
680 free(devmap);
9ee314da
N
681 tst->ss->free_super(tst);
682 free(tst);
df842e69 683 *stp = st;
2c355c22
N
684 return -1;
685 }
686 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
687
688 memcpy(content->uuid, ident->uuid, 16);
689 strcpy(content->name, ident->name);
690 content->array.md_minor = minor(stb2.st_rdev);
691
3a87fa67 692 if (c->update == UOPT_BYTEORDER)
2c355c22 693 err = 0;
3a87fa67 694 else if (c->update == UOPT_HOME_CLUSTER) {
0aa2f15b 695 tst->cluster_name = c->homecluster;
d80f7aa9 696 err = tst->ss->write_bitmap(tst, dfd, NameUpdate);
3a87fa67 697 } else if (c->update == UOPT_NODES) {
7e6e839a
GJ
698 tst->nodes = c->nodes;
699 err = tst->ss->write_bitmap(tst, dfd, NodeNumUpdate);
3a87fa67 700 } else if (c->update == UOPT_REVERT_RESHAPE && c->invalid_backup)
d5ff855d 701 err = tst->ss->update_super(tst, content,
03312b52 702 UOPT_SPEC_REVERT_RESHAPE_NOBACKUP,
d5ff855d
N
703 devname, c->verbose,
704 ident->uuid_set,
705 c->homehost);
706 else
03312b52 707 err = tst->ss->update_super(tst, content,
3a87fa67 708 c->update,
2c355c22
N
709 devname, c->verbose,
710 ident->uuid_set,
711 c->homehost);
712 if (err < 0) {
afa368f4 713 if (err == -1)
7a862a02 714 pr_err("--update=%s not understood for %s metadata\n",
3a87fa67 715 map_num(update_options, c->update), tst->ss->name);
2c355c22
N
716 tst->ss->free_super(tst);
717 free(tst);
718 close(mdfd);
719 close(dfd);
720 free(devices);
721 free(devmap);
df842e69 722 *stp = st;
2c355c22
N
723 return -1;
724 }
3a87fa67 725 if (c->update == UOPT_UUID &&
2c355c22
N
726 !ident->uuid_set) {
727 ident->uuid_set = 1;
728 memcpy(ident->uuid, content->uuid, 16);
729 }
730 if (tst->ss->store_super(tst, dfd))
731 pr_err("Could not re-write superblock on %s.\n",
732 devname);
2c355c22 733
3a87fa67 734 if (c->update == UOPT_UUID &&
2c355c22
N
735 ident->bitmap_fd >= 0 && !bitmap_done) {
736 if (bitmap_update_uuid(ident->bitmap_fd,
737 content->uuid,
738 tst->ss->swapuuid) != 0)
739 pr_err("Could not update uuid on external bitmap.\n");
740 else
741 bitmap_done = 1;
742 }
32141c17 743 } else {
e9e6894d
N
744 dfd = dev_open(devname,
745 tmpdev->disposition == 'I'
746 ? O_RDWR : (O_RDWR|O_EXCL));
9ee314da 747 tst = dup_super(st);
2c355c22
N
748
749 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
750 pr_err("cannot re-read metadata from %s - aborting\n",
751 devname);
752 if (dfd >= 0)
753 close(dfd);
754 close(mdfd);
755 free(devices);
756 free(devmap);
9ee314da
N
757 tst->ss->free_super(tst);
758 free(tst);
df842e69 759 *stp = st;
2c355c22
N
760 return -1;
761 }
762 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
2c355c22
N
763 }
764
e9e6894d
N
765 fstat(dfd, &stb);
766 close(dfd);
2c355c22
N
767
768 if (c->verbose > 0)
aacb2f81
N
769 pr_err("%s is identified as a member of %s, slot %d%s.\n",
770 devname, mddev, content->disk.raid_disk,
771 (content->disk.state & (1<<MD_DISK_REPLACEMENT)) ? " replacement":"");
2c355c22
N
772 devices[devcnt].devname = devname;
773 devices[devcnt].uptodate = 0;
774 devices[devcnt].included = (tmpdev->disposition == 'I');
775 devices[devcnt].i = *content;
776 devices[devcnt].i.disk.major = major(stb.st_rdev);
777 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
f80057ae 778
563ac108
GK
779 disk_state = devices[devcnt].i.disk.state & ~((1<<MD_DISK_FAILFAST) |
780 (1<<MD_DISK_WRITEMOSTLY));
781 if (disk_state == ((1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC))) {
a792ece6
N
782 if (most_recent < 0 ||
783 devices[devcnt].i.events
56bbc588
N
784 > devices[most_recent].i.events) {
785 struct supertype *tmp = tst;
786 tst = st;
787 st = tmp;
2c355c22 788 most_recent = devcnt;
56bbc588 789 }
a792ece6 790 }
9ee314da
N
791 tst->ss->free_super(tst);
792 free(tst);
f80057ae 793
2c355c22
N
794 if (content->array.level == LEVEL_MULTIPATH)
795 /* with multipath, the raid_disk from the superblock is meaningless */
796 i = devcnt;
797 else
798 i = devices[devcnt].i.disk.raid_disk;
69a48116 799 if (i+1 == 0 || i == MD_DISK_ROLE_JOURNAL) {
aacb2f81
N
800 if (nextspare < content->array.raid_disks*2)
801 nextspare = content->array.raid_disks*2;
2c355c22
N
802 i = nextspare++;
803 } else {
aacb2f81
N
804 /* i is raid_disk - double it so there is room for
805 * replacements */
806 i *= 2;
807 if (devices[devcnt].i.disk.state & (1<<MD_DISK_REPLACEMENT))
808 i++;
809 if (i >= content->array.raid_disks*2 &&
2c355c22
N
810 i >= nextspare)
811 nextspare = i+1;
812 }
813 if (i < 10000) {
814 if (i >= bestcnt) {
815 int newbestcnt = i+10;
816 int *newbest = xmalloc(sizeof(int)*newbestcnt);
817 int c;
818 for (c=0; c < newbestcnt; c++)
819 if (c < bestcnt)
820 newbest[c] = best[c];
821 else
822 newbest[c] = -1;
823 if (best)free(best);
824 best = newbest;
825 bestcnt = newbestcnt;
826 }
827 if (best[i] >=0 &&
fc54fe7a
JS
828 devices[best[i]].i.events ==
829 devices[devcnt].i.events &&
d16a7494
JS
830 (devices[best[i]].i.disk.minor !=
831 devices[devcnt].i.disk.minor) &&
fc54fe7a
JS
832 st->ss == &super0 &&
833 content->array.level != LEVEL_MULTIPATH) {
2c355c22
N
834 /* two different devices with identical superblock.
835 * Could be a mis-detection caused by overlapping
836 * partitions. fail-safe.
837 */
7a862a02
N
838 pr_err("WARNING %s and %s appear to have very similar superblocks.\n"
839 " If they are really different, please --zero the superblock on one\n"
840 " If they are the same or overlap, please remove one from %s.\n",
2c355c22
N
841 devices[best[i]].devname, devname,
842 inargv ? "the list" :
843 "the\n DEVICE list in mdadm.conf"
844 );
845 close(mdfd);
846 free(devices);
847 free(devmap);
e9fb93af 848 free(best);
df842e69 849 *stp = st;
2c355c22
N
850 return -1;
851 }
d7be7d87
JS
852 if (best[i] == -1 || (devices[best[i]].i.events
853 < devices[devcnt].i.events))
2c355c22 854 best[i] = devcnt;
7298c9a6
MT
855 else if (st->ss == &super_imsm)
856 best[i+1] = devcnt;
2c355c22
N
857 }
858 devcnt++;
859 }
a792ece6
N
860 if (most_recent >= 0)
861 *most_recentp = most_recent;
2c355c22
N
862 *bestcntp = bestcnt;
863 *bestp = best;
df842e69 864 *stp = st;
2c355c22
N
865 return devcnt;
866}
867
9f5470ce
N
868static int force_array(struct mdinfo *content,
869 struct devs *devices,
870 int *best, int bestcnt, char *avail,
871 int most_recent,
872 struct supertype *st,
873 struct context *c)
874{
875 int okcnt = 0;
876 while (!enough(content->array.level, content->array.raid_disks,
877 content->array.layout, 1,
fc54fe7a 878 avail) ||
9f5470ce
N
879 (content->reshape_active && content->delta_disks > 0 &&
880 !enough(content->array.level, (content->array.raid_disks
881 - content->delta_disks),
fc54fe7a 882 content->new_layout, 1, avail))) {
9f5470ce
N
883 /* Choose the newest best drive which is
884 * not up-to-date, update the superblock
885 * and add it.
886 */
887 int fd;
888 struct supertype *tst;
889 unsigned long long current_events;
890 int chosen_drive = -1;
891 int i;
892
f81a2b56
N
893 for (i = 0;
894 i < content->array.raid_disks * 2 && i < bestcnt;
895 i += 2) {
9f5470ce 896 int j = best[i];
87af7267
N
897 if (j < 0)
898 continue;
899 if (devices[j].uptodate)
900 continue;
901 if (devices[j].i.recovery_start != MaxSector) {
902 int delta;
903 if (!devices[j].i.reshape_active ||
904 devices[j].i.delta_disks <= 0)
905 continue;
906 /* When increasing number of devices, an
907 * added device also appears to be
908 * recovering. It is safe to include it
909 * as long as it won't be a source of
910 * data.
911 * For now, just allow for last data
912 * devices in RAID4 or last devices in RAID4/5/6.
913 */
914 delta = devices[j].i.delta_disks;
70f1ff42 915 if (is_level456(devices[j].i.array.level) &&
87af7267
N
916 i/2 >= content->array.raid_disks - delta)
917 /* OK */;
918 else if (devices[j].i.array.level == 4 &&
919 i/2 >= content->array.raid_disks - delta - 1)
920 /* OK */;
921 else
922 continue;
62f1aee7
BC
923 } else if (devices[j].i.reshape_active !=
924 content->reshape_active ||
925 (devices[j].i.reshape_active &&
926 devices[j].i.reshape_progress !=
927 content->reshape_progress))
928 /* Here, it may be a source of data. If two
929 * devices claim different progresses, it
930 * means that reshape boundaries differ for
931 * their own devices. Kernel will only treat
932 * the first one as reshape progress and
933 * go on. It may cause disaster, so avoid it.
934 */
935 continue;
87af7267 936 if (chosen_drive < 0 ||
9f5470ce 937 devices[j].i.events
87af7267 938 > devices[chosen_drive].i.events)
9f5470ce
N
939 chosen_drive = j;
940 }
941 if (chosen_drive < 0)
942 break;
943 current_events = devices[chosen_drive].i.events;
944 add_another:
945 if (c->verbose >= 0)
ebf3be99 946 pr_err("forcing event count in %s(%d) from %d up to %d\n",
79f9f56d
N
947 devices[chosen_drive].devname,
948 devices[chosen_drive].i.disk.raid_disk,
949 (int)(devices[chosen_drive].i.events),
950 (int)(devices[most_recent].i.events));
9f5470ce
N
951 fd = dev_open(devices[chosen_drive].devname,
952 devices[chosen_drive].included ? O_RDWR
953 : (O_RDWR|O_EXCL));
954 if (fd < 0) {
955 pr_err("Couldn't open %s for write - not updating\n",
79f9f56d 956 devices[chosen_drive].devname);
9f5470ce
N
957 devices[chosen_drive].i.events = 0;
958 continue;
959 }
960 tst = dup_super(st);
961 if (tst->ss->load_super(tst,fd, NULL)) {
962 close(fd);
963 pr_err("RAID superblock disappeared from %s - not updating.\n",
79f9f56d 964 devices[chosen_drive].devname);
9f5470ce
N
965 devices[chosen_drive].i.events = 0;
966 continue;
967 }
968 content->events = devices[most_recent].i.events;
03312b52 969 tst->ss->update_super(tst, content, UOPT_SPEC_FORCE_ONE,
79f9f56d
N
970 devices[chosen_drive].devname, c->verbose,
971 0, NULL);
9f5470ce
N
972
973 if (tst->ss->store_super(tst, fd)) {
974 close(fd);
975 pr_err("Could not re-write superblock on %s\n",
79f9f56d 976 devices[chosen_drive].devname);
9f5470ce
N
977 devices[chosen_drive].i.events = 0;
978 tst->ss->free_super(tst);
979 continue;
980 }
981 close(fd);
982 devices[chosen_drive].i.events = devices[most_recent].i.events;
983 devices[chosen_drive].uptodate = 1;
984 avail[chosen_drive] = 1;
985 okcnt++;
986 tst->ss->free_super(tst);
9f5470ce
N
987 /* If there are any other drives of the same vintage,
988 * add them in as well. We can't lose and we might gain
989 */
f81a2b56
N
990 for (i = 0;
991 i < content->array.raid_disks * 2 && i < bestcnt ;
992 i += 2) {
9f5470ce
N
993 int j = best[i];
994 if (j >= 0 &&
995 !devices[j].uptodate &&
996 devices[j].i.recovery_start == MaxSector &&
62f1aee7
BC
997 devices[j].i.events == current_events &&
998 ((!devices[j].i.reshape_active &&
999 !content->reshape_active) ||
1000 (devices[j].i.reshape_active ==
1001 content->reshape_active &&
1002 devices[j].i.reshape_progress ==
1003 content->reshape_progress))) {
9f5470ce
N
1004 chosen_drive = j;
1005 goto add_another;
1006 }
1007 }
1008 }
1009 return okcnt;
1010}
1011
ddc1b11f
N
1012static int start_array(int mdfd,
1013 char *mddev,
1014 struct mdinfo *content,
1015 struct supertype *st,
1016 struct mddev_ident *ident,
1017 int *best, int bestcnt,
1018 int chosen_drive,
1019 struct devs *devices,
1020 unsigned int okcnt,
1021 unsigned int sparecnt,
1022 unsigned int rebuilding_cnt,
69a48116 1023 unsigned int journalcnt,
ddc1b11f
N
1024 struct context *c,
1025 int clean, char *avail,
8cde842b 1026 int start_partial_ok,
71417de6 1027 int err_ok,
051f3265 1028 int was_forced
ddc1b11f
N
1029 )
1030{
1031 int rv;
1032 int i;
1033 unsigned int req_cnt;
1034
051f3265 1035 if (content->journal_device_required && (content->journal_clean == 0)) {
69a48116
SL
1036 if (!c->force) {
1037 pr_err("Not safe to assemble with missing or stale journal device, consider --force.\n");
1038 return 1;
1039 }
1040 pr_err("Journal is missing or stale, starting array read only.\n");
1041 c->readonly = 1;
1042 }
1043
e97a7cd0
AP
1044 if (content->consistency_policy == CONSISTENCY_POLICY_PPL)
1045 clean = 1;
1046
ddc1b11f 1047 rv = set_array_info(mdfd, st, content);
71417de6 1048 if (rv && !err_ok) {
ddc1b11f
N
1049 pr_err("failed to set array info for %s: %s\n",
1050 mddev, strerror(errno));
1051 return 1;
1052 }
1053 if (ident->bitmap_fd >= 0) {
1054 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
1055 pr_err("SET_BITMAP_FILE failed.\n");
1056 return 1;
1057 }
1058 } else if (ident->bitmap_file) {
1059 /* From config file */
1060 int bmfd = open(ident->bitmap_file, O_RDWR);
1061 if (bmfd < 0) {
1062 pr_err("Could not open bitmap file %s\n",
1063 ident->bitmap_file);
1064 return 1;
1065 }
1066 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
1067 pr_err("Failed to set bitmapfile for %s\n", mddev);
1068 close(bmfd);
1069 return 1;
1070 }
1071 close(bmfd);
1072 }
1073
1074 /* First, add the raid disks, but add the chosen one last */
94b53b77 1075 for (i = 0; i <= bestcnt; i++) {
ddc1b11f
N
1076 int j;
1077 if (i < bestcnt) {
1078 j = best[i];
1079 if (j == chosen_drive)
1080 continue;
1081 } else
1082 j = chosen_drive;
1083
1084 if (j >= 0 && !devices[j].included) {
94b53b77
JS
1085 int dfd;
1086
1087 dfd = dev_open(devices[j].devname, O_RDWR|O_EXCL);
ddc1b11f
N
1088 if (dfd >= 0) {
1089 remove_partitions(dfd);
1090 close(dfd);
1091 }
1092 rv = add_disk(mdfd, st, content, &devices[j].i);
1093
1094 if (rv) {
7a862a02 1095 pr_err("failed to add %s to %s: %s\n",
94b53b77 1096 devices[j].devname, mddev,
ddc1b11f 1097 strerror(errno));
027c099f
N
1098 if (errno == EINVAL && content->array.level == 0 &&
1099 content->array.layout != 0) {
1100 cont_err("Possibly your kernel doesn't support RAID0 layouts.\n");
1101 cont_err("Please upgrade.\n");
1102 }
94b53b77
JS
1103 if (i < content->array.raid_disks * 2 ||
1104 i == bestcnt)
ddc1b11f
N
1105 okcnt--;
1106 else
1107 sparecnt--;
94b53b77 1108 } else if (c->verbose > 0) {
aacb2f81 1109 pr_err("added %s to %s as %d%s%s\n",
ddc1b11f
N
1110 devices[j].devname, mddev,
1111 devices[j].i.disk.raid_disk,
1112 devices[j].uptodate?"":
aacb2f81 1113 " (possibly out of date)",
94b53b77
JS
1114 (devices[j].i.disk.state &
1115 (1<<MD_DISK_REPLACEMENT)) ?
1116 " replacement":"");
1117 }
ddc1b11f
N
1118 } else if (j >= 0) {
1119 if (c->verbose > 0)
1120 pr_err("%s is already in %s as %d\n",
1121 devices[j].devname, mddev,
1122 devices[j].i.disk.raid_disk);
94b53b77
JS
1123 } else if (c->verbose > 0 &&
1124 i < content->array.raid_disks * 2 && (i & 1) == 0)
ddc1b11f 1125 pr_err("no uptodate device for slot %d of %s\n",
ee466574 1126 i/2, mddev);
ddc1b11f
N
1127 }
1128
6f2af6a4 1129 if (is_container(content->array.level)) {
d64a37b9 1130 sysfs_rules_apply(mddev, content);
ddc1b11f 1131 if (c->verbose >= 0) {
7a862a02 1132 pr_err("Container %s has been assembled with %d drive%s",
94b53b77
JS
1133 mddev, okcnt + sparecnt + journalcnt,
1134 okcnt + sparecnt + journalcnt == 1 ? "" : "s");
ddc1b11f 1135 if (okcnt < (unsigned)content->array.raid_disks)
b0681598 1136 fprintf(stderr, " (out of %d)\n",
ddc1b11f 1137 content->array.raid_disks);
d64a37b9 1138 else
b0681598 1139 fprintf(stderr, "\n");
ddc1b11f 1140 }
0c21b485
PB
1141
1142 if (st->ss->validate_container) {
1143 struct mdinfo *devices_list;
94b53b77 1144 struct mdinfo *info_devices;
0c21b485 1145 unsigned int count;
94b53b77 1146
0c21b485 1147 devices_list = NULL;
94b53b77
JS
1148 info_devices = xmalloc(sizeof(struct mdinfo) *
1149 (okcnt + sparecnt));
1150 for (count = 0; count < okcnt + sparecnt; count++) {
0c21b485
PB
1151 info_devices[count] = devices[count].i;
1152 info_devices[count].next = devices_list;
1153 devices_list = &info_devices[count];
1154 }
1155 if (st->ss->validate_container(devices_list))
1156 pr_err("Mismatch detected!\n");
1157 free(info_devices);
1158 }
1159
ddc1b11f
N
1160 st->ss->free_super(st);
1161 sysfs_uevent(content, "change");
284546ef
N
1162 if (err_ok && okcnt < (unsigned)content->array.raid_disks)
1163 /* Was partial, is still partial, so signal an error
1164 * to ensure we don't retry */
1165 return 1;
ddc1b11f
N
1166 return 0;
1167 }
1168
1169 /* Get number of in-sync devices according to the superblock.
1170 * We must have this number to start the array without -s or -R
1171 */
1172 req_cnt = content->array.working_disks;
1173
1174 if (c->runstop == 1 ||
1175 (c->runstop <= 0 &&
94b53b77
JS
1176 (enough(content->array.level, content->array.raid_disks,
1177 content->array.layout, clean, avail) &&
1178 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)))) {
ddc1b11f
N
1179 /* This array is good-to-go.
1180 * If a reshape is in progress then we might need to
1181 * continue monitoring it. In that case we start
1182 * it read-only and let the grow code make it writable.
1183 */
1184 int rv;
94b53b77 1185
ddc1b11f
N
1186 if (content->reshape_active &&
1187 !(content->reshape_active & RESHAPE_NO_BACKUP) &&
1188 content->delta_disks <= 0) {
be7c26b4
N
1189 if (!c->backup_file) {
1190 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1191 mddev);
1192 pr_err("Please provided one with \"--backup-file=...\"\n");
3a87fa67 1193 if (c->update == UOPT_REVERT_RESHAPE)
be7c26b4
N
1194 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1195 return 1;
1196 }
ddc1b11f
N
1197 rv = sysfs_set_str(content, NULL,
1198 "array_state", "readonly");
1199 if (rv == 0)
1200 rv = Grow_continue(mdfd, st, content,
06e293d0 1201 c->backup_file, 0,
ddc1b11f
N
1202 c->freeze_reshape);
1203 } else if (c->readonly &&
94b53b77
JS
1204 sysfs_attribute_available(content, NULL,
1205 "array_state")) {
ddc1b11f
N
1206 rv = sysfs_set_str(content, NULL,
1207 "array_state", "readonly");
1208 } else
ddc1b11f 1209 rv = ioctl(mdfd, RUN_ARRAY, NULL);
8832342d 1210 reopen_mddev(mdfd); /* drop O_EXCL */
ddc1b11f 1211 if (rv == 0) {
b0681598 1212 sysfs_rules_apply(mddev, content);
ddc1b11f
N
1213 if (c->verbose >= 0) {
1214 pr_err("%s has been started with %d drive%s",
1215 mddev, okcnt, okcnt==1?"":"s");
1216 if (okcnt < (unsigned)content->array.raid_disks)
94b53b77
JS
1217 fprintf(stderr, " (out of %d)",
1218 content->array.raid_disks);
ddc1b11f 1219 if (rebuilding_cnt)
94b53b77
JS
1220 fprintf(stderr, "%s %d rebuilding",
1221 sparecnt?",":" and",
1222 rebuilding_cnt);
ddc1b11f 1223 if (sparecnt)
94b53b77
JS
1224 fprintf(stderr, " and %d spare%s",
1225 sparecnt,
1226 sparecnt == 1 ? "" : "s");
051f3265 1227 if (content->journal_clean)
94b53b77
JS
1228 fprintf(stderr, " and %d journal",
1229 journalcnt);
ddc1b11f
N
1230 fprintf(stderr, ".\n");
1231 }
1232 if (content->reshape_active &&
70f1ff42 1233 is_level456(content->array.level)) {
ddc1b11f
N
1234 /* might need to increase the size
1235 * of the stripe cache - default is 256
1236 */
56fcbcbb 1237 int chunk_size = content->array.chunk_size;
94b53b77 1238
56fcbcbb
N
1239 if (content->reshape_active &&
1240 content->new_chunk > chunk_size)
1241 chunk_size = content->new_chunk;
29a312f2 1242 if (256 < 4 * ((chunk_size+4065)/4096)) {
94b53b77
JS
1243 struct mdinfo *sra;
1244
1245 sra = sysfs_read(mdfd, NULL, 0);
ddc1b11f
N
1246 if (sra)
1247 sysfs_set_num(sra, NULL,
1248 "stripe_cache_size",
29a312f2 1249 (4 * chunk_size / 4096) + 1);
ddc1b11f
N
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 }
8cde842b
N
1275 if (content->array.level == 6 &&
1276 okcnt + 1 == (unsigned)content->array.raid_disks &&
1277 was_forced) {
94b53b77
JS
1278 struct mdinfo *sra;
1279
1280 sra = sysfs_read(mdfd, NULL, 0);
8cde842b
N
1281 if (sra)
1282 sysfs_set_str(sra, NULL,
1283 "sync_action", "repair");
1284 sysfs_free(sra);
1285 }
ddc1b11f
N
1286 return 0;
1287 }
94b53b77 1288 pr_err("failed to RUN_ARRAY %s: %s\n", mddev, strerror(errno));
027c099f
N
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");
ddc1b11f
N
1292
1293 if (!enough(content->array.level, content->array.raid_disks,
1294 content->array.layout, 1, avail))
7a862a02 1295 pr_err("Not enough devices to start the array.\n");
ddc1b11f
N
1296 else if (!enough(content->array.level,
1297 content->array.raid_disks,
94b53b77 1298 content->array.layout, clean, avail))
7a862a02 1299 pr_err("Not enough devices to start the array while not clean - consider --force.\n");
ddc1b11f
N
1300
1301 return 1;
1302 }
1303 if (c->runstop == -1) {
1304 pr_err("%s assembled from %d drive%s",
94b53b77 1305 mddev, okcnt, okcnt == 1 ? "" : "s");
ddc1b11f 1306 if (okcnt != (unsigned)content->array.raid_disks)
94b53b77
JS
1307 fprintf(stderr, " (out of %d)",
1308 content->array.raid_disks);
ddc1b11f
N
1309 fprintf(stderr, ", but not started.\n");
1310 return 2;
1311 }
1312 if (c->verbose >= -1) {
94b53b77
JS
1313 pr_err("%s assembled from %d drive%s",
1314 mddev, okcnt, okcnt == 1 ? "" : "s");
ddc1b11f 1315 if (rebuilding_cnt)
94b53b77
JS
1316 fprintf(stderr, "%s %d rebuilding",
1317 sparecnt ? "," : " and", rebuilding_cnt);
ddc1b11f 1318 if (sparecnt)
94b53b77
JS
1319 fprintf(stderr, " and %d spare%s", sparecnt,
1320 sparecnt == 1 ? "" : "s");
ddc1b11f
N
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,
94b53b77 1326 content->array.layout, clean, avail))
7a862a02 1327 fprintf(stderr, " - not enough to start the array while not clean - consider --force.\n");
ddc1b11f
N
1328 else {
1329 if (req_cnt == (unsigned)content->array.raid_disks)
94b53b77
JS
1330 fprintf(stderr, " - need all %d to start it",
1331 req_cnt);
ddc1b11f 1332 else
aacb2f81 1333 fprintf(stderr, " - need %d to start", req_cnt);
ddc1b11f
N
1334 fprintf(stderr, " (use --run to insist).\n");
1335 }
1336 }
1337 return 1;
1338}
1339
95425a89
N
1340int 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 */
57908e9e
GJ
1399 int rv = -1;
1400 int mdfd = -1;
95425a89
N
1401 int clean;
1402 int auto_assem = (mddev == NULL && !ident->uuid_set &&
fc54fe7a
JS
1403 ident->super_minor == UnSet && ident->name[0] == 0 &&
1404 (ident->container == NULL || ident->member == NULL));
57908e9e 1405 struct devs *devices = NULL;
95425a89
N
1406 char *devmap;
1407 int *best = NULL; /* indexed by raid_disk */
1408 int bestcnt = 0;
2c355c22 1409 int devcnt;
69a48116 1410 unsigned int okcnt, sparecnt, rebuilding_cnt, replcnt, journalcnt;
dbfbca43 1411 int journal_clean = 0;
95425a89 1412 int i;
8cde842b 1413 int was_forced = 0;
95425a89
N
1414 int most_recent = 0;
1415 int chosen_drive;
1416 int change = 0;
1417 int inargv = 0;
95425a89
N
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;
95425a89
N
1426 char *name = NULL;
1427 char chosen_name[1024];
1428 struct map_ent *map = NULL;
1429 struct map_ent *mp;
1430
95425a89
N
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
79f9f56d 1455try_again:
95425a89
N
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;
9f2e55a4 1467 if (st && c->force)
cb8f6859 1468 st->ignore_hw_compat = 1;
95425a89
N
1469 num_devs = select_devices(devlist, ident, &st, &content, c,
1470 inargv, auto_assem);
1471 if (num_devs < 0)
1472 return 1;
1011e834 1473
98dbd966 1474 if (!st || !st->sb || !content)
c30e5369
N
1475 return 2;
1476
0431869c
N
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");
3a87fa67 1491 if (c->update == UOPT_UUID)
ec6db5ba
N
1492 mp = NULL;
1493 else
1494 mp = map_by_uuid(&map, content->uuid);
0431869c
N
1495 if (mp) {
1496 struct mdinfo *dv;
1497 /* array already exists. */
4dd2df09 1498 pre_exist = sysfs_read(-1, mp->devnm, GET_LEVEL|GET_DEVS);
0431869c
N
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");
57908e9e 1503 goto out;
0431869c
N
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);
4dd2df09 1531 mdfd = open_dev_excl(mp->devnm);
0431869c
N
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 }
7758ada9
KT
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 }
0431869c
N
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;
ac2ecf55 1556
6f2af6a4 1557 if (!name[0] && is_container(content->array.level)) {
0431869c
N
1558 name = content->text_version;
1559 trustworthy = METADATA;
1560 }
0ac91628 1561
0431869c
N
1562 if (name[0] && trustworthy != LOCAL &&
1563 ! c->require_homehost &&
1564 conf_name_is_free(name))
1565 trustworthy = LOCAL;
0ac91628 1566
0431869c
N
1567 if (trustworthy == LOCAL &&
1568 strchr(name, ':'))
1569 /* Ignore 'host:' prefix of name */
1570 name = strchr(name, ':')+1;
7cdc0872 1571
0431869c 1572 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
cd6cbb08 1573 chosen_name, 0);
0431869c 1574 }
c30e5369
N
1575 if (mdfd < 0) {
1576 st->ss->free_super(st);
c30e5369 1577 if (auto_assem)
60e1bc1a 1578 goto try_again;
57908e9e 1579 goto out;
c30e5369 1580 }
a04d5763 1581 mddev = chosen_name;
0431869c 1582 if (pre_exist == NULL) {
4dd2df09 1583 if (mddev_busy(fd2devnm(mdfd))) {
0431869c
N
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;
57908e9e 1598 goto out;
0431869c
N
1599 }
1600 /* just incase it was started but has no content */
1601 ioctl(mdfd, STOP_ARRAY, NULL);
8a46fe84
NB
1602 }
1603
9008ed1c
N
1604 if (content != &info) {
1605 /* This is a member of a container. Try starting the array. */
588bebfc 1606 int err;
11b6d91d 1607 err = assemble_container_content(st, mdfd, content, c,
9ca39acb 1608 chosen_name, NULL);
588bebfc 1609 close(mdfd);
588bebfc 1610 return err;
9008ed1c 1611 }
32141c17 1612
811e6cbe 1613 /* Ok, no bad inconsistancy, we can try updating etc */
503975b9
N
1614 devices = xcalloc(num_devs, sizeof(*devices));
1615 devmap = xcalloc(num_devs, content->array.raid_disks);
df842e69 1616 devcnt = load_devices(devices, devmap, ident, &st, devlist,
2c355c22
N
1617 c, content, mdfd, mddev,
1618 &most_recent, &bestcnt, &best, inargv);
76781701 1619 if (devcnt < 0) {
57908e9e 1620 mdfd = -3;
783a4a93
GJ
1621 /*
1622 * devices is already freed in load_devices, so set devices
1623 * to NULL to avoid double free devices.
1624 */
1625 devices = NULL;
57908e9e 1626 goto out;
76781701 1627 }
4b1ac34b 1628
64c4757e 1629 if (devcnt == 0) {
e7b84f9d 1630 pr_err("no devices found for %s\n",
79f9f56d 1631 mddev);
3d2b16e7
NB
1632 if (st)
1633 st->ss->free_super(st);
02e7c5b7 1634 free(devmap);
57908e9e 1635 goto out;
64c4757e 1636 }
4b1ac34b 1637
3a87fa67 1638 if (c->update == UOPT_BYTEORDER)
3d2b16e7
NB
1639 st->minor_version = 90;
1640
a5d85af7 1641 st->ss->getinfo_super(st, content, NULL);
98dbd966 1642 clean = content->array.state & 1;
4b1ac34b 1643
64c4757e
NB
1644 /* now we have some devices that might be suitable.
1645 * I wonder how many
1646 */
503975b9 1647 avail = xcalloc(content->array.raid_disks, 1);
64c4757e 1648 okcnt = 0;
aacb2f81 1649 replcnt = 0;
52826846 1650 sparecnt=0;
69a48116 1651 journalcnt=0;
1ff98339 1652 rebuilding_cnt=0;
f21e18ca 1653 for (i=0; i< bestcnt; i++) {
64c4757e 1654 int j = best[i];
ee04451c
NB
1655 int event_margin = 1; /* always allow a difference of '1'
1656 * like the kernel does
1657 */
64c4757e 1658 if (j < 0) continue;
d013a55e
NB
1659 /* note: we ignore error flags in multipath arrays
1660 * as they don't make sense
1661 */
69a48116
SL
1662 if (content->array.level != LEVEL_MULTIPATH) {
1663 if (devices[j].i.disk.state & (1<<MD_DISK_JOURNAL)) {
051f3265 1664 if (content->journal_device_required)
69a48116
SL
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))) {
213ee40b 1669 if (!(devices[j].i.disk.state
ed7fc6b4
AC
1670 & (1<<MD_DISK_FAULTY))) {
1671 devices[j].uptodate = 1;
aa88f531 1672 sparecnt++;
ed7fc6b4 1673 }
d013a55e 1674 continue;
aa88f531 1675 }
69a48116 1676 }
de5a472e 1677 /* If this device thinks that 'most_recent' has failed, then
02e7c5b7
N
1678 * we must reject this device.
1679 */
eb20ecf1 1680 if (j != most_recent && !c->force &&
02e7c5b7
N
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) {
4977146a 1684 if (c->verbose > -1)
e7b84f9d 1685 pr_err("ignoring %s as it reports %s as failed\n",
79f9f56d 1686 devices[j].devname, devices[most_recent].devname);
02e7c5b7
N
1687 best[i] = -1;
1688 continue;
1689 }
f80057ae
N
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 */
213ee40b 1694 if (devices[j].i.events+event_margin >=
f80057ae
N
1695 devices[most_recent].i.events &&
1696 devices[j].i.events <=
1697 devices[most_recent].i.events
1698 ) {
64c4757e 1699 devices[j].uptodate = 1;
69a48116 1700 if (devices[j].i.disk.state & (1<<MD_DISK_JOURNAL))
dbfbca43 1701 journal_clean = 1;
aacb2f81 1702 if (i < content->array.raid_disks * 2) {
dcc4210f
DW
1703 if (devices[j].i.recovery_start == MaxSector ||
1704 (content->reshape_active &&
d316dba7 1705 i >= content->array.raid_disks - content->delta_disks)) {
aacb2f81
N
1706 if (!avail[i/2]) {
1707 okcnt++;
1708 avail[i/2]=1;
1709 } else
1710 replcnt++;
1ff98339
N
1711 } else
1712 rebuilding_cnt++;
69a48116 1713 } else if (devices[j].i.disk.raid_disk != MD_DISK_ROLE_JOURNAL)
52826846 1714 sparecnt++;
64c4757e
NB
1715 }
1716 }
02e7c5b7 1717 free(devmap);
8cde842b
N
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 }
52826846
NB
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 */
cd29a5c8 1731 chosen_drive = -1;
3da92f27 1732 st->ss->free_super(st);
f81a2b56 1733 for (i=0; chosen_drive < 0 && i<bestcnt; i+=2) {
52826846
NB
1734 int j = best[i];
1735 int fd;
4b1ac34b 1736
52826846
NB
1737 if (j<0)
1738 continue;
1739 if (!devices[j].uptodate)
1740 continue;
a28232b8
N
1741 if (devices[j].i.events < devices[most_recent].i.events)
1742 continue;
52826846 1743 chosen_drive = j;
0431869c
N
1744 if ((fd=dev_open(devices[j].devname,
1745 devices[j].included ? O_RDONLY
1746 : (O_RDONLY|O_EXCL)))< 0) {
e7b84f9d 1747 pr_err("Cannot open %s: %s\n",
79f9f56d 1748 devices[j].devname, strerror(errno));
57908e9e 1749 goto out;
52826846 1750 }
3da92f27 1751 if (st->ss->load_super(st,fd, NULL)) {
52826846 1752 close(fd);
e7b84f9d 1753 pr_err("RAID superblock has disappeared from %s\n",
79f9f56d 1754 devices[j].devname);
57908e9e 1755 goto out;
52826846
NB
1756 }
1757 close(fd);
1758 }
3da92f27 1759 if (st->sb == NULL) {
e7b84f9d 1760 pr_err("No suitable drives found for %s\n", mddev);
57908e9e 1761 goto out;
4b1ac34b 1762 }
a5d85af7 1763 st->ss->getinfo_super(st, content, NULL);
dae13137
JS
1764 if (sysfs_init(content, mdfd, NULL)) {
1765 pr_err("Unable to initialize sysfs\n");
57908e9e 1766 goto out;
dae13137 1767 }
32141c17 1768
dbfbca43
SL
1769 /* after reload context, store journal_clean in context */
1770 content->journal_clean = journal_clean;
56eedc1a 1771 for (i=0; i<bestcnt; i++) {
52826846 1772 int j = best[i];
98c6faba 1773 unsigned int desired_state;
11a3e71d 1774
31b6f0cd
AR
1775 if (j < 0)
1776 continue;
69a48116
SL
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)
11a3e71d 1780 desired_state = 0;
aacb2f81
N
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);
11a3e71d 1785
0833f9c3
GK
1786 desired_state |= devices[j].i.disk.state & ((1<<MD_DISK_FAILFAST) |
1787 (1<<MD_DISK_WRITEMOSTLY));
1788
52826846
NB
1789 if (!devices[j].uptodate)
1790 continue;
4b1ac34b 1791
213ee40b 1792 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1793 if (!(devices[j].i.array.state & 1))
1794 clean = 0;
213ee40b 1795
03312b52 1796 if (st->ss->update_super(st, &devices[j].i, UOPT_SPEC_ASSEMBLE, NULL,
4977146a
N
1797 c->verbose, 0, NULL)) {
1798 if (c->force) {
1799 if (c->verbose >= 0)
e7b84f9d 1800 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
79f9f56d 1801 j, mddev, devices[j].devname);
4b1ac34b 1802 change = 1;
52826846 1803 } else {
4977146a 1804 if (c->verbose >= -1)
e7b84f9d 1805 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
79f9f56d 1806 i, mddev, devices[j].devname);
52826846
NB
1807 }
1808 }
52826846 1809 }
6f2af6a4 1810 if (c->force && !clean && !is_container(content->array.level) &&
98dbd966 1811 !enough(content->array.level, content->array.raid_disks,
5c3c3df6 1812 content->array.layout, clean, avail)) {
03312b52 1813 change += st->ss->update_super(st, content, UOPT_SPEC_FORCE_ARRAY,
6f4dbdc4 1814 devices[chosen_drive].devname, c->verbose,
67a8c82d 1815 0, NULL);
8cde842b 1816 was_forced = 1;
583315d9 1817 clean = 1;
d013a55e 1818 }
52826846 1819
4b1ac34b 1820 if (change) {
52826846 1821 int fd;
0431869c
N
1822 fd = dev_open(devices[chosen_drive].devname,
1823 devices[chosen_drive].included ?
1824 O_RDWR : (O_RDWR|O_EXCL));
52826846 1825 if (fd < 0) {
e7b84f9d 1826 pr_err("Could not open %s for write - cannot Assemble array.\n",
79f9f56d 1827 devices[chosen_drive].devname);
57908e9e 1828 goto out;
52826846 1829 }
3da92f27 1830 if (st->ss->store_super(st, fd)) {
52826846 1831 close(fd);
e7b84f9d 1832 pr_err("Could not re-write superblock on %s\n",
79f9f56d 1833 devices[chosen_drive].devname);
57908e9e 1834 goto out;
52826846 1835 }
4977146a 1836 if (c->verbose >= 0)
e7b84f9d 1837 pr_err("Marking array %s as 'clean'\n",
79f9f56d 1838 mddev);
52826846 1839 close(fd);
52826846
NB
1840 }
1841
353632d9
NB
1842 /* If we are in the middle of a reshape we may need to restore saved data
1843 * that was moved aside due to the reshape overwriting live data
1844 * The code of doing this lives in Grow.c
1845 */
5e88ab2e
N
1846 if (content->reshape_active &&
1847 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
353632d9 1848 int err = 0;
503975b9 1849 int *fdlist = xmalloc(sizeof(int)* bestcnt);
4977146a 1850 if (c->verbose > 0)
c34fef77 1851 pr_err("%s has an active reshape - checking if critical section needs to be restored\n",
79f9f56d 1852 chosen_name);
54ded86f
N
1853 if (!c->backup_file)
1854 c->backup_file = locate_backup(content->sys_name);
a7dec3fd 1855 enable_fds(bestcnt/2);
5e9fd96f
N
1856 for (i = 0; i < bestcnt/2; i++) {
1857 int j = best[i*2];
353632d9 1858 if (j >= 0) {
0431869c
N
1859 fdlist[i] = dev_open(devices[j].devname,
1860 devices[j].included
1861 ? O_RDWR : (O_RDWR|O_EXCL));
353632d9 1862 if (fdlist[i] < 0) {
e7b84f9d 1863 pr_err("Could not open %s for write - cannot Assemble array.\n",
79f9f56d 1864 devices[j].devname);
353632d9
NB
1865 err = 1;
1866 break;
1867 }
1868 } else
1869 fdlist[i] = -1;
1870 }
87f26d14 1871 if (!err) {
ba53ea59
AK
1872 if (st->ss->external && st->ss->recover_backup)
1873 err = st->ss->recover_backup(st, content);
1874 else
5e9fd96f 1875 err = Grow_restart(st, content, fdlist, bestcnt/2,
4977146a
N
1876 c->backup_file, c->verbose > 0);
1877 if (err && c->invalid_backup) {
1878 if (c->verbose > 0)
7a862a02 1879 pr_err("continuing without restoring backup\n");
87f26d14
N
1880 err = 0;
1881 }
1882 }
353632d9
NB
1883 while (i>0) {
1884 i--;
1885 if (fdlist[i]>=0) close(fdlist[i]);
1886 }
56dcaa6b 1887 free(fdlist);
353632d9 1888 if (err) {
e7b84f9d 1889 pr_err("Failed to restore critical section for reshape, sorry.\n");
4977146a 1890 if (c->backup_file == NULL)
e7b84f9d 1891 cont_err("Possibly you needed to specify the --backup-file\n");
57908e9e 1892 goto out;
353632d9
NB
1893 }
1894 }
aa88f531 1895
64c4757e 1896 /* Almost ready to actually *do* something */
6f4dbdc4
N
1897 /* First, fill in the map, so that udev can find our name
1898 * as soon as we become active.
1899 */
3a87fa67 1900 if (c->update == UOPT_METADATA) {
afa368f4
N
1901 content->array.major_version = 1;
1902 content->array.minor_version = 0;
1903 strcpy(content->text_version, "1.0");
1904 }
1905
4dd2df09 1906 map_update(&map, fd2devnm(mdfd), content->text_version,
6f4dbdc4
N
1907 content->uuid, chosen_name);
1908
1909 rv = start_array(mdfd, mddev, content,
1910 st, ident, best, bestcnt,
1911 chosen_drive, devices, okcnt, sparecnt,
69a48116 1912 rebuilding_cnt, journalcnt,
6f4dbdc4 1913 c,
71417de6
N
1914 clean, avail, start_partial_ok,
1915 pre_exist != NULL,
051f3265 1916 was_forced);
6f4dbdc4
N
1917 if (rv == 1 && !pre_exist)
1918 ioctl(mdfd, STOP_ARRAY, NULL);
6f4dbdc4 1919 free(devices);
57908e9e 1920out:
898bd1ec 1921 map_unlock(&map);
6f4dbdc4
N
1922 if (rv == 0) {
1923 wait_for(chosen_name, mdfd);
b20c8a50 1924 close(mdfd);
6f4dbdc4
N
1925 if (auto_assem) {
1926 int usecs = 1;
1927 /* There is a nasty race with 'mdadm --monitor'.
1928 * If it opens this device before we close it,
1929 * it gets an incomplete open on which IO
1930 * doesn't work and the capacity is
1931 * wrong.
1932 * If we reopen (to check for layered devices)
1933 * before --monitor closes, we loose.
1934 *
1935 * So: wait upto 1 second for there to be
1936 * a non-zero capacity.
1937 */
1938 while (usecs < 1000) {
1939 mdfd = open(mddev, O_RDONLY);
1940 if (mdfd >= 0) {
1941 unsigned long long size;
1942 if (get_dev_size(mdfd, NULL, &size) &&
1943 size > 0)
1944 break;
1945 close(mdfd);
589395d6 1946 }
239b3cc0 1947 sleep_for(0, USEC_TO_NSEC(usecs), true);
6f4dbdc4 1948 usecs <<= 1;
dab6685f 1949 }
aa88f531 1950 }
57908e9e 1951 } else if (mdfd >= 0)
b20c8a50
N
1952 close(mdfd);
1953
e9fb93af 1954 free(best);
6f4dbdc4 1955 /* '2' means 'OK, but not started yet' */
57908e9e
GJ
1956 if (rv == -1) {
1957 free(devices);
1958 return 1;
1959 }
6f4dbdc4 1960 return rv == 2 ? 0 : rv;
64c4757e 1961}
6234c63c 1962
6234c63c 1963int assemble_container_content(struct supertype *st, int mdfd,
11b6d91d 1964 struct mdinfo *content, struct context *c,
9ca39acb 1965 char *chosen_name, int *result)
6234c63c 1966{
02b70e83 1967 struct mdinfo *dev, *sra, *dev2;
92a647c8 1968 struct assembly_array_info array = {chosen_name, 0, 0, 0};
7af03341 1969 int old_raid_disks;
4aecb54a 1970 int start_reshape;
0a8e239c 1971 char *avail;
c1736844 1972 int err;
70f1ff42
LF
1973 int is_clean, all_disks;
1974 bool is_raid456;
6234c63c 1975
dae13137
JS
1976 if (sysfs_init(content, mdfd, NULL)) {
1977 pr_err("Unable to initialize sysfs\n");
1978 return 1;
1979 }
6234c63c 1980
02b70e83 1981 sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
626bc453
MG
1982 if (sra == NULL) {
1983 pr_err("Failed to read sysfs parameters\n");
1984 return 1;
1985 }
1986
d8d09c16
PP
1987 /* Fill sysfs properties only if they are not set. Determine it by checking text_version
1988 * and ignoring special character on the first place.
1989 */
1990 if (strcmp(sra->text_version + 1, content->text_version + 1) != 0) {
de23e12a 1991 if (sysfs_set_array(content) != 0) {
30e19bf8 1992 sysfs_free(sra);
6234c63c 1993 return 1;
22472ee1 1994 }
0ea8f5b1 1995 }
588bebfc 1996
4aecb54a
AK
1997 /* There are two types of reshape: container wide or sub-array specific
1998 * Check if metadata requests blocking container wide reshapes
1999 */
2000 start_reshape = (content->reshape_active &&
79f9f56d
N
2001 !((content->reshape_active == CONTAINER_RESHAPE) &&
2002 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
4aecb54a
AK
2003
2004 /* Block subarray here if it is under reshape now
2005 * Do not allow for any changes in this array
2006 */
2007 if (st->ss->external && content->recovery_blocked && start_reshape)
b8063f07
AK
2008 block_subarray(content);
2009
02b70e83
N
2010 for (dev2 = sra->devs; dev2; dev2 = dev2->next) {
2011 for (dev = content->devs; dev; dev = dev->next)
2012 if (dev2->disk.major == dev->disk.major &&
2013 dev2->disk.minor == dev->disk.minor)
2014 break;
2015 if (dev)
2016 continue;
2017 /* Don't want this one any more */
b823c8f9 2018 if (sysfs_set_str(sra, dev2, "slot", STR_COMMON_NONE) < 0 && errno == EBUSY) {
02b70e83
N
2019 pr_err("Cannot remove old device %s: not updating %s\n", dev2->sys_name, sra->sys_name);
2020 sysfs_free(sra);
2021 return 1;
2022 }
2023 sysfs_set_str(sra, dev2, "state", "remove");
2024 }
7af03341 2025 old_raid_disks = content->array.raid_disks - content->delta_disks;
c1736844
N
2026 avail = xcalloc(content->array.raid_disks, 1);
2027 for (dev = content->devs; dev; dev = dev->next) {
2028 if (dev->disk.raid_disk >= 0)
2029 avail[dev->disk.raid_disk] = 1;
14032016 2030 if (sysfs_add_disk(content, dev, 1) == 0) {
7af03341 2031 if (dev->disk.raid_disk >= old_raid_disks &&
14032016 2032 content->reshape_active)
92a647c8 2033 array.exp_cnt++;
14032016 2034 else
92a647c8 2035 array.new_cnt++;
14032016 2036 } else if (errno == EEXIST)
92a647c8 2037 array.preexist_cnt++;
c1736844 2038 }
02b70e83 2039 sysfs_free(sra);
92a647c8
MT
2040
2041 all_disks = array.new_cnt + array.exp_cnt + array.preexist_cnt;
2042
0c79d8ca 2043 map_update(NULL, fd2devnm(mdfd), content->text_version,
8b4e5ea9
N
2044 content->uuid, chosen_name);
2045
2432ce9b
AP
2046 if (content->consistency_policy == CONSISTENCY_POLICY_PPL &&
2047 st->ss->validate_ppl) {
2048 content->array.state |= 1;
2049 err = 0;
2050
2051 for (dev = content->devs; dev; dev = dev->next) {
2052 int dfd;
2053 char *devpath;
2054 int ret;
2055
2056 ret = st->ss->validate_ppl(st, content, dev);
2057 if (ret == 0)
2058 continue;
2059
2060 if (ret < 0) {
2061 err = 1;
2062 break;
2063 }
2064
2065 if (!c->force) {
2066 pr_err("%s contains invalid PPL - consider --force or --update-subarray with --update=no-ppl\n",
2067 chosen_name);
2068 content->array.state &= ~1;
2069 avail[dev->disk.raid_disk] = 0;
2070 break;
2071 }
2072
2073 /* have --force - overwrite the invalid ppl */
2074 devpath = map_dev(dev->disk.major, dev->disk.minor, 0);
2075 dfd = dev_open(devpath, O_RDWR);
2076 if (dfd < 0) {
2077 pr_err("Failed to open %s\n", devpath);
2078 err = 1;
2079 break;
2080 }
2081
2082 err = st->ss->write_init_ppl(st, content, dfd);
2083 close(dfd);
2084
2085 if (err)
2086 break;
2087 }
2088
2089 if (err) {
2090 free(avail);
2091 return err;
2092 }
7b99edab
MT
2093 } else if (c->force) {
2094 /* Set the array as 'clean' so that we can proceed with starting
2095 * it even if we don't have all devices. Mdmon doesn't care
2096 * if the dirty flag is set in metadata, it will start managing
2097 * it anyway.
2098 * This is really important for raid456 (RWH case), other levels
2099 * are started anyway.
2100 */
2101 content->array.state |= 1;
2432ce9b
AP
2102 }
2103
70f1ff42 2104 is_raid456 = is_level456(content->array.level);
92a647c8
MT
2105 is_clean = content->array.state & 1;
2106
c1736844 2107 if (enough(content->array.level, content->array.raid_disks,
92a647c8
MT
2108 content->array.layout, is_clean, avail) == 0) {
2109 set_array_assembly_status(c, result, INCR_NO, &array);
2110
2111 if (c->verbose >= 0 && is_raid456 && !is_clean)
2112 pr_err("Consider --force to start dirty degraded array\n");
2113
c1736844
N
2114 free(avail);
2115 return 1;
2116 }
2117 free(avail);
2118
92a647c8
MT
2119 if (c->runstop <= 0 && all_disks < content->array.working_disks) {
2120
2121 set_array_assembly_status(c, result, INCR_UNSAFE, &array);
2122
2123 if (c->verbose >= 0 && c->force)
2124 pr_err("Consider --run to start array as degraded.\n");
2125 return 1;
2126 }
2127
2128 if (is_raid456 && content->resync_start != MaxSector && c->force &&
2129 all_disks < content->array.raid_disks) {
2130
2131 content->resync_start = MaxSector;
2132 err = sysfs_set_num(content, NULL, "resync_start", MaxSector);
2133 if (err)
7b99edab 2134 return 1;
92a647c8
MT
2135
2136 pr_err("%s array state forced to clean. It may cause data corruption.\n",
2137 chosen_name);
7cb2aa33 2138 }
c1736844 2139
b554ab5c
JR
2140 /*
2141 * Before activating the array, perform extra steps required
2142 * to configure the internal write-intent bitmap.
2143 */
2144 if (content->consistency_policy == CONSISTENCY_POLICY_BITMAP &&
2145 st->ss->set_bitmap)
2146 st->ss->set_bitmap(st, content);
c1736844
N
2147
2148 if (start_reshape) {
92a647c8 2149 int spare = content->array.raid_disks + array.exp_cnt;
c1736844 2150 if (restore_backup(st, content,
92a647c8 2151 array.new_cnt,
54ded86f 2152 spare, &c->backup_file, c->verbose) == 1)
c1736844
N
2153 return 1;
2154
2b57e4fe
PB
2155 if (content->reshape_progress == 0) {
2156 /* If reshape progress is 0 - we are assembling the
2157 * array that was stopped, before reshape has started.
2158 * Array needs to be started as active, Grow_continue()
2159 * will start the reshape.
2160 */
2161 sysfs_set_num(content, NULL, "reshape_position",
2162 MaxSector);
2163 err = sysfs_set_str(content, NULL,
2164 "array_state", "active");
2165 sysfs_set_num(content, NULL, "reshape_position", 0);
2166 } else {
2167 err = sysfs_set_str(content, NULL,
2168 "array_state", "readonly");
2169 }
2170
c1736844
N
2171 if (err)
2172 return 1;
2173
2174 if (st->ss->external) {
2175 if (!mdmon_running(st->container_devnm))
2176 start_mdmon(st->container_devnm);
2177 ping_monitor(st->container_devnm);
2178 if (mdmon_running(st->container_devnm) &&
2179 st->update_tail == NULL)
2180 st->update_tail = &st->updates;
2181 }
2182
2183 err = Grow_continue(mdfd, st, content, c->backup_file,
06e293d0 2184 0, c->freeze_reshape);
c1736844
N
2185 } else switch(content->array.level) {
2186 case LEVEL_LINEAR:
2187 case LEVEL_MULTIPATH:
2188 case 0:
2189 err = sysfs_set_str(content, NULL, "array_state",
2190 c->readonly ? "readonly" : "active");
2191 break;
2192 default:
2193 err = sysfs_set_str(content, NULL, "array_state",
2194 "readonly");
2195 /* start mdmon if needed. */
2196 if (!err) {
2197 if (!mdmon_running(st->container_devnm))
2198 start_mdmon(st->container_devnm);
2199 ping_monitor(st->container_devnm);
2200 }
2201 break;
2202 }
2203 if (!err)
2204 sysfs_set_safemode(content, content->safe_mode_delay);
2205
2206 /* Block subarray here if it is not reshaped now
2207 * It has be blocked a little later to allow mdmon to switch in
2208 * in to R/W state
2209 */
2210 if (st->ss->external && content->recovery_blocked &&
2211 !start_reshape)
2212 block_subarray(content);
2213
92a647c8
MT
2214 if (err)
2215 set_array_assembly_status(c, result, INCR_NO, &array);
2216 else {
2217 set_array_assembly_status(c, result, INCR_YES, &array);
c1736844 2218 wait_for(chosen_name, mdfd);
92a647c8
MT
2219 sysfs_rules_apply(chosen_name, content);
2220 }
2221
c1736844
N
2222 return err;
2223 /* FIXME should have an O_EXCL and wait for read-auto */
6234c63c 2224}