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