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