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