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 memset(&info
, 0, sizeof(info
));
206 st
->ss
->getinfo_super(st
, &info
, NULL
);
208 /* 3/ Check if there is a match in mdadm.conf */
209 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
210 if (!match
&& rv
== 2)
213 if (match
&& match
->devname
214 && strcasecmp(match
->devname
, "<ignore>") == 0) {
216 fprintf(stderr
, Name
": array containing %s is explicitly"
217 " ignored by mdadm.conf\n",
222 /* 3a/ if not, check for homehost match. If no match, continue
223 * but don't trust the 'name' in the array. Thus a 'random' minor
224 * number will be assigned, and the device name will be based
228 else if (st
->ss
->match_home(st
, homehost
) == 1)
230 else if (st
->ss
->match_home(st
, "any") == 1)
231 trustworthy
= LOCAL_ANY
;
233 trustworthy
= FOREIGN
;
236 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
237 (trustworthy
== LOCAL
))) {
240 ": %s has metadata type %s for which "
241 "auto-assembly is disabled\n",
242 devname
, st
->ss
->name
);
245 if (trustworthy
== LOCAL_ANY
)
248 /* There are three possible sources for 'autof': command line,
249 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
250 * ARRAY takes precedence, then command line, then
253 if (match
&& match
->autof
)
254 autof
= match
->autof
;
258 name_to_use
= info
.name
;
259 if (name_to_use
[0] == 0 &&
260 info
.array
.level
== LEVEL_CONTAINER
&&
261 trustworthy
== LOCAL
) {
262 name_to_use
= info
.text_version
;
263 trustworthy
= METADATA
;
265 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
266 ! require_homehost
&&
267 conf_name_is_free(name_to_use
))
270 /* strip "hostname:" prefix from name if we have decided
271 * to treat it as LOCAL
273 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
274 name_to_use
= strchr(name_to_use
, ':')+1;
276 /* 4/ Check if array exists.
279 fprintf(stderr
, Name
": failed to get exclusive lock on "
281 mp
= map_by_uuid(&map
, info
.uuid
);
283 mdfd
= open_dev(mp
->devnum
);
289 /* Couldn't find an existing array, maybe make a new one */
290 mdfd
= create_mddev(match
? match
->devname
: NULL
,
291 name_to_use
, autof
, trustworthy
, chosen_name
);
296 sysfs_init(&info
, mdfd
, 0);
298 if (set_array_info(mdfd
, st
, &info
) != 0) {
299 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
300 chosen_name
, strerror(errno
));
306 dinfo
.disk
.major
= major(stb
.st_rdev
);
307 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
308 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
309 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
310 devname
, chosen_name
, strerror(errno
));
311 ioctl(mdfd
, STOP_ARRAY
, 0);
315 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
316 GET_OFFSET
| GET_SIZE
));
318 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
319 /* It really should be 'none' - must be old buggy
320 * kernel, and mdadm -I may not be able to complete.
323 ioctl(mdfd
, STOP_ARRAY
, NULL
);
325 ": You have an old buggy kernel which cannot support\n"
326 " --incremental reliably. Aborting.\n");
331 info
.array
.working_disks
= 1;
332 /* 6/ Make sure /var/run/mdadm.map contains this array. */
333 map_update(&map
, fd2devnum(mdfd
),
335 info
.uuid
, chosen_name
);
338 /* - check one drive in array to make sure metadata is a reasonably */
339 /* close match. Reject if not (e.g. different type) */
340 /* - add the device */
344 struct supertype
*st2
;
345 struct mdinfo info2
, *d
;
347 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
348 GET_OFFSET
| GET_SIZE
));
351 strcpy(chosen_name
, mp
->path
);
353 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
355 /* It is generally not OK to add non-spare drives to a
356 * running array as they are probably missing because
357 * they failed. However if runstop is 1, then the
358 * array was possibly started early and our best bet is
359 * to add this anyway.
360 * Also if action policy is re-add or better we allow
362 * This doesn't apply to containers as the 'non-spare'
363 * flag has a different meaning. The test has to happen
364 * at the device level there
366 if (!st
->ss
->external
367 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
368 && ! policy_action_allows(policy
, st
->ss
->name
,
371 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
373 ": not adding %s to active array (without --run) %s\n",
374 devname
, chosen_name
);
384 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
385 sra
->devs
->disk
.minor
);
386 dfd2
= dev_open(dn
, O_RDONLY
);
388 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
389 st
->ss
->compare_super(st
, st2
) != 0) {
391 ": metadata mismatch between %s and "
393 devname
, chosen_name
);
399 memset(&info2
, 0, sizeof(info2
));
400 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
401 st2
->ss
->free_super(st2
);
402 if (info
.array
.level
!= info2
.array
.level
||
403 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
404 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
406 ": unexpected difference between %s and %s.\n",
407 chosen_name
, devname
);
412 info2
.disk
.major
= major(stb
.st_rdev
);
413 info2
.disk
.minor
= minor(stb
.st_rdev
);
414 /* add disk needs to know about containers */
415 if (st
->ss
->external
)
416 sra
->array
.level
= LEVEL_CONTAINER
;
417 err
= add_disk(mdfd
, st
, sra
, &info2
);
418 if (err
< 0 && errno
== EBUSY
) {
419 /* could be another device present with the same
420 * disk.number. Find and reject any such
422 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
423 info
.events
, verbose
, chosen_name
);
424 err
= add_disk(mdfd
, st
, sra
, &info2
);
427 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
428 devname
, chosen_name
, strerror(errno
));
432 info
.array
.working_disks
= 0;
433 for (d
= sra
->devs
; d
; d
=d
->next
)
434 info
.array
.working_disks
++;
438 /* 7/ Is there enough devices to possibly start the array? */
439 /* 7a/ if not, finish with success. */
440 if (info
.array
.level
== LEVEL_CONTAINER
) {
442 /* Try to assemble within the container */
444 sysfs_uevent(&info
, "change");
447 ": container %s now has %d devices\n",
448 chosen_name
, info
.array
.working_disks
);
449 wait_for(chosen_name
, mdfd
);
450 if (st
->ss
->external
)
451 devnum
= fd2devnum(mdfd
);
454 rv
= Incremental(chosen_name
, verbose
, runstop
,
455 NULL
, homehost
, require_homehost
, autof
);
457 /* Don't fail the whole -I if a subarray didn't
458 * have enough devices to start yet
461 /* after spare is added, ping monitor for external metadata
462 * so that it can eg. try to rebuild degraded array */
463 if (st
->ss
->external
)
464 ping_monitor_by_id(devnum
);
468 /* We have added something to the array, so need to re-read the
469 * state. Eventually this state should be kept up-to-date as
473 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
474 GET_OFFSET
| GET_SIZE
));
475 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
476 if (enough(info
.array
.level
, info
.array
.raid_disks
,
477 info
.array
.layout
, info
.array
.state
& 1,
478 avail
, active_disks
) == 0) {
481 ": %s attached to %s, not enough to start (%d).\n",
482 devname
, chosen_name
, active_disks
);
489 /* - if number of OK devices match expected, or -R and there */
491 /* + add any bitmap file */
492 /* + start the array (auto-readonly). */
494 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
497 ": %s attached to %s which is already active.\n",
498 devname
, chosen_name
);
505 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
507 /* Let's try to start it */
508 if (match
&& match
->bitmap_file
) {
509 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
512 ": Could not open bitmap file %s.\n",
516 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
519 ": Failed to set bitmapfile for %s.\n",
525 /* Need to remove from the array any devices which
526 * 'count_active' discerned were too old or inappropriate
528 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
529 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
530 remove_disk(mdfd
, st
, sra
, d
);
532 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
533 && trustworthy
!= FOREIGN
)
534 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
536 rv
= sysfs_set_str(sra
, NULL
,
537 "array_state", "read-auto");
541 ": %s attached to %s, which has been started.\n",
542 devname
, chosen_name
);
544 wait_for(chosen_name
, mdfd
);
545 /* We just started the array, so some devices
546 * might have been evicted from the array
547 * because their event counts were too old.
548 * If the action=re-add policy is in-force for
549 * those devices we should re-add them now.
551 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
552 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
553 add_disk(mdfd
, st
, sra
, dsk
) == 0)
555 ": %s re-added to %s\n",
556 dsk
->sys_name
, chosen_name
);
560 ": %s attached to %s, but failed to start: %s.\n",
561 devname
, chosen_name
, strerror(errno
));
567 ": %s attached to %s, not enough to start safely.\n",
568 devname
, chosen_name
);
578 dev_policy_free(policy
);
584 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
587 int verbose
, int *rvp
)
589 struct mddev_ident
*array_list
, *match
;
590 array_list
= conf_get_ident(NULL
);
592 for (; array_list
; array_list
= array_list
->next
) {
593 if (array_list
->uuid_set
&&
594 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
596 if (verbose
>= 2 && array_list
->devname
)
598 ": UUID differs from %s.\n",
599 array_list
->devname
);
602 if (array_list
->name
[0] &&
603 strcasecmp(array_list
->name
, info
->name
) != 0) {
604 if (verbose
>= 2 && array_list
->devname
)
606 ": Name differs from %s.\n",
607 array_list
->devname
);
610 if (array_list
->devices
&&
611 !match_oneof(array_list
->devices
, devname
)) {
612 if (verbose
>= 2 && array_list
->devname
)
614 ": Not a listed device for %s.\n",
615 array_list
->devname
);
618 if (array_list
->super_minor
!= UnSet
&&
619 array_list
->super_minor
!= info
->array
.md_minor
) {
620 if (verbose
>= 2 && array_list
->devname
)
622 ": Different super-minor to %s.\n",
623 array_list
->devname
);
626 if (!array_list
->uuid_set
&&
627 !array_list
->name
[0] &&
628 !array_list
->devices
&&
629 array_list
->super_minor
== UnSet
) {
630 if (verbose
>= 2 && array_list
->devname
)
632 ": %s doesn't have any identifying information.\n",
633 array_list
->devname
);
636 /* FIXME, should I check raid_disks and level too?? */
640 if (match
->devname
&& array_list
->devname
)
642 ": we match both %s and %s - cannot decide which to use.\n",
643 match
->devname
, array_list
->devname
);
646 ": multiple lines in mdadm.conf match\n");
657 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
658 int number
, __u64 events
, int verbose
,
661 /* Find a device attached to this array with a disk.number of number
662 * and events less than the passed events, and remove the device.
667 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
668 return; /* not safe to remove from active arrays
669 * without thinking more */
671 for (d
= sra
->devs
; d
; d
= d
->next
) {
675 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
676 dfd
= dev_open(dn
, O_RDONLY
);
679 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
683 st
->ss
->getinfo_super(st
, &info
, NULL
);
684 st
->ss
->free_super(st
);
687 if (info
.disk
.number
!= number
||
688 info
.events
>= events
)
691 if (d
->disk
.raid_disk
> -1)
692 sysfs_set_str(sra
, d
, "slot", "none");
693 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
696 ": removing old device %s from %s\n",
697 d
->sys_name
+4, array_name
);
701 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
702 int mdfd
, char **availp
,
703 struct mdinfo
*bestinfo
)
705 /* count how many devices in sra think they are active */
708 __u64 max_events
= 0;
720 for (d
= sra
->devs
; d
; d
= d
->next
)
722 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
728 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
729 dfd
= dev_open(dn
, O_RDONLY
);
732 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
736 info
.array
.raid_disks
= raid_disks
;
737 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
739 raid_disks
= info
.array
.raid_disks
;
740 avail
= calloc(raid_disks
, 1);
742 fprintf(stderr
, Name
": out of memory.\n");
747 best
= calloc(raid_disks
, sizeof(int));
748 devmap
= calloc(raid_disks
* numdevs
, 1);
750 st
->ss
->getinfo_super(st
, &info
, devmap
);
753 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
757 max_events
= info
.events
;
758 avail
[info
.disk
.raid_disk
] = 2;
759 best
[info
.disk
.raid_disk
] = devnum
;
760 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
761 } else if (info
.events
== max_events
) {
762 avail
[info
.disk
.raid_disk
] = 2;
763 best
[info
.disk
.raid_disk
] = devnum
;
764 } else if (info
.events
== max_events
-1) {
765 if (avail
[info
.disk
.raid_disk
] == 0) {
766 avail
[info
.disk
.raid_disk
] = 1;
767 best
[info
.disk
.raid_disk
] = devnum
;
769 } else if (info
.events
< max_events
- 1)
771 else if (info
.events
== max_events
+1) {
773 max_events
= info
.events
;
774 for (i
=0; i
< raid_disks
; i
++)
777 avail
[info
.disk
.raid_disk
] = 2;
778 best
[info
.disk
.raid_disk
] = devnum
;
779 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
780 } else { /* info.events much bigger */
781 memset(avail
, 0, info
.disk
.raid_disk
);
782 max_events
= info
.events
;
783 avail
[info
.disk
.raid_disk
] = 2;
784 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
787 st
->ss
->free_super(st
);
791 /* We need to reject any device that thinks the best device is
792 * failed or missing */
793 for (b
= 0; b
< raid_disks
; b
++)
797 for (i
= 0 ; i
< raid_disks
; i
++) {
798 if (i
!= b
&& avail
[i
])
799 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
800 /* This device thinks 'b' is failed -
803 for (d
=sra
->devs
; devnum
; d
= d
->next
)
805 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
816 /* test if container has degraded member(s) */
817 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
819 mdu_array_info_t array
;
821 int max_degraded
= 0;
823 for(; map
; map
= map
->next
) {
824 if (!is_subarray(map
->metadata
) ||
825 devname2devnum(map
->metadata
+1) != me
->devnum
)
827 afd
= open_dev(map
->devnum
);
830 /* most accurate information regarding array degradation */
831 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
832 int degraded
= array
.raid_disks
- array
.active_disks
-
834 if (degraded
> max_degraded
)
835 max_degraded
= degraded
;
839 return (max_degraded
);
842 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
843 struct map_ent
*target
, int bare
,
844 struct supertype
*st
, int verbose
)
846 /* This device doesn't have any md metadata
847 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
848 * If 'st' is not set, then we only know that some metadata allows this,
849 * others possibly don't.
850 * So look for a container or array to attach the device to.
851 * Prefer 'target' if that is set and the array is found.
853 * If st is set, then only arrays of that type are considered
854 * Return 0 on success, or some exit code on failure, probably 1.
858 struct map_ent
*mp
, *map
= NULL
;
859 struct mdinfo
*chosen
= NULL
;
862 if (fstat(dfd
, &stb
) != 0)
866 * Now we need to find a suitable array to add this to.
867 * We only accept arrays that:
869 * - are in the same domains as the device
870 * - are of an size for which the device will be useful
871 * and we choose the one that is the most degraded
874 if (map_lock(&map
)) {
875 fprintf(stderr
, Name
": failed to get exclusive lock on "
879 for (mp
= map
; mp
; mp
= mp
->next
) {
880 struct supertype
*st2
;
881 struct domainlist
*dl
= NULL
;
883 unsigned long long devsize
;
884 unsigned long long component_size
= 0;
886 if (is_subarray(mp
->metadata
))
889 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
891 (st
->minor_version
>= 0 &&
892 st
->minor_version
!= st2
->minor_version
)) {
894 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
901 sra
= sysfs_read(-1, mp
->devnum
,
902 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
903 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
905 /* Probably a container - no degraded info */
906 sra
= sysfs_read(-1, mp
->devnum
,
907 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
908 GET_COMPONENT
|GET_VERSION
);
910 sra
->array
.failed_disks
= -1;
917 for(i
=0; !st2
&& superlist
[i
]; i
++)
918 st2
= superlist
[i
]->match_metadata_desc(
922 fprintf(stderr
, Name
": not adding %s to %s"
923 " as metadata not recognised.\n",
927 /* Need to double check the 'act_spare' permissions applies
930 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
932 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
933 act_spare_same_slot
))
937 /* update number of failed disks for mostly degraded
938 * container member */
939 if (sra
->array
.failed_disks
== -1)
940 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
942 get_dev_size(dfd
, NULL
, &devsize
);
943 if (sra
->component_size
== 0) {
944 /* true for containers, here we must read superblock
945 * to obtain minimum spare size */
946 struct supertype
*st3
= dup_super(st2
);
947 int mdfd
= open_dev(mp
->devnum
);
950 if (st3
->ss
->load_container
&&
951 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
952 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
953 st3
->ss
->free_super(st3
);
958 if ((sra
->component_size
> 0 &&
959 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
961 (sra
->component_size
== 0 && devsize
< component_size
)) {
963 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
967 /* test against target.
968 * If 'target' is set and 'bare' is false, we only accept
969 * arrays/containers that match 'target'.
970 * If 'target' is set and 'bare' is true, we prefer the
971 * array which matches 'target'.
972 * target is considered only if we deal with degraded array
974 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
975 act_spare_same_slot
)) {
976 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
977 memcmp(target
->uuid
, mp
->uuid
,
978 sizeof(target
->uuid
)) == 0 &&
979 sra
->array
.failed_disks
> 0) {
980 /* This is our target!! */
985 /* skip to end so we don't check any more */
995 dl
= domain_from_array(sra
, st2
->ss
->name
);
996 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
997 /* domain test fails */
999 fprintf(stderr
, Name
": not adding %s to %s as"
1000 " it is not in a compatible domain\n",
1005 /* all tests passed, OK to add to this array */
1009 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1023 /* add current device to chosen array as a spare */
1024 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
1026 struct mddev_dev devlist
;
1028 devlist
.next
= NULL
;
1031 devlist
.writemostly
= 0;
1032 devlist
.devname
= devname
;
1033 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
1034 minor(stb
.st_rdev
));
1035 devlist
.disposition
= 'a';
1038 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1044 fprintf(stderr
, Name
": added %s as spare for %s\n",
1045 devname
, chosen
->sys_name
);
1047 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
1048 devname
, chosen
->sys_name
);
1055 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1056 struct supertype
*st
, int verbose
)
1058 /* we know that at least one partition virtual-metadata is
1059 * allowed to incorporate spares like this device. We need to
1060 * find a suitable device to copy partition information from.
1062 * Getting a list of all disk (not partition) devices is
1063 * slightly non-trivial. We could look at /sys/block, but
1064 * that is theoretically due to be removed. Maybe best to use
1065 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1066 * we depend on this directory of 'path' info. But that fails
1067 * to find loop devices and probably others. Maybe don't
1068 * worry about that, they aren't the real target.
1070 * So: check things in /dev/disk/by-path to see if they are in
1071 * a compatible domain, then load the partition table and see
1072 * if it is OK for the new device, and choose the largest
1073 * partition table that fits.
1077 char *chosen
= NULL
;
1078 unsigned long long chosen_size
= 0;
1079 struct supertype
*chosen_st
= NULL
;
1082 dir
= opendir("/dev/disk/by-path");
1085 while ((de
= readdir(dir
)) != NULL
) {
1087 struct dev_policy
*pol2
= NULL
;
1088 struct domainlist
*domlist
= NULL
;
1091 struct supertype
*st2
= NULL
;
1092 char *devname
= NULL
;
1093 unsigned long long devsectors
;
1095 if (de
->d_ino
== 0 ||
1096 de
->d_name
[0] == '.' ||
1097 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1100 ep
= de
->d_name
+ strlen(de
->d_name
);
1101 while (ep
> de
->d_name
&&
1104 if (ep
> de
->d_name
+ 5 &&
1105 strncmp(ep
-5, "-part", 5) == 0)
1106 /* This is a partition - skip it */
1109 pol2
= path_policy(de
->d_name
, type_disk
);
1111 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1112 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1113 /* new device is incompatible with this device. */
1116 domain_free(domlist
);
1119 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1123 fd
= open(devname
, O_RDONLY
);
1126 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1131 st2
= dup_super(st
);
1133 st2
= guess_super_type(fd
, guess_partitions
);
1135 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1139 /* Check domain policy again, this time referring to metadata */
1140 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1141 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1142 /* Incompatible devices for this metadata type */
1144 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1145 /* Some partition types allow sparing, but not
1151 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1152 if (info
.component_size
> devsectors
)
1153 /* This partitioning doesn't fit in the device */
1156 /* This is an acceptable device to copy partition
1157 * metadata from. We could just stop here, but I
1158 * think I want to keep looking incase a larger
1159 * metadata which makes better use of the device can
1162 if (chosen
== NULL
||
1163 chosen_size
< info
.component_size
) {
1164 chosen_size
= info
.component_size
;
1169 chosen_st
->ss
->free_super(chosen_st
);
1178 domain_free(domlist
);
1179 dev_policy_free(pol2
);
1181 st2
->ss
->free_super(st2
);
1191 /* 'chosen' is the best device we can find. Let's write its
1192 * metadata to devname dfd is read-only so don't use that
1194 fd
= open(devname
, O_RDWR
);
1196 chosen_st
->ss
->store_super(chosen_st
, fd
);
1200 chosen_st
->ss
->free_super(chosen_st
);
1205 static int is_bare(int dfd
)
1207 unsigned long long size
= 0;
1208 char bufpad
[4096 + 4096];
1209 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1211 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1212 read(dfd
, buf
, 4096) != 4096)
1215 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1217 if (memcmp(buf
, buf
+1, 4095) != 0)
1220 /* OK, first 4K appear blank, try the end. */
1221 get_dev_size(dfd
, NULL
, &size
);
1222 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1223 read(dfd
, buf
, 4096) != 4096)
1226 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1228 if (memcmp(buf
, buf
+1, 4095) != 0)
1234 /* adding a spare to a regular array is quite different from adding one to
1235 * a set-of-partitions virtual array.
1236 * This function determines which is worth trying and tries as appropriate.
1237 * Arrays are given priority over partitions.
1239 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1240 struct map_ent
*target
,
1241 struct supertype
*st
, int verbose
)
1246 int partitions_ok
= 0;
1250 /* Can only add a spare if device has at least one domain */
1251 if (pol_find(pol
, pol_domain
) == NULL
)
1253 /* And only if some action allows spares */
1254 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1257 /* Now check if the device is bare.
1258 * bare devices can always be added as a spare
1259 * non-bare devices can only be added if spare-same-slot is permitted,
1260 * and this device is replacing a previous device - in which case 'target'
1263 if (!is_bare(dfd
)) {
1264 /* Must have a target and allow same_slot */
1265 /* Later - may allow force_spare without target */
1267 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1268 act_spare_same_slot
)) {
1270 fprintf(stderr
, Name
": %s is not bare, so not "
1271 "considering as a spare\n",
1279 /* It might be OK to add this device to an array - need to see
1280 * what arrays might be candidates.
1283 /* just try try 'array' or 'partition' based on this metadata */
1284 if (st
->ss
->add_to_super
)
1285 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1288 return partition_try_spare(devname
, dfdp
, pol
,
1291 /* No metadata was specified or found so options are open.
1292 * Check for whether any array metadata, or any partition metadata
1293 * might allow adding the spare. This check is just help to avoid
1294 * a more costly scan of all arrays when we can be sure that will
1297 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1298 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1299 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1301 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1302 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1307 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1309 if (rv
!= 0 && partitions_ok
)
1310 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1314 int IncrementalScan(int verbose
)
1316 /* look at every device listed in the 'map' file.
1317 * If one is found that is not running then:
1318 * look in mdadm.conf for bitmap file.
1319 * if one exists, but array has none, add it.
1320 * try to start array in auto-readonly mode
1322 struct map_ent
*mapl
= NULL
;
1324 struct mddev_ident
*devs
, *mddev
;
1328 devs
= conf_get_ident(NULL
);
1330 for (me
= mapl
; me
; me
= me
->next
) {
1331 mdu_array_info_t array
;
1332 mdu_bitmap_file_t bmf
;
1334 int mdfd
= open_dev(me
->devnum
);
1338 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1343 /* Ok, we can try this one. Maybe it needs a bitmap */
1344 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1345 if (mddev
->devname
&& me
->path
1346 && devname_matches(mddev
->devname
, me
->path
))
1348 if (mddev
&& mddev
->bitmap_file
) {
1350 * Note: early kernels will wrongly fail this, so it
1354 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1355 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1357 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1364 fprintf(stderr
, Name
1365 ": Added bitmap %s to %s\n",
1366 mddev
->bitmap_file
, me
->path
);
1367 else if (errno
!= EEXIST
)
1368 fprintf(stderr
, Name
1369 ": Failed to add bitmap to %s: %s\n",
1370 me
->path
, strerror(errno
));
1373 sra
= sysfs_read(mdfd
, 0, 0);
1375 if (sysfs_set_str(sra
, NULL
,
1376 "array_state", "read-auto") == 0) {
1378 fprintf(stderr
, Name
1379 ": started array %s\n",
1380 me
->path
?: devnum2devname(me
->devnum
));
1382 fprintf(stderr
, Name
1383 ": failed to start array %s: %s\n",
1384 me
->path
?: devnum2devname(me
->devnum
),
1393 static char *container2devname(char *devname
)
1395 char *mdname
= NULL
;
1397 if (devname
[0] == '/') {
1398 int fd
= open(devname
, O_RDONLY
);
1400 mdname
= devnum2devname(fd2devnum(fd
));
1405 struct map_ent
*mp
, *map
= NULL
;
1407 if (!parse_uuid(devname
, uuid
))
1409 mp
= map_by_uuid(&map
, uuid
);
1411 mdname
= devnum2devname(mp
->devnum
);
1418 static int Incremental_container(struct supertype
*st
, char *devname
,
1419 char *homehost
, int verbose
,
1420 int runstop
, int autof
)
1422 /* Collect the contents of this container and for each
1423 * array, choose a device name and assemble the array.
1426 struct mdinfo
*list
;
1428 struct map_ent
*map
= NULL
;
1431 struct mddev_ident
*match
;
1433 struct domainlist
*domains
;
1434 struct map_ent
*smp
;
1438 memset(&info
, 0, sizeof(info
));
1439 st
->ss
->getinfo_super(st
, &info
, NULL
);
1441 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1442 info
.container_enough
> 0)
1446 fprintf(stderr
, Name
": not enough devices to start the container\n");
1450 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1451 if (match
== NULL
&& rv
== 2)
1454 /* Need to compute 'trustworthy' */
1456 trustworthy
= LOCAL
;
1457 else if (st
->ss
->match_home(st
, homehost
) == 1)
1458 trustworthy
= LOCAL
;
1459 else if (st
->ss
->match_home(st
, "any") == 1)
1460 trustworthy
= LOCAL
;
1462 trustworthy
= FOREIGN
;
1464 list
= st
->ss
->container_content(st
, NULL
);
1466 fprintf(stderr
, Name
": failed to get exclusive lock on "
1468 /* do not assemble arrays that might have bad blocks */
1469 if (list
->array
.state
& (1<<MD_SB_BBM_ERRORS
)) {
1470 fprintf(stderr
, Name
": BBM log found in metadata. "
1471 "Cannot activate array(s).\n");
1472 /* free container data and exit */
1477 for (ra
= list
; ra
; ra
= ra
->next
) {
1479 char chosen_name
[1024];
1481 struct mddev_ident
*match
= NULL
;
1483 mp
= map_by_uuid(&map
, ra
->uuid
);
1486 mdfd
= open_dev(mp
->devnum
);
1488 strcpy(chosen_name
, mp
->path
);
1490 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1493 /* Check in mdadm.conf for container == devname and
1494 * member == ra->text_version after second slash.
1496 char *sub
= strchr(ra
->text_version
+1, '/');
1497 struct mddev_ident
*array_list
;
1500 array_list
= conf_get_ident(NULL
);
1503 for(; array_list
; array_list
= array_list
->next
) {
1505 if (array_list
->member
== NULL
||
1506 array_list
->container
== NULL
)
1508 if (strcmp(array_list
->member
, sub
) != 0)
1510 if (array_list
->uuid_set
&&
1511 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1513 dn
= container2devname(array_list
->container
);
1516 if (strncmp(dn
, ra
->text_version
+1,
1518 ra
->text_version
[strlen(dn
)+1] != '/') {
1523 /* we have a match */
1526 fprintf(stderr
, Name
": match found for member %s\n",
1527 array_list
->member
);
1531 if (match
&& match
->devname
&&
1532 strcasecmp(match
->devname
, "<ignore>") == 0) {
1534 fprintf(stderr
, Name
": array %s/%s is "
1535 "explicitly ignored by mdadm.conf\n",
1536 match
->container
, match
->member
);
1540 trustworthy
= LOCAL
;
1542 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1550 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1551 chosen_name
, strerror(errno
));
1555 assemble_container_content(st
, mdfd
, ra
, runstop
,
1556 chosen_name
, verbose
, NULL
);
1560 /* Now move all suitable spares from spare container */
1561 domains
= domain_from_array(list
, st
->ss
->name
);
1562 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1564 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1565 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1566 /* spare container found */
1567 struct supertype
*sst
=
1568 super_imsm
.match_metadata_desc("imsm");
1569 struct mdinfo
*sinfo
;
1570 unsigned long long min_size
= 0;
1571 if (st
->ss
->min_acceptable_spare_size
)
1572 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1573 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1575 sinfo
= container_choose_spares(sst
, min_size
,
1578 sst
->ss
->free_super(sst
);
1581 struct mdinfo
*disks
= sinfo
->devs
;
1583 /* move spare from spare
1584 * container to currently
1590 makedev(disks
->disk
.major
,
1591 disks
->disk
.minor
)))
1593 disks
= disks
->next
;
1596 fprintf(stderr
, Name
1597 ": Added %d spare%s to %s\n",
1598 count
, count
>1?"s":"", devname
);
1604 domain_free(domains
);
1610 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1611 * raid arrays, and if so first fail (if needed) and then remove the device.
1613 * @devname - The device we want to remove
1614 * @id_path - name as found in /dev/disk/by-path for this device
1616 * Note: the device name must be a kernel name like "sda", so
1617 * that we can find it in /proc/mdstat
1619 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1623 struct mdstat_ent
*ent
;
1624 struct mddev_dev devlist
;
1627 dprintf(Name
": incremental removal without --path <id_path> "
1628 "lacks the possibility to re-add new device in this "
1631 if (strchr(devname
, '/')) {
1632 fprintf(stderr
, Name
": incremental removal requires a "
1633 "kernel device name, not a file: %s\n", devname
);
1636 ent
= mdstat_by_component(devname
);
1638 fprintf(stderr
, Name
": %s does not appear to be a component "
1639 "of any array\n", devname
);
1642 mdfd
= open_dev(ent
->devnum
);
1644 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1650 struct map_ent
*map
= NULL
, *me
;
1651 me
= map_by_devnum(&map
, ent
->devnum
);
1653 policy_save_path(id_path
, me
);
1657 memset(&devlist
, 0, sizeof(devlist
));
1658 devlist
.devname
= devname
;
1659 devlist
.disposition
= 'f';
1660 /* for a container, we must fail each member array */
1661 if (ent
->metadata_version
&&
1662 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1663 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1664 struct mdstat_ent
*memb
;
1665 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1666 if (is_container_member(memb
, ent
->dev
)) {
1667 int subfd
= open_dev(memb
->devnum
);
1669 Manage_subdevs(memb
->dev
, subfd
,
1670 &devlist
, verbose
, 0,
1675 free_mdstat(mdstat
);
1677 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
);
1678 devlist
.disposition
= 'r';
1679 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
);