]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Merge branch 'master' into devel-3.0
[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, reject.
52 * 4/ Determine device number.
53 * - If in mdadm.conf with std name, use that
54 * - UUID in /var/run/mdadm.map use that
55 * - If name is suggestive, use that. unless in use with different uuid.
56 * - Choose a free, high number.
57 * - Use a partitioned device unless strong suggestion not to.
58 * e.g. auto=md
59 * Don't choose partitioned for containers.
60 * 5/ Find out if array already exists
61 * 5a/ if it does not
62 * - choose a name, from mdadm.conf or 'name' field in array.
63 * - create the array
64 * - add the device
65 * 5b/ if it does
66 * - check one drive in array to make sure metadata is a reasonably
67 * close match. Reject if not (e.g. different type)
68 * - add the device
69 * 6/ Make sure /var/run/mdadm.map contains this array.
70 * 7/ Is there enough devices to possibly start the array?
71 * For a container, this means running Incremental_container.
72 * 7a/ if not, finish with success.
73 * 7b/ if yes,
74 * - read all metadata and arrange devices like -A does
75 * - if number of OK devices match expected, or -R and there are enough,
76 * start the array (auto-readonly).
77 */
78 struct stat stb;
79 struct mdinfo info;
80 struct mddev_ident_s *array_list, *match;
81 char chosen_name[1024];
82 int rv;
83 int devnum;
84 struct map_ent *mp, *map = NULL;
85 int dfd, mdfd;
86 char *avail;
87 int active_disks;
88 int uuid_for_name = 0;
89 char *name_to_use;
90 char nbuf[64];
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 if (st->ss->container_content && st->loaded_container) {
148 /* This is a pre-built container array, so we do something
149 * rather different.
150 */
151 return Incremental_container(st, devname, verbose, runstop,
152 autof);
153 }
154
155 memset(&info, 0, sizeof(info));
156 st->ss->getinfo_super(st, &info);
157 /* 3/ Check if there is a match in mdadm.conf */
158
159 array_list = conf_get_ident(NULL);
160 match = NULL;
161 for (; array_list; array_list = array_list->next) {
162 if (array_list->uuid_set &&
163 same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid)
164 == 0) {
165 if (verbose >= 2)
166 fprintf(stderr, Name
167 ": UUID differs from %s.\n",
168 array_list->devname);
169 continue;
170 }
171 if (array_list->name[0] &&
172 strcasecmp(array_list->name, info.name) != 0) {
173 if (verbose >= 2)
174 fprintf(stderr, Name
175 ": Name differs from %s.\n",
176 array_list->devname);
177 continue;
178 }
179 if (array_list->devices &&
180 !match_oneof(array_list->devices, devname)) {
181 if (verbose >= 2)
182 fprintf(stderr, Name
183 ": Not a listed device for %s.\n",
184 array_list->devname);
185 continue;
186 }
187 if (array_list->super_minor != UnSet &&
188 array_list->super_minor != info.array.md_minor) {
189 if (verbose >= 2)
190 fprintf(stderr, Name
191 ": Different super-minor to %s.\n",
192 array_list->devname);
193 continue;
194 }
195 if (!array_list->uuid_set &&
196 !array_list->name[0] &&
197 !array_list->devices &&
198 array_list->super_minor == UnSet) {
199 if (verbose >= 2)
200 fprintf(stderr, Name
201 ": %s doesn't have any identifying information.\n",
202 array_list->devname);
203 continue;
204 }
205 /* FIXME, should I check raid_disks and level too?? */
206
207 if (match) {
208 if (verbose >= 0)
209 fprintf(stderr, Name
210 ": we match both %s and %s - cannot decide which to use.\n",
211 match->devname, array_list->devname);
212 return 2;
213 }
214 match = array_list;
215 }
216
217 /* 3a/ if not, check for homehost match. If no match, continue
218 * but don't trust the 'name' in the array. Thus a 'random' minor
219 * number will be assigned, and the device name will be based
220 * on that. */
221 if (!match) {
222 if (homehost == NULL ||
223 st->ss->match_home(st, homehost) != 1)
224 uuid_for_name = 1;
225 }
226 /* 4/ Determine device number. */
227 /* - If in mdadm.conf with std name, get number from name. */
228 /* - UUID in /var/run/mdadm.map get number from mapping */
229 /* - If name is suggestive, use that. unless in use with */
230 /* different uuid. */
231 /* - Choose a free, high number. */
232 /* - Use a partitioned device unless strong suggestion not to. */
233 /* e.g. auto=md */
234 mp = map_by_uuid(&map, info.uuid);
235
236 if (uuid_for_name && ! mp) {
237 name_to_use = fname_from_uuid(st, &info, nbuf, '-');
238 if (verbose >= 0)
239 fprintf(stderr, Name
240 ": not found in mdadm.conf and not identified by homehost"
241 " - using uuid based name\n");
242 } else
243 name_to_use = info.name;
244
245 /* There are three possible sources for 'autof': command line,
246 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
247 * They have precedence in that order.
248 */
249 if (autof == 0 && match)
250 autof = match->autof;
251 if (autof == 0)
252 autof = ci->autof;
253
254 if (match && (rv = is_standard(match->devname, &devnum))) {
255 devnum = (rv > 0) ? (-1-devnum) : devnum;
256 } else if (mp != NULL)
257 devnum = mp->devnum;
258 else {
259 /* Have to guess a bit. */
260 int use_partitions = 1;
261 char *np, *ep;
262 char *nm, nbuf[1024];
263 struct stat stb2;
264
265 if ((autof&7) == 3 || (autof&7) == 5)
266 use_partitions = 0;
267 if (st->ss->external)
268 use_partitions = 0;
269 np = strchr(name_to_use, ':');
270 if (np)
271 np++;
272 else
273 np = name_to_use;
274 devnum = strtoul(np, &ep, 10);
275 if (ep > np && *ep == 0) {
276 /* This is a number. Let check that it is unused. */
277 if (mddev_busy(use_partitions ? (-1-devnum) : devnum))
278 devnum = -1;
279 } else
280 devnum = -1;
281
282 if (match)
283 nm = match->devname;
284 else {
285 sprintf(nbuf, "/dev/md/%s", np);
286 nm = nbuf;
287 }
288 if (stat(nm, &stb2) == 0 &&
289 S_ISBLK(stb2.st_mode) &&
290 major(stb2.st_rdev) == (use_partitions ?
291 get_mdp_major() : MD_MAJOR)) {
292 if (use_partitions)
293 devnum = minor(stb2.st_rdev) >> MdpMinorShift;
294 else
295 devnum = minor(stb2.st_rdev);
296 if (mddev_busy(use_partitions ? (-1-devnum) : devnum))
297 devnum = -1;
298 }
299
300 if (devnum < 0) {
301 /* Haven't found anything yet, choose something free */
302 devnum = find_free_devnum(use_partitions);
303
304 if (devnum == NoMdDev) {
305 fprintf(stderr, Name
306 ": No spare md devices!!\n");
307 return 2;
308 }
309 } else
310 devnum = use_partitions ? (-1-devnum) : devnum;
311 }
312
313 mdfd = open_mddev_devnum(match ? match->devname : mp ? mp->path : NULL,
314 devnum,
315 name_to_use,
316 chosen_name, autof >> 3);
317 if (mdfd < 0) {
318 fprintf(stderr, Name ": failed to open %s: %s.\n",
319 chosen_name, strerror(errno));
320 return 2;
321 }
322 sysfs_init(&info, mdfd, 0);
323
324 /* 5/ Find out if array already exists */
325 if (! mddev_busy(devnum)) {
326 /* 5a/ if it does not */
327 /* - choose a name, from mdadm.conf or 'name' field in array. */
328 /* - create the array */
329 /* - add the device */
330 struct mdinfo *sra;
331 struct mdinfo dinfo;
332
333 if (set_array_info(mdfd, st, &info) != 0) {
334 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
335 chosen_name, strerror(errno));
336 close(mdfd);
337 return 2;
338 }
339
340 dinfo = info;
341 dinfo.disk.major = major(stb.st_rdev);
342 dinfo.disk.minor = minor(stb.st_rdev);
343 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
344 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
345 devname, chosen_name, strerror(errno));
346 ioctl(mdfd, STOP_ARRAY, 0);
347 close(mdfd);
348 return 2;
349 }
350 sra = sysfs_read(mdfd, devnum, GET_DEVS);
351 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
352 /* It really should be 'none' - must be old buggy
353 * kernel, and mdadm -I may not be able to complete.
354 * So reject it.
355 */
356 ioctl(mdfd, STOP_ARRAY, NULL);
357 fprintf(stderr, Name
358 ": You have an old buggy kernel which cannot support\n"
359 " --incremental reliably. Aborting.\n");
360 close(mdfd);
361 sysfs_free(sra);
362 return 2;
363 }
364 info.array.working_disks = 1;
365 sysfs_free(sra);
366 } else {
367 /* 5b/ if it does */
368 /* - check one drive in array to make sure metadata is a reasonably */
369 /* close match. Reject if not (e.g. different type) */
370 /* - add the device */
371 char dn[20];
372 int dfd2;
373 int err;
374 struct mdinfo *sra;
375 struct supertype *st2;
376 struct mdinfo info2, *d;
377 sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE));
378
379 sprintf(dn, "%d:%d", sra->devs->disk.major,
380 sra->devs->disk.minor);
381 dfd2 = dev_open(dn, O_RDONLY);
382 st2 = dup_super(st);
383 if (st2->ss->load_super(st2, dfd2, NULL) ||
384 st->ss->compare_super(st, st2) != 0) {
385 fprintf(stderr, Name
386 ": metadata mismatch between %s and "
387 "chosen array %s\n",
388 devname, chosen_name);
389 close(mdfd);
390 close(dfd2);
391 return 2;
392 }
393 close(dfd2);
394 memset(&info2, 0, sizeof(info2));
395 st2->ss->getinfo_super(st2, &info2);
396 st2->ss->free_super(st2);
397 if (info.array.level != info2.array.level ||
398 memcmp(info.uuid, info2.uuid, 16) != 0 ||
399 info.array.raid_disks != info2.array.raid_disks) {
400 fprintf(stderr, Name
401 ": unexpected difference between %s and %s.\n",
402 chosen_name, devname);
403 close(mdfd);
404 return 2;
405 }
406 info2.disk.major = major(stb.st_rdev);
407 info2.disk.minor = minor(stb.st_rdev);
408 /* add disk needs to know about containers */
409 if (st->ss->external)
410 sra->array.level = LEVEL_CONTAINER;
411 err = add_disk(mdfd, st2, sra, &info2);
412 if (err < 0 && errno == EBUSY) {
413 /* could be another device present with the same
414 * disk.number. Find and reject any such
415 */
416 find_reject(mdfd, st, sra, info.disk.number,
417 info.events, verbose, chosen_name);
418 err = add_disk(mdfd, st2, sra, &info2);
419 }
420 if (err < 0) {
421 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
422 devname, chosen_name, strerror(errno));
423 close(mdfd);
424 return 2;
425 }
426 info.array.working_disks = 0;
427 for (d = sra->devs; d; d=d->next)
428 info.array.working_disks ++;
429
430 }
431 /* 6/ Make sure /var/run/mdadm.map contains this array. */
432 map_update(&map, devnum,
433 info.text_version,
434 info.uuid, chosen_name);
435
436 /* 7/ Is there enough devices to possibly start the array? */
437 /* 7a/ if not, finish with success. */
438 if (info.array.level == LEVEL_CONTAINER) {
439 /* Try to assemble within the container */
440 close(mdfd);
441 if (verbose >= 0)
442 fprintf(stderr, Name
443 ": container %s now has %d devices\n",
444 chosen_name, info.array.working_disks);
445 return Incremental(chosen_name, verbose, runstop,
446 NULL, homehost, autof);
447 }
448 avail = NULL;
449 active_disks = count_active(st, mdfd, &avail, &info);
450 if (enough(info.array.level, info.array.raid_disks,
451 info.array.layout, info.array.state & 1,
452 avail, active_disks) == 0) {
453 free(avail);
454 if (verbose >= 0)
455 fprintf(stderr, Name
456 ": %s attached to %s, not enough to start (%d).\n",
457 devname, chosen_name, active_disks);
458 close(mdfd);
459 return 0;
460 }
461 free(avail);
462
463 /* 7b/ if yes, */
464 /* - if number of OK devices match expected, or -R and there */
465 /* are enough, */
466 /* + add any bitmap file */
467 /* + start the array (auto-readonly). */
468 {
469 mdu_array_info_t ainf;
470
471 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
472 if (verbose >= 0)
473 fprintf(stderr, Name
474 ": %s attached to %s which is already active.\n",
475 devname, chosen_name);
476 close (mdfd);
477 return 0;
478 }
479 }
480 if (runstop > 0 || active_disks >= info.array.working_disks) {
481 struct mdinfo *sra;
482 /* Let's try to start it */
483 if (match && match->bitmap_file) {
484 int bmfd = open(match->bitmap_file, O_RDWR);
485 if (bmfd < 0) {
486 fprintf(stderr, Name
487 ": Could not open bitmap file %s.\n",
488 match->bitmap_file);
489 close(mdfd);
490 return 1;
491 }
492 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
493 close(bmfd);
494 fprintf(stderr, Name
495 ": Failed to set bitmapfile for %s.\n",
496 chosen_name);
497 close(mdfd);
498 return 1;
499 }
500 close(bmfd);
501 }
502 sra = sysfs_read(mdfd, devnum, 0);
503 if ((sra == NULL || active_disks >= info.array.working_disks)
504 && uuid_for_name == 0)
505 rv = ioctl(mdfd, RUN_ARRAY, NULL);
506 else
507 rv = sysfs_set_str(sra, NULL,
508 "array_state", "read-auto");
509 if (rv == 0) {
510 if (verbose >= 0)
511 fprintf(stderr, Name
512 ": %s attached to %s, which has been started.\n",
513 devname, chosen_name);
514 rv = 0;
515 } else {
516 fprintf(stderr, Name
517 ": %s attached to %s, but failed to start: %s.\n",
518 devname, chosen_name, strerror(errno));
519 rv = 1;
520 }
521 } else {
522 if (verbose >= 0)
523 fprintf(stderr, Name
524 ": %s attached to %s, not enough to start safely.\n",
525 devname, chosen_name);
526 rv = 0;
527 }
528 close(mdfd);
529 return rv;
530 }
531
532 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
533 int number, __u64 events, int verbose,
534 char *array_name)
535 {
536 /* Find a device attached to this array with a disk.number of number
537 * and events less than the passed events, and remove the device.
538 */
539 struct mdinfo *d;
540 mdu_array_info_t ra;
541
542 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
543 return; /* not safe to remove from active arrays
544 * without thinking more */
545
546 for (d = sra->devs; d ; d = d->next) {
547 char dn[10];
548 int dfd;
549 struct mdinfo info;
550 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
551 dfd = dev_open(dn, O_RDONLY);
552 if (dfd < 0)
553 continue;
554 if (st->ss->load_super(st, dfd, NULL)) {
555 close(dfd);
556 continue;
557 }
558 st->ss->getinfo_super(st, &info);
559 st->ss->free_super(st);
560 close(dfd);
561
562 if (info.disk.number != number ||
563 info.events >= events)
564 continue;
565
566 if (d->disk.raid_disk > -1)
567 sysfs_set_str(sra, d, "slot", "none");
568 if (sysfs_set_str(sra, d, "state", "remove") == 0)
569 if (verbose >= 0)
570 fprintf(stderr, Name
571 ": removing old device %s from %s\n",
572 d->sys_name+4, array_name);
573 }
574 }
575
576 static int count_active(struct supertype *st, int mdfd, char **availp,
577 struct mdinfo *bestinfo)
578 {
579 /* count how many devices in sra think they are active */
580 struct mdinfo *d;
581 int cnt = 0, cnt1 = 0;
582 __u64 max_events = 0;
583 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
584 char *avail = NULL;
585
586 for (d = sra->devs ; d ; d = d->next) {
587 char dn[30];
588 int dfd;
589 int ok;
590 struct mdinfo info;
591
592 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
593 dfd = dev_open(dn, O_RDONLY);
594 if (dfd < 0)
595 continue;
596 ok = st->ss->load_super(st, dfd, NULL);
597 close(dfd);
598 if (ok != 0)
599 continue;
600 st->ss->getinfo_super(st, &info);
601 if (!avail) {
602 avail = malloc(info.array.raid_disks);
603 if (!avail) {
604 fprintf(stderr, Name ": out of memory.\n");
605 exit(1);
606 }
607 memset(avail, 0, info.array.raid_disks);
608 *availp = avail;
609 }
610
611 if (info.disk.state & (1<<MD_DISK_SYNC))
612 {
613 if (cnt == 0) {
614 cnt++;
615 max_events = info.events;
616 avail[info.disk.raid_disk] = 2;
617 st->ss->getinfo_super(st, bestinfo);
618 } else if (info.events == max_events) {
619 cnt++;
620 avail[info.disk.raid_disk] = 2;
621 } else if (info.events == max_events-1) {
622 cnt1++;
623 avail[info.disk.raid_disk] = 1;
624 } else if (info.events < max_events - 1)
625 ;
626 else if (info.events == max_events+1) {
627 int i;
628 cnt1 = cnt;
629 cnt = 1;
630 max_events = info.events;
631 for (i=0; i<info.array.raid_disks; i++)
632 if (avail[i])
633 avail[i]--;
634 avail[info.disk.raid_disk] = 2;
635 st->ss->getinfo_super(st, bestinfo);
636 } else { /* info.events much bigger */
637 cnt = 1; cnt1 = 0;
638 memset(avail, 0, info.disk.raid_disk);
639 max_events = info.events;
640 st->ss->getinfo_super(st, bestinfo);
641 }
642 }
643 st->ss->free_super(st);
644 }
645 return cnt + cnt1;
646 }
647
648 void RebuildMap(void)
649 {
650 struct mdstat_ent *mdstat = mdstat_read(0, 0);
651 struct mdstat_ent *md;
652 struct map_ent *map = NULL;
653 int mdp = get_mdp_major();
654
655 for (md = mdstat ; md ; md = md->next) {
656 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
657 struct mdinfo *sd;
658
659 for (sd = sra->devs ; sd ; sd = sd->next) {
660 char dn[30];
661 int dfd;
662 int ok;
663 struct supertype *st;
664 char *path;
665 struct mdinfo info;
666
667 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
668 dfd = dev_open(dn, O_RDONLY);
669 if (dfd < 0)
670 continue;
671 st = guess_super(dfd);
672 if ( st == NULL)
673 ok = -1;
674 else
675 ok = st->ss->load_super(st, dfd, NULL);
676 close(dfd);
677 if (ok != 0)
678 continue;
679 st->ss->getinfo_super(st, &info);
680 if (md->devnum > 0)
681 path = map_dev(MD_MAJOR, md->devnum, 0);
682 else
683 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
684 map_add(&map, md->devnum,
685 info.text_version,
686 info.uuid, path ? : "/unknown");
687 st->ss->free_super(st);
688 break;
689 }
690 }
691 map_write(map);
692 map_free(map);
693 }
694
695 int IncrementalScan(int verbose)
696 {
697 /* look at every device listed in the 'map' file.
698 * If one is found that is not running then:
699 * look in mdadm.conf for bitmap file.
700 * if one exists, but array has none, add it.
701 * try to start array in auto-readonly mode
702 */
703 struct map_ent *mapl = NULL;
704 struct map_ent *me;
705 mddev_ident_t devs, mddev;
706 int rv = 0;
707
708 map_read(&mapl);
709 devs = conf_get_ident(NULL);
710
711 for (me = mapl ; me ; me = me->next) {
712 char path[1024];
713 mdu_array_info_t array;
714 mdu_bitmap_file_t bmf;
715 struct mdinfo *sra;
716 int mdfd = open_mddev_devnum(me->path, me->devnum,
717 NULL, path, 0);
718 if (mdfd < 0)
719 continue;
720 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
721 errno != ENODEV) {
722 close(mdfd);
723 continue;
724 }
725 /* Ok, we can try this one. Maybe it needs a bitmap */
726 for (mddev = devs ; mddev ; mddev = mddev->next)
727 if (strcmp(mddev->devname, me->path) == 0)
728 break;
729 if (mddev && mddev->bitmap_file) {
730 /*
731 * Note: early kernels will wrongly fail this, so it
732 * is a hint only
733 */
734 int added = -1;
735 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
736 int bmfd = open(mddev->bitmap_file, O_RDWR);
737 if (bmfd >= 0) {
738 added = ioctl(mdfd, SET_BITMAP_FILE,
739 bmfd);
740 close(bmfd);
741 }
742 }
743 if (verbose >= 0) {
744 if (added == 0)
745 fprintf(stderr, Name
746 ": Added bitmap %s to %s\n",
747 mddev->bitmap_file, me->path);
748 else if (errno != EEXIST)
749 fprintf(stderr, Name
750 ": Failed to add bitmap to %s: %s\n",
751 me->path, strerror(errno));
752 }
753 }
754 sra = sysfs_read(mdfd, 0, 0);
755 if (sra) {
756 if (sysfs_set_str(sra, NULL,
757 "array_state", "read-auto") == 0) {
758 if (verbose >= 0)
759 fprintf(stderr, Name
760 ": started array %s\n",
761 me->path);
762 } else {
763 fprintf(stderr, Name
764 ": failed to start array %s: %s\n",
765 me->path, strerror(errno));
766 rv = 1;
767 }
768 }
769 }
770 return rv;
771 }
772
773 static char *container2devname(char *devname)
774 {
775 int fd = open(devname, O_RDONLY);
776 char *mdname = NULL;
777
778 if (fd >= 0) {
779 mdname = devnum2devname(fd2devnum(fd));
780 close(fd);
781 }
782
783 return mdname;
784 }
785
786 int Incremental_container(struct supertype *st, char *devname, int verbose,
787 int runstop, int autof)
788 {
789 /* Collect the contents of this container and for each
790 * array, choose a device name and assemble the array.
791 */
792
793 struct mdinfo *list = st->ss->container_content(st);
794 struct mdinfo *ra;
795 char *mdname = container2devname(devname);
796
797 if (!mdname) {
798 fprintf(stderr, Name": failed to determine device name\n");
799 return 2;
800 }
801
802 for (ra = list ; ra ; ra = ra->next) {
803 struct mdinfo *dev, *sra;
804 int devnum = -1;
805 int mdfd;
806 char chosen_name[1024];
807 int usepart = 1;
808 char *n;
809 int working = 0, preexist = 0;
810 struct map_ent *mp, *map = NULL;
811 char nbuf[64];
812 char *name_to_use;
813 struct mddev_ident_s *match = NULL;
814
815 if ((autof&7) == 3 || (autof&7) == 5)
816 usepart = 0;
817
818 mp = map_by_uuid(&map, ra->uuid);
819
820 name_to_use = ra->name;
821 if (! name_to_use ||
822 ! *name_to_use ||
823 (*devname != '/' || strncmp("UUID-", strrchr(devname,'/')+1,5) == 0)
824 )
825 name_to_use = fname_from_uuid(st, ra, nbuf, '-');
826
827 if (!mp) {
828
829 /* Check in mdadm.conf for devices == devname and
830 * member == ra->text_version after second slash.
831 */
832 char *sub = strchr(ra->text_version+1, '/');
833 struct mddev_ident_s *array_list;
834 if (sub) {
835 sub++;
836 array_list = conf_get_ident(NULL);
837 } else
838 array_list = NULL;
839 for(; array_list ; array_list = array_list->next) {
840 int fd;
841 char *dn;
842 if (array_list->member == NULL ||
843 array_list->container == NULL)
844 continue;
845 if (strcmp(array_list->member, sub) != 0)
846 continue;
847 if (array_list->uuid_set &&
848 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
849 continue;
850 fd = open(array_list->container, O_RDONLY);
851 if (fd < 0)
852 continue;
853 dn = devnum2devname(fd2devnum(fd));
854 close(fd);
855 if (strncmp(dn, ra->text_version+1,
856 strlen(dn)) != 0 ||
857 ra->text_version[strlen(dn)+1] != '/') {
858 free(dn);
859 continue;
860 }
861 free(dn);
862 /* we have a match */
863 match = array_list;
864 if (verbose>0)
865 fprintf(stderr, Name ": match found for member %s\n",
866 array_list->member);
867 break;
868 }
869 }
870
871 if (match && is_standard(match->devname, &devnum))
872 /* we have devnum now */;
873 else if (mp)
874 devnum = mp->devnum;
875 else if (is_standard(name_to_use, &devnum))
876 /* have devnum */;
877 else {
878 n = name_to_use;
879 if (*n == 'd')
880 n++;
881 if (*n && devnum < 0) {
882 devnum = strtoul(n, &n, 10);
883 if (devnum >= 0 && (*n == 0 || *n == ' ')) {
884 /* Use this devnum */
885 usepart = (name_to_use[0] == 'd');
886 if (mddev_busy(usepart ? (-1-devnum) : devnum))
887 devnum = -1;
888 } else
889 devnum = -1;
890 }
891
892 if (devnum < 0) {
893 char *nm = name_to_use;
894 char nbuf[1024];
895 struct stat stb;
896 if (strchr(nm, ':'))
897 nm = strchr(nm, ':')+1;
898 sprintf(nbuf, "/dev/md/%s", nm);
899
900 if (stat(nbuf, &stb) == 0 &&
901 S_ISBLK(stb.st_mode) &&
902 major(stb.st_rdev) == (usepart ?
903 get_mdp_major() : MD_MAJOR)){
904 if (usepart)
905 devnum = minor(stb.st_rdev)
906 >> MdpMinorShift;
907 else
908 devnum = minor(stb.st_rdev);
909 if (mddev_busy(usepart ? (-1-devnum) : devnum))
910 devnum = -1;
911 }
912 }
913
914 if (devnum >= 0)
915 devnum = usepart ? (-1-devnum) : devnum;
916 else
917 devnum = find_free_devnum(usepart);
918 }
919 mdfd = open_mddev_devnum(mp ? mp->path : match ? match->devname : NULL,
920 devnum, name_to_use,
921 chosen_name, autof>>3);
922
923 if (mdfd < 0) {
924 fprintf(stderr, Name ": failed to open %s: %s.\n",
925 chosen_name, strerror(errno));
926 return 2;
927 }
928
929
930 sysfs_init(ra, mdfd, 0);
931
932 sra = sysfs_read(mdfd, 0, GET_VERSION);
933 if (sra == NULL || strcmp(sra->text_version, ra->text_version) != 0)
934 if (sysfs_set_array(ra, md_get_version(mdfd)) != 0)
935 return 1;
936 if (sra)
937 sysfs_free(sra);
938
939 for (dev = ra->devs; dev; dev = dev->next)
940 if (sysfs_add_disk(ra, dev) == 0)
941 working++;
942 else if (errno == EEXIST)
943 preexist++;
944 if (working == 0)
945 /* Nothing new, don't try to start */ ;
946 else if (runstop > 0 ||
947 (working + preexist) >= ra->array.working_disks) {
948 switch(ra->array.level) {
949 case LEVEL_LINEAR:
950 case LEVEL_MULTIPATH:
951 case 0:
952 sysfs_set_str(ra, NULL, "array_state",
953 "active");
954 break;
955 default:
956 sysfs_set_str(ra, NULL, "array_state",
957 "readonly");
958 /* start mdmon if needed. */
959 if (!mdmon_running(st->container_dev))
960 start_mdmon(st->container_dev);
961 ping_monitor(devnum2devname(st->container_dev));
962 break;
963 }
964 sysfs_set_safemode(ra, ra->safe_mode_delay);
965 if (verbose >= 0) {
966 fprintf(stderr, Name
967 ": Started %s with %d devices",
968 chosen_name, working + preexist);
969 if (preexist)
970 fprintf(stderr, " (%d new)", working);
971 fprintf(stderr, "\n");
972 }
973 /* FIXME should have an O_EXCL and wait for read-auto */
974 } else
975 if (verbose >= 0)
976 fprintf(stderr, Name
977 ": %s assembled with %d devices but "
978 "not started\n",
979 chosen_name, working);
980 close(mdfd);
981 map_update(&map, devnum,
982 ra->text_version,
983 ra->uuid, chosen_name);
984 }
985 return 0;
986 }