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