]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Incremental: honor --no-degraded to delay assembly
[thirdparty/mdadm.git] / Incremental.c
1 /*
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Author: Neil Brown
23 * Email: <neilb@suse.de>
24 * Paper: Neil Brown
25 * Novell Inc
26 * GPO Box Q1283
27 * QVB Post Office, NSW 1230
28 * Australia
29 */
30
31 #include "mdadm.h"
32
33 static int count_active(struct supertype *st, int mdfd, char **availp,
34 struct mdinfo *info);
35 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
36 int number, __u64 events, int verbose,
37 char *array_name);
38
39 int Incremental(char *devname, int verbose, int runstop,
40 struct supertype *st, char *homehost, int autof)
41 {
42 /* Add this device to an array, creating the array if necessary
43 * and starting the array if sensible or - if runstop>0 - if possible.
44 *
45 * This has several steps:
46 *
47 * 1/ Check if device is permitted by mdadm.conf, reject if not.
48 * 2/ Find metadata, reject if none appropriate (check
49 * version/name from args)
50 * 3/ Check if there is a match in mdadm.conf
51 * 3a/ if not, check for homehost match. If no match, assemble as
52 * a 'foreign' array.
53 * 4/ Determine device number.
54 * - If in mdadm.conf with std name, use that
55 * - UUID in /var/run/mdadm.map use that
56 * - If name is suggestive, use that. unless in use with different uuid.
57 * - Choose a free, high number.
58 * - Use a partitioned device unless strong suggestion not to.
59 * e.g. auto=md
60 * Don't choose partitioned for containers.
61 * 5/ Find out if array already exists
62 * 5a/ if it does not
63 * - choose a name, from mdadm.conf or 'name' field in array.
64 * - create the array
65 * - add the device
66 * 5b/ if it does
67 * - check one drive in array to make sure metadata is a reasonably
68 * close match. Reject if not (e.g. different type)
69 * - add the device
70 * 6/ Make sure /var/run/mdadm.map contains this array.
71 * 7/ Is there enough devices to possibly start the array?
72 * For a container, this means running Incremental_container.
73 * 7a/ if not, finish with success.
74 * 7b/ if yes,
75 * - read all metadata and arrange devices like -A does
76 * - if number of OK devices match expected, or -R and there are enough,
77 * start the array (auto-readonly).
78 */
79 struct stat stb;
80 struct mdinfo info;
81 struct mddev_ident_s *array_list, *match;
82 char chosen_name[1024];
83 int rv;
84 struct map_ent *mp, *map = NULL;
85 int dfd, mdfd;
86 char *avail;
87 int active_disks;
88 int trustworthy = FOREIGN;
89 char *name_to_use;
90 mdu_array_info_t ainf;
91
92 struct createinfo *ci = conf_get_create_info();
93
94
95 /* 1/ Check if device is permitted by mdadm.conf */
96
97 if (!conf_test_dev(devname)) {
98 if (verbose >= 0)
99 fprintf(stderr, Name
100 ": %s not permitted by mdadm.conf.\n",
101 devname);
102 return 1;
103 }
104
105 /* 2/ Find metadata, reject if none appropriate (check
106 * version/name from args) */
107
108 dfd = dev_open(devname, O_RDONLY|O_EXCL);
109 if (dfd < 0) {
110 if (verbose >= 0)
111 fprintf(stderr, Name ": cannot open %s: %s.\n",
112 devname, strerror(errno));
113 return 1;
114 }
115 if (fstat(dfd, &stb) < 0) {
116 if (verbose >= 0)
117 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
118 devname, strerror(errno));
119 close(dfd);
120 return 1;
121 }
122 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
123 if (verbose >= 0)
124 fprintf(stderr, Name ": %s is not a block device.\n",
125 devname);
126 close(dfd);
127 return 1;
128 }
129
130 if (st == NULL && (st = guess_super(dfd)) == NULL) {
131 if (verbose >= 0)
132 fprintf(stderr, Name
133 ": no recognisable superblock on %s.\n",
134 devname);
135 close(dfd);
136 return 1;
137 }
138 if (st->ss->load_super(st, dfd, NULL)) {
139 if (verbose >= 0)
140 fprintf(stderr, Name ": no RAID superblock on %s.\n",
141 devname);
142 close(dfd);
143 return 1;
144 }
145 close (dfd);
146
147 memset(&info, 0, sizeof(info));
148 st->ss->getinfo_super(st, &info);
149 /* 3/ Check if there is a match in mdadm.conf */
150
151 array_list = conf_get_ident(NULL);
152 match = NULL;
153 for (; array_list; array_list = array_list->next) {
154 if (array_list->uuid_set &&
155 same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid)
156 == 0) {
157 if (verbose >= 2 && array_list->devname)
158 fprintf(stderr, Name
159 ": UUID differs from %s.\n",
160 array_list->devname);
161 continue;
162 }
163 if (array_list->name[0] &&
164 strcasecmp(array_list->name, info.name) != 0) {
165 if (verbose >= 2 && array_list->devname)
166 fprintf(stderr, Name
167 ": Name differs from %s.\n",
168 array_list->devname);
169 continue;
170 }
171 if (array_list->devices &&
172 !match_oneof(array_list->devices, devname)) {
173 if (verbose >= 2 && array_list->devname)
174 fprintf(stderr, Name
175 ": Not a listed device for %s.\n",
176 array_list->devname);
177 continue;
178 }
179 if (array_list->super_minor != UnSet &&
180 array_list->super_minor != info.array.md_minor) {
181 if (verbose >= 2 && array_list->devname)
182 fprintf(stderr, Name
183 ": Different super-minor to %s.\n",
184 array_list->devname);
185 continue;
186 }
187 if (!array_list->uuid_set &&
188 !array_list->name[0] &&
189 !array_list->devices &&
190 array_list->super_minor == UnSet) {
191 if (verbose >= 2 && array_list->devname)
192 fprintf(stderr, Name
193 ": %s doesn't have any identifying information.\n",
194 array_list->devname);
195 continue;
196 }
197 /* FIXME, should I check raid_disks and level too?? */
198
199 if (match) {
200 if (verbose >= 0) {
201 if (match->devname && array_list->devname)
202 fprintf(stderr, Name
203 ": we match both %s and %s - cannot decide which to use.\n",
204 match->devname, array_list->devname);
205 else
206 fprintf(stderr, Name
207 ": multiple lines in mdadm.conf match\n");
208 }
209 return 2;
210 }
211 match = array_list;
212 }
213
214 /* 3a/ if not, check for homehost match. If no match, continue
215 * but don't trust the 'name' in the array. Thus a 'random' minor
216 * number will be assigned, and the device name will be based
217 * on that. */
218 if (match)
219 trustworthy = LOCAL;
220 else if (homehost == NULL ||
221 st->ss->match_home(st, homehost) != 1)
222 trustworthy = FOREIGN;
223 name_to_use = strchr(info.name, ':');
224 if (name_to_use)
225 name_to_use++;
226 else
227 name_to_use = info.name;
228
229 if ((!name_to_use || name_to_use[0] == 0) &&
230 info.array.level == LEVEL_CONTAINER &&
231 trustworthy == LOCAL) {
232 name_to_use = info.text_version;
233 trustworthy = METADATA;
234 }
235
236
237 /* There are three possible sources for 'autof': command line,
238 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
239 * ARRAY takes precedence, then command line, then
240 * CREATE.
241 */
242 if (match && match->autof)
243 autof = match->autof;
244 if (autof == 0)
245 autof = ci->autof;
246
247 if (st->ss->container_content && st->loaded_container) {
248 /* This is a pre-built container array, so we do something
249 * rather different.
250 */
251 return Incremental_container(st, devname, verbose, runstop,
252 autof, trustworthy);
253 }
254 /* 4/ Check if array exists.
255 */
256 map_lock(&map);
257 mp = map_by_uuid(&map, info.uuid);
258 if (mp) {
259 mdfd = open_mddev(mp->path, 0);
260 if (mdfd < 0 && mddev_busy(mp->devnum)) {
261 /* maybe udev hasn't created it yet. */
262 char buf[50];
263 sprintf(buf, "%d:%d", dev2major(mp->devnum),
264 dev2minor(mp->devnum));
265 mdfd = dev_open(buf, O_RDWR);
266 }
267 } else
268 mdfd = -1;
269
270 if (mdfd < 0) {
271 struct mdinfo *sra;
272 struct mdinfo dinfo;
273
274 /* Couldn't find an existing array, maybe make a new one */
275 mdfd = create_mddev(match ? match->devname : NULL,
276 name_to_use, autof, trustworthy, chosen_name);
277
278 if (mdfd < 0)
279 return 1;
280
281 sysfs_init(&info, mdfd, 0);
282
283 if (set_array_info(mdfd, st, &info) != 0) {
284 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
285 chosen_name, strerror(errno));
286 close(mdfd);
287 return 2;
288 }
289
290 dinfo = info;
291 dinfo.disk.major = major(stb.st_rdev);
292 dinfo.disk.minor = minor(stb.st_rdev);
293 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
294 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
295 devname, chosen_name, strerror(errno));
296 ioctl(mdfd, STOP_ARRAY, 0);
297 close(mdfd);
298 return 2;
299 }
300 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
301 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
302 /* It really should be 'none' - must be old buggy
303 * kernel, and mdadm -I may not be able to complete.
304 * So reject it.
305 */
306 ioctl(mdfd, STOP_ARRAY, NULL);
307 fprintf(stderr, Name
308 ": You have an old buggy kernel which cannot support\n"
309 " --incremental reliably. Aborting.\n");
310 close(mdfd);
311 sysfs_free(sra);
312 return 2;
313 }
314 info.array.working_disks = 1;
315 sysfs_free(sra);
316 /* 6/ Make sure /var/run/mdadm.map contains this array. */
317 map_update(&map, fd2devnum(mdfd),
318 info.text_version,
319 info.uuid, chosen_name);
320 } else {
321 /* 5b/ if it does */
322 /* - check one drive in array to make sure metadata is a reasonably */
323 /* close match. Reject if not (e.g. different type) */
324 /* - add the device */
325 char dn[20];
326 int dfd2;
327 int err;
328 struct mdinfo *sra;
329 struct supertype *st2;
330 struct mdinfo info2, *d;
331
332 strcpy(chosen_name, mp->path);
333
334 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
335
336 sprintf(dn, "%d:%d", sra->devs->disk.major,
337 sra->devs->disk.minor);
338 dfd2 = dev_open(dn, O_RDONLY);
339 st2 = dup_super(st);
340 if (st2->ss->load_super(st2, dfd2, NULL) ||
341 st->ss->compare_super(st, st2) != 0) {
342 fprintf(stderr, Name
343 ": metadata mismatch between %s and "
344 "chosen array %s\n",
345 devname, chosen_name);
346 close(mdfd);
347 close(dfd2);
348 return 2;
349 }
350 close(dfd2);
351 memset(&info2, 0, sizeof(info2));
352 st2->ss->getinfo_super(st2, &info2);
353 st2->ss->free_super(st2);
354 if (info.array.level != info2.array.level ||
355 memcmp(info.uuid, info2.uuid, 16) != 0 ||
356 info.array.raid_disks != info2.array.raid_disks) {
357 fprintf(stderr, Name
358 ": unexpected difference between %s and %s.\n",
359 chosen_name, devname);
360 close(mdfd);
361 return 2;
362 }
363 info2.disk.major = major(stb.st_rdev);
364 info2.disk.minor = minor(stb.st_rdev);
365 /* add disk needs to know about containers */
366 if (st->ss->external)
367 sra->array.level = LEVEL_CONTAINER;
368 err = add_disk(mdfd, st2, sra, &info2);
369 if (err < 0 && errno == EBUSY) {
370 /* could be another device present with the same
371 * disk.number. Find and reject any such
372 */
373 find_reject(mdfd, st, sra, info.disk.number,
374 info.events, verbose, chosen_name);
375 err = add_disk(mdfd, st2, sra, &info2);
376 }
377 if (err < 0) {
378 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
379 devname, chosen_name, strerror(errno));
380 close(mdfd);
381 return 2;
382 }
383 info.array.working_disks = 0;
384 for (d = sra->devs; d; d=d->next)
385 info.array.working_disks ++;
386
387 }
388
389 /* 7/ Is there enough devices to possibly start the array? */
390 /* 7a/ if not, finish with success. */
391 if (info.array.level == LEVEL_CONTAINER) {
392 /* Try to assemble within the container */
393 close(mdfd);
394 map_unlock(&map);
395 sysfs_uevent(&info, "change");
396 if (verbose >= 0)
397 fprintf(stderr, Name
398 ": container %s now has %d devices\n",
399 chosen_name, info.array.working_disks);
400 wait_for(chosen_name);
401 if (runstop < 0)
402 return 0; /* don't try to assemble */
403 return Incremental(chosen_name, verbose, runstop,
404 NULL, homehost, autof);
405 }
406 avail = NULL;
407 active_disks = count_active(st, mdfd, &avail, &info);
408 if (enough(info.array.level, info.array.raid_disks,
409 info.array.layout, info.array.state & 1,
410 avail, active_disks) == 0 ||
411 (runstop < 0 && active_disks < info.array.raid_disks)) {
412 free(avail);
413 if (verbose >= 0)
414 fprintf(stderr, Name
415 ": %s attached to %s, not enough to start (%d).\n",
416 devname, chosen_name, active_disks);
417 map_unlock(&map);
418 close(mdfd);
419 return 0;
420 }
421 free(avail);
422
423 /* 7b/ if yes, */
424 /* - if number of OK devices match expected, or -R and there */
425 /* are enough, */
426 /* + add any bitmap file */
427 /* + start the array (auto-readonly). */
428
429 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
430 if (verbose >= 0)
431 fprintf(stderr, Name
432 ": %s attached to %s which is already active.\n",
433 devname, chosen_name);
434 close(mdfd);
435 map_unlock(&map);
436 return 0;
437 }
438
439 map_unlock(&map);
440 if (runstop > 0 || active_disks >= info.array.working_disks) {
441 struct mdinfo *sra;
442 /* Let's try to start it */
443 if (match && match->bitmap_file) {
444 int bmfd = open(match->bitmap_file, O_RDWR);
445 if (bmfd < 0) {
446 fprintf(stderr, Name
447 ": Could not open bitmap file %s.\n",
448 match->bitmap_file);
449 close(mdfd);
450 return 1;
451 }
452 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
453 close(bmfd);
454 fprintf(stderr, Name
455 ": Failed to set bitmapfile for %s.\n",
456 chosen_name);
457 close(mdfd);
458 return 1;
459 }
460 close(bmfd);
461 }
462 sra = sysfs_read(mdfd, fd2devnum(mdfd), 0);
463 if ((sra == NULL || active_disks >= info.array.working_disks)
464 && trustworthy != FOREIGN)
465 rv = ioctl(mdfd, RUN_ARRAY, NULL);
466 else
467 rv = sysfs_set_str(sra, NULL,
468 "array_state", "read-auto");
469 if (rv == 0) {
470 if (verbose >= 0)
471 fprintf(stderr, Name
472 ": %s attached to %s, which has been started.\n",
473 devname, chosen_name);
474 rv = 0;
475 wait_for(chosen_name);
476 } else {
477 fprintf(stderr, Name
478 ": %s attached to %s, but failed to start: %s.\n",
479 devname, chosen_name, strerror(errno));
480 rv = 1;
481 }
482 } else {
483 if (verbose >= 0)
484 fprintf(stderr, Name
485 ": %s attached to %s, not enough to start safely.\n",
486 devname, chosen_name);
487 rv = 0;
488 }
489 close(mdfd);
490 return rv;
491 }
492
493 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
494 int number, __u64 events, int verbose,
495 char *array_name)
496 {
497 /* Find a device attached to this array with a disk.number of number
498 * and events less than the passed events, and remove the device.
499 */
500 struct mdinfo *d;
501 mdu_array_info_t ra;
502
503 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
504 return; /* not safe to remove from active arrays
505 * without thinking more */
506
507 for (d = sra->devs; d ; d = d->next) {
508 char dn[10];
509 int dfd;
510 struct mdinfo info;
511 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
512 dfd = dev_open(dn, O_RDONLY);
513 if (dfd < 0)
514 continue;
515 if (st->ss->load_super(st, dfd, NULL)) {
516 close(dfd);
517 continue;
518 }
519 st->ss->getinfo_super(st, &info);
520 st->ss->free_super(st);
521 close(dfd);
522
523 if (info.disk.number != number ||
524 info.events >= events)
525 continue;
526
527 if (d->disk.raid_disk > -1)
528 sysfs_set_str(sra, d, "slot", "none");
529 if (sysfs_set_str(sra, d, "state", "remove") == 0)
530 if (verbose >= 0)
531 fprintf(stderr, Name
532 ": removing old device %s from %s\n",
533 d->sys_name+4, array_name);
534 }
535 }
536
537 static int count_active(struct supertype *st, int mdfd, char **availp,
538 struct mdinfo *bestinfo)
539 {
540 /* count how many devices in sra think they are active */
541 struct mdinfo *d;
542 int cnt = 0, cnt1 = 0;
543 __u64 max_events = 0;
544 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
545 char *avail = NULL;
546
547 for (d = sra->devs ; d ; d = d->next) {
548 char dn[30];
549 int dfd;
550 int ok;
551 struct mdinfo info;
552
553 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
554 dfd = dev_open(dn, O_RDONLY);
555 if (dfd < 0)
556 continue;
557 ok = st->ss->load_super(st, dfd, NULL);
558 close(dfd);
559 if (ok != 0)
560 continue;
561 st->ss->getinfo_super(st, &info);
562 if (!avail) {
563 avail = malloc(info.array.raid_disks);
564 if (!avail) {
565 fprintf(stderr, Name ": out of memory.\n");
566 exit(1);
567 }
568 memset(avail, 0, info.array.raid_disks);
569 *availp = avail;
570 }
571
572 if (info.disk.state & (1<<MD_DISK_SYNC))
573 {
574 if (cnt == 0) {
575 cnt++;
576 max_events = info.events;
577 avail[info.disk.raid_disk] = 2;
578 st->ss->getinfo_super(st, bestinfo);
579 } else if (info.events == max_events) {
580 cnt++;
581 avail[info.disk.raid_disk] = 2;
582 } else if (info.events == max_events-1) {
583 cnt1++;
584 avail[info.disk.raid_disk] = 1;
585 } else if (info.events < max_events - 1)
586 ;
587 else if (info.events == max_events+1) {
588 int i;
589 cnt1 = cnt;
590 cnt = 1;
591 max_events = info.events;
592 for (i=0; i<info.array.raid_disks; i++)
593 if (avail[i])
594 avail[i]--;
595 avail[info.disk.raid_disk] = 2;
596 st->ss->getinfo_super(st, bestinfo);
597 } else { /* info.events much bigger */
598 cnt = 1; cnt1 = 0;
599 memset(avail, 0, info.disk.raid_disk);
600 max_events = info.events;
601 st->ss->getinfo_super(st, bestinfo);
602 }
603 }
604 st->ss->free_super(st);
605 }
606 return cnt + cnt1;
607 }
608
609 int IncrementalScan(int verbose)
610 {
611 /* look at every device listed in the 'map' file.
612 * If one is found that is not running then:
613 * look in mdadm.conf for bitmap file.
614 * if one exists, but array has none, add it.
615 * try to start array in auto-readonly mode
616 */
617 struct map_ent *mapl = NULL;
618 struct map_ent *me;
619 mddev_ident_t devs, mddev;
620 int rv = 0;
621
622 map_read(&mapl);
623 devs = conf_get_ident(NULL);
624
625 for (me = mapl ; me ; me = me->next) {
626 mdu_array_info_t array;
627 mdu_bitmap_file_t bmf;
628 struct mdinfo *sra;
629 int mdfd = open_mddev(me->path, 0);
630
631 if (mdfd < 0)
632 continue;
633 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
634 errno != ENODEV) {
635 close(mdfd);
636 continue;
637 }
638 /* Ok, we can try this one. Maybe it needs a bitmap */
639 for (mddev = devs ; mddev ; mddev = mddev->next)
640 if (mddev->devname
641 && strcmp(mddev->devname, me->path) == 0)
642 break;
643 if (mddev && mddev->bitmap_file) {
644 /*
645 * Note: early kernels will wrongly fail this, so it
646 * is a hint only
647 */
648 int added = -1;
649 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
650 int bmfd = open(mddev->bitmap_file, O_RDWR);
651 if (bmfd >= 0) {
652 added = ioctl(mdfd, SET_BITMAP_FILE,
653 bmfd);
654 close(bmfd);
655 }
656 }
657 if (verbose >= 0) {
658 if (added == 0)
659 fprintf(stderr, Name
660 ": Added bitmap %s to %s\n",
661 mddev->bitmap_file, me->path);
662 else if (errno != EEXIST)
663 fprintf(stderr, Name
664 ": Failed to add bitmap to %s: %s\n",
665 me->path, strerror(errno));
666 }
667 }
668 sra = sysfs_read(mdfd, 0, 0);
669 if (sra) {
670 if (sysfs_set_str(sra, NULL,
671 "array_state", "read-auto") == 0) {
672 if (verbose >= 0)
673 fprintf(stderr, Name
674 ": started array %s\n",
675 me->path);
676 } else {
677 fprintf(stderr, Name
678 ": failed to start array %s: %s\n",
679 me->path, strerror(errno));
680 rv = 1;
681 }
682 }
683 }
684 return rv;
685 }
686
687 static char *container2devname(char *devname)
688 {
689 char *mdname = NULL;
690
691 if (devname[0] == '/') {
692 int fd = open(devname, O_RDONLY);
693 if (fd >= 0) {
694 mdname = devnum2devname(fd2devnum(fd));
695 close(fd);
696 }
697 } else {
698 int uuid[4];
699 struct map_ent *mp, *map = NULL;
700
701 if (!parse_uuid(devname, uuid))
702 return mdname;
703 mp = map_by_uuid(&map, uuid);
704 if (mp)
705 mdname = devnum2devname(mp->devnum);
706 map_free(map);
707 }
708
709 return mdname;
710 }
711
712 int Incremental_container(struct supertype *st, char *devname, int verbose,
713 int runstop, int autof, int trustworthy)
714 {
715 /* Collect the contents of this container and for each
716 * array, choose a device name and assemble the array.
717 */
718
719 struct mdinfo *list = st->ss->container_content(st);
720 struct mdinfo *ra;
721 struct map_ent *map = NULL;
722
723 map_lock(&map);
724
725 for (ra = list ; ra ; ra = ra->next) {
726 int mdfd;
727 char chosen_name[1024];
728 struct map_ent *mp;
729 struct mddev_ident_s *match = NULL;
730 int err;
731
732 mp = map_by_uuid(&map, ra->uuid);
733
734 if (!mp) {
735
736 /* Check in mdadm.conf for devices == devname and
737 * member == ra->text_version after second slash.
738 */
739 char *sub = strchr(ra->text_version+1, '/');
740 struct mddev_ident_s *array_list;
741 if (sub) {
742 sub++;
743 array_list = conf_get_ident(NULL);
744 } else
745 array_list = NULL;
746 for(; array_list ; array_list = array_list->next) {
747 char *dn;
748 if (array_list->member == NULL ||
749 array_list->container == NULL)
750 continue;
751 if (strcmp(array_list->member, sub) != 0)
752 continue;
753 if (array_list->uuid_set &&
754 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
755 continue;
756 dn = container2devname(array_list->container);
757 if (dn == NULL)
758 continue;
759 if (strncmp(dn, ra->text_version+1,
760 strlen(dn)) != 0 ||
761 ra->text_version[strlen(dn)+1] != '/') {
762 free(dn);
763 continue;
764 }
765 free(dn);
766 /* we have a match */
767 match = array_list;
768 if (verbose>0)
769 fprintf(stderr, Name ": match found for member %s\n",
770 array_list->member);
771 break;
772 }
773 }
774
775 mdfd = create_mddev(match ? match->devname : NULL,
776 ra->name,
777 autof,
778 trustworthy,
779 chosen_name);
780
781 if (mdfd < 0) {
782 fprintf(stderr, Name ": failed to open %s: %s.\n",
783 chosen_name, strerror(errno));
784 return 2;
785 }
786
787 err = assemble_container_content(st, mdfd, ra, runstop,
788 chosen_name, verbose);
789 if (err)
790 return err;
791 }
792 map_unlock(&map);
793 return 0;
794 }