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