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