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