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