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