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