2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
35 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
36 int mdfd
, char **availp
,
38 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
39 int number
, __u64 events
, int verbose
,
41 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
42 struct map_ent
*target
,
43 struct supertype
*st
, int verbose
);
45 static int Incremental_container(struct supertype
*st
, char *devname
,
47 int verbose
, int runstop
, int autof
);
49 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
52 int verbose
, int *rvp
);
54 int Incremental(char *devname
, int verbose
, int runstop
,
55 struct supertype
*st
, char *homehost
, int require_homehost
,
58 /* Add this device to an array, creating the array if necessary
59 * and starting the array if sensible or - if runstop>0 - if possible.
61 * This has several steps:
63 * 1/ Check if device is permitted by mdadm.conf, reject if not.
64 * 2/ Find metadata, reject if none appropriate (check
65 * version/name from args)
66 * 3/ Check if there is a match in mdadm.conf
67 * 3a/ if not, check for homehost match. If no match, assemble as
69 * 4/ Determine device number.
70 * - If in mdadm.conf with std name, use that
71 * - UUID in /var/run/mdadm.map use that
72 * - If name is suggestive, use that. unless in use with different uuid.
73 * - Choose a free, high number.
74 * - Use a partitioned device unless strong suggestion not to.
76 * Don't choose partitioned for containers.
77 * 5/ Find out if array already exists
79 * - choose a name, from mdadm.conf or 'name' field in array.
83 * - check one drive in array to make sure metadata is a reasonably
84 * close match. Reject if not (e.g. different type)
86 * 6/ Make sure /var/run/mdadm.map contains this array.
87 * 7/ Is there enough devices to possibly start the array?
88 * For a container, this means running Incremental_container.
89 * 7a/ if not, finish with success.
91 * - read all metadata and arrange devices like -A does
92 * - if number of OK devices match expected, or -R and there are enough,
93 * start the array (auto-readonly).
96 struct mdinfo info
, dinfo
;
97 struct mdinfo
*sra
= NULL
, *d
;
98 struct mddev_ident
*match
;
99 char chosen_name
[1024];
101 struct map_ent
*mp
, *map
= NULL
;
102 int dfd
= -1, mdfd
= -1;
107 mdu_array_info_t ainf
;
108 struct dev_policy
*policy
= NULL
;
109 struct map_ent target_array
;
112 struct createinfo
*ci
= conf_get_create_info();
114 if (stat(devname
, &stb
) < 0) {
116 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
117 devname
, strerror(errno
));
120 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
122 fprintf(stderr
, Name
": %s is not a block device.\n",
126 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
129 fprintf(stderr
, Name
": cannot open %s: %s.\n",
130 devname
, strerror(errno
));
133 /* If the device is a container, we do something very different */
134 if (must_be_container(dfd
)) {
136 st
= super_by_fd(dfd
, NULL
);
137 if (st
&& st
->ss
->load_container
)
138 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
141 if (!rv
&& st
->ss
->container_content
)
142 return Incremental_container(st
, devname
, homehost
,
143 verbose
, runstop
, autof
);
145 fprintf(stderr
, Name
": %s is not part of an md array.\n",
150 /* 1/ Check if device is permitted by mdadm.conf */
152 if (!conf_test_dev(devname
)) {
155 ": %s not permitted by mdadm.conf.\n",
160 /* 2/ Find metadata, reject if none appropriate (check
161 * version/name from args) */
163 if (fstat(dfd
, &stb
) < 0) {
165 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
166 devname
, strerror(errno
));
169 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
171 fprintf(stderr
, Name
": %s is not a block device.\n",
176 dinfo
.disk
.major
= major(stb
.st_rdev
);
177 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
179 policy
= disk_policy(&dinfo
);
180 have_target
= policy_check_path(&dinfo
, &target_array
);
182 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
185 ": no recognisable superblock on %s.\n",
187 rv
= try_spare(devname
, &dfd
, policy
,
188 have_target
? &target_array
: NULL
,
192 if (st
->ss
->compare_super
== NULL
||
193 st
->ss
->load_super(st
, dfd
, NULL
)) {
195 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
197 rv
= try_spare(devname
, &dfd
, policy
,
198 have_target
? &target_array
: NULL
,
203 close (dfd
); dfd
= -1;
205 st
->ss
->getinfo_super(st
, &info
, NULL
);
207 /* 3/ Check if there is a match in mdadm.conf */
208 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
209 if (!match
&& rv
== 2)
212 if (match
&& match
->devname
213 && strcasecmp(match
->devname
, "<ignore>") == 0) {
215 fprintf(stderr
, Name
": array containing %s is explicitly"
216 " ignored by mdadm.conf\n",
221 /* 3a/ if not, check for homehost match. If no match, continue
222 * but don't trust the 'name' in the array. Thus a 'random' minor
223 * number will be assigned, and the device name will be based
227 else if (st
->ss
->match_home(st
, homehost
) == 1)
229 else if (st
->ss
->match_home(st
, "any") == 1)
230 trustworthy
= LOCAL_ANY
;
232 trustworthy
= FOREIGN
;
235 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
236 (trustworthy
== LOCAL
))) {
239 ": %s has metadata type %s for which "
240 "auto-assembly is disabled\n",
241 devname
, st
->ss
->name
);
244 if (trustworthy
== LOCAL_ANY
)
247 /* There are three possible sources for 'autof': command line,
248 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
249 * ARRAY takes precedence, then command line, then
252 if (match
&& match
->autof
)
253 autof
= match
->autof
;
257 name_to_use
= info
.name
;
258 if (name_to_use
[0] == 0 &&
259 info
.array
.level
== LEVEL_CONTAINER
&&
260 trustworthy
== LOCAL
) {
261 name_to_use
= info
.text_version
;
262 trustworthy
= METADATA
;
264 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
265 ! require_homehost
&&
266 conf_name_is_free(name_to_use
))
269 /* strip "hostname:" prefix from name if we have decided
270 * to treat it as LOCAL
272 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
273 name_to_use
= strchr(name_to_use
, ':')+1;
275 /* 4/ Check if array exists.
278 fprintf(stderr
, Name
": failed to get exclusive lock on "
280 mp
= map_by_uuid(&map
, info
.uuid
);
282 mdfd
= open_dev(mp
->devnum
);
288 /* Couldn't find an existing array, maybe make a new one */
289 mdfd
= create_mddev(match
? match
->devname
: NULL
,
290 name_to_use
, autof
, trustworthy
, chosen_name
);
295 sysfs_init(&info
, mdfd
, 0);
297 if (set_array_info(mdfd
, st
, &info
) != 0) {
298 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
299 chosen_name
, strerror(errno
));
305 dinfo
.disk
.major
= major(stb
.st_rdev
);
306 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
307 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
308 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
309 devname
, chosen_name
, strerror(errno
));
310 ioctl(mdfd
, STOP_ARRAY
, 0);
314 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
315 GET_OFFSET
| GET_SIZE
));
317 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
318 /* It really should be 'none' - must be old buggy
319 * kernel, and mdadm -I may not be able to complete.
322 ioctl(mdfd
, STOP_ARRAY
, NULL
);
324 ": You have an old buggy kernel which cannot support\n"
325 " --incremental reliably. Aborting.\n");
330 info
.array
.working_disks
= 1;
331 /* 6/ Make sure /var/run/mdadm.map contains this array. */
332 map_update(&map
, fd2devnum(mdfd
),
334 info
.uuid
, chosen_name
);
337 /* - check one drive in array to make sure metadata is a reasonably */
338 /* close match. Reject if not (e.g. different type) */
339 /* - add the device */
343 struct supertype
*st2
;
344 struct mdinfo info2
, *d
;
346 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
347 GET_OFFSET
| GET_SIZE
));
350 strcpy(chosen_name
, mp
->path
);
352 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
354 /* It is generally not OK to add non-spare drives to a
355 * running array as they are probably missing because
356 * they failed. However if runstop is 1, then the
357 * array was possibly started early and our best bet is
358 * to add this anyway.
359 * Also if action policy is re-add or better we allow
361 * This doesn't apply to containers as the 'non-spare'
362 * flag has a different meaning. The test has to happen
363 * at the device level there
365 if (!st
->ss
->external
366 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
367 && ! policy_action_allows(policy
, st
->ss
->name
,
370 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
372 ": not adding %s to active array (without --run) %s\n",
373 devname
, chosen_name
);
383 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
384 sra
->devs
->disk
.minor
);
385 dfd2
= dev_open(dn
, O_RDONLY
);
387 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
388 st
->ss
->compare_super(st
, st2
) != 0) {
390 ": metadata mismatch between %s and "
392 devname
, chosen_name
);
398 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
399 st2
->ss
->free_super(st2
);
400 if (info
.array
.level
!= info2
.array
.level
||
401 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
402 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
404 ": unexpected difference between %s and %s.\n",
405 chosen_name
, devname
);
410 info2
.disk
.major
= major(stb
.st_rdev
);
411 info2
.disk
.minor
= minor(stb
.st_rdev
);
412 /* add disk needs to know about containers */
413 if (st
->ss
->external
)
414 sra
->array
.level
= LEVEL_CONTAINER
;
415 err
= add_disk(mdfd
, st
, sra
, &info2
);
416 if (err
< 0 && errno
== EBUSY
) {
417 /* could be another device present with the same
418 * disk.number. Find and reject any such
420 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
421 info
.events
, verbose
, chosen_name
);
422 err
= add_disk(mdfd
, st
, sra
, &info2
);
425 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
426 devname
, chosen_name
, strerror(errno
));
430 info
.array
.working_disks
= 0;
431 for (d
= sra
->devs
; d
; d
=d
->next
)
432 info
.array
.working_disks
++;
436 /* 7/ Is there enough devices to possibly start the array? */
437 /* 7a/ if not, finish with success. */
438 if (info
.array
.level
== LEVEL_CONTAINER
) {
439 int devnum
= devnum
; /* defined and used iff ->external */
440 /* Try to assemble within the container */
442 sysfs_uevent(&info
, "change");
445 ": container %s now has %d devices\n",
446 chosen_name
, info
.array
.working_disks
);
447 wait_for(chosen_name
, mdfd
);
448 if (st
->ss
->external
)
449 devnum
= fd2devnum(mdfd
);
452 rv
= Incremental(chosen_name
, verbose
, runstop
,
453 NULL
, homehost
, require_homehost
, autof
);
455 /* Don't fail the whole -I if a subarray didn't
456 * have enough devices to start yet
459 /* after spare is added, ping monitor for external metadata
460 * so that it can eg. try to rebuild degraded array */
461 if (st
->ss
->external
)
462 ping_monitor_by_id(devnum
);
466 /* We have added something to the array, so need to re-read the
467 * state. Eventually this state should be kept up-to-date as
471 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
472 GET_OFFSET
| GET_SIZE
));
473 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
474 if (enough(info
.array
.level
, info
.array
.raid_disks
,
475 info
.array
.layout
, info
.array
.state
& 1,
476 avail
, active_disks
) == 0) {
479 ": %s attached to %s, not enough to start (%d).\n",
480 devname
, chosen_name
, active_disks
);
487 /* - if number of OK devices match expected, or -R and there */
489 /* + add any bitmap file */
490 /* + start the array (auto-readonly). */
492 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
495 ": %s attached to %s which is already active.\n",
496 devname
, chosen_name
);
503 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
505 /* Let's try to start it */
506 if (match
&& match
->bitmap_file
) {
507 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
510 ": Could not open bitmap file %s.\n",
514 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
517 ": Failed to set bitmapfile for %s.\n",
523 /* Need to remove from the array any devices which
524 * 'count_active' discerned were too old or inappropriate
526 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
527 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
528 remove_disk(mdfd
, st
, sra
, d
);
530 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
531 && trustworthy
!= FOREIGN
)
532 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
534 rv
= sysfs_set_str(sra
, NULL
,
535 "array_state", "read-auto");
539 ": %s attached to %s, which has been started.\n",
540 devname
, chosen_name
);
542 wait_for(chosen_name
, mdfd
);
543 /* We just started the array, so some devices
544 * might have been evicted from the array
545 * because their event counts were too old.
546 * If the action=re-add policy is in-force for
547 * those devices we should re-add them now.
549 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
550 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
551 add_disk(mdfd
, st
, sra
, dsk
) == 0)
553 ": %s re-added to %s\n",
554 dsk
->sys_name
, chosen_name
);
558 ": %s attached to %s, but failed to start: %s.\n",
559 devname
, chosen_name
, strerror(errno
));
565 ": %s attached to %s, not enough to start safely.\n",
566 devname
, chosen_name
);
576 dev_policy_free(policy
);
582 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
585 int verbose
, int *rvp
)
587 struct mddev_ident
*array_list
, *match
;
588 array_list
= conf_get_ident(NULL
);
590 for (; array_list
; array_list
= array_list
->next
) {
591 if (array_list
->uuid_set
&&
592 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
594 if (verbose
>= 2 && array_list
->devname
)
596 ": UUID differs from %s.\n",
597 array_list
->devname
);
600 if (array_list
->name
[0] &&
601 strcasecmp(array_list
->name
, info
->name
) != 0) {
602 if (verbose
>= 2 && array_list
->devname
)
604 ": Name differs from %s.\n",
605 array_list
->devname
);
608 if (array_list
->devices
&&
609 !match_oneof(array_list
->devices
, devname
)) {
610 if (verbose
>= 2 && array_list
->devname
)
612 ": Not a listed device for %s.\n",
613 array_list
->devname
);
616 if (array_list
->super_minor
!= UnSet
&&
617 array_list
->super_minor
!= info
->array
.md_minor
) {
618 if (verbose
>= 2 && array_list
->devname
)
620 ": Different super-minor to %s.\n",
621 array_list
->devname
);
624 if (!array_list
->uuid_set
&&
625 !array_list
->name
[0] &&
626 !array_list
->devices
&&
627 array_list
->super_minor
== UnSet
) {
628 if (verbose
>= 2 && array_list
->devname
)
630 ": %s doesn't have any identifying information.\n",
631 array_list
->devname
);
634 /* FIXME, should I check raid_disks and level too?? */
638 if (match
->devname
&& array_list
->devname
)
640 ": we match both %s and %s - cannot decide which to use.\n",
641 match
->devname
, array_list
->devname
);
644 ": multiple lines in mdadm.conf match\n");
655 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
656 int number
, __u64 events
, int verbose
,
659 /* Find a device attached to this array with a disk.number of number
660 * and events less than the passed events, and remove the device.
665 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
666 return; /* not safe to remove from active arrays
667 * without thinking more */
669 for (d
= sra
->devs
; d
; d
= d
->next
) {
673 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
674 dfd
= dev_open(dn
, O_RDONLY
);
677 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
681 st
->ss
->getinfo_super(st
, &info
, NULL
);
682 st
->ss
->free_super(st
);
685 if (info
.disk
.number
!= number
||
686 info
.events
>= events
)
689 if (d
->disk
.raid_disk
> -1)
690 sysfs_set_str(sra
, d
, "slot", "none");
691 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
694 ": removing old device %s from %s\n",
695 d
->sys_name
+4, array_name
);
699 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
700 int mdfd
, char **availp
,
701 struct mdinfo
*bestinfo
)
703 /* count how many devices in sra think they are active */
706 __u64 max_events
= 0;
718 for (d
= sra
->devs
; d
; d
= d
->next
)
720 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
726 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
727 dfd
= dev_open(dn
, O_RDONLY
);
730 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
734 info
.array
.raid_disks
= raid_disks
;
735 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
737 raid_disks
= info
.array
.raid_disks
;
738 avail
= calloc(raid_disks
, 1);
740 fprintf(stderr
, Name
": out of memory.\n");
745 best
= calloc(raid_disks
, sizeof(int));
746 devmap
= calloc(raid_disks
* numdevs
, 1);
748 st
->ss
->getinfo_super(st
, &info
, devmap
);
751 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
755 max_events
= info
.events
;
756 avail
[info
.disk
.raid_disk
] = 2;
757 best
[info
.disk
.raid_disk
] = devnum
;
758 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
759 } else if (info
.events
== max_events
) {
760 avail
[info
.disk
.raid_disk
] = 2;
761 best
[info
.disk
.raid_disk
] = devnum
;
762 } else if (info
.events
== max_events
-1) {
763 if (avail
[info
.disk
.raid_disk
] == 0) {
764 avail
[info
.disk
.raid_disk
] = 1;
765 best
[info
.disk
.raid_disk
] = devnum
;
767 } else if (info
.events
< max_events
- 1)
769 else if (info
.events
== max_events
+1) {
771 max_events
= info
.events
;
772 for (i
=0; i
< raid_disks
; i
++)
775 avail
[info
.disk
.raid_disk
] = 2;
776 best
[info
.disk
.raid_disk
] = devnum
;
777 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
778 } else { /* info.events much bigger */
779 memset(avail
, 0, raid_disks
);
780 max_events
= info
.events
;
781 avail
[info
.disk
.raid_disk
] = 2;
782 best
[info
.disk
.raid_disk
] = devnum
;
783 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
786 st
->ss
->free_super(st
);
790 /* We need to reject any device that thinks the best device is
791 * failed or missing */
792 for (b
= 0; b
< raid_disks
; b
++)
796 for (i
= 0 ; i
< raid_disks
; i
++) {
797 if (i
!= b
&& avail
[i
])
798 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
799 /* This device thinks 'b' is failed -
802 for (d
=sra
->devs
; devnum
; d
= d
->next
)
804 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
815 /* test if container has degraded member(s) */
816 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
818 mdu_array_info_t array
;
820 int max_degraded
= 0;
822 for(; map
; map
= map
->next
) {
823 if (!is_subarray(map
->metadata
) ||
824 devname2devnum(map
->metadata
+1) != me
->devnum
)
826 afd
= open_dev(map
->devnum
);
829 /* most accurate information regarding array degradation */
830 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
831 int degraded
= array
.raid_disks
- array
.active_disks
-
833 if (degraded
> max_degraded
)
834 max_degraded
= degraded
;
838 return (max_degraded
);
841 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
842 struct map_ent
*target
, int bare
,
843 struct supertype
*st
, int verbose
)
845 /* This device doesn't have any md metadata
846 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
847 * If 'st' is not set, then we only know that some metadata allows this,
848 * others possibly don't.
849 * So look for a container or array to attach the device to.
850 * Prefer 'target' if that is set and the array is found.
852 * If st is set, then only arrays of that type are considered
853 * Return 0 on success, or some exit code on failure, probably 1.
857 struct map_ent
*mp
, *map
= NULL
;
858 struct mdinfo
*chosen
= NULL
;
861 if (fstat(dfd
, &stb
) != 0)
865 * Now we need to find a suitable array to add this to.
866 * We only accept arrays that:
868 * - are in the same domains as the device
869 * - are of an size for which the device will be useful
870 * and we choose the one that is the most degraded
873 if (map_lock(&map
)) {
874 fprintf(stderr
, Name
": failed to get exclusive lock on "
878 for (mp
= map
; mp
; mp
= mp
->next
) {
879 struct supertype
*st2
;
880 struct domainlist
*dl
= NULL
;
882 unsigned long long devsize
;
883 unsigned long long component_size
= 0;
885 if (is_subarray(mp
->metadata
))
888 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
890 (st
->minor_version
>= 0 &&
891 st
->minor_version
!= st2
->minor_version
)) {
893 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
900 sra
= sysfs_read(-1, mp
->devnum
,
901 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
902 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
904 /* Probably a container - no degraded info */
905 sra
= sysfs_read(-1, mp
->devnum
,
906 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
907 GET_COMPONENT
|GET_VERSION
);
909 sra
->array
.failed_disks
= -1;
916 for(i
=0; !st2
&& superlist
[i
]; i
++)
917 st2
= superlist
[i
]->match_metadata_desc(
921 fprintf(stderr
, Name
": not adding %s to %s"
922 " as metadata not recognised.\n",
926 /* Need to double check the 'act_spare' permissions applies
929 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
931 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
932 act_spare_same_slot
))
936 /* update number of failed disks for mostly degraded
937 * container member */
938 if (sra
->array
.failed_disks
== -1)
939 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
941 get_dev_size(dfd
, NULL
, &devsize
);
942 if (sra
->component_size
== 0) {
943 /* true for containers, here we must read superblock
944 * to obtain minimum spare size */
945 struct supertype
*st3
= dup_super(st2
);
946 int mdfd
= open_dev(mp
->devnum
);
949 if (st3
->ss
->load_container
&&
950 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
951 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
952 st3
->ss
->free_super(st3
);
957 if ((sra
->component_size
> 0 &&
958 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
960 (sra
->component_size
== 0 && devsize
< component_size
)) {
962 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
966 /* test against target.
967 * If 'target' is set and 'bare' is false, we only accept
968 * arrays/containers that match 'target'.
969 * If 'target' is set and 'bare' is true, we prefer the
970 * array which matches 'target'.
971 * target is considered only if we deal with degraded array
973 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
974 act_spare_same_slot
)) {
975 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
976 memcmp(target
->uuid
, mp
->uuid
,
977 sizeof(target
->uuid
)) == 0 &&
978 sra
->array
.failed_disks
> 0) {
979 /* This is our target!! */
984 /* skip to end so we don't check any more */
994 dl
= domain_from_array(sra
, st2
->ss
->name
);
995 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
996 /* domain test fails */
998 fprintf(stderr
, Name
": not adding %s to %s as"
999 " it is not in a compatible domain\n",
1004 /* all tests passed, OK to add to this array */
1008 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1022 /* add current device to chosen array as a spare */
1023 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
1025 struct mddev_dev devlist
;
1027 devlist
.next
= NULL
;
1030 devlist
.writemostly
= 0;
1031 devlist
.devname
= devname
;
1032 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
1033 minor(stb
.st_rdev
));
1034 devlist
.disposition
= 'a';
1037 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1043 fprintf(stderr
, Name
": added %s as spare for %s\n",
1044 devname
, chosen
->sys_name
);
1046 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
1047 devname
, chosen
->sys_name
);
1054 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1055 struct supertype
*st
, int verbose
)
1057 /* we know that at least one partition virtual-metadata is
1058 * allowed to incorporate spares like this device. We need to
1059 * find a suitable device to copy partition information from.
1061 * Getting a list of all disk (not partition) devices is
1062 * slightly non-trivial. We could look at /sys/block, but
1063 * that is theoretically due to be removed. Maybe best to use
1064 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1065 * we depend on this directory of 'path' info. But that fails
1066 * to find loop devices and probably others. Maybe don't
1067 * worry about that, they aren't the real target.
1069 * So: check things in /dev/disk/by-path to see if they are in
1070 * a compatible domain, then load the partition table and see
1071 * if it is OK for the new device, and choose the largest
1072 * partition table that fits.
1076 char *chosen
= NULL
;
1077 unsigned long long chosen_size
= 0;
1078 struct supertype
*chosen_st
= NULL
;
1081 dir
= opendir("/dev/disk/by-path");
1084 while ((de
= readdir(dir
)) != NULL
) {
1086 struct dev_policy
*pol2
= NULL
;
1087 struct domainlist
*domlist
= NULL
;
1090 struct supertype
*st2
= NULL
;
1091 char *devname
= NULL
;
1092 unsigned long long devsectors
;
1094 if (de
->d_ino
== 0 ||
1095 de
->d_name
[0] == '.' ||
1096 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1099 ep
= de
->d_name
+ strlen(de
->d_name
);
1100 while (ep
> de
->d_name
&&
1103 if (ep
> de
->d_name
+ 5 &&
1104 strncmp(ep
-5, "-part", 5) == 0)
1105 /* This is a partition - skip it */
1108 pol2
= path_policy(de
->d_name
, type_disk
);
1110 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1111 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1112 /* new device is incompatible with this device. */
1115 domain_free(domlist
);
1118 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1122 fd
= open(devname
, O_RDONLY
);
1125 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1130 st2
= dup_super(st
);
1132 st2
= guess_super_type(fd
, guess_partitions
);
1134 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1138 /* Check domain policy again, this time referring to metadata */
1139 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1140 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1141 /* Incompatible devices for this metadata type */
1143 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1144 /* Some partition types allow sparing, but not
1150 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1151 if (info
.component_size
> devsectors
)
1152 /* This partitioning doesn't fit in the device */
1155 /* This is an acceptable device to copy partition
1156 * metadata from. We could just stop here, but I
1157 * think I want to keep looking incase a larger
1158 * metadata which makes better use of the device can
1161 if (chosen
== NULL
||
1162 chosen_size
< info
.component_size
) {
1163 chosen_size
= info
.component_size
;
1168 chosen_st
->ss
->free_super(chosen_st
);
1177 domain_free(domlist
);
1178 dev_policy_free(pol2
);
1180 st2
->ss
->free_super(st2
);
1190 /* 'chosen' is the best device we can find. Let's write its
1191 * metadata to devname dfd is read-only so don't use that
1193 fd
= open(devname
, O_RDWR
);
1195 chosen_st
->ss
->store_super(chosen_st
, fd
);
1199 chosen_st
->ss
->free_super(chosen_st
);
1204 static int is_bare(int dfd
)
1206 unsigned long long size
= 0;
1207 char bufpad
[4096 + 4096];
1208 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1210 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1211 read(dfd
, buf
, 4096) != 4096)
1214 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1216 if (memcmp(buf
, buf
+1, 4095) != 0)
1219 /* OK, first 4K appear blank, try the end. */
1220 get_dev_size(dfd
, NULL
, &size
);
1221 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1222 read(dfd
, buf
, 4096) != 4096)
1225 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1227 if (memcmp(buf
, buf
+1, 4095) != 0)
1233 /* adding a spare to a regular array is quite different from adding one to
1234 * a set-of-partitions virtual array.
1235 * This function determines which is worth trying and tries as appropriate.
1236 * Arrays are given priority over partitions.
1238 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1239 struct map_ent
*target
,
1240 struct supertype
*st
, int verbose
)
1245 int partitions_ok
= 0;
1249 /* Can only add a spare if device has at least one domain */
1250 if (pol_find(pol
, pol_domain
) == NULL
)
1252 /* And only if some action allows spares */
1253 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1256 /* Now check if the device is bare.
1257 * bare devices can always be added as a spare
1258 * non-bare devices can only be added if spare-same-slot is permitted,
1259 * and this device is replacing a previous device - in which case 'target'
1262 if (!is_bare(dfd
)) {
1263 /* Must have a target and allow same_slot */
1264 /* Later - may allow force_spare without target */
1266 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1267 act_spare_same_slot
)) {
1269 fprintf(stderr
, Name
": %s is not bare, so not "
1270 "considering as a spare\n",
1278 /* It might be OK to add this device to an array - need to see
1279 * what arrays might be candidates.
1282 /* just try try 'array' or 'partition' based on this metadata */
1283 if (st
->ss
->add_to_super
)
1284 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1287 return partition_try_spare(devname
, dfdp
, pol
,
1290 /* No metadata was specified or found so options are open.
1291 * Check for whether any array metadata, or any partition metadata
1292 * might allow adding the spare. This check is just help to avoid
1293 * a more costly scan of all arrays when we can be sure that will
1296 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1297 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1298 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1300 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1301 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1306 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1308 if (rv
!= 0 && partitions_ok
)
1309 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1313 int IncrementalScan(int verbose
)
1315 /* look at every device listed in the 'map' file.
1316 * If one is found that is not running then:
1317 * look in mdadm.conf for bitmap file.
1318 * if one exists, but array has none, add it.
1319 * try to start array in auto-readonly mode
1321 struct map_ent
*mapl
= NULL
;
1323 struct mddev_ident
*devs
, *mddev
;
1327 devs
= conf_get_ident(NULL
);
1329 for (me
= mapl
; me
; me
= me
->next
) {
1330 mdu_array_info_t array
;
1331 mdu_bitmap_file_t bmf
;
1333 int mdfd
= open_dev(me
->devnum
);
1337 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1342 /* Ok, we can try this one. Maybe it needs a bitmap */
1343 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1344 if (mddev
->devname
&& me
->path
1345 && devname_matches(mddev
->devname
, me
->path
))
1347 if (mddev
&& mddev
->bitmap_file
) {
1349 * Note: early kernels will wrongly fail this, so it
1353 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1354 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1356 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1363 fprintf(stderr
, Name
1364 ": Added bitmap %s to %s\n",
1365 mddev
->bitmap_file
, me
->path
);
1366 else if (errno
!= EEXIST
)
1367 fprintf(stderr
, Name
1368 ": Failed to add bitmap to %s: %s\n",
1369 me
->path
, strerror(errno
));
1372 sra
= sysfs_read(mdfd
, 0, 0);
1374 if (sysfs_set_str(sra
, NULL
,
1375 "array_state", "read-auto") == 0) {
1377 fprintf(stderr
, Name
1378 ": started array %s\n",
1379 me
->path
?: devnum2devname(me
->devnum
));
1381 fprintf(stderr
, Name
1382 ": failed to start array %s: %s\n",
1383 me
->path
?: devnum2devname(me
->devnum
),
1392 static char *container2devname(char *devname
)
1394 char *mdname
= NULL
;
1396 if (devname
[0] == '/') {
1397 int fd
= open(devname
, O_RDONLY
);
1399 mdname
= devnum2devname(fd2devnum(fd
));
1404 struct map_ent
*mp
, *map
= NULL
;
1406 if (!parse_uuid(devname
, uuid
))
1408 mp
= map_by_uuid(&map
, uuid
);
1410 mdname
= devnum2devname(mp
->devnum
);
1417 static int Incremental_container(struct supertype
*st
, char *devname
,
1418 char *homehost
, int verbose
,
1419 int runstop
, int autof
)
1421 /* Collect the contents of this container and for each
1422 * array, choose a device name and assemble the array.
1425 struct mdinfo
*list
;
1427 struct map_ent
*map
= NULL
;
1430 struct mddev_ident
*match
;
1432 struct domainlist
*domains
;
1433 struct map_ent
*smp
;
1437 st
->ss
->getinfo_super(st
, &info
, NULL
);
1439 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1440 info
.container_enough
> 0)
1444 fprintf(stderr
, Name
": not enough devices to start the container\n");
1448 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1449 if (match
== NULL
&& rv
== 2)
1452 /* Need to compute 'trustworthy' */
1454 trustworthy
= LOCAL
;
1455 else if (st
->ss
->match_home(st
, homehost
) == 1)
1456 trustworthy
= LOCAL
;
1457 else if (st
->ss
->match_home(st
, "any") == 1)
1458 trustworthy
= LOCAL
;
1460 trustworthy
= FOREIGN
;
1462 list
= st
->ss
->container_content(st
, NULL
);
1464 fprintf(stderr
, Name
": failed to get exclusive lock on "
1466 /* do not assemble arrays that might have bad blocks */
1467 if (list
&& list
->array
.state
& (1<<MD_SB_BBM_ERRORS
)) {
1468 fprintf(stderr
, Name
": BBM log found in metadata. "
1469 "Cannot activate array(s).\n");
1470 /* free container data and exit */
1475 for (ra
= list
; ra
; ra
= ra
->next
) {
1477 char chosen_name
[1024];
1479 struct mddev_ident
*match
= NULL
;
1481 mp
= map_by_uuid(&map
, ra
->uuid
);
1484 mdfd
= open_dev(mp
->devnum
);
1486 strcpy(chosen_name
, mp
->path
);
1488 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1491 /* Check in mdadm.conf for container == devname and
1492 * member == ra->text_version after second slash.
1494 char *sub
= strchr(ra
->text_version
+1, '/');
1495 struct mddev_ident
*array_list
;
1498 array_list
= conf_get_ident(NULL
);
1501 for(; array_list
; array_list
= array_list
->next
) {
1503 if (array_list
->member
== NULL
||
1504 array_list
->container
== NULL
)
1506 if (strcmp(array_list
->member
, sub
) != 0)
1508 if (array_list
->uuid_set
&&
1509 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1511 dn
= container2devname(array_list
->container
);
1514 if (strncmp(dn
, ra
->text_version
+1,
1516 ra
->text_version
[strlen(dn
)+1] != '/') {
1521 /* we have a match */
1524 fprintf(stderr
, Name
": match found for member %s\n",
1525 array_list
->member
);
1529 if (match
&& match
->devname
&&
1530 strcasecmp(match
->devname
, "<ignore>") == 0) {
1532 fprintf(stderr
, Name
": array %s/%s is "
1533 "explicitly ignored by mdadm.conf\n",
1534 match
->container
, match
->member
);
1538 trustworthy
= LOCAL
;
1540 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1548 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1549 chosen_name
, strerror(errno
));
1553 assemble_container_content(st
, mdfd
, ra
, runstop
,
1554 chosen_name
, verbose
, NULL
);
1558 /* Now move all suitable spares from spare container */
1559 domains
= domain_from_array(list
, st
->ss
->name
);
1560 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1562 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1563 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1564 /* spare container found */
1565 struct supertype
*sst
=
1566 super_imsm
.match_metadata_desc("imsm");
1567 struct mdinfo
*sinfo
;
1568 unsigned long long min_size
= 0;
1569 if (st
->ss
->min_acceptable_spare_size
)
1570 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1571 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1573 sinfo
= container_choose_spares(sst
, min_size
,
1576 sst
->ss
->free_super(sst
);
1579 struct mdinfo
*disks
= sinfo
->devs
;
1581 /* move spare from spare
1582 * container to currently
1588 makedev(disks
->disk
.major
,
1589 disks
->disk
.minor
)))
1591 disks
= disks
->next
;
1594 fprintf(stderr
, Name
1595 ": Added %d spare%s to %s\n",
1596 count
, count
>1?"s":"", devname
);
1602 domain_free(domains
);
1608 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1609 * raid arrays, and if so first fail (if needed) and then remove the device.
1611 * @devname - The device we want to remove
1612 * @id_path - name as found in /dev/disk/by-path for this device
1614 * Note: the device name must be a kernel name like "sda", so
1615 * that we can find it in /proc/mdstat
1617 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1621 struct mdstat_ent
*ent
;
1622 struct mddev_dev devlist
;
1625 dprintf(Name
": incremental removal without --path <id_path> "
1626 "lacks the possibility to re-add new device in this "
1629 if (strchr(devname
, '/')) {
1630 fprintf(stderr
, Name
": incremental removal requires a "
1631 "kernel device name, not a file: %s\n", devname
);
1634 ent
= mdstat_by_component(devname
);
1636 fprintf(stderr
, Name
": %s does not appear to be a component "
1637 "of any array\n", devname
);
1640 mdfd
= open_dev(ent
->devnum
);
1642 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1648 struct map_ent
*map
= NULL
, *me
;
1649 me
= map_by_devnum(&map
, ent
->devnum
);
1651 policy_save_path(id_path
, me
);
1655 memset(&devlist
, 0, sizeof(devlist
));
1656 devlist
.devname
= devname
;
1657 devlist
.disposition
= 'f';
1658 /* for a container, we must fail each member array */
1659 if (ent
->metadata_version
&&
1660 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1661 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1662 struct mdstat_ent
*memb
;
1663 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1664 if (is_container_member(memb
, ent
->dev
)) {
1665 int subfd
= open_dev(memb
->devnum
);
1667 Manage_subdevs(memb
->dev
, subfd
,
1668 &devlist
, verbose
, 0,
1673 free_mdstat(mdstat
);
1675 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1676 devlist
.disposition
= 'r';
1677 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);