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