]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
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
, int mdfd
, char **availp
,
37 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
38 int number
, __u64 events
, int verbose
,
40 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
41 struct map_ent
*target
,
42 struct supertype
*st
, int verbose
);
44 static int Incremental_container(struct supertype
*st
, char *devname
,
46 int verbose
, int runstop
, int autof
);
48 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
51 int verbose
, int *rvp
);
53 int Incremental(char *devname
, int verbose
, int runstop
,
54 struct supertype
*st
, char *homehost
, int require_homehost
,
57 /* Add this device to an array, creating the array if necessary
58 * and starting the array if sensible or - if runstop>0 - if possible.
60 * This has several steps:
62 * 1/ Check if device is permitted by mdadm.conf, reject if not.
63 * 2/ Find metadata, reject if none appropriate (check
64 * version/name from args)
65 * 3/ Check if there is a match in mdadm.conf
66 * 3a/ if not, check for homehost match. If no match, assemble as
68 * 4/ Determine device number.
69 * - If in mdadm.conf with std name, use that
70 * - UUID in /var/run/mdadm.map use that
71 * - If name is suggestive, use that. unless in use with different uuid.
72 * - Choose a free, high number.
73 * - Use a partitioned device unless strong suggestion not to.
75 * Don't choose partitioned for containers.
76 * 5/ Find out if array already exists
78 * - choose a name, from mdadm.conf or 'name' field in array.
82 * - check one drive in array to make sure metadata is a reasonably
83 * close match. Reject if not (e.g. different type)
85 * 6/ Make sure /var/run/mdadm.map contains this array.
86 * 7/ Is there enough devices to possibly start the array?
87 * For a container, this means running Incremental_container.
88 * 7a/ if not, finish with success.
90 * - read all metadata and arrange devices like -A does
91 * - if number of OK devices match expected, or -R and there are enough,
92 * start the array (auto-readonly).
95 struct mdinfo info
, dinfo
;
96 struct mddev_ident
*match
;
97 char chosen_name
[1024];
99 struct map_ent
*mp
, *map
= NULL
;
100 int dfd
= -1, mdfd
= -1;
105 mdu_array_info_t ainf
;
106 struct dev_policy
*policy
= NULL
;
107 struct map_ent target_array
;
110 struct createinfo
*ci
= conf_get_create_info();
112 if (stat(devname
, &stb
) < 0) {
114 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
115 devname
, strerror(errno
));
118 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
120 fprintf(stderr
, Name
": %s is not a block device.\n",
124 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
127 fprintf(stderr
, Name
": cannot open %s: %s.\n",
128 devname
, strerror(errno
));
131 /* If the device is a container, we do something very different */
132 if (must_be_container(dfd
)) {
134 st
= super_by_fd(dfd
, NULL
);
136 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
139 if (!rv
&& st
->ss
->container_content
)
140 return Incremental_container(st
, devname
, homehost
,
141 verbose
, runstop
, autof
);
143 fprintf(stderr
, Name
": %s is not part of an md array.\n",
148 /* 1/ Check if device is permitted by mdadm.conf */
150 if (!conf_test_dev(devname
)) {
153 ": %s not permitted by mdadm.conf.\n",
158 /* 2/ Find metadata, reject if none appropriate (check
159 * version/name from args) */
161 if (fstat(dfd
, &stb
) < 0) {
163 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
164 devname
, strerror(errno
));
167 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
169 fprintf(stderr
, Name
": %s is not a block device.\n",
174 dinfo
.disk
.major
= major(stb
.st_rdev
);
175 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
177 policy
= disk_policy(&dinfo
);
178 have_target
= policy_check_path(&dinfo
, &target_array
);
180 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
183 ": no recognisable superblock on %s.\n",
185 rv
= try_spare(devname
, &dfd
, policy
,
186 have_target
? &target_array
: NULL
,
190 if (st
->ss
->compare_super
== NULL
||
191 st
->ss
->load_super(st
, dfd
, NULL
)) {
193 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
195 rv
= try_spare(devname
, &dfd
, policy
,
196 have_target
? &target_array
: NULL
,
201 close (dfd
); dfd
= -1;
203 memset(&info
, 0, sizeof(info
));
204 st
->ss
->getinfo_super(st
, &info
, NULL
);
206 /* 3/ Check if there is a match in mdadm.conf */
207 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
208 if (!match
&& rv
== 2)
211 if (match
&& match
->devname
212 && strcasecmp(match
->devname
, "<ignore>") == 0) {
214 fprintf(stderr
, Name
": array containing %s is explicitly"
215 " ignored by mdadm.conf\n",
220 /* 3a/ if not, check for homehost match. If no match, continue
221 * but don't trust the 'name' in the array. Thus a 'random' minor
222 * number will be assigned, and the device name will be based
226 else if (st
->ss
->match_home(st
, homehost
) == 1)
228 else if (st
->ss
->match_home(st
, "any") == 1)
229 trustworthy
= LOCAL_ANY
;
231 trustworthy
= FOREIGN
;
234 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
235 (trustworthy
== LOCAL
))) {
238 ": %s has metadata type %s for which "
239 "auto-assembly is disabled\n",
240 devname
, st
->ss
->name
);
243 if (trustworthy
== LOCAL_ANY
)
246 /* There are three possible sources for 'autof': command line,
247 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
248 * ARRAY takes precedence, then command line, then
251 if (match
&& match
->autof
)
252 autof
= match
->autof
;
256 name_to_use
= info
.name
;
257 if (name_to_use
[0] == 0 &&
258 info
.array
.level
== LEVEL_CONTAINER
&&
259 trustworthy
== LOCAL
) {
260 name_to_use
= info
.text_version
;
261 trustworthy
= METADATA
;
263 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
264 ! require_homehost
&&
265 conf_name_is_free(name_to_use
))
268 /* strip "hostname:" prefix from name if we have decided
269 * to treat it as LOCAL
271 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
272 name_to_use
= strchr(name_to_use
, ':')+1;
274 /* 4/ Check if array exists.
277 fprintf(stderr
, Name
": failed to get exclusive lock on "
279 mp
= map_by_uuid(&map
, info
.uuid
);
281 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
, fd2devnum(mdfd
), GET_DEVS
);
315 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
316 /* It really should be 'none' - must be old buggy
317 * kernel, and mdadm -I may not be able to complete.
320 ioctl(mdfd
, STOP_ARRAY
, NULL
);
322 ": You have an old buggy kernel which cannot support\n"
323 " --incremental reliably. Aborting.\n");
328 info
.array
.working_disks
= 1;
330 /* 6/ Make sure /var/run/mdadm.map contains this array. */
331 map_update(&map
, fd2devnum(mdfd
),
333 info
.uuid
, chosen_name
);
336 /* - check one drive in array to make sure metadata is a reasonably */
337 /* close match. Reject if not (e.g. different type) */
338 /* - add the device */
343 struct supertype
*st2
;
344 struct mdinfo info2
, *d
;
347 strcpy(chosen_name
, mp
->path
);
349 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
351 /* It is generally not OK to add non-spare drives to a
352 * running array as they are probably missing because
353 * they failed. However if runstop is 1, then the
354 * array was possibly started early and our best bet is
355 * to add this anyway.
356 * Also if action policy is re-add or better we allow
359 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
360 && ! policy_action_allows(policy
, st
->ss
->name
,
365 if (st
->ss
->external
) {
366 char *devname
= devnum2devname(fd2devnum(mdfd
));
368 active
= devname
&& is_container_active(devname
);
370 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
374 ": not adding %s to active array (without --run) %s\n",
375 devname
, chosen_name
);
380 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
386 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
387 sra
->devs
->disk
.minor
);
388 dfd2
= dev_open(dn
, O_RDONLY
);
390 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
391 st
->ss
->compare_super(st
, st2
) != 0) {
393 ": metadata mismatch between %s and "
395 devname
, chosen_name
);
401 memset(&info2
, 0, sizeof(info2
));
402 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
403 st2
->ss
->free_super(st2
);
404 if (info
.array
.level
!= info2
.array
.level
||
405 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
406 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
408 ": unexpected difference between %s and %s.\n",
409 chosen_name
, devname
);
414 info2
.disk
.major
= major(stb
.st_rdev
);
415 info2
.disk
.minor
= minor(stb
.st_rdev
);
416 /* add disk needs to know about containers */
417 if (st
->ss
->external
)
418 sra
->array
.level
= LEVEL_CONTAINER
;
419 err
= add_disk(mdfd
, st
, sra
, &info2
);
420 if (err
< 0 && errno
== EBUSY
) {
421 /* could be another device present with the same
422 * disk.number. Find and reject any such
424 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
425 info
.events
, verbose
, chosen_name
);
426 err
= add_disk(mdfd
, st
, sra
, &info2
);
429 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
430 devname
, chosen_name
, strerror(errno
));
434 info
.array
.working_disks
= 0;
435 for (d
= sra
->devs
; d
; d
=d
->next
)
436 info
.array
.working_disks
++;
440 /* 7/ Is there enough devices to possibly start the array? */
441 /* 7a/ if not, finish with success. */
442 if (info
.array
.level
== LEVEL_CONTAINER
) {
443 /* Try to assemble within the container */
445 sysfs_uevent(&info
, "change");
448 ": container %s now has %d devices\n",
449 chosen_name
, info
.array
.working_disks
);
450 wait_for(chosen_name
, 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
462 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
463 if (enough(info
.array
.level
, info
.array
.raid_disks
,
464 info
.array
.layout
, info
.array
.state
& 1,
465 avail
, active_disks
) == 0) {
469 ": %s attached to %s, not enough to start (%d).\n",
470 devname
, chosen_name
, active_disks
);
478 /* - if number of OK devices match expected, or -R and there */
480 /* + add any bitmap file */
481 /* + start the array (auto-readonly). */
483 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
486 ": %s attached to %s which is already active.\n",
487 devname
, chosen_name
);
494 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
495 struct mdinfo
*sra
, *dsk
;
496 /* Let's try to start it */
497 if (match
&& match
->bitmap_file
) {
498 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
501 ": Could not open bitmap file %s.\n",
505 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
508 ": Failed to set bitmapfile for %s.\n",
514 /* GET_* needed so add_disk works below */
515 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
),
516 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
517 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
518 && trustworthy
!= FOREIGN
)
519 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
521 rv
= sysfs_set_str(sra
, NULL
,
522 "array_state", "read-auto");
526 ": %s attached to %s, which has been started.\n",
527 devname
, chosen_name
);
529 wait_for(chosen_name
, mdfd
);
530 /* We just started the array, so some devices
531 * might have been evicted from the array
532 * because their event counts were too old.
533 * If the action=re-add policy is in-force for
534 * those devices we should re-add them now.
536 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
537 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
538 add_disk(mdfd
, st
, sra
, dsk
) == 0)
540 ": %s re-added to %s\n",
541 dsk
->sys_name
, chosen_name
);
545 ": %s attached to %s, but failed to start: %s.\n",
546 devname
, chosen_name
, strerror(errno
));
552 ": %s attached to %s, not enough to start safely.\n",
553 devname
, chosen_name
);
562 dev_policy_free(policy
);
566 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
569 int verbose
, int *rvp
)
571 struct mddev_ident
*array_list
, *match
;
572 array_list
= conf_get_ident(NULL
);
574 for (; array_list
; array_list
= array_list
->next
) {
575 if (array_list
->uuid_set
&&
576 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
578 if (verbose
>= 2 && array_list
->devname
)
580 ": UUID differs from %s.\n",
581 array_list
->devname
);
584 if (array_list
->name
[0] &&
585 strcasecmp(array_list
->name
, info
->name
) != 0) {
586 if (verbose
>= 2 && array_list
->devname
)
588 ": Name differs from %s.\n",
589 array_list
->devname
);
592 if (array_list
->devices
&&
593 !match_oneof(array_list
->devices
, devname
)) {
594 if (verbose
>= 2 && array_list
->devname
)
596 ": Not a listed device for %s.\n",
597 array_list
->devname
);
600 if (array_list
->super_minor
!= UnSet
&&
601 array_list
->super_minor
!= info
->array
.md_minor
) {
602 if (verbose
>= 2 && array_list
->devname
)
604 ": Different super-minor to %s.\n",
605 array_list
->devname
);
608 if (!array_list
->uuid_set
&&
609 !array_list
->name
[0] &&
610 !array_list
->devices
&&
611 array_list
->super_minor
== UnSet
) {
612 if (verbose
>= 2 && array_list
->devname
)
614 ": %s doesn't have any identifying information.\n",
615 array_list
->devname
);
618 /* FIXME, should I check raid_disks and level too?? */
622 if (match
->devname
&& array_list
->devname
)
624 ": we match both %s and %s - cannot decide which to use.\n",
625 match
->devname
, array_list
->devname
);
628 ": multiple lines in mdadm.conf match\n");
639 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
640 int number
, __u64 events
, int verbose
,
643 /* Find a device attached to this array with a disk.number of number
644 * and events less than the passed events, and remove the device.
649 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
650 return; /* not safe to remove from active arrays
651 * without thinking more */
653 for (d
= sra
->devs
; d
; d
= d
->next
) {
657 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
658 dfd
= dev_open(dn
, O_RDONLY
);
661 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
665 st
->ss
->getinfo_super(st
, &info
, NULL
);
666 st
->ss
->free_super(st
);
669 if (info
.disk
.number
!= number
||
670 info
.events
>= events
)
673 if (d
->disk
.raid_disk
> -1)
674 sysfs_set_str(sra
, d
, "slot", "none");
675 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
678 ": removing old device %s from %s\n",
679 d
->sys_name
+4, array_name
);
683 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
684 struct mdinfo
*bestinfo
)
686 /* count how many devices in sra think they are active */
688 int cnt
= 0, cnt1
= 0;
689 __u64 max_events
= 0;
690 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
696 for (d
= sra
->devs
; d
; d
= d
->next
) {
702 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
703 dfd
= dev_open(dn
, O_RDONLY
);
706 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
710 st
->ss
->getinfo_super(st
, &info
, NULL
);
712 avail
= malloc(info
.array
.raid_disks
);
714 fprintf(stderr
, Name
": out of memory.\n");
717 memset(avail
, 0, info
.array
.raid_disks
);
721 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
725 max_events
= info
.events
;
726 avail
[info
.disk
.raid_disk
] = 2;
727 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
728 } else if (info
.events
== max_events
) {
730 avail
[info
.disk
.raid_disk
] = 2;
731 } else if (info
.events
== max_events
-1) {
733 avail
[info
.disk
.raid_disk
] = 1;
734 } else if (info
.events
< max_events
- 1)
736 else if (info
.events
== max_events
+1) {
740 max_events
= info
.events
;
741 for (i
=0; i
<info
.array
.raid_disks
; i
++)
744 avail
[info
.disk
.raid_disk
] = 2;
745 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
746 } else { /* info.events much bigger */
748 memset(avail
, 0, info
.disk
.raid_disk
);
749 max_events
= info
.events
;
750 avail
[info
.disk
.raid_disk
] = 2;
751 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
754 st
->ss
->free_super(st
);
759 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
760 struct map_ent
*target
, int bare
,
761 struct supertype
*st
, int verbose
)
763 /* This device doesn't have any md metadata
764 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
765 * If 'st' is not set, then we only know that some metadata allows this,
766 * others possibly don't.
767 * So look for a container or array to attach the device to.
768 * Prefer 'target' if that is set and the array is found.
770 * If st is set, then only arrays of that type are considered
771 * Return 0 on success, or some exit code on failure, probably 1.
775 struct map_ent
*mp
, *map
= NULL
;
776 struct mdinfo
*chosen
= NULL
;
779 if (fstat(dfd
, &stb
) != 0)
783 * Now we need to find a suitable array to add this to.
784 * We only accept arrays that:
786 * - are in the same domains as the device
787 * - are of an size for which the device will be useful
788 * and we choose the one that is the most degraded
791 if (map_lock(&map
)) {
792 fprintf(stderr
, Name
": failed to get exclusive lock on "
796 for (mp
= map
; mp
; mp
= mp
->next
) {
797 struct supertype
*st2
;
798 struct domainlist
*dl
= NULL
;
800 unsigned long long devsize
;
802 if (is_subarray(mp
->metadata
))
805 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
807 (st
->minor_version
>= 0 &&
808 st
->minor_version
!= st2
->minor_version
)) {
810 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
817 sra
= sysfs_read(-1, mp
->devnum
,
818 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
819 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
821 /* Probably a container - no degraded info */
822 sra
= sysfs_read(-1, mp
->devnum
,
823 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
824 GET_COMPONENT
|GET_VERSION
);
826 sra
->array
.failed_disks
= 0;
833 for(i
=0; !st2
&& superlist
[i
]; i
++)
834 st2
= superlist
[i
]->match_metadata_desc(
838 fprintf(stderr
, Name
": not adding %s to %s"
839 " as metadata not recognised.\n",
843 /* Need to double check the 'act_spare' permissions applies
846 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
848 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
849 act_spare_same_slot
))
853 get_dev_size(dfd
, NULL
, &devsize
);
854 if (st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
) {
856 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
860 dl
= domain_from_array(sra
, st2
->ss
->name
);
861 if (!domain_test(dl
, pol
, st2
->ss
->name
)) {
862 /* domain test fails */
864 fprintf(stderr
, Name
": not adding %s to %s as it is not in a compatible domain\n",
869 /* test against target.
870 * If 'target' is set and 'bare' is false, we only accept
871 * arrays/containers that match 'target'.
872 * If 'target' is set and 'bare' is true, we prefer the
873 * array which matches 'target'.
876 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
877 memcmp(target
->uuid
, mp
->uuid
,
878 sizeof(target
->uuid
)) == 0) {
879 /* This is our target!! */
884 /* skip to end so we don't check any more */
894 /* all tests passed, OK to add to this array */
898 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
912 /* add current device to chosen array as a spare */
913 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
915 struct mddev_dev devlist
;
920 devlist
.writemostly
= 0;
921 devlist
.devname
= devname
;
922 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
924 devlist
.disposition
= 'a';
927 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
933 fprintf(stderr
, Name
": added %s as spare for %s\n",
934 devname
, chosen
->sys_name
);
936 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
937 devname
, chosen
->sys_name
);
944 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
945 struct supertype
*st
, int verbose
)
947 /* we know that at least one partition virtual-metadata is
948 * allowed to incorporate spares like this device. We need to
949 * find a suitable device to copy partition information from.
951 * Getting a list of all disk (not partition) devices is
952 * slightly non-trivial. We could look at /sys/block, but
953 * that is theoretically due to be removed. Maybe best to use
954 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
955 * we depend on this directory of 'path' info. But that fails
956 * to find loop devices and probably others. Maybe don't
957 * worry about that, they aren't the real target.
959 * So: check things in /dev/disk/by-path to see if they are in
960 * a compatible domain, then load the partition table and see
961 * if it is OK for the new device, and choose the largest
962 * partition table that fits.
967 unsigned long long chosen_size
;
968 struct supertype
*chosen_st
= NULL
;
971 dir
= opendir("/dev/disk/by-path");
974 while ((de
= readdir(dir
)) != NULL
) {
976 struct dev_policy
*pol2
= NULL
;
977 struct domainlist
*domlist
= NULL
;
980 struct supertype
*st2
= NULL
;
981 char *devname
= NULL
;
982 unsigned long long devsectors
;
984 if (de
->d_ino
== 0 ||
985 de
->d_name
[0] == '.' ||
986 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
989 ep
= de
->d_name
+ strlen(de
->d_name
);
990 while (ep
> de
->d_name
&&
993 if (ep
> de
->d_name
+ 5 &&
994 strncmp(ep
-5, "-part", 5) == 0)
995 /* This is a partition - skip it */
998 pol2
= path_policy(de
->d_name
, type_disk
);
1000 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1001 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) == 0)
1002 /* new device is incompatible with this device. */
1005 domain_free(domlist
);
1008 asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
);
1009 fd
= open(devname
, O_RDONLY
);
1012 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1017 st2
= dup_super(st
);
1019 st2
= guess_super_type(fd
, guess_partitions
);
1021 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1025 /* Check domain policy again, this time referring to metadata */
1026 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1027 if (domain_test(domlist
, pol
, st2
->ss
->name
) == 0)
1028 /* Incompatible devices for this metadata type */
1030 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1031 /* Some partition types allow sparing, but not
1037 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1038 if (info
.component_size
> devsectors
)
1039 /* This partitioning doesn't fit in the device */
1042 /* This is an acceptable device to copy partition
1043 * metadata from. We could just stop here, but I
1044 * think I want to keep looking incase a larger
1045 * metadata which makes better use of the device can
1048 if (chosen
== NULL
||
1049 chosen_size
< info
.component_size
) {
1050 chosen_size
= info
.component_size
;
1055 chosen_st
->ss
->free_super(chosen_st
);
1064 domain_free(domlist
);
1065 dev_policy_free(pol2
);
1067 st2
->ss
->free_super(st2
);
1077 /* 'chosen' is the best device we can find. Let's write its
1078 * metadata to devname dfd is read-only so don't use that
1080 fd
= open(devname
, O_RDWR
);
1082 chosen_st
->ss
->store_super(chosen_st
, fd
);
1086 chosen_st
->ss
->free_super(chosen_st
);
1091 static int is_bare(int dfd
)
1093 unsigned long long size
= 0;
1094 char bufpad
[4096 + 4096];
1095 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1097 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1098 read(dfd
, buf
, 4096) != 4096)
1101 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1103 if (memcmp(buf
, buf
+1, 4095) != 0)
1106 /* OK, first 4K appear blank, try the end. */
1107 get_dev_size(dfd
, NULL
, &size
);
1108 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1109 read(dfd
, buf
, 4096) != 4096)
1112 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1114 if (memcmp(buf
, buf
+1, 4095) != 0)
1120 /* adding a spare to a regular array is quite different from adding one to
1121 * a set-of-partitions virtual array.
1122 * This function determines which is worth trying and tries as appropriate.
1123 * Arrays are given priority over partitions.
1125 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1126 struct map_ent
*target
,
1127 struct supertype
*st
, int verbose
)
1132 int partitions_ok
= 0;
1136 /* Can only add a spare if device has at least one domain */
1137 if (pol_find(pol
, pol_domain
) == NULL
)
1139 /* And only if some action allows spares */
1140 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1143 /* Now check if the device is bare.
1144 * bare devices can always be added as a spare
1145 * non-bare devices can only be added if spare-same-slot is permitted,
1146 * and this device is replacing a previous device - in which case 'target'
1149 if (!is_bare(dfd
)) {
1150 /* Must have a target and allow same_slot */
1151 /* Later - may allow force_spare without target */
1153 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1154 act_spare_same_slot
)) {
1156 fprintf(stderr
, Name
": %s is not bare, so not "
1157 "considering as a spare\n",
1165 /* It might be OK to add this device to an array - need to see
1166 * what arrays might be candidates.
1169 /* just try try 'array' or 'partition' based on this metadata */
1170 if (st
->ss
->add_to_super
)
1171 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1174 return partition_try_spare(devname
, dfdp
, pol
,
1177 /* No metadata was specified or found so options are open.
1178 * Check for whether any array metadata, or any partition metadata
1179 * might allow adding the spare. This check is just help to avoid
1180 * a more costly scan of all arrays when we can be sure that will
1183 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1184 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1185 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1187 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1188 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1193 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1195 if (rv
!= 0 && partitions_ok
)
1196 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1200 int IncrementalScan(int verbose
)
1202 /* look at every device listed in the 'map' file.
1203 * If one is found that is not running then:
1204 * look in mdadm.conf for bitmap file.
1205 * if one exists, but array has none, add it.
1206 * try to start array in auto-readonly mode
1208 struct map_ent
*mapl
= NULL
;
1210 struct mddev_ident
*devs
, *mddev
;
1214 devs
= conf_get_ident(NULL
);
1216 for (me
= mapl
; me
; me
= me
->next
) {
1217 mdu_array_info_t array
;
1218 mdu_bitmap_file_t bmf
;
1220 int mdfd
= open_dev(me
->devnum
);
1224 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1229 /* Ok, we can try this one. Maybe it needs a bitmap */
1230 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1231 if (mddev
->devname
&& me
->path
1232 && devname_matches(mddev
->devname
, me
->path
))
1234 if (mddev
&& mddev
->bitmap_file
) {
1236 * Note: early kernels will wrongly fail this, so it
1240 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1241 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1243 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1250 fprintf(stderr
, Name
1251 ": Added bitmap %s to %s\n",
1252 mddev
->bitmap_file
, me
->path
);
1253 else if (errno
!= EEXIST
)
1254 fprintf(stderr
, Name
1255 ": Failed to add bitmap to %s: %s\n",
1256 me
->path
, strerror(errno
));
1259 sra
= sysfs_read(mdfd
, 0, 0);
1261 if (sysfs_set_str(sra
, NULL
,
1262 "array_state", "read-auto") == 0) {
1264 fprintf(stderr
, Name
1265 ": started array %s\n",
1266 me
->path
?: devnum2devname(me
->devnum
));
1268 fprintf(stderr
, Name
1269 ": failed to start array %s: %s\n",
1270 me
->path
?: devnum2devname(me
->devnum
),
1279 static char *container2devname(char *devname
)
1281 char *mdname
= NULL
;
1283 if (devname
[0] == '/') {
1284 int fd
= open(devname
, O_RDONLY
);
1286 mdname
= devnum2devname(fd2devnum(fd
));
1291 struct map_ent
*mp
, *map
= NULL
;
1293 if (!parse_uuid(devname
, uuid
))
1295 mp
= map_by_uuid(&map
, uuid
);
1297 mdname
= devnum2devname(mp
->devnum
);
1304 static int Incremental_container(struct supertype
*st
, char *devname
,
1305 char *homehost
, int verbose
,
1306 int runstop
, int autof
)
1308 /* Collect the contents of this container and for each
1309 * array, choose a device name and assemble the array.
1312 struct mdinfo
*list
;
1314 struct map_ent
*map
= NULL
;
1317 struct mddev_ident
*match
;
1320 memset(&info
, 0, sizeof(info
));
1321 st
->ss
->getinfo_super(st
, &info
, NULL
);
1323 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1324 info
.container_enough
> 0)
1328 fprintf(stderr
, Name
": not enough devices to start the container\n");
1332 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1333 if (match
== NULL
&& rv
== 2)
1336 /* Need to compute 'trustworthy' */
1338 trustworthy
= LOCAL
;
1339 else if (st
->ss
->match_home(st
, homehost
) == 1)
1340 trustworthy
= LOCAL
;
1341 else if (st
->ss
->match_home(st
, "any") == 1)
1342 trustworthy
= LOCAL
;
1344 trustworthy
= FOREIGN
;
1346 list
= st
->ss
->container_content(st
, NULL
);
1348 fprintf(stderr
, Name
": failed to get exclusive lock on "
1351 for (ra
= list
; ra
; ra
= ra
->next
) {
1353 char chosen_name
[1024];
1355 struct mddev_ident
*match
= NULL
;
1357 mp
= map_by_uuid(&map
, ra
->uuid
);
1360 mdfd
= open_dev(mp
->devnum
);
1362 strcpy(chosen_name
, mp
->path
);
1364 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1367 /* Check in mdadm.conf for container == devname and
1368 * member == ra->text_version after second slash.
1370 char *sub
= strchr(ra
->text_version
+1, '/');
1371 struct mddev_ident
*array_list
;
1374 array_list
= conf_get_ident(NULL
);
1377 for(; array_list
; array_list
= array_list
->next
) {
1379 if (array_list
->member
== NULL
||
1380 array_list
->container
== NULL
)
1382 if (strcmp(array_list
->member
, sub
) != 0)
1384 if (array_list
->uuid_set
&&
1385 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1387 dn
= container2devname(array_list
->container
);
1390 if (strncmp(dn
, ra
->text_version
+1,
1392 ra
->text_version
[strlen(dn
)+1] != '/') {
1397 /* we have a match */
1400 fprintf(stderr
, Name
": match found for member %s\n",
1401 array_list
->member
);
1405 if (match
&& match
->devname
&&
1406 strcasecmp(match
->devname
, "<ignore>") == 0) {
1408 fprintf(stderr
, Name
": array %s/%s is "
1409 "explicitly ignored by mdadm.conf\n",
1410 match
->container
, match
->member
);
1414 trustworthy
= LOCAL
;
1416 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1424 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1425 chosen_name
, strerror(errno
));
1429 assemble_container_content(st
, mdfd
, ra
, runstop
,
1430 chosen_name
, verbose
);
1437 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1438 * raid arrays, and if so first fail (if needed) and then remove the device.
1440 * @devname - The device we want to remove
1441 * @id_path - name as found in /dev/disk/by-path for this device
1443 * Note: the device name must be a kernel name like "sda", so
1444 * that we can find it in /proc/mdstat
1446 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1450 struct mdstat_ent
*ent
;
1451 struct mddev_dev devlist
;
1454 dprintf(Name
": incremental removal without --path <id_path> "
1455 "lacks the possibility to re-add new device in this "
1458 if (strchr(devname
, '/')) {
1459 fprintf(stderr
, Name
": incremental removal requires a "
1460 "kernel device name, not a file: %s\n", devname
);
1463 ent
= mdstat_by_component(devname
);
1465 fprintf(stderr
, Name
": %s does not appear to be a component "
1466 "of any array\n", devname
);
1469 mdfd
= open_dev(ent
->devnum
);
1471 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1477 struct map_ent
*map
= NULL
, *me
;
1478 me
= map_by_devnum(&map
, ent
->devnum
);
1480 policy_save_path(id_path
, me
);
1484 memset(&devlist
, 0, sizeof(devlist
));
1485 devlist
.devname
= devname
;
1486 devlist
.disposition
= 'f';
1487 /* for a container, we must fail each member array */
1488 if (ent
->metadata_version
&&
1489 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1490 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1491 struct mdstat_ent
*memb
;
1492 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1493 if (is_container_member(memb
, ent
->dev
)) {
1494 int subfd
= open_dev(memb
->devnum
);
1496 Manage_subdevs(memb
->dev
, subfd
,
1497 &devlist
, verbose
, 0);
1501 free_mdstat(mdstat
);
1503 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
1504 devlist
.disposition
= 'r';
1505 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);