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