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