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