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