]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
imsm: set imsm spare uuid to 0
[thirdparty/mdadm.git] / Assemble.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
64c4757e
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
41a3b72a 26#include <ctype.h>
64c4757e 27
624920bb
NB
28static int name_matches(char *found, char *required, char *homehost)
29{
30 /* See if the name found matches the required name, possibly
31 * prefixed with 'homehost'
32 */
33 char fnd[33];
34
35 strncpy(fnd, found, 32);
36 fnd[32] = 0;
37 if (strcmp(found, required)==0)
38 return 1;
39 if (homehost) {
40 int l = strlen(homehost);
41 if (l < 32 && fnd[l] == ':' &&
42 strcmp(fnd+l+1, required)==0)
43 return 1;
44 }
45 return 0;
46}
47
9008ed1c 48static int is_member_busy(char *metadata_version)
2de8884f
DW
49{
50 /* check if the given member array is active */
51 struct mdstat_ent *mdstat = mdstat_read(1, 0);
52 struct mdstat_ent *ent;
53 int busy = 0;
54
55 for (ent = mdstat; ent; ent = ent->next) {
56 if (ent->metadata_version == NULL)
57 continue;
58 if (strncmp(ent->metadata_version, "external:", 9) != 0)
59 continue;
60 if (!is_subarray(&ent->metadata_version[9]))
61 continue;
62 /* Skip first char - it can be '/' or '-' */
63 if (strcmp(&ent->metadata_version[10], metadata_version+1) == 0) {
64 busy = 1;
65 break;
66 }
67 }
68 free_mdstat(mdstat);
69
70 return busy;
71}
72
fa56eddb 73static int ident_matches(struct mddev_ident *ident,
08fb91a3
N
74 struct mdinfo *content,
75 struct supertype *tst,
76 char *homehost,
77 char *update, char *devname)
78{
79
80 if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
81 same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0) {
82 if (devname)
83 fprintf(stderr, Name ": %s has wrong uuid.\n",
84 devname);
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)
90 fprintf(stderr, Name ": %s has wrong name.\n",
91 devname);
92 return 0;
93 }
94 if (ident->super_minor != UnSet &&
95 ident->super_minor != content->array.md_minor) {
96 if (devname)
97 fprintf(stderr, Name ": %s has wrong super-minor.\n",
98 devname);
99 return 0;
100 }
101 if (ident->level != UnSet &&
102 ident->level != content->array.level) {
103 if (devname)
104 fprintf(stderr, Name ": %s has wrong raid level.\n",
105 devname);
106 return 0;
107 }
108 if (ident->raid_disks != UnSet &&
109 ident->raid_disks!= content->array.raid_disks) {
110 if (devname)
111 fprintf(stderr, Name ": %s requires wrong number of drives.\n",
112 devname);
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)
120 fprintf(stderr, Name ": %s is not a container and one is required.\n",
121 devname);
122 return 0;
123 } else if (strcmp(ident->member, s+1) != 0) {
124 if (devname)
125 fprintf(stderr, Name ": skipping wrong member %s is %s\n",
126 content->text_version, devname);
127 return 0;
128 }
129 }
08fb91a3
N
130 return 1;
131}
132
133
7f91af49 134int Assemble(struct supertype *st, char *mddev,
fa56eddb 135 struct mddev_ident *ident,
87f26d14
N
136 struct mddev_dev *devlist,
137 char *backup_file, int invalid_backup,
64c4757e 138 int readonly, int runstop,
0ac91628 139 char *update, char *homehost, int require_homehost,
64c4757e
NB
140 int verbose, int force)
141{
142 /*
52826846
NB
143 * The task of Assemble is to find a collection of
144 * devices that should (according to their superblocks)
145 * form an array, and to give this collection to the MD driver.
146 * In Linux-2.4 and later, this involves submitting a
64c4757e
NB
147 * SET_ARRAY_INFO ioctl with no arg - to prepare
148 * the array - and then submit a number of
149 * ADD_NEW_DISK ioctls to add disks into
150 * the array. Finally RUN_ARRAY might
151 * be submitted to start the array.
152 *
153 * Much of the work of Assemble is in finding and/or
154 * checking the disks to make sure they look right.
155 *
4b1ac34b 156 * If mddev is not set, then scan must be set and we
64c4757e
NB
157 * read through the config file for dev+uuid mapping
158 * We recurse, setting mddev, for each device that
159 * - isn't running
4b1ac34b 160 * - has a valid uuid (or any uuid if !uuidset)
64c4757e
NB
161 *
162 * If mddev is set, we try to determine state of md.
163 * check version - must be at least 0.90.0
164 * check kernel version. must be at least 2.4.
165 * If not, we can possibly fall back on START_ARRAY
166 * Try to GET_ARRAY_INFO.
167 * If possible, give up
168 * If not, try to STOP_ARRAY just to make sure
169 *
170 * If !uuidset and scan, look in conf-file for uuid
171 * If not found, give up
b8a8ccf9 172 * If !devlist and scan and uuidset, get list of devs from conf-file
64c4757e
NB
173 *
174 * For each device:
175 * Check superblock - discard if bad
176 * Check uuid (set if we don't have one) - discard if no match
5787fa49 177 * Check superblock similarity if we have a superblock - discard if different
4b1ac34b 178 * Record events, devicenum
64c4757e 179 * This should give us a list of devices for the array
4b1ac34b 180 * We should collect the most recent event number
64c4757e
NB
181 *
182 * Count disks with recent enough event count
183 * While force && !enough disks
184 * Choose newest rejected disks, update event count
185 * mark clean and rewrite superblock
186 * If recent kernel:
187 * SET_ARRAY_INFO
188 * foreach device with recent events : ADD_NEW_DISK
189 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
190 * If old kernel:
191 * Check the device numbers in superblock are right
192 * update superblock if any changes
193 * START_ARRAY
194 *
195 */
c30e5369
N
196 int mdfd;
197 int clean;
f05641cf
N
198 int auto_assem = (mddev == NULL && !ident->uuid_set &&
199 ident->super_minor == UnSet && ident->name[0] == 0
aa7c284c 200 && (ident->container == NULL || ident->member == NULL));
64c4757e 201 int old_linux = 0;
c30e5369 202 int vers = vers; /* Keep gcc quite - it really is initialised */
64c4757e
NB
203 struct {
204 char *devname;
213ee40b
NB
205 int uptodate; /* set once we decide that this device is as
206 * recent as everything else in the array.
207 */
208 struct mdinfo i;
5787fa49 209 } *devices;
02e7c5b7 210 char *devmap;
56eedc1a 211 int *best = NULL; /* indexed by raid_disk */
f21e18ca 212 int bestcnt = 0;
98c6faba 213 int devcnt = 0;
1ff98339 214 unsigned int okcnt, sparecnt, rebuilding_cnt;
98c6faba 215 unsigned int req_cnt;
f21e18ca 216 int i;
64c4757e 217 int most_recent = 0;
cd29a5c8 218 int chosen_drive;
52826846 219 int change = 0;
cd29a5c8 220 int inargv = 0;
52437b4f 221 int report_missmatch;
bf4fb153 222 int bitmap_done;
7f91af49
N
223 int start_partial_ok = (runstop >= 0) &&
224 (force || devlist==NULL || auto_assem);
98c6faba 225 unsigned int num_devs;
a655e550 226 struct mddev_dev *tmpdev;
4b1ac34b 227 struct mdinfo info;
98dbd966 228 struct mdinfo *content = NULL;
265e0f17 229 char *avail;
308e1801 230 int nextspare = 0;
197e3eb6 231 char *name = NULL;
69207ff6 232 int trustworthy;
a04d5763 233 char chosen_name[1024];
8a46fe84 234
682c7051 235 if (get_linux_version() < 2004000)
64c4757e
NB
236 old_linux = 1;
237
64c4757e 238 /*
52826846
NB
239 * If any subdevs are listed, then any that don't
240 * match ident are discarded. Remainder must all match and
241 * become the array.
242 * If no subdevs, then we scan all devices in the config file, but
243 * there must be something in the identity
64c4757e 244 */
64c4757e 245
cd29a5c8 246 if (!devlist &&
52826846 247 ident->uuid_set == 0 &&
f21e18ca 248 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
e0fe762a
N
249 ident->name[0] == 0 &&
250 (ident->container == NULL || ident->member == NULL) &&
52826846
NB
251 ident->devices == NULL) {
252 fprintf(stderr, Name ": No identity information available for %s - cannot assemble.\n",
8a46fe84 253 mddev ? mddev : "further assembly");
52826846 254 return 1;
64c4757e 255 }
71d60c48 256
cd29a5c8 257 if (devlist == NULL)
8aec876d 258 devlist = conf_get_devs();
7f91af49 259 else if (mddev)
da6b5ca9 260 inargv = 1;
64c4757e 261
52437b4f 262 report_missmatch = ((inargv && verbose >= 0) || verbose > 0);
60e1bc1a 263 try_again:
c30e5369
N
264 /* We come back here when doing auto-assembly and attempting some
265 * set of devices failed. Those are now marked as ->used==2 and
266 * we ignore them and try again
267 */
60e1bc1a 268
5787fa49
NB
269 tmpdev = devlist; num_devs = 0;
270 while (tmpdev) {
da6b5ca9
NB
271 if (tmpdev->used)
272 tmpdev->used = 2;
273 else
274 num_devs++;
5787fa49
NB
275 tmpdev = tmpdev->next;
276 }
5787fa49 277
82d9eba6 278 if (!st && ident->st) st = ident->st;
64c4757e 279
dab6685f 280 if (verbose>0)
82b27616 281 fprintf(stderr, Name ": looking for devices for %s\n",
8a46fe84 282 mddev ? mddev : "further assembly");
82b27616 283
811e6cbe
NB
284 /* first walk the list of devices to find a consistent set
285 * that match the criterea, if that is possible.
c30e5369 286 * We flag the ones we like with 'used'.
811e6cbe
NB
287 */
288 for (tmpdev = devlist;
289 tmpdev;
5083d66b 290 tmpdev = tmpdev ? tmpdev->next : NULL) {
811e6cbe 291 char *devname = tmpdev->devname;
64c4757e
NB
292 int dfd;
293 struct stat stb;
3da92f27 294 struct supertype *tst = dup_super(st);
4e8d9f0a 295 struct dev_policy *pol = NULL;
5083d66b 296 int found_container = 0;
52826846 297
da6b5ca9
NB
298 if (tmpdev->used > 1) continue;
299
52826846 300 if (ident->devices &&
56eedc1a 301 !match_oneof(ident->devices, devname)) {
52437b4f 302 if (report_missmatch)
56eedc1a 303 fprintf(stderr, Name ": %s is not one of %s\n", devname, ident->devices);
52826846 304 continue;
56eedc1a 305 }
4b1ac34b 306
8b0dabea 307 dfd = dev_open(devname, O_RDONLY|O_EXCL);
64c4757e 308 if (dfd < 0) {
52437b4f 309 if (report_missmatch)
682c7051 310 fprintf(stderr, Name ": cannot open device %s: %s\n",
64c4757e 311 devname, strerror(errno));
da6b5ca9 312 tmpdev->used = 2;
52826846
NB
313 } else if (fstat(dfd, &stb)< 0) {
314 /* Impossible! */
315 fprintf(stderr, Name ": fstat failed for %s: %s\n",
316 devname, strerror(errno));
da6b5ca9 317 tmpdev->used = 2;
cd29a5c8
NB
318 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
319 fprintf(stderr, Name ": %s is not a block device.\n",
52826846 320 devname);
da6b5ca9 321 tmpdev->used = 2;
5083d66b
N
322 } else if (must_be_container(dfd)) {
323 if (st) {
324 /* already found some components, this cannot
325 * be another one.
326 */
327 if (report_missmatch)
328 fprintf(stderr, Name ": %s is a container, but we are looking for components\n",
329 devname);
330 tmpdev->used = 2;
331 } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
332 if (report_missmatch)
333 fprintf(stderr, Name ": not a recognisable container: %s\n",
334 devname);
335 tmpdev->used = 2;
417f346e
HCP
336 } else if (!tst->ss->load_container
337 || tst->ss->load_container(tst, dfd, NULL)) {
5083d66b
N
338 if (report_missmatch)
339 fprintf(stderr, Name ": no correct container type: %s\n",
340 devname);
341 tmpdev->used = 2;
342 } else if (auto_assem &&
343 !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)),
344 tst->ss->match_home(tst, homehost) == 1)) {
345 if (report_missmatch)
346 fprintf(stderr, Name ": %s has metadata type %s for which "
347 "auto-assembly is disabled\n",
348 devname, tst->ss->name);
349 tmpdev->used = 2;
350 } else
351 found_container = 1;
52826846 352 } else {
5083d66b
N
353 if (!tst && (tst = guess_super(dfd)) == NULL) {
354 if (report_missmatch)
355 fprintf(stderr, Name ": no recogniseable superblock on %s\n",
356 devname);
357 tmpdev->used = 2;
358 } else if (tst->ss->load_super(tst,dfd, NULL)) {
359 if (report_missmatch)
360 fprintf(stderr, Name ": no RAID superblock on %s\n",
361 devname);
362 tmpdev->used = 2;
363 } else if (tst->ss->compare_super == NULL) {
364 if (report_missmatch)
365 fprintf(stderr, Name ": Cannot assemble %s metadata on %s\n",
366 tst->ss->name, devname);
367 tmpdev->used = 2;
368 } else if (auto_assem && st == NULL &&
369 !conf_test_metadata(tst->ss->name, (pol = devnum_policy(stb.st_rdev)),
370 tst->ss->match_home(tst, homehost) == 1)) {
371 if (report_missmatch)
372 fprintf(stderr, Name ": %s has metadata type %s for which "
373 "auto-assembly is disabled\n",
374 devname, tst->ss->name);
375 tmpdev->used = 2;
376 }
64c4757e 377 }
c06487ce 378 if (dfd >= 0) close(dfd);
d68ea4d7
N
379 if (tmpdev->used == 2) {
380 if (auto_assem)
381 /* Ignore unrecognised devices during auto-assembly */
382 goto loop;
383 if (ident->uuid_set || ident->name[0] ||
384 ident->super_minor != UnSet)
385 /* Ignore unrecognised device if looking for
386 * specific array */
387 goto loop;
388
389
390 fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
391 devname);
392 if (st)
393 st->ss->free_super(st);
394 dev_policy_free(pol);
395 return 1;
396 }
52826846 397
5083d66b 398 if (found_container) {
9008ed1c
N
399 /* tmpdev is a container. We need to be either
400 * looking for a member, or auto-assembling
401 */
9008ed1c
N
402
403 if (ident->container) {
404 if (ident->container[0] == '/' &&
405 !same_dev(ident->container, devname)) {
406 if (report_missmatch)
407 fprintf(stderr, Name ": %s is not the container required (%s)\n",
408 devname, ident->container);
409 goto loop;
410 }
411 if (ident->container[0] != '/') {
412 /* we have a uuid */
413 int uuid[4];
87477e6d
N
414
415 content = &info;
416 memset(content, 0, sizeof(*content));
417 tst->ss->getinfo_super(tst, content, NULL);
418
9008ed1c
N
419 if (!parse_uuid(ident->container, uuid) ||
420 !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) {
421 if (report_missmatch)
422 fprintf(stderr, Name ": %s has wrong UUID to be required container\n",
423 devname);
424 goto loop;
425 }
426 }
427 }
428 /* It is worth looking inside this container.
429 */
7636b5a8
N
430 if (verbose > 0)
431 fprintf(stderr, Name ": looking in container %s\n",
432 devname);
1415fe4b 433
88cef9b3
N
434 for (content = tst->ss->container_content(tst, NULL);
435 content;
436 content = content->next) {
1415fe4b 437
a06d022d
KW
438 /* do not assemble arrays that might have bad blocks */
439 if (content->array.state & (1<<MD_SB_BBM_ERRORS)) {
440 fprintf(stderr, Name ": BBM log found in metadata. "
441 "Cannot activate array(s).\n");
442 tmpdev->used = 2;
443 goto loop;
444 }
88cef9b3
N
445 if (!ident_matches(ident, content, tst,
446 homehost, update,
447 report_missmatch ? devname : NULL))
448 /* message already printed */;
449 else if (is_member_busy(content->text_version)) {
450 if (report_missmatch)
451 fprintf(stderr, Name ": member %s in %s is already assembled\n",
452 content->text_version,
453 devname);
454 } else
455 break;
456 }
457 if (!content) {
f7ad3ccc 458 tmpdev->used = 2;
88cef9b3 459 goto loop; /* empty container */
fa031239 460 }
88cef9b3 461
9008ed1c 462 st = tst; tst = NULL;
4c1c3ad8 463 if (!auto_assem && inargv && tmpdev->next != NULL) {
9008ed1c
N
464 fprintf(stderr, Name ": %s is a container, but is not "
465 "only device given: confused and aborting\n",
466 devname);
467 st->ss->free_super(st);
4e8d9f0a 468 dev_policy_free(pol);
9008ed1c
N
469 return 1;
470 }
7636b5a8
N
471 if (verbose > 0)
472 fprintf(stderr, Name ": found match on member %s in %s\n",
473 content->text_version, devname);
bac0d92e 474
5083d66b
N
475 /* make sure we finished the loop */
476 tmpdev = NULL;
bac0d92e 477 goto loop;
5083d66b 478 } else {
bac0d92e 479
5083d66b
N
480 content = &info;
481 memset(content, 0, sizeof(*content));
482 tst->ss->getinfo_super(tst, content, NULL);
483
484 if (!ident_matches(ident, content, tst,
485 homehost, update,
486 report_missmatch ? devname : NULL))
df37ffc0 487 goto loop;
5083d66b
N
488
489 if (st == NULL)
490 st = dup_super(tst);
491 if (st->minor_version == -1)
492 st->minor_version = tst->minor_version;
493 if (st->ss != tst->ss ||
494 st->minor_version != tst->minor_version ||
495 st->ss->compare_super(st, tst) != 0) {
496 /* Some mismatch. If exactly one array matches this host,
497 * we can resolve on that one.
498 * Or, if we are auto assembling, we just ignore the second
499 * for now.
500 */
501 if (auto_assem)
502 goto loop;
503 if (homehost) {
504 int first = st->ss->match_home(st, homehost);
505 int last = tst->ss->match_home(tst, homehost);
506 if (first != last &&
507 (first == 1 || last == 1)) {
508 /* We can do something */
509 if (first) {/* just ignore this one */
510 if (report_missmatch)
511 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
512 devname);
513 goto loop;
514 } else { /* reject all those sofar */
515 struct mddev_dev *td;
516 if (report_missmatch)
517 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
518 devname);
519 for (td=devlist; td != tmpdev; td=td->next)
520 if (td->used == 1)
521 td->used = 0;
522 tmpdev->used = 1;
523 goto loop;
524 }
83b6208e
NB
525 }
526 }
5083d66b
N
527 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
528 devname);
529 tst->ss->free_super(tst);
530 st->ss->free_super(st);
531 dev_policy_free(pol);
532 return 1;
83b6208e 533 }
5083d66b 534 tmpdev->used = 1;
64c4757e 535 }
df37ffc0 536 loop:
4e8d9f0a
N
537 dev_policy_free(pol);
538 pol = NULL;
3d2b16e7
NB
539 if (tst)
540 tst->ss->free_super(tst);
811e6cbe
NB
541 }
542
98dbd966 543 if (!st || !st->sb || !content)
c30e5369
N
544 return 2;
545
05833051 546 /* Now need to open the array device. Use create_mddev */
98dbd966 547 if (content == &info)
a5d85af7 548 st->ss->getinfo_super(st, content, NULL);
3d2c4fc7 549
69207ff6 550 trustworthy = FOREIGN;
05833051 551 name = content->name;
745f72f6
N
552 switch (st->ss->match_home(st, homehost)
553 ?: st->ss->match_home(st, "any")) {
69207ff6
N
554 case 1:
555 trustworthy = LOCAL;
98dbd966 556 name = strchr(content->name, ':');
69207ff6
N
557 if (name)
558 name++;
3d2c4fc7 559 else
98dbd966 560 name = content->name;
69207ff6 561 break;
c30e5369 562 }
05833051 563 if (!auto_assem)
aa7c284c 564 /* If the array is listed in mdadm.conf or on
ac2ecf55
N
565 * command line, then we trust the name
566 * even if the array doesn't look local
567 */
568 trustworthy = LOCAL;
569
05833051 570 if (name[0] == 0 &&
98dbd966
DW
571 content->array.level == LEVEL_CONTAINER) {
572 name = content->text_version;
a4bc1720
N
573 trustworthy = METADATA;
574 }
0ac91628
N
575
576 if (name[0] && trustworthy != LOCAL &&
577 ! require_homehost &&
578 conf_name_is_free(name))
579 trustworthy = LOCAL;
580
7cdc0872
N
581 if (trustworthy == LOCAL &&
582 strchr(name, ':'))
583 /* Ignore 'host:' prefix of name */
584 name = strchr(name, ':')+1;
585
a04d5763
N
586 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
587 chosen_name);
c30e5369
N
588 if (mdfd < 0) {
589 st->ss->free_super(st);
c30e5369 590 if (auto_assem)
60e1bc1a 591 goto try_again;
c30e5369
N
592 return 1;
593 }
a04d5763 594 mddev = chosen_name;
c30e5369
N
595 vers = md_get_version(mdfd);
596 if (vers < 9000) {
597 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
598 " Upgrade your kernel or try --build\n");
599 close(mdfd);
600 return 1;
601 }
66afdfa9 602 if (mddev_busy(fd2devnum(mdfd))) {
c30e5369
N
603 fprintf(stderr, Name ": %s already active, cannot restart it!\n",
604 mddev);
605 for (tmpdev = devlist ;
606 tmpdev && tmpdev->used != 1;
607 tmpdev = tmpdev->next)
608 ;
609 if (tmpdev && auto_assem)
610 fprintf(stderr, Name ": %s needed for %s...\n",
611 mddev, tmpdev->devname);
612 close(mdfd);
613 mdfd = -3;
614 st->ss->free_super(st);
c30e5369
N
615 if (auto_assem)
616 goto try_again;
617 return 1;
8a46fe84 618 }
c30e5369 619 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
8a46fe84 620
9008ed1c
N
621#ifndef MDASSEMBLE
622 if (content != &info) {
623 /* This is a member of a container. Try starting the array. */
624 return assemble_container_content(st, mdfd, content, runstop,
625 chosen_name, verbose);
626 }
627#endif
811e6cbe 628 /* Ok, no bad inconsistancy, we can try updating etc */
bf4fb153 629 bitmap_done = 0;
6e46bf34 630 content->update_private = NULL;
d7f7ebb7 631 devices = malloc(num_devs * sizeof(*devices));
02e7c5b7 632 devmap = calloc(num_devs * content->array.raid_disks, 1);
da6b5ca9 633 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
811e6cbe
NB
634 char *devname = tmpdev->devname;
635 struct stat stb;
5787fa49 636 /* looks like a good enough match to update the super block if needed */
d1f1011b 637#ifndef MDASSEMBLE
5787fa49 638 if (update) {
811e6cbe 639 int dfd;
4b1ac34b
NB
640 /* prepare useful information in info structures */
641 struct stat stb2;
3da92f27 642 struct supertype *tst;
1e2b2765 643 int err;
4b1ac34b 644 fstat(mdfd, &stb2);
7d99579f
NB
645
646 if (strcmp(update, "uuid")==0 &&
647 !ident->uuid_set) {
648 int rfd;
649 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
650 read(rfd, ident->uuid, 16) != 16) {
651 *(__u32*)(ident->uuid) = random();
652 *(__u32*)(ident->uuid+1) = random();
653 *(__u32*)(ident->uuid+2) = random();
654 *(__u32*)(ident->uuid+3) = random();
655 }
656 if (rfd >= 0) close(rfd);
7d99579f 657 }
811e6cbe
NB
658 dfd = dev_open(devname, O_RDWR|O_EXCL);
659
3da92f27 660 tst = dup_super(st);
9f22b13f
N
661 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
662 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
663 devname);
664 if (dfd >= 0)
665 close(dfd);
666 close(mdfd);
d7f7ebb7 667 free(devices);
02e7c5b7 668 free(devmap);
9f22b13f
N
669 return 1;
670 }
02e7c5b7 671 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
811e6cbe 672
98dbd966
DW
673 memcpy(content->uuid, ident->uuid, 16);
674 strcpy(content->name, ident->name);
675 content->array.md_minor = minor(stb2.st_rdev);
811e6cbe 676
1e2b2765
N
677 if (strcmp(update, "byteorder") == 0)
678 err = 0;
679 else
680 err = tst->ss->update_super(tst, content, update,
681 devname, verbose,
682 ident->uuid_set,
683 homehost);
684 if (err < 0) {
685 fprintf(stderr,
686 Name ": --update=%s not understood"
687 " for %s metadata\n",
688 update, tst->ss->name);
689 tst->ss->free_super(tst);
690 free(tst);
691 close(mdfd);
692 close(dfd);
d7f7ebb7 693 free(devices);
02e7c5b7 694 free(devmap);
1e2b2765
N
695 return 1;
696 }
e5eac01f
NB
697 if (strcmp(update, "uuid")==0 &&
698 !ident->uuid_set) {
699 ident->uuid_set = 1;
98dbd966 700 memcpy(ident->uuid, content->uuid, 16);
e5eac01f 701 }
1e2b2765 702 if (tst->ss->store_super(tst, dfd))
5787fa49
NB
703 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
704 devname);
1e2b2765 705 close(dfd);
8131b493
NB
706
707 if (strcmp(update, "uuid")==0 &&
bf4fb153 708 ident->bitmap_fd >= 0 && !bitmap_done) {
3da92f27 709 if (bitmap_update_uuid(ident->bitmap_fd,
98dbd966 710 content->uuid,
3da92f27 711 tst->ss->swapuuid) != 0)
bf4fb153
NB
712 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
713 else
714 bitmap_done = 1;
715 }
3da92f27 716 tst->ss->free_super(tst);
d1f1011b
NB
717 } else
718#endif
719 {
30e1b9a5 720 struct supertype *tst = dup_super(st);
da6b5ca9
NB
721 int dfd;
722 dfd = dev_open(devname, O_RDWR|O_EXCL);
723
9f22b13f
N
724 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
725 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
726 devname);
727 if (dfd >= 0)
728 close(dfd);
729 close(mdfd);
d7f7ebb7 730 free(devices);
02e7c5b7 731 free(devmap);
9f22b13f
N
732 return 1;
733 }
02e7c5b7 734 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
3da92f27 735 tst->ss->free_super(tst);
da6b5ca9 736 close(dfd);
5787fa49
NB
737 }
738
811e6cbe
NB
739 stat(devname, &stb);
740
dab6685f 741 if (verbose > 0)
cd29a5c8 742 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
98dbd966 743 devname, mddev, content->disk.raid_disk);
64c4757e 744 devices[devcnt].devname = devname;
64c4757e 745 devices[devcnt].uptodate = 0;
98dbd966 746 devices[devcnt].i = *content;
213ee40b
NB
747 devices[devcnt].i.disk.major = major(stb.st_rdev);
748 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
64c4757e 749 if (most_recent < devcnt) {
213ee40b
NB
750 if (devices[devcnt].i.events
751 > devices[most_recent].i.events)
64c4757e
NB
752 most_recent = devcnt;
753 }
df0d4ea0 754 if (content->array.level == LEVEL_MULTIPATH)
5787fa49
NB
755 /* with multipath, the raid_disk from the superblock is meaningless */
756 i = devcnt;
757 else
213ee40b 758 i = devices[devcnt].i.disk.raid_disk;
308e1801 759 if (i+1 == 0) {
98dbd966
DW
760 if (nextspare < content->array.raid_disks)
761 nextspare = content->array.raid_disks;
308e1801 762 i = nextspare++;
08110d41 763 } else {
98dbd966 764 if (i >= content->array.raid_disks &&
08110d41
NB
765 i >= nextspare)
766 nextspare = i+1;
308e1801 767 }
98c6faba 768 if (i < 10000) {
56eedc1a 769 if (i >= bestcnt) {
f21e18ca 770 int newbestcnt = i+10;
56eedc1a 771 int *newbest = malloc(sizeof(int)*newbestcnt);
f21e18ca 772 int c;
56eedc1a
NB
773 for (c=0; c < newbestcnt; c++)
774 if (c < bestcnt)
775 newbest[c] = best[c];
776 else
777 newbest[c] = -1;
778 if (best)free(best);
779 best = newbest;
780 bestcnt = newbestcnt;
781 }
6a255b69 782 if (best[i] >=0 &&
213ee40b
NB
783 devices[best[i]].i.events
784 == devices[devcnt].i.events
785 && (devices[best[i]].i.disk.minor
786 != devices[devcnt].i.disk.minor)
b8ac1967 787 && st->ss == &super0
98dbd966 788 && content->array.level != LEVEL_MULTIPATH) {
6a255b69
NB
789 /* two different devices with identical superblock.
790 * Could be a mis-detection caused by overlapping
791 * partitions. fail-safe.
792 */
793 fprintf(stderr, Name ": WARNING %s and %s appear"
794 " to have very similar superblocks.\n"
795 " If they are really different, "
796 "please --zero the superblock on one\n"
d2df86e0
NB
797 " If they are the same or overlap,"
798 " please remove one from %s.\n",
799 devices[best[i]].devname, devname,
800 inargv ? "the list" :
801 "the\n DEVICE list in mdadm.conf"
802 );
7f91af49 803 close(mdfd);
d7f7ebb7 804 free(devices);
02e7c5b7 805 free(devmap);
6a255b69
NB
806 return 1;
807 }
52826846 808 if (best[i] == -1
213ee40b
NB
809 || (devices[best[i]].i.events
810 < devices[devcnt].i.events))
52826846 811 best[i] = devcnt;
56eedc1a 812 }
64c4757e 813 devcnt++;
df37ffc0 814 }
6e46bf34
DW
815 free(content->update_private);
816 content->update_private = NULL;
4b1ac34b 817
64c4757e 818 if (devcnt == 0) {
682c7051 819 fprintf(stderr, Name ": no devices found for %s\n",
64c4757e 820 mddev);
3d2b16e7
NB
821 if (st)
822 st->ss->free_super(st);
7f91af49 823 close(mdfd);
d7f7ebb7 824 free(devices);
02e7c5b7 825 free(devmap);
64c4757e
NB
826 return 1;
827 }
4b1ac34b 828
3d2b16e7
NB
829 if (update && strcmp(update, "byteorder")==0)
830 st->minor_version = 90;
831
a5d85af7 832 st->ss->getinfo_super(st, content, NULL);
98dbd966 833 clean = content->array.state & 1;
4b1ac34b 834
64c4757e
NB
835 /* now we have some devices that might be suitable.
836 * I wonder how many
837 */
98dbd966
DW
838 avail = malloc(content->array.raid_disks);
839 memset(avail, 0, content->array.raid_disks);
64c4757e 840 okcnt = 0;
52826846 841 sparecnt=0;
1ff98339 842 rebuilding_cnt=0;
f21e18ca 843 for (i=0; i< bestcnt; i++) {
64c4757e 844 int j = best[i];
ee04451c
NB
845 int event_margin = 1; /* always allow a difference of '1'
846 * like the kernel does
847 */
64c4757e 848 if (j < 0) continue;
d013a55e
NB
849 /* note: we ignore error flags in multipath arrays
850 * as they don't make sense
851 */
df0d4ea0 852 if (content->array.level != LEVEL_MULTIPATH)
f22385f9 853 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
213ee40b
NB
854 if (!(devices[j].i.disk.state
855 & (1<<MD_DISK_FAULTY)))
aa88f531 856 sparecnt++;
d013a55e 857 continue;
aa88f531 858 }
02e7c5b7
N
859 /* If this devices thinks that 'most_recent' has failed, then
860 * we must reject this device.
861 */
862 if (j != most_recent &&
863 content->array.raid_disks > 0 &&
864 devices[most_recent].i.disk.raid_disk >= 0 &&
865 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
866 if (verbose > -1)
867 fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n",
868 devices[j].devname, devices[most_recent].devname);
869 best[i] = -1;
870 continue;
871 }
213ee40b
NB
872 if (devices[j].i.events+event_margin >=
873 devices[most_recent].i.events) {
64c4757e 874 devices[j].uptodate = 1;
1ff98339 875 if (i < content->array.raid_disks) {
dcc4210f
DW
876 if (devices[j].i.recovery_start == MaxSector ||
877 (content->reshape_active &&
878 j >= content->array.raid_disks - content->delta_disks)) {
1ff98339
N
879 okcnt++;
880 avail[i]=1;
881 } else
882 rebuilding_cnt++;
265e0f17 883 } else
52826846 884 sparecnt++;
64c4757e
NB
885 }
886 }
02e7c5b7 887 free(devmap);
98dbd966
DW
888 while (force && !enough(content->array.level, content->array.raid_disks,
889 content->array.layout, 1,
265e0f17 890 avail, okcnt)) {
64c4757e
NB
891 /* Choose the newest best drive which is
892 * not up-to-date, update the superblock
893 * and add it.
894 */
52826846 895 int fd;
3da92f27 896 struct supertype *tst;
f21e18ca 897 unsigned long long current_events;
cd29a5c8 898 chosen_drive = -1;
f21e18ca 899 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
52826846
NB
900 int j = best[i];
901 if (j>=0 &&
902 !devices[j].uptodate &&
921d9e16 903 devices[j].i.recovery_start == MaxSector &&
52826846 904 (chosen_drive < 0 ||
213ee40b
NB
905 devices[j].i.events
906 > devices[chosen_drive].i.events))
52826846
NB
907 chosen_drive = j;
908 }
909 if (chosen_drive < 0)
910 break;
213ee40b 911 current_events = devices[chosen_drive].i.events;
c5a6f9a6 912 add_another:
dab6685f
NB
913 if (verbose >= 0)
914 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
213ee40b
NB
915 devices[chosen_drive].devname,
916 devices[chosen_drive].i.disk.raid_disk,
917 (int)(devices[chosen_drive].i.events),
918 (int)(devices[most_recent].i.events));
8b0dabea 919 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846
NB
920 if (fd < 0) {
921 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
922 devices[chosen_drive].devname);
213ee40b 923 devices[chosen_drive].i.events = 0;
52826846
NB
924 continue;
925 }
3da92f27 926 tst = dup_super(st);
60b435db 927 if (tst->ss->load_super(tst,fd, NULL)) {
52826846
NB
928 close(fd);
929 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
930 devices[chosen_drive].devname);
213ee40b 931 devices[chosen_drive].i.events = 0;
52826846
NB
932 continue;
933 }
98dbd966
DW
934 content->events = devices[most_recent].i.events;
935 tst->ss->update_super(tst, content, "force-one",
67a8c82d
NB
936 devices[chosen_drive].devname, verbose,
937 0, NULL);
4b1ac34b 938
3da92f27 939 if (tst->ss->store_super(tst, fd)) {
52826846
NB
940 close(fd);
941 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
942 devices[chosen_drive].devname);
213ee40b 943 devices[chosen_drive].i.events = 0;
3da92f27 944 tst->ss->free_super(tst);
52826846
NB
945 continue;
946 }
947 close(fd);
213ee40b 948 devices[chosen_drive].i.events = devices[most_recent].i.events;
52826846 949 devices[chosen_drive].uptodate = 1;
265e0f17 950 avail[chosen_drive] = 1;
52826846 951 okcnt++;
3da92f27 952 tst->ss->free_super(tst);
c5a6f9a6
NB
953
954 /* If there are any other drives of the same vintage,
955 * add them in as well. We can't lose and we might gain
956 */
f21e18ca 957 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
c5a6f9a6
NB
958 int j = best[i];
959 if (j >= 0 &&
960 !devices[j].uptodate &&
213ee40b 961 devices[j].i.events == current_events) {
c5a6f9a6
NB
962 chosen_drive = j;
963 goto add_another;
964 }
965 }
64c4757e 966 }
52826846
NB
967
968 /* Now we want to look at the superblock which the kernel will base things on
969 * and compare the devices that we think are working with the devices that the
970 * superblock thinks are working.
971 * If there are differences and --force is given, then update this chosen
972 * superblock.
973 */
cd29a5c8 974 chosen_drive = -1;
3da92f27 975 st->ss->free_super(st);
56eedc1a 976 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
977 int j = best[i];
978 int fd;
4b1ac34b 979
52826846
NB
980 if (j<0)
981 continue;
982 if (!devices[j].uptodate)
983 continue;
984 chosen_drive = j;
8b0dabea 985 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
52826846
NB
986 fprintf(stderr, Name ": Cannot open %s: %s\n",
987 devices[j].devname, strerror(errno));
7f91af49 988 close(mdfd);
d7f7ebb7 989 free(devices);
52826846
NB
990 return 1;
991 }
3da92f27 992 if (st->ss->load_super(st,fd, NULL)) {
52826846
NB
993 close(fd);
994 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
995 devices[j].devname);
7f91af49 996 close(mdfd);
d7f7ebb7 997 free(devices);
52826846
NB
998 return 1;
999 }
1000 close(fd);
1001 }
3da92f27 1002 if (st->sb == NULL) {
4b1ac34b 1003 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
7f91af49 1004 close(mdfd);
d7f7ebb7 1005 free(devices);
4b1ac34b
NB
1006 return 1;
1007 }
a5d85af7 1008 st->ss->getinfo_super(st, content, NULL);
f35f2525 1009#ifndef MDASSEMBLE
98dbd966 1010 sysfs_init(content, mdfd, 0);
f35f2525 1011#endif
56eedc1a 1012 for (i=0; i<bestcnt; i++) {
52826846 1013 int j = best[i];
98c6faba 1014 unsigned int desired_state;
11a3e71d 1015
98dbd966 1016 if (i < content->array.raid_disks)
11a3e71d
NB
1017 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1018 else
1019 desired_state = 0;
1020
52826846
NB
1021 if (j<0)
1022 continue;
1023 if (!devices[j].uptodate)
1024 continue;
4b1ac34b 1025
213ee40b 1026 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1027 if (!(devices[j].i.array.state & 1))
1028 clean = 0;
213ee40b
NB
1029
1030 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
68c7d6d7 1031 verbose, 0, NULL)) {
52826846 1032 if (force) {
dab6685f
NB
1033 if (verbose >= 0)
1034 fprintf(stderr, Name ": "
1035 "clearing FAULTY flag for device %d in %s for %s\n",
1036 j, mddev, devices[j].devname);
4b1ac34b 1037 change = 1;
52826846 1038 } else {
dab6685f
NB
1039 if (verbose >= -1)
1040 fprintf(stderr, Name ": "
1041 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1042 i, mddev, devices[j].devname);
52826846
NB
1043 }
1044 }
4b1ac34b 1045#if 0
213ee40b 1046 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
52826846
NB
1047 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1048 i, mddev);
1049 }
4b1ac34b 1050#endif
52826846 1051 }
c5a6f9a6 1052 if (force && !clean &&
98dbd966
DW
1053 !enough(content->array.level, content->array.raid_disks,
1054 content->array.layout, clean,
c5a6f9a6 1055 avail, okcnt)) {
98dbd966 1056 change += st->ss->update_super(st, content, "force-array",
67a8c82d
NB
1057 devices[chosen_drive].devname, verbose,
1058 0, NULL);
583315d9 1059 clean = 1;
d013a55e 1060 }
52826846 1061
4b1ac34b 1062 if (change) {
52826846 1063 int fd;
8b0dabea 1064 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 1065 if (fd < 0) {
353632d9 1066 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
52826846 1067 devices[chosen_drive].devname);
7f91af49 1068 close(mdfd);
d7f7ebb7 1069 free(devices);
52826846
NB
1070 return 1;
1071 }
3da92f27 1072 if (st->ss->store_super(st, fd)) {
52826846
NB
1073 close(fd);
1074 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1075 devices[chosen_drive].devname);
7f91af49 1076 close(mdfd);
d7f7ebb7 1077 free(devices);
52826846
NB
1078 return 1;
1079 }
1080 close(fd);
52826846
NB
1081 }
1082
353632d9
NB
1083 /* If we are in the middle of a reshape we may need to restore saved data
1084 * that was moved aside due to the reshape overwriting live data
1085 * The code of doing this lives in Grow.c
1086 */
39c5a909 1087#ifndef MDASSEMBLE
98dbd966 1088 if (content->reshape_active) {
353632d9
NB
1089 int err = 0;
1090 int *fdlist = malloc(sizeof(int)* bestcnt);
cd77ac4e 1091 if (verbose > 0)
ea0ebe96
N
1092 fprintf(stderr, Name ":%s has an active reshape - checking "
1093 "if critical section needs to be restored\n",
1094 chosen_name);
353632d9
NB
1095 for (i=0; i<bestcnt; i++) {
1096 int j = best[i];
1097 if (j >= 0) {
1098 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
1099 if (fdlist[i] < 0) {
1100 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1101 devices[j].devname);
1102 err = 1;
1103 break;
1104 }
1105 } else
1106 fdlist[i] = -1;
1107 }
87f26d14
N
1108 if (!err) {
1109 err = Grow_restart(st, content, fdlist, bestcnt,
1110 backup_file, verbose > 0);
1111 if (err && invalid_backup) {
1112 if (verbose > 0)
1113 fprintf(stderr, Name ": continuing"
1114 " without restoring backup\n");
1115 err = 0;
1116 }
1117 }
353632d9
NB
1118 while (i>0) {
1119 i--;
1120 if (fdlist[i]>=0) close(fdlist[i]);
1121 }
1122 if (err) {
1123 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
e9e43ec3
N
1124 if (backup_file == NULL)
1125 fprintf(stderr," Possibly you needed to specify the --backup-file\n");
7f91af49 1126 close(mdfd);
d7f7ebb7 1127 free(devices);
353632d9
NB
1128 return err;
1129 }
1130 }
39c5a909 1131#endif
aa88f531
NB
1132 /* count number of in-sync devices according to the superblock.
1133 * We must have this number to start the array without -s or -R
1134 */
98dbd966 1135 req_cnt = content->array.working_disks;
aa88f531 1136
64c4757e
NB
1137 /* Almost ready to actually *do* something */
1138 if (!old_linux) {
fbf8a0b7 1139 int rv;
a19c88b8 1140
a04d5763
N
1141 /* First, fill in the map, so that udev can find our name
1142 * as soon as we become active.
1143 */
98dbd966
DW
1144 map_update(NULL, fd2devnum(mdfd), content->text_version,
1145 content->uuid, chosen_name);
a04d5763 1146
98dbd966 1147 rv = set_array_info(mdfd, st, content);
fbf8a0b7 1148 if (rv) {
f35f2525 1149 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
64c4757e 1150 mddev, strerror(errno));
24af7a87 1151 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1152 close(mdfd);
d7f7ebb7 1153 free(devices);
64c4757e
NB
1154 return 1;
1155 }
11484a63 1156 if (ident->bitmap_fd >= 0) {
c82f047c
NB
1157 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
1158 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
24af7a87 1159 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1160 close(mdfd);
d7f7ebb7 1161 free(devices);
c82f047c
NB
1162 return 1;
1163 }
7ef02d01
NB
1164 } else if (ident->bitmap_file) {
1165 /* From config file */
1166 int bmfd = open(ident->bitmap_file, O_RDWR);
1167 if (bmfd < 0) {
1168 fprintf(stderr, Name ": Could not open bitmap file %s\n",
1169 ident->bitmap_file);
24af7a87 1170 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1171 close(mdfd);
d7f7ebb7 1172 free(devices);
7ef02d01
NB
1173 return 1;
1174 }
1175 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
1176 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
1177 close(bmfd);
24af7a87 1178 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1179 close(mdfd);
d7f7ebb7 1180 free(devices);
7ef02d01
NB
1181 return 1;
1182 }
1183 close(bmfd);
c82f047c 1184 }
7ef02d01 1185
52826846 1186 /* First, add the raid disks, but add the chosen one last */
56eedc1a 1187 for (i=0; i<= bestcnt; i++) {
52826846 1188 int j;
56eedc1a 1189 if (i < bestcnt) {
52826846
NB
1190 j = best[i];
1191 if (j == chosen_drive)
1192 continue;
1193 } else
1194 j = chosen_drive;
1195
56eedc1a 1196 if (j >= 0 /* && devices[j].uptodate */) {
484ae54d
N
1197 int dfd = dev_open(devices[j].devname,
1198 O_RDWR|O_EXCL);
1199 if (dfd >= 0) {
1200 remove_partitions(dfd);
1201 close(dfd);
1202 }
98dbd966 1203 rv = add_disk(mdfd, st, content, &devices[j].i);
7801ac20 1204
a19c88b8 1205 if (rv) {
213ee40b
NB
1206 fprintf(stderr, Name ": failed to add "
1207 "%s to %s: %s\n",
64c4757e
NB
1208 devices[j].devname,
1209 mddev,
1210 strerror(errno));
98dbd966 1211 if (i < content->array.raid_disks
213ee40b 1212 || i == bestcnt)
52826846
NB
1213 okcnt--;
1214 else
1215 sparecnt--;
dab6685f 1216 } else if (verbose > 0)
213ee40b
NB
1217 fprintf(stderr, Name ": added %s "
1218 "to %s as %d\n",
1219 devices[j].devname, mddev,
1220 devices[j].i.disk.raid_disk);
98dbd966 1221 } else if (verbose > 0 && i < content->array.raid_disks)
213ee40b
NB
1222 fprintf(stderr, Name ": no uptodate device for "
1223 "slot %d of %s\n",
64c4757e
NB
1224 i, mddev);
1225 }
aba69144 1226
98dbd966 1227 if (content->array.level == LEVEL_CONTAINER) {
598f0d58
NB
1228 if (verbose >= 0) {
1229 fprintf(stderr, Name ": Container %s has been "
1230 "assembled with %d drive%s",
57ed8c91 1231 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
f21e18ca 1232 if (okcnt < (unsigned)content->array.raid_disks)
598f0d58 1233 fprintf(stderr, " (out of %d)",
98dbd966 1234 content->array.raid_disks);
598f0d58
NB
1235 fprintf(stderr, "\n");
1236 }
98dbd966 1237 sysfs_uevent(content, "change");
a7c6e3fb 1238 wait_for(chosen_name, mdfd);
7f91af49 1239 close(mdfd);
d7f7ebb7 1240 free(devices);
598f0d58
NB
1241 return 0;
1242 }
1243
64c4757e 1244 if (runstop == 1 ||
b8a8ccf9 1245 (runstop <= 0 &&
98dbd966
DW
1246 ( enough(content->array.level, content->array.raid_disks,
1247 content->array.layout, clean, avail, okcnt) &&
1ff98339 1248 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
aa88f531 1249 ))) {
e9e43ec3
N
1250 /* This array is good-to-go.
1251 * If a reshape is in progress then we might need to
1252 * continue monitoring it. In that case we start
1253 * it read-only and let the grow code make it writable.
1254 */
1255 int rv;
eb3929a4 1256#ifndef MDASSEMBLE
e9e43ec3
N
1257 if (content->reshape_active &&
1258 content->delta_disks <= 0)
1259 rv = Grow_continue(mdfd, st, content, backup_file);
1260 else
eb3929a4 1261#endif
e9e43ec3
N
1262 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1263 if (rv == 0) {
dab6685f
NB
1264 if (verbose >= 0) {
1265 fprintf(stderr, Name ": %s has been started with %d drive%s",
1266 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1267 if (okcnt < (unsigned)content->array.raid_disks)
98dbd966 1268 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1ff98339
N
1269 if (rebuilding_cnt)
1270 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
dab6685f
NB
1271 if (sparecnt)
1272 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1273 fprintf(stderr, ".\n");
1274 }
8a659c33
N
1275 if (content->reshape_active &&
1276 content->array.level >= 4 &&
1277 content->array.level <= 6) {
acee8e89
N
1278 /* might need to increase the size
1279 * of the stripe cache - default is 256
1280 */
8a659c33 1281 if (256 < 4 * (content->array.chunk_size/4096)) {
acee8e89
N
1282 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1283 if (sra)
1284 sysfs_set_num(sra, NULL,
1285 "stripe_cache_size",
8a659c33 1286 (4 * content->array.chunk_size / 4096) + 1);
acee8e89
N
1287 }
1288 }
7e83544b
N
1289 if (okcnt < (unsigned)content->array.raid_disks) {
1290 /* If any devices did not get added
1291 * because the kernel rejected them based
1292 * on event count, try adding them
1293 * again providing the action policy is
1294 * 're-add' or greater. The bitmap
1295 * might allow them to be included, or
1296 * they will become spares.
1297 */
1298 for (i = 0; i <= bestcnt; i++) {
1299 int j = best[i];
1300 if (j >= 0 && !devices[j].uptodate) {
1301 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1302 continue;
1303 rv = add_disk(mdfd, st, content,
1304 &devices[j].i);
1305 if (rv == 0 && verbose >= 0)
1306 fprintf(stderr,
1307 Name ": %s has been re-added.\n",
1308 devices[j].devname);
1309 }
1310 }
1311 }
a7c6e3fb 1312 wait_for(mddev, mdfd);
7f91af49
N
1313 close(mdfd);
1314 if (auto_assem) {
589395d6 1315 int usecs = 1;
589395d6
NB
1316 /* There is a nasty race with 'mdadm --monitor'.
1317 * If it opens this device before we close it,
1318 * it gets an incomplete open on which IO
598f0d58
NB
1319 * doesn't work and the capacity is
1320 * wrong.
589395d6
NB
1321 * If we reopen (to check for layered devices)
1322 * before --monitor closes, we loose.
1323 *
1324 * So: wait upto 1 second for there to be
1325 * a non-zero capacity.
1326 */
1327 while (usecs < 1000) {
1328 mdfd = open(mddev, O_RDONLY);
1329 if (mdfd >= 0) {
beae1dfe
NB
1330 unsigned long long size;
1331 if (get_dev_size(mdfd, NULL, &size) &&
589395d6
NB
1332 size > 0)
1333 break;
1334 close(mdfd);
1335 }
1336 usleep(usecs);
1337 usecs <<= 1;
1338 }
1339 }
d7f7ebb7 1340 free(devices);
64c4757e 1341 return 0;
82b27616 1342 }
682c7051 1343 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
64c4757e 1344 mddev, strerror(errno));
583315d9 1345
98dbd966
DW
1346 if (!enough(content->array.level, content->array.raid_disks,
1347 content->array.layout, 1, avail, okcnt))
583315d9
NB
1348 fprintf(stderr, Name ": Not enough devices to "
1349 "start the array.\n");
98dbd966
DW
1350 else if (!enough(content->array.level,
1351 content->array.raid_disks,
1352 content->array.layout, clean,
583315d9
NB
1353 avail, okcnt))
1354 fprintf(stderr, Name ": Not enough devices to "
1355 "start the array while not clean "
1356 "- consider --force.\n");
1357
7f91af49 1358 if (auto_assem)
1c203a4b 1359 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1360 close(mdfd);
d7f7ebb7 1361 free(devices);
64c4757e
NB
1362 return 1;
1363 }
82b27616 1364 if (runstop == -1) {
b8a8ccf9 1365 fprintf(stderr, Name ": %s assembled from %d drive%s",
52826846 1366 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1367 if (okcnt != (unsigned)content->array.raid_disks)
98dbd966 1368 fprintf(stderr, " (out of %d)", content->array.raid_disks);
b8a8ccf9 1369 fprintf(stderr, ", but not started.\n");
7f91af49 1370 close(mdfd);
d7f7ebb7 1371 free(devices);
64c4757e 1372 return 0;
82b27616 1373 }
4855f95c 1374 if (verbose >= -1) {
dab6685f 1375 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1ff98339
N
1376 if (rebuilding_cnt)
1377 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
dab6685f
NB
1378 if (sparecnt)
1379 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
98dbd966
DW
1380 if (!enough(content->array.level, content->array.raid_disks,
1381 content->array.layout, 1, avail, okcnt))
dab6685f 1382 fprintf(stderr, " - not enough to start the array.\n");
98dbd966
DW
1383 else if (!enough(content->array.level,
1384 content->array.raid_disks,
1385 content->array.layout, clean,
583315d9
NB
1386 avail, okcnt))
1387 fprintf(stderr, " - not enough to start the "
1388 "array while not clean - consider "
1389 "--force.\n");
dab6685f 1390 else {
f21e18ca 1391 if (req_cnt == (unsigned)content->array.raid_disks)
dab6685f
NB
1392 fprintf(stderr, " - need all %d to start it", req_cnt);
1393 else
98dbd966 1394 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
dab6685f
NB
1395 fprintf(stderr, " (use --run to insist).\n");
1396 }
aa88f531 1397 }
7f91af49 1398 if (auto_assem)
1c203a4b 1399 ioctl(mdfd, STOP_ARRAY, NULL);
56a8da69 1400 close(mdfd);
d7f7ebb7 1401 free(devices);
64c4757e 1402 return 1;
82b27616 1403 } else {
52826846
NB
1404 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1405 * been updated to point to the current locations of devices.
1406 * so we can just start the array
82b27616 1407 */
cd29a5c8 1408 unsigned long dev;
213ee40b
NB
1409 dev = makedev(devices[chosen_drive].i.disk.major,
1410 devices[chosen_drive].i.disk.minor);
82b27616
NB
1411 if (ioctl(mdfd, START_ARRAY, dev)) {
1412 fprintf(stderr, Name ": Cannot start array: %s\n",
1413 strerror(errno));
1414 }
aba69144 1415
64c4757e 1416 }
7f91af49 1417 close(mdfd);
d7f7ebb7 1418 free(devices);
e0d19036 1419 return 0;
64c4757e 1420}
6234c63c
DW
1421
1422#ifndef MDASSEMBLE
1423int assemble_container_content(struct supertype *st, int mdfd,
1424 struct mdinfo *content, int runstop,
1425 char *chosen_name, int verbose)
1426{
1427 struct mdinfo *dev, *sra;
1428 int working = 0, preexist = 0;
1429 struct map_ent *map = NULL;
1430
1431 sysfs_init(content, mdfd, 0);
1432
1433 sra = sysfs_read(mdfd, 0, GET_VERSION);
1434 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
4408ee76
N
1435 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1436 close(mdfd);
6234c63c 1437 return 1;
4408ee76 1438 }
6234c63c
DW
1439 if (sra)
1440 sysfs_free(sra);
1441
1442 for (dev = content->devs; dev; dev = dev->next)
462906cd 1443 if (sysfs_add_disk(content, dev, 1) == 0)
6234c63c
DW
1444 working++;
1445 else if (errno == EEXIST)
1446 preexist++;
4408ee76
N
1447 if (working == 0) {
1448 close(mdfd);
7cb2aa33 1449 return 1;/* Nothing new, don't try to start */
8b4e5ea9
N
1450 }
1451
1452 map_update(&map, fd2devnum(mdfd),
1453 content->text_version,
1454 content->uuid, chosen_name);
1455
1456 if (runstop > 0 ||
6234c63c 1457 (working + preexist) >= content->array.working_disks) {
bb50e5d3 1458 int err;
a714580e 1459
6234c63c
DW
1460 switch(content->array.level) {
1461 case LEVEL_LINEAR:
1462 case LEVEL_MULTIPATH:
1463 case 0:
bb50e5d3
N
1464 err = sysfs_set_str(content, NULL, "array_state",
1465 "active");
6234c63c
DW
1466 break;
1467 default:
bb50e5d3 1468 err = sysfs_set_str(content, NULL, "array_state",
6234c63c
DW
1469 "readonly");
1470 /* start mdmon if needed. */
bb50e5d3
N
1471 if (!err) {
1472 if (!mdmon_running(st->container_dev))
1473 start_mdmon(st->container_dev);
1474 ping_monitor(devnum2devname(st->container_dev));
1475 }
6234c63c
DW
1476 break;
1477 }
bb50e5d3
N
1478 if (!err)
1479 sysfs_set_safemode(content, content->safe_mode_delay);
6234c63c 1480 if (verbose >= 0) {
bb50e5d3
N
1481 if (err)
1482 fprintf(stderr, Name
1483 ": array %s now has %d devices",
1484 chosen_name, working + preexist);
1485 else
1486 fprintf(stderr, Name
1487 ": Started %s with %d devices",
1488 chosen_name, working + preexist);
6234c63c
DW
1489 if (preexist)
1490 fprintf(stderr, " (%d new)", working);
1491 fprintf(stderr, "\n");
1492 }
bb50e5d3 1493 if (!err)
a7c6e3fb 1494 wait_for(chosen_name, mdfd);
4408ee76 1495 close(mdfd);
7cb2aa33 1496 return 0;
6234c63c 1497 /* FIXME should have an O_EXCL and wait for read-auto */
7cb2aa33 1498 } else {
6234c63c
DW
1499 if (verbose >= 0)
1500 fprintf(stderr, Name
1501 ": %s assembled with %d devices but "
1502 "not started\n",
1503 chosen_name, working);
4408ee76 1504 close(mdfd);
7cb2aa33
N
1505 return 1;
1506 }
6234c63c
DW
1507}
1508#endif
1509