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