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