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