]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Assemble: split out select_devices function.
[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
517int Assemble(struct supertype *st, char *mddev,
518 struct mddev_ident *ident,
519 struct mddev_dev *devlist,
520 struct context *c)
521{
522 /*
523 * The task of Assemble is to find a collection of
524 * devices that should (according to their superblocks)
525 * form an array, and to give this collection to the MD driver.
526 * In Linux-2.4 and later, this involves submitting a
527 * SET_ARRAY_INFO ioctl with no arg - to prepare
528 * the array - and then submit a number of
529 * ADD_NEW_DISK ioctls to add disks into
530 * the array. Finally RUN_ARRAY might
531 * be submitted to start the array.
532 *
533 * Much of the work of Assemble is in finding and/or
534 * checking the disks to make sure they look right.
535 *
536 * If mddev is not set, then scan must be set and we
537 * read through the config file for dev+uuid mapping
538 * We recurse, setting mddev, for each device that
539 * - isn't running
540 * - has a valid uuid (or any uuid if !uuidset)
541 *
542 * If mddev is set, we try to determine state of md.
543 * check version - must be at least 0.90.0
544 * check kernel version. must be at least 2.4.
545 * If not, we can possibly fall back on START_ARRAY
546 * Try to GET_ARRAY_INFO.
547 * If possible, give up
548 * If not, try to STOP_ARRAY just to make sure
549 *
550 * If !uuidset and scan, look in conf-file for uuid
551 * If not found, give up
552 * If !devlist and scan and uuidset, get list of devs from conf-file
553 *
554 * For each device:
555 * Check superblock - discard if bad
556 * Check uuid (set if we don't have one) - discard if no match
557 * Check superblock similarity if we have a superblock - discard if different
558 * Record events, devicenum
559 * This should give us a list of devices for the array
560 * We should collect the most recent event number
561 *
562 * Count disks with recent enough event count
563 * While force && !enough disks
564 * Choose newest rejected disks, update event count
565 * mark clean and rewrite superblock
566 * If recent kernel:
567 * SET_ARRAY_INFO
568 * foreach device with recent events : ADD_NEW_DISK
569 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
570 * If old kernel:
571 * Check the device numbers in superblock are right
572 * update superblock if any changes
573 * START_ARRAY
574 *
575 */
576 int mdfd;
577 int clean;
578 int auto_assem = (mddev == NULL && !ident->uuid_set &&
579 ident->super_minor == UnSet && ident->name[0] == 0
580 && (ident->container == NULL || ident->member == NULL));
581 int old_linux = 0;
582 int vers = vers; /* Keep gcc quite - it really is initialised */
583 struct {
584 char *devname;
585 int uptodate; /* set once we decide that this device is as
586 * recent as everything else in the array.
587 */
588 int included; /* set if the device is already in the array
589 * due to a previous '-I'
590 */
591 struct mdinfo i;
592 } *devices;
593 char *devmap;
594 int *best = NULL; /* indexed by raid_disk */
595 int bestcnt = 0;
596 int devcnt = 0;
597 unsigned int okcnt, sparecnt, rebuilding_cnt;
598 unsigned int req_cnt;
599 int i;
600 int most_recent = 0;
601 int chosen_drive;
602 int change = 0;
603 int inargv = 0;
604#ifndef MDASSEMBLE
605 int bitmap_done;
606#endif
607 int start_partial_ok = (c->runstop >= 0) &&
608 (c->force || devlist==NULL || auto_assem);
609 int num_devs;
610 struct mddev_dev *tmpdev;
611 struct mdinfo info;
612 struct mdinfo *content = NULL;
613 struct mdinfo *pre_exist = NULL;
614 char *avail;
615 int nextspare = 0;
616 char *name = NULL;
617 char chosen_name[1024];
618 struct map_ent *map = NULL;
619 struct map_ent *mp;
620
621 if (get_linux_version() < 2004000)
622 old_linux = 1;
623
624 /*
625 * If any subdevs are listed, then any that don't
626 * match ident are discarded. Remainder must all match and
627 * become the array.
628 * If no subdevs, then we scan all devices in the config file, but
629 * there must be something in the identity
630 */
631
632 if (!devlist &&
633 ident->uuid_set == 0 &&
634 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
635 ident->name[0] == 0 &&
636 (ident->container == NULL || ident->member == NULL) &&
637 ident->devices == NULL) {
638 pr_err("No identity information available for %s - cannot assemble.\n",
639 mddev ? mddev : "further assembly");
640 return 1;
641 }
642
643 if (devlist == NULL)
644 devlist = conf_get_devs();
645 else if (mddev)
646 inargv = 1;
647
648 try_again:
649 /* We come back here when doing auto-assembly and attempting some
650 * set of devices failed. Those are now marked as ->used==2 and
651 * we ignore them and try again
652 */
653 if (!st && ident->st)
654 st = ident->st;
655 if (c->verbose>0)
656 pr_err("looking for devices for %s\n",
657 mddev ? mddev : "further assembly");
658
659 content = &info;
660 num_devs = select_devices(devlist, ident, &st, &content, c,
661 inargv, auto_assem);
662 if (num_devs < 0)
663 return 1;
cbeeb0e5 664
98dbd966 665 if (!st || !st->sb || !content)
c30e5369
N
666 return 2;
667
0431869c
N
668 /* We have a full set of devices - we now need to find the
669 * array device.
670 * However there is a risk that we are racing with "mdadm -I"
671 * and the array is already partially assembled - we will have
672 * rejected any devices already in this address.
673 * So we take a lock on the map file - to prevent further races -
674 * and look for the uuid in there. If found and the array is
675 * active, we abort. If found and the array is not active
676 * we commit to that md device and add all the contained devices
677 * to our list. We flag them so that we don't try to re-add,
678 * but can remove if they turn out to not be wanted.
679 */
680 if (map_lock(&map))
681 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
682 mp = map_by_uuid(&map, content->uuid);
683 if (mp) {
684 struct mdinfo *dv;
685 /* array already exists. */
686 pre_exist = sysfs_read(-1, mp->devnum, GET_LEVEL|GET_DEVS);
687 if (pre_exist->array.level != UnSet) {
688 pr_err("Found some drive for an array that is already active: %s\n",
689 mp->path);
690 pr_err("giving up.\n");
691 return 1;
692 }
693 for (dv = pre_exist->devs; dv; dv = dv->next) {
694 /* We want to add this device to our list,
695 * but it could already be there if "mdadm -I"
696 * started *after* we checked for O_EXCL.
697 * If we add it to the top of the list
698 * it will be preferred over later copies.
699 */
700 struct mddev_dev *newdev;
701 char *devname = map_dev(dv->disk.major,
702 dv->disk.minor,
703 0);
704 if (!devname)
705 continue;
706 newdev = xmalloc(sizeof(*newdev));
707 newdev->devname = devname;
708 newdev->disposition = 'I';
709 newdev->used = 1;
710 newdev->next = devlist;
711 devlist = newdev;
712 num_devs++;
713 }
714 strcpy(chosen_name, mp->path);
715 if (c->verbose > 0 || mddev == NULL ||
716 strcmp(mddev, chosen_name) != 0)
717 pr_err("Merging with already-assembled %s\n",
718 chosen_name);
719 mdfd = open_dev_excl(mp->devnum);
720 } else {
721 int trustworthy = FOREIGN;
722 name = content->name;
723 switch (st->ss->match_home(st, c->homehost)
724 ?: st->ss->match_home(st, "any")) {
725 case 1:
726 trustworthy = LOCAL;
727 name = strchr(content->name, ':');
728 if (name)
729 name++;
730 else
731 name = content->name;
732 break;
733 }
734 if (!auto_assem)
735 /* If the array is listed in mdadm.conf or on
736 * command line, then we trust the name
737 * even if the array doesn't look local
738 */
739 trustworthy = LOCAL;
ac2ecf55 740
0431869c
N
741 if (name[0] == 0 &&
742 content->array.level == LEVEL_CONTAINER) {
743 name = content->text_version;
744 trustworthy = METADATA;
745 }
0ac91628 746
0431869c
N
747 if (name[0] && trustworthy != LOCAL &&
748 ! c->require_homehost &&
749 conf_name_is_free(name))
750 trustworthy = LOCAL;
0ac91628 751
0431869c
N
752 if (trustworthy == LOCAL &&
753 strchr(name, ':'))
754 /* Ignore 'host:' prefix of name */
755 name = strchr(name, ':')+1;
7cdc0872 756
0431869c
N
757 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
758 chosen_name);
759 }
c30e5369
N
760 if (mdfd < 0) {
761 st->ss->free_super(st);
c30e5369 762 if (auto_assem)
60e1bc1a 763 goto try_again;
c30e5369
N
764 return 1;
765 }
a04d5763 766 mddev = chosen_name;
c30e5369
N
767 vers = md_get_version(mdfd);
768 if (vers < 9000) {
e7b84f9d 769 pr_err("Assemble requires driver version 0.90.0 or later.\n"
c30e5369
N
770 " Upgrade your kernel or try --build\n");
771 close(mdfd);
772 return 1;
773 }
0431869c
N
774 if (pre_exist == NULL) {
775 if (mddev_busy(fd2devnum(mdfd))) {
776 pr_err("%s already active, cannot restart it!\n",
777 mddev);
778 for (tmpdev = devlist ;
779 tmpdev && tmpdev->used != 1;
780 tmpdev = tmpdev->next)
781 ;
782 if (tmpdev && auto_assem)
783 pr_err("%s needed for %s...\n",
784 mddev, tmpdev->devname);
785 close(mdfd);
786 mdfd = -3;
787 st->ss->free_super(st);
788 if (auto_assem)
789 goto try_again;
790 return 1;
791 }
792 /* just incase it was started but has no content */
793 ioctl(mdfd, STOP_ARRAY, NULL);
8a46fe84
NB
794 }
795
9008ed1c
N
796#ifndef MDASSEMBLE
797 if (content != &info) {
798 /* This is a member of a container. Try starting the array. */
588bebfc 799 int err;
11b6d91d
N
800 err = assemble_container_content(st, mdfd, content, c,
801 chosen_name);
588bebfc 802 close(mdfd);
588bebfc 803 return err;
9008ed1c 804 }
a6482415 805 bitmap_done = 0;
9008ed1c 806#endif
811e6cbe 807 /* Ok, no bad inconsistancy, we can try updating etc */
503975b9
N
808 devices = xcalloc(num_devs, sizeof(*devices));
809 devmap = xcalloc(num_devs, content->array.raid_disks);
da6b5ca9 810 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
811e6cbe
NB
811 char *devname = tmpdev->devname;
812 struct stat stb;
5787fa49 813 /* looks like a good enough match to update the super block if needed */
d1f1011b 814#ifndef MDASSEMBLE
4977146a 815 if (c->update) {
811e6cbe 816 int dfd;
4b1ac34b
NB
817 /* prepare useful information in info structures */
818 struct stat stb2;
3da92f27 819 struct supertype *tst;
1e2b2765 820 int err;
4b1ac34b 821 fstat(mdfd, &stb2);
7d99579f 822
4977146a 823 if (strcmp(c->update, "uuid")==0 &&
7d99579f
NB
824 !ident->uuid_set) {
825 int rfd;
826 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
827 read(rfd, ident->uuid, 16) != 16) {
828 *(__u32*)(ident->uuid) = random();
829 *(__u32*)(ident->uuid+1) = random();
830 *(__u32*)(ident->uuid+2) = random();
831 *(__u32*)(ident->uuid+3) = random();
832 }
833 if (rfd >= 0) close(rfd);
7d99579f 834 }
0431869c
N
835 dfd = dev_open(devname,
836 tmpdev->disposition == 'I'
837 ? O_RDWR : (O_RDWR|O_EXCL));
811e6cbe 838
3da92f27 839 tst = dup_super(st);
9f22b13f 840 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
e7b84f9d 841 pr_err("cannot re-read metadata from %s - aborting\n",
9f22b13f
N
842 devname);
843 if (dfd >= 0)
844 close(dfd);
845 close(mdfd);
d7f7ebb7 846 free(devices);
02e7c5b7 847 free(devmap);
9f22b13f
N
848 return 1;
849 }
02e7c5b7 850 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
811e6cbe 851
98dbd966
DW
852 memcpy(content->uuid, ident->uuid, 16);
853 strcpy(content->name, ident->name);
854 content->array.md_minor = minor(stb2.st_rdev);
811e6cbe 855
4977146a 856 if (strcmp(c->update, "byteorder") == 0)
1e2b2765
N
857 err = 0;
858 else
4977146a
N
859 err = tst->ss->update_super(tst, content, c->update,
860 devname, c->verbose,
1e2b2765 861 ident->uuid_set,
4977146a 862 c->homehost);
1e2b2765 863 if (err < 0) {
e7b84f9d
N
864 pr_err("--update=%s not understood"
865 " for %s metadata\n",
4977146a 866 c->update, tst->ss->name);
1e2b2765
N
867 tst->ss->free_super(tst);
868 free(tst);
869 close(mdfd);
870 close(dfd);
d7f7ebb7 871 free(devices);
02e7c5b7 872 free(devmap);
1e2b2765
N
873 return 1;
874 }
4977146a 875 if (strcmp(c->update, "uuid")==0 &&
e5eac01f
NB
876 !ident->uuid_set) {
877 ident->uuid_set = 1;
98dbd966 878 memcpy(ident->uuid, content->uuid, 16);
e5eac01f 879 }
1e2b2765 880 if (tst->ss->store_super(tst, dfd))
e7b84f9d 881 pr_err("Could not re-write superblock on %s.\n",
5787fa49 882 devname);
1e2b2765 883 close(dfd);
8131b493 884
4977146a 885 if (strcmp(c->update, "uuid")==0 &&
bf4fb153 886 ident->bitmap_fd >= 0 && !bitmap_done) {
3da92f27 887 if (bitmap_update_uuid(ident->bitmap_fd,
98dbd966 888 content->uuid,
3da92f27 889 tst->ss->swapuuid) != 0)
e7b84f9d 890 pr_err("Could not update uuid on external bitmap.\n");
bf4fb153
NB
891 else
892 bitmap_done = 1;
893 }
3da92f27 894 tst->ss->free_super(tst);
d1f1011b
NB
895 } else
896#endif
897 {
30e1b9a5 898 struct supertype *tst = dup_super(st);
da6b5ca9 899 int dfd;
0431869c
N
900 dfd = dev_open(devname,
901 tmpdev->disposition == 'I'
902 ? O_RDWR : (O_RDWR|O_EXCL));
da6b5ca9 903
9f22b13f 904 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
e7b84f9d 905 pr_err("cannot re-read metadata from %s - aborting\n",
9f22b13f
N
906 devname);
907 if (dfd >= 0)
908 close(dfd);
909 close(mdfd);
d7f7ebb7 910 free(devices);
02e7c5b7 911 free(devmap);
9f22b13f
N
912 return 1;
913 }
02e7c5b7 914 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
3da92f27 915 tst->ss->free_super(tst);
da6b5ca9 916 close(dfd);
5787fa49
NB
917 }
918
811e6cbe
NB
919 stat(devname, &stb);
920
4977146a 921 if (c->verbose > 0)
e7b84f9d 922 pr_err("%s is identified as a member of %s, slot %d.\n",
98dbd966 923 devname, mddev, content->disk.raid_disk);
64c4757e 924 devices[devcnt].devname = devname;
64c4757e 925 devices[devcnt].uptodate = 0;
0431869c 926 devices[devcnt].included = (tmpdev->disposition == 'I');
98dbd966 927 devices[devcnt].i = *content;
213ee40b
NB
928 devices[devcnt].i.disk.major = major(stb.st_rdev);
929 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
64c4757e 930 if (most_recent < devcnt) {
213ee40b
NB
931 if (devices[devcnt].i.events
932 > devices[most_recent].i.events)
64c4757e
NB
933 most_recent = devcnt;
934 }
df0d4ea0 935 if (content->array.level == LEVEL_MULTIPATH)
5787fa49
NB
936 /* with multipath, the raid_disk from the superblock is meaningless */
937 i = devcnt;
938 else
213ee40b 939 i = devices[devcnt].i.disk.raid_disk;
308e1801 940 if (i+1 == 0) {
98dbd966
DW
941 if (nextspare < content->array.raid_disks)
942 nextspare = content->array.raid_disks;
308e1801 943 i = nextspare++;
08110d41 944 } else {
98dbd966 945 if (i >= content->array.raid_disks &&
08110d41
NB
946 i >= nextspare)
947 nextspare = i+1;
308e1801 948 }
98c6faba 949 if (i < 10000) {
56eedc1a 950 if (i >= bestcnt) {
f21e18ca 951 int newbestcnt = i+10;
503975b9 952 int *newbest = xmalloc(sizeof(int)*newbestcnt);
f21e18ca 953 int c;
56eedc1a
NB
954 for (c=0; c < newbestcnt; c++)
955 if (c < bestcnt)
956 newbest[c] = best[c];
957 else
958 newbest[c] = -1;
959 if (best)free(best);
960 best = newbest;
961 bestcnt = newbestcnt;
962 }
6a255b69 963 if (best[i] >=0 &&
213ee40b
NB
964 devices[best[i]].i.events
965 == devices[devcnt].i.events
966 && (devices[best[i]].i.disk.minor
967 != devices[devcnt].i.disk.minor)
b8ac1967 968 && st->ss == &super0
98dbd966 969 && content->array.level != LEVEL_MULTIPATH) {
6a255b69
NB
970 /* two different devices with identical superblock.
971 * Could be a mis-detection caused by overlapping
972 * partitions. fail-safe.
973 */
e7b84f9d 974 pr_err("WARNING %s and %s appear"
6a255b69
NB
975 " to have very similar superblocks.\n"
976 " If they are really different, "
977 "please --zero the superblock on one\n"
d2df86e0
NB
978 " If they are the same or overlap,"
979 " please remove one from %s.\n",
980 devices[best[i]].devname, devname,
981 inargv ? "the list" :
982 "the\n DEVICE list in mdadm.conf"
983 );
7f91af49 984 close(mdfd);
d7f7ebb7 985 free(devices);
02e7c5b7 986 free(devmap);
6a255b69
NB
987 return 1;
988 }
52826846 989 if (best[i] == -1
213ee40b
NB
990 || (devices[best[i]].i.events
991 < devices[devcnt].i.events))
52826846 992 best[i] = devcnt;
56eedc1a 993 }
64c4757e 994 devcnt++;
df37ffc0 995 }
4b1ac34b 996
64c4757e 997 if (devcnt == 0) {
e7b84f9d 998 pr_err("no devices found for %s\n",
64c4757e 999 mddev);
3d2b16e7
NB
1000 if (st)
1001 st->ss->free_super(st);
7f91af49 1002 close(mdfd);
d7f7ebb7 1003 free(devices);
02e7c5b7 1004 free(devmap);
64c4757e
NB
1005 return 1;
1006 }
4b1ac34b 1007
4977146a 1008 if (c->update && strcmp(c->update, "byteorder")==0)
3d2b16e7
NB
1009 st->minor_version = 90;
1010
a5d85af7 1011 st->ss->getinfo_super(st, content, NULL);
98dbd966 1012 clean = content->array.state & 1;
4b1ac34b 1013
64c4757e
NB
1014 /* now we have some devices that might be suitable.
1015 * I wonder how many
1016 */
503975b9 1017 avail = xcalloc(content->array.raid_disks, 1);
64c4757e 1018 okcnt = 0;
52826846 1019 sparecnt=0;
1ff98339 1020 rebuilding_cnt=0;
f21e18ca 1021 for (i=0; i< bestcnt; i++) {
64c4757e 1022 int j = best[i];
ee04451c
NB
1023 int event_margin = 1; /* always allow a difference of '1'
1024 * like the kernel does
1025 */
64c4757e 1026 if (j < 0) continue;
d013a55e
NB
1027 /* note: we ignore error flags in multipath arrays
1028 * as they don't make sense
1029 */
df0d4ea0 1030 if (content->array.level != LEVEL_MULTIPATH)
f22385f9 1031 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
213ee40b 1032 if (!(devices[j].i.disk.state
ed7fc6b4
AC
1033 & (1<<MD_DISK_FAULTY))) {
1034 devices[j].uptodate = 1;
aa88f531 1035 sparecnt++;
ed7fc6b4 1036 }
d013a55e 1037 continue;
aa88f531 1038 }
de5a472e 1039 /* If this device thinks that 'most_recent' has failed, then
02e7c5b7
N
1040 * we must reject this device.
1041 */
1042 if (j != most_recent &&
1043 content->array.raid_disks > 0 &&
1044 devices[most_recent].i.disk.raid_disk >= 0 &&
1045 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
4977146a 1046 if (c->verbose > -1)
e7b84f9d 1047 pr_err("ignoring %s as it reports %s as failed\n",
02e7c5b7
N
1048 devices[j].devname, devices[most_recent].devname);
1049 best[i] = -1;
1050 continue;
1051 }
213ee40b
NB
1052 if (devices[j].i.events+event_margin >=
1053 devices[most_recent].i.events) {
64c4757e 1054 devices[j].uptodate = 1;
1ff98339 1055 if (i < content->array.raid_disks) {
dcc4210f
DW
1056 if (devices[j].i.recovery_start == MaxSector ||
1057 (content->reshape_active &&
b720636a
N
1058 ((i >= content->array.raid_disks - content->delta_disks) ||
1059 (i >= content->array.raid_disks - content->delta_disks - 1
1060 && content->array.level == 4)))) {
1ff98339
N
1061 okcnt++;
1062 avail[i]=1;
1063 } else
1064 rebuilding_cnt++;
265e0f17 1065 } else
52826846 1066 sparecnt++;
64c4757e
NB
1067 }
1068 }
02e7c5b7 1069 free(devmap);
4977146a 1070 while (c->force &&
da8fe5aa
N
1071 (!enough(content->array.level, content->array.raid_disks,
1072 content->array.layout, 1,
1073 avail)
1074 ||
1075 (content->reshape_active && content->delta_disks > 0 &&
1076 !enough(content->array.level, (content->array.raid_disks
1077 - content->delta_disks),
1078 content->new_layout, 1,
1079 avail)
1080 ))) {
64c4757e
NB
1081 /* Choose the newest best drive which is
1082 * not up-to-date, update the superblock
1083 * and add it.
1084 */
52826846 1085 int fd;
3da92f27 1086 struct supertype *tst;
f21e18ca 1087 unsigned long long current_events;
cd29a5c8 1088 chosen_drive = -1;
f21e18ca 1089 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
52826846
NB
1090 int j = best[i];
1091 if (j>=0 &&
1092 !devices[j].uptodate &&
921d9e16 1093 devices[j].i.recovery_start == MaxSector &&
52826846 1094 (chosen_drive < 0 ||
213ee40b
NB
1095 devices[j].i.events
1096 > devices[chosen_drive].i.events))
52826846
NB
1097 chosen_drive = j;
1098 }
1099 if (chosen_drive < 0)
1100 break;
213ee40b 1101 current_events = devices[chosen_drive].i.events;
c5a6f9a6 1102 add_another:
4977146a 1103 if (c->verbose >= 0)
e7b84f9d 1104 pr_err("forcing event count in %s(%d) from %d upto %d\n",
213ee40b
NB
1105 devices[chosen_drive].devname,
1106 devices[chosen_drive].i.disk.raid_disk,
1107 (int)(devices[chosen_drive].i.events),
1108 (int)(devices[most_recent].i.events));
0431869c
N
1109 fd = dev_open(devices[chosen_drive].devname,
1110 devices[chosen_drive].included ? O_RDWR
1111 : (O_RDWR|O_EXCL));
52826846 1112 if (fd < 0) {
e7b84f9d 1113 pr_err("Couldn't open %s for write - not updating\n",
52826846 1114 devices[chosen_drive].devname);
213ee40b 1115 devices[chosen_drive].i.events = 0;
52826846
NB
1116 continue;
1117 }
3da92f27 1118 tst = dup_super(st);
60b435db 1119 if (tst->ss->load_super(tst,fd, NULL)) {
52826846 1120 close(fd);
e7b84f9d 1121 pr_err("RAID superblock disappeared from %s - not updating.\n",
52826846 1122 devices[chosen_drive].devname);
213ee40b 1123 devices[chosen_drive].i.events = 0;
52826846
NB
1124 continue;
1125 }
98dbd966
DW
1126 content->events = devices[most_recent].i.events;
1127 tst->ss->update_super(tst, content, "force-one",
4977146a 1128 devices[chosen_drive].devname, c->verbose,
67a8c82d 1129 0, NULL);
4b1ac34b 1130
3da92f27 1131 if (tst->ss->store_super(tst, fd)) {
52826846 1132 close(fd);
e7b84f9d 1133 pr_err("Could not re-write superblock on %s\n",
52826846 1134 devices[chosen_drive].devname);
213ee40b 1135 devices[chosen_drive].i.events = 0;
3da92f27 1136 tst->ss->free_super(tst);
52826846
NB
1137 continue;
1138 }
1139 close(fd);
213ee40b 1140 devices[chosen_drive].i.events = devices[most_recent].i.events;
52826846 1141 devices[chosen_drive].uptodate = 1;
265e0f17 1142 avail[chosen_drive] = 1;
52826846 1143 okcnt++;
3da92f27 1144 tst->ss->free_super(tst);
c5a6f9a6
NB
1145
1146 /* If there are any other drives of the same vintage,
1147 * add them in as well. We can't lose and we might gain
1148 */
f21e18ca 1149 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
c5a6f9a6
NB
1150 int j = best[i];
1151 if (j >= 0 &&
1152 !devices[j].uptodate &&
135a31f5 1153 devices[j].i.recovery_start == MaxSector &&
213ee40b 1154 devices[j].i.events == current_events) {
c5a6f9a6
NB
1155 chosen_drive = j;
1156 goto add_another;
1157 }
1158 }
64c4757e 1159 }
52826846
NB
1160
1161 /* Now we want to look at the superblock which the kernel will base things on
1162 * and compare the devices that we think are working with the devices that the
1163 * superblock thinks are working.
1164 * If there are differences and --force is given, then update this chosen
1165 * superblock.
1166 */
cd29a5c8 1167 chosen_drive = -1;
3da92f27 1168 st->ss->free_super(st);
56eedc1a 1169 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
1170 int j = best[i];
1171 int fd;
4b1ac34b 1172
52826846
NB
1173 if (j<0)
1174 continue;
1175 if (!devices[j].uptodate)
1176 continue;
a28232b8
N
1177 if (devices[j].i.events < devices[most_recent].i.events)
1178 continue;
52826846 1179 chosen_drive = j;
0431869c
N
1180 if ((fd=dev_open(devices[j].devname,
1181 devices[j].included ? O_RDONLY
1182 : (O_RDONLY|O_EXCL)))< 0) {
e7b84f9d 1183 pr_err("Cannot open %s: %s\n",
52826846 1184 devices[j].devname, strerror(errno));
7f91af49 1185 close(mdfd);
d7f7ebb7 1186 free(devices);
52826846
NB
1187 return 1;
1188 }
3da92f27 1189 if (st->ss->load_super(st,fd, NULL)) {
52826846 1190 close(fd);
e7b84f9d 1191 pr_err("RAID superblock has disappeared from %s\n",
52826846 1192 devices[j].devname);
7f91af49 1193 close(mdfd);
d7f7ebb7 1194 free(devices);
52826846
NB
1195 return 1;
1196 }
1197 close(fd);
1198 }
3da92f27 1199 if (st->sb == NULL) {
e7b84f9d 1200 pr_err("No suitable drives found for %s\n", mddev);
7f91af49 1201 close(mdfd);
d7f7ebb7 1202 free(devices);
4b1ac34b
NB
1203 return 1;
1204 }
a5d85af7 1205 st->ss->getinfo_super(st, content, NULL);
f35f2525 1206#ifndef MDASSEMBLE
98dbd966 1207 sysfs_init(content, mdfd, 0);
f35f2525 1208#endif
56eedc1a 1209 for (i=0; i<bestcnt; i++) {
52826846 1210 int j = best[i];
98c6faba 1211 unsigned int desired_state;
11a3e71d 1212
98dbd966 1213 if (i < content->array.raid_disks)
11a3e71d
NB
1214 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1215 else
1216 desired_state = 0;
1217
52826846
NB
1218 if (j<0)
1219 continue;
1220 if (!devices[j].uptodate)
1221 continue;
4b1ac34b 1222
213ee40b 1223 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1224 if (!(devices[j].i.array.state & 1))
1225 clean = 0;
213ee40b
NB
1226
1227 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
4977146a
N
1228 c->verbose, 0, NULL)) {
1229 if (c->force) {
1230 if (c->verbose >= 0)
e7b84f9d 1231 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
dab6685f 1232 j, mddev, devices[j].devname);
4b1ac34b 1233 change = 1;
52826846 1234 } else {
4977146a 1235 if (c->verbose >= -1)
e7b84f9d 1236 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
dab6685f 1237 i, mddev, devices[j].devname);
52826846
NB
1238 }
1239 }
4b1ac34b 1240#if 0
213ee40b 1241 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
e7b84f9d 1242 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
52826846
NB
1243 i, mddev);
1244 }
4b1ac34b 1245#endif
52826846 1246 }
4977146a 1247 if (c->force && !clean &&
98dbd966
DW
1248 !enough(content->array.level, content->array.raid_disks,
1249 content->array.layout, clean,
de5a472e 1250 avail)) {
98dbd966 1251 change += st->ss->update_super(st, content, "force-array",
4977146a 1252 devices[chosen_drive].devname, c->verbose,
67a8c82d 1253 0, NULL);
583315d9 1254 clean = 1;
d013a55e 1255 }
52826846 1256
4b1ac34b 1257 if (change) {
52826846 1258 int fd;
0431869c
N
1259 fd = dev_open(devices[chosen_drive].devname,
1260 devices[chosen_drive].included ?
1261 O_RDWR : (O_RDWR|O_EXCL));
52826846 1262 if (fd < 0) {
e7b84f9d 1263 pr_err("Could not open %s for write - cannot Assemble array.\n",
52826846 1264 devices[chosen_drive].devname);
7f91af49 1265 close(mdfd);
d7f7ebb7 1266 free(devices);
52826846
NB
1267 return 1;
1268 }
3da92f27 1269 if (st->ss->store_super(st, fd)) {
52826846 1270 close(fd);
e7b84f9d 1271 pr_err("Could not re-write superblock on %s\n",
52826846 1272 devices[chosen_drive].devname);
7f91af49 1273 close(mdfd);
d7f7ebb7 1274 free(devices);
52826846
NB
1275 return 1;
1276 }
4977146a 1277 if (c->verbose >= 0)
e7b84f9d 1278 pr_err("Marking array %s as 'clean'\n",
a28232b8 1279 mddev);
52826846 1280 close(fd);
52826846
NB
1281 }
1282
353632d9
NB
1283 /* If we are in the middle of a reshape we may need to restore saved data
1284 * that was moved aside due to the reshape overwriting live data
1285 * The code of doing this lives in Grow.c
1286 */
39c5a909 1287#ifndef MDASSEMBLE
5e88ab2e
N
1288 if (content->reshape_active &&
1289 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
353632d9 1290 int err = 0;
503975b9 1291 int *fdlist = xmalloc(sizeof(int)* bestcnt);
4977146a 1292 if (c->verbose > 0)
e7b84f9d 1293 pr_err(":%s has an active reshape - checking "
ea0ebe96
N
1294 "if critical section needs to be restored\n",
1295 chosen_name);
353632d9
NB
1296 for (i=0; i<bestcnt; i++) {
1297 int j = best[i];
1298 if (j >= 0) {
0431869c
N
1299 fdlist[i] = dev_open(devices[j].devname,
1300 devices[j].included
1301 ? O_RDWR : (O_RDWR|O_EXCL));
353632d9 1302 if (fdlist[i] < 0) {
e7b84f9d 1303 pr_err("Could not open %s for write - cannot Assemble array.\n",
353632d9
NB
1304 devices[j].devname);
1305 err = 1;
1306 break;
1307 }
1308 } else
1309 fdlist[i] = -1;
1310 }
87f26d14 1311 if (!err) {
ba53ea59
AK
1312 if (st->ss->external && st->ss->recover_backup)
1313 err = st->ss->recover_backup(st, content);
1314 else
1315 err = Grow_restart(st, content, fdlist, bestcnt,
4977146a
N
1316 c->backup_file, c->verbose > 0);
1317 if (err && c->invalid_backup) {
1318 if (c->verbose > 0)
e7b84f9d 1319 pr_err("continuing"
87f26d14
N
1320 " without restoring backup\n");
1321 err = 0;
1322 }
1323 }
353632d9
NB
1324 while (i>0) {
1325 i--;
1326 if (fdlist[i]>=0) close(fdlist[i]);
1327 }
56dcaa6b 1328 free(fdlist);
353632d9 1329 if (err) {
e7b84f9d 1330 pr_err("Failed to restore critical section for reshape, sorry.\n");
4977146a 1331 if (c->backup_file == NULL)
e7b84f9d 1332 cont_err("Possibly you needed to specify the --backup-file\n");
7f91af49 1333 close(mdfd);
d7f7ebb7 1334 free(devices);
353632d9
NB
1335 return err;
1336 }
1337 }
39c5a909 1338#endif
aa88f531
NB
1339 /* count number of in-sync devices according to the superblock.
1340 * We must have this number to start the array without -s or -R
1341 */
98dbd966 1342 req_cnt = content->array.working_disks;
aa88f531 1343
64c4757e
NB
1344 /* Almost ready to actually *do* something */
1345 if (!old_linux) {
fbf8a0b7 1346 int rv;
a19c88b8 1347
a04d5763
N
1348 /* First, fill in the map, so that udev can find our name
1349 * as soon as we become active.
1350 */
0431869c 1351 map_update(&map, fd2devnum(mdfd), content->text_version,
98dbd966 1352 content->uuid, chosen_name);
a04d5763 1353
98dbd966 1354 rv = set_array_info(mdfd, st, content);
0431869c 1355 if (rv && !pre_exist) {
e7b84f9d 1356 pr_err("failed to set array info for %s: %s\n",
64c4757e 1357 mddev, strerror(errno));
24af7a87 1358 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1359 close(mdfd);
d7f7ebb7 1360 free(devices);
0431869c 1361 map_unlock(&map);
64c4757e
NB
1362 return 1;
1363 }
11484a63 1364 if (ident->bitmap_fd >= 0) {
c82f047c 1365 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
e7b84f9d 1366 pr_err("SET_BITMAP_FILE failed.\n");
24af7a87 1367 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1368 close(mdfd);
d7f7ebb7 1369 free(devices);
0431869c 1370 map_unlock(&map);
c82f047c
NB
1371 return 1;
1372 }
7ef02d01
NB
1373 } else if (ident->bitmap_file) {
1374 /* From config file */
1375 int bmfd = open(ident->bitmap_file, O_RDWR);
1376 if (bmfd < 0) {
e7b84f9d 1377 pr_err("Could not open bitmap file %s\n",
7ef02d01 1378 ident->bitmap_file);
24af7a87 1379 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1380 close(mdfd);
d7f7ebb7 1381 free(devices);
0431869c 1382 map_unlock(&map);
7ef02d01
NB
1383 return 1;
1384 }
1385 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
e7b84f9d 1386 pr_err("Failed to set bitmapfile for %s\n", mddev);
7ef02d01 1387 close(bmfd);
24af7a87 1388 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1389 close(mdfd);
d7f7ebb7 1390 free(devices);
0431869c 1391 map_unlock(&map);
7ef02d01
NB
1392 return 1;
1393 }
1394 close(bmfd);
c82f047c 1395 }
7ef02d01 1396
52826846 1397 /* First, add the raid disks, but add the chosen one last */
56eedc1a 1398 for (i=0; i<= bestcnt; i++) {
52826846 1399 int j;
56eedc1a 1400 if (i < bestcnt) {
52826846
NB
1401 j = best[i];
1402 if (j == chosen_drive)
1403 continue;
1404 } else
1405 j = chosen_drive;
1406
0431869c 1407 if (j >= 0 && !devices[j].included) {
484ae54d
N
1408 int dfd = dev_open(devices[j].devname,
1409 O_RDWR|O_EXCL);
1410 if (dfd >= 0) {
1411 remove_partitions(dfd);
1412 close(dfd);
1413 }
98dbd966 1414 rv = add_disk(mdfd, st, content, &devices[j].i);
7801ac20 1415
a19c88b8 1416 if (rv) {
e7b84f9d
N
1417 pr_err("failed to add "
1418 "%s to %s: %s\n",
1419 devices[j].devname,
1420 mddev,
1421 strerror(errno));
98dbd966 1422 if (i < content->array.raid_disks
213ee40b 1423 || i == bestcnt)
52826846
NB
1424 okcnt--;
1425 else
1426 sparecnt--;
4977146a 1427 } else if (c->verbose > 0)
e7b84f9d
N
1428 pr_err("added %s to %s as %d%s\n",
1429 devices[j].devname, mddev,
1430 devices[j].i.disk.raid_disk,
1431 devices[j].uptodate?"":
1432 " (possibly out of date)");
0431869c
N
1433 } else if (j >= 0) {
1434 if (c->verbose > 0)
1435 pr_err("%s is already in %s as %d\n",
1436 devices[j].devname, mddev,
1437 devices[j].i.disk.raid_disk);
4977146a 1438 } else if (c->verbose > 0 && i < content->array.raid_disks)
0431869c 1439 pr_err("no uptodate device for slot %d of %s\n",
64c4757e
NB
1440 i, mddev);
1441 }
aba69144 1442
98dbd966 1443 if (content->array.level == LEVEL_CONTAINER) {
4977146a 1444 if (c->verbose >= 0) {
e7b84f9d 1445 pr_err("Container %s has been "
598f0d58 1446 "assembled with %d drive%s",
57ed8c91 1447 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
f21e18ca 1448 if (okcnt < (unsigned)content->array.raid_disks)
598f0d58 1449 fprintf(stderr, " (out of %d)",
98dbd966 1450 content->array.raid_disks);
598f0d58
NB
1451 fprintf(stderr, "\n");
1452 }
ac597b1c 1453 st->ss->free_super(st);
98dbd966 1454 sysfs_uevent(content, "change");
0431869c 1455 map_unlock(&map);
a7c6e3fb 1456 wait_for(chosen_name, mdfd);
7f91af49 1457 close(mdfd);
d7f7ebb7 1458 free(devices);
598f0d58
NB
1459 return 0;
1460 }
1461
4977146a
N
1462 if (c->runstop == 1 ||
1463 (c->runstop <= 0 &&
98dbd966 1464 ( enough(content->array.level, content->array.raid_disks,
de5a472e 1465 content->array.layout, clean, avail) &&
1ff98339 1466 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
aa88f531 1467 ))) {
e9e43ec3
N
1468 /* This array is good-to-go.
1469 * If a reshape is in progress then we might need to
1470 * continue monitoring it. In that case we start
1471 * it read-only and let the grow code make it writable.
1472 */
1473 int rv;
eb3929a4 1474#ifndef MDASSEMBLE
e9e43ec3 1475 if (content->reshape_active &&
5e88ab2e 1476 !(content->reshape_active & RESHAPE_NO_BACKUP) &&
3bd58dc6
AK
1477 content->delta_disks <= 0) {
1478 rv = sysfs_set_str(content, NULL,
1479 "array_state", "readonly");
1480 if (rv == 0)
1481 rv = Grow_continue(mdfd, st, content,
4977146a
N
1482 c->backup_file,
1483 c->freeze_reshape);
1484 } else if (c->readonly &&
0ea8f5b1
N
1485 sysfs_attribute_available(
1486 content, NULL, "array_state")) {
1487 rv = sysfs_set_str(content, NULL,
1488 "array_state", "readonly");
3bd58dc6 1489 } else
eb3929a4 1490#endif
e9e43ec3
N
1491 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1492 if (rv == 0) {
4977146a 1493 if (c->verbose >= 0) {
e7b84f9d 1494 pr_err("%s has been started with %d drive%s",
dab6685f 1495 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1496 if (okcnt < (unsigned)content->array.raid_disks)
98dbd966 1497 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1ff98339
N
1498 if (rebuilding_cnt)
1499 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
dab6685f
NB
1500 if (sparecnt)
1501 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1502 fprintf(stderr, ".\n");
1503 }
8a659c33
N
1504 if (content->reshape_active &&
1505 content->array.level >= 4 &&
1506 content->array.level <= 6) {
acee8e89
N
1507 /* might need to increase the size
1508 * of the stripe cache - default is 256
1509 */
8a659c33 1510 if (256 < 4 * (content->array.chunk_size/4096)) {
acee8e89
N
1511 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1512 if (sra)
1513 sysfs_set_num(sra, NULL,
1514 "stripe_cache_size",
8a659c33 1515 (4 * content->array.chunk_size / 4096) + 1);
83366b33 1516 sysfs_free(sra);
acee8e89
N
1517 }
1518 }
7e83544b
N
1519 if (okcnt < (unsigned)content->array.raid_disks) {
1520 /* If any devices did not get added
1521 * because the kernel rejected them based
1522 * on event count, try adding them
1523 * again providing the action policy is
1524 * 're-add' or greater. The bitmap
1525 * might allow them to be included, or
1526 * they will become spares.
1527 */
b787bec6 1528 for (i = 0; i < bestcnt; i++) {
7e83544b
N
1529 int j = best[i];
1530 if (j >= 0 && !devices[j].uptodate) {
1531 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1532 continue;
1533 rv = add_disk(mdfd, st, content,
1534 &devices[j].i);
4977146a 1535 if (rv == 0 && c->verbose >= 0)
e7b84f9d
N
1536 pr_err("%s has been re-added.\n",
1537 devices[j].devname);
7e83544b
N
1538 }
1539 }
1540 }
0431869c 1541 map_unlock(&map);
a7c6e3fb 1542 wait_for(mddev, mdfd);
7f91af49
N
1543 close(mdfd);
1544 if (auto_assem) {
589395d6 1545 int usecs = 1;
589395d6
NB
1546 /* There is a nasty race with 'mdadm --monitor'.
1547 * If it opens this device before we close it,
1548 * it gets an incomplete open on which IO
598f0d58
NB
1549 * doesn't work and the capacity is
1550 * wrong.
589395d6
NB
1551 * If we reopen (to check for layered devices)
1552 * before --monitor closes, we loose.
1553 *
1554 * So: wait upto 1 second for there to be
1555 * a non-zero capacity.
1556 */
1557 while (usecs < 1000) {
1558 mdfd = open(mddev, O_RDONLY);
1559 if (mdfd >= 0) {
beae1dfe
NB
1560 unsigned long long size;
1561 if (get_dev_size(mdfd, NULL, &size) &&
589395d6
NB
1562 size > 0)
1563 break;
1564 close(mdfd);
1565 }
1566 usleep(usecs);
1567 usecs <<= 1;
1568 }
1569 }
d7f7ebb7 1570 free(devices);
64c4757e 1571 return 0;
82b27616 1572 }
e7b84f9d 1573 pr_err("failed to RUN_ARRAY %s: %s\n",
64c4757e 1574 mddev, strerror(errno));
583315d9 1575
98dbd966 1576 if (!enough(content->array.level, content->array.raid_disks,
de5a472e 1577 content->array.layout, 1, avail))
e7b84f9d 1578 pr_err("Not enough devices to "
583315d9 1579 "start the array.\n");
98dbd966
DW
1580 else if (!enough(content->array.level,
1581 content->array.raid_disks,
1582 content->array.layout, clean,
de5a472e 1583 avail))
e7b84f9d 1584 pr_err("Not enough devices to "
583315d9
NB
1585 "start the array while not clean "
1586 "- consider --force.\n");
1587
7f91af49 1588 if (auto_assem)
1c203a4b 1589 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1590 close(mdfd);
d7f7ebb7 1591 free(devices);
0431869c 1592 map_unlock(&map);
64c4757e
NB
1593 return 1;
1594 }
4977146a 1595 if (c->runstop == -1) {
e7b84f9d
N
1596 pr_err("%s assembled from %d drive%s",
1597 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1598 if (okcnt != (unsigned)content->array.raid_disks)
98dbd966 1599 fprintf(stderr, " (out of %d)", content->array.raid_disks);
b8a8ccf9 1600 fprintf(stderr, ", but not started.\n");
7f91af49 1601 close(mdfd);
d7f7ebb7 1602 free(devices);
0431869c 1603 map_unlock(&map);
64c4757e 1604 return 0;
82b27616 1605 }
4977146a 1606 if (c->verbose >= -1) {
e7b84f9d 1607 pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1ff98339
N
1608 if (rebuilding_cnt)
1609 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
dab6685f
NB
1610 if (sparecnt)
1611 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
98dbd966 1612 if (!enough(content->array.level, content->array.raid_disks,
de5a472e 1613 content->array.layout, 1, avail))
dab6685f 1614 fprintf(stderr, " - not enough to start the array.\n");
98dbd966
DW
1615 else if (!enough(content->array.level,
1616 content->array.raid_disks,
1617 content->array.layout, clean,
de5a472e 1618 avail))
583315d9
NB
1619 fprintf(stderr, " - not enough to start the "
1620 "array while not clean - consider "
1621 "--force.\n");
dab6685f 1622 else {
f21e18ca 1623 if (req_cnt == (unsigned)content->array.raid_disks)
dab6685f
NB
1624 fprintf(stderr, " - need all %d to start it", req_cnt);
1625 else
98dbd966 1626 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
dab6685f
NB
1627 fprintf(stderr, " (use --run to insist).\n");
1628 }
aa88f531 1629 }
7f91af49 1630 if (auto_assem)
1c203a4b 1631 ioctl(mdfd, STOP_ARRAY, NULL);
56a8da69 1632 close(mdfd);
d7f7ebb7 1633 free(devices);
0431869c 1634 map_unlock(&map);
64c4757e 1635 return 1;
82b27616 1636 } else {
52826846
NB
1637 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1638 * been updated to point to the current locations of devices.
1639 * so we can just start the array
82b27616 1640 */
cd29a5c8 1641 unsigned long dev;
213ee40b
NB
1642 dev = makedev(devices[chosen_drive].i.disk.major,
1643 devices[chosen_drive].i.disk.minor);
82b27616 1644 if (ioctl(mdfd, START_ARRAY, dev)) {
e7b84f9d 1645 pr_err("Cannot start array: %s\n",
82b27616
NB
1646 strerror(errno));
1647 }
aba69144 1648
64c4757e 1649 }
7f91af49 1650 close(mdfd);
d7f7ebb7 1651 free(devices);
0431869c 1652 map_unlock(&map);
e0d19036 1653 return 0;
64c4757e 1654}
6234c63c
DW
1655
1656#ifndef MDASSEMBLE
1657int assemble_container_content(struct supertype *st, int mdfd,
11b6d91d
N
1658 struct mdinfo *content, struct context *c,
1659 char *chosen_name)
6234c63c
DW
1660{
1661 struct mdinfo *dev, *sra;
1662 int working = 0, preexist = 0;
882029c8 1663 int expansion = 0;
6234c63c 1664 struct map_ent *map = NULL;
7af03341 1665 int old_raid_disks;
4aecb54a 1666 int start_reshape;
6234c63c
DW
1667
1668 sysfs_init(content, mdfd, 0);
1669
1670 sra = sysfs_read(mdfd, 0, GET_VERSION);
0ea8f5b1
N
1671 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
1672 if (content->array.major_version == -1 &&
1673 content->array.minor_version == -2 &&
11b6d91d 1674 c->readonly &&
0ea8f5b1
N
1675 content->text_version[0] == '/')
1676 content->text_version[0] = '-';
22472ee1
JS
1677 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1678 if (sra)
1679 sysfs_free(sra);
6234c63c 1680 return 1;
22472ee1 1681 }
0ea8f5b1 1682 }
588bebfc 1683
4aecb54a
AK
1684 /* There are two types of reshape: container wide or sub-array specific
1685 * Check if metadata requests blocking container wide reshapes
1686 */
1687 start_reshape = (content->reshape_active &&
1688 !((content->reshape_active == CONTAINER_RESHAPE) &&
1689 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1690
1691 /* Block subarray here if it is under reshape now
1692 * Do not allow for any changes in this array
1693 */
1694 if (st->ss->external && content->recovery_blocked && start_reshape)
b8063f07
AK
1695 block_subarray(content);
1696
6234c63c
DW
1697 if (sra)
1698 sysfs_free(sra);
7af03341 1699 old_raid_disks = content->array.raid_disks - content->delta_disks;
6234c63c 1700 for (dev = content->devs; dev; dev = dev->next)
14032016 1701 if (sysfs_add_disk(content, dev, 1) == 0) {
7af03341 1702 if (dev->disk.raid_disk >= old_raid_disks &&
14032016
AK
1703 content->reshape_active)
1704 expansion++;
1705 else
1706 working++;
1707 } else if (errno == EEXIST)
6234c63c 1708 preexist++;
111e9fda 1709 if (working + expansion == 0)
7cb2aa33 1710 return 1;/* Nothing new, don't try to start */
588bebfc 1711
8b4e5ea9
N
1712 map_update(&map, fd2devnum(mdfd),
1713 content->text_version,
1714 content->uuid, chosen_name);
1715
11b6d91d 1716 if (c->runstop > 0 ||
882029c8
AK
1717 (working + preexist + expansion) >=
1718 content->array.working_disks) {
bb50e5d3 1719 int err;
a714580e 1720
81219e70 1721 if (start_reshape) {
49680258 1722 int spare = content->array.raid_disks + expansion;
3f54bd62
AK
1723 if (restore_backup(st, content,
1724 working,
11b6d91d 1725 spare, c->backup_file, c->verbose) == 1)
49680258 1726 return 1;
49680258 1727
3bd58dc6
AK
1728 err = sysfs_set_str(content, NULL,
1729 "array_state", "readonly");
1730 if (err)
1731 return 1;
1732
a93ada3b
AK
1733 if (st->ss->external) {
1734 if (!mdmon_running(st->container_dev))
1735 start_mdmon(st->container_dev);
1736 ping_monitor_by_id(st->container_dev);
7728e1c6
LD
1737 if (mdmon_running(st->container_dev) &&
1738 st->update_tail == NULL)
1739 st->update_tail = &st->updates;
a93ada3b
AK
1740 }
1741
11b6d91d
N
1742 err = Grow_continue(mdfd, st, content, c->backup_file,
1743 c->freeze_reshape);
49680258 1744 } else switch(content->array.level) {
6234c63c
DW
1745 case LEVEL_LINEAR:
1746 case LEVEL_MULTIPATH:
1747 case 0:
bb50e5d3 1748 err = sysfs_set_str(content, NULL, "array_state",
11b6d91d 1749 c->readonly ? "readonly" : "active");
6234c63c
DW
1750 break;
1751 default:
bb50e5d3 1752 err = sysfs_set_str(content, NULL, "array_state",
6234c63c
DW
1753 "readonly");
1754 /* start mdmon if needed. */
bb50e5d3
N
1755 if (!err) {
1756 if (!mdmon_running(st->container_dev))
1757 start_mdmon(st->container_dev);
983fff45 1758 ping_monitor_by_id(st->container_dev);
bb50e5d3 1759 }
6234c63c
DW
1760 break;
1761 }
bb50e5d3
N
1762 if (!err)
1763 sysfs_set_safemode(content, content->safe_mode_delay);
4aecb54a
AK
1764
1765 /* Block subarray here if it is not reshaped now
1766 * It has be blocked a little later to allow mdmon to switch in
1767 * in to R/W state
1768 */
1769 if (st->ss->external && content->recovery_blocked &&
1770 !start_reshape)
1771 block_subarray(content);
1772
11b6d91d 1773 if (c->verbose >= 0) {
bb50e5d3 1774 if (err)
e7b84f9d
N
1775 pr_err("array %s now has %d device%s",
1776 chosen_name, working + preexist,
1777 working + preexist == 1 ? "":"s");
bb50e5d3 1778 else
e7b84f9d
N
1779 pr_err("Started %s with %d device%s",
1780 chosen_name, working + preexist,
1781 working + preexist == 1 ? "":"s");
6234c63c
DW
1782 if (preexist)
1783 fprintf(stderr, " (%d new)", working);
882029c8
AK
1784 if (expansion)
1785 fprintf(stderr, " ( + %d for expansion)",
1786 expansion);
6234c63c
DW
1787 fprintf(stderr, "\n");
1788 }
bb50e5d3 1789 if (!err)
a7c6e3fb 1790 wait_for(chosen_name, mdfd);
588bebfc 1791 return err;
6234c63c 1792 /* FIXME should have an O_EXCL and wait for read-auto */
7cb2aa33 1793 } else {
11b6d91d 1794 if (c->verbose >= 0) {
e7b84f9d
N
1795 pr_err("%s assembled with %d device%s",
1796 chosen_name, preexist + working,
1797 preexist + working == 1 ? "":"s");
88716263
N
1798 if (preexist)
1799 fprintf(stderr, " (%d new)", working);
1800 fprintf(stderr, " but not started\n");
1801 }
7cb2aa33
N
1802 return 1;
1803 }
6234c63c
DW
1804}
1805#endif
1806