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