]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Assemble.c
--update: add 'bbl' and 'no-bbl' to the list of known updates.
[thirdparty/mdadm.git] / Assemble.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
6f02172d 4 * Copyright (C) 2001-2013 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 */
b4924f46 51 struct mdstat_ent *mdstat = mdstat_read(0, 0);
2de8884f
DW
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 &&
babb8dd4 108 content->array.raid_disks != 0 && /* metadata doesn't know how many to expect */
08fb91a3
N
109 ident->raid_disks!= content->array.raid_disks) {
110 if (devname)
e7b84f9d
N
111 pr_err("%s requires wrong number of drives.\n",
112 devname);
08fb91a3
N
113 return 0;
114 }
805d30b2
N
115 if (ident->member && ident->member[0]) {
116 /* content->text_version must match */
117 char *s = strchr(content->text_version+1, '/');
118 if (s == NULL) {
119 if (devname)
e7b84f9d
N
120 pr_err("%s is not a container and one is required.\n",
121 devname);
805d30b2
N
122 return 0;
123 } else if (strcmp(ident->member, s+1) != 0) {
124 if (devname)
e7b84f9d
N
125 pr_err("skipping wrong member %s is %s\n",
126 content->text_version, devname);
805d30b2
N
127 return 0;
128 }
129 }
08fb91a3
N
130 return 1;
131}
08fb91a3 132
95425a89
N
133static int select_devices(struct mddev_dev *devlist,
134 struct mddev_ident *ident,
135 struct supertype **stp,
136 struct mdinfo **contentp,
137 struct context *c,
138 int inargv, int auto_assem)
64c4757e 139{
a655e550 140 struct mddev_dev *tmpdev;
95425a89
N
141 int num_devs;
142 struct supertype *st = *stp;
98dbd966 143 struct mdinfo *content = NULL;
8cf2eb96 144 int report_mismatch = ((inargv && c->verbose >= 0) || c->verbose > 0);
cbeeb0e5 145 struct domainlist *domains = NULL;
60e1bc1a 146
5787fa49
NB
147 tmpdev = devlist; num_devs = 0;
148 while (tmpdev) {
da6b5ca9
NB
149 if (tmpdev->used)
150 tmpdev->used = 2;
151 else
152 num_devs++;
0431869c 153 tmpdev->disposition = 0;
5787fa49
NB
154 tmpdev = tmpdev->next;
155 }
5787fa49 156
811e6cbe
NB
157 /* first walk the list of devices to find a consistent set
158 * that match the criterea, if that is possible.
c30e5369 159 * We flag the ones we like with 'used'.
811e6cbe
NB
160 */
161 for (tmpdev = devlist;
162 tmpdev;
5083d66b 163 tmpdev = tmpdev ? tmpdev->next : NULL) {
811e6cbe 164 char *devname = tmpdev->devname;
64c4757e
NB
165 int dfd;
166 struct stat stb;
518a60f3 167 struct supertype *tst;
4e8d9f0a 168 struct dev_policy *pol = NULL;
5083d66b 169 int found_container = 0;
52826846 170
95425a89
N
171 if (tmpdev->used > 1)
172 continue;
da6b5ca9 173
c39b2e63
N
174 if (ident->container) {
175 if (ident->container[0] == '/' &&
176 !same_dev(ident->container, devname)) {
177 if (report_mismatch)
178 pr_err("%s is not the container required (%s)\n",
179 devname, ident->container);
180 continue;
181 }
182 } else if (ident->devices &&
183 !match_oneof(ident->devices, devname)) {
184 /* Note that we ignore the "device=" identifier if a
185 * "container=" is given. Checking both is unnecessarily
186 * complicated.
187 */
8cf2eb96 188 if (report_mismatch)
e7b84f9d 189 pr_err("%s is not one of %s\n", devname, ident->devices);
52826846 190 continue;
56eedc1a 191 }
4b1ac34b 192
518a60f3
JS
193 tst = dup_super(st);
194
56d18859 195 dfd = dev_open(devname, O_RDONLY);
64c4757e 196 if (dfd < 0) {
8cf2eb96 197 if (report_mismatch)
e7b84f9d
N
198 pr_err("cannot open device %s: %s\n",
199 devname, strerror(errno));
da6b5ca9 200 tmpdev->used = 2;
52826846
NB
201 } else if (fstat(dfd, &stb)< 0) {
202 /* Impossible! */
e7b84f9d
N
203 pr_err("fstat failed for %s: %s\n",
204 devname, strerror(errno));
da6b5ca9 205 tmpdev->used = 2;
cd29a5c8 206 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
e7b84f9d
N
207 pr_err("%s is not a block device.\n",
208 devname);
da6b5ca9 209 tmpdev->used = 2;
5083d66b
N
210 } else if (must_be_container(dfd)) {
211 if (st) {
212 /* already found some components, this cannot
213 * be another one.
214 */
8cf2eb96 215 if (report_mismatch)
e7b84f9d
N
216 pr_err("%s is a container, but we are looking for components\n",
217 devname);
5083d66b 218 tmpdev->used = 2;
71204a50 219#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
5083d66b 220 } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
8cf2eb96 221 if (report_mismatch)
e7b84f9d
N
222 pr_err("not a recognisable container: %s\n",
223 devname);
5083d66b 224 tmpdev->used = 2;
71204a50 225#endif
417f346e
HCP
226 } else if (!tst->ss->load_container
227 || tst->ss->load_container(tst, dfd, NULL)) {
8cf2eb96 228 if (report_mismatch)
e7b84f9d
N
229 pr_err("no correct container type: %s\n",
230 devname);
5083d66b
N
231 tmpdev->used = 2;
232 } else if (auto_assem &&
4dd2df09 233 !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
4977146a 234 tst->ss->match_home(tst, c->homehost) == 1)) {
8cf2eb96 235 if (report_mismatch)
e7b84f9d
N
236 pr_err("%s has metadata type %s for which "
237 "auto-assembly is disabled\n",
238 devname, tst->ss->name);
5083d66b
N
239 tmpdev->used = 2;
240 } else
241 found_container = 1;
52826846 242 } else {
5083d66b 243 if (!tst && (tst = guess_super(dfd)) == NULL) {
8cf2eb96 244 if (report_mismatch)
e7b84f9d
N
245 pr_err("no recogniseable superblock on %s\n",
246 devname);
5083d66b
N
247 tmpdev->used = 2;
248 } else if (tst->ss->load_super(tst,dfd, NULL)) {
8cf2eb96 249 if (report_mismatch)
e7b84f9d
N
250 pr_err("no RAID superblock on %s\n",
251 devname);
5083d66b
N
252 tmpdev->used = 2;
253 } else if (tst->ss->compare_super == NULL) {
8cf2eb96 254 if (report_mismatch)
e7b84f9d
N
255 pr_err("Cannot assemble %s metadata on %s\n",
256 tst->ss->name, devname);
5083d66b
N
257 tmpdev->used = 2;
258 } else if (auto_assem && st == NULL &&
4dd2df09 259 !conf_test_metadata(tst->ss->name, (pol = devid_policy(stb.st_rdev)),
4977146a 260 tst->ss->match_home(tst, c->homehost) == 1)) {
8cf2eb96 261 if (report_mismatch)
e7b84f9d
N
262 pr_err("%s has metadata type %s for which "
263 "auto-assembly is disabled\n",
264 devname, tst->ss->name);
5083d66b
N
265 tmpdev->used = 2;
266 }
64c4757e 267 }
c06487ce 268 if (dfd >= 0) close(dfd);
d68ea4d7 269 if (tmpdev->used == 2) {
d4386799 270 if (auto_assem || !inargv)
d68ea4d7
N
271 /* Ignore unrecognised devices during auto-assembly */
272 goto loop;
273 if (ident->uuid_set || ident->name[0] ||
274 ident->super_minor != UnSet)
275 /* Ignore unrecognised device if looking for
276 * specific array */
277 goto loop;
d68ea4d7 278
e7b84f9d 279 pr_err("%s has no superblock - assembly aborted\n",
79f9f56d 280 devname);
d68ea4d7
N
281 if (st)
282 st->ss->free_super(st);
283 dev_policy_free(pol);
cbeeb0e5 284 domain_free(domains);
95425a89 285 return -1;
d68ea4d7 286 }
52826846 287
5083d66b 288 if (found_container) {
9008ed1c
N
289 /* tmpdev is a container. We need to be either
290 * looking for a member, or auto-assembling
291 */
56d18859
N
292 /* should be safe to try an exclusive open now, we
293 * have rejected anything that some other mdadm might
294 * be looking at
295 */
296 dfd = dev_open(devname, O_RDONLY | O_EXCL);
297 if (dfd < 0) {
8cf2eb96 298 if (report_mismatch)
e7b84f9d 299 pr_err("%s is busy - skipping\n", devname);
56d18859
N
300 goto loop;
301 }
302 close(dfd);
9008ed1c 303
c39b2e63
N
304 if (ident->container && ident->container[0] != '/') {
305 /* we have a uuid */
306 int uuid[4];
87477e6d 307
c39b2e63
N
308 content = *contentp;
309 tst->ss->getinfo_super(tst, content, NULL);
87477e6d 310
c39b2e63
N
311 if (!parse_uuid(ident->container, uuid) ||
312 !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) {
313 if (report_mismatch)
314 pr_err("%s has wrong UUID to be required container\n",
315 devname);
316 goto loop;
9008ed1c
N
317 }
318 }
319 /* It is worth looking inside this container.
320 */
4977146a 321 if (c->verbose > 0)
e7b84f9d 322 pr_err("looking in container %s\n",
79f9f56d 323 devname);
1415fe4b 324
88cef9b3
N
325 for (content = tst->ss->container_content(tst, NULL);
326 content;
327 content = content->next) {
1415fe4b 328
88cef9b3 329 if (!ident_matches(ident, content, tst,
4977146a 330 c->homehost, c->update,
8cf2eb96 331 report_mismatch ? devname : NULL))
88cef9b3
N
332 /* message already printed */;
333 else if (is_member_busy(content->text_version)) {
8cf2eb96 334 if (report_mismatch)
e7b84f9d 335 pr_err("member %s in %s is already assembled\n",
79f9f56d
N
336 content->text_version,
337 devname);
81219e70
LM
338 } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
339 /* do not assemble arrays with unsupported configurations */
e7b84f9d 340 pr_err("Cannot activate member %s in %s.\n",
79f9f56d
N
341 content->text_version,
342 devname);
88cef9b3
N
343 } else
344 break;
345 }
346 if (!content) {
f7ad3ccc 347 tmpdev->used = 2;
88cef9b3 348 goto loop; /* empty container */
fa031239 349 }
88cef9b3 350
9008ed1c 351 st = tst; tst = NULL;
4c1c3ad8 352 if (!auto_assem && inargv && tmpdev->next != NULL) {
e7b84f9d 353 pr_err("%s is a container, but is not "
79f9f56d
N
354 "only device given: confused and aborting\n",
355 devname);
9008ed1c 356 st->ss->free_super(st);
4e8d9f0a 357 dev_policy_free(pol);
cbeeb0e5 358 domain_free(domains);
95425a89 359 return -1;
9008ed1c 360 }
4977146a 361 if (c->verbose > 0)
e7b84f9d 362 pr_err("found match on member %s in %s\n",
79f9f56d 363 content->text_version, devname);
bac0d92e 364
5083d66b
N
365 /* make sure we finished the loop */
366 tmpdev = NULL;
bac0d92e 367 goto loop;
5083d66b 368 } else {
95425a89 369 content = *contentp;
5083d66b
N
370 tst->ss->getinfo_super(tst, content, NULL);
371
372 if (!ident_matches(ident, content, tst,
4977146a 373 c->homehost, c->update,
8cf2eb96 374 report_mismatch ? devname : NULL))
df37ffc0 375 goto loop;
66eb2c93 376
5141638c
N
377 if (auto_assem) {
378 /* Never auto-assemble things that conflict
379 * with mdadm.conf in some way
1d04e275 380 */
5141638c
N
381 struct mddev_ident *match;
382 int rv = 0;
383
384 match = conf_match(tst, content, devname,
385 report_mismatch ? c->verbose : -1,
386 &rv);
387 if (!match && rv == 2)
388 goto loop;
389 if (match && match->devname &&
390 strcasecmp(match->devname, "<ignore>") == 0) {
391 if (report_mismatch)
392 pr_err("%s is a member of an explicitly ignored array\n",
393 devname);
394 goto loop;
395 }
396 if (match && !ident_matches(match, content, tst,
397 c->homehost, c->update,
398 report_mismatch ? devname : NULL))
399 /* Array exists in mdadm.conf but some
400 * details don't match, so reject it
401 */
402 goto loop;
403 }
66eb2c93 404
56d18859
N
405 /* should be safe to try an exclusive open now, we
406 * have rejected anything that some other mdadm might
407 * be looking at
408 */
409 dfd = dev_open(devname, O_RDONLY | O_EXCL);
410 if (dfd < 0) {
8cf2eb96 411 if (report_mismatch)
e7b84f9d 412 pr_err("%s is busy - skipping\n", devname);
56d18859
N
413 goto loop;
414 }
415 close(dfd);
416
5083d66b
N
417 if (st == NULL)
418 st = dup_super(tst);
419 if (st->minor_version == -1)
420 st->minor_version = tst->minor_version;
ed7fc6b4
AC
421
422 if (memcmp(content->uuid, uuid_zero,
423 sizeof(int[4])) == 0) {
424 /* this is a floating spare. It cannot define
425 * an array unless there are no more arrays of
426 * this type to be found. It can be included
427 * in an array of this type though.
428 */
429 tmpdev->used = 3;
430 goto loop;
431 }
432
5083d66b
N
433 if (st->ss != tst->ss ||
434 st->minor_version != tst->minor_version ||
435 st->ss->compare_super(st, tst) != 0) {
436 /* Some mismatch. If exactly one array matches this host,
437 * we can resolve on that one.
438 * Or, if we are auto assembling, we just ignore the second
439 * for now.
440 */
441 if (auto_assem)
442 goto loop;
4977146a
N
443 if (c->homehost) {
444 int first = st->ss->match_home(st, c->homehost);
445 int last = tst->ss->match_home(tst, c->homehost);
5083d66b
N
446 if (first != last &&
447 (first == 1 || last == 1)) {
448 /* We can do something */
449 if (first) {/* just ignore this one */
8cf2eb96 450 if (report_mismatch)
e7b84f9d 451 pr_err("%s misses out due to wrong homehost\n",
79f9f56d 452 devname);
5083d66b
N
453 goto loop;
454 } else { /* reject all those sofar */
455 struct mddev_dev *td;
8cf2eb96 456 if (report_mismatch)
e7b84f9d 457 pr_err("%s overrides previous devices due to good homehost\n",
79f9f56d 458 devname);
5083d66b
N
459 for (td=devlist; td != tmpdev; td=td->next)
460 if (td->used == 1)
461 td->used = 0;
462 tmpdev->used = 1;
463 goto loop;
464 }
83b6208e
NB
465 }
466 }
e7b84f9d 467 pr_err("superblock on %s doesn't match others - assembly aborted\n",
79f9f56d 468 devname);
5083d66b
N
469 tst->ss->free_super(tst);
470 st->ss->free_super(st);
471 dev_policy_free(pol);
cbeeb0e5 472 domain_free(domains);
95425a89 473 return -1;
83b6208e 474 }
5083d66b 475 tmpdev->used = 1;
64c4757e 476 }
df37ffc0 477 loop:
cbeeb0e5 478 /* Collect domain information from members only */
26b05aea
AC
479 if (tmpdev && tmpdev->used == 1) {
480 if (!pol)
4dd2df09 481 pol = devid_policy(stb.st_rdev);
cbeeb0e5 482 domain_merge(&domains, pol, tst?tst->ss->name:NULL);
26b05aea 483 }
4e8d9f0a
N
484 dev_policy_free(pol);
485 pol = NULL;
3d2b16e7
NB
486 if (tst)
487 tst->ss->free_super(tst);
811e6cbe
NB
488 }
489
ed7fc6b4 490 /* Check if we found some imsm spares but no members */
3c7b4a25
CA
491 if ((auto_assem ||
492 (ident->uuid_set &&
493 memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) &&
494 (!st || !st->sb))
ed7fc6b4
AC
495 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
496 if (tmpdev->used != 3)
497 continue;
498 tmpdev->used = 1;
95425a89 499 content = *contentp;
ed7fc6b4
AC
500
501 if (!st->sb) {
502 /* we need sb from one of the spares */
503 int dfd = dev_open(tmpdev->devname, O_RDONLY);
504 if (dfd < 0 ||
505 st->ss->load_super(st, dfd, NULL))
506 tmpdev->used = 2;
507 if (dfd > 0)
508 close(dfd);
509 }
510 }
511
cbeeb0e5
AC
512 /* Now reject spares that don't match domains of identified members */
513 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
514 struct stat stb;
515 if (tmpdev->used != 3)
516 continue;
517 if (stat(tmpdev->devname, &stb)< 0) {
e7b84f9d 518 pr_err("fstat failed for %s: %s\n",
79f9f56d 519 tmpdev->devname, strerror(errno));
cbeeb0e5
AC
520 tmpdev->used = 2;
521 } else {
4dd2df09 522 struct dev_policy *pol = devid_policy(stb.st_rdev);
a5d10dce
N
523 int dt = domain_test(domains, pol, NULL);
524 if (inargv && dt != 0)
525 /* take this spare as domains match
526 * if there are any */
527 tmpdev->used = 1;
528 else if (!inargv && dt == 1)
529 /* device wasn't explicitly listed, so need
530 * explicit domain match - which we have */
cbeeb0e5
AC
531 tmpdev->used = 1;
532 else
533 /* if domains don't match mark as unused */
534 tmpdev->used = 0;
535 dev_policy_free(pol);
536 }
537 }
538 domain_free(domains);
95425a89
N
539 *stp = st;
540 if (st && st->sb && content == *contentp)
541 st->ss->getinfo_super(st, content, NULL);
542 *contentp = content;
543
544 return num_devs;
545}
546
2c355c22
N
547struct devs {
548 char *devname;
549 int uptodate; /* set once we decide that this device is as
550 * recent as everything else in the array.
551 */
552 int included; /* set if the device is already in the array
553 * due to a previous '-I'
554 */
555 struct mdinfo i;
556};
557
558static int load_devices(struct devs *devices, char *devmap,
df842e69 559 struct mddev_ident *ident, struct supertype **stp,
2c355c22
N
560 struct mddev_dev *devlist, struct context *c,
561 struct mdinfo *content,
562 int mdfd, char *mddev,
563 int *most_recentp, int *bestcntp, int **bestp,
564 int inargv)
565{
566 struct mddev_dev *tmpdev;
567 int devcnt = 0;
568 int nextspare = 0;
569#ifndef MDASSEMBLE
570 int bitmap_done = 0;
571#endif
a792ece6 572 int most_recent = -1;
2c355c22
N
573 int bestcnt = 0;
574 int *best = *bestp;
df842e69 575 struct supertype *st = *stp;
2c355c22
N
576
577 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) {
578 char *devname = tmpdev->devname;
579 struct stat stb;
9ee314da 580 struct supertype *tst;
2c355c22
N
581 int i;
582
583 if (tmpdev->used != 1)
584 continue;
585 /* looks like a good enough match to update the super block if needed */
586#ifndef MDASSEMBLE
587 if (c->update) {
588 int dfd;
589 /* prepare useful information in info structures */
590 struct stat stb2;
2c355c22
N
591 int err;
592 fstat(mdfd, &stb2);
593
594 if (strcmp(c->update, "uuid")==0 &&
595 !ident->uuid_set) {
596 int rfd;
597 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
598 read(rfd, ident->uuid, 16) != 16) {
599 *(__u32*)(ident->uuid) = random();
600 *(__u32*)(ident->uuid+1) = random();
601 *(__u32*)(ident->uuid+2) = random();
602 *(__u32*)(ident->uuid+3) = random();
603 }
604 if (rfd >= 0) close(rfd);
605 }
606 dfd = dev_open(devname,
607 tmpdev->disposition == 'I'
608 ? O_RDWR : (O_RDWR|O_EXCL));
609
610 tst = dup_super(st);
611 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
612 pr_err("cannot re-read metadata from %s - aborting\n",
613 devname);
614 if (dfd >= 0)
615 close(dfd);
616 close(mdfd);
617 free(devices);
618 free(devmap);
9ee314da
N
619 tst->ss->free_super(tst);
620 free(tst);
df842e69 621 *stp = st;
2c355c22
N
622 return -1;
623 }
624 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
625
626 memcpy(content->uuid, ident->uuid, 16);
627 strcpy(content->name, ident->name);
628 content->array.md_minor = minor(stb2.st_rdev);
629
630 if (strcmp(c->update, "byteorder") == 0)
631 err = 0;
632 else
633 err = tst->ss->update_super(tst, content, c->update,
634 devname, c->verbose,
635 ident->uuid_set,
636 c->homehost);
637 if (err < 0) {
afa368f4
N
638 if (err == -1)
639 pr_err("--update=%s not understood"
640 " for %s metadata\n",
641 c->update, tst->ss->name);
2c355c22
N
642 tst->ss->free_super(tst);
643 free(tst);
644 close(mdfd);
645 close(dfd);
646 free(devices);
647 free(devmap);
df842e69 648 *stp = st;
2c355c22
N
649 return -1;
650 }
651 if (strcmp(c->update, "uuid")==0 &&
652 !ident->uuid_set) {
653 ident->uuid_set = 1;
654 memcpy(ident->uuid, content->uuid, 16);
655 }
656 if (tst->ss->store_super(tst, dfd))
657 pr_err("Could not re-write superblock on %s.\n",
658 devname);
659 close(dfd);
660
661 if (strcmp(c->update, "uuid")==0 &&
662 ident->bitmap_fd >= 0 && !bitmap_done) {
663 if (bitmap_update_uuid(ident->bitmap_fd,
664 content->uuid,
665 tst->ss->swapuuid) != 0)
666 pr_err("Could not update uuid on external bitmap.\n");
667 else
668 bitmap_done = 1;
669 }
2c355c22
N
670 } else
671#endif
672 {
9ee314da
N
673 int dfd = dev_open(devname,
674 tmpdev->disposition == 'I'
675 ? O_RDWR : (O_RDWR|O_EXCL));
676 tst = dup_super(st);
2c355c22
N
677
678 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
679 pr_err("cannot re-read metadata from %s - aborting\n",
680 devname);
681 if (dfd >= 0)
682 close(dfd);
683 close(mdfd);
684 free(devices);
685 free(devmap);
9ee314da
N
686 tst->ss->free_super(tst);
687 free(tst);
df842e69 688 *stp = st;
2c355c22
N
689 return -1;
690 }
691 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
2c355c22
N
692 close(dfd);
693 }
694
695 stat(devname, &stb);
696
697 if (c->verbose > 0)
aacb2f81
N
698 pr_err("%s is identified as a member of %s, slot %d%s.\n",
699 devname, mddev, content->disk.raid_disk,
700 (content->disk.state & (1<<MD_DISK_REPLACEMENT)) ? " replacement":"");
2c355c22
N
701 devices[devcnt].devname = devname;
702 devices[devcnt].uptodate = 0;
703 devices[devcnt].included = (tmpdev->disposition == 'I');
704 devices[devcnt].i = *content;
705 devices[devcnt].i.disk.major = major(stb.st_rdev);
706 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
f80057ae 707
a792ece6
N
708 if (devices[devcnt].i.disk.state == 6) {
709 if (most_recent < 0 ||
710 devices[devcnt].i.events
56bbc588
N
711 > devices[most_recent].i.events) {
712 struct supertype *tmp = tst;
713 tst = st;
714 st = tmp;
2c355c22 715 most_recent = devcnt;
56bbc588 716 }
a792ece6 717 }
9ee314da
N
718 tst->ss->free_super(tst);
719 free(tst);
f80057ae 720
2c355c22
N
721 if (content->array.level == LEVEL_MULTIPATH)
722 /* with multipath, the raid_disk from the superblock is meaningless */
723 i = devcnt;
724 else
725 i = devices[devcnt].i.disk.raid_disk;
726 if (i+1 == 0) {
aacb2f81
N
727 if (nextspare < content->array.raid_disks*2)
728 nextspare = content->array.raid_disks*2;
2c355c22
N
729 i = nextspare++;
730 } else {
aacb2f81
N
731 /* i is raid_disk - double it so there is room for
732 * replacements */
733 i *= 2;
734 if (devices[devcnt].i.disk.state & (1<<MD_DISK_REPLACEMENT))
735 i++;
736 if (i >= content->array.raid_disks*2 &&
2c355c22
N
737 i >= nextspare)
738 nextspare = i+1;
739 }
740 if (i < 10000) {
741 if (i >= bestcnt) {
742 int newbestcnt = i+10;
743 int *newbest = xmalloc(sizeof(int)*newbestcnt);
744 int c;
745 for (c=0; c < newbestcnt; c++)
746 if (c < bestcnt)
747 newbest[c] = best[c];
748 else
749 newbest[c] = -1;
750 if (best)free(best);
751 best = newbest;
752 bestcnt = newbestcnt;
753 }
754 if (best[i] >=0 &&
755 devices[best[i]].i.events
756 == devices[devcnt].i.events
757 && (devices[best[i]].i.disk.minor
758 != devices[devcnt].i.disk.minor)
759 && st->ss == &super0
760 && content->array.level != LEVEL_MULTIPATH) {
761 /* two different devices with identical superblock.
762 * Could be a mis-detection caused by overlapping
763 * partitions. fail-safe.
764 */
765 pr_err("WARNING %s and %s appear"
766 " to have very similar superblocks.\n"
767 " If they are really different, "
768 "please --zero the superblock on one\n"
769 " If they are the same or overlap,"
770 " please remove one from %s.\n",
771 devices[best[i]].devname, devname,
772 inargv ? "the list" :
773 "the\n DEVICE list in mdadm.conf"
774 );
775 close(mdfd);
776 free(devices);
777 free(devmap);
df842e69 778 *stp = st;
2c355c22
N
779 return -1;
780 }
781 if (best[i] == -1
782 || (devices[best[i]].i.events
783 < devices[devcnt].i.events))
784 best[i] = devcnt;
785 }
786 devcnt++;
787 }
a792ece6
N
788 if (most_recent >= 0)
789 *most_recentp = most_recent;
2c355c22
N
790 *bestcntp = bestcnt;
791 *bestp = best;
df842e69 792 *stp = st;
2c355c22
N
793 return devcnt;
794}
795
9f5470ce
N
796static int force_array(struct mdinfo *content,
797 struct devs *devices,
798 int *best, int bestcnt, char *avail,
799 int most_recent,
800 struct supertype *st,
801 struct context *c)
802{
803 int okcnt = 0;
804 while (!enough(content->array.level, content->array.raid_disks,
805 content->array.layout, 1,
806 avail)
807 ||
808 (content->reshape_active && content->delta_disks > 0 &&
809 !enough(content->array.level, (content->array.raid_disks
810 - content->delta_disks),
811 content->new_layout, 1,
812 avail)
813 )) {
814 /* Choose the newest best drive which is
815 * not up-to-date, update the superblock
816 * and add it.
817 */
818 int fd;
819 struct supertype *tst;
820 unsigned long long current_events;
821 int chosen_drive = -1;
822 int i;
823
f81a2b56
N
824 for (i = 0;
825 i < content->array.raid_disks * 2 && i < bestcnt;
826 i += 2) {
9f5470ce
N
827 int j = best[i];
828 if (j>=0 &&
829 !devices[j].uptodate &&
830 devices[j].i.recovery_start == MaxSector &&
831 (chosen_drive < 0 ||
832 devices[j].i.events
833 > devices[chosen_drive].i.events))
834 chosen_drive = j;
835 }
836 if (chosen_drive < 0)
837 break;
838 current_events = devices[chosen_drive].i.events;
839 add_another:
840 if (c->verbose >= 0)
841 pr_err("forcing event count in %s(%d) from %d upto %d\n",
79f9f56d
N
842 devices[chosen_drive].devname,
843 devices[chosen_drive].i.disk.raid_disk,
844 (int)(devices[chosen_drive].i.events),
845 (int)(devices[most_recent].i.events));
9f5470ce
N
846 fd = dev_open(devices[chosen_drive].devname,
847 devices[chosen_drive].included ? O_RDWR
848 : (O_RDWR|O_EXCL));
849 if (fd < 0) {
850 pr_err("Couldn't open %s for write - not updating\n",
79f9f56d 851 devices[chosen_drive].devname);
9f5470ce
N
852 devices[chosen_drive].i.events = 0;
853 continue;
854 }
855 tst = dup_super(st);
856 if (tst->ss->load_super(tst,fd, NULL)) {
857 close(fd);
858 pr_err("RAID superblock disappeared from %s - not updating.\n",
79f9f56d 859 devices[chosen_drive].devname);
9f5470ce
N
860 devices[chosen_drive].i.events = 0;
861 continue;
862 }
863 content->events = devices[most_recent].i.events;
864 tst->ss->update_super(tst, content, "force-one",
79f9f56d
N
865 devices[chosen_drive].devname, c->verbose,
866 0, NULL);
9f5470ce
N
867
868 if (tst->ss->store_super(tst, fd)) {
869 close(fd);
870 pr_err("Could not re-write superblock on %s\n",
79f9f56d 871 devices[chosen_drive].devname);
9f5470ce
N
872 devices[chosen_drive].i.events = 0;
873 tst->ss->free_super(tst);
874 continue;
875 }
876 close(fd);
877 devices[chosen_drive].i.events = devices[most_recent].i.events;
878 devices[chosen_drive].uptodate = 1;
879 avail[chosen_drive] = 1;
880 okcnt++;
881 tst->ss->free_super(tst);
882
883 /* If there are any other drives of the same vintage,
884 * add them in as well. We can't lose and we might gain
885 */
f81a2b56
N
886 for (i = 0;
887 i < content->array.raid_disks * 2 && i < bestcnt ;
888 i += 2) {
9f5470ce
N
889 int j = best[i];
890 if (j >= 0 &&
891 !devices[j].uptodate &&
892 devices[j].i.recovery_start == MaxSector &&
893 devices[j].i.events == current_events) {
894 chosen_drive = j;
895 goto add_another;
896 }
897 }
898 }
899 return okcnt;
900}
901
ddc1b11f
N
902static int start_array(int mdfd,
903 char *mddev,
904 struct mdinfo *content,
905 struct supertype *st,
906 struct mddev_ident *ident,
907 int *best, int bestcnt,
908 int chosen_drive,
909 struct devs *devices,
910 unsigned int okcnt,
911 unsigned int sparecnt,
912 unsigned int rebuilding_cnt,
913 struct context *c,
914 int clean, char *avail,
8cde842b 915 int start_partial_ok,
71417de6 916 int err_ok,
8cde842b 917 int was_forced
ddc1b11f
N
918 )
919{
920 int rv;
921 int i;
922 unsigned int req_cnt;
923
924 rv = set_array_info(mdfd, st, content);
71417de6 925 if (rv && !err_ok) {
ddc1b11f
N
926 pr_err("failed to set array info for %s: %s\n",
927 mddev, strerror(errno));
928 return 1;
929 }
930 if (ident->bitmap_fd >= 0) {
931 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
932 pr_err("SET_BITMAP_FILE failed.\n");
933 return 1;
934 }
935 } else if (ident->bitmap_file) {
936 /* From config file */
937 int bmfd = open(ident->bitmap_file, O_RDWR);
938 if (bmfd < 0) {
939 pr_err("Could not open bitmap file %s\n",
940 ident->bitmap_file);
941 return 1;
942 }
943 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
944 pr_err("Failed to set bitmapfile for %s\n", mddev);
945 close(bmfd);
946 return 1;
947 }
948 close(bmfd);
949 }
950
951 /* First, add the raid disks, but add the chosen one last */
952 for (i=0; i<= bestcnt; i++) {
953 int j;
954 if (i < bestcnt) {
955 j = best[i];
956 if (j == chosen_drive)
957 continue;
958 } else
959 j = chosen_drive;
960
961 if (j >= 0 && !devices[j].included) {
962 int dfd = dev_open(devices[j].devname,
963 O_RDWR|O_EXCL);
964 if (dfd >= 0) {
965 remove_partitions(dfd);
966 close(dfd);
967 }
968 rv = add_disk(mdfd, st, content, &devices[j].i);
969
970 if (rv) {
971 pr_err("failed to add "
972 "%s to %s: %s\n",
973 devices[j].devname,
974 mddev,
975 strerror(errno));
aacb2f81 976 if (i < content->array.raid_disks * 2
ddc1b11f
N
977 || i == bestcnt)
978 okcnt--;
979 else
980 sparecnt--;
981 } else if (c->verbose > 0)
aacb2f81 982 pr_err("added %s to %s as %d%s%s\n",
ddc1b11f
N
983 devices[j].devname, mddev,
984 devices[j].i.disk.raid_disk,
985 devices[j].uptodate?"":
aacb2f81
N
986 " (possibly out of date)",
987 (devices[j].i.disk.state & (1<<MD_DISK_REPLACEMENT))?" replacement":"");
ddc1b11f
N
988 } else if (j >= 0) {
989 if (c->verbose > 0)
990 pr_err("%s is already in %s as %d\n",
991 devices[j].devname, mddev,
992 devices[j].i.disk.raid_disk);
aacb2f81
N
993 } else if (c->verbose > 0 && i < content->array.raid_disks*2
994 && (i&1) == 0)
ddc1b11f
N
995 pr_err("no uptodate device for slot %d of %s\n",
996 i, mddev);
997 }
998
999 if (content->array.level == LEVEL_CONTAINER) {
1000 if (c->verbose >= 0) {
1001 pr_err("Container %s has been "
1002 "assembled with %d drive%s",
1003 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
1004 if (okcnt < (unsigned)content->array.raid_disks)
1005 fprintf(stderr, " (out of %d)",
1006 content->array.raid_disks);
1007 fprintf(stderr, "\n");
1008 }
0c21b485
PB
1009
1010 if (st->ss->validate_container) {
1011 struct mdinfo *devices_list;
1012 struct mdinfo *info_devices = xmalloc(sizeof(struct mdinfo)*(okcnt+sparecnt));
1013 unsigned int count;
1014 devices_list = NULL;
1015 for (count = 0; count < okcnt+sparecnt; count++) {
1016 info_devices[count] = devices[count].i;
1017 info_devices[count].next = devices_list;
1018 devices_list = &info_devices[count];
1019 }
1020 if (st->ss->validate_container(devices_list))
1021 pr_err("Mismatch detected!\n");
1022 free(info_devices);
1023 }
1024
ddc1b11f
N
1025 st->ss->free_super(st);
1026 sysfs_uevent(content, "change");
284546ef
N
1027 if (err_ok && okcnt < (unsigned)content->array.raid_disks)
1028 /* Was partial, is still partial, so signal an error
1029 * to ensure we don't retry */
1030 return 1;
ddc1b11f
N
1031 return 0;
1032 }
1033
1034 /* Get number of in-sync devices according to the superblock.
1035 * We must have this number to start the array without -s or -R
1036 */
1037 req_cnt = content->array.working_disks;
1038
1039 if (c->runstop == 1 ||
1040 (c->runstop <= 0 &&
1041 ( enough(content->array.level, content->array.raid_disks,
1042 content->array.layout, clean, avail) &&
1043 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
1044 ))) {
1045 /* This array is good-to-go.
1046 * If a reshape is in progress then we might need to
1047 * continue monitoring it. In that case we start
1048 * it read-only and let the grow code make it writable.
1049 */
1050 int rv;
1051#ifndef MDASSEMBLE
1052 if (content->reshape_active &&
1053 !(content->reshape_active & RESHAPE_NO_BACKUP) &&
1054 content->delta_disks <= 0) {
be7c26b4
N
1055 if (!c->backup_file) {
1056 pr_err("%s: Need a backup file to complete reshape of this array.\n",
1057 mddev);
1058 pr_err("Please provided one with \"--backup-file=...\"\n");
1059 if (c->update &&
1060 strcmp(c->update, "revert-reshape") == 0)
1061 pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
1062 return 1;
1063 }
ddc1b11f
N
1064 rv = sysfs_set_str(content, NULL,
1065 "array_state", "readonly");
1066 if (rv == 0)
1067 rv = Grow_continue(mdfd, st, content,
06e293d0 1068 c->backup_file, 0,
ddc1b11f
N
1069 c->freeze_reshape);
1070 } else if (c->readonly &&
1071 sysfs_attribute_available(
1072 content, NULL, "array_state")) {
1073 rv = sysfs_set_str(content, NULL,
1074 "array_state", "readonly");
1075 } else
1076#endif
1077 rv = ioctl(mdfd, RUN_ARRAY, NULL);
8832342d 1078 reopen_mddev(mdfd); /* drop O_EXCL */
ddc1b11f
N
1079 if (rv == 0) {
1080 if (c->verbose >= 0) {
1081 pr_err("%s has been started with %d drive%s",
1082 mddev, okcnt, okcnt==1?"":"s");
1083 if (okcnt < (unsigned)content->array.raid_disks)
1084 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1085 if (rebuilding_cnt)
1086 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1087 if (sparecnt)
1088 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1089 fprintf(stderr, ".\n");
1090 }
1091 if (content->reshape_active &&
1092 content->array.level >= 4 &&
1093 content->array.level <= 6) {
1094 /* might need to increase the size
1095 * of the stripe cache - default is 256
1096 */
1097 if (256 < 4 * (content->array.chunk_size/4096)) {
4dd2df09 1098 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
ddc1b11f
N
1099 if (sra)
1100 sysfs_set_num(sra, NULL,
1101 "stripe_cache_size",
1102 (4 * content->array.chunk_size / 4096) + 1);
1103 sysfs_free(sra);
1104 }
1105 }
1106 if (okcnt < (unsigned)content->array.raid_disks) {
1107 /* If any devices did not get added
1108 * because the kernel rejected them based
1109 * on event count, try adding them
1110 * again providing the action policy is
1111 * 're-add' or greater. The bitmap
1112 * might allow them to be included, or
1113 * they will become spares.
1114 */
1115 for (i = 0; i < bestcnt; i++) {
1116 int j = best[i];
1117 if (j >= 0 && !devices[j].uptodate) {
1118 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1119 continue;
1120 rv = add_disk(mdfd, st, content,
1121 &devices[j].i);
1122 if (rv == 0 && c->verbose >= 0)
1123 pr_err("%s has been re-added.\n",
1124 devices[j].devname);
1125 }
1126 }
1127 }
8cde842b
N
1128 if (content->array.level == 6 &&
1129 okcnt + 1 == (unsigned)content->array.raid_disks &&
1130 was_forced) {
1131 struct mdinfo *sra = sysfs_read(mdfd, NULL, 0);
1132 if (sra)
1133 sysfs_set_str(sra, NULL,
1134 "sync_action", "repair");
1135 sysfs_free(sra);
1136 }
ddc1b11f
N
1137 return 0;
1138 }
1139 pr_err("failed to RUN_ARRAY %s: %s\n",
1140 mddev, strerror(errno));
1141
1142 if (!enough(content->array.level, content->array.raid_disks,
1143 content->array.layout, 1, avail))
1144 pr_err("Not enough devices to "
1145 "start the array.\n");
1146 else if (!enough(content->array.level,
1147 content->array.raid_disks,
1148 content->array.layout, clean,
1149 avail))
1150 pr_err("Not enough devices to "
1151 "start the array while not clean "
1152 "- consider --force.\n");
1153
1154 return 1;
1155 }
1156 if (c->runstop == -1) {
1157 pr_err("%s assembled from %d drive%s",
1158 mddev, okcnt, okcnt==1?"":"s");
1159 if (okcnt != (unsigned)content->array.raid_disks)
1160 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1161 fprintf(stderr, ", but not started.\n");
1162 return 2;
1163 }
1164 if (c->verbose >= -1) {
1165 pr_err("%s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1166 if (rebuilding_cnt)
aacb2f81 1167 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
ddc1b11f
N
1168 if (sparecnt)
1169 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1170 if (!enough(content->array.level, content->array.raid_disks,
1171 content->array.layout, 1, avail))
1172 fprintf(stderr, " - not enough to start the array.\n");
1173 else if (!enough(content->array.level,
1174 content->array.raid_disks,
1175 content->array.layout, clean,
1176 avail))
1177 fprintf(stderr, " - not enough to start the "
1178 "array while not clean - consider "
1179 "--force.\n");
1180 else {
1181 if (req_cnt == (unsigned)content->array.raid_disks)
1182 fprintf(stderr, " - need all %d to start it", req_cnt);
1183 else
aacb2f81 1184 fprintf(stderr, " - need %d to start", req_cnt);
ddc1b11f
N
1185 fprintf(stderr, " (use --run to insist).\n");
1186 }
1187 }
1188 return 1;
1189}
1190
95425a89
N
1191int Assemble(struct supertype *st, char *mddev,
1192 struct mddev_ident *ident,
1193 struct mddev_dev *devlist,
1194 struct context *c)
1195{
1196 /*
1197 * The task of Assemble is to find a collection of
1198 * devices that should (according to their superblocks)
1199 * form an array, and to give this collection to the MD driver.
1200 * In Linux-2.4 and later, this involves submitting a
1201 * SET_ARRAY_INFO ioctl with no arg - to prepare
1202 * the array - and then submit a number of
1203 * ADD_NEW_DISK ioctls to add disks into
1204 * the array. Finally RUN_ARRAY might
1205 * be submitted to start the array.
1206 *
1207 * Much of the work of Assemble is in finding and/or
1208 * checking the disks to make sure they look right.
1209 *
1210 * If mddev is not set, then scan must be set and we
1211 * read through the config file for dev+uuid mapping
1212 * We recurse, setting mddev, for each device that
1213 * - isn't running
1214 * - has a valid uuid (or any uuid if !uuidset)
1215 *
1216 * If mddev is set, we try to determine state of md.
1217 * check version - must be at least 0.90.0
1218 * check kernel version. must be at least 2.4.
1219 * If not, we can possibly fall back on START_ARRAY
1220 * Try to GET_ARRAY_INFO.
1221 * If possible, give up
1222 * If not, try to STOP_ARRAY just to make sure
1223 *
1224 * If !uuidset and scan, look in conf-file for uuid
1225 * If not found, give up
1226 * If !devlist and scan and uuidset, get list of devs from conf-file
1227 *
1228 * For each device:
1229 * Check superblock - discard if bad
1230 * Check uuid (set if we don't have one) - discard if no match
1231 * Check superblock similarity if we have a superblock - discard if different
1232 * Record events, devicenum
1233 * This should give us a list of devices for the array
1234 * We should collect the most recent event number
1235 *
1236 * Count disks with recent enough event count
1237 * While force && !enough disks
1238 * Choose newest rejected disks, update event count
1239 * mark clean and rewrite superblock
1240 * If recent kernel:
1241 * SET_ARRAY_INFO
1242 * foreach device with recent events : ADD_NEW_DISK
1243 * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY
1244 * If old kernel:
1245 * Check the device numbers in superblock are right
1246 * update superblock if any changes
1247 * START_ARRAY
1248 *
1249 */
6f4dbdc4 1250 int rv;
95425a89
N
1251 int mdfd;
1252 int clean;
1253 int auto_assem = (mddev == NULL && !ident->uuid_set &&
1254 ident->super_minor == UnSet && ident->name[0] == 0
1255 && (ident->container == NULL || ident->member == NULL));
2c355c22 1256 struct devs *devices;
95425a89
N
1257 char *devmap;
1258 int *best = NULL; /* indexed by raid_disk */
1259 int bestcnt = 0;
2c355c22 1260 int devcnt;
aacb2f81 1261 unsigned int okcnt, sparecnt, rebuilding_cnt, replcnt;
95425a89 1262 int i;
8cde842b 1263 int was_forced = 0;
95425a89
N
1264 int most_recent = 0;
1265 int chosen_drive;
1266 int change = 0;
1267 int inargv = 0;
95425a89
N
1268 int start_partial_ok = (c->runstop >= 0) &&
1269 (c->force || devlist==NULL || auto_assem);
1270 int num_devs;
1271 struct mddev_dev *tmpdev;
1272 struct mdinfo info;
1273 struct mdinfo *content = NULL;
1274 struct mdinfo *pre_exist = NULL;
1275 char *avail;
95425a89
N
1276 char *name = NULL;
1277 char chosen_name[1024];
1278 struct map_ent *map = NULL;
1279 struct map_ent *mp;
1280
95425a89
N
1281 /*
1282 * If any subdevs are listed, then any that don't
1283 * match ident are discarded. Remainder must all match and
1284 * become the array.
1285 * If no subdevs, then we scan all devices in the config file, but
1286 * there must be something in the identity
1287 */
1288
1289 if (!devlist &&
1290 ident->uuid_set == 0 &&
1291 (ident->super_minor < 0 || ident->super_minor == UnSet) &&
1292 ident->name[0] == 0 &&
1293 (ident->container == NULL || ident->member == NULL) &&
1294 ident->devices == NULL) {
1295 pr_err("No identity information available for %s - cannot assemble.\n",
1296 mddev ? mddev : "further assembly");
1297 return 1;
1298 }
1299
1300 if (devlist == NULL)
1301 devlist = conf_get_devs();
1302 else if (mddev)
1303 inargv = 1;
1304
79f9f56d 1305try_again:
95425a89
N
1306 /* We come back here when doing auto-assembly and attempting some
1307 * set of devices failed. Those are now marked as ->used==2 and
1308 * we ignore them and try again
1309 */
1310 if (!st && ident->st)
1311 st = ident->st;
1312 if (c->verbose>0)
1313 pr_err("looking for devices for %s\n",
1314 mddev ? mddev : "further assembly");
1315
1316 content = &info;
cb8f6859
N
1317 if (st)
1318 st->ignore_hw_compat = 1;
95425a89
N
1319 num_devs = select_devices(devlist, ident, &st, &content, c,
1320 inargv, auto_assem);
1321 if (num_devs < 0)
1322 return 1;
1011e834 1323
98dbd966 1324 if (!st || !st->sb || !content)
c30e5369
N
1325 return 2;
1326
0431869c
N
1327 /* We have a full set of devices - we now need to find the
1328 * array device.
1329 * However there is a risk that we are racing with "mdadm -I"
1330 * and the array is already partially assembled - we will have
1331 * rejected any devices already in this address.
1332 * So we take a lock on the map file - to prevent further races -
1333 * and look for the uuid in there. If found and the array is
1334 * active, we abort. If found and the array is not active
1335 * we commit to that md device and add all the contained devices
1336 * to our list. We flag them so that we don't try to re-add,
1337 * but can remove if they turn out to not be wanted.
1338 */
1339 if (map_lock(&map))
1340 pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
1341 mp = map_by_uuid(&map, content->uuid);
1342 if (mp) {
1343 struct mdinfo *dv;
1344 /* array already exists. */
4dd2df09 1345 pre_exist = sysfs_read(-1, mp->devnm, GET_LEVEL|GET_DEVS);
0431869c
N
1346 if (pre_exist->array.level != UnSet) {
1347 pr_err("Found some drive for an array that is already active: %s\n",
1348 mp->path);
1349 pr_err("giving up.\n");
1350 return 1;
1351 }
1352 for (dv = pre_exist->devs; dv; dv = dv->next) {
1353 /* We want to add this device to our list,
1354 * but it could already be there if "mdadm -I"
1355 * started *after* we checked for O_EXCL.
1356 * If we add it to the top of the list
1357 * it will be preferred over later copies.
1358 */
1359 struct mddev_dev *newdev;
1360 char *devname = map_dev(dv->disk.major,
1361 dv->disk.minor,
1362 0);
1363 if (!devname)
1364 continue;
1365 newdev = xmalloc(sizeof(*newdev));
1366 newdev->devname = devname;
1367 newdev->disposition = 'I';
1368 newdev->used = 1;
1369 newdev->next = devlist;
1370 devlist = newdev;
1371 num_devs++;
1372 }
1373 strcpy(chosen_name, mp->path);
1374 if (c->verbose > 0 || mddev == NULL ||
1375 strcmp(mddev, chosen_name) != 0)
1376 pr_err("Merging with already-assembled %s\n",
1377 chosen_name);
4dd2df09 1378 mdfd = open_dev_excl(mp->devnm);
0431869c
N
1379 } else {
1380 int trustworthy = FOREIGN;
1381 name = content->name;
1382 switch (st->ss->match_home(st, c->homehost)
1383 ?: st->ss->match_home(st, "any")) {
1384 case 1:
1385 trustworthy = LOCAL;
1386 name = strchr(content->name, ':');
1387 if (name)
1388 name++;
1389 else
1390 name = content->name;
1391 break;
1392 }
1393 if (!auto_assem)
1394 /* If the array is listed in mdadm.conf or on
1395 * command line, then we trust the name
1396 * even if the array doesn't look local
1397 */
1398 trustworthy = LOCAL;
ac2ecf55 1399
0431869c
N
1400 if (name[0] == 0 &&
1401 content->array.level == LEVEL_CONTAINER) {
1402 name = content->text_version;
1403 trustworthy = METADATA;
1404 }
0ac91628 1405
0431869c
N
1406 if (name[0] && trustworthy != LOCAL &&
1407 ! c->require_homehost &&
1408 conf_name_is_free(name))
1409 trustworthy = LOCAL;
0ac91628 1410
0431869c
N
1411 if (trustworthy == LOCAL &&
1412 strchr(name, ':'))
1413 /* Ignore 'host:' prefix of name */
1414 name = strchr(name, ':')+1;
7cdc0872 1415
0431869c
N
1416 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
1417 chosen_name);
1418 }
c30e5369
N
1419 if (mdfd < 0) {
1420 st->ss->free_super(st);
c30e5369 1421 if (auto_assem)
60e1bc1a 1422 goto try_again;
c30e5369
N
1423 return 1;
1424 }
a04d5763 1425 mddev = chosen_name;
6f4dbdc4
N
1426 if (get_linux_version() < 2004000 ||
1427 md_get_version(mdfd) < 9000) {
1428 pr_err("Assemble requires Linux 2.4 or later, and\n"
79f9f56d
N
1429 " md driver version 0.90.0 or later.\n"
1430 " Upgrade your kernel or try --build\n");
c30e5369
N
1431 close(mdfd);
1432 return 1;
1433 }
0431869c 1434 if (pre_exist == NULL) {
4dd2df09 1435 if (mddev_busy(fd2devnm(mdfd))) {
0431869c
N
1436 pr_err("%s already active, cannot restart it!\n",
1437 mddev);
1438 for (tmpdev = devlist ;
1439 tmpdev && tmpdev->used != 1;
1440 tmpdev = tmpdev->next)
1441 ;
1442 if (tmpdev && auto_assem)
1443 pr_err("%s needed for %s...\n",
1444 mddev, tmpdev->devname);
1445 close(mdfd);
1446 mdfd = -3;
1447 st->ss->free_super(st);
1448 if (auto_assem)
1449 goto try_again;
1450 return 1;
1451 }
1452 /* just incase it was started but has no content */
1453 ioctl(mdfd, STOP_ARRAY, NULL);
8a46fe84
NB
1454 }
1455
9008ed1c
N
1456#ifndef MDASSEMBLE
1457 if (content != &info) {
1458 /* This is a member of a container. Try starting the array. */
588bebfc 1459 int err;
11b6d91d 1460 err = assemble_container_content(st, mdfd, content, c,
9ca39acb 1461 chosen_name, NULL);
588bebfc 1462 close(mdfd);
588bebfc 1463 return err;
9008ed1c
N
1464 }
1465#endif
811e6cbe 1466 /* Ok, no bad inconsistancy, we can try updating etc */
503975b9
N
1467 devices = xcalloc(num_devs, sizeof(*devices));
1468 devmap = xcalloc(num_devs, content->array.raid_disks);
df842e69 1469 devcnt = load_devices(devices, devmap, ident, &st, devlist,
2c355c22
N
1470 c, content, mdfd, mddev,
1471 &most_recent, &bestcnt, &best, inargv);
1472 if (devcnt < 0)
1473 return 1;
4b1ac34b 1474
64c4757e 1475 if (devcnt == 0) {
e7b84f9d 1476 pr_err("no devices found for %s\n",
79f9f56d 1477 mddev);
3d2b16e7
NB
1478 if (st)
1479 st->ss->free_super(st);
7f91af49 1480 close(mdfd);
d7f7ebb7 1481 free(devices);
02e7c5b7 1482 free(devmap);
64c4757e
NB
1483 return 1;
1484 }
4b1ac34b 1485
4977146a 1486 if (c->update && strcmp(c->update, "byteorder")==0)
3d2b16e7
NB
1487 st->minor_version = 90;
1488
a5d85af7 1489 st->ss->getinfo_super(st, content, NULL);
98dbd966 1490 clean = content->array.state & 1;
4b1ac34b 1491
64c4757e
NB
1492 /* now we have some devices that might be suitable.
1493 * I wonder how many
1494 */
503975b9 1495 avail = xcalloc(content->array.raid_disks, 1);
64c4757e 1496 okcnt = 0;
aacb2f81 1497 replcnt = 0;
52826846 1498 sparecnt=0;
1ff98339 1499 rebuilding_cnt=0;
f21e18ca 1500 for (i=0; i< bestcnt; i++) {
64c4757e 1501 int j = best[i];
ee04451c
NB
1502 int event_margin = 1; /* always allow a difference of '1'
1503 * like the kernel does
1504 */
64c4757e 1505 if (j < 0) continue;
d013a55e
NB
1506 /* note: we ignore error flags in multipath arrays
1507 * as they don't make sense
1508 */
df0d4ea0 1509 if (content->array.level != LEVEL_MULTIPATH)
f22385f9 1510 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
213ee40b 1511 if (!(devices[j].i.disk.state
ed7fc6b4
AC
1512 & (1<<MD_DISK_FAULTY))) {
1513 devices[j].uptodate = 1;
aa88f531 1514 sparecnt++;
ed7fc6b4 1515 }
d013a55e 1516 continue;
aa88f531 1517 }
de5a472e 1518 /* If this device thinks that 'most_recent' has failed, then
02e7c5b7
N
1519 * we must reject this device.
1520 */
eb20ecf1 1521 if (j != most_recent && !c->force &&
02e7c5b7
N
1522 content->array.raid_disks > 0 &&
1523 devices[most_recent].i.disk.raid_disk >= 0 &&
1524 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
4977146a 1525 if (c->verbose > -1)
e7b84f9d 1526 pr_err("ignoring %s as it reports %s as failed\n",
79f9f56d 1527 devices[j].devname, devices[most_recent].devname);
02e7c5b7
N
1528 best[i] = -1;
1529 continue;
1530 }
f80057ae
N
1531 /* Require event counter to be same as, or just less than,
1532 * most recent. If it is bigger, it must be a stray spare and
1533 * should be ignored.
1534 */
213ee40b 1535 if (devices[j].i.events+event_margin >=
f80057ae
N
1536 devices[most_recent].i.events &&
1537 devices[j].i.events <=
1538 devices[most_recent].i.events
1539 ) {
64c4757e 1540 devices[j].uptodate = 1;
aacb2f81 1541 if (i < content->array.raid_disks * 2) {
dcc4210f
DW
1542 if (devices[j].i.recovery_start == MaxSector ||
1543 (content->reshape_active &&
b720636a
N
1544 ((i >= content->array.raid_disks - content->delta_disks) ||
1545 (i >= content->array.raid_disks - content->delta_disks - 1
1546 && content->array.level == 4)))) {
aacb2f81
N
1547 if (!avail[i/2]) {
1548 okcnt++;
1549 avail[i/2]=1;
1550 } else
1551 replcnt++;
1ff98339
N
1552 } else
1553 rebuilding_cnt++;
265e0f17 1554 } else
52826846 1555 sparecnt++;
64c4757e
NB
1556 }
1557 }
02e7c5b7 1558 free(devmap);
8cde842b
N
1559 if (c->force) {
1560 int force_ok = force_array(content, devices, best, bestcnt,
1561 avail, most_recent, st, c);
1562 okcnt += force_ok;
1563 if (force_ok)
1564 was_forced = 1;
1565 }
52826846
NB
1566 /* Now we want to look at the superblock which the kernel will base things on
1567 * and compare the devices that we think are working with the devices that the
1568 * superblock thinks are working.
1569 * If there are differences and --force is given, then update this chosen
1570 * superblock.
1571 */
cd29a5c8 1572 chosen_drive = -1;
3da92f27 1573 st->ss->free_super(st);
f81a2b56 1574 for (i=0; chosen_drive < 0 && i<bestcnt; i+=2) {
52826846
NB
1575 int j = best[i];
1576 int fd;
4b1ac34b 1577
52826846
NB
1578 if (j<0)
1579 continue;
1580 if (!devices[j].uptodate)
1581 continue;
a28232b8
N
1582 if (devices[j].i.events < devices[most_recent].i.events)
1583 continue;
52826846 1584 chosen_drive = j;
0431869c
N
1585 if ((fd=dev_open(devices[j].devname,
1586 devices[j].included ? O_RDONLY
1587 : (O_RDONLY|O_EXCL)))< 0) {
e7b84f9d 1588 pr_err("Cannot open %s: %s\n",
79f9f56d 1589 devices[j].devname, strerror(errno));
7f91af49 1590 close(mdfd);
d7f7ebb7 1591 free(devices);
52826846
NB
1592 return 1;
1593 }
3da92f27 1594 if (st->ss->load_super(st,fd, NULL)) {
52826846 1595 close(fd);
e7b84f9d 1596 pr_err("RAID superblock has disappeared from %s\n",
79f9f56d 1597 devices[j].devname);
7f91af49 1598 close(mdfd);
d7f7ebb7 1599 free(devices);
52826846
NB
1600 return 1;
1601 }
1602 close(fd);
1603 }
3da92f27 1604 if (st->sb == NULL) {
e7b84f9d 1605 pr_err("No suitable drives found for %s\n", mddev);
7f91af49 1606 close(mdfd);
d7f7ebb7 1607 free(devices);
4b1ac34b
NB
1608 return 1;
1609 }
a5d85af7 1610 st->ss->getinfo_super(st, content, NULL);
f35f2525 1611#ifndef MDASSEMBLE
4dd2df09 1612 sysfs_init(content, mdfd, NULL);
f35f2525 1613#endif
56eedc1a 1614 for (i=0; i<bestcnt; i++) {
52826846 1615 int j = best[i];
98c6faba 1616 unsigned int desired_state;
11a3e71d 1617
aacb2f81 1618 if (i >= content->array.raid_disks * 2)
11a3e71d 1619 desired_state = 0;
aacb2f81
N
1620 else if (i & 1)
1621 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_REPLACEMENT);
1622 else
1623 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
11a3e71d 1624
52826846
NB
1625 if (j<0)
1626 continue;
1627 if (!devices[j].uptodate)
1628 continue;
4b1ac34b 1629
213ee40b 1630 devices[j].i.disk.state = desired_state;
4e9a6ff7
N
1631 if (!(devices[j].i.array.state & 1))
1632 clean = 0;
213ee40b
NB
1633
1634 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
4977146a
N
1635 c->verbose, 0, NULL)) {
1636 if (c->force) {
1637 if (c->verbose >= 0)
e7b84f9d 1638 pr_err("clearing FAULTY flag for device %d in %s for %s\n",
79f9f56d 1639 j, mddev, devices[j].devname);
4b1ac34b 1640 change = 1;
52826846 1641 } else {
4977146a 1642 if (c->verbose >= -1)
e7b84f9d 1643 pr_err("device %d in %s has wrong state in superblock, but %s seems ok\n",
79f9f56d 1644 i, mddev, devices[j].devname);
52826846
NB
1645 }
1646 }
4b1ac34b 1647#if 0
213ee40b 1648 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
e7b84f9d 1649 pr_err("devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
79f9f56d 1650 i, mddev);
52826846 1651 }
4b1ac34b 1652#endif
52826846 1653 }
4977146a 1654 if (c->force && !clean &&
98dbd966
DW
1655 !enough(content->array.level, content->array.raid_disks,
1656 content->array.layout, clean,
de5a472e 1657 avail)) {
98dbd966 1658 change += st->ss->update_super(st, content, "force-array",
6f4dbdc4 1659 devices[chosen_drive].devname, c->verbose,
67a8c82d 1660 0, NULL);
8cde842b 1661 was_forced = 1;
583315d9 1662 clean = 1;
d013a55e 1663 }
52826846 1664
4b1ac34b 1665 if (change) {
52826846 1666 int fd;
0431869c
N
1667 fd = dev_open(devices[chosen_drive].devname,
1668 devices[chosen_drive].included ?
1669 O_RDWR : (O_RDWR|O_EXCL));
52826846 1670 if (fd < 0) {
e7b84f9d 1671 pr_err("Could not open %s for write - cannot Assemble array.\n",
79f9f56d 1672 devices[chosen_drive].devname);
7f91af49 1673 close(mdfd);
d7f7ebb7 1674 free(devices);
52826846
NB
1675 return 1;
1676 }
3da92f27 1677 if (st->ss->store_super(st, fd)) {
52826846 1678 close(fd);
e7b84f9d 1679 pr_err("Could not re-write superblock on %s\n",
79f9f56d 1680 devices[chosen_drive].devname);
7f91af49 1681 close(mdfd);
d7f7ebb7 1682 free(devices);
52826846
NB
1683 return 1;
1684 }
4977146a 1685 if (c->verbose >= 0)
e7b84f9d 1686 pr_err("Marking array %s as 'clean'\n",
79f9f56d 1687 mddev);
52826846 1688 close(fd);
52826846
NB
1689 }
1690
353632d9
NB
1691 /* If we are in the middle of a reshape we may need to restore saved data
1692 * that was moved aside due to the reshape overwriting live data
1693 * The code of doing this lives in Grow.c
1694 */
39c5a909 1695#ifndef MDASSEMBLE
5e88ab2e
N
1696 if (content->reshape_active &&
1697 !(content->reshape_active & RESHAPE_NO_BACKUP)) {
353632d9 1698 int err = 0;
503975b9 1699 int *fdlist = xmalloc(sizeof(int)* bestcnt);
4977146a 1700 if (c->verbose > 0)
e7b84f9d 1701 pr_err(":%s has an active reshape - checking "
79f9f56d
N
1702 "if critical section needs to be restored\n",
1703 chosen_name);
54ded86f
N
1704 if (!c->backup_file)
1705 c->backup_file = locate_backup(content->sys_name);
a7dec3fd 1706 enable_fds(bestcnt/2);
5e9fd96f
N
1707 for (i = 0; i < bestcnt/2; i++) {
1708 int j = best[i*2];
353632d9 1709 if (j >= 0) {
0431869c
N
1710 fdlist[i] = dev_open(devices[j].devname,
1711 devices[j].included
1712 ? O_RDWR : (O_RDWR|O_EXCL));
353632d9 1713 if (fdlist[i] < 0) {
e7b84f9d 1714 pr_err("Could not open %s for write - cannot Assemble array.\n",
79f9f56d 1715 devices[j].devname);
353632d9
NB
1716 err = 1;
1717 break;
1718 }
1719 } else
1720 fdlist[i] = -1;
1721 }
87f26d14 1722 if (!err) {
ba53ea59
AK
1723 if (st->ss->external && st->ss->recover_backup)
1724 err = st->ss->recover_backup(st, content);
1725 else
5e9fd96f 1726 err = Grow_restart(st, content, fdlist, bestcnt/2,
4977146a
N
1727 c->backup_file, c->verbose > 0);
1728 if (err && c->invalid_backup) {
1729 if (c->verbose > 0)
e7b84f9d 1730 pr_err("continuing"
79f9f56d 1731 " without restoring backup\n");
87f26d14
N
1732 err = 0;
1733 }
1734 }
353632d9
NB
1735 while (i>0) {
1736 i--;
1737 if (fdlist[i]>=0) close(fdlist[i]);
1738 }
56dcaa6b 1739 free(fdlist);
353632d9 1740 if (err) {
e7b84f9d 1741 pr_err("Failed to restore critical section for reshape, sorry.\n");
4977146a 1742 if (c->backup_file == NULL)
e7b84f9d 1743 cont_err("Possibly you needed to specify the --backup-file\n");
7f91af49 1744 close(mdfd);
d7f7ebb7 1745 free(devices);
353632d9
NB
1746 return err;
1747 }
1748 }
39c5a909 1749#endif
aa88f531 1750
64c4757e 1751 /* Almost ready to actually *do* something */
6f4dbdc4
N
1752 /* First, fill in the map, so that udev can find our name
1753 * as soon as we become active.
1754 */
afa368f4
N
1755 if (c->update && strcmp(c->update, "metadata")==0) {
1756 content->array.major_version = 1;
1757 content->array.minor_version = 0;
1758 strcpy(content->text_version, "1.0");
1759 }
1760
4dd2df09 1761 map_update(&map, fd2devnm(mdfd), content->text_version,
6f4dbdc4
N
1762 content->uuid, chosen_name);
1763
1764 rv = start_array(mdfd, mddev, content,
1765 st, ident, best, bestcnt,
1766 chosen_drive, devices, okcnt, sparecnt,
1767 rebuilding_cnt,
1768 c,
71417de6
N
1769 clean, avail, start_partial_ok,
1770 pre_exist != NULL,
1771 was_forced);
6f4dbdc4
N
1772 if (rv == 1 && !pre_exist)
1773 ioctl(mdfd, STOP_ARRAY, NULL);
6f4dbdc4
N
1774 free(devices);
1775 map_unlock(&map);
1776 if (rv == 0) {
1777 wait_for(chosen_name, mdfd);
b20c8a50 1778 close(mdfd);
6f4dbdc4
N
1779 if (auto_assem) {
1780 int usecs = 1;
1781 /* There is a nasty race with 'mdadm --monitor'.
1782 * If it opens this device before we close it,
1783 * it gets an incomplete open on which IO
1784 * doesn't work and the capacity is
1785 * wrong.
1786 * If we reopen (to check for layered devices)
1787 * before --monitor closes, we loose.
1788 *
1789 * So: wait upto 1 second for there to be
1790 * a non-zero capacity.
1791 */
1792 while (usecs < 1000) {
1793 mdfd = open(mddev, O_RDONLY);
1794 if (mdfd >= 0) {
1795 unsigned long long size;
1796 if (get_dev_size(mdfd, NULL, &size) &&
1797 size > 0)
1798 break;
1799 close(mdfd);
589395d6 1800 }
6f4dbdc4
N
1801 usleep(usecs);
1802 usecs <<= 1;
dab6685f 1803 }
aa88f531 1804 }
b20c8a50
N
1805 } else
1806 close(mdfd);
1807
6f4dbdc4
N
1808 /* '2' means 'OK, but not started yet' */
1809 return rv == 2 ? 0 : rv;
64c4757e 1810}
6234c63c
DW
1811
1812#ifndef MDASSEMBLE
1813int assemble_container_content(struct supertype *st, int mdfd,
11b6d91d 1814 struct mdinfo *content, struct context *c,
9ca39acb 1815 char *chosen_name, int *result)
6234c63c 1816{
02b70e83 1817 struct mdinfo *dev, *sra, *dev2;
6234c63c 1818 int working = 0, preexist = 0;
882029c8 1819 int expansion = 0;
6234c63c 1820 struct map_ent *map = NULL;
7af03341 1821 int old_raid_disks;
4aecb54a 1822 int start_reshape;
c1736844
N
1823 char *avail = NULL;
1824 int err;
6234c63c 1825
4dd2df09 1826 sysfs_init(content, mdfd, NULL);
6234c63c 1827
02b70e83 1828 sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
0ea8f5b1
N
1829 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
1830 if (content->array.major_version == -1 &&
1831 content->array.minor_version == -2 &&
11b6d91d 1832 c->readonly &&
0ea8f5b1
N
1833 content->text_version[0] == '/')
1834 content->text_version[0] = '-';
22472ee1
JS
1835 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1836 if (sra)
1837 sysfs_free(sra);
6234c63c 1838 return 1;
22472ee1 1839 }
0ea8f5b1 1840 }
588bebfc 1841
4aecb54a
AK
1842 /* There are two types of reshape: container wide or sub-array specific
1843 * Check if metadata requests blocking container wide reshapes
1844 */
1845 start_reshape = (content->reshape_active &&
79f9f56d
N
1846 !((content->reshape_active == CONTAINER_RESHAPE) &&
1847 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
4aecb54a
AK
1848
1849 /* Block subarray here if it is under reshape now
1850 * Do not allow for any changes in this array
1851 */
1852 if (st->ss->external && content->recovery_blocked && start_reshape)
b8063f07
AK
1853 block_subarray(content);
1854
02b70e83
N
1855 for (dev2 = sra->devs; dev2; dev2 = dev2->next) {
1856 for (dev = content->devs; dev; dev = dev->next)
1857 if (dev2->disk.major == dev->disk.major &&
1858 dev2->disk.minor == dev->disk.minor)
1859 break;
1860 if (dev)
1861 continue;
1862 /* Don't want this one any more */
1863 if (sysfs_set_str(sra, dev2, "slot", "none") < 0 &&
1864 errno == EBUSY) {
1865 pr_err("Cannot remove old device %s: not updating %s\n", dev2->sys_name, sra->sys_name);
1866 sysfs_free(sra);
1867 return 1;
1868 }
1869 sysfs_set_str(sra, dev2, "state", "remove");
1870 }
7af03341 1871 old_raid_disks = content->array.raid_disks - content->delta_disks;
c1736844
N
1872 avail = xcalloc(content->array.raid_disks, 1);
1873 for (dev = content->devs; dev; dev = dev->next) {
1874 if (dev->disk.raid_disk >= 0)
1875 avail[dev->disk.raid_disk] = 1;
14032016 1876 if (sysfs_add_disk(content, dev, 1) == 0) {
7af03341 1877 if (dev->disk.raid_disk >= old_raid_disks &&
14032016
AK
1878 content->reshape_active)
1879 expansion++;
1880 else
1881 working++;
1882 } else if (errno == EEXIST)
6234c63c 1883 preexist++;
c1736844 1884 }
02b70e83 1885 sysfs_free(sra);
c1736844
N
1886 if (working + expansion == 0 && c->runstop <= 0) {
1887 free(avail);
7cb2aa33 1888 return 1;/* Nothing new, don't try to start */
c1736844 1889 }
4dd2df09 1890 map_update(&map, fd2devnm(mdfd),
8b4e5ea9
N
1891 content->text_version,
1892 content->uuid, chosen_name);
1893
4aecb54a 1894
c1736844
N
1895 if (enough(content->array.level, content->array.raid_disks,
1896 content->array.layout, content->array.state & 1, avail) == 0) {
9ca39acb
N
1897 if (c->export && result)
1898 *result |= INCR_NO;
1899 else if (c->verbose >= 0) {
c1736844
N
1900 pr_err("%s assembled with %d device%s",
1901 chosen_name, preexist + working,
1902 preexist + working == 1 ? "":"s");
6234c63c
DW
1903 if (preexist)
1904 fprintf(stderr, " (%d new)", working);
c1736844 1905 fprintf(stderr, " but not started\n");
6234c63c 1906 }
c1736844
N
1907 free(avail);
1908 return 1;
1909 }
1910 free(avail);
1911
1912 if (c->runstop <= 0 &&
1913 (working + preexist + expansion) <
1914 content->array.working_disks) {
9ca39acb
N
1915 if (c->export && result)
1916 *result |= INCR_UNSAFE;
1917 else if (c->verbose >= 0) {
e7b84f9d
N
1918 pr_err("%s assembled with %d device%s",
1919 chosen_name, preexist + working,
1920 preexist + working == 1 ? "":"s");
88716263
N
1921 if (preexist)
1922 fprintf(stderr, " (%d new)", working);
c1736844 1923 fprintf(stderr, " but not safe to start\n");
88716263 1924 }
7cb2aa33
N
1925 return 1;
1926 }
c1736844
N
1927
1928
1929 if (start_reshape) {
1930 int spare = content->array.raid_disks + expansion;
1931 if (restore_backup(st, content,
1932 working,
54ded86f 1933 spare, &c->backup_file, c->verbose) == 1)
c1736844
N
1934 return 1;
1935
1936 err = sysfs_set_str(content, NULL,
1937 "array_state", "readonly");
1938 if (err)
1939 return 1;
1940
1941 if (st->ss->external) {
1942 if (!mdmon_running(st->container_devnm))
1943 start_mdmon(st->container_devnm);
1944 ping_monitor(st->container_devnm);
1945 if (mdmon_running(st->container_devnm) &&
1946 st->update_tail == NULL)
1947 st->update_tail = &st->updates;
1948 }
1949
1950 err = Grow_continue(mdfd, st, content, c->backup_file,
06e293d0 1951 0, c->freeze_reshape);
c1736844
N
1952 } else switch(content->array.level) {
1953 case LEVEL_LINEAR:
1954 case LEVEL_MULTIPATH:
1955 case 0:
1956 err = sysfs_set_str(content, NULL, "array_state",
1957 c->readonly ? "readonly" : "active");
1958 break;
1959 default:
1960 err = sysfs_set_str(content, NULL, "array_state",
1961 "readonly");
1962 /* start mdmon if needed. */
1963 if (!err) {
1964 if (!mdmon_running(st->container_devnm))
1965 start_mdmon(st->container_devnm);
1966 ping_monitor(st->container_devnm);
1967 }
1968 break;
1969 }
1970 if (!err)
1971 sysfs_set_safemode(content, content->safe_mode_delay);
1972
1973 /* Block subarray here if it is not reshaped now
1974 * It has be blocked a little later to allow mdmon to switch in
1975 * in to R/W state
1976 */
1977 if (st->ss->external && content->recovery_blocked &&
1978 !start_reshape)
1979 block_subarray(content);
1980
9ca39acb
N
1981 if (c->export && result) {
1982 if (err)
1983 *result |= INCR_NO;
1984 else
1985 *result |= INCR_YES;
1986 } else if (c->verbose >= 0) {
c1736844
N
1987 if (err)
1988 pr_err("array %s now has %d device%s",
1989 chosen_name, working + preexist,
1990 working + preexist == 1 ? "":"s");
1991 else
1992 pr_err("Started %s with %d device%s",
1993 chosen_name, working + preexist,
1994 working + preexist == 1 ? "":"s");
1995 if (preexist)
1996 fprintf(stderr, " (%d new)", working);
1997 if (expansion)
1998 fprintf(stderr, " ( + %d for expansion)",
1999 expansion);
2000 fprintf(stderr, "\n");
2001 }
2002 if (!err)
2003 wait_for(chosen_name, mdfd);
2004 return err;
2005 /* FIXME should have an O_EXCL and wait for read-auto */
6234c63c
DW
2006}
2007#endif