]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
Introduce pr_err for printing error messages.
[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 */
d7f7ebb7 730 devices = malloc(num_devs * sizeof(*devices));
02e7c5b7 731 devmap = calloc(num_devs * content->array.raid_disks, 1);
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;
56eedc1a 869 int *newbest = malloc(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 */
98dbd966
DW
934 avail = malloc(content->array.raid_disks);
935 memset(avail, 0, content->array.raid_disks);
64c4757e 936 okcnt = 0;
52826846 937 sparecnt=0;
1ff98339 938 rebuilding_cnt=0;
f21e18ca 939 for (i=0; i< bestcnt; i++) {
64c4757e 940 int j = best[i];
ee04451c
NB
941 int event_margin = 1; /* always allow a difference of '1'
942 * like the kernel does
943 */
64c4757e 944 if (j < 0) continue;
d013a55e
NB
945 /* note: we ignore error flags in multipath arrays
946 * as they don't make sense
947 */
df0d4ea0 948 if (content->array.level != LEVEL_MULTIPATH)
f22385f9 949 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
213ee40b 950 if (!(devices[j].i.disk.state
ed7fc6b4
AC
951 & (1<<MD_DISK_FAULTY))) {
952 devices[j].uptodate = 1;
aa88f531 953 sparecnt++;
ed7fc6b4 954 }
d013a55e 955 continue;
aa88f531 956 }
de5a472e 957 /* If this device thinks that 'most_recent' has failed, then
02e7c5b7
N
958 * we must reject this device.
959 */
960 if (j != most_recent &&
961 content->array.raid_disks > 0 &&
962 devices[most_recent].i.disk.raid_disk >= 0 &&
963 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
964 if (verbose > -1)
e7b84f9d 965 pr_err("ignoring %s as it reports %s as failed\n",
02e7c5b7
N
966 devices[j].devname, devices[most_recent].devname);
967 best[i] = -1;
968 continue;
969 }
213ee40b
NB
970 if (devices[j].i.events+event_margin >=
971 devices[most_recent].i.events) {
64c4757e 972 devices[j].uptodate = 1;
1ff98339 973 if (i < content->array.raid_disks) {
dcc4210f
DW
974 if (devices[j].i.recovery_start == MaxSector ||
975 (content->reshape_active &&
b720636a
N
976 ((i >= content->array.raid_disks - content->delta_disks) ||
977 (i >= content->array.raid_disks - content->delta_disks - 1
978 && content->array.level == 4)))) {
1ff98339
N
979 okcnt++;
980 avail[i]=1;
981 } else
982 rebuilding_cnt++;
265e0f17 983 } else
52826846 984 sparecnt++;
64c4757e
NB
985 }
986 }
02e7c5b7 987 free(devmap);
da8fe5aa
N
988 while (force &&
989 (!enough(content->array.level, content->array.raid_disks,
990 content->array.layout, 1,
991 avail)
992 ||
993 (content->reshape_active && content->delta_disks > 0 &&
994 !enough(content->array.level, (content->array.raid_disks
995 - content->delta_disks),
996 content->new_layout, 1,
997 avail)
998 ))) {
64c4757e
NB
999 /* Choose the newest best drive which is
1000 * not up-to-date, update the superblock
1001 * and add it.
1002 */
52826846 1003 int fd;
3da92f27 1004 struct supertype *tst;
f21e18ca 1005 unsigned long long current_events;
cd29a5c8 1006 chosen_drive = -1;
f21e18ca 1007 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
52826846
NB
1008 int j = best[i];
1009 if (j>=0 &&
1010 !devices[j].uptodate &&
921d9e16 1011 devices[j].i.recovery_start == MaxSector &&
52826846 1012 (chosen_drive < 0 ||
213ee40b
NB
1013 devices[j].i.events
1014 > devices[chosen_drive].i.events))
52826846
NB
1015 chosen_drive = j;
1016 }
1017 if (chosen_drive < 0)
1018 break;
213ee40b 1019 current_events = devices[chosen_drive].i.events;
c5a6f9a6 1020 add_another:
dab6685f 1021 if (verbose >= 0)
e7b84f9d 1022 pr_err("forcing event count in %s(%d) from %d upto %d\n",
213ee40b
NB
1023 devices[chosen_drive].devname,
1024 devices[chosen_drive].i.disk.raid_disk,
1025 (int)(devices[chosen_drive].i.events),
1026 (int)(devices[most_recent].i.events));
8b0dabea 1027 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 1028 if (fd < 0) {
e7b84f9d 1029 pr_err("Couldn't open %s for write - not updating\n",
52826846 1030 devices[chosen_drive].devname);
213ee40b 1031 devices[chosen_drive].i.events = 0;
52826846
NB
1032 continue;
1033 }
3da92f27 1034 tst = dup_super(st);
60b435db 1035 if (tst->ss->load_super(tst,fd, NULL)) {
52826846 1036 close(fd);
e7b84f9d 1037 pr_err("RAID superblock disappeared from %s - not updating.\n",
52826846 1038 devices[chosen_drive].devname);
213ee40b 1039 devices[chosen_drive].i.events = 0;
52826846
NB
1040 continue;
1041 }
98dbd966
DW
1042 content->events = devices[most_recent].i.events;
1043 tst->ss->update_super(tst, content, "force-one",
67a8c82d
NB
1044 devices[chosen_drive].devname, verbose,
1045 0, NULL);
4b1ac34b 1046
3da92f27 1047 if (tst->ss->store_super(tst, fd)) {
52826846 1048 close(fd);
e7b84f9d 1049 pr_err("Could not re-write superblock on %s\n",
52826846 1050 devices[chosen_drive].devname);
213ee40b 1051 devices[chosen_drive].i.events = 0;
3da92f27 1052 tst->ss->free_super(tst);
52826846
NB
1053 continue;
1054 }
1055 close(fd);
213ee40b 1056 devices[chosen_drive].i.events = devices[most_recent].i.events;
52826846 1057 devices[chosen_drive].uptodate = 1;
265e0f17 1058 avail[chosen_drive] = 1;
52826846 1059 okcnt++;
3da92f27 1060 tst->ss->free_super(tst);
c5a6f9a6
NB
1061
1062 /* If there are any other drives of the same vintage,
1063 * add them in as well. We can't lose and we might gain
1064 */
f21e18ca 1065 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
c5a6f9a6
NB
1066 int j = best[i];
1067 if (j >= 0 &&
1068 !devices[j].uptodate &&
135a31f5 1069 devices[j].i.recovery_start == MaxSector &&
213ee40b 1070 devices[j].i.events == current_events) {
c5a6f9a6
NB
1071 chosen_drive = j;
1072 goto add_another;
1073 }
1074 }
64c4757e 1075 }
52826846
NB
1076
1077 /* Now we want to look at the superblock which the kernel will base things on
1078 * and compare the devices that we think are working with the devices that the
1079 * superblock thinks are working.
1080 * If there are differences and --force is given, then update this chosen
1081 * superblock.
1082 */
cd29a5c8 1083 chosen_drive = -1;
3da92f27 1084 st->ss->free_super(st);
56eedc1a 1085 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
52826846
NB
1086 int j = best[i];
1087 int fd;
4b1ac34b 1088
52826846
NB
1089 if (j<0)
1090 continue;
1091 if (!devices[j].uptodate)
1092 continue;
a28232b8
N
1093 if (devices[j].i.events < devices[most_recent].i.events)
1094 continue;
52826846 1095 chosen_drive = j;
8b0dabea 1096 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
e7b84f9d 1097 pr_err("Cannot open %s: %s\n",
52826846 1098 devices[j].devname, strerror(errno));
7f91af49 1099 close(mdfd);
d7f7ebb7 1100 free(devices);
52826846
NB
1101 return 1;
1102 }
3da92f27 1103 if (st->ss->load_super(st,fd, NULL)) {
52826846 1104 close(fd);
e7b84f9d 1105 pr_err("RAID superblock has disappeared from %s\n",
52826846 1106 devices[j].devname);
7f91af49 1107 close(mdfd);
d7f7ebb7 1108 free(devices);
52826846
NB
1109 return 1;
1110 }
1111 close(fd);
1112 }
3da92f27 1113 if (st->sb == NULL) {
e7b84f9d 1114 pr_err("No suitable drives found for %s\n", mddev);
7f91af49 1115 close(mdfd);
d7f7ebb7 1116 free(devices);
4b1ac34b
NB
1117 return 1;
1118 }
a5d85af7 1119 st->ss->getinfo_super(st, content, NULL);
f35f2525 1120#ifndef MDASSEMBLE
98dbd966 1121 sysfs_init(content, mdfd, 0);
f35f2525 1122#endif
56eedc1a 1123 for (i=0; i<bestcnt; i++) {
52826846 1124 int j = best[i];
98c6faba 1125 unsigned int desired_state;
11a3e71d 1126
98dbd966 1127 if (i < content->array.raid_disks)
11a3e71d
NB
1128 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1129 else
1130 desired_state = 0;
1131
52826846
NB
1132 if (j<0)
1133 continue;
1134 if (!devices[j].uptodate)
1135 continue;
4b1ac34b 1136
213ee40b 1137 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1138 if (!(devices[j].i.array.state & 1))
1139 clean = 0;
213ee40b
NB
1140
1141 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
68c7d6d7 1142 verbose, 0, NULL)) {
52826846 1143 if (force) {
dab6685f 1144 if (verbose >= 0)
e7b84f9d 1145 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
dab6685f 1146 j, mddev, devices[j].devname);
4b1ac34b 1147 change = 1;
52826846 1148 } else {
dab6685f 1149 if (verbose >= -1)
e7b84f9d 1150 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
dab6685f 1151 i, mddev, devices[j].devname);
52826846
NB
1152 }
1153 }
4b1ac34b 1154#if 0
213ee40b 1155 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
e7b84f9d 1156 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
52826846
NB
1157 i, mddev);
1158 }
4b1ac34b 1159#endif
52826846 1160 }
c5a6f9a6 1161 if (force && !clean &&
98dbd966
DW
1162 !enough(content->array.level, content->array.raid_disks,
1163 content->array.layout, clean,
de5a472e 1164 avail)) {
98dbd966 1165 change += st->ss->update_super(st, content, "force-array",
67a8c82d
NB
1166 devices[chosen_drive].devname, verbose,
1167 0, NULL);
583315d9 1168 clean = 1;
d013a55e 1169 }
52826846 1170
4b1ac34b 1171 if (change) {
52826846 1172 int fd;
8b0dabea 1173 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
52826846 1174 if (fd < 0) {
e7b84f9d 1175 pr_err("Could not open %s for write - cannot Assemble array.\n",
52826846 1176 devices[chosen_drive].devname);
7f91af49 1177 close(mdfd);
d7f7ebb7 1178 free(devices);
52826846
NB
1179 return 1;
1180 }
3da92f27 1181 if (st->ss->store_super(st, fd)) {
52826846 1182 close(fd);
e7b84f9d 1183 pr_err("Could not re-write superblock on %s\n",
52826846 1184 devices[chosen_drive].devname);
7f91af49 1185 close(mdfd);
d7f7ebb7 1186 free(devices);
52826846
NB
1187 return 1;
1188 }
a28232b8 1189 if (verbose >= 0)
e7b84f9d 1190 pr_err("Marking array %s as 'clean'\n",
a28232b8 1191 mddev);
52826846 1192 close(fd);
52826846
NB
1193 }
1194
353632d9
NB
1195 /* If we are in the middle of a reshape we may need to restore saved data
1196 * that was moved aside due to the reshape overwriting live data
1197 * The code of doing this lives in Grow.c
1198 */
39c5a909 1199#ifndef MDASSEMBLE
98dbd966 1200 if (content->reshape_active) {
353632d9
NB
1201 int err = 0;
1202 int *fdlist = malloc(sizeof(int)* bestcnt);
cd77ac4e 1203 if (verbose > 0)
e7b84f9d 1204 pr_err(":%s has an active reshape - checking "
ea0ebe96
N
1205 "if critical section needs to be restored\n",
1206 chosen_name);
353632d9
NB
1207 for (i=0; i<bestcnt; i++) {
1208 int j = best[i];
1209 if (j >= 0) {
1210 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
1211 if (fdlist[i] < 0) {
e7b84f9d 1212 pr_err("Could not open %s for write - cannot Assemble array.\n",
353632d9
NB
1213 devices[j].devname);
1214 err = 1;
1215 break;
1216 }
1217 } else
1218 fdlist[i] = -1;
1219 }
87f26d14 1220 if (!err) {
ba53ea59
AK
1221 if (st->ss->external && st->ss->recover_backup)
1222 err = st->ss->recover_backup(st, content);
1223 else
1224 err = Grow_restart(st, content, fdlist, bestcnt,
1225 backup_file, verbose > 0);
87f26d14
N
1226 if (err && invalid_backup) {
1227 if (verbose > 0)
e7b84f9d 1228 pr_err("continuing"
87f26d14
N
1229 " without restoring backup\n");
1230 err = 0;
1231 }
1232 }
353632d9
NB
1233 while (i>0) {
1234 i--;
1235 if (fdlist[i]>=0) close(fdlist[i]);
1236 }
1237 if (err) {
e7b84f9d 1238 pr_err("Failed to restore critical section for reshape, sorry.\n");
e9e43ec3 1239 if (backup_file == NULL)
e7b84f9d 1240 cont_err("Possibly you needed to specify the --backup-file\n");
7f91af49 1241 close(mdfd);
d7f7ebb7 1242 free(devices);
353632d9
NB
1243 return err;
1244 }
1245 }
39c5a909 1246#endif
aa88f531
NB
1247 /* count number of in-sync devices according to the superblock.
1248 * We must have this number to start the array without -s or -R
1249 */
98dbd966 1250 req_cnt = content->array.working_disks;
aa88f531 1251
64c4757e
NB
1252 /* Almost ready to actually *do* something */
1253 if (!old_linux) {
fbf8a0b7 1254 int rv;
a19c88b8 1255
a04d5763
N
1256 /* First, fill in the map, so that udev can find our name
1257 * as soon as we become active.
1258 */
98dbd966
DW
1259 map_update(NULL, fd2devnum(mdfd), content->text_version,
1260 content->uuid, chosen_name);
a04d5763 1261
98dbd966 1262 rv = set_array_info(mdfd, st, content);
fbf8a0b7 1263 if (rv) {
e7b84f9d 1264 pr_err("failed to set array info for %s: %s\n",
64c4757e 1265 mddev, strerror(errno));
24af7a87 1266 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1267 close(mdfd);
d7f7ebb7 1268 free(devices);
64c4757e
NB
1269 return 1;
1270 }
11484a63 1271 if (ident->bitmap_fd >= 0) {
c82f047c 1272 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
e7b84f9d 1273 pr_err("SET_BITMAP_FILE failed.\n");
24af7a87 1274 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1275 close(mdfd);
d7f7ebb7 1276 free(devices);
c82f047c
NB
1277 return 1;
1278 }
7ef02d01
NB
1279 } else if (ident->bitmap_file) {
1280 /* From config file */
1281 int bmfd = open(ident->bitmap_file, O_RDWR);
1282 if (bmfd < 0) {
e7b84f9d 1283 pr_err("Could not open bitmap file %s\n",
7ef02d01 1284 ident->bitmap_file);
24af7a87 1285 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1286 close(mdfd);
d7f7ebb7 1287 free(devices);
7ef02d01
NB
1288 return 1;
1289 }
1290 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
e7b84f9d 1291 pr_err("Failed to set bitmapfile for %s\n", mddev);
7ef02d01 1292 close(bmfd);
24af7a87 1293 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1294 close(mdfd);
d7f7ebb7 1295 free(devices);
7ef02d01
NB
1296 return 1;
1297 }
1298 close(bmfd);
c82f047c 1299 }
7ef02d01 1300
52826846 1301 /* First, add the raid disks, but add the chosen one last */
56eedc1a 1302 for (i=0; i<= bestcnt; i++) {
52826846 1303 int j;
56eedc1a 1304 if (i < bestcnt) {
52826846
NB
1305 j = best[i];
1306 if (j == chosen_drive)
1307 continue;
1308 } else
1309 j = chosen_drive;
1310
56eedc1a 1311 if (j >= 0 /* && devices[j].uptodate */) {
484ae54d
N
1312 int dfd = dev_open(devices[j].devname,
1313 O_RDWR|O_EXCL);
1314 if (dfd >= 0) {
1315 remove_partitions(dfd);
1316 close(dfd);
1317 }
98dbd966 1318 rv = add_disk(mdfd, st, content, &devices[j].i);
7801ac20 1319
a19c88b8 1320 if (rv) {
e7b84f9d
N
1321 pr_err("failed to add "
1322 "%s to %s: %s\n",
1323 devices[j].devname,
1324 mddev,
1325 strerror(errno));
98dbd966 1326 if (i < content->array.raid_disks
213ee40b 1327 || i == bestcnt)
52826846
NB
1328 okcnt--;
1329 else
1330 sparecnt--;
dab6685f 1331 } else if (verbose > 0)
e7b84f9d
N
1332 pr_err("added %s to %s as %d%s\n",
1333 devices[j].devname, mddev,
1334 devices[j].i.disk.raid_disk,
1335 devices[j].uptodate?"":
1336 " (possibly out of date)");
98dbd966 1337 } else if (verbose > 0 && i < content->array.raid_disks)
e7b84f9d 1338 pr_err("no uptodate device for "
213ee40b 1339 "slot %d of %s\n",
64c4757e
NB
1340 i, mddev);
1341 }
aba69144 1342
98dbd966 1343 if (content->array.level == LEVEL_CONTAINER) {
598f0d58 1344 if (verbose >= 0) {
e7b84f9d 1345 pr_err("Container %s has been "
598f0d58 1346 "assembled with %d drive%s",
57ed8c91 1347 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
f21e18ca 1348 if (okcnt < (unsigned)content->array.raid_disks)
598f0d58 1349 fprintf(stderr, " (out of %d)",
98dbd966 1350 content->array.raid_disks);
598f0d58
NB
1351 fprintf(stderr, "\n");
1352 }
ac597b1c 1353 st->ss->free_super(st);
98dbd966 1354 sysfs_uevent(content, "change");
a7c6e3fb 1355 wait_for(chosen_name, mdfd);
7f91af49 1356 close(mdfd);
d7f7ebb7 1357 free(devices);
598f0d58
NB
1358 return 0;
1359 }
1360
64c4757e 1361 if (runstop == 1 ||
b8a8ccf9 1362 (runstop <= 0 &&
98dbd966 1363 ( enough(content->array.level, content->array.raid_disks,
de5a472e 1364 content->array.layout, clean, avail) &&
1ff98339 1365 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
aa88f531 1366 ))) {
e9e43ec3
N
1367 /* This array is good-to-go.
1368 * If a reshape is in progress then we might need to
1369 * continue monitoring it. In that case we start
1370 * it read-only and let the grow code make it writable.
1371 */
1372 int rv;
eb3929a4 1373#ifndef MDASSEMBLE
e9e43ec3 1374 if (content->reshape_active &&
3bd58dc6
AK
1375 content->delta_disks <= 0) {
1376 rv = sysfs_set_str(content, NULL,
1377 "array_state", "readonly");
1378 if (rv == 0)
1379 rv = Grow_continue(mdfd, st, content,
1380 backup_file,
1381 freeze_reshape);
1382 } else
eb3929a4 1383#endif
e9e43ec3
N
1384 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1385 if (rv == 0) {
dab6685f 1386 if (verbose >= 0) {
e7b84f9d 1387 pr_err("%s has been started with %d drive%s",
dab6685f 1388 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1389 if (okcnt < (unsigned)content->array.raid_disks)
98dbd966 1390 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1ff98339
N
1391 if (rebuilding_cnt)
1392 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
dab6685f
NB
1393 if (sparecnt)
1394 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1395 fprintf(stderr, ".\n");
1396 }
8a659c33
N
1397 if (content->reshape_active &&
1398 content->array.level >= 4 &&
1399 content->array.level <= 6) {
acee8e89
N
1400 /* might need to increase the size
1401 * of the stripe cache - default is 256
1402 */
8a659c33 1403 if (256 < 4 * (content->array.chunk_size/4096)) {
acee8e89
N
1404 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1405 if (sra)
1406 sysfs_set_num(sra, NULL,
1407 "stripe_cache_size",
8a659c33 1408 (4 * content->array.chunk_size / 4096) + 1);
83366b33 1409 sysfs_free(sra);
acee8e89
N
1410 }
1411 }
7e83544b
N
1412 if (okcnt < (unsigned)content->array.raid_disks) {
1413 /* If any devices did not get added
1414 * because the kernel rejected them based
1415 * on event count, try adding them
1416 * again providing the action policy is
1417 * 're-add' or greater. The bitmap
1418 * might allow them to be included, or
1419 * they will become spares.
1420 */
b787bec6 1421 for (i = 0; i < bestcnt; i++) {
7e83544b
N
1422 int j = best[i];
1423 if (j >= 0 && !devices[j].uptodate) {
1424 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1425 continue;
1426 rv = add_disk(mdfd, st, content,
1427 &devices[j].i);
1428 if (rv == 0 && verbose >= 0)
e7b84f9d
N
1429 pr_err("%s has been re-added.\n",
1430 devices[j].devname);
7e83544b
N
1431 }
1432 }
1433 }
a7c6e3fb 1434 wait_for(mddev, mdfd);
7f91af49
N
1435 close(mdfd);
1436 if (auto_assem) {
589395d6 1437 int usecs = 1;
589395d6
NB
1438 /* There is a nasty race with 'mdadm --monitor'.
1439 * If it opens this device before we close it,
1440 * it gets an incomplete open on which IO
598f0d58
NB
1441 * doesn't work and the capacity is
1442 * wrong.
589395d6
NB
1443 * If we reopen (to check for layered devices)
1444 * before --monitor closes, we loose.
1445 *
1446 * So: wait upto 1 second for there to be
1447 * a non-zero capacity.
1448 */
1449 while (usecs < 1000) {
1450 mdfd = open(mddev, O_RDONLY);
1451 if (mdfd >= 0) {
beae1dfe
NB
1452 unsigned long long size;
1453 if (get_dev_size(mdfd, NULL, &size) &&
589395d6
NB
1454 size > 0)
1455 break;
1456 close(mdfd);
1457 }
1458 usleep(usecs);
1459 usecs <<= 1;
1460 }
1461 }
d7f7ebb7 1462 free(devices);
64c4757e 1463 return 0;
82b27616 1464 }
e7b84f9d 1465 pr_err("failed to RUN_ARRAY %s: %s\n",
64c4757e 1466 mddev, strerror(errno));
583315d9 1467
98dbd966 1468 if (!enough(content->array.level, content->array.raid_disks,
de5a472e 1469 content->array.layout, 1, avail))
e7b84f9d 1470 pr_err("Not enough devices to "
583315d9 1471 "start the array.\n");
98dbd966
DW
1472 else if (!enough(content->array.level,
1473 content->array.raid_disks,
1474 content->array.layout, clean,
de5a472e 1475 avail))
e7b84f9d 1476 pr_err("Not enough devices to "
583315d9
NB
1477 "start the array while not clean "
1478 "- consider --force.\n");
1479
7f91af49 1480 if (auto_assem)
1c203a4b 1481 ioctl(mdfd, STOP_ARRAY, NULL);
7f91af49 1482 close(mdfd);
d7f7ebb7 1483 free(devices);
64c4757e
NB
1484 return 1;
1485 }
82b27616 1486 if (runstop == -1) {
e7b84f9d
N
1487 pr_err("%s assembled from %d drive%s",
1488 mddev, okcnt, okcnt==1?"":"s");
f21e18ca 1489 if (okcnt != (unsigned)content->array.raid_disks)
98dbd966 1490 fprintf(stderr, " (out of %d)", content->array.raid_disks);
b8a8ccf9 1491 fprintf(stderr, ", but not started.\n");
7f91af49 1492 close(mdfd);
d7f7ebb7 1493 free(devices);
64c4757e 1494 return 0;
82b27616 1495 }
4855f95c 1496 if (verbose >= -1) {
e7b84f9d 1497 pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1ff98339
N
1498 if (rebuilding_cnt)
1499 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
dab6685f
NB
1500 if (sparecnt)
1501 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
98dbd966 1502 if (!enough(content->array.level, content->array.raid_disks,
de5a472e 1503 content->array.layout, 1, avail))
dab6685f 1504 fprintf(stderr, " - not enough to start the array.\n");
98dbd966
DW
1505 else if (!enough(content->array.level,
1506 content->array.raid_disks,
1507 content->array.layout, clean,
de5a472e 1508 avail))
583315d9
NB
1509 fprintf(stderr, " - not enough to start the "
1510 "array while not clean - consider "
1511 "--force.\n");
dab6685f 1512 else {
f21e18ca 1513 if (req_cnt == (unsigned)content->array.raid_disks)
dab6685f
NB
1514 fprintf(stderr, " - need all %d to start it", req_cnt);
1515 else
98dbd966 1516 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
dab6685f
NB
1517 fprintf(stderr, " (use --run to insist).\n");
1518 }
aa88f531 1519 }
7f91af49 1520 if (auto_assem)
1c203a4b 1521 ioctl(mdfd, STOP_ARRAY, NULL);
56a8da69 1522 close(mdfd);
d7f7ebb7 1523 free(devices);
64c4757e 1524 return 1;
82b27616 1525 } else {
52826846
NB
1526 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1527 * been updated to point to the current locations of devices.
1528 * so we can just start the array
82b27616 1529 */
cd29a5c8 1530 unsigned long dev;
213ee40b
NB
1531 dev = makedev(devices[chosen_drive].i.disk.major,
1532 devices[chosen_drive].i.disk.minor);
82b27616 1533 if (ioctl(mdfd, START_ARRAY, dev)) {
e7b84f9d 1534 pr_err("Cannot start array: %s\n",
82b27616
NB
1535 strerror(errno));
1536 }
aba69144 1537
64c4757e 1538 }
7f91af49 1539 close(mdfd);
d7f7ebb7 1540 free(devices);
e0d19036 1541 return 0;
64c4757e 1542}
6234c63c
DW
1543
1544#ifndef MDASSEMBLE
1545int assemble_container_content(struct supertype *st, int mdfd,
1546 struct mdinfo *content, int runstop,
49680258 1547 char *chosen_name, int verbose,
b76b30e0 1548 char *backup_file, int freeze_reshape)
6234c63c
DW
1549{
1550 struct mdinfo *dev, *sra;
1551 int working = 0, preexist = 0;
882029c8 1552 int expansion = 0;
6234c63c 1553 struct map_ent *map = NULL;
7af03341 1554 int old_raid_disks;
4aecb54a 1555 int start_reshape;
6234c63c
DW
1556
1557 sysfs_init(content, mdfd, 0);
1558
1559 sra = sysfs_read(mdfd, 0, GET_VERSION);
1560 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
22472ee1
JS
1561 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1562 if (sra)
1563 sysfs_free(sra);
6234c63c 1564 return 1;
22472ee1 1565 }
588bebfc 1566
4aecb54a
AK
1567 /* There are two types of reshape: container wide or sub-array specific
1568 * Check if metadata requests blocking container wide reshapes
1569 */
1570 start_reshape = (content->reshape_active &&
1571 !((content->reshape_active == CONTAINER_RESHAPE) &&
1572 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1573
1574 /* Block subarray here if it is under reshape now
1575 * Do not allow for any changes in this array
1576 */
1577 if (st->ss->external && content->recovery_blocked && start_reshape)
b8063f07
AK
1578 block_subarray(content);
1579
6234c63c
DW
1580 if (sra)
1581 sysfs_free(sra);
7af03341 1582 old_raid_disks = content->array.raid_disks - content->delta_disks;
6234c63c 1583 for (dev = content->devs; dev; dev = dev->next)
14032016 1584 if (sysfs_add_disk(content, dev, 1) == 0) {
7af03341 1585 if (dev->disk.raid_disk >= old_raid_disks &&
14032016
AK
1586 content->reshape_active)
1587 expansion++;
1588 else
1589 working++;
1590 } else if (errno == EEXIST)
6234c63c 1591 preexist++;
111e9fda 1592 if (working + expansion == 0)
7cb2aa33 1593 return 1;/* Nothing new, don't try to start */
588bebfc 1594
8b4e5ea9
N
1595 map_update(&map, fd2devnum(mdfd),
1596 content->text_version,
1597 content->uuid, chosen_name);
1598
1599 if (runstop > 0 ||
882029c8
AK
1600 (working + preexist + expansion) >=
1601 content->array.working_disks) {
bb50e5d3 1602 int err;
a714580e 1603
81219e70 1604 if (start_reshape) {
49680258 1605 int spare = content->array.raid_disks + expansion;
3f54bd62
AK
1606 if (restore_backup(st, content,
1607 working,
1608 spare, backup_file, verbose) == 1)
49680258 1609 return 1;
49680258 1610
3bd58dc6
AK
1611 err = sysfs_set_str(content, NULL,
1612 "array_state", "readonly");
1613 if (err)
1614 return 1;
1615
a93ada3b
AK
1616 if (st->ss->external) {
1617 if (!mdmon_running(st->container_dev))
1618 start_mdmon(st->container_dev);
1619 ping_monitor_by_id(st->container_dev);
7728e1c6
LD
1620 if (mdmon_running(st->container_dev) &&
1621 st->update_tail == NULL)
1622 st->update_tail = &st->updates;
a93ada3b
AK
1623 }
1624
b76b30e0
AK
1625 err = Grow_continue(mdfd, st, content, backup_file,
1626 freeze_reshape);
49680258 1627 } else switch(content->array.level) {
6234c63c
DW
1628 case LEVEL_LINEAR:
1629 case LEVEL_MULTIPATH:
1630 case 0:
bb50e5d3
N
1631 err = sysfs_set_str(content, NULL, "array_state",
1632 "active");
6234c63c
DW
1633 break;
1634 default:
bb50e5d3 1635 err = sysfs_set_str(content, NULL, "array_state",
6234c63c
DW
1636 "readonly");
1637 /* start mdmon if needed. */
bb50e5d3
N
1638 if (!err) {
1639 if (!mdmon_running(st->container_dev))
1640 start_mdmon(st->container_dev);
983fff45 1641 ping_monitor_by_id(st->container_dev);
bb50e5d3 1642 }
6234c63c
DW
1643 break;
1644 }
bb50e5d3
N
1645 if (!err)
1646 sysfs_set_safemode(content, content->safe_mode_delay);
4aecb54a
AK
1647
1648 /* Block subarray here if it is not reshaped now
1649 * It has be blocked a little later to allow mdmon to switch in
1650 * in to R/W state
1651 */
1652 if (st->ss->external && content->recovery_blocked &&
1653 !start_reshape)
1654 block_subarray(content);
1655
6234c63c 1656 if (verbose >= 0) {
bb50e5d3 1657 if (err)
e7b84f9d
N
1658 pr_err("array %s now has %d device%s",
1659 chosen_name, working + preexist,
1660 working + preexist == 1 ? "":"s");
bb50e5d3 1661 else
e7b84f9d
N
1662 pr_err("Started %s with %d device%s",
1663 chosen_name, working + preexist,
1664 working + preexist == 1 ? "":"s");
6234c63c
DW
1665 if (preexist)
1666 fprintf(stderr, " (%d new)", working);
882029c8
AK
1667 if (expansion)
1668 fprintf(stderr, " ( + %d for expansion)",
1669 expansion);
6234c63c
DW
1670 fprintf(stderr, "\n");
1671 }
bb50e5d3 1672 if (!err)
a7c6e3fb 1673 wait_for(chosen_name, mdfd);
588bebfc 1674 return err;
6234c63c 1675 /* FIXME should have an O_EXCL and wait for read-auto */
7cb2aa33 1676 } else {
88716263 1677 if (verbose >= 0) {
e7b84f9d
N
1678 pr_err("%s assembled with %d device%s",
1679 chosen_name, preexist + working,
1680 preexist + working == 1 ? "":"s");
88716263
N
1681 if (preexist)
1682 fprintf(stderr, " (%d new)", working);
1683 fprintf(stderr, " but not started\n");
1684 }
7cb2aa33
N
1685 return 1;
1686 }
6234c63c
DW
1687}
1688#endif
1689