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