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