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