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