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