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