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