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