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