]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Change some "fprintf(stderr,"s to pr_err.
[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 free(best);
764 free(devmap);
765 return cnt + replcnt;
766 }
767
768 /* test if container has degraded member(s) */
769 static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
770 {
771 mdu_array_info_t array;
772 int afd;
773 int max_degraded = 0;
774
775 for(; map; map = map->next) {
776 if (!metadata_container_matches(map->metadata, me->devnm))
777 continue;
778 afd = open_dev(map->devnm);
779 if (afd < 0)
780 continue;
781 /* most accurate information regarding array degradation */
782 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
783 int degraded = array.raid_disks - array.active_disks -
784 array.spare_disks;
785 if (degraded > max_degraded)
786 max_degraded = degraded;
787 }
788 close(afd);
789 }
790 return (max_degraded);
791 }
792
793 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
794 struct map_ent *target, int bare,
795 struct supertype *st, int verbose)
796 {
797 /* This device doesn't have any md metadata
798 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
799 * If 'st' is not set, then we only know that some metadata allows this,
800 * others possibly don't.
801 * So look for a container or array to attach the device to.
802 * Prefer 'target' if that is set and the array is found.
803 *
804 * If st is set, then only arrays of that type are considered
805 * Return 0 on success, or some exit code on failure, probably 1.
806 */
807 int rv = 1;
808 struct stat stb;
809 struct map_ent *mp, *map = NULL;
810 struct mdinfo *chosen = NULL;
811 int dfd = *dfdp;
812
813 if (fstat(dfd, &stb) != 0)
814 return 1;
815
816 /*
817 * Now we need to find a suitable array to add this to.
818 * We only accept arrays that:
819 * - match 'st'
820 * - are in the same domains as the device
821 * - are of an size for which the device will be useful
822 * and we choose the one that is the most degraded
823 */
824
825 if (map_lock(&map)) {
826 pr_err("failed to get exclusive lock on "
827 "mapfile\n");
828 return 1;
829 }
830 for (mp = map ; mp ; mp = mp->next) {
831 struct supertype *st2;
832 struct domainlist *dl = NULL;
833 struct mdinfo *sra;
834 unsigned long long devsize;
835 unsigned long long component_size = 0;
836
837 if (is_subarray(mp->metadata))
838 continue;
839 if (st) {
840 st2 = st->ss->match_metadata_desc(mp->metadata);
841 if (!st2 ||
842 (st->minor_version >= 0 &&
843 st->minor_version != st2->minor_version)) {
844 if (verbose > 1)
845 pr_err("not adding %s to %s as metadata type doesn't match\n",
846 devname, mp->path);
847 free(st2);
848 continue;
849 }
850 free(st2);
851 }
852 sra = sysfs_read(-1, mp->devnm,
853 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
854 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
855 if (!sra) {
856 /* Probably a container - no degraded info */
857 sra = sysfs_read(-1, mp->devnm,
858 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
859 GET_COMPONENT|GET_VERSION);
860 if (sra)
861 sra->array.failed_disks = -1;
862 }
863 if (!sra)
864 continue;
865 if (st == NULL) {
866 int i;
867 st2 = NULL;
868 for(i = 0; !st2 && superlist[i]; i++)
869 st2 = superlist[i]->match_metadata_desc(
870 sra->text_version);
871 if (!st2) {
872 if (verbose > 1)
873 pr_err("not adding %s to %s"
874 " as metadata not recognised.\n",
875 devname, mp->path);
876 goto next;
877 }
878 /* Need to double check the 'act_spare' permissions applies
879 * to this metadata.
880 */
881 if (!policy_action_allows(pol, st2->ss->name, act_spare))
882 goto next;
883 if (!bare && !policy_action_allows(pol, st2->ss->name,
884 act_spare_same_slot))
885 goto next;
886 } else
887 st2 = st;
888 /* update number of failed disks for mostly degraded
889 * container member */
890 if (sra->array.failed_disks == -1)
891 sra->array.failed_disks = container_members_max_degradation(map, mp);
892
893 get_dev_size(dfd, NULL, &devsize);
894 if (sra->component_size == 0) {
895 /* true for containers, here we must read superblock
896 * to obtain minimum spare size */
897 struct supertype *st3 = dup_super(st2);
898 int mdfd = open_dev(mp->devnm);
899 if (mdfd < 0) {
900 free(st3);
901 goto next;
902 }
903 if (st3->ss->load_container &&
904 !st3->ss->load_container(st3, mdfd, mp->path)) {
905 component_size = st3->ss->min_acceptable_spare_size(st3);
906 st3->ss->free_super(st3);
907 }
908 free(st3);
909 close(mdfd);
910 }
911 if ((sra->component_size > 0 &&
912 st2->ss->avail_size(st2, devsize,
913 sra->devs
914 ? sra->devs->data_offset
915 : INVALID_SECTORS)
916 < sra->component_size)
917 ||
918 (sra->component_size == 0 && devsize < component_size)) {
919 if (verbose > 1)
920 pr_err("not adding %s to %s as it is too small\n",
921 devname, mp->path);
922 goto next;
923 }
924 /* test against target.
925 * If 'target' is set and 'bare' is false, we only accept
926 * arrays/containers that match 'target'.
927 * If 'target' is set and 'bare' is true, we prefer the
928 * array which matches 'target'.
929 * target is considered only if we deal with degraded array
930 */
931 if (target && policy_action_allows(pol, st2->ss->name,
932 act_spare_same_slot)) {
933 if (strcmp(target->metadata, mp->metadata) == 0 &&
934 memcmp(target->uuid, mp->uuid,
935 sizeof(target->uuid)) == 0 &&
936 sra->array.failed_disks > 0) {
937 /* This is our target!! */
938 if (chosen)
939 sysfs_free(chosen);
940 chosen = sra;
941 sra = NULL;
942 /* skip to end so we don't check any more */
943 while (mp->next)
944 mp = mp->next;
945 goto next;
946 }
947 /* not our target */
948 if (!bare)
949 goto next;
950 }
951
952 dl = domain_from_array(sra, st2->ss->name);
953 if (domain_test(dl, pol, st2->ss->name) != 1) {
954 /* domain test fails */
955 if (verbose > 1)
956 pr_err("not adding %s to %s as"
957 " it is not in a compatible domain\n",
958 devname, mp->path);
959
960 goto next;
961 }
962 /* all tests passed, OK to add to this array */
963 if (!chosen) {
964 chosen = sra;
965 sra = NULL;
966 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
967 sysfs_free(chosen);
968 chosen = sra;
969 sra = NULL;
970 }
971 next:
972 if (sra)
973 sysfs_free(sra);
974 if (st != st2)
975 free(st2);
976 if (dl)
977 domain_free(dl);
978 }
979 if (chosen) {
980 /* add current device to chosen array as a spare */
981 int mdfd = open_dev(chosen->sys_name);
982 if (mdfd >= 0) {
983 struct mddev_dev devlist;
984 char devname[20];
985 devlist.next = NULL;
986 devlist.used = 0;
987 devlist.writemostly = 0;
988 devlist.devname = devname;
989 sprintf(devname, "%d:%d", major(stb.st_rdev),
990 minor(stb.st_rdev));
991 devlist.disposition = 'a';
992 close(dfd);
993 *dfdp = -1;
994 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
995 -1, 0, NULL, 0);
996 close(mdfd);
997 }
998 if (verbose > 0) {
999 if (rv == 0)
1000 pr_err("added %s as spare for %s\n",
1001 devname, chosen->sys_name);
1002 else
1003 pr_err("failed to add %s as spare for %s\n",
1004 devname, chosen->sys_name);
1005 }
1006 sysfs_free(chosen);
1007 }
1008 map_unlock(&map);
1009 return rv;
1010 }
1011
1012 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1013 struct supertype *st, int verbose)
1014 {
1015 /* we know that at least one partition virtual-metadata is
1016 * allowed to incorporate spares like this device. We need to
1017 * find a suitable device to copy partition information from.
1018 *
1019 * Getting a list of all disk (not partition) devices is
1020 * slightly non-trivial. We could look at /sys/block, but
1021 * that is theoretically due to be removed. Maybe best to use
1022 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1023 * we depend on this directory of 'path' info. But that fails
1024 * to find loop devices and probably others. Maybe don't
1025 * worry about that, they aren't the real target.
1026 *
1027 * So: check things in /dev/disk/by-path to see if they are in
1028 * a compatible domain, then load the partition table and see
1029 * if it is OK for the new device, and choose the largest
1030 * partition table that fits.
1031 */
1032 DIR *dir;
1033 struct dirent *de;
1034 char *chosen = NULL;
1035 unsigned long long chosen_size = 0;
1036 struct supertype *chosen_st = NULL;
1037 int fd;
1038
1039 dir = opendir("/dev/disk/by-path");
1040 if (!dir)
1041 return 1;
1042 while ((de = readdir(dir)) != NULL) {
1043 char *ep;
1044 struct dev_policy *pol2 = NULL;
1045 struct domainlist *domlist = NULL;
1046 int fd = -1;
1047 struct mdinfo info;
1048 struct supertype *st2 = NULL;
1049 char *devname = NULL;
1050 unsigned long long devsectors;
1051
1052 if (de->d_ino == 0 ||
1053 de->d_name[0] == '.' ||
1054 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1055 goto next;
1056
1057 ep = de->d_name + strlen(de->d_name);
1058 while (ep > de->d_name &&
1059 isdigit(ep[-1]))
1060 ep--;
1061 if (ep > de->d_name + 5 &&
1062 strncmp(ep-5, "-part", 5) == 0)
1063 /* This is a partition - skip it */
1064 goto next;
1065
1066 pol2 = path_policy(de->d_name, type_disk);
1067
1068 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1069 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1070 /* new device is incompatible with this device. */
1071 goto next;
1072
1073 domain_free(domlist);
1074 domlist = NULL;
1075
1076 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1077 devname = NULL;
1078 goto next;
1079 }
1080 fd = open(devname, O_RDONLY);
1081 if (fd < 0)
1082 goto next;
1083 if (get_dev_size(fd, devname, &devsectors) == 0)
1084 goto next;
1085 devsectors >>= 9;
1086
1087 if (st)
1088 st2 = dup_super(st);
1089 else
1090 st2 = guess_super_type(fd, guess_partitions);
1091 if (st2 == NULL ||
1092 st2->ss->load_super(st2, fd, NULL) < 0)
1093 goto next;
1094
1095 if (!st) {
1096 /* Check domain policy again, this time referring to metadata */
1097 domain_merge(&domlist, pol2, st2->ss->name);
1098 if (domain_test(domlist, pol, st2->ss->name) != 1)
1099 /* Incompatible devices for this metadata type */
1100 goto next;
1101 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1102 /* Some partition types allow sparing, but not
1103 * this one.
1104 */
1105 goto next;
1106 }
1107
1108 st2->ss->getinfo_super(st2, &info, NULL);
1109 if (info.component_size > devsectors)
1110 /* This partitioning doesn't fit in the device */
1111 goto next;
1112
1113 /* This is an acceptable device to copy partition
1114 * metadata from. We could just stop here, but I
1115 * think I want to keep looking incase a larger
1116 * metadata which makes better use of the device can
1117 * be found.
1118 */
1119 if (chosen == NULL ||
1120 chosen_size < info.component_size) {
1121 chosen_size = info.component_size;
1122 free(chosen);
1123 chosen = devname;
1124 devname = NULL;
1125 if (chosen_st) {
1126 chosen_st->ss->free_super(chosen_st);
1127 free(chosen_st);
1128 }
1129 chosen_st = st2;
1130 st2 = NULL;
1131 }
1132
1133 next:
1134 free(devname);
1135 domain_free(domlist);
1136 dev_policy_free(pol2);
1137 if (st2)
1138 st2->ss->free_super(st2);
1139 free(st2);
1140
1141 if (fd >= 0)
1142 close(fd);
1143 }
1144
1145 closedir(dir);
1146
1147 if (!chosen)
1148 return 1;
1149
1150 /* 'chosen' is the best device we can find. Let's write its
1151 * metadata to devname dfd is read-only so don't use that
1152 */
1153 fd = open(devname, O_RDWR);
1154 if (fd >= 0) {
1155 chosen_st->ss->store_super(chosen_st, fd);
1156 close(fd);
1157 }
1158 free(chosen);
1159 chosen_st->ss->free_super(chosen_st);
1160 free(chosen_st);
1161 return 0;
1162 }
1163
1164 static int is_bare(int dfd)
1165 {
1166 unsigned long long size = 0;
1167 char bufpad[4096 + 4096];
1168 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1169
1170 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1171 read(dfd, buf, 4096) != 4096)
1172 return 0;
1173
1174 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1175 return 0;
1176 if (memcmp(buf, buf+1, 4095) != 0)
1177 return 0;
1178
1179 /* OK, first 4K appear blank, try the end. */
1180 get_dev_size(dfd, NULL, &size);
1181 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1182 read(dfd, buf, 4096) != 4096)
1183 return 0;
1184
1185 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1186 return 0;
1187 if (memcmp(buf, buf+1, 4095) != 0)
1188 return 0;
1189
1190 return 1;
1191 }
1192
1193 /* adding a spare to a regular array is quite different from adding one to
1194 * a set-of-partitions virtual array.
1195 * This function determines which is worth trying and tries as appropriate.
1196 * Arrays are given priority over partitions.
1197 */
1198 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1199 struct map_ent *target,
1200 struct supertype *st, int verbose)
1201 {
1202 int i;
1203 int rv;
1204 int arrays_ok = 0;
1205 int partitions_ok = 0;
1206 int dfd = *dfdp;
1207 int bare;
1208
1209 /* Can only add a spare if device has at least one domain */
1210 if (pol_find(pol, pol_domain) == NULL)
1211 return 1;
1212 /* And only if some action allows spares */
1213 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1214 return 1;
1215
1216 /* Now check if the device is bare.
1217 * bare devices can always be added as a spare
1218 * non-bare devices can only be added if spare-same-slot is permitted,
1219 * and this device is replacing a previous device - in which case 'target'
1220 * will be set.
1221 */
1222 if (!is_bare(dfd)) {
1223 /* Must have a target and allow same_slot */
1224 /* Later - may allow force_spare without target */
1225 if (!target ||
1226 !policy_action_allows(pol, st?st->ss->name:NULL,
1227 act_spare_same_slot)) {
1228 if (verbose > 1)
1229 pr_err("%s is not bare, so not "
1230 "considering as a spare\n",
1231 devname);
1232 return 1;
1233 }
1234 bare = 0;
1235 } else
1236 bare = 1;
1237
1238 /* It might be OK to add this device to an array - need to see
1239 * what arrays might be candidates.
1240 */
1241 if (st) {
1242 /* just try try 'array' or 'partition' based on this metadata */
1243 if (st->ss->add_to_super)
1244 return array_try_spare(devname, dfdp, pol, target, bare,
1245 st, verbose);
1246 else
1247 return partition_try_spare(devname, dfdp, pol,
1248 st, verbose);
1249 }
1250 /* No metadata was specified or found so options are open.
1251 * Check for whether any array metadata, or any partition metadata
1252 * might allow adding the spare. This check is just help to avoid
1253 * a more costly scan of all arrays when we can be sure that will
1254 * fail.
1255 */
1256 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1257 if (superlist[i]->add_to_super && !arrays_ok &&
1258 policy_action_allows(pol, superlist[i]->name, act_spare))
1259 arrays_ok = 1;
1260 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1261 policy_action_allows(pol, superlist[i]->name, act_spare))
1262 partitions_ok = 1;
1263 }
1264 rv = 1;
1265 if (arrays_ok)
1266 rv = array_try_spare(devname, dfdp, pol, target, bare,
1267 st, verbose);
1268 if (rv != 0 && partitions_ok)
1269 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1270 return rv;
1271 }
1272
1273 int IncrementalScan(int verbose)
1274 {
1275 /* look at every device listed in the 'map' file.
1276 * If one is found that is not running then:
1277 * look in mdadm.conf for bitmap file.
1278 * if one exists, but array has none, add it.
1279 * try to start array in auto-readonly mode
1280 */
1281 struct map_ent *mapl = NULL;
1282 struct map_ent *me;
1283 struct mddev_ident *devs, *mddev;
1284 int rv = 0;
1285
1286 map_read(&mapl);
1287 devs = conf_get_ident(NULL);
1288
1289 for (me = mapl ; me ; me = me->next) {
1290 mdu_array_info_t array;
1291 mdu_bitmap_file_t bmf;
1292 struct mdinfo *sra;
1293 int mdfd = open_dev(me->devnm);
1294
1295 if (mdfd < 0)
1296 continue;
1297 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1298 errno != ENODEV) {
1299 close(mdfd);
1300 continue;
1301 }
1302 /* Ok, we can try this one. Maybe it needs a bitmap */
1303 for (mddev = devs ; mddev ; mddev = mddev->next)
1304 if (mddev->devname && me->path
1305 && devname_matches(mddev->devname, me->path))
1306 break;
1307 if (mddev && mddev->bitmap_file) {
1308 /*
1309 * Note: early kernels will wrongly fail this, so it
1310 * is a hint only
1311 */
1312 int added = -1;
1313 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1314 int bmfd = open(mddev->bitmap_file, O_RDWR);
1315 if (bmfd >= 0) {
1316 added = ioctl(mdfd, SET_BITMAP_FILE,
1317 bmfd);
1318 close(bmfd);
1319 }
1320 }
1321 if (verbose >= 0) {
1322 if (added == 0)
1323 pr_err("Added bitmap %s to %s\n",
1324 mddev->bitmap_file, me->path);
1325 else if (errno != EEXIST)
1326 pr_err("Failed to add bitmap to %s: %s\n",
1327 me->path, strerror(errno));
1328 }
1329 }
1330 /* FIXME check for reshape_active and consider not
1331 * starting array.
1332 */
1333 sra = sysfs_read(mdfd, NULL, 0);
1334 if (sra) {
1335 if (sysfs_set_str(sra, NULL,
1336 "array_state", "read-auto") == 0) {
1337 if (verbose >= 0)
1338 pr_err("started array %s\n",
1339 me->path ?: me->devnm);
1340 } else {
1341 pr_err("failed to start array %s: %s\n",
1342 me->path ?: me->devnm,
1343 strerror(errno));
1344 rv = 1;
1345 }
1346 sysfs_free(sra);
1347 }
1348 }
1349 return rv;
1350 }
1351
1352 static char *container2devname(char *devname)
1353 {
1354 char *mdname = NULL;
1355
1356 if (devname[0] == '/') {
1357 int fd = open(devname, O_RDONLY);
1358 if (fd >= 0) {
1359 mdname = xstrdup(fd2devnm(fd));
1360 close(fd);
1361 }
1362 } else {
1363 int uuid[4];
1364 struct map_ent *mp, *map = NULL;
1365
1366 if (!parse_uuid(devname, uuid))
1367 return mdname;
1368 mp = map_by_uuid(&map, uuid);
1369 if (mp)
1370 mdname = xstrdup(mp->devnm);
1371 map_free(map);
1372 }
1373
1374 return mdname;
1375 }
1376
1377 static int Incremental_container(struct supertype *st, char *devname,
1378 struct context *c)
1379 {
1380 /* Collect the contents of this container and for each
1381 * array, choose a device name and assemble the array.
1382 */
1383
1384 struct mdinfo *list;
1385 struct mdinfo *ra;
1386 struct map_ent *map = NULL;
1387 struct mdinfo info;
1388 int trustworthy;
1389 struct mddev_ident *match;
1390 int rv = 0;
1391 struct domainlist *domains;
1392 struct map_ent *smp;
1393 int suuid[4];
1394 int sfd;
1395 int ra_blocked = 0;
1396 int ra_all = 0;
1397
1398 st->ss->getinfo_super(st, &info, NULL);
1399
1400 if ((c->runstop > 0 && info.container_enough >= 0) ||
1401 info.container_enough > 0)
1402 /* pass */;
1403 else {
1404 if (c->verbose)
1405 pr_err("not enough devices to start the container\n");
1406 return 0;
1407 }
1408
1409 match = conf_match(st, &info, devname, c->verbose, &rv);
1410 if (match == NULL && rv == 2)
1411 return rv;
1412
1413 /* Need to compute 'trustworthy' */
1414 if (match)
1415 trustworthy = LOCAL;
1416 else if (st->ss->match_home(st, c->homehost) == 1)
1417 trustworthy = LOCAL;
1418 else if (st->ss->match_home(st, "any") == 1)
1419 trustworthy = LOCAL;
1420 else
1421 trustworthy = FOREIGN;
1422
1423 list = st->ss->container_content(st, NULL);
1424 /* when nothing to activate - quit */
1425 if (list == NULL)
1426 return 0;
1427 for (ra = list ; ra ; ra = ra->next) {
1428 int mdfd;
1429 char chosen_name[1024];
1430 struct map_ent *mp;
1431 struct mddev_ident *match = NULL;
1432
1433 ra_all++;
1434 /* do not activate arrays blocked by metadata handler */
1435 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
1436 pr_err("Cannot activate array %s in %s.\n",
1437 ra->text_version, devname);
1438 ra_blocked++;
1439 continue;
1440 }
1441 mp = map_by_uuid(&map, ra->uuid);
1442
1443 if (mp) {
1444 mdfd = open_dev(mp->devnm);
1445 if (mp->path)
1446 strcpy(chosen_name, mp->path);
1447 else
1448 strcpy(chosen_name, mp->devnm);
1449 } else {
1450
1451 /* Check in mdadm.conf for container == devname and
1452 * member == ra->text_version after second slash.
1453 */
1454 char *sub = strchr(ra->text_version+1, '/');
1455 struct mddev_ident *array_list;
1456 if (sub) {
1457 sub++;
1458 array_list = conf_get_ident(NULL);
1459 } else
1460 array_list = NULL;
1461 for(; array_list ; array_list = array_list->next) {
1462 char *dn;
1463 if (array_list->member == NULL ||
1464 array_list->container == NULL)
1465 continue;
1466 if (strcmp(array_list->member, sub) != 0)
1467 continue;
1468 if (array_list->uuid_set &&
1469 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1470 continue;
1471 dn = container2devname(array_list->container);
1472 if (dn == NULL)
1473 continue;
1474 if (strncmp(dn, ra->text_version+1,
1475 strlen(dn)) != 0 ||
1476 ra->text_version[strlen(dn)+1] != '/') {
1477 free(dn);
1478 continue;
1479 }
1480 free(dn);
1481 /* we have a match */
1482 match = array_list;
1483 if (c->verbose>0)
1484 pr_err("match found for member %s\n",
1485 array_list->member);
1486 break;
1487 }
1488
1489 if (match && match->devname &&
1490 strcasecmp(match->devname, "<ignore>") == 0) {
1491 if (c->verbose > 0)
1492 pr_err("array %s/%s is "
1493 "explicitly ignored by mdadm.conf\n",
1494 match->container, match->member);
1495 return 2;
1496 }
1497 if (match)
1498 trustworthy = LOCAL;
1499
1500 mdfd = create_mddev(match ? match->devname : NULL,
1501 ra->name,
1502 c->autof,
1503 trustworthy,
1504 chosen_name);
1505 }
1506
1507 if (mdfd < 0) {
1508 pr_err("failed to open %s: %s.\n",
1509 chosen_name, strerror(errno));
1510 return 2;
1511 }
1512
1513 assemble_container_content(st, mdfd, ra, c,
1514 chosen_name);
1515 close(mdfd);
1516 }
1517
1518 /* don't move spares to container with volume being activated
1519 when all volumes are blocked */
1520 if (ra_all == ra_blocked)
1521 return 0;
1522
1523 /* Now move all suitable spares from spare container */
1524 domains = domain_from_array(list, st->ss->name);
1525 memcpy(suuid, uuid_zero, sizeof(int[4]));
1526 if (domains &&
1527 (smp = map_by_uuid(&map, suuid)) != NULL &&
1528 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1529 /* spare container found */
1530 struct supertype *sst =
1531 super_imsm.match_metadata_desc("imsm");
1532 struct mdinfo *sinfo;
1533 unsigned long long min_size = 0;
1534 if (st->ss->min_acceptable_spare_size)
1535 min_size = st->ss->min_acceptable_spare_size(st);
1536 if (!sst->ss->load_container(sst, sfd, NULL)) {
1537 close(sfd);
1538 sinfo = container_choose_spares(sst, min_size,
1539 domains, NULL,
1540 st->ss->name, 0);
1541 sst->ss->free_super(sst);
1542 if (sinfo){
1543 int count = 0;
1544 struct mdinfo *disks = sinfo->devs;
1545 while (disks) {
1546 /* move spare from spare
1547 * container to currently
1548 * assembled one
1549 */
1550 if (move_spare(
1551 smp->path,
1552 devname,
1553 makedev(disks->disk.major,
1554 disks->disk.minor)))
1555 count++;
1556 disks = disks->next;
1557 }
1558 if (count)
1559 pr_err("Added %d spare%s to %s\n",
1560 count, count>1?"s":"", devname);
1561 }
1562 sysfs_free(sinfo);
1563 } else
1564 close(sfd);
1565 }
1566 domain_free(domains);
1567 return 0;
1568 }
1569
1570 static void run_udisks(char *arg1, char *arg2)
1571 {
1572 int pid = fork();
1573 int status;
1574 if (pid == 0) {
1575 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1576 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1577 exit(1);
1578 }
1579 while (pid > 0 && wait(&status) != pid)
1580 ;
1581 }
1582
1583 /*
1584 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1585 * raid arrays, and if so first fail (if needed) and then remove the device.
1586 *
1587 * @devname - The device we want to remove
1588 * @id_path - name as found in /dev/disk/by-path for this device
1589 *
1590 * Note: the device name must be a kernel name like "sda", so
1591 * that we can find it in /proc/mdstat
1592 */
1593 int IncrementalRemove(char *devname, char *id_path, int verbose)
1594 {
1595 int mdfd;
1596 int rv = 0;
1597 struct mdstat_ent *ent;
1598 struct mddev_dev devlist;
1599 struct mdinfo mdi;
1600 char buf[32];
1601
1602 if (!id_path)
1603 dprintf(Name ": incremental removal without --path <id_path> "
1604 "lacks the possibility to re-add new device in this "
1605 "port\n");
1606
1607 if (strchr(devname, '/')) {
1608 pr_err("incremental removal requires a "
1609 "kernel device name, not a file: %s\n", devname);
1610 return 1;
1611 }
1612 ent = mdstat_by_component(devname);
1613 if (!ent) {
1614 pr_err("%s does not appear to be a component "
1615 "of any array\n", devname);
1616 return 1;
1617 }
1618 sysfs_init(&mdi, -1, ent->devnm);
1619 if (sysfs_get_str(&mdi, NULL, "array_state",
1620 buf, sizeof(buf)) > 0) {
1621 if (strncmp(buf, "active", 6) == 0 ||
1622 strncmp(buf, "clean", 5) == 0)
1623 sysfs_set_str(&mdi, NULL,
1624 "array_state", "read-auto");
1625 }
1626 mdfd = open_dev(ent->devnm);
1627 if (mdfd < 0) {
1628 pr_err("Cannot open array %s!!\n", ent->dev);
1629 free_mdstat(ent);
1630 return 1;
1631 }
1632
1633 if (id_path) {
1634 struct map_ent *map = NULL, *me;
1635 me = map_by_devnm(&map, ent->devnm);
1636 if (me)
1637 policy_save_path(id_path, me);
1638 map_free(map);
1639 }
1640
1641 memset(&devlist, 0, sizeof(devlist));
1642 devlist.devname = devname;
1643 devlist.disposition = 'f';
1644 /* for a container, we must fail each member array */
1645 if (ent->metadata_version &&
1646 strncmp(ent->metadata_version, "external:", 9) == 0) {
1647 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1648 struct mdstat_ent *memb;
1649 for (memb = mdstat ; memb ; memb = memb->next)
1650 if (is_container_member(memb, ent->dev)) {
1651 int subfd = open_dev(memb->devnm);
1652 if (subfd >= 0) {
1653 rv |= Manage_subdevs(
1654 memb->dev, subfd,
1655 &devlist, verbose, 0,
1656 NULL, 0);
1657 close(subfd);
1658 }
1659 }
1660 free_mdstat(mdstat);
1661 } else
1662 rv |= Manage_subdevs(ent->dev, mdfd, &devlist,
1663 verbose, 0, NULL, 0);
1664 if (rv & 2) {
1665 /* Failed due to EBUSY, try to stop the array.
1666 * Give udisks a chance to unmount it first.
1667 */
1668 int devid = devnm2devid(ent->devnm);
1669 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
1670 rv = Manage_runstop(ent->dev, mdfd, -1,
1671 verbose, 1);
1672 if (rv)
1673 /* At least we can try to trigger a 'remove' */
1674 sysfs_uevent(&mdi, "remove");
1675 if (verbose) {
1676 if (rv)
1677 pr_err("Fail to stop %s too.\n", ent->devnm);
1678 }
1679 } else {
1680 devlist.disposition = 'r';
1681 rv = Manage_subdevs(ent->dev, mdfd, &devlist,
1682 verbose, 0, NULL, 0);
1683 }
1684 close(mdfd);
1685 free_mdstat(ent);
1686 return rv;
1687 }