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