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