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