]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Assemble.c
Resolve some more warnings
[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|O_EXCL);
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
412 if (ident->container) {
413 if (ident->container[0] == '/' &&
414 !same_dev(ident->container, devname)) {
415 if (report_missmatch)
416 fprintf(stderr, Name ": %s is not the container required (%s)\n",
417 devname, ident->container);
418 goto loop;
419 }
420 if (ident->container[0] != '/') {
421 /* we have a uuid */
422 int uuid[4];
423
424 content = &info;
425 tst->ss->getinfo_super(tst, content, NULL);
426
427 if (!parse_uuid(ident->container, uuid) ||
428 !same_uuid(content->uuid, uuid, tst->ss->swapuuid)) {
429 if (report_missmatch)
430 fprintf(stderr, Name ": %s has wrong UUID to be required container\n",
431 devname);
432 goto loop;
433 }
434 }
435 }
436 /* It is worth looking inside this container.
437 */
438 if (verbose > 0)
439 fprintf(stderr, Name ": looking in container %s\n",
440 devname);
441
442 for (content = tst->ss->container_content(tst, NULL);
443 content;
444 content = content->next) {
445
446 if (!ident_matches(ident, content, tst,
447 homehost, update,
448 report_missmatch ? devname : NULL))
449 /* message already printed */;
450 else if (is_member_busy(content->text_version)) {
451 if (report_missmatch)
452 fprintf(stderr, Name ": member %s in %s is already assembled\n",
453 content->text_version,
454 devname);
455 } else if (content->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
456 /* do not assemble arrays with unsupported configurations */
457 fprintf(stderr, Name ": Cannot activate member %s in %s.\n",
458 content->text_version,
459 devname);
460 } else
461 break;
462 }
463 if (!content) {
464 tmpdev->used = 2;
465 goto loop; /* empty container */
466 }
467
468 st = tst; tst = NULL;
469 if (!auto_assem && inargv && tmpdev->next != NULL) {
470 fprintf(stderr, Name ": %s is a container, but is not "
471 "only device given: confused and aborting\n",
472 devname);
473 st->ss->free_super(st);
474 dev_policy_free(pol);
475 domain_free(domains);
476 return 1;
477 }
478 if (verbose > 0)
479 fprintf(stderr, Name ": found match on member %s in %s\n",
480 content->text_version, devname);
481
482 /* make sure we finished the loop */
483 tmpdev = NULL;
484 goto loop;
485 } else {
486
487 content = &info;
488 tst->ss->getinfo_super(tst, content, NULL);
489
490 if (!ident_matches(ident, content, tst,
491 homehost, update,
492 report_missmatch ? devname : NULL))
493 goto loop;
494
495 if (st == NULL)
496 st = dup_super(tst);
497 if (st->minor_version == -1)
498 st->minor_version = tst->minor_version;
499
500 if (memcmp(content->uuid, uuid_zero,
501 sizeof(int[4])) == 0) {
502 /* this is a floating spare. It cannot define
503 * an array unless there are no more arrays of
504 * this type to be found. It can be included
505 * in an array of this type though.
506 */
507 tmpdev->used = 3;
508 goto loop;
509 }
510
511 if (st->ss != tst->ss ||
512 st->minor_version != tst->minor_version ||
513 st->ss->compare_super(st, tst) != 0) {
514 /* Some mismatch. If exactly one array matches this host,
515 * we can resolve on that one.
516 * Or, if we are auto assembling, we just ignore the second
517 * for now.
518 */
519 if (auto_assem)
520 goto loop;
521 if (homehost) {
522 int first = st->ss->match_home(st, homehost);
523 int last = tst->ss->match_home(tst, homehost);
524 if (first != last &&
525 (first == 1 || last == 1)) {
526 /* We can do something */
527 if (first) {/* just ignore this one */
528 if (report_missmatch)
529 fprintf(stderr, Name ": %s misses out due to wrong homehost\n",
530 devname);
531 goto loop;
532 } else { /* reject all those sofar */
533 struct mddev_dev *td;
534 if (report_missmatch)
535 fprintf(stderr, Name ": %s overrides previous devices due to good homehost\n",
536 devname);
537 for (td=devlist; td != tmpdev; td=td->next)
538 if (td->used == 1)
539 td->used = 0;
540 tmpdev->used = 1;
541 goto loop;
542 }
543 }
544 }
545 fprintf(stderr, Name ": superblock on %s doesn't match others - assembly aborted\n",
546 devname);
547 tst->ss->free_super(tst);
548 st->ss->free_super(st);
549 dev_policy_free(pol);
550 domain_free(domains);
551 return 1;
552 }
553 tmpdev->used = 1;
554 }
555 loop:
556 /* Collect domain information from members only */
557 if (tmpdev && tmpdev->used == 1) {
558 if (!pol)
559 pol = devnum_policy(stb.st_rdev);
560 domain_merge(&domains, pol, tst?tst->ss->name:NULL);
561 }
562 dev_policy_free(pol);
563 pol = NULL;
564 if (tst)
565 tst->ss->free_super(tst);
566 }
567
568 /* Check if we found some imsm spares but no members */
569 if ((auto_assem ||
570 (ident->uuid_set &&
571 memcmp(uuid_zero, ident->uuid,sizeof(uuid_zero)) == 0)) &&
572 (!st || !st->sb))
573 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
574 if (tmpdev->used != 3)
575 continue;
576 tmpdev->used = 1;
577 content = &info;
578
579 if (!st->sb) {
580 /* we need sb from one of the spares */
581 int dfd = dev_open(tmpdev->devname, O_RDONLY);
582 if (dfd < 0 ||
583 st->ss->load_super(st, dfd, NULL))
584 tmpdev->used = 2;
585 if (dfd > 0)
586 close(dfd);
587 }
588 }
589
590 /* Now reject spares that don't match domains of identified members */
591 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
592 struct stat stb;
593 if (tmpdev->used != 3)
594 continue;
595 if (stat(tmpdev->devname, &stb)< 0) {
596 fprintf(stderr, Name ": fstat failed for %s: %s\n",
597 tmpdev->devname, strerror(errno));
598 tmpdev->used = 2;
599 } else {
600 struct dev_policy *pol = devnum_policy(stb.st_rdev);
601 int dt = domain_test(domains, pol, NULL);
602 if (inargv && dt != 0)
603 /* take this spare as domains match
604 * if there are any */
605 tmpdev->used = 1;
606 else if (!inargv && dt == 1)
607 /* device wasn't explicitly listed, so need
608 * explicit domain match - which we have */
609 tmpdev->used = 1;
610 else
611 /* if domains don't match mark as unused */
612 tmpdev->used = 0;
613 dev_policy_free(pol);
614 }
615 }
616 domain_free(domains);
617
618 if (!st || !st->sb || !content)
619 return 2;
620
621 /* Now need to open the array device. Use create_mddev */
622 if (content == &info)
623 st->ss->getinfo_super(st, content, NULL);
624
625 trustworthy = FOREIGN;
626 name = content->name;
627 switch (st->ss->match_home(st, homehost)
628 ?: st->ss->match_home(st, "any")) {
629 case 1:
630 trustworthy = LOCAL;
631 name = strchr(content->name, ':');
632 if (name)
633 name++;
634 else
635 name = content->name;
636 break;
637 }
638 if (!auto_assem)
639 /* If the array is listed in mdadm.conf or on
640 * command line, then we trust the name
641 * even if the array doesn't look local
642 */
643 trustworthy = LOCAL;
644
645 if (name[0] == 0 &&
646 content->array.level == LEVEL_CONTAINER) {
647 name = content->text_version;
648 trustworthy = METADATA;
649 }
650
651 if (name[0] && trustworthy != LOCAL &&
652 ! require_homehost &&
653 conf_name_is_free(name))
654 trustworthy = LOCAL;
655
656 if (trustworthy == LOCAL &&
657 strchr(name, ':'))
658 /* Ignore 'host:' prefix of name */
659 name = strchr(name, ':')+1;
660
661 mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
662 chosen_name);
663 if (mdfd < 0) {
664 st->ss->free_super(st);
665 if (auto_assem)
666 goto try_again;
667 return 1;
668 }
669 mddev = chosen_name;
670 vers = md_get_version(mdfd);
671 if (vers < 9000) {
672 fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
673 " Upgrade your kernel or try --build\n");
674 close(mdfd);
675 return 1;
676 }
677 if (mddev_busy(fd2devnum(mdfd))) {
678 fprintf(stderr, Name ": %s already active, cannot restart it!\n",
679 mddev);
680 for (tmpdev = devlist ;
681 tmpdev && tmpdev->used != 1;
682 tmpdev = tmpdev->next)
683 ;
684 if (tmpdev && auto_assem)
685 fprintf(stderr, Name ": %s needed for %s...\n",
686 mddev, tmpdev->devname);
687 close(mdfd);
688 mdfd = -3;
689 st->ss->free_super(st);
690 if (auto_assem)
691 goto try_again;
692 return 1;
693 }
694 ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
695
696 #ifndef MDASSEMBLE
697 if (content != &info) {
698 /* This is a member of a container. Try starting the array. */
699 int err;
700 err = assemble_container_content(st, mdfd, content, runstop,
701 chosen_name, verbose,
702 backup_file, freeze_reshape);
703 close(mdfd);
704 return err;
705 }
706 bitmap_done = 0;
707 #endif
708 /* Ok, no bad inconsistancy, we can try updating etc */
709 content->update_private = NULL;
710 devices = malloc(num_devs * sizeof(*devices));
711 devmap = calloc(num_devs * content->array.raid_disks, 1);
712 for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) {
713 char *devname = tmpdev->devname;
714 struct stat stb;
715 /* looks like a good enough match to update the super block if needed */
716 #ifndef MDASSEMBLE
717 if (update) {
718 int dfd;
719 /* prepare useful information in info structures */
720 struct stat stb2;
721 struct supertype *tst;
722 int err;
723 fstat(mdfd, &stb2);
724
725 if (strcmp(update, "uuid")==0 &&
726 !ident->uuid_set) {
727 int rfd;
728 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
729 read(rfd, ident->uuid, 16) != 16) {
730 *(__u32*)(ident->uuid) = random();
731 *(__u32*)(ident->uuid+1) = random();
732 *(__u32*)(ident->uuid+2) = random();
733 *(__u32*)(ident->uuid+3) = random();
734 }
735 if (rfd >= 0) close(rfd);
736 }
737 dfd = dev_open(devname, O_RDWR|O_EXCL);
738
739 tst = dup_super(st);
740 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
741 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
742 devname);
743 if (dfd >= 0)
744 close(dfd);
745 close(mdfd);
746 free(devices);
747 free(devmap);
748 return 1;
749 }
750 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
751
752 memcpy(content->uuid, ident->uuid, 16);
753 strcpy(content->name, ident->name);
754 content->array.md_minor = minor(stb2.st_rdev);
755
756 if (strcmp(update, "byteorder") == 0)
757 err = 0;
758 else
759 err = tst->ss->update_super(tst, content, update,
760 devname, verbose,
761 ident->uuid_set,
762 homehost);
763 if (err < 0) {
764 fprintf(stderr,
765 Name ": --update=%s not understood"
766 " for %s metadata\n",
767 update, tst->ss->name);
768 tst->ss->free_super(tst);
769 free(tst);
770 close(mdfd);
771 close(dfd);
772 free(devices);
773 free(devmap);
774 return 1;
775 }
776 if (strcmp(update, "uuid")==0 &&
777 !ident->uuid_set) {
778 ident->uuid_set = 1;
779 memcpy(ident->uuid, content->uuid, 16);
780 }
781 if (tst->ss->store_super(tst, dfd))
782 fprintf(stderr, Name ": Could not re-write superblock on %s.\n",
783 devname);
784 close(dfd);
785
786 if (strcmp(update, "uuid")==0 &&
787 ident->bitmap_fd >= 0 && !bitmap_done) {
788 if (bitmap_update_uuid(ident->bitmap_fd,
789 content->uuid,
790 tst->ss->swapuuid) != 0)
791 fprintf(stderr, Name ": Could not update uuid on external bitmap.\n");
792 else
793 bitmap_done = 1;
794 }
795 tst->ss->free_super(tst);
796 } else
797 #endif
798 {
799 struct supertype *tst = dup_super(st);
800 int dfd;
801 dfd = dev_open(devname, O_RDWR|O_EXCL);
802
803 if (dfd < 0 || tst->ss->load_super(tst, dfd, NULL) != 0) {
804 fprintf(stderr, Name ": cannot re-read metadata from %s - aborting\n",
805 devname);
806 if (dfd >= 0)
807 close(dfd);
808 close(mdfd);
809 free(devices);
810 free(devmap);
811 return 1;
812 }
813 tst->ss->getinfo_super(tst, content, devmap + devcnt * content->array.raid_disks);
814 tst->ss->free_super(tst);
815 close(dfd);
816 }
817
818 stat(devname, &stb);
819
820 if (verbose > 0)
821 fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
822 devname, mddev, content->disk.raid_disk);
823 devices[devcnt].devname = devname;
824 devices[devcnt].uptodate = 0;
825 devices[devcnt].i = *content;
826 devices[devcnt].i.disk.major = major(stb.st_rdev);
827 devices[devcnt].i.disk.minor = minor(stb.st_rdev);
828 if (most_recent < devcnt) {
829 if (devices[devcnt].i.events
830 > devices[most_recent].i.events)
831 most_recent = devcnt;
832 }
833 if (content->array.level == LEVEL_MULTIPATH)
834 /* with multipath, the raid_disk from the superblock is meaningless */
835 i = devcnt;
836 else
837 i = devices[devcnt].i.disk.raid_disk;
838 if (i+1 == 0) {
839 if (nextspare < content->array.raid_disks)
840 nextspare = content->array.raid_disks;
841 i = nextspare++;
842 } else {
843 if (i >= content->array.raid_disks &&
844 i >= nextspare)
845 nextspare = i+1;
846 }
847 if (i < 10000) {
848 if (i >= bestcnt) {
849 int newbestcnt = i+10;
850 int *newbest = malloc(sizeof(int)*newbestcnt);
851 int c;
852 for (c=0; c < newbestcnt; c++)
853 if (c < bestcnt)
854 newbest[c] = best[c];
855 else
856 newbest[c] = -1;
857 if (best)free(best);
858 best = newbest;
859 bestcnt = newbestcnt;
860 }
861 if (best[i] >=0 &&
862 devices[best[i]].i.events
863 == devices[devcnt].i.events
864 && (devices[best[i]].i.disk.minor
865 != devices[devcnt].i.disk.minor)
866 && st->ss == &super0
867 && content->array.level != LEVEL_MULTIPATH) {
868 /* two different devices with identical superblock.
869 * Could be a mis-detection caused by overlapping
870 * partitions. fail-safe.
871 */
872 fprintf(stderr, Name ": WARNING %s and %s appear"
873 " to have very similar superblocks.\n"
874 " If they are really different, "
875 "please --zero the superblock on one\n"
876 " If they are the same or overlap,"
877 " please remove one from %s.\n",
878 devices[best[i]].devname, devname,
879 inargv ? "the list" :
880 "the\n DEVICE list in mdadm.conf"
881 );
882 close(mdfd);
883 free(devices);
884 free(devmap);
885 return 1;
886 }
887 if (best[i] == -1
888 || (devices[best[i]].i.events
889 < devices[devcnt].i.events))
890 best[i] = devcnt;
891 }
892 devcnt++;
893 }
894 free(content->update_private);
895 content->update_private = NULL;
896
897 if (devcnt == 0) {
898 fprintf(stderr, Name ": no devices found for %s\n",
899 mddev);
900 if (st)
901 st->ss->free_super(st);
902 close(mdfd);
903 free(devices);
904 free(devmap);
905 return 1;
906 }
907
908 if (update && strcmp(update, "byteorder")==0)
909 st->minor_version = 90;
910
911 st->ss->getinfo_super(st, content, NULL);
912 clean = content->array.state & 1;
913
914 /* now we have some devices that might be suitable.
915 * I wonder how many
916 */
917 avail = malloc(content->array.raid_disks);
918 memset(avail, 0, content->array.raid_disks);
919 okcnt = 0;
920 sparecnt=0;
921 rebuilding_cnt=0;
922 for (i=0; i< bestcnt; i++) {
923 int j = best[i];
924 int event_margin = 1; /* always allow a difference of '1'
925 * like the kernel does
926 */
927 if (j < 0) continue;
928 /* note: we ignore error flags in multipath arrays
929 * as they don't make sense
930 */
931 if (content->array.level != LEVEL_MULTIPATH)
932 if (!(devices[j].i.disk.state & (1<<MD_DISK_ACTIVE))) {
933 if (!(devices[j].i.disk.state
934 & (1<<MD_DISK_FAULTY))) {
935 devices[j].uptodate = 1;
936 sparecnt++;
937 }
938 continue;
939 }
940 /* If this devices thinks that 'most_recent' has failed, then
941 * we must reject this device.
942 */
943 if (j != most_recent &&
944 content->array.raid_disks > 0 &&
945 devices[most_recent].i.disk.raid_disk >= 0 &&
946 devmap[j * content->array.raid_disks + devices[most_recent].i.disk.raid_disk] == 0) {
947 if (verbose > -1)
948 fprintf(stderr, Name ": ignoring %s as it reports %s as failed\n",
949 devices[j].devname, devices[most_recent].devname);
950 best[i] = -1;
951 continue;
952 }
953 if (devices[j].i.events+event_margin >=
954 devices[most_recent].i.events) {
955 devices[j].uptodate = 1;
956 if (i < content->array.raid_disks) {
957 if (devices[j].i.recovery_start == MaxSector ||
958 (content->reshape_active &&
959 j >= content->array.raid_disks - content->delta_disks)) {
960 okcnt++;
961 avail[i]=1;
962 } else
963 rebuilding_cnt++;
964 } else
965 sparecnt++;
966 }
967 }
968 free(devmap);
969 while (force && !enough(content->array.level, content->array.raid_disks,
970 content->array.layout, 1,
971 avail, okcnt)) {
972 /* Choose the newest best drive which is
973 * not up-to-date, update the superblock
974 * and add it.
975 */
976 int fd;
977 struct supertype *tst;
978 unsigned long long current_events;
979 chosen_drive = -1;
980 for (i = 0; i < content->array.raid_disks && i < bestcnt; i++) {
981 int j = best[i];
982 if (j>=0 &&
983 !devices[j].uptodate &&
984 devices[j].i.recovery_start == MaxSector &&
985 (chosen_drive < 0 ||
986 devices[j].i.events
987 > devices[chosen_drive].i.events))
988 chosen_drive = j;
989 }
990 if (chosen_drive < 0)
991 break;
992 current_events = devices[chosen_drive].i.events;
993 add_another:
994 if (verbose >= 0)
995 fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
996 devices[chosen_drive].devname,
997 devices[chosen_drive].i.disk.raid_disk,
998 (int)(devices[chosen_drive].i.events),
999 (int)(devices[most_recent].i.events));
1000 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
1001 if (fd < 0) {
1002 fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
1003 devices[chosen_drive].devname);
1004 devices[chosen_drive].i.events = 0;
1005 continue;
1006 }
1007 tst = dup_super(st);
1008 if (tst->ss->load_super(tst,fd, NULL)) {
1009 close(fd);
1010 fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
1011 devices[chosen_drive].devname);
1012 devices[chosen_drive].i.events = 0;
1013 continue;
1014 }
1015 content->events = devices[most_recent].i.events;
1016 tst->ss->update_super(tst, content, "force-one",
1017 devices[chosen_drive].devname, verbose,
1018 0, NULL);
1019
1020 if (tst->ss->store_super(tst, fd)) {
1021 close(fd);
1022 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1023 devices[chosen_drive].devname);
1024 devices[chosen_drive].i.events = 0;
1025 tst->ss->free_super(tst);
1026 continue;
1027 }
1028 close(fd);
1029 devices[chosen_drive].i.events = devices[most_recent].i.events;
1030 devices[chosen_drive].uptodate = 1;
1031 avail[chosen_drive] = 1;
1032 okcnt++;
1033 tst->ss->free_super(tst);
1034
1035 /* If there are any other drives of the same vintage,
1036 * add them in as well. We can't lose and we might gain
1037 */
1038 for (i = 0; i < content->array.raid_disks && i < bestcnt ; i++) {
1039 int j = best[i];
1040 if (j >= 0 &&
1041 !devices[j].uptodate &&
1042 devices[j].i.events == current_events) {
1043 chosen_drive = j;
1044 goto add_another;
1045 }
1046 }
1047 }
1048
1049 /* Now we want to look at the superblock which the kernel will base things on
1050 * and compare the devices that we think are working with the devices that the
1051 * superblock thinks are working.
1052 * If there are differences and --force is given, then update this chosen
1053 * superblock.
1054 */
1055 chosen_drive = -1;
1056 st->ss->free_super(st);
1057 for (i=0; chosen_drive < 0 && i<bestcnt; i++) {
1058 int j = best[i];
1059 int fd;
1060
1061 if (j<0)
1062 continue;
1063 if (!devices[j].uptodate)
1064 continue;
1065 if (devices[j].i.events < devices[most_recent].i.events)
1066 continue;
1067 chosen_drive = j;
1068 if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
1069 fprintf(stderr, Name ": Cannot open %s: %s\n",
1070 devices[j].devname, strerror(errno));
1071 close(mdfd);
1072 free(devices);
1073 return 1;
1074 }
1075 if (st->ss->load_super(st,fd, NULL)) {
1076 close(fd);
1077 fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
1078 devices[j].devname);
1079 close(mdfd);
1080 free(devices);
1081 return 1;
1082 }
1083 close(fd);
1084 }
1085 if (st->sb == NULL) {
1086 fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
1087 close(mdfd);
1088 free(devices);
1089 return 1;
1090 }
1091 st->ss->getinfo_super(st, content, NULL);
1092 #ifndef MDASSEMBLE
1093 sysfs_init(content, mdfd, 0);
1094 #endif
1095 for (i=0; i<bestcnt; i++) {
1096 int j = best[i];
1097 unsigned int desired_state;
1098
1099 if (i < content->array.raid_disks)
1100 desired_state = (1<<MD_DISK_ACTIVE) | (1<<MD_DISK_SYNC);
1101 else
1102 desired_state = 0;
1103
1104 if (j<0)
1105 continue;
1106 if (!devices[j].uptodate)
1107 continue;
1108
1109 devices[j].i.disk.state = desired_state;
1110 if (!(devices[j].i.array.state & 1))
1111 clean = 0;
1112
1113 if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
1114 verbose, 0, NULL)) {
1115 if (force) {
1116 if (verbose >= 0)
1117 fprintf(stderr, Name ": "
1118 "clearing FAULTY flag for device %d in %s for %s\n",
1119 j, mddev, devices[j].devname);
1120 change = 1;
1121 } else {
1122 if (verbose >= -1)
1123 fprintf(stderr, Name ": "
1124 "device %d in %s has wrong state in superblock, but %s seems ok\n",
1125 i, mddev, devices[j].devname);
1126 }
1127 }
1128 #if 0
1129 if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
1130 fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
1131 i, mddev);
1132 }
1133 #endif
1134 }
1135 if (force && !clean &&
1136 !enough(content->array.level, content->array.raid_disks,
1137 content->array.layout, clean,
1138 avail, okcnt)) {
1139 change += st->ss->update_super(st, content, "force-array",
1140 devices[chosen_drive].devname, verbose,
1141 0, NULL);
1142 clean = 1;
1143 }
1144
1145 if (change) {
1146 int fd;
1147 fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
1148 if (fd < 0) {
1149 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1150 devices[chosen_drive].devname);
1151 close(mdfd);
1152 free(devices);
1153 return 1;
1154 }
1155 if (st->ss->store_super(st, fd)) {
1156 close(fd);
1157 fprintf(stderr, Name ": Could not re-write superblock on %s\n",
1158 devices[chosen_drive].devname);
1159 close(mdfd);
1160 free(devices);
1161 return 1;
1162 }
1163 if (verbose >= 0)
1164 fprintf(stderr, Name ": Marking array %s as 'clean'\n",
1165 mddev);
1166 close(fd);
1167 }
1168
1169 /* If we are in the middle of a reshape we may need to restore saved data
1170 * that was moved aside due to the reshape overwriting live data
1171 * The code of doing this lives in Grow.c
1172 */
1173 #ifndef MDASSEMBLE
1174 if (content->reshape_active) {
1175 int err = 0;
1176 int *fdlist = malloc(sizeof(int)* bestcnt);
1177 if (verbose > 0)
1178 fprintf(stderr, Name ":%s has an active reshape - checking "
1179 "if critical section needs to be restored\n",
1180 chosen_name);
1181 for (i=0; i<bestcnt; i++) {
1182 int j = best[i];
1183 if (j >= 0) {
1184 fdlist[i] = dev_open(devices[j].devname, O_RDWR|O_EXCL);
1185 if (fdlist[i] < 0) {
1186 fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
1187 devices[j].devname);
1188 err = 1;
1189 break;
1190 }
1191 } else
1192 fdlist[i] = -1;
1193 }
1194 if (!err) {
1195 if (st->ss->external && st->ss->recover_backup)
1196 err = st->ss->recover_backup(st, content);
1197 else
1198 err = Grow_restart(st, content, fdlist, bestcnt,
1199 backup_file, verbose > 0);
1200 if (err && invalid_backup) {
1201 if (verbose > 0)
1202 fprintf(stderr, Name ": continuing"
1203 " without restoring backup\n");
1204 err = 0;
1205 }
1206 }
1207 while (i>0) {
1208 i--;
1209 if (fdlist[i]>=0) close(fdlist[i]);
1210 }
1211 if (err) {
1212 fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
1213 if (backup_file == NULL)
1214 fprintf(stderr," Possibly you needed to specify the --backup-file\n");
1215 close(mdfd);
1216 free(devices);
1217 return err;
1218 }
1219 }
1220 #endif
1221 /* count number of in-sync devices according to the superblock.
1222 * We must have this number to start the array without -s or -R
1223 */
1224 req_cnt = content->array.working_disks;
1225
1226 /* Almost ready to actually *do* something */
1227 if (!old_linux) {
1228 int rv;
1229
1230 /* First, fill in the map, so that udev can find our name
1231 * as soon as we become active.
1232 */
1233 map_update(NULL, fd2devnum(mdfd), content->text_version,
1234 content->uuid, chosen_name);
1235
1236 rv = set_array_info(mdfd, st, content);
1237 if (rv) {
1238 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
1239 mddev, strerror(errno));
1240 ioctl(mdfd, STOP_ARRAY, NULL);
1241 close(mdfd);
1242 free(devices);
1243 return 1;
1244 }
1245 if (ident->bitmap_fd >= 0) {
1246 if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
1247 fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
1248 ioctl(mdfd, STOP_ARRAY, NULL);
1249 close(mdfd);
1250 free(devices);
1251 return 1;
1252 }
1253 } else if (ident->bitmap_file) {
1254 /* From config file */
1255 int bmfd = open(ident->bitmap_file, O_RDWR);
1256 if (bmfd < 0) {
1257 fprintf(stderr, Name ": Could not open bitmap file %s\n",
1258 ident->bitmap_file);
1259 ioctl(mdfd, STOP_ARRAY, NULL);
1260 close(mdfd);
1261 free(devices);
1262 return 1;
1263 }
1264 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
1265 fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
1266 close(bmfd);
1267 ioctl(mdfd, STOP_ARRAY, NULL);
1268 close(mdfd);
1269 free(devices);
1270 return 1;
1271 }
1272 close(bmfd);
1273 }
1274
1275 /* First, add the raid disks, but add the chosen one last */
1276 for (i=0; i<= bestcnt; i++) {
1277 int j;
1278 if (i < bestcnt) {
1279 j = best[i];
1280 if (j == chosen_drive)
1281 continue;
1282 } else
1283 j = chosen_drive;
1284
1285 if (j >= 0 /* && devices[j].uptodate */) {
1286 int dfd = dev_open(devices[j].devname,
1287 O_RDWR|O_EXCL);
1288 if (dfd >= 0) {
1289 remove_partitions(dfd);
1290 close(dfd);
1291 }
1292 rv = add_disk(mdfd, st, content, &devices[j].i);
1293
1294 if (rv) {
1295 fprintf(stderr, Name ": failed to add "
1296 "%s to %s: %s\n",
1297 devices[j].devname,
1298 mddev,
1299 strerror(errno));
1300 if (i < content->array.raid_disks
1301 || i == bestcnt)
1302 okcnt--;
1303 else
1304 sparecnt--;
1305 } else if (verbose > 0)
1306 fprintf(stderr, Name ": added %s "
1307 "to %s as %d\n",
1308 devices[j].devname, mddev,
1309 devices[j].i.disk.raid_disk);
1310 } else if (verbose > 0 && i < content->array.raid_disks)
1311 fprintf(stderr, Name ": no uptodate device for "
1312 "slot %d of %s\n",
1313 i, mddev);
1314 }
1315
1316 if (content->array.level == LEVEL_CONTAINER) {
1317 if (verbose >= 0) {
1318 fprintf(stderr, Name ": Container %s has been "
1319 "assembled with %d drive%s",
1320 mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
1321 if (okcnt < (unsigned)content->array.raid_disks)
1322 fprintf(stderr, " (out of %d)",
1323 content->array.raid_disks);
1324 fprintf(stderr, "\n");
1325 }
1326 st->ss->free_super(st);
1327 sysfs_uevent(content, "change");
1328 wait_for(chosen_name, mdfd);
1329 close(mdfd);
1330 free(devices);
1331 return 0;
1332 }
1333
1334 if (runstop == 1 ||
1335 (runstop <= 0 &&
1336 ( enough(content->array.level, content->array.raid_disks,
1337 content->array.layout, clean, avail, okcnt) &&
1338 (okcnt + rebuilding_cnt >= req_cnt || start_partial_ok)
1339 ))) {
1340 /* This array is good-to-go.
1341 * If a reshape is in progress then we might need to
1342 * continue monitoring it. In that case we start
1343 * it read-only and let the grow code make it writable.
1344 */
1345 int rv;
1346 #ifndef MDASSEMBLE
1347 if (content->reshape_active &&
1348 content->delta_disks <= 0) {
1349 rv = sysfs_set_str(content, NULL,
1350 "array_state", "readonly");
1351 if (rv == 0)
1352 rv = Grow_continue(mdfd, st, content,
1353 backup_file,
1354 freeze_reshape);
1355 } else
1356 #endif
1357 rv = ioctl(mdfd, RUN_ARRAY, NULL);
1358 if (rv == 0) {
1359 if (verbose >= 0) {
1360 fprintf(stderr, Name ": %s has been started with %d drive%s",
1361 mddev, okcnt, okcnt==1?"":"s");
1362 if (okcnt < (unsigned)content->array.raid_disks)
1363 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1364 if (rebuilding_cnt)
1365 fprintf(stderr, "%s %d rebuilding", sparecnt?",":" and", rebuilding_cnt);
1366 if (sparecnt)
1367 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1368 fprintf(stderr, ".\n");
1369 }
1370 if (content->reshape_active &&
1371 content->array.level >= 4 &&
1372 content->array.level <= 6) {
1373 /* might need to increase the size
1374 * of the stripe cache - default is 256
1375 */
1376 if (256 < 4 * (content->array.chunk_size/4096)) {
1377 struct mdinfo *sra = sysfs_read(mdfd, 0, 0);
1378 if (sra)
1379 sysfs_set_num(sra, NULL,
1380 "stripe_cache_size",
1381 (4 * content->array.chunk_size / 4096) + 1);
1382 sysfs_free(sra);
1383 }
1384 }
1385 if (okcnt < (unsigned)content->array.raid_disks) {
1386 /* If any devices did not get added
1387 * because the kernel rejected them based
1388 * on event count, try adding them
1389 * again providing the action policy is
1390 * 're-add' or greater. The bitmap
1391 * might allow them to be included, or
1392 * they will become spares.
1393 */
1394 for (i = 0; i < bestcnt; i++) {
1395 int j = best[i];
1396 if (j >= 0 && !devices[j].uptodate) {
1397 if (!disk_action_allows(&devices[j].i, st->ss->name, act_re_add))
1398 continue;
1399 rv = add_disk(mdfd, st, content,
1400 &devices[j].i);
1401 if (rv == 0 && verbose >= 0)
1402 fprintf(stderr,
1403 Name ": %s has been re-added.\n",
1404 devices[j].devname);
1405 }
1406 }
1407 }
1408 wait_for(mddev, mdfd);
1409 close(mdfd);
1410 if (auto_assem) {
1411 int usecs = 1;
1412 /* There is a nasty race with 'mdadm --monitor'.
1413 * If it opens this device before we close it,
1414 * it gets an incomplete open on which IO
1415 * doesn't work and the capacity is
1416 * wrong.
1417 * If we reopen (to check for layered devices)
1418 * before --monitor closes, we loose.
1419 *
1420 * So: wait upto 1 second for there to be
1421 * a non-zero capacity.
1422 */
1423 while (usecs < 1000) {
1424 mdfd = open(mddev, O_RDONLY);
1425 if (mdfd >= 0) {
1426 unsigned long long size;
1427 if (get_dev_size(mdfd, NULL, &size) &&
1428 size > 0)
1429 break;
1430 close(mdfd);
1431 }
1432 usleep(usecs);
1433 usecs <<= 1;
1434 }
1435 }
1436 free(devices);
1437 return 0;
1438 }
1439 fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n",
1440 mddev, strerror(errno));
1441
1442 if (!enough(content->array.level, content->array.raid_disks,
1443 content->array.layout, 1, avail, okcnt))
1444 fprintf(stderr, Name ": Not enough devices to "
1445 "start the array.\n");
1446 else if (!enough(content->array.level,
1447 content->array.raid_disks,
1448 content->array.layout, clean,
1449 avail, okcnt))
1450 fprintf(stderr, Name ": Not enough devices to "
1451 "start the array while not clean "
1452 "- consider --force.\n");
1453
1454 if (auto_assem)
1455 ioctl(mdfd, STOP_ARRAY, NULL);
1456 close(mdfd);
1457 free(devices);
1458 return 1;
1459 }
1460 if (runstop == -1) {
1461 fprintf(stderr, Name ": %s assembled from %d drive%s",
1462 mddev, okcnt, okcnt==1?"":"s");
1463 if (okcnt != (unsigned)content->array.raid_disks)
1464 fprintf(stderr, " (out of %d)", content->array.raid_disks);
1465 fprintf(stderr, ", but not started.\n");
1466 close(mdfd);
1467 free(devices);
1468 return 0;
1469 }
1470 if (verbose >= -1) {
1471 fprintf(stderr, Name ": %s assembled from %d drive%s", mddev, okcnt, okcnt==1?"":"s");
1472 if (rebuilding_cnt)
1473 fprintf(stderr, "%s %d rebuilding", sparecnt?", ":" and ", rebuilding_cnt);
1474 if (sparecnt)
1475 fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
1476 if (!enough(content->array.level, content->array.raid_disks,
1477 content->array.layout, 1, avail, okcnt))
1478 fprintf(stderr, " - not enough to start the array.\n");
1479 else if (!enough(content->array.level,
1480 content->array.raid_disks,
1481 content->array.layout, clean,
1482 avail, okcnt))
1483 fprintf(stderr, " - not enough to start the "
1484 "array while not clean - consider "
1485 "--force.\n");
1486 else {
1487 if (req_cnt == (unsigned)content->array.raid_disks)
1488 fprintf(stderr, " - need all %d to start it", req_cnt);
1489 else
1490 fprintf(stderr, " - need %d of %d to start", req_cnt, content->array.raid_disks);
1491 fprintf(stderr, " (use --run to insist).\n");
1492 }
1493 }
1494 if (auto_assem)
1495 ioctl(mdfd, STOP_ARRAY, NULL);
1496 close(mdfd);
1497 free(devices);
1498 return 1;
1499 } else {
1500 /* The "chosen_drive" is a good choice, and if necessary, the superblock has
1501 * been updated to point to the current locations of devices.
1502 * so we can just start the array
1503 */
1504 unsigned long dev;
1505 dev = makedev(devices[chosen_drive].i.disk.major,
1506 devices[chosen_drive].i.disk.minor);
1507 if (ioctl(mdfd, START_ARRAY, dev)) {
1508 fprintf(stderr, Name ": Cannot start array: %s\n",
1509 strerror(errno));
1510 }
1511
1512 }
1513 close(mdfd);
1514 free(devices);
1515 return 0;
1516 }
1517
1518 #ifndef MDASSEMBLE
1519 int assemble_container_content(struct supertype *st, int mdfd,
1520 struct mdinfo *content, int runstop,
1521 char *chosen_name, int verbose,
1522 char *backup_file, int freeze_reshape)
1523 {
1524 struct mdinfo *dev, *sra;
1525 int working = 0, preexist = 0;
1526 int expansion = 0;
1527 struct map_ent *map = NULL;
1528 int old_raid_disks;
1529
1530 sysfs_init(content, mdfd, 0);
1531
1532 sra = sysfs_read(mdfd, 0, GET_VERSION);
1533 if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
1534 if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
1535 if (sra)
1536 sysfs_free(sra);
1537 return 1;
1538 }
1539
1540 if (st->ss->external && content->recovery_blocked)
1541 block_subarray(content);
1542
1543 if (sra)
1544 sysfs_free(sra);
1545 old_raid_disks = content->array.raid_disks - content->delta_disks;
1546 for (dev = content->devs; dev; dev = dev->next)
1547 if (sysfs_add_disk(content, dev, 1) == 0) {
1548 if (dev->disk.raid_disk >= old_raid_disks &&
1549 content->reshape_active)
1550 expansion++;
1551 else
1552 working++;
1553 } else if (errno == EEXIST)
1554 preexist++;
1555 if (working == 0)
1556 return 1;/* Nothing new, don't try to start */
1557
1558 map_update(&map, fd2devnum(mdfd),
1559 content->text_version,
1560 content->uuid, chosen_name);
1561
1562 if (runstop > 0 ||
1563 (working + preexist + expansion) >=
1564 content->array.working_disks) {
1565 int err;
1566 int start_reshape;
1567
1568 /* There are two types of reshape: container wide or sub-array specific
1569 * Check if metadata requests blocking container wide reshapes
1570 */
1571 start_reshape = (content->reshape_active &&
1572 !((content->reshape_active == CONTAINER_RESHAPE) &&
1573 (content->array.state & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))));
1574 if (start_reshape) {
1575 int spare = content->array.raid_disks + expansion;
1576 if (restore_backup(st, content,
1577 working,
1578 spare, backup_file, verbose) == 1)
1579 return 1;
1580
1581 err = sysfs_set_str(content, NULL,
1582 "array_state", "readonly");
1583 if (err)
1584 return 1;
1585
1586 if (st->ss->external) {
1587 if (!mdmon_running(st->container_dev))
1588 start_mdmon(st->container_dev);
1589 ping_monitor_by_id(st->container_dev);
1590 if (mdmon_running(st->container_dev) &&
1591 st->update_tail == NULL)
1592 st->update_tail = &st->updates;
1593 }
1594
1595 err = Grow_continue(mdfd, st, content, backup_file,
1596 freeze_reshape);
1597 } else switch(content->array.level) {
1598 case LEVEL_LINEAR:
1599 case LEVEL_MULTIPATH:
1600 case 0:
1601 err = sysfs_set_str(content, NULL, "array_state",
1602 "active");
1603 break;
1604 default:
1605 err = sysfs_set_str(content, NULL, "array_state",
1606 "readonly");
1607 /* start mdmon if needed. */
1608 if (!err) {
1609 if (!mdmon_running(st->container_dev))
1610 start_mdmon(st->container_dev);
1611 ping_monitor_by_id(st->container_dev);
1612 }
1613 break;
1614 }
1615 if (!err)
1616 sysfs_set_safemode(content, content->safe_mode_delay);
1617 if (verbose >= 0) {
1618 if (err)
1619 fprintf(stderr, Name
1620 ": array %s now has %d devices",
1621 chosen_name, working + preexist);
1622 else
1623 fprintf(stderr, Name
1624 ": Started %s with %d devices",
1625 chosen_name, working + preexist);
1626 if (preexist)
1627 fprintf(stderr, " (%d new)", working);
1628 if (expansion)
1629 fprintf(stderr, " ( + %d for expansion)",
1630 expansion);
1631 fprintf(stderr, "\n");
1632 }
1633 if (!err)
1634 wait_for(chosen_name, mdfd);
1635 return err;
1636 /* FIXME should have an O_EXCL and wait for read-auto */
1637 } else {
1638 if (verbose >= 0)
1639 fprintf(stderr, Name
1640 ": %s assembled with %d devices but "
1641 "not started\n",
1642 chosen_name, working);
1643 return 1;
1644 }
1645 }
1646 #endif
1647