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