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