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