]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Incrmental: cope with udev slowness and errors in map file.
[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 /* There are three possible sources for 'autof': command line,
230 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
231 * ARRAY takes precedence, then command line, then
232 * CREATE.
233 */
234 if (match && match->autof)
235 autof = match->autof;
236 if (autof == 0)
237 autof = ci->autof;
238
239 if (st->ss->container_content && st->loaded_container) {
240 /* This is a pre-built container array, so we do something
241 * rather different.
242 */
243 return Incremental_container(st, devname, verbose, runstop,
244 autof, trustworthy);
245 }
246 /* 4/ Check if array exists.
247 */
248 map_lock(&map);
249 mp = map_by_uuid(&map, info.uuid);
250 if (mp) {
251 mdfd = open_mddev(mp->path, 0);
252 if (mdfd < 0 && mddev_busy(mp->devnum)) {
253 /* maybe udev hasn't created it yet. */
254 char buf[50];
255 sprintf(buf, "%d:%d", dev2major(mp->devnum),
256 dev2minor(mp->devnum));
257 mdfd = dev_open(buf, O_RDWR);
258 }
259 } else
260 mdfd = -1;
261
262 if (mdfd < 0) {
263 struct mdinfo *sra;
264 struct mdinfo dinfo;
265
266 /* Couldn't find an existing array, maybe make a new one */
267 mdfd = create_mddev(match ? match->devname : NULL,
268 info.name, autof, trustworthy, chosen_name);
269
270
271 if (mdfd < 0)
272 return 1;
273
274 sysfs_init(&info, mdfd, 0);
275
276 if (set_array_info(mdfd, st, &info) != 0) {
277 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
278 chosen_name, strerror(errno));
279 close(mdfd);
280 return 2;
281 }
282
283 dinfo = info;
284 dinfo.disk.major = major(stb.st_rdev);
285 dinfo.disk.minor = minor(stb.st_rdev);
286 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
287 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
288 devname, chosen_name, strerror(errno));
289 ioctl(mdfd, STOP_ARRAY, 0);
290 close(mdfd);
291 return 2;
292 }
293 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
294 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
295 /* It really should be 'none' - must be old buggy
296 * kernel, and mdadm -I may not be able to complete.
297 * So reject it.
298 */
299 ioctl(mdfd, STOP_ARRAY, NULL);
300 fprintf(stderr, Name
301 ": You have an old buggy kernel which cannot support\n"
302 " --incremental reliably. Aborting.\n");
303 close(mdfd);
304 sysfs_free(sra);
305 return 2;
306 }
307 info.array.working_disks = 1;
308 sysfs_free(sra);
309 /* 6/ Make sure /var/run/mdadm.map contains this array. */
310 map_update(&map, fd2devnum(mdfd),
311 info.text_version,
312 info.uuid, chosen_name);
313 } else {
314 /* 5b/ if it does */
315 /* - check one drive in array to make sure metadata is a reasonably */
316 /* close match. Reject if not (e.g. different type) */
317 /* - add the device */
318 char dn[20];
319 int dfd2;
320 int err;
321 struct mdinfo *sra;
322 struct supertype *st2;
323 struct mdinfo info2, *d;
324
325 strcpy(chosen_name, mp->path);
326
327 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
328
329 sprintf(dn, "%d:%d", sra->devs->disk.major,
330 sra->devs->disk.minor);
331 dfd2 = dev_open(dn, O_RDONLY);
332 st2 = dup_super(st);
333 if (st2->ss->load_super(st2, dfd2, NULL) ||
334 st->ss->compare_super(st, st2) != 0) {
335 fprintf(stderr, Name
336 ": metadata mismatch between %s and "
337 "chosen array %s\n",
338 devname, chosen_name);
339 close(mdfd);
340 close(dfd2);
341 return 2;
342 }
343 close(dfd2);
344 memset(&info2, 0, sizeof(info2));
345 st2->ss->getinfo_super(st2, &info2);
346 st2->ss->free_super(st2);
347 if (info.array.level != info2.array.level ||
348 memcmp(info.uuid, info2.uuid, 16) != 0 ||
349 info.array.raid_disks != info2.array.raid_disks) {
350 fprintf(stderr, Name
351 ": unexpected difference between %s and %s.\n",
352 chosen_name, devname);
353 close(mdfd);
354 return 2;
355 }
356 info2.disk.major = major(stb.st_rdev);
357 info2.disk.minor = minor(stb.st_rdev);
358 /* add disk needs to know about containers */
359 if (st->ss->external)
360 sra->array.level = LEVEL_CONTAINER;
361 err = add_disk(mdfd, st2, sra, &info2);
362 if (err < 0 && errno == EBUSY) {
363 /* could be another device present with the same
364 * disk.number. Find and reject any such
365 */
366 find_reject(mdfd, st, sra, info.disk.number,
367 info.events, verbose, chosen_name);
368 err = add_disk(mdfd, st2, sra, &info2);
369 }
370 if (err < 0) {
371 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
372 devname, chosen_name, strerror(errno));
373 close(mdfd);
374 return 2;
375 }
376 info.array.working_disks = 0;
377 for (d = sra->devs; d; d=d->next)
378 info.array.working_disks ++;
379
380 }
381
382 /* 7/ Is there enough devices to possibly start the array? */
383 /* 7a/ if not, finish with success. */
384 if (info.array.level == LEVEL_CONTAINER) {
385 /* Try to assemble within the container */
386 close(mdfd);
387 map_unlock(&map);
388 sysfs_uevent(&info, "change");
389 if (verbose >= 0)
390 fprintf(stderr, Name
391 ": container %s now has %d devices\n",
392 chosen_name, info.array.working_disks);
393 return Incremental(chosen_name, verbose, runstop,
394 NULL, homehost, autof);
395 }
396 avail = NULL;
397 active_disks = count_active(st, mdfd, &avail, &info);
398 if (enough(info.array.level, info.array.raid_disks,
399 info.array.layout, info.array.state & 1,
400 avail, active_disks) == 0) {
401 free(avail);
402 if (verbose >= 0)
403 fprintf(stderr, Name
404 ": %s attached to %s, not enough to start (%d).\n",
405 devname, chosen_name, active_disks);
406 map_unlock(&map);
407 close(mdfd);
408 return 0;
409 }
410 free(avail);
411
412 /* 7b/ if yes, */
413 /* - if number of OK devices match expected, or -R and there */
414 /* are enough, */
415 /* + add any bitmap file */
416 /* + start the array (auto-readonly). */
417
418 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
419 if (verbose >= 0)
420 fprintf(stderr, Name
421 ": %s attached to %s which is already active.\n",
422 devname, chosen_name);
423 close(mdfd);
424 map_unlock(&map);
425 return 0;
426 }
427
428 map_unlock(&map);
429 if (runstop > 0 || active_disks >= info.array.working_disks) {
430 struct mdinfo *sra;
431 /* Let's try to start it */
432 if (match && match->bitmap_file) {
433 int bmfd = open(match->bitmap_file, O_RDWR);
434 if (bmfd < 0) {
435 fprintf(stderr, Name
436 ": Could not open bitmap file %s.\n",
437 match->bitmap_file);
438 close(mdfd);
439 return 1;
440 }
441 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
442 close(bmfd);
443 fprintf(stderr, Name
444 ": Failed to set bitmapfile for %s.\n",
445 chosen_name);
446 close(mdfd);
447 return 1;
448 }
449 close(bmfd);
450 }
451 sra = sysfs_read(mdfd, fd2devnum(mdfd), 0);
452 if ((sra == NULL || active_disks >= info.array.working_disks)
453 && trustworthy != FOREIGN)
454 rv = ioctl(mdfd, RUN_ARRAY, NULL);
455 else
456 rv = sysfs_set_str(sra, NULL,
457 "array_state", "read-auto");
458 if (rv == 0) {
459 if (verbose >= 0)
460 fprintf(stderr, Name
461 ": %s attached to %s, which has been started.\n",
462 devname, chosen_name);
463 rv = 0;
464 } else {
465 fprintf(stderr, Name
466 ": %s attached to %s, but failed to start: %s.\n",
467 devname, chosen_name, strerror(errno));
468 rv = 1;
469 }
470 } else {
471 if (verbose >= 0)
472 fprintf(stderr, Name
473 ": %s attached to %s, not enough to start safely.\n",
474 devname, chosen_name);
475 rv = 0;
476 }
477 close(mdfd);
478 return rv;
479 }
480
481 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
482 int number, __u64 events, int verbose,
483 char *array_name)
484 {
485 /* Find a device attached to this array with a disk.number of number
486 * and events less than the passed events, and remove the device.
487 */
488 struct mdinfo *d;
489 mdu_array_info_t ra;
490
491 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
492 return; /* not safe to remove from active arrays
493 * without thinking more */
494
495 for (d = sra->devs; d ; d = d->next) {
496 char dn[10];
497 int dfd;
498 struct mdinfo info;
499 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
500 dfd = dev_open(dn, O_RDONLY);
501 if (dfd < 0)
502 continue;
503 if (st->ss->load_super(st, dfd, NULL)) {
504 close(dfd);
505 continue;
506 }
507 st->ss->getinfo_super(st, &info);
508 st->ss->free_super(st);
509 close(dfd);
510
511 if (info.disk.number != number ||
512 info.events >= events)
513 continue;
514
515 if (d->disk.raid_disk > -1)
516 sysfs_set_str(sra, d, "slot", "none");
517 if (sysfs_set_str(sra, d, "state", "remove") == 0)
518 if (verbose >= 0)
519 fprintf(stderr, Name
520 ": removing old device %s from %s\n",
521 d->sys_name+4, array_name);
522 }
523 }
524
525 static int count_active(struct supertype *st, int mdfd, char **availp,
526 struct mdinfo *bestinfo)
527 {
528 /* count how many devices in sra think they are active */
529 struct mdinfo *d;
530 int cnt = 0, cnt1 = 0;
531 __u64 max_events = 0;
532 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
533 char *avail = NULL;
534
535 for (d = sra->devs ; d ; d = d->next) {
536 char dn[30];
537 int dfd;
538 int ok;
539 struct mdinfo info;
540
541 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
542 dfd = dev_open(dn, O_RDONLY);
543 if (dfd < 0)
544 continue;
545 ok = st->ss->load_super(st, dfd, NULL);
546 close(dfd);
547 if (ok != 0)
548 continue;
549 st->ss->getinfo_super(st, &info);
550 if (!avail) {
551 avail = malloc(info.array.raid_disks);
552 if (!avail) {
553 fprintf(stderr, Name ": out of memory.\n");
554 exit(1);
555 }
556 memset(avail, 0, info.array.raid_disks);
557 *availp = avail;
558 }
559
560 if (info.disk.state & (1<<MD_DISK_SYNC))
561 {
562 if (cnt == 0) {
563 cnt++;
564 max_events = info.events;
565 avail[info.disk.raid_disk] = 2;
566 st->ss->getinfo_super(st, bestinfo);
567 } else if (info.events == max_events) {
568 cnt++;
569 avail[info.disk.raid_disk] = 2;
570 } else if (info.events == max_events-1) {
571 cnt1++;
572 avail[info.disk.raid_disk] = 1;
573 } else if (info.events < max_events - 1)
574 ;
575 else if (info.events == max_events+1) {
576 int i;
577 cnt1 = cnt;
578 cnt = 1;
579 max_events = info.events;
580 for (i=0; i<info.array.raid_disks; i++)
581 if (avail[i])
582 avail[i]--;
583 avail[info.disk.raid_disk] = 2;
584 st->ss->getinfo_super(st, bestinfo);
585 } else { /* info.events much bigger */
586 cnt = 1; cnt1 = 0;
587 memset(avail, 0, info.disk.raid_disk);
588 max_events = info.events;
589 st->ss->getinfo_super(st, bestinfo);
590 }
591 }
592 st->ss->free_super(st);
593 }
594 return cnt + cnt1;
595 }
596
597 void RebuildMap(void)
598 {
599 struct mdstat_ent *mdstat = mdstat_read(0, 0);
600 struct mdstat_ent *md;
601 struct map_ent *map = NULL;
602 int mdp = get_mdp_major();
603
604 for (md = mdstat ; md ; md = md->next) {
605 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
606 struct mdinfo *sd;
607
608 for (sd = sra->devs ; sd ; sd = sd->next) {
609 char dn[30];
610 int dfd;
611 int ok;
612 struct supertype *st;
613 char *path;
614 struct mdinfo info;
615
616 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
617 dfd = dev_open(dn, O_RDONLY);
618 if (dfd < 0)
619 continue;
620 st = guess_super(dfd);
621 if ( st == NULL)
622 ok = -1;
623 else
624 ok = st->ss->load_super(st, dfd, NULL);
625 close(dfd);
626 if (ok != 0)
627 continue;
628 st->ss->getinfo_super(st, &info);
629 if (md->devnum > 0)
630 path = map_dev(MD_MAJOR, md->devnum, 0);
631 else
632 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
633 map_add(&map, md->devnum,
634 info.text_version,
635 info.uuid, path ? : "/unknown");
636 st->ss->free_super(st);
637 break;
638 }
639 sysfs_free(sra);
640 }
641 map_write(map);
642 map_free(map);
643 for (md = mdstat ; md ; md = md->next) {
644 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION);
645 sysfs_uevent(sra, "change");
646 sysfs_free(sra);
647 }
648 }
649
650 int IncrementalScan(int verbose)
651 {
652 /* look at every device listed in the 'map' file.
653 * If one is found that is not running then:
654 * look in mdadm.conf for bitmap file.
655 * if one exists, but array has none, add it.
656 * try to start array in auto-readonly mode
657 */
658 struct map_ent *mapl = NULL;
659 struct map_ent *me;
660 mddev_ident_t devs, mddev;
661 int rv = 0;
662
663 map_read(&mapl);
664 devs = conf_get_ident(NULL);
665
666 for (me = mapl ; me ; me = me->next) {
667 mdu_array_info_t array;
668 mdu_bitmap_file_t bmf;
669 struct mdinfo *sra;
670 int mdfd = open_mddev(me->path, 0);
671
672 if (mdfd < 0)
673 continue;
674 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
675 errno != ENODEV) {
676 close(mdfd);
677 continue;
678 }
679 /* Ok, we can try this one. Maybe it needs a bitmap */
680 for (mddev = devs ; mddev ; mddev = mddev->next)
681 if (mddev->devname
682 && strcmp(mddev->devname, me->path) == 0)
683 break;
684 if (mddev && mddev->bitmap_file) {
685 /*
686 * Note: early kernels will wrongly fail this, so it
687 * is a hint only
688 */
689 int added = -1;
690 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
691 int bmfd = open(mddev->bitmap_file, O_RDWR);
692 if (bmfd >= 0) {
693 added = ioctl(mdfd, SET_BITMAP_FILE,
694 bmfd);
695 close(bmfd);
696 }
697 }
698 if (verbose >= 0) {
699 if (added == 0)
700 fprintf(stderr, Name
701 ": Added bitmap %s to %s\n",
702 mddev->bitmap_file, me->path);
703 else if (errno != EEXIST)
704 fprintf(stderr, Name
705 ": Failed to add bitmap to %s: %s\n",
706 me->path, strerror(errno));
707 }
708 }
709 sra = sysfs_read(mdfd, 0, 0);
710 if (sra) {
711 if (sysfs_set_str(sra, NULL,
712 "array_state", "read-auto") == 0) {
713 if (verbose >= 0)
714 fprintf(stderr, Name
715 ": started array %s\n",
716 me->path);
717 } else {
718 fprintf(stderr, Name
719 ": failed to start array %s: %s\n",
720 me->path, strerror(errno));
721 rv = 1;
722 }
723 }
724 }
725 return rv;
726 }
727
728 static char *container2devname(char *devname)
729 {
730 char *mdname = NULL;
731
732 if (devname[0] == '/') {
733 int fd = open(devname, O_RDONLY);
734 if (fd >= 0) {
735 mdname = devnum2devname(fd2devnum(fd));
736 close(fd);
737 }
738 } else {
739 int uuid[4];
740 struct map_ent *mp, *map = NULL;
741
742 if (!parse_uuid(devname, uuid))
743 return mdname;
744 mp = map_by_uuid(&map, uuid);
745 if (mp)
746 mdname = devnum2devname(mp->devnum);
747 map_free(map);
748 }
749
750 return mdname;
751 }
752
753 int Incremental_container(struct supertype *st, char *devname, int verbose,
754 int runstop, int autof, int trustworthy)
755 {
756 /* Collect the contents of this container and for each
757 * array, choose a device name and assemble the array.
758 */
759
760 struct mdinfo *list = st->ss->container_content(st);
761 struct mdinfo *ra;
762 struct map_ent *map = NULL;
763
764 map_lock(&map);
765
766 for (ra = list ; ra ; ra = ra->next) {
767 int mdfd;
768 char chosen_name[1024];
769 struct map_ent *mp;
770 struct mddev_ident_s *match = NULL;
771 int err;
772
773 mp = map_by_uuid(&map, ra->uuid);
774
775 if (!mp) {
776
777 /* Check in mdadm.conf for devices == devname and
778 * member == ra->text_version after second slash.
779 */
780 char *sub = strchr(ra->text_version+1, '/');
781 struct mddev_ident_s *array_list;
782 if (sub) {
783 sub++;
784 array_list = conf_get_ident(NULL);
785 } else
786 array_list = NULL;
787 for(; array_list ; array_list = array_list->next) {
788 char *dn;
789 if (array_list->member == NULL ||
790 array_list->container == NULL)
791 continue;
792 if (strcmp(array_list->member, sub) != 0)
793 continue;
794 if (array_list->uuid_set &&
795 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
796 continue;
797 dn = container2devname(array_list->container);
798 if (dn == NULL)
799 continue;
800 if (strncmp(dn, ra->text_version+1,
801 strlen(dn)) != 0 ||
802 ra->text_version[strlen(dn)+1] != '/') {
803 free(dn);
804 continue;
805 }
806 free(dn);
807 /* we have a match */
808 match = array_list;
809 if (verbose>0)
810 fprintf(stderr, Name ": match found for member %s\n",
811 array_list->member);
812 break;
813 }
814 }
815
816 mdfd = create_mddev(match ? match->devname : NULL,
817 ra->name,
818 autof,
819 trustworthy,
820 chosen_name);
821
822 if (mdfd < 0) {
823 fprintf(stderr, Name ": failed to open %s: %s.\n",
824 chosen_name, strerror(errno));
825 return 2;
826 }
827
828 err = assemble_container_content(st, mdfd, ra, runstop,
829 chosen_name, verbose);
830 close(mdfd);
831 if (err)
832 return err;
833 }
834 map_unlock(&map);
835 return 0;
836 }