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