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