]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
MAN: Man update for check-pointing
[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 #include <dirent.h>
33 #include <ctype.h>
34
35 static int count_active(struct supertype *st, struct mdinfo *sra,
36 int mdfd, char **availp,
37 struct mdinfo *info);
38 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
39 int number, __u64 events, int verbose,
40 char *array_name);
41 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
42 struct map_ent *target,
43 struct supertype *st, int verbose);
44
45 static int Incremental_container(struct supertype *st, char *devname,
46 char *homehost,
47 int verbose, int runstop, int autof);
48
49 static struct mddev_ident *search_mdstat(struct supertype *st,
50 struct mdinfo *info,
51 char *devname,
52 int verbose, int *rvp);
53
54 int Incremental(char *devname, int verbose, int runstop,
55 struct supertype *st, char *homehost, int require_homehost,
56 int autof)
57 {
58 /* Add this device to an array, creating the array if necessary
59 * and starting the array if sensible or - if runstop>0 - if possible.
60 *
61 * This has several steps:
62 *
63 * 1/ Check if device is permitted by mdadm.conf, reject if not.
64 * 2/ Find metadata, reject if none appropriate (check
65 * version/name from args)
66 * 3/ Check if there is a match in mdadm.conf
67 * 3a/ if not, check for homehost match. If no match, assemble as
68 * a 'foreign' array.
69 * 4/ Determine device number.
70 * - If in mdadm.conf with std name, use that
71 * - UUID in /var/run/mdadm.map use that
72 * - If name is suggestive, use that. unless in use with different uuid.
73 * - Choose a free, high number.
74 * - Use a partitioned device unless strong suggestion not to.
75 * e.g. auto=md
76 * Don't choose partitioned for containers.
77 * 5/ Find out if array already exists
78 * 5a/ if it does not
79 * - choose a name, from mdadm.conf or 'name' field in array.
80 * - create the array
81 * - add the device
82 * 5b/ if it does
83 * - check one drive in array to make sure metadata is a reasonably
84 * close match. Reject if not (e.g. different type)
85 * - add the device
86 * 6/ Make sure /var/run/mdadm.map contains this array.
87 * 7/ Is there enough devices to possibly start the array?
88 * For a container, this means running Incremental_container.
89 * 7a/ if not, finish with success.
90 * 7b/ if yes,
91 * - read all metadata and arrange devices like -A does
92 * - if number of OK devices match expected, or -R and there are enough,
93 * start the array (auto-readonly).
94 */
95 struct stat stb;
96 struct mdinfo info, dinfo;
97 struct mdinfo *sra = NULL, *d;
98 struct mddev_ident *match;
99 char chosen_name[1024];
100 int rv = 1;
101 struct map_ent *mp, *map = NULL;
102 int dfd = -1, mdfd = -1;
103 char *avail = NULL;
104 int active_disks;
105 int trustworthy;
106 char *name_to_use;
107 mdu_array_info_t ainf;
108 struct dev_policy *policy = NULL;
109 struct map_ent target_array;
110 int have_target;
111
112 struct createinfo *ci = conf_get_create_info();
113
114 if (stat(devname, &stb) < 0) {
115 if (verbose >= 0)
116 fprintf(stderr, Name ": stat failed for %s: %s.\n",
117 devname, strerror(errno));
118 return rv;
119 }
120 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
121 if (verbose >= 0)
122 fprintf(stderr, Name ": %s is not a block device.\n",
123 devname);
124 return rv;
125 }
126 dfd = dev_open(devname, O_RDONLY|O_EXCL);
127 if (dfd < 0) {
128 if (verbose >= 0)
129 fprintf(stderr, Name ": cannot open %s: %s.\n",
130 devname, strerror(errno));
131 return rv;
132 }
133 /* If the device is a container, we do something very different */
134 if (must_be_container(dfd)) {
135 if (!st)
136 st = super_by_fd(dfd, NULL);
137 if (st && st->ss->load_container)
138 rv = st->ss->load_container(st, dfd, NULL);
139
140 close(dfd);
141 if (!rv && st->ss->container_content)
142 return Incremental_container(st, devname, homehost,
143 verbose, runstop, autof);
144
145 fprintf(stderr, Name ": %s is not part of an md array.\n",
146 devname);
147 return rv;
148 }
149
150 /* 1/ Check if device is permitted by mdadm.conf */
151
152 if (!conf_test_dev(devname)) {
153 if (verbose >= 0)
154 fprintf(stderr, Name
155 ": %s not permitted by mdadm.conf.\n",
156 devname);
157 goto out;
158 }
159
160 /* 2/ Find metadata, reject if none appropriate (check
161 * version/name from args) */
162
163 if (fstat(dfd, &stb) < 0) {
164 if (verbose >= 0)
165 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
166 devname, strerror(errno));
167 goto out;
168 }
169 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
170 if (verbose >= 0)
171 fprintf(stderr, Name ": %s is not a block device.\n",
172 devname);
173 goto out;
174 }
175
176 dinfo.disk.major = major(stb.st_rdev);
177 dinfo.disk.minor = minor(stb.st_rdev);
178
179 policy = disk_policy(&dinfo);
180 have_target = policy_check_path(&dinfo, &target_array);
181
182 if (st == NULL && (st = guess_super(dfd)) == NULL) {
183 if (verbose >= 0)
184 fprintf(stderr, Name
185 ": no recognisable superblock on %s.\n",
186 devname);
187 rv = try_spare(devname, &dfd, policy,
188 have_target ? &target_array : NULL,
189 st, verbose);
190 goto out;
191 }
192 if (st->ss->compare_super == NULL ||
193 st->ss->load_super(st, dfd, NULL)) {
194 if (verbose >= 0)
195 fprintf(stderr, Name ": no RAID superblock on %s.\n",
196 devname);
197 rv = try_spare(devname, &dfd, policy,
198 have_target ? &target_array : NULL,
199 st, verbose);
200 free(st);
201 goto out;
202 }
203 close (dfd); dfd = -1;
204
205 st->ss->getinfo_super(st, &info, NULL);
206
207 /* 3/ Check if there is a match in mdadm.conf */
208 match = search_mdstat(st, &info, devname, verbose, &rv);
209 if (!match && rv == 2)
210 goto out;
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, policy,
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 name_to_use = info.name;
258 if (name_to_use[0] == 0 &&
259 info.array.level == LEVEL_CONTAINER &&
260 trustworthy == LOCAL) {
261 name_to_use = info.text_version;
262 trustworthy = METADATA;
263 }
264 if (name_to_use[0] && trustworthy != LOCAL &&
265 ! require_homehost &&
266 conf_name_is_free(name_to_use))
267 trustworthy = LOCAL;
268
269 /* strip "hostname:" prefix from name if we have decided
270 * to treat it as LOCAL
271 */
272 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
273 name_to_use = strchr(name_to_use, ':')+1;
274
275 /* 4/ Check if array exists.
276 */
277 if (map_lock(&map))
278 fprintf(stderr, Name ": failed to get exclusive lock on "
279 "mapfile\n");
280 mp = map_by_uuid(&map, info.uuid);
281 if (mp)
282 mdfd = open_dev(mp->devnum);
283 else
284 mdfd = -1;
285
286 if (mdfd < 0) {
287
288 /* Couldn't find an existing array, maybe make a new one */
289 mdfd = create_mddev(match ? match->devname : NULL,
290 name_to_use, autof, trustworthy, chosen_name);
291
292 if (mdfd < 0)
293 goto out;
294
295 sysfs_init(&info, mdfd, 0);
296
297 if (set_array_info(mdfd, st, &info) != 0) {
298 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
299 chosen_name, strerror(errno));
300 rv = 2;
301 goto out;
302 }
303
304 dinfo = info;
305 dinfo.disk.major = major(stb.st_rdev);
306 dinfo.disk.minor = minor(stb.st_rdev);
307 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
308 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
309 devname, chosen_name, strerror(errno));
310 ioctl(mdfd, STOP_ARRAY, 0);
311 rv = 2;
312 goto out;
313 }
314 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
315 GET_OFFSET | GET_SIZE));
316
317 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
318 /* It really should be 'none' - must be old buggy
319 * kernel, and mdadm -I may not be able to complete.
320 * So reject it.
321 */
322 ioctl(mdfd, STOP_ARRAY, NULL);
323 fprintf(stderr, Name
324 ": You have an old buggy kernel which cannot support\n"
325 " --incremental reliably. Aborting.\n");
326 sysfs_free(sra);
327 rv = 2;
328 goto out;
329 }
330 info.array.working_disks = 1;
331 /* 6/ Make sure /var/run/mdadm.map contains this array. */
332 map_update(&map, fd2devnum(mdfd),
333 info.text_version,
334 info.uuid, chosen_name);
335 } else {
336 /* 5b/ if it does */
337 /* - check one drive in array to make sure metadata is a reasonably */
338 /* close match. Reject if not (e.g. different type) */
339 /* - add the device */
340 char dn[20];
341 int dfd2;
342 int err;
343 struct supertype *st2;
344 struct mdinfo info2, *d;
345
346 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
347 GET_OFFSET | GET_SIZE));
348
349 if (mp->path)
350 strcpy(chosen_name, mp->path);
351 else
352 strcpy(chosen_name, devnum2devname(mp->devnum));
353
354 /* It is generally not OK to add non-spare drives to a
355 * running array as they are probably missing because
356 * they failed. However if runstop is 1, then the
357 * array was possibly started early and our best bet is
358 * to add this anyway.
359 * Also if action policy is re-add or better we allow
360 * re-add.
361 * This doesn't apply to containers as the 'non-spare'
362 * flag has a different meaning. The test has to happen
363 * at the device level there
364 */
365 if (!st->ss->external
366 && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
367 && ! policy_action_allows(policy, st->ss->name,
368 act_re_add)
369 && runstop < 1) {
370 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
371 fprintf(stderr, Name
372 ": not adding %s to active array (without --run) %s\n",
373 devname, chosen_name);
374 rv = 2;
375 goto out;
376 }
377 }
378 if (!sra) {
379 rv = 2;
380 goto out;
381 }
382 if (sra->devs) {
383 sprintf(dn, "%d:%d", sra->devs->disk.major,
384 sra->devs->disk.minor);
385 dfd2 = dev_open(dn, O_RDONLY);
386 st2 = dup_super(st);
387 if (st2->ss->load_super(st2, dfd2, NULL) ||
388 st->ss->compare_super(st, st2) != 0) {
389 fprintf(stderr, Name
390 ": metadata mismatch between %s and "
391 "chosen array %s\n",
392 devname, chosen_name);
393 close(dfd2);
394 rv = 2;
395 goto out;
396 }
397 close(dfd2);
398 st2->ss->getinfo_super(st2, &info2, NULL);
399 st2->ss->free_super(st2);
400 if (info.array.level != info2.array.level ||
401 memcmp(info.uuid, info2.uuid, 16) != 0 ||
402 info.array.raid_disks != info2.array.raid_disks) {
403 fprintf(stderr, Name
404 ": unexpected difference between %s and %s.\n",
405 chosen_name, devname);
406 rv = 2;
407 goto out;
408 }
409 }
410 info2.disk.major = major(stb.st_rdev);
411 info2.disk.minor = minor(stb.st_rdev);
412 /* add disk needs to know about containers */
413 if (st->ss->external)
414 sra->array.level = LEVEL_CONTAINER;
415 err = add_disk(mdfd, st, sra, &info2);
416 if (err < 0 && errno == EBUSY) {
417 /* could be another device present with the same
418 * disk.number. Find and reject any such
419 */
420 find_reject(mdfd, st, sra, info.disk.number,
421 info.events, verbose, chosen_name);
422 err = add_disk(mdfd, st, sra, &info2);
423 }
424 if (err < 0) {
425 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
426 devname, chosen_name, strerror(errno));
427 rv = 2;
428 goto out;
429 }
430 info.array.working_disks = 0;
431 for (d = sra->devs; d; d=d->next)
432 info.array.working_disks ++;
433
434 }
435
436 /* 7/ Is there enough devices to possibly start the array? */
437 /* 7a/ if not, finish with success. */
438 if (info.array.level == LEVEL_CONTAINER) {
439 int devnum = devnum; /* defined and used iff ->external */
440 /* Try to assemble within the container */
441 map_unlock(&map);
442 sysfs_uevent(&info, "change");
443 if (verbose >= 0)
444 fprintf(stderr, Name
445 ": container %s now has %d devices\n",
446 chosen_name, info.array.working_disks);
447 wait_for(chosen_name, mdfd);
448 if (st->ss->external)
449 devnum = fd2devnum(mdfd);
450 close(mdfd);
451 sysfs_free(sra);
452 rv = Incremental(chosen_name, verbose, runstop,
453 NULL, homehost, require_homehost, autof);
454 if (rv == 1)
455 /* Don't fail the whole -I if a subarray didn't
456 * have enough devices to start yet
457 */
458 rv = 0;
459 /* after spare is added, ping monitor for external metadata
460 * so that it can eg. try to rebuild degraded array */
461 if (st->ss->external)
462 ping_monitor_by_id(devnum);
463 return rv;
464 }
465
466 /* We have added something to the array, so need to re-read the
467 * state. Eventually this state should be kept up-to-date as
468 * things change.
469 */
470 sysfs_free(sra);
471 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
472 GET_OFFSET | GET_SIZE));
473 active_disks = count_active(st, sra, mdfd, &avail, &info);
474 if (enough(info.array.level, info.array.raid_disks,
475 info.array.layout, info.array.state & 1,
476 avail, active_disks) == 0) {
477 if (verbose >= 0)
478 fprintf(stderr, Name
479 ": %s attached to %s, not enough to start (%d).\n",
480 devname, chosen_name, active_disks);
481 map_unlock(&map);
482 rv = 0;
483 goto out;
484 }
485
486 /* 7b/ if yes, */
487 /* - if number of OK devices match expected, or -R and there */
488 /* are enough, */
489 /* + add any bitmap file */
490 /* + start the array (auto-readonly). */
491
492 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
493 if (verbose >= 0)
494 fprintf(stderr, Name
495 ": %s attached to %s which is already active.\n",
496 devname, chosen_name);
497 map_unlock(&map);
498 rv = 0;
499 goto out;
500 }
501
502 map_unlock(&map);
503 if (runstop > 0 || active_disks >= info.array.working_disks) {
504 struct mdinfo *dsk;
505 /* Let's try to start it */
506 if (match && match->bitmap_file) {
507 int bmfd = open(match->bitmap_file, O_RDWR);
508 if (bmfd < 0) {
509 fprintf(stderr, Name
510 ": Could not open bitmap file %s.\n",
511 match->bitmap_file);
512 goto out;
513 }
514 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
515 close(bmfd);
516 fprintf(stderr, Name
517 ": Failed to set bitmapfile for %s.\n",
518 chosen_name);
519 goto out;
520 }
521 close(bmfd);
522 }
523 /* Need to remove from the array any devices which
524 * 'count_active' discerned were too old or inappropriate
525 */
526 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
527 if (d->disk.state & (1<<MD_DISK_REMOVED))
528 remove_disk(mdfd, st, sra, d);
529
530 if ((sra == NULL || active_disks >= info.array.working_disks)
531 && trustworthy != FOREIGN)
532 rv = ioctl(mdfd, RUN_ARRAY, NULL);
533 else
534 rv = sysfs_set_str(sra, NULL,
535 "array_state", "read-auto");
536 if (rv == 0) {
537 if (verbose >= 0)
538 fprintf(stderr, Name
539 ": %s attached to %s, which has been started.\n",
540 devname, chosen_name);
541 rv = 0;
542 wait_for(chosen_name, mdfd);
543 /* We just started the array, so some devices
544 * might have been evicted from the array
545 * because their event counts were too old.
546 * If the action=re-add policy is in-force for
547 * those devices we should re-add them now.
548 */
549 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
550 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
551 add_disk(mdfd, st, sra, dsk) == 0)
552 fprintf(stderr, Name
553 ": %s re-added to %s\n",
554 dsk->sys_name, chosen_name);
555 }
556 } else {
557 fprintf(stderr, Name
558 ": %s attached to %s, but failed to start: %s.\n",
559 devname, chosen_name, strerror(errno));
560 rv = 1;
561 }
562 } else {
563 if (verbose >= 0)
564 fprintf(stderr, Name
565 ": %s attached to %s, not enough to start safely.\n",
566 devname, chosen_name);
567 rv = 0;
568 }
569 out:
570 free(avail);
571 if (dfd >= 0)
572 close(dfd);
573 if (mdfd >= 0)
574 close(mdfd);
575 if (policy)
576 dev_policy_free(policy);
577 if (sra)
578 sysfs_free(sra);
579 return rv;
580 }
581
582 static struct mddev_ident *search_mdstat(struct supertype *st,
583 struct mdinfo *info,
584 char *devname,
585 int verbose, int *rvp)
586 {
587 struct mddev_ident *array_list, *match;
588 array_list = conf_get_ident(NULL);
589 match = NULL;
590 for (; array_list; array_list = array_list->next) {
591 if (array_list->uuid_set &&
592 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
593 == 0) {
594 if (verbose >= 2 && array_list->devname)
595 fprintf(stderr, Name
596 ": UUID differs from %s.\n",
597 array_list->devname);
598 continue;
599 }
600 if (array_list->name[0] &&
601 strcasecmp(array_list->name, info->name) != 0) {
602 if (verbose >= 2 && array_list->devname)
603 fprintf(stderr, Name
604 ": Name differs from %s.\n",
605 array_list->devname);
606 continue;
607 }
608 if (array_list->devices &&
609 !match_oneof(array_list->devices, devname)) {
610 if (verbose >= 2 && array_list->devname)
611 fprintf(stderr, Name
612 ": Not a listed device for %s.\n",
613 array_list->devname);
614 continue;
615 }
616 if (array_list->super_minor != UnSet &&
617 array_list->super_minor != info->array.md_minor) {
618 if (verbose >= 2 && array_list->devname)
619 fprintf(stderr, Name
620 ": Different super-minor to %s.\n",
621 array_list->devname);
622 continue;
623 }
624 if (!array_list->uuid_set &&
625 !array_list->name[0] &&
626 !array_list->devices &&
627 array_list->super_minor == UnSet) {
628 if (verbose >= 2 && array_list->devname)
629 fprintf(stderr, Name
630 ": %s doesn't have any identifying information.\n",
631 array_list->devname);
632 continue;
633 }
634 /* FIXME, should I check raid_disks and level too?? */
635
636 if (match) {
637 if (verbose >= 0) {
638 if (match->devname && array_list->devname)
639 fprintf(stderr, Name
640 ": we match both %s and %s - cannot decide which to use.\n",
641 match->devname, array_list->devname);
642 else
643 fprintf(stderr, Name
644 ": multiple lines in mdadm.conf match\n");
645 }
646 *rvp = 2;
647 match = NULL;
648 break;
649 }
650 match = array_list;
651 }
652 return match;
653 }
654
655 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
656 int number, __u64 events, int verbose,
657 char *array_name)
658 {
659 /* Find a device attached to this array with a disk.number of number
660 * and events less than the passed events, and remove the device.
661 */
662 struct mdinfo *d;
663 mdu_array_info_t ra;
664
665 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
666 return; /* not safe to remove from active arrays
667 * without thinking more */
668
669 for (d = sra->devs; d ; d = d->next) {
670 char dn[10];
671 int dfd;
672 struct mdinfo info;
673 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
674 dfd = dev_open(dn, O_RDONLY);
675 if (dfd < 0)
676 continue;
677 if (st->ss->load_super(st, dfd, NULL)) {
678 close(dfd);
679 continue;
680 }
681 st->ss->getinfo_super(st, &info, NULL);
682 st->ss->free_super(st);
683 close(dfd);
684
685 if (info.disk.number != number ||
686 info.events >= events)
687 continue;
688
689 if (d->disk.raid_disk > -1)
690 sysfs_set_str(sra, d, "slot", "none");
691 if (sysfs_set_str(sra, d, "state", "remove") == 0)
692 if (verbose >= 0)
693 fprintf(stderr, Name
694 ": removing old device %s from %s\n",
695 d->sys_name+4, array_name);
696 }
697 }
698
699 static int count_active(struct supertype *st, struct mdinfo *sra,
700 int mdfd, char **availp,
701 struct mdinfo *bestinfo)
702 {
703 /* count how many devices in sra think they are active */
704 struct mdinfo *d;
705 int cnt = 0;
706 __u64 max_events = 0;
707 char *avail = NULL;
708 int *best;
709 char *devmap = NULL;
710 int numdevs = 0;
711 int devnum;
712 int b, i;
713 int raid_disks = 0;
714
715 if (!sra)
716 return 0;
717
718 for (d = sra->devs ; d ; d = d->next)
719 numdevs++;
720 for (d = sra->devs, devnum=0 ; d ; d = d->next, devnum++) {
721 char dn[30];
722 int dfd;
723 int ok;
724 struct mdinfo info;
725
726 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
727 dfd = dev_open(dn, O_RDONLY);
728 if (dfd < 0)
729 continue;
730 ok = st->ss->load_super(st, dfd, NULL);
731 close(dfd);
732 if (ok != 0)
733 continue;
734 info.array.raid_disks = raid_disks;
735 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
736 if (!avail) {
737 raid_disks = info.array.raid_disks;
738 avail = calloc(raid_disks, 1);
739 if (!avail) {
740 fprintf(stderr, Name ": out of memory.\n");
741 exit(1);
742 }
743 *availp = avail;
744
745 best = calloc(raid_disks, sizeof(int));
746 devmap = calloc(raid_disks * numdevs, 1);
747
748 st->ss->getinfo_super(st, &info, devmap);
749 }
750
751 if (info.disk.state & (1<<MD_DISK_SYNC))
752 {
753 if (cnt == 0) {
754 cnt++;
755 max_events = info.events;
756 avail[info.disk.raid_disk] = 2;
757 best[info.disk.raid_disk] = devnum;
758 st->ss->getinfo_super(st, bestinfo, NULL);
759 } else if (info.events == max_events) {
760 avail[info.disk.raid_disk] = 2;
761 best[info.disk.raid_disk] = devnum;
762 } else if (info.events == max_events-1) {
763 if (avail[info.disk.raid_disk] == 0) {
764 avail[info.disk.raid_disk] = 1;
765 best[info.disk.raid_disk] = devnum;
766 }
767 } else if (info.events < max_events - 1)
768 ;
769 else if (info.events == max_events+1) {
770 int i;
771 max_events = info.events;
772 for (i=0; i < raid_disks; i++)
773 if (avail[i])
774 avail[i]--;
775 avail[info.disk.raid_disk] = 2;
776 best[info.disk.raid_disk] = devnum;
777 st->ss->getinfo_super(st, bestinfo, NULL);
778 } else { /* info.events much bigger */
779 memset(avail, 0, info.disk.raid_disk);
780 max_events = info.events;
781 avail[info.disk.raid_disk] = 2;
782 st->ss->getinfo_super(st, bestinfo, NULL);
783 }
784 }
785 st->ss->free_super(st);
786 }
787 if (!avail)
788 return 0;
789 /* We need to reject any device that thinks the best device is
790 * failed or missing */
791 for (b = 0; b < raid_disks; b++)
792 if (avail[b] == 2)
793 break;
794 cnt = 0;
795 for (i = 0 ; i < raid_disks ; i++) {
796 if (i != b && avail[i])
797 if (devmap[raid_disks * best[i] + b] == 0) {
798 /* This device thinks 'b' is failed -
799 * don't use it */
800 devnum = best[i];
801 for (d=sra->devs ; devnum; d = d->next)
802 devnum--;
803 d->disk.state |= (1 << MD_DISK_REMOVED);
804 avail[i] = 0;
805 }
806 if (avail[i])
807 cnt++;
808 }
809 free(best);
810 free(devmap);
811 return cnt;
812 }
813
814 /* test if container has degraded member(s) */
815 static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
816 {
817 mdu_array_info_t array;
818 int afd;
819 int max_degraded = 0;
820
821 for(; map; map = map->next) {
822 if (!is_subarray(map->metadata) ||
823 devname2devnum(map->metadata+1) != me->devnum)
824 continue;
825 afd = open_dev(map->devnum);
826 if (afd < 0)
827 continue;
828 /* most accurate information regarding array degradation */
829 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
830 int degraded = array.raid_disks - array.active_disks -
831 array.spare_disks;
832 if (degraded > max_degraded)
833 max_degraded = degraded;
834 }
835 close(afd);
836 }
837 return (max_degraded);
838 }
839
840 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
841 struct map_ent *target, int bare,
842 struct supertype *st, int verbose)
843 {
844 /* This device doesn't have any md metadata
845 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
846 * If 'st' is not set, then we only know that some metadata allows this,
847 * others possibly don't.
848 * So look for a container or array to attach the device to.
849 * Prefer 'target' if that is set and the array is found.
850 *
851 * If st is set, then only arrays of that type are considered
852 * Return 0 on success, or some exit code on failure, probably 1.
853 */
854 int rv = 1;
855 struct stat stb;
856 struct map_ent *mp, *map = NULL;
857 struct mdinfo *chosen = NULL;
858 int dfd = *dfdp;
859
860 if (fstat(dfd, &stb) != 0)
861 return 1;
862
863 /*
864 * Now we need to find a suitable array to add this to.
865 * We only accept arrays that:
866 * - match 'st'
867 * - are in the same domains as the device
868 * - are of an size for which the device will be useful
869 * and we choose the one that is the most degraded
870 */
871
872 if (map_lock(&map)) {
873 fprintf(stderr, Name ": failed to get exclusive lock on "
874 "mapfile\n");
875 return 1;
876 }
877 for (mp = map ; mp ; mp = mp->next) {
878 struct supertype *st2;
879 struct domainlist *dl = NULL;
880 struct mdinfo *sra;
881 unsigned long long devsize;
882 unsigned long long component_size = 0;
883
884 if (is_subarray(mp->metadata))
885 continue;
886 if (st) {
887 st2 = st->ss->match_metadata_desc(mp->metadata);
888 if (!st2 ||
889 (st->minor_version >= 0 &&
890 st->minor_version != st2->minor_version)) {
891 if (verbose > 1)
892 fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
893 devname, mp->path);
894 free(st2);
895 continue;
896 }
897 free(st2);
898 }
899 sra = sysfs_read(-1, mp->devnum,
900 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
901 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
902 if (!sra) {
903 /* Probably a container - no degraded info */
904 sra = sysfs_read(-1, mp->devnum,
905 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
906 GET_COMPONENT|GET_VERSION);
907 if (sra)
908 sra->array.failed_disks = -1;
909 }
910 if (!sra)
911 continue;
912 if (st == NULL) {
913 int i;
914 st2 = NULL;
915 for(i=0; !st2 && superlist[i]; i++)
916 st2 = superlist[i]->match_metadata_desc(
917 sra->text_version);
918 if (!st2) {
919 if (verbose > 1)
920 fprintf(stderr, Name ": not adding %s to %s"
921 " as metadata not recognised.\n",
922 devname, mp->path);
923 goto next;
924 }
925 /* Need to double check the 'act_spare' permissions applies
926 * to this metadata.
927 */
928 if (!policy_action_allows(pol, st2->ss->name, act_spare))
929 goto next;
930 if (!bare && !policy_action_allows(pol, st2->ss->name,
931 act_spare_same_slot))
932 goto next;
933 } else
934 st2 = st;
935 /* update number of failed disks for mostly degraded
936 * container member */
937 if (sra->array.failed_disks == -1)
938 sra->array.failed_disks = container_members_max_degradation(map, mp);
939
940 get_dev_size(dfd, NULL, &devsize);
941 if (sra->component_size == 0) {
942 /* true for containers, here we must read superblock
943 * to obtain minimum spare size */
944 struct supertype *st3 = dup_super(st2);
945 int mdfd = open_dev(mp->devnum);
946 if (!mdfd)
947 goto next;
948 if (st3->ss->load_container &&
949 !st3->ss->load_container(st3, mdfd, mp->path)) {
950 component_size = st3->ss->min_acceptable_spare_size(st3);
951 st3->ss->free_super(st3);
952 }
953 free(st3);
954 close(mdfd);
955 }
956 if ((sra->component_size > 0 &&
957 st2->ss->avail_size(st2, devsize) < sra->component_size)
958 ||
959 (sra->component_size == 0 && devsize < component_size)) {
960 if (verbose > 1)
961 fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
962 devname, mp->path);
963 goto next;
964 }
965 /* test against target.
966 * If 'target' is set and 'bare' is false, we only accept
967 * arrays/containers that match 'target'.
968 * If 'target' is set and 'bare' is true, we prefer the
969 * array which matches 'target'.
970 * target is considered only if we deal with degraded array
971 */
972 if (target && policy_action_allows(pol, st2->ss->name,
973 act_spare_same_slot)) {
974 if (strcmp(target->metadata, mp->metadata) == 0 &&
975 memcmp(target->uuid, mp->uuid,
976 sizeof(target->uuid)) == 0 &&
977 sra->array.failed_disks > 0) {
978 /* This is our target!! */
979 if (chosen)
980 sysfs_free(chosen);
981 chosen = sra;
982 sra = NULL;
983 /* skip to end so we don't check any more */
984 while (mp->next)
985 mp = mp->next;
986 goto next;
987 }
988 /* not our target */
989 if (!bare)
990 goto next;
991 }
992
993 dl = domain_from_array(sra, st2->ss->name);
994 if (domain_test(dl, pol, st2->ss->name) != 1) {
995 /* domain test fails */
996 if (verbose > 1)
997 fprintf(stderr, Name ": not adding %s to %s as"
998 " it is not in a compatible domain\n",
999 devname, mp->path);
1000
1001 goto next;
1002 }
1003 /* all tests passed, OK to add to this array */
1004 if (!chosen) {
1005 chosen = sra;
1006 sra = NULL;
1007 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
1008 sysfs_free(chosen);
1009 chosen = sra;
1010 sra = NULL;
1011 }
1012 next:
1013 if (sra)
1014 sysfs_free(sra);
1015 if (st != st2)
1016 free(st2);
1017 if (dl)
1018 domain_free(dl);
1019 }
1020 if (chosen) {
1021 /* add current device to chosen array as a spare */
1022 int mdfd = open_dev(devname2devnum(chosen->sys_name));
1023 if (mdfd >= 0) {
1024 struct mddev_dev devlist;
1025 char devname[20];
1026 devlist.next = NULL;
1027 devlist.used = 0;
1028 devlist.re_add = 0;
1029 devlist.writemostly = 0;
1030 devlist.devname = devname;
1031 sprintf(devname, "%d:%d", major(stb.st_rdev),
1032 minor(stb.st_rdev));
1033 devlist.disposition = 'a';
1034 close(dfd);
1035 *dfdp = -1;
1036 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
1037 -1, 0, NULL);
1038 close(mdfd);
1039 }
1040 if (verbose > 0) {
1041 if (rv == 0)
1042 fprintf(stderr, Name ": added %s as spare for %s\n",
1043 devname, chosen->sys_name);
1044 else
1045 fprintf(stderr, Name ": failed to add %s as spare for %s\n",
1046 devname, chosen->sys_name);
1047 }
1048 sysfs_free(chosen);
1049 }
1050 return rv;
1051 }
1052
1053 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1054 struct supertype *st, int verbose)
1055 {
1056 /* we know that at least one partition virtual-metadata is
1057 * allowed to incorporate spares like this device. We need to
1058 * find a suitable device to copy partition information from.
1059 *
1060 * Getting a list of all disk (not partition) devices is
1061 * slightly non-trivial. We could look at /sys/block, but
1062 * that is theoretically due to be removed. Maybe best to use
1063 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1064 * we depend on this directory of 'path' info. But that fails
1065 * to find loop devices and probably others. Maybe don't
1066 * worry about that, they aren't the real target.
1067 *
1068 * So: check things in /dev/disk/by-path to see if they are in
1069 * a compatible domain, then load the partition table and see
1070 * if it is OK for the new device, and choose the largest
1071 * partition table that fits.
1072 */
1073 DIR *dir;
1074 struct dirent *de;
1075 char *chosen = NULL;
1076 unsigned long long chosen_size = 0;
1077 struct supertype *chosen_st = NULL;
1078 int fd;
1079
1080 dir = opendir("/dev/disk/by-path");
1081 if (!dir)
1082 return 1;
1083 while ((de = readdir(dir)) != NULL) {
1084 char *ep;
1085 struct dev_policy *pol2 = NULL;
1086 struct domainlist *domlist = NULL;
1087 int fd = -1;
1088 struct mdinfo info;
1089 struct supertype *st2 = NULL;
1090 char *devname = NULL;
1091 unsigned long long devsectors;
1092
1093 if (de->d_ino == 0 ||
1094 de->d_name[0] == '.' ||
1095 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1096 goto next;
1097
1098 ep = de->d_name + strlen(de->d_name);
1099 while (ep > de->d_name &&
1100 isdigit(ep[-1]))
1101 ep--;
1102 if (ep > de->d_name + 5 &&
1103 strncmp(ep-5, "-part", 5) == 0)
1104 /* This is a partition - skip it */
1105 goto next;
1106
1107 pol2 = path_policy(de->d_name, type_disk);
1108
1109 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1110 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1111 /* new device is incompatible with this device. */
1112 goto next;
1113
1114 domain_free(domlist);
1115 domlist = NULL;
1116
1117 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1118 devname = NULL;
1119 goto next;
1120 }
1121 fd = open(devname, O_RDONLY);
1122 if (fd < 0)
1123 goto next;
1124 if (get_dev_size(fd, devname, &devsectors) == 0)
1125 goto next;
1126 devsectors >>= 9;
1127
1128 if (st)
1129 st2 = dup_super(st);
1130 else
1131 st2 = guess_super_type(fd, guess_partitions);
1132 if (st2 == NULL ||
1133 st2->ss->load_super(st2, fd, NULL) < 0)
1134 goto next;
1135
1136 if (!st) {
1137 /* Check domain policy again, this time referring to metadata */
1138 domain_merge(&domlist, pol2, st2->ss->name);
1139 if (domain_test(domlist, pol, st2->ss->name) != 1)
1140 /* Incompatible devices for this metadata type */
1141 goto next;
1142 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1143 /* Some partition types allow sparing, but not
1144 * this one.
1145 */
1146 goto next;
1147 }
1148
1149 st2->ss->getinfo_super(st2, &info, NULL);
1150 if (info.component_size > devsectors)
1151 /* This partitioning doesn't fit in the device */
1152 goto next;
1153
1154 /* This is an acceptable device to copy partition
1155 * metadata from. We could just stop here, but I
1156 * think I want to keep looking incase a larger
1157 * metadata which makes better use of the device can
1158 * be found.
1159 */
1160 if (chosen == NULL ||
1161 chosen_size < info.component_size) {
1162 chosen_size = info.component_size;
1163 free(chosen);
1164 chosen = devname;
1165 devname = NULL;
1166 if (chosen_st) {
1167 chosen_st->ss->free_super(chosen_st);
1168 free(chosen_st);
1169 }
1170 chosen_st = st2;
1171 st2 = NULL;
1172 }
1173
1174 next:
1175 free(devname);
1176 domain_free(domlist);
1177 dev_policy_free(pol2);
1178 if (st2)
1179 st2->ss->free_super(st2);
1180 free(st2);
1181
1182 if (fd >= 0)
1183 close(fd);
1184 }
1185
1186 if (!chosen)
1187 return 1;
1188
1189 /* 'chosen' is the best device we can find. Let's write its
1190 * metadata to devname dfd is read-only so don't use that
1191 */
1192 fd = open(devname, O_RDWR);
1193 if (fd >= 0) {
1194 chosen_st->ss->store_super(chosen_st, fd);
1195 close(fd);
1196 }
1197 free(chosen);
1198 chosen_st->ss->free_super(chosen_st);
1199 free(chosen_st);
1200 return 0;
1201 }
1202
1203 static int is_bare(int dfd)
1204 {
1205 unsigned long long size = 0;
1206 char bufpad[4096 + 4096];
1207 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1208
1209 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1210 read(dfd, buf, 4096) != 4096)
1211 return 0;
1212
1213 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1214 return 0;
1215 if (memcmp(buf, buf+1, 4095) != 0)
1216 return 0;
1217
1218 /* OK, first 4K appear blank, try the end. */
1219 get_dev_size(dfd, NULL, &size);
1220 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1221 read(dfd, buf, 4096) != 4096)
1222 return 0;
1223
1224 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1225 return 0;
1226 if (memcmp(buf, buf+1, 4095) != 0)
1227 return 0;
1228
1229 return 1;
1230 }
1231
1232 /* adding a spare to a regular array is quite different from adding one to
1233 * a set-of-partitions virtual array.
1234 * This function determines which is worth trying and tries as appropriate.
1235 * Arrays are given priority over partitions.
1236 */
1237 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1238 struct map_ent *target,
1239 struct supertype *st, int verbose)
1240 {
1241 int i;
1242 int rv;
1243 int arrays_ok = 0;
1244 int partitions_ok = 0;
1245 int dfd = *dfdp;
1246 int bare;
1247
1248 /* Can only add a spare if device has at least one domain */
1249 if (pol_find(pol, pol_domain) == NULL)
1250 return 1;
1251 /* And only if some action allows spares */
1252 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1253 return 1;
1254
1255 /* Now check if the device is bare.
1256 * bare devices can always be added as a spare
1257 * non-bare devices can only be added if spare-same-slot is permitted,
1258 * and this device is replacing a previous device - in which case 'target'
1259 * will be set.
1260 */
1261 if (!is_bare(dfd)) {
1262 /* Must have a target and allow same_slot */
1263 /* Later - may allow force_spare without target */
1264 if (!target ||
1265 !policy_action_allows(pol, st?st->ss->name:NULL,
1266 act_spare_same_slot)) {
1267 if (verbose > 1)
1268 fprintf(stderr, Name ": %s is not bare, so not "
1269 "considering as a spare\n",
1270 devname);
1271 return 1;
1272 }
1273 bare = 0;
1274 } else
1275 bare = 1;
1276
1277 /* It might be OK to add this device to an array - need to see
1278 * what arrays might be candidates.
1279 */
1280 if (st) {
1281 /* just try try 'array' or 'partition' based on this metadata */
1282 if (st->ss->add_to_super)
1283 return array_try_spare(devname, dfdp, pol, target, bare,
1284 st, verbose);
1285 else
1286 return partition_try_spare(devname, dfdp, pol,
1287 st, verbose);
1288 }
1289 /* No metadata was specified or found so options are open.
1290 * Check for whether any array metadata, or any partition metadata
1291 * might allow adding the spare. This check is just help to avoid
1292 * a more costly scan of all arrays when we can be sure that will
1293 * fail.
1294 */
1295 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1296 if (superlist[i]->add_to_super && !arrays_ok &&
1297 policy_action_allows(pol, superlist[i]->name, act_spare))
1298 arrays_ok = 1;
1299 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1300 policy_action_allows(pol, superlist[i]->name, act_spare))
1301 partitions_ok = 1;
1302 }
1303 rv = 1;
1304 if (arrays_ok)
1305 rv = array_try_spare(devname, dfdp, pol, target, bare,
1306 st, verbose);
1307 if (rv != 0 && partitions_ok)
1308 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1309 return rv;
1310 }
1311
1312 int IncrementalScan(int verbose)
1313 {
1314 /* look at every device listed in the 'map' file.
1315 * If one is found that is not running then:
1316 * look in mdadm.conf for bitmap file.
1317 * if one exists, but array has none, add it.
1318 * try to start array in auto-readonly mode
1319 */
1320 struct map_ent *mapl = NULL;
1321 struct map_ent *me;
1322 struct mddev_ident *devs, *mddev;
1323 int rv = 0;
1324
1325 map_read(&mapl);
1326 devs = conf_get_ident(NULL);
1327
1328 for (me = mapl ; me ; me = me->next) {
1329 mdu_array_info_t array;
1330 mdu_bitmap_file_t bmf;
1331 struct mdinfo *sra;
1332 int mdfd = open_dev(me->devnum);
1333
1334 if (mdfd < 0)
1335 continue;
1336 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1337 errno != ENODEV) {
1338 close(mdfd);
1339 continue;
1340 }
1341 /* Ok, we can try this one. Maybe it needs a bitmap */
1342 for (mddev = devs ; mddev ; mddev = mddev->next)
1343 if (mddev->devname && me->path
1344 && devname_matches(mddev->devname, me->path))
1345 break;
1346 if (mddev && mddev->bitmap_file) {
1347 /*
1348 * Note: early kernels will wrongly fail this, so it
1349 * is a hint only
1350 */
1351 int added = -1;
1352 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1353 int bmfd = open(mddev->bitmap_file, O_RDWR);
1354 if (bmfd >= 0) {
1355 added = ioctl(mdfd, SET_BITMAP_FILE,
1356 bmfd);
1357 close(bmfd);
1358 }
1359 }
1360 if (verbose >= 0) {
1361 if (added == 0)
1362 fprintf(stderr, Name
1363 ": Added bitmap %s to %s\n",
1364 mddev->bitmap_file, me->path);
1365 else if (errno != EEXIST)
1366 fprintf(stderr, Name
1367 ": Failed to add bitmap to %s: %s\n",
1368 me->path, strerror(errno));
1369 }
1370 }
1371 sra = sysfs_read(mdfd, 0, 0);
1372 if (sra) {
1373 if (sysfs_set_str(sra, NULL,
1374 "array_state", "read-auto") == 0) {
1375 if (verbose >= 0)
1376 fprintf(stderr, Name
1377 ": started array %s\n",
1378 me->path ?: devnum2devname(me->devnum));
1379 } else {
1380 fprintf(stderr, Name
1381 ": failed to start array %s: %s\n",
1382 me->path ?: devnum2devname(me->devnum),
1383 strerror(errno));
1384 rv = 1;
1385 }
1386 }
1387 }
1388 return rv;
1389 }
1390
1391 static char *container2devname(char *devname)
1392 {
1393 char *mdname = NULL;
1394
1395 if (devname[0] == '/') {
1396 int fd = open(devname, O_RDONLY);
1397 if (fd >= 0) {
1398 mdname = devnum2devname(fd2devnum(fd));
1399 close(fd);
1400 }
1401 } else {
1402 int uuid[4];
1403 struct map_ent *mp, *map = NULL;
1404
1405 if (!parse_uuid(devname, uuid))
1406 return mdname;
1407 mp = map_by_uuid(&map, uuid);
1408 if (mp)
1409 mdname = devnum2devname(mp->devnum);
1410 map_free(map);
1411 }
1412
1413 return mdname;
1414 }
1415
1416 static int Incremental_container(struct supertype *st, char *devname,
1417 char *homehost, int verbose,
1418 int runstop, int autof)
1419 {
1420 /* Collect the contents of this container and for each
1421 * array, choose a device name and assemble the array.
1422 */
1423
1424 struct mdinfo *list;
1425 struct mdinfo *ra;
1426 struct map_ent *map = NULL;
1427 struct mdinfo info;
1428 int trustworthy;
1429 struct mddev_ident *match;
1430 int rv = 0;
1431 struct domainlist *domains;
1432 struct map_ent *smp;
1433 int suuid[4];
1434 int sfd;
1435
1436 st->ss->getinfo_super(st, &info, NULL);
1437
1438 if ((runstop > 0 && info.container_enough >= 0) ||
1439 info.container_enough > 0)
1440 /* pass */;
1441 else {
1442 if (verbose)
1443 fprintf(stderr, Name ": not enough devices to start the container\n");
1444 return 0;
1445 }
1446
1447 match = search_mdstat(st, &info, devname, verbose, &rv);
1448 if (match == NULL && rv == 2)
1449 return rv;
1450
1451 /* Need to compute 'trustworthy' */
1452 if (match)
1453 trustworthy = LOCAL;
1454 else if (st->ss->match_home(st, homehost) == 1)
1455 trustworthy = LOCAL;
1456 else if (st->ss->match_home(st, "any") == 1)
1457 trustworthy = LOCAL;
1458 else
1459 trustworthy = FOREIGN;
1460
1461 list = st->ss->container_content(st, NULL);
1462 if (map_lock(&map))
1463 fprintf(stderr, Name ": failed to get exclusive lock on "
1464 "mapfile\n");
1465 /* do not assemble arrays that might have bad blocks */
1466 if (list->array.state & (1<<MD_SB_BBM_ERRORS)) {
1467 fprintf(stderr, Name ": BBM log found in metadata. "
1468 "Cannot activate array(s).\n");
1469 /* free container data and exit */
1470 sysfs_free(list);
1471 return 2;
1472 }
1473
1474 for (ra = list ; ra ; ra = ra->next) {
1475 int mdfd;
1476 char chosen_name[1024];
1477 struct map_ent *mp;
1478 struct mddev_ident *match = NULL;
1479
1480 mp = map_by_uuid(&map, ra->uuid);
1481
1482 if (mp) {
1483 mdfd = open_dev(mp->devnum);
1484 if (mp->path)
1485 strcpy(chosen_name, mp->path);
1486 else
1487 strcpy(chosen_name, devnum2devname(mp->devnum));
1488 } else {
1489
1490 /* Check in mdadm.conf for container == devname and
1491 * member == ra->text_version after second slash.
1492 */
1493 char *sub = strchr(ra->text_version+1, '/');
1494 struct mddev_ident *array_list;
1495 if (sub) {
1496 sub++;
1497 array_list = conf_get_ident(NULL);
1498 } else
1499 array_list = NULL;
1500 for(; array_list ; array_list = array_list->next) {
1501 char *dn;
1502 if (array_list->member == NULL ||
1503 array_list->container == NULL)
1504 continue;
1505 if (strcmp(array_list->member, sub) != 0)
1506 continue;
1507 if (array_list->uuid_set &&
1508 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1509 continue;
1510 dn = container2devname(array_list->container);
1511 if (dn == NULL)
1512 continue;
1513 if (strncmp(dn, ra->text_version+1,
1514 strlen(dn)) != 0 ||
1515 ra->text_version[strlen(dn)+1] != '/') {
1516 free(dn);
1517 continue;
1518 }
1519 free(dn);
1520 /* we have a match */
1521 match = array_list;
1522 if (verbose>0)
1523 fprintf(stderr, Name ": match found for member %s\n",
1524 array_list->member);
1525 break;
1526 }
1527
1528 if (match && match->devname &&
1529 strcasecmp(match->devname, "<ignore>") == 0) {
1530 if (verbose > 0)
1531 fprintf(stderr, Name ": array %s/%s is "
1532 "explicitly ignored by mdadm.conf\n",
1533 match->container, match->member);
1534 return 2;
1535 }
1536 if (match)
1537 trustworthy = LOCAL;
1538
1539 mdfd = create_mddev(match ? match->devname : NULL,
1540 ra->name,
1541 autof,
1542 trustworthy,
1543 chosen_name);
1544 }
1545
1546 if (mdfd < 0) {
1547 fprintf(stderr, Name ": failed to open %s: %s.\n",
1548 chosen_name, strerror(errno));
1549 return 2;
1550 }
1551
1552 assemble_container_content(st, mdfd, ra, runstop,
1553 chosen_name, verbose, NULL);
1554 close(mdfd);
1555 }
1556
1557 /* Now move all suitable spares from spare container */
1558 domains = domain_from_array(list, st->ss->name);
1559 memcpy(suuid, uuid_zero, sizeof(int[4]));
1560 if (domains &&
1561 (smp = map_by_uuid(&map, suuid)) != NULL &&
1562 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1563 /* spare container found */
1564 struct supertype *sst =
1565 super_imsm.match_metadata_desc("imsm");
1566 struct mdinfo *sinfo;
1567 unsigned long long min_size = 0;
1568 if (st->ss->min_acceptable_spare_size)
1569 min_size = st->ss->min_acceptable_spare_size(st);
1570 if (!sst->ss->load_container(sst, sfd, NULL)) {
1571 close(sfd);
1572 sinfo = container_choose_spares(sst, min_size,
1573 domains, NULL,
1574 st->ss->name, 0);
1575 sst->ss->free_super(sst);
1576 if (sinfo){
1577 int count = 0;
1578 struct mdinfo *disks = sinfo->devs;
1579 while (disks) {
1580 /* move spare from spare
1581 * container to currently
1582 * assembled one
1583 */
1584 if (move_spare(
1585 smp->path,
1586 devname,
1587 makedev(disks->disk.major,
1588 disks->disk.minor)))
1589 count++;
1590 disks = disks->next;
1591 }
1592 if (count)
1593 fprintf(stderr, Name
1594 ": Added %d spare%s to %s\n",
1595 count, count>1?"s":"", devname);
1596 }
1597 sysfs_free(sinfo);
1598 } else
1599 close(sfd);
1600 }
1601 domain_free(domains);
1602 map_unlock(&map);
1603 return 0;
1604 }
1605
1606 /*
1607 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1608 * raid arrays, and if so first fail (if needed) and then remove the device.
1609 *
1610 * @devname - The device we want to remove
1611 * @id_path - name as found in /dev/disk/by-path for this device
1612 *
1613 * Note: the device name must be a kernel name like "sda", so
1614 * that we can find it in /proc/mdstat
1615 */
1616 int IncrementalRemove(char *devname, char *id_path, int verbose)
1617 {
1618 int mdfd;
1619 int rv;
1620 struct mdstat_ent *ent;
1621 struct mddev_dev devlist;
1622
1623 if (!id_path)
1624 dprintf(Name ": incremental removal without --path <id_path> "
1625 "lacks the possibility to re-add new device in this "
1626 "port\n");
1627
1628 if (strchr(devname, '/')) {
1629 fprintf(stderr, Name ": incremental removal requires a "
1630 "kernel device name, not a file: %s\n", devname);
1631 return 1;
1632 }
1633 ent = mdstat_by_component(devname);
1634 if (!ent) {
1635 fprintf(stderr, Name ": %s does not appear to be a component "
1636 "of any array\n", devname);
1637 return 1;
1638 }
1639 mdfd = open_dev(ent->devnum);
1640 if (mdfd < 0) {
1641 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
1642 free_mdstat(ent);
1643 return 1;
1644 }
1645
1646 if (id_path) {
1647 struct map_ent *map = NULL, *me;
1648 me = map_by_devnum(&map, ent->devnum);
1649 if (me)
1650 policy_save_path(id_path, me);
1651 map_free(map);
1652 }
1653
1654 memset(&devlist, 0, sizeof(devlist));
1655 devlist.devname = devname;
1656 devlist.disposition = 'f';
1657 /* for a container, we must fail each member array */
1658 if (ent->metadata_version &&
1659 strncmp(ent->metadata_version, "external:", 9) == 0) {
1660 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1661 struct mdstat_ent *memb;
1662 for (memb = mdstat ; memb ; memb = memb->next)
1663 if (is_container_member(memb, ent->dev)) {
1664 int subfd = open_dev(memb->devnum);
1665 if (subfd >= 0) {
1666 Manage_subdevs(memb->dev, subfd,
1667 &devlist, verbose, 0,
1668 NULL);
1669 close(subfd);
1670 }
1671 }
1672 free_mdstat(mdstat);
1673 } else
1674 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
1675 devlist.disposition = 'r';
1676 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
1677 close(mdfd);
1678 free_mdstat(ent);
1679 return rv;
1680 }