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