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