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