]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Merge branch 'master' into devel-3.2
[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
88cef9b3
N
438 if (!ident_matches(ident, content, tst,
439 homehost, update,
440 report_missmatch ? devname : NULL))
441 /* message already printed */;
442 else if (is_member_busy(content->text_version)) {
443 if (report_missmatch)
444 fprintf(stderr, Name ": member %s in %s is already assembled\n",
445 content->text_version,
446 devname);
447 } else
448 break;
449 }
450 if (!content) {
f7ad3ccc 451 tmpdev->used = 2;
88cef9b3 452 goto loop; /* empty container */
fa031239 453 }
88cef9b3 454
9008ed1c 455 st = tst; tst = NULL;
4c1c3ad8 456 if (!auto_assem && inargv && tmpdev->next != NULL) {
9008ed1c
N
457 fprintf(stderr, Name ": %s is a container, but is not "
458 "only device given: confused and aborting\n",
459 devname);
460 st->ss->free_super(st);
4e8d9f0a 461 dev_policy_free(pol);
9008ed1c
N
462 return 1;
463 }
7636b5a8
N
464 if (verbose > 0)
465 fprintf(stderr, Name ": found match on member %s in %s\n",
466 content->text_version, devname);
bac0d92e 467
5083d66b
N
468 /* make sure we finished the loop */
469 tmpdev = NULL;
bac0d92e 470 goto loop;
5083d66b 471 } else {
bac0d92e 472
5083d66b
N
473 content = &info;
474 memset(content, 0, sizeof(*content));
475 tst->ss->getinfo_super(tst, content, NULL);
476
477 if (!ident_matches(ident, content, tst,
478 homehost, update,
479 report_missmatch ? devname : NULL))
df37ffc0 480 goto loop;
5083d66b
N
481
482 if (st == NULL)
483 st = dup_super(tst);
484 if (st->minor_version == -1)
485 st->minor_version = tst->minor_version;
486 if (st->ss != tst->ss ||
487 st->minor_version != tst->minor_version ||
488 st->ss->compare_super(st, tst) != 0) {
489 /* Some mismatch. If exactly one array matches this host,
490 * we can resolve on that one.
491 * Or, if we are auto assembling, we just ignore the second
492 * for now.
493 */
494 if (auto_assem)
495 goto loop;
496 if (homehost) {
497 int first = st->ss->match_home(st, homehost);
498 int last = tst->ss->match_home(tst, homehost);
499 if (first != last &&
500 (first == 1 || last == 1)) {
501 /* We can do something */
502 if (first) {/* just ignore this one */
503 if (report_missmatch)
504 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
505 devname);
506 goto loop;
507 } else { /* reject all those sofar */
508 struct mddev_dev *td;
509 if (report_missmatch)
510 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
511 devname);
512 for (td=devlist; td != tmpdev; td=td->next)
513 if (td->used == 1)
514 td->used = 0;
515 tmpdev->used = 1;
516 goto loop;
517 }
83b6208e
NB
518 }
519 }
5083d66b
N
520 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
521 devname);
522 tst->ss->free_super(tst);
523 st->ss->free_super(st);
524 dev_policy_free(pol);
525 return 1;
83b6208e 526 }
5083d66b 527 tmpdev->used = 1;
64c4757e 528 }
df37ffc0 529 loop:
4e8d9f0a
N
530 dev_policy_free(pol);
531 pol = NULL;
3d2b16e7
NB
532 if (tst)
533 tst->ss->free_super(tst);
811e6cbe
NB
534 }
535
98dbd966 536 if (!st || !st->sb || !content)
c30e5369
N
537 return 2;
538
05833051 539 /* Now need to open the array device. Use create_mddev */
98dbd966 540 if (content == &info)
a5d85af7 541 st->ss->getinfo_super(st, content, NULL);
3d2c4fc7 542
69207ff6 543 trustworthy = FOREIGN;
05833051 544 name = content->name;
745f72f6
N
545 switch (st->ss->match_home(st, homehost)
546 ?: st->ss->match_home(st, "any")) {
69207ff6
N
547 case 1:
548 trustworthy = LOCAL;
98dbd966 549 name = strchr(content->name, ':');
69207ff6
N
550 if (name)
551 name++;
3d2c4fc7 552 else
98dbd966 553 name = content->name;
69207ff6 554 break;
c30e5369 555 }
05833051 556 if (!auto_assem)
aa7c284c 557 /* If the array is listed in mdadm.conf or on
ac2ecf55
N
558 * command line, then we trust the name
559 * even if the array doesn't look local
560 */
561 trustworthy = LOCAL;
562
05833051 563 if (name[0] == 0 &&
98dbd966
DW
564 content->array.level == LEVEL_CONTAINER) {
565 name = content->text_version;
a4bc1720
N
566 trustworthy = METADATA;
567 }
0ac91628
N
568
569 if (name[0] && trustworthy != LOCAL &&
570 ! require_homehost &&
571 conf_name_is_free(name))
572 trustworthy = LOCAL;
573
7cdc0872
N
574 if (trustworthy == LOCAL &&
575 strchr(name, ':'))
576 /* Ignore 'host:' prefix of name */
577 name = strchr(name, ':')+1;
578
a04d5763
N
579 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
580 chosen_name);
c30e5369
N
581 if (mdfd < 0) {
582 st->ss->free_super(st);
c30e5369 583 if (auto_assem)
60e1bc1a 584 goto try_again;
c30e5369
N
585 return 1;
586 }
a04d5763 587 mddev = chosen_name;
c30e5369
N
588 vers = md_get_version(mdfd);
589 if (vers < 9000) {
590 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
591 " Upgrade your kernel or try --build\n");
592 close(mdfd);
593 return 1;
594 }
66afdfa9 595 if (mddev_busy(fd2devnum(mdfd))) {
c30e5369
N
596 fprintf(stderr, Name ": %s already active, cannot restart it!\n",
597 mddev);
598 for (tmpdev = devlist ;
599 tmpdev && tmpdev->used != 1;
600 tmpdev = tmpdev->next)
601 ;
602 if (tmpdev && auto_assem)
603 fprintf(stderr, Name ": %s needed for %s...\n",
604 mddev, tmpdev->devname);
605 close(mdfd);
606 mdfd = -3;
607 st->ss->free_super(st);
c30e5369
N
608 if (auto_assem)
609 goto try_again;
610 return 1;
8a46fe84 611 }
c30e5369 612 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
8a46fe84 613
9008ed1c
N
614#ifndef MDASSEMBLE
615 if (content != &info) {
616 /* This is a member of a container. Try starting the array. */
617 return assemble_container_content(st, mdfd, content, runstop,
618 chosen_name, verbose);
619 }
620#endif
811e6cbe 621 /* Ok, no bad inconsistancy, we can try updating etc */
bf4fb153 622 bitmap_done = 0;
6e46bf34 623 content->update_private = NULL;
d7f7ebb7 624 devices = malloc(num_devs * sizeof(*devices));
02e7c5b7 625 devmap = calloc(num_devs * content->array.raid_disks, 1);
da6b5ca9 626 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
811e6cbe
NB
627 char *devname = tmpdev->devname;
628 struct stat stb;
5787fa49 629 /* looks like a good enough match to update the super block if needed */
d1f1011b 630#ifndef MDASSEMBLE
5787fa49 631 if (update) {
811e6cbe 632 int dfd;
4b1ac34b
NB
633 /* prepare useful information in info structures */
634 struct stat stb2;
3da92f27 635 struct supertype *tst;
1e2b2765 636 int err;
4b1ac34b 637 fstat(mdfd, &stb2);
7d99579f
NB
638
639 if (strcmp(update, "uuid")==0 &&
640 !ident->uuid_set) {
641 int rfd;
642 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
643 read(rfd, ident->uuid, 16) != 16) {
644 *(__u32*)(ident->uuid) = random();
645 *(__u32*)(ident->uuid+1) = random();
646 *(__u32*)(ident->uuid+2) = random();
647 *(__u32*)(ident->uuid+3) = random();
648 }
649 if (rfd >= 0) close(rfd);
7d99579f 650 }
811e6cbe
NB
651 dfd = dev_open(devname, O_RDWR|O_EXCL);
652
3da92f27 653 tst = dup_super(st);
9f22b13f
N
654 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
655 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
656 devname);
657 if (dfd >= 0)
658 close(dfd);
659 close(mdfd);
d7f7ebb7 660 free(devices);
02e7c5b7 661 free(devmap);
9f22b13f
N
662 return 1;
663 }
02e7c5b7 664 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
811e6cbe 665
98dbd966
DW
666 memcpy(content->uuid, ident->uuid, 16);
667 strcpy(content->name, ident->name);
668 content->array.md_minor = minor(stb2.st_rdev);
811e6cbe 669
1e2b2765
N
670 if (strcmp(update, "byteorder") == 0)
671 err = 0;
672 else
673 err = tst->ss->update_super(tst, content, update,
674 devname, verbose,
675 ident->uuid_set,
676 homehost);
677 if (err < 0) {
678 fprintf(stderr,
679 Name ": --update=%s not understood"
680 " for %s metadata\n",
681 update, tst->ss->name);
682 tst->ss->free_super(tst);
683 free(tst);
684 close(mdfd);
685 close(dfd);
d7f7ebb7 686 free(devices);
02e7c5b7 687 free(devmap);
1e2b2765
N
688 return 1;
689 }
e5eac01f
NB
690 if (strcmp(update, "uuid")==0 &&
691 !ident->uuid_set) {
692 ident->uuid_set = 1;
98dbd966 693 memcpy(ident->uuid, content->uuid, 16);
e5eac01f 694 }
1e2b2765 695 if (tst->ss->store_super(tst, dfd))
5787fa49
NB
696 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
697 devname);
1e2b2765 698 close(dfd);
8131b493
NB
699
700 if (strcmp(update, "uuid")==0 &&
bf4fb153 701 ident->bitmap_fd >= 0 && !bitmap_done) {
3da92f27 702 if (bitmap_update_uuid(ident->bitmap_fd,
98dbd966 703 content->uuid,
3da92f27 704 tst->ss->swapuuid) != 0)
bf4fb153
NB
705 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
706 else
707 bitmap_done = 1;
708 }
3da92f27 709 tst->ss->free_super(tst);
d1f1011b
NB
710 } else
711#endif
712 {
30e1b9a5 713 struct supertype *tst = dup_super(st);
da6b5ca9
NB
714 int dfd;
715 dfd = dev_open(devname, O_RDWR|O_EXCL);
716
9f22b13f
N
717 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
718 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
719 devname);
720 if (dfd >= 0)
721 close(dfd);
722 close(mdfd);
d7f7ebb7 723 free(devices);
02e7c5b7 724 free(devmap);
9f22b13f
N
725 return 1;
726 }
02e7c5b7 727 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
3da92f27 728 tst->ss->free_super(tst);
da6b5ca9 729 close(dfd);
5787fa49
NB
730 }
731
811e6cbe
NB
732 stat(devname, &stb);
733
dab6685f 734 if (verbose > 0)
cd29a5c8 735 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
98dbd966 736 devname, mddev, content->disk.raid_disk);
64c4757e 737 devices[devcnt].devname = devname;
64c4757e 738 devices[devcnt].uptodate = 0;
98dbd966 739 devices[devcnt].i = *content;
213ee40b
NB
740 devices[devcnt].i.disk.major = major(stb.st_rdev);
741 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
64c4757e 742 if (most_recent < devcnt) {
213ee40b
NB
743 if (devices[devcnt].i.events
744 > devices[most_recent].i.events)
64c4757e
NB
745 most_recent = devcnt;
746 }
df0d4ea0 747 if (content->array.level == LEVEL_MULTIPATH)
5787fa49
NB
748 /* with multipath, the raid_disk from the superblock is meaningless */
749 i = devcnt;
750 else
213ee40b 751 i = devices[devcnt].i.disk.raid_disk;
308e1801 752 if (i+1 == 0) {
98dbd966
DW
753 if (nextspare < content->array.raid_disks)
754 nextspare = content->array.raid_disks;
308e1801 755 i = nextspare++;
08110d41 756 } else {
98dbd966 757 if (i >= content->array.raid_disks &&
08110d41
NB
758 i >= nextspare)
759 nextspare = i+1;
308e1801 760 }
98c6faba 761 if (i < 10000) {
56eedc1a 762 if (i >= bestcnt) {
f21e18ca 763 int newbestcnt = i+10;
56eedc1a 764 int *newbest = malloc(sizeof(int)*newbestcnt);
f21e18ca 765 int c;
56eedc1a
NB
766 for (c=0; c < newbestcnt; c++)
767 if (c < bestcnt)
768 newbest[c] = best[c];
769 else
770 newbest[c] = -1;
771 if (best)free(best);
772 best = newbest;
773 bestcnt = newbestcnt;
774 }
6a255b69 775 if (best[i] >=0 &&
213ee40b
NB
776 devices[best[i]].i.events
777 == devices[devcnt].i.events
778 && (devices[best[i]].i.disk.minor
779 != devices[devcnt].i.disk.minor)
b8ac1967 780 && st->ss == &super0
98dbd966 781 && content->array.level != LEVEL_MULTIPATH) {
6a255b69
NB
782 /* two different devices with identical superblock.
783 * Could be a mis-detection caused by overlapping
784 * partitions. fail-safe.
785 */
786 fprintf(stderr, Name ": WARNING %s and %s appear"
787 " to have very similar superblocks.\n"
788 " If they are really different, "
789 "please --zero the superblock on one\n"
d2df86e0
NB
790 " If they are the same or overlap,"
791 " please remove one from %s.\n",
792 devices[best[i]].devname, devname,
793 inargv ? "the list" :
794 "the\n DEVICE list in mdadm.conf"
795 );
7f91af49 796 close(mdfd);
d7f7ebb7 797 free(devices);
02e7c5b7 798 free(devmap);
6a255b69
NB
799 return 1;
800 }
52826846 801 if (best[i] == -1
213ee40b
NB
802 || (devices[best[i]].i.events
803 < devices[devcnt].i.events))
52826846 804 best[i] = devcnt;
56eedc1a 805 }
64c4757e 806 devcnt++;
df37ffc0 807 }
6e46bf34
DW
808 free(content->update_private);
809 content->update_private = NULL;
4b1ac34b 810
64c4757e 811 if (devcnt == 0) {
682c7051 812 fprintf(stderr, Name ": no devices found for %s\n",
64c4757e 813 mddev);
3d2b16e7
NB
814 if (st)
815 st->ss->free_super(st);
7f91af49 816 close(mdfd);
d7f7ebb7 817 free(devices);
02e7c5b7 818 free(devmap);
64c4757e
NB
819 return 1;
820 }
4b1ac34b 821
3d2b16e7
NB
822 if (update && strcmp(update, "byteorder")==0)
823 st->minor_version = 90;
824
a5d85af7 825 st->ss->getinfo_super(st, content, NULL);
98dbd966 826 clean = content->array.state & 1;
4b1ac34b 827
64c4757e
NB
828 /* now we have some devices that might be suitable.
829 * I wonder how many
830 */
98dbd966
DW
831 avail = malloc(content->array.raid_disks);
832 memset(avail, 0, content->array.raid_disks);
64c4757e 833 okcnt = 0;
52826846 834 sparecnt=0;
1ff98339 835 rebuilding_cnt=0;
f21e18ca 836 for (i=0; i< bestcnt; i++) {
64c4757e 837 int j = best[i];
ee04451c
NB
838 int event_margin = 1; /* always allow a difference of '1'
839 * like the kernel does
840 */
64c4757e 841 if (j < 0) continue;
d013a55e
NB
842 /* note: we ignore error flags in multipath arrays
843 * as they don't make sense
844 */
df0d4ea0 845 if (content->array.level != LEVEL_MULTIPATH)
f22385f9 846 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
213ee40b
NB
847 if (!(devices[j].i.disk.state
848 & (1<<MD_DISK_FAULTY)))
aa88f531 849 sparecnt++;
d013a55e 850 continue;
aa88f531 851 }
02e7c5b7
N
852 /* If this devices thinks that 'most_recent' has failed, then
853 * we must reject this device.
854 */
855 if (j != most_recent &&
856 content->array.raid_disks > 0 &&
857 devices[most_recent].i.disk.raid_disk >= 0 &&
858 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
859 if (verbose > -1)
860 fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n",
861 devices[j].devname, devices[most_recent].devname);
862 best[i] = -1;
863 continue;
864 }
213ee40b
NB
865 if (devices[j].i.events+event_margin >=
866 devices[most_recent].i.events) {
64c4757e 867 devices[j].uptodate = 1;
1ff98339 868 if (i < content->array.raid_disks) {
dcc4210f
DW
869 if (devices[j].i.recovery_start == MaxSector ||
870 (content->reshape_active &&
871 j >= content->array.raid_disks - content->delta_disks)) {
1ff98339
N
872 okcnt++;
873 avail[i]=1;
874 } else
875 rebuilding_cnt++;
265e0f17 876 } else
52826846 877 sparecnt++;
64c4757e
NB
878 }
879 }
02e7c5b7 880 free(devmap);
98dbd966
DW
881 while (force && !enough(content->array.level, content->array.raid_disks,
882 content->array.layout, 1,
265e0f17 883 avail, okcnt)) {
64c4757e
NB
884 /* Choose the newest best drive which is
885 * not up-to-date, update the superblock
886 * and add it.
887 */
52826846 888 int fd;
3da92f27 889 struct supertype *tst;
f21e18ca 890 unsigned long long current_events;
cd29a5c8 891 chosen_drive = -1;
f21e18ca 892 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
52826846
NB
893 int j = best[i];
894 if (j>=0 &&
895 !devices[j].uptodate &&
921d9e16 896 devices[j].i.recovery_start == MaxSector &&
52826846 897 (chosen_drive < 0 ||
213ee40b
NB
898 devices[j].i.events
899 > devices[chosen_drive].i.events))
52826846
NB
900 chosen_drive = j;
901 }
902 if (chosen_drive < 0)
903 break;
213ee40b 904 current_events = devices[chosen_drive].i.events;
c5a6f9a6 905 add_another:
dab6685f
NB
906 if (verbose >= 0)
907 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
213ee40b
NB
908 devices[chosen_drive].devname,
909 devices[chosen_drive].i.disk.raid_disk,
910 (int)(devices[chosen_drive].i.events),
911 (int)(devices[most_recent].i.events));
8b0dabea 912 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846
NB
913 if (fd < 0) {
914 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
915 devices[chosen_drive].devname);
213ee40b 916 devices[chosen_drive].i.events = 0;
52826846
NB
917 continue;
918 }
3da92f27 919 tst = dup_super(st);
60b435db 920 if (tst->ss->load_super(tst,fd, NULL)) {
52826846
NB
921 close(fd);
922 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
923 devices[chosen_drive].devname);
213ee40b 924 devices[chosen_drive].i.events = 0;
52826846
NB
925 continue;
926 }
98dbd966
DW
927 content->events = devices[most_recent].i.events;
928 tst->ss->update_super(tst, content, "force-one",
67a8c82d
NB
929 devices[chosen_drive].devname, verbose,
930 0, NULL);
4b1ac34b 931
3da92f27 932 if (tst->ss->store_super(tst, fd)) {
52826846
NB
933 close(fd);
934 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
935 devices[chosen_drive].devname);
213ee40b 936 devices[chosen_drive].i.events = 0;
3da92f27 937 tst->ss->free_super(tst);
52826846
NB
938 continue;
939 }
940 close(fd);
213ee40b 941 devices[chosen_drive].i.events = devices[most_recent].i.events;
52826846 942 devices[chosen_drive].uptodate = 1;
265e0f17 943 avail[chosen_drive] = 1;
52826846 944 okcnt++;
3da92f27 945 tst->ss->free_super(tst);
c5a6f9a6
NB
946
947 /* If there are any other drives of the same vintage,
948 * add them in as well. We can't lose and we might gain
949 */
f21e18ca 950 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
c5a6f9a6
NB
951 int j = best[i];
952 if (j >= 0 &&
953 !devices[j].uptodate &&
213ee40b 954 devices[j].i.events == current_events) {
c5a6f9a6
NB
955 chosen_drive = j;
956 goto add_another;
957 }
958 }
64c4757e 959 }
52826846
NB
960
961 /* Now we want to look at the superblock which the kernel will base things on
962 * and compare the devices that we think are working with the devices that the
963 * superblock thinks are working.
964 * If there are differences and --force is given, then update this chosen
965 * superblock.
966 */
cd29a5c8 967 chosen_drive = -1;
3da92f27 968 st->ss->free_super(st);
56eedc1a 969 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
970 int j = best[i];
971 int fd;
4b1ac34b 972
52826846
NB
973 if (j<0)
974 continue;
975 if (!devices[j].uptodate)
976 continue;
977 chosen_drive = j;
8b0dabea 978 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
52826846
NB
979 fprintf(stderr, Name ": Cannot open %s: %s\n",
980 devices[j].devname, strerror(errno));
7f91af49 981 close(mdfd);
d7f7ebb7 982 free(devices);
52826846
NB
983 return 1;
984 }
3da92f27 985 if (st->ss->load_super(st,fd, NULL)) {
52826846
NB
986 close(fd);
987 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
988 devices[j].devname);
7f91af49 989 close(mdfd);
d7f7ebb7 990 free(devices);
52826846
NB
991 return 1;
992 }
993 close(fd);
994 }
3da92f27 995 if (st->sb == NULL) {
4b1ac34b 996 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
7f91af49 997 close(mdfd);
d7f7ebb7 998 free(devices);
4b1ac34b
NB
999 return 1;
1000 }
a5d85af7 1001 st->ss->getinfo_super(st, content, NULL);
f35f2525 1002#ifndef MDASSEMBLE
98dbd966 1003 sysfs_init(content, mdfd, 0);
f35f2525 1004#endif
56eedc1a 1005 for (i=0; i<bestcnt; i++) {
52826846 1006 int j = best[i];
98c6faba 1007 unsigned int desired_state;
11a3e71d 1008
98dbd966 1009 if (i < content->array.raid_disks)
11a3e71d
NB
1010 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1011 else
1012 desired_state = 0;
1013
52826846
NB
1014 if (j<0)
1015 continue;
1016 if (!devices[j].uptodate)
1017 continue;
4b1ac34b 1018
213ee40b 1019 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1020 if (!(devices[j].i.array.state & 1))
1021 clean = 0;
213ee40b
NB
1022
1023 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
68c7d6d7 1024 verbose, 0, NULL)) {
52826846 1025 if (force) {
dab6685f
NB
1026 if (verbose >= 0)
1027 fprintf(stderr, Name ": "
1028 "clearing FAULTY flag for device %d in %s for %s\n",
1029 j, mddev, devices[j].devname);
4b1ac34b 1030 change = 1;
52826846 1031 } else {
dab6685f
NB
1032 if (verbose >= -1)
1033 fprintf(stderr, Name ": "
1034 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1035 i, mddev, devices[j].devname);
52826846
NB
1036 }
1037 }
4b1ac34b 1038#if 0
213ee40b 1039 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
52826846
NB
1040 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1041 i, mddev);
1042 }
4b1ac34b 1043#endif
52826846 1044 }
c5a6f9a6 1045 if (force && !clean &&
98dbd966
DW
1046 !enough(content->array.level, content->array.raid_disks,
1047 content->array.layout, clean,
c5a6f9a6 1048 avail, okcnt)) {
98dbd966 1049 change += st->ss->update_super(st, content, "force-array",
67a8c82d
NB
1050 devices[chosen_drive].devname, verbose,
1051 0, NULL);
583315d9 1052 clean = 1;
d013a55e 1053 }
52826846 1054
4b1ac34b 1055 if (change) {
52826846 1056 int fd;
8b0dabea 1057 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 1058 if (fd < 0) {
353632d9 1059 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
52826846 1060 devices[chosen_drive].devname);
7f91af49 1061 close(mdfd);
d7f7ebb7 1062 free(devices);
52826846
NB
1063 return 1;
1064 }
3da92f27 1065 if (st->ss->store_super(st, fd)) {
52826846
NB
1066 close(fd);
1067 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1068 devices[chosen_drive].devname);
7f91af49 1069 close(mdfd);
d7f7ebb7 1070 free(devices);
52826846
NB
1071 return 1;
1072 }
1073 close(fd);
52826846
NB
1074 }
1075
353632d9
NB
1076 /* If we are in the middle of a reshape we may need to restore saved data
1077 * that was moved aside due to the reshape overwriting live data
1078 * The code of doing this lives in Grow.c
1079 */
39c5a909 1080#ifndef MDASSEMBLE
98dbd966 1081 if (content->reshape_active) {
353632d9
NB
1082 int err = 0;
1083 int *fdlist = malloc(sizeof(int)* bestcnt);
cd77ac4e 1084 if (verbose > 0)
ea0ebe96
N
1085 fprintf(stderr, Name ":%s has an active reshape - checking "
1086 "if critical section needs to be restored\n",
1087 chosen_name);
353632d9
NB
1088 for (i=0; i<bestcnt; i++) {
1089 int j = best[i];
1090 if (j >= 0) {
1091 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
1092 if (fdlist[i] < 0) {
1093 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1094 devices[j].devname);
1095 err = 1;
1096 break;
1097 }
1098 } else
1099 fdlist[i] = -1;
1100 }
87f26d14
N
1101 if (!err) {
1102 err = Grow_restart(st, content, fdlist, bestcnt,
1103 backup_file, verbose > 0);
1104 if (err && invalid_backup) {
1105 if (verbose > 0)
1106 fprintf(stderr, Name ": continuing"
1107 " without restoring backup\n");
1108 err = 0;
1109 }
1110 }
353632d9
NB
1111 while (i>0) {
1112 i--;
1113 if (fdlist[i]>=0) close(fdlist[i]);
1114 }
1115 if (err) {
1116 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
e9e43ec3
N
1117 if (backup_file == NULL)
1118 fprintf(stderr," Possibly you needed to specify the --backup-file\n");
7f91af49 1119 close(mdfd);
d7f7ebb7 1120 free(devices);
353632d9
NB
1121 return err;
1122 }
1123 }
39c5a909 1124#endif
aa88f531
NB
1125 /* count number of in-sync devices according to the superblock.
1126 * We must have this number to start the array without -s or -R
1127 */
98dbd966 1128 req_cnt = content->array.working_disks;
aa88f531 1129
64c4757e
NB
1130 /* Almost ready to actually *do* something */
1131 if (!old_linux) {
fbf8a0b7 1132 int rv;
a19c88b8 1133
a04d5763
N
1134 /* First, fill in the map, so that udev can find our name
1135 * as soon as we become active.
1136 */
98dbd966
DW
1137 map_update(NULL, fd2devnum(mdfd), content->text_version,
1138 content->uuid, chosen_name);
a04d5763 1139
98dbd966 1140 rv = set_array_info(mdfd, st, content);
fbf8a0b7 1141 if (rv) {
f35f2525 1142 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
64c4757e 1143 mddev, strerror(errno));
24af7a87 1144 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1145 close(mdfd);
d7f7ebb7 1146 free(devices);
64c4757e
NB
1147 return 1;
1148 }
11484a63 1149 if (ident->bitmap_fd >= 0) {
c82f047c
NB
1150 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
1151 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
24af7a87 1152 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1153 close(mdfd);
d7f7ebb7 1154 free(devices);
c82f047c
NB
1155 return 1;
1156 }
7ef02d01
NB
1157 } else if (ident->bitmap_file) {
1158 /* From config file */
1159 int bmfd = open(ident->bitmap_file, O_RDWR);
1160 if (bmfd < 0) {
1161 fprintf(stderr, Name ": Could not open bitmap file %s\n",
1162 ident->bitmap_file);
24af7a87 1163 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1164 close(mdfd);
d7f7ebb7 1165 free(devices);
7ef02d01
NB
1166 return 1;
1167 }
1168 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
1169 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
1170 close(bmfd);
24af7a87 1171 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1172 close(mdfd);
d7f7ebb7 1173 free(devices);
7ef02d01
NB
1174 return 1;
1175 }
1176 close(bmfd);
c82f047c 1177 }
7ef02d01 1178
52826846 1179 /* First, add the raid disks, but add the chosen one last */
56eedc1a 1180 for (i=0; i<= bestcnt; i++) {
52826846 1181 int j;
56eedc1a 1182 if (i < bestcnt) {
52826846
NB
1183 j = best[i];
1184 if (j == chosen_drive)
1185 continue;
1186 } else
1187 j = chosen_drive;
1188
56eedc1a 1189 if (j >= 0 /* && devices[j].uptodate */) {
484ae54d
N
1190 int dfd = dev_open(devices[j].devname,
1191 O_RDWR|O_EXCL);
1192 if (dfd >= 0) {
1193 remove_partitions(dfd);
1194 close(dfd);
1195 }
98dbd966 1196 rv = add_disk(mdfd, st, content, &devices[j].i);
7801ac20 1197
a19c88b8 1198 if (rv) {
213ee40b
NB
1199 fprintf(stderr, Name ": failed to add "
1200 "%s to %s: %s\n",
64c4757e
NB
1201 devices[j].devname,
1202 mddev,
1203 strerror(errno));
98dbd966 1204 if (i < content->array.raid_disks
213ee40b 1205 || i == bestcnt)
52826846
NB
1206 okcnt--;
1207 else
1208 sparecnt--;
dab6685f 1209 } else if (verbose > 0)
213ee40b
NB
1210 fprintf(stderr, Name ": added %s "
1211 "to %s as %d\n",
1212 devices[j].devname, mddev,
1213 devices[j].i.disk.raid_disk);
98dbd966 1214 } else if (verbose > 0 && i < content->array.raid_disks)
213ee40b
NB
1215 fprintf(stderr, Name ": no uptodate device for "
1216 "slot %d of %s\n",
64c4757e
NB
1217 i, mddev);
1218 }
aba69144 1219
98dbd966 1220 if (content->array.level == LEVEL_CONTAINER) {
598f0d58
NB
1221 if (verbose >= 0) {
1222 fprintf(stderr, Name ": Container %s has been "
1223 "assembled with %d drive%s",
57ed8c91 1224 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
f21e18ca 1225 if (okcnt < (unsigned)content->array.raid_disks)
598f0d58 1226 fprintf(stderr, " (out of %d)",
98dbd966 1227 content->array.raid_disks);
598f0d58
NB
1228 fprintf(stderr, "\n");
1229 }
98dbd966 1230 sysfs_uevent(content, "change");
a7c6e3fb 1231 wait_for(chosen_name, mdfd);
7f91af49 1232 close(mdfd);
d7f7ebb7 1233 free(devices);
598f0d58
NB
1234 return 0;
1235 }
1236
64c4757e 1237 if (runstop == 1 ||
b8a8ccf9 1238 (runstop <= 0 &&
98dbd966
DW
1239 ( enough(content->array.level, content->array.raid_disks,
1240 content->array.layout, clean, avail, okcnt) &&
1ff98339 1241 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
aa88f531 1242 ))) {
e9e43ec3
N
1243 /* This array is good-to-go.
1244 * If a reshape is in progress then we might need to
1245 * continue monitoring it. In that case we start
1246 * it read-only and let the grow code make it writable.
1247 */
1248 int rv;
eb3929a4 1249#ifndef MDASSEMBLE
e9e43ec3
N
1250 if (content->reshape_active &&
1251 content->delta_disks <= 0)
1252 rv = Grow_continue(mdfd, st, content, backup_file);
1253 else
eb3929a4 1254#endif
e9e43ec3
N
1255 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1256 if (rv == 0) {
dab6685f
NB
1257 if (verbose >= 0) {
1258 fprintf(stderr, Name ": %s has been started with %d drive%s",
1259 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1260 if (okcnt < (unsigned)content->array.raid_disks)
98dbd966 1261 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1ff98339
N
1262 if (rebuilding_cnt)
1263 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
dab6685f
NB
1264 if (sparecnt)
1265 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1266 fprintf(stderr, ".\n");
1267 }
8a659c33
N
1268 if (content->reshape_active &&
1269 content->array.level >= 4 &&
1270 content->array.level <= 6) {
acee8e89
N
1271 /* might need to increase the size
1272 * of the stripe cache - default is 256
1273 */
8a659c33 1274 if (256 < 4 * (content->array.chunk_size/4096)) {
acee8e89
N
1275 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1276 if (sra)
1277 sysfs_set_num(sra, NULL,
1278 "stripe_cache_size",
8a659c33 1279 (4 * content->array.chunk_size / 4096) + 1);
acee8e89
N
1280 }
1281 }
7e83544b
N
1282 if (okcnt < (unsigned)content->array.raid_disks) {
1283 /* If any devices did not get added
1284 * because the kernel rejected them based
1285 * on event count, try adding them
1286 * again providing the action policy is
1287 * 're-add' or greater. The bitmap
1288 * might allow them to be included, or
1289 * they will become spares.
1290 */
1291 for (i = 0; i <= bestcnt; i++) {
1292 int j = best[i];
1293 if (j >= 0 && !devices[j].uptodate) {
1294 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1295 continue;
1296 rv = add_disk(mdfd, st, content,
1297 &devices[j].i);
1298 if (rv == 0 && verbose >= 0)
1299 fprintf(stderr,
1300 Name ": %s has been re-added.\n",
1301 devices[j].devname);
1302 }
1303 }
1304 }
a7c6e3fb 1305 wait_for(mddev, mdfd);
7f91af49
N
1306 close(mdfd);
1307 if (auto_assem) {
589395d6 1308 int usecs = 1;
589395d6
NB
1309 /* There is a nasty race with 'mdadm --monitor'.
1310 * If it opens this device before we close it,
1311 * it gets an incomplete open on which IO
598f0d58
NB
1312 * doesn't work and the capacity is
1313 * wrong.
589395d6
NB
1314 * If we reopen (to check for layered devices)
1315 * before --monitor closes, we loose.
1316 *
1317 * So: wait upto 1 second for there to be
1318 * a non-zero capacity.
1319 */
1320 while (usecs < 1000) {
1321 mdfd = open(mddev, O_RDONLY);
1322 if (mdfd >= 0) {
beae1dfe
NB
1323 unsigned long long size;
1324 if (get_dev_size(mdfd, NULL, &size) &&
589395d6
NB
1325 size > 0)
1326 break;
1327 close(mdfd);
1328 }
1329 usleep(usecs);
1330 usecs <<= 1;
1331 }
1332 }
d7f7ebb7 1333 free(devices);
64c4757e 1334 return 0;
82b27616 1335 }
682c7051 1336 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
64c4757e 1337 mddev, strerror(errno));
583315d9 1338
98dbd966
DW
1339 if (!enough(content->array.level, content->array.raid_disks,
1340 content->array.layout, 1, avail, okcnt))
583315d9
NB
1341 fprintf(stderr, Name ": Not enough devices to "
1342 "start the array.\n");
98dbd966
DW
1343 else if (!enough(content->array.level,
1344 content->array.raid_disks,
1345 content->array.layout, clean,
583315d9
NB
1346 avail, okcnt))
1347 fprintf(stderr, Name ": Not enough devices to "
1348 "start the array while not clean "
1349 "- consider --force.\n");
1350
7f91af49 1351 if (auto_assem)
1c203a4b 1352 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1353 close(mdfd);
d7f7ebb7 1354 free(devices);
64c4757e
NB
1355 return 1;
1356 }
82b27616 1357 if (runstop == -1) {
b8a8ccf9 1358 fprintf(stderr, Name ": %s assembled from %d drive%s",
52826846 1359 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1360 if (okcnt != (unsigned)content->array.raid_disks)
98dbd966 1361 fprintf(stderr, " (out of %d)", content->array.raid_disks);
b8a8ccf9 1362 fprintf(stderr, ", but not started.\n");
7f91af49 1363 close(mdfd);
d7f7ebb7 1364 free(devices);
64c4757e 1365 return 0;
82b27616 1366 }
4855f95c 1367 if (verbose >= -1) {
dab6685f 1368 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1ff98339
N
1369 if (rebuilding_cnt)
1370 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
dab6685f
NB
1371 if (sparecnt)
1372 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
98dbd966
DW
1373 if (!enough(content->array.level, content->array.raid_disks,
1374 content->array.layout, 1, avail, okcnt))
dab6685f 1375 fprintf(stderr, " - not enough to start the array.\n");
98dbd966
DW
1376 else if (!enough(content->array.level,
1377 content->array.raid_disks,
1378 content->array.layout, clean,
583315d9
NB
1379 avail, okcnt))
1380 fprintf(stderr, " - not enough to start the "
1381 "array while not clean - consider "
1382 "--force.\n");
dab6685f 1383 else {
f21e18ca 1384 if (req_cnt == (unsigned)content->array.raid_disks)
dab6685f
NB
1385 fprintf(stderr, " - need all %d to start it", req_cnt);
1386 else
98dbd966 1387 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
dab6685f
NB
1388 fprintf(stderr, " (use --run to insist).\n");
1389 }
aa88f531 1390 }
7f91af49 1391 if (auto_assem)
1c203a4b 1392 ioctl(mdfd, STOP_ARRAY, NULL);
56a8da69 1393 close(mdfd);
d7f7ebb7 1394 free(devices);
64c4757e 1395 return 1;
82b27616 1396 } else {
52826846
NB
1397 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1398 * been updated to point to the current locations of devices.
1399 * so we can just start the array
82b27616 1400 */
cd29a5c8 1401 unsigned long dev;
213ee40b
NB
1402 dev = makedev(devices[chosen_drive].i.disk.major,
1403 devices[chosen_drive].i.disk.minor);
82b27616
NB
1404 if (ioctl(mdfd, START_ARRAY, dev)) {
1405 fprintf(stderr, Name ": Cannot start array: %s\n",
1406 strerror(errno));
1407 }
aba69144 1408
64c4757e 1409 }
7f91af49 1410 close(mdfd);
d7f7ebb7 1411 free(devices);
e0d19036 1412 return 0;
64c4757e 1413}
6234c63c
DW
1414
1415#ifndef MDASSEMBLE
1416int assemble_container_content(struct supertype *st, int mdfd,
1417 struct mdinfo *content, int runstop,
1418 char *chosen_name, int verbose)
1419{
1420 struct mdinfo *dev, *sra;
1421 int working = 0, preexist = 0;
1422 struct map_ent *map = NULL;
1423
1424 sysfs_init(content, mdfd, 0);
1425
1426 sra = sysfs_read(mdfd, 0, GET_VERSION);
1427 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
4408ee76
N
1428 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1429 close(mdfd);
6234c63c 1430 return 1;
4408ee76 1431 }
6234c63c
DW
1432 if (sra)
1433 sysfs_free(sra);
1434
1435 for (dev = content->devs; dev; dev = dev->next)
462906cd 1436 if (sysfs_add_disk(content, dev, 1) == 0)
6234c63c
DW
1437 working++;
1438 else if (errno == EEXIST)
1439 preexist++;
4408ee76
N
1440 if (working == 0) {
1441 close(mdfd);
7cb2aa33 1442 return 1;/* Nothing new, don't try to start */
8b4e5ea9
N
1443 }
1444
1445 map_update(&map, fd2devnum(mdfd),
1446 content->text_version,
1447 content->uuid, chosen_name);
1448
1449 if (runstop > 0 ||
6234c63c 1450 (working + preexist) >= content->array.working_disks) {
bb50e5d3 1451 int err;
a714580e 1452
6234c63c
DW
1453 switch(content->array.level) {
1454 case LEVEL_LINEAR:
1455 case LEVEL_MULTIPATH:
1456 case 0:
bb50e5d3
N
1457 err = sysfs_set_str(content, NULL, "array_state",
1458 "active");
6234c63c
DW
1459 break;
1460 default:
bb50e5d3 1461 err = sysfs_set_str(content, NULL, "array_state",
6234c63c
DW
1462 "readonly");
1463 /* start mdmon if needed. */
bb50e5d3
N
1464 if (!err) {
1465 if (!mdmon_running(st->container_dev))
1466 start_mdmon(st->container_dev);
1467 ping_monitor(devnum2devname(st->container_dev));
1468 }
6234c63c
DW
1469 break;
1470 }
bb50e5d3
N
1471 if (!err)
1472 sysfs_set_safemode(content, content->safe_mode_delay);
6234c63c 1473 if (verbose >= 0) {
bb50e5d3
N
1474 if (err)
1475 fprintf(stderr, Name
1476 ": array %s now has %d devices",
1477 chosen_name, working + preexist);
1478 else
1479 fprintf(stderr, Name
1480 ": Started %s with %d devices",
1481 chosen_name, working + preexist);
6234c63c
DW
1482 if (preexist)
1483 fprintf(stderr, " (%d new)", working);
1484 fprintf(stderr, "\n");
1485 }
bb50e5d3 1486 if (!err)
a7c6e3fb 1487 wait_for(chosen_name, mdfd);
4408ee76 1488 close(mdfd);
7cb2aa33 1489 return 0;
6234c63c 1490 /* FIXME should have an O_EXCL and wait for read-auto */
7cb2aa33 1491 } else {
6234c63c
DW
1492 if (verbose >= 0)
1493 fprintf(stderr, Name
1494 ": %s assembled with %d devices but "
1495 "not started\n",
1496 chosen_name, working);
4408ee76 1497 close(mdfd);
7cb2aa33
N
1498 return 1;
1499 }
6234c63c
DW
1500}
1501#endif
1502