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
,
50 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
53 int verbose
, int *rvp
);
55 int Incremental(char *devname
, int verbose
, int runstop
,
56 struct supertype
*st
, char *homehost
, int require_homehost
,
57 int autof
, int freeze_reshape
)
59 /* Add this device to an array, creating the array if necessary
60 * and starting the array if sensible or - if runstop>0 - if possible.
62 * This has several steps:
64 * 1/ Check if device is permitted by mdadm.conf, reject if not.
65 * 2/ Find metadata, reject if none appropriate (check
66 * version/name from args)
67 * 3/ Check if there is a match in mdadm.conf
68 * 3a/ if not, check for homehost match. If no match, assemble as
70 * 4/ Determine device number.
71 * - If in mdadm.conf with std name, use that
72 * - UUID in /var/run/mdadm.map use that
73 * - If name is suggestive, use that. unless in use with different uuid.
74 * - Choose a free, high number.
75 * - Use a partitioned device unless strong suggestion not to.
77 * Don't choose partitioned for containers.
78 * 5/ Find out if array already exists
80 * - choose a name, from mdadm.conf or 'name' field in array.
84 * - check one drive in array to make sure metadata is a reasonably
85 * close match. Reject if not (e.g. different type)
87 * 6/ Make sure /var/run/mdadm.map contains this array.
88 * 7/ Is there enough devices to possibly start the array?
89 * For a container, this means running Incremental_container.
90 * 7a/ if not, finish with success.
92 * - read all metadata and arrange devices like -A does
93 * - if number of OK devices match expected, or -R and there are enough,
94 * start the array (auto-readonly).
97 struct mdinfo info
, dinfo
;
98 struct mdinfo
*sra
= NULL
, *d
;
99 struct mddev_ident
*match
;
100 char chosen_name
[1024];
102 struct map_ent
*mp
, *map
= NULL
;
103 int dfd
= -1, mdfd
= -1;
108 mdu_array_info_t ainf
;
109 struct dev_policy
*policy
= NULL
;
110 struct map_ent target_array
;
113 struct createinfo
*ci
= conf_get_create_info();
115 if (stat(devname
, &stb
) < 0) {
117 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
118 devname
, strerror(errno
));
121 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
123 fprintf(stderr
, Name
": %s is not a block device.\n",
127 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
130 fprintf(stderr
, Name
": cannot open %s: %s.\n",
131 devname
, strerror(errno
));
134 /* If the device is a container, we do something very different */
135 if (must_be_container(dfd
)) {
137 st
= super_by_fd(dfd
, NULL
);
138 if (st
&& st
->ss
->load_container
)
139 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
142 if (!rv
&& st
->ss
->container_content
) {
144 fprintf(stderr
, Name
": failed to get "
145 "exclusive lock on mapfile\n");
146 rv
= Incremental_container(st
, devname
, homehost
,
147 verbose
, runstop
, autof
,
153 fprintf(stderr
, Name
": %s is not part of an md array.\n",
158 /* 1/ Check if device is permitted by mdadm.conf */
160 if (!conf_test_dev(devname
)) {
163 ": %s not permitted by mdadm.conf.\n",
168 /* 2/ Find metadata, reject if none appropriate (check
169 * version/name from args) */
171 if (fstat(dfd
, &stb
) < 0) {
173 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
174 devname
, strerror(errno
));
177 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
179 fprintf(stderr
, Name
": %s is not a block device.\n",
184 dinfo
.disk
.major
= major(stb
.st_rdev
);
185 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
187 policy
= disk_policy(&dinfo
);
188 have_target
= policy_check_path(&dinfo
, &target_array
);
190 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
193 ": no recognisable superblock on %s.\n",
195 rv
= try_spare(devname
, &dfd
, policy
,
196 have_target
? &target_array
: NULL
,
200 if (st
->ss
->compare_super
== NULL
||
201 st
->ss
->load_super(st
, dfd
, NULL
)) {
203 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
205 rv
= try_spare(devname
, &dfd
, policy
,
206 have_target
? &target_array
: NULL
,
211 close (dfd
); dfd
= -1;
213 st
->ss
->getinfo_super(st
, &info
, NULL
);
215 /* 3/ Check if there is a match in mdadm.conf */
216 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
217 if (!match
&& rv
== 2)
220 if (match
&& match
->devname
221 && strcasecmp(match
->devname
, "<ignore>") == 0) {
223 fprintf(stderr
, Name
": array containing %s is explicitly"
224 " ignored by mdadm.conf\n",
229 /* 3a/ if not, check for homehost match. If no match, continue
230 * but don't trust the 'name' in the array. Thus a 'random' minor
231 * number will be assigned, and the device name will be based
235 else if (st
->ss
->match_home(st
, homehost
) == 1)
237 else if (st
->ss
->match_home(st
, "any") == 1)
238 trustworthy
= LOCAL_ANY
;
240 trustworthy
= FOREIGN
;
243 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
244 (trustworthy
== LOCAL
))) {
247 ": %s has metadata type %s for which "
248 "auto-assembly is disabled\n",
249 devname
, st
->ss
->name
);
252 if (trustworthy
== LOCAL_ANY
)
255 /* There are three possible sources for 'autof': command line,
256 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
257 * ARRAY takes precedence, then command line, then
260 if (match
&& match
->autof
)
261 autof
= match
->autof
;
265 name_to_use
= info
.name
;
266 if (name_to_use
[0] == 0 &&
267 info
.array
.level
== LEVEL_CONTAINER
&&
268 trustworthy
== LOCAL
) {
269 name_to_use
= info
.text_version
;
270 trustworthy
= METADATA
;
272 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
273 ! require_homehost
&&
274 conf_name_is_free(name_to_use
))
277 /* strip "hostname:" prefix from name if we have decided
278 * to treat it as LOCAL
280 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
281 name_to_use
= strchr(name_to_use
, ':')+1;
283 /* 4/ Check if array exists.
286 fprintf(stderr
, Name
": failed to get exclusive lock on "
288 mp
= map_by_uuid(&map
, info
.uuid
);
290 mdfd
= open_dev(mp
->devnum
);
296 /* Couldn't find an existing array, maybe make a new one */
297 mdfd
= create_mddev(match
? match
->devname
: NULL
,
298 name_to_use
, autof
, trustworthy
, chosen_name
);
303 sysfs_init(&info
, mdfd
, 0);
305 if (set_array_info(mdfd
, st
, &info
) != 0) {
306 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
307 chosen_name
, strerror(errno
));
313 dinfo
.disk
.major
= major(stb
.st_rdev
);
314 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
315 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
316 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
317 devname
, chosen_name
, strerror(errno
));
318 ioctl(mdfd
, STOP_ARRAY
, 0);
322 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
323 GET_OFFSET
| GET_SIZE
));
325 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
326 /* It really should be 'none' - must be old buggy
327 * kernel, and mdadm -I may not be able to complete.
330 ioctl(mdfd
, STOP_ARRAY
, NULL
);
332 ": You have an old buggy kernel which cannot support\n"
333 " --incremental reliably. Aborting.\n");
338 info
.array
.working_disks
= 1;
339 /* 6/ Make sure /var/run/mdadm.map contains this array. */
340 map_update(&map
, fd2devnum(mdfd
),
342 info
.uuid
, chosen_name
);
345 /* - check one drive in array to make sure metadata is a reasonably */
346 /* close match. Reject if not (e.g. different type) */
347 /* - add the device */
351 struct supertype
*st2
;
352 struct mdinfo info2
, *d
;
354 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
355 GET_OFFSET
| GET_SIZE
));
358 strcpy(chosen_name
, mp
->path
);
360 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
362 /* It is generally not OK to add non-spare drives to a
363 * running array as they are probably missing because
364 * they failed. However if runstop is 1, then the
365 * array was possibly started early and our best bet is
366 * to add this anyway.
367 * Also if action policy is re-add or better we allow
369 * This doesn't apply to containers as the 'non-spare'
370 * flag has a different meaning. The test has to happen
371 * at the device level there
373 if (!st
->ss
->external
374 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
375 && ! policy_action_allows(policy
, st
->ss
->name
,
378 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
380 ": not adding %s to active array (without --run) %s\n",
381 devname
, chosen_name
);
391 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
392 sra
->devs
->disk
.minor
);
393 dfd2
= dev_open(dn
, O_RDONLY
);
395 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
396 st
->ss
->compare_super(st
, st2
) != 0) {
398 ": metadata mismatch between %s and "
400 devname
, chosen_name
);
406 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
407 st2
->ss
->free_super(st2
);
408 if (info
.array
.level
!= info2
.array
.level
||
409 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
410 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
412 ": unexpected difference between %s and %s.\n",
413 chosen_name
, devname
);
418 info2
.disk
.major
= major(stb
.st_rdev
);
419 info2
.disk
.minor
= minor(stb
.st_rdev
);
420 /* add disk needs to know about containers */
421 if (st
->ss
->external
)
422 sra
->array
.level
= LEVEL_CONTAINER
;
423 err
= add_disk(mdfd
, st
, sra
, &info2
);
424 if (err
< 0 && errno
== EBUSY
) {
425 /* could be another device present with the same
426 * disk.number. Find and reject any such
428 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
429 info
.events
, verbose
, chosen_name
);
430 err
= add_disk(mdfd
, st
, sra
, &info2
);
433 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
434 devname
, chosen_name
, strerror(errno
));
438 info
.array
.working_disks
= 0;
439 for (d
= sra
->devs
; d
; d
=d
->next
)
440 info
.array
.working_disks
++;
444 /* 7/ Is there enough devices to possibly start the array? */
445 /* 7a/ if not, finish with success. */
446 if (info
.array
.level
== LEVEL_CONTAINER
) {
447 int devnum
= devnum
; /* defined and used iff ->external */
448 /* Try to assemble within the container */
451 ": container %s now has %d devices\n",
452 chosen_name
, info
.array
.working_disks
);
453 wait_for(chosen_name
, mdfd
);
454 if (st
->ss
->external
)
455 devnum
= fd2devnum(mdfd
);
456 if (st
->ss
->load_container
)
457 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
461 rv
= Incremental_container(st
, chosen_name
, homehost
,
462 verbose
, runstop
, autof
,
466 /* Don't fail the whole -I if a subarray didn't
467 * have enough devices to start yet
470 /* after spare is added, ping monitor for external metadata
471 * so that it can eg. try to rebuild degraded array */
472 if (st
->ss
->external
)
473 ping_monitor_by_id(devnum
);
477 /* We have added something to the array, so need to re-read the
478 * state. Eventually this state should be kept up-to-date as
482 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
483 GET_OFFSET
| GET_SIZE
));
484 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
485 if (enough(info
.array
.level
, info
.array
.raid_disks
,
486 info
.array
.layout
, info
.array
.state
& 1,
487 avail
, active_disks
) == 0) {
490 ": %s attached to %s, not enough to start (%d).\n",
491 devname
, chosen_name
, active_disks
);
497 /* - if number of OK devices match expected, or -R and there */
499 /* + add any bitmap file */
500 /* + start the array (auto-readonly). */
502 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
505 ": %s attached to %s which is already active.\n",
506 devname
, chosen_name
);
512 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
514 /* Let's try to start it */
515 if (match
&& match
->bitmap_file
) {
516 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
519 ": Could not open bitmap file %s.\n",
523 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
526 ": Failed to set bitmapfile for %s.\n",
532 /* Need to remove from the array any devices which
533 * 'count_active' discerned were too old or inappropriate
535 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
536 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
537 remove_disk(mdfd
, st
, sra
, d
);
539 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
540 && trustworthy
!= FOREIGN
)
541 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
543 rv
= sysfs_set_str(sra
, NULL
,
544 "array_state", "read-auto");
548 ": %s attached to %s, which has been started.\n",
549 devname
, chosen_name
);
551 wait_for(chosen_name
, mdfd
);
552 /* We just started the array, so some devices
553 * might have been evicted from the array
554 * because their event counts were too old.
555 * If the action=re-add policy is in-force for
556 * those devices we should re-add them now.
558 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
559 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
560 add_disk(mdfd
, st
, sra
, dsk
) == 0)
562 ": %s re-added to %s\n",
563 dsk
->sys_name
, chosen_name
);
567 ": %s attached to %s, but failed to start: %s.\n",
568 devname
, chosen_name
, strerror(errno
));
574 ": %s attached to %s, not enough to start safely.\n",
575 devname
, chosen_name
);
585 dev_policy_free(policy
);
594 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
597 int verbose
, int *rvp
)
599 struct mddev_ident
*array_list
, *match
;
600 array_list
= conf_get_ident(NULL
);
602 for (; array_list
; array_list
= array_list
->next
) {
603 if (array_list
->uuid_set
&&
604 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
606 if (verbose
>= 2 && array_list
->devname
)
608 ": UUID differs from %s.\n",
609 array_list
->devname
);
612 if (array_list
->name
[0] &&
613 strcasecmp(array_list
->name
, info
->name
) != 0) {
614 if (verbose
>= 2 && array_list
->devname
)
616 ": Name differs from %s.\n",
617 array_list
->devname
);
620 if (array_list
->devices
&&
621 !match_oneof(array_list
->devices
, devname
)) {
622 if (verbose
>= 2 && array_list
->devname
)
624 ": Not a listed device for %s.\n",
625 array_list
->devname
);
628 if (array_list
->super_minor
!= UnSet
&&
629 array_list
->super_minor
!= info
->array
.md_minor
) {
630 if (verbose
>= 2 && array_list
->devname
)
632 ": Different super-minor to %s.\n",
633 array_list
->devname
);
636 if (!array_list
->uuid_set
&&
637 !array_list
->name
[0] &&
638 !array_list
->devices
&&
639 array_list
->super_minor
== UnSet
) {
640 if (verbose
>= 2 && array_list
->devname
)
642 ": %s doesn't have any identifying information.\n",
643 array_list
->devname
);
646 /* FIXME, should I check raid_disks and level too?? */
650 if (match
->devname
&& array_list
->devname
)
652 ": we match both %s and %s - cannot decide which to use.\n",
653 match
->devname
, array_list
->devname
);
656 ": multiple lines in mdadm.conf match\n");
667 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
668 int number
, __u64 events
, int verbose
,
671 /* Find a device attached to this array with a disk.number of number
672 * and events less than the passed events, and remove the device.
677 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
678 return; /* not safe to remove from active arrays
679 * without thinking more */
681 for (d
= sra
->devs
; d
; d
= d
->next
) {
685 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
686 dfd
= dev_open(dn
, O_RDONLY
);
689 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
693 st
->ss
->getinfo_super(st
, &info
, NULL
);
694 st
->ss
->free_super(st
);
697 if (info
.disk
.number
!= number
||
698 info
.events
>= events
)
701 if (d
->disk
.raid_disk
> -1)
702 sysfs_set_str(sra
, d
, "slot", "none");
703 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
706 ": removing old device %s from %s\n",
707 d
->sys_name
+4, array_name
);
711 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
712 int mdfd
, char **availp
,
713 struct mdinfo
*bestinfo
)
715 /* count how many devices in sra think they are active */
718 __u64 max_events
= 0;
730 for (d
= sra
->devs
; d
; d
= d
->next
)
732 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
738 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
739 dfd
= dev_open(dn
, O_RDONLY
);
742 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
746 info
.array
.raid_disks
= raid_disks
;
747 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
749 raid_disks
= info
.array
.raid_disks
;
750 avail
= calloc(raid_disks
, 1);
752 fprintf(stderr
, Name
": out of memory.\n");
757 best
= calloc(raid_disks
, sizeof(int));
758 devmap
= calloc(raid_disks
* numdevs
, 1);
760 st
->ss
->getinfo_super(st
, &info
, devmap
);
763 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
767 max_events
= info
.events
;
768 avail
[info
.disk
.raid_disk
] = 2;
769 best
[info
.disk
.raid_disk
] = devnum
;
770 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
771 } else if (info
.events
== max_events
) {
772 avail
[info
.disk
.raid_disk
] = 2;
773 best
[info
.disk
.raid_disk
] = devnum
;
774 } else if (info
.events
== max_events
-1) {
775 if (avail
[info
.disk
.raid_disk
] == 0) {
776 avail
[info
.disk
.raid_disk
] = 1;
777 best
[info
.disk
.raid_disk
] = devnum
;
779 } else if (info
.events
< max_events
- 1)
781 else if (info
.events
== max_events
+1) {
783 max_events
= info
.events
;
784 for (i
=0; i
< raid_disks
; i
++)
787 avail
[info
.disk
.raid_disk
] = 2;
788 best
[info
.disk
.raid_disk
] = devnum
;
789 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
790 } else { /* info.events much bigger */
791 memset(avail
, 0, raid_disks
);
792 max_events
= info
.events
;
793 avail
[info
.disk
.raid_disk
] = 2;
794 best
[info
.disk
.raid_disk
] = devnum
;
795 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
798 st
->ss
->free_super(st
);
802 /* We need to reject any device that thinks the best device is
803 * failed or missing */
804 for (b
= 0; b
< raid_disks
; b
++)
808 for (i
= 0 ; i
< raid_disks
; i
++) {
809 if (i
!= b
&& avail
[i
])
810 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
811 /* This device thinks 'b' is failed -
814 for (d
=sra
->devs
; devnum
; d
= d
->next
)
816 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
827 /* test if container has degraded member(s) */
828 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
830 mdu_array_info_t array
;
832 int max_degraded
= 0;
834 for(; map
; map
= map
->next
) {
835 if (!is_subarray(map
->metadata
) ||
836 devname2devnum(map
->metadata
+1) != me
->devnum
)
838 afd
= open_dev(map
->devnum
);
841 /* most accurate information regarding array degradation */
842 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
843 int degraded
= array
.raid_disks
- array
.active_disks
-
845 if (degraded
> max_degraded
)
846 max_degraded
= degraded
;
850 return (max_degraded
);
853 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
854 struct map_ent
*target
, int bare
,
855 struct supertype
*st
, int verbose
)
857 /* This device doesn't have any md metadata
858 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
859 * If 'st' is not set, then we only know that some metadata allows this,
860 * others possibly don't.
861 * So look for a container or array to attach the device to.
862 * Prefer 'target' if that is set and the array is found.
864 * If st is set, then only arrays of that type are considered
865 * Return 0 on success, or some exit code on failure, probably 1.
869 struct map_ent
*mp
, *map
= NULL
;
870 struct mdinfo
*chosen
= NULL
;
873 if (fstat(dfd
, &stb
) != 0)
877 * Now we need to find a suitable array to add this to.
878 * We only accept arrays that:
880 * - are in the same domains as the device
881 * - are of an size for which the device will be useful
882 * and we choose the one that is the most degraded
885 if (map_lock(&map
)) {
886 fprintf(stderr
, Name
": failed to get exclusive lock on "
890 for (mp
= map
; mp
; mp
= mp
->next
) {
891 struct supertype
*st2
;
892 struct domainlist
*dl
= NULL
;
894 unsigned long long devsize
;
895 unsigned long long component_size
= 0;
897 if (is_subarray(mp
->metadata
))
900 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
902 (st
->minor_version
>= 0 &&
903 st
->minor_version
!= st2
->minor_version
)) {
905 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
912 sra
= sysfs_read(-1, mp
->devnum
,
913 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
914 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
916 /* Probably a container - no degraded info */
917 sra
= sysfs_read(-1, mp
->devnum
,
918 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
919 GET_COMPONENT
|GET_VERSION
);
921 sra
->array
.failed_disks
= -1;
928 for(i
=0; !st2
&& superlist
[i
]; i
++)
929 st2
= superlist
[i
]->match_metadata_desc(
933 fprintf(stderr
, Name
": not adding %s to %s"
934 " as metadata not recognised.\n",
938 /* Need to double check the 'act_spare' permissions applies
941 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
943 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
944 act_spare_same_slot
))
948 /* update number of failed disks for mostly degraded
949 * container member */
950 if (sra
->array
.failed_disks
== -1)
951 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
953 get_dev_size(dfd
, NULL
, &devsize
);
954 if (sra
->component_size
== 0) {
955 /* true for containers, here we must read superblock
956 * to obtain minimum spare size */
957 struct supertype
*st3
= dup_super(st2
);
958 int mdfd
= open_dev(mp
->devnum
);
961 if (st3
->ss
->load_container
&&
962 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
963 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
964 st3
->ss
->free_super(st3
);
969 if ((sra
->component_size
> 0 &&
970 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
972 (sra
->component_size
== 0 && devsize
< component_size
)) {
974 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
978 /* test against target.
979 * If 'target' is set and 'bare' is false, we only accept
980 * arrays/containers that match 'target'.
981 * If 'target' is set and 'bare' is true, we prefer the
982 * array which matches 'target'.
983 * target is considered only if we deal with degraded array
985 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
986 act_spare_same_slot
)) {
987 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
988 memcmp(target
->uuid
, mp
->uuid
,
989 sizeof(target
->uuid
)) == 0 &&
990 sra
->array
.failed_disks
> 0) {
991 /* This is our target!! */
996 /* skip to end so we don't check any more */
1001 /* not our target */
1006 dl
= domain_from_array(sra
, st2
->ss
->name
);
1007 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
1008 /* domain test fails */
1010 fprintf(stderr
, Name
": not adding %s to %s as"
1011 " it is not in a compatible domain\n",
1016 /* all tests passed, OK to add to this array */
1020 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1034 /* add current device to chosen array as a spare */
1035 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
1037 struct mddev_dev devlist
;
1039 devlist
.next
= NULL
;
1042 devlist
.writemostly
= 0;
1043 devlist
.devname
= devname
;
1044 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
1045 minor(stb
.st_rdev
));
1046 devlist
.disposition
= 'a';
1049 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1055 fprintf(stderr
, Name
": added %s as spare for %s\n",
1056 devname
, chosen
->sys_name
);
1058 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
1059 devname
, chosen
->sys_name
);
1067 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1068 struct supertype
*st
, int verbose
)
1070 /* we know that at least one partition virtual-metadata is
1071 * allowed to incorporate spares like this device. We need to
1072 * find a suitable device to copy partition information from.
1074 * Getting a list of all disk (not partition) devices is
1075 * slightly non-trivial. We could look at /sys/block, but
1076 * that is theoretically due to be removed. Maybe best to use
1077 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1078 * we depend on this directory of 'path' info. But that fails
1079 * to find loop devices and probably others. Maybe don't
1080 * worry about that, they aren't the real target.
1082 * So: check things in /dev/disk/by-path to see if they are in
1083 * a compatible domain, then load the partition table and see
1084 * if it is OK for the new device, and choose the largest
1085 * partition table that fits.
1089 char *chosen
= NULL
;
1090 unsigned long long chosen_size
= 0;
1091 struct supertype
*chosen_st
= NULL
;
1094 dir
= opendir("/dev/disk/by-path");
1097 while ((de
= readdir(dir
)) != NULL
) {
1099 struct dev_policy
*pol2
= NULL
;
1100 struct domainlist
*domlist
= NULL
;
1103 struct supertype
*st2
= NULL
;
1104 char *devname
= NULL
;
1105 unsigned long long devsectors
;
1107 if (de
->d_ino
== 0 ||
1108 de
->d_name
[0] == '.' ||
1109 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1112 ep
= de
->d_name
+ strlen(de
->d_name
);
1113 while (ep
> de
->d_name
&&
1116 if (ep
> de
->d_name
+ 5 &&
1117 strncmp(ep
-5, "-part", 5) == 0)
1118 /* This is a partition - skip it */
1121 pol2
= path_policy(de
->d_name
, type_disk
);
1123 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1124 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1125 /* new device is incompatible with this device. */
1128 domain_free(domlist
);
1131 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1135 fd
= open(devname
, O_RDONLY
);
1138 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1143 st2
= dup_super(st
);
1145 st2
= guess_super_type(fd
, guess_partitions
);
1147 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1151 /* Check domain policy again, this time referring to metadata */
1152 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1153 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1154 /* Incompatible devices for this metadata type */
1156 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1157 /* Some partition types allow sparing, but not
1163 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1164 if (info
.component_size
> devsectors
)
1165 /* This partitioning doesn't fit in the device */
1168 /* This is an acceptable device to copy partition
1169 * metadata from. We could just stop here, but I
1170 * think I want to keep looking incase a larger
1171 * metadata which makes better use of the device can
1174 if (chosen
== NULL
||
1175 chosen_size
< info
.component_size
) {
1176 chosen_size
= info
.component_size
;
1181 chosen_st
->ss
->free_super(chosen_st
);
1190 domain_free(domlist
);
1191 dev_policy_free(pol2
);
1193 st2
->ss
->free_super(st2
);
1203 /* 'chosen' is the best device we can find. Let's write its
1204 * metadata to devname dfd is read-only so don't use that
1206 fd
= open(devname
, O_RDWR
);
1208 chosen_st
->ss
->store_super(chosen_st
, fd
);
1212 chosen_st
->ss
->free_super(chosen_st
);
1217 static int is_bare(int dfd
)
1219 unsigned long long size
= 0;
1220 char bufpad
[4096 + 4096];
1221 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1223 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1224 read(dfd
, buf
, 4096) != 4096)
1227 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1229 if (memcmp(buf
, buf
+1, 4095) != 0)
1232 /* OK, first 4K appear blank, try the end. */
1233 get_dev_size(dfd
, NULL
, &size
);
1234 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1235 read(dfd
, buf
, 4096) != 4096)
1238 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1240 if (memcmp(buf
, buf
+1, 4095) != 0)
1246 /* adding a spare to a regular array is quite different from adding one to
1247 * a set-of-partitions virtual array.
1248 * This function determines which is worth trying and tries as appropriate.
1249 * Arrays are given priority over partitions.
1251 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1252 struct map_ent
*target
,
1253 struct supertype
*st
, int verbose
)
1258 int partitions_ok
= 0;
1262 /* Can only add a spare if device has at least one domain */
1263 if (pol_find(pol
, pol_domain
) == NULL
)
1265 /* And only if some action allows spares */
1266 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1269 /* Now check if the device is bare.
1270 * bare devices can always be added as a spare
1271 * non-bare devices can only be added if spare-same-slot is permitted,
1272 * and this device is replacing a previous device - in which case 'target'
1275 if (!is_bare(dfd
)) {
1276 /* Must have a target and allow same_slot */
1277 /* Later - may allow force_spare without target */
1279 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1280 act_spare_same_slot
)) {
1282 fprintf(stderr
, Name
": %s is not bare, so not "
1283 "considering as a spare\n",
1291 /* It might be OK to add this device to an array - need to see
1292 * what arrays might be candidates.
1295 /* just try try 'array' or 'partition' based on this metadata */
1296 if (st
->ss
->add_to_super
)
1297 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1300 return partition_try_spare(devname
, dfdp
, pol
,
1303 /* No metadata was specified or found so options are open.
1304 * Check for whether any array metadata, or any partition metadata
1305 * might allow adding the spare. This check is just help to avoid
1306 * a more costly scan of all arrays when we can be sure that will
1309 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1310 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1311 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1313 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1314 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1319 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1321 if (rv
!= 0 && partitions_ok
)
1322 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1326 int IncrementalScan(int verbose
)
1328 /* look at every device listed in the 'map' file.
1329 * If one is found that is not running then:
1330 * look in mdadm.conf for bitmap file.
1331 * if one exists, but array has none, add it.
1332 * try to start array in auto-readonly mode
1334 struct map_ent
*mapl
= NULL
;
1336 struct mddev_ident
*devs
, *mddev
;
1340 devs
= conf_get_ident(NULL
);
1342 for (me
= mapl
; me
; me
= me
->next
) {
1343 mdu_array_info_t array
;
1344 mdu_bitmap_file_t bmf
;
1346 int mdfd
= open_dev(me
->devnum
);
1350 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1355 /* Ok, we can try this one. Maybe it needs a bitmap */
1356 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1357 if (mddev
->devname
&& me
->path
1358 && devname_matches(mddev
->devname
, me
->path
))
1360 if (mddev
&& mddev
->bitmap_file
) {
1362 * Note: early kernels will wrongly fail this, so it
1366 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1367 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1369 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1376 fprintf(stderr
, Name
1377 ": Added bitmap %s to %s\n",
1378 mddev
->bitmap_file
, me
->path
);
1379 else if (errno
!= EEXIST
)
1380 fprintf(stderr
, Name
1381 ": Failed to add bitmap to %s: %s\n",
1382 me
->path
, strerror(errno
));
1385 sra
= sysfs_read(mdfd
, 0, 0);
1387 if (sysfs_set_str(sra
, NULL
,
1388 "array_state", "read-auto") == 0) {
1390 fprintf(stderr
, Name
1391 ": started array %s\n",
1392 me
->path
?: devnum2devname(me
->devnum
));
1394 fprintf(stderr
, Name
1395 ": failed to start array %s: %s\n",
1396 me
->path
?: devnum2devname(me
->devnum
),
1405 static char *container2devname(char *devname
)
1407 char *mdname
= NULL
;
1409 if (devname
[0] == '/') {
1410 int fd
= open(devname
, O_RDONLY
);
1412 mdname
= devnum2devname(fd2devnum(fd
));
1417 struct map_ent
*mp
, *map
= NULL
;
1419 if (!parse_uuid(devname
, uuid
))
1421 mp
= map_by_uuid(&map
, uuid
);
1423 mdname
= devnum2devname(mp
->devnum
);
1430 static int Incremental_container(struct supertype
*st
, char *devname
,
1431 char *homehost
, int verbose
,
1432 int runstop
, int autof
, int freeze_reshape
)
1434 /* Collect the contents of this container and for each
1435 * array, choose a device name and assemble the array.
1438 struct mdinfo
*list
;
1440 struct map_ent
*map
= NULL
;
1443 struct mddev_ident
*match
;
1445 struct domainlist
*domains
;
1446 struct map_ent
*smp
;
1450 st
->ss
->getinfo_super(st
, &info
, NULL
);
1452 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1453 info
.container_enough
> 0)
1457 fprintf(stderr
, Name
": not enough devices to start the container\n");
1461 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1462 if (match
== NULL
&& rv
== 2)
1465 /* Need to compute 'trustworthy' */
1467 trustworthy
= LOCAL
;
1468 else if (st
->ss
->match_home(st
, homehost
) == 1)
1469 trustworthy
= LOCAL
;
1470 else if (st
->ss
->match_home(st
, "any") == 1)
1471 trustworthy
= LOCAL
;
1473 trustworthy
= FOREIGN
;
1475 list
= st
->ss
->container_content(st
, NULL
);
1476 /* do not assemble arrays that might have bad blocks */
1477 if (list
&& list
->array
.state
& (1<<MD_SB_BBM_ERRORS
)) {
1478 fprintf(stderr
, Name
": BBM log found in metadata. "
1479 "Cannot activate array(s).\n");
1480 /* free container data and exit */
1485 for (ra
= list
; ra
; ra
= ra
->next
) {
1487 char chosen_name
[1024];
1489 struct mddev_ident
*match
= NULL
;
1491 mp
= map_by_uuid(&map
, ra
->uuid
);
1494 mdfd
= open_dev(mp
->devnum
);
1496 strcpy(chosen_name
, mp
->path
);
1498 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1501 /* Check in mdadm.conf for container == devname and
1502 * member == ra->text_version after second slash.
1504 char *sub
= strchr(ra
->text_version
+1, '/');
1505 struct mddev_ident
*array_list
;
1508 array_list
= conf_get_ident(NULL
);
1511 for(; array_list
; array_list
= array_list
->next
) {
1513 if (array_list
->member
== NULL
||
1514 array_list
->container
== NULL
)
1516 if (strcmp(array_list
->member
, sub
) != 0)
1518 if (array_list
->uuid_set
&&
1519 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1521 dn
= container2devname(array_list
->container
);
1524 if (strncmp(dn
, ra
->text_version
+1,
1526 ra
->text_version
[strlen(dn
)+1] != '/') {
1531 /* we have a match */
1534 fprintf(stderr
, Name
": match found for member %s\n",
1535 array_list
->member
);
1539 if (match
&& match
->devname
&&
1540 strcasecmp(match
->devname
, "<ignore>") == 0) {
1542 fprintf(stderr
, Name
": array %s/%s is "
1543 "explicitly ignored by mdadm.conf\n",
1544 match
->container
, match
->member
);
1548 trustworthy
= LOCAL
;
1550 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1558 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1559 chosen_name
, strerror(errno
));
1563 assemble_container_content(st
, mdfd
, ra
, runstop
,
1564 chosen_name
, verbose
, NULL
,
1569 /* Now move all suitable spares from spare container */
1570 domains
= domain_from_array(list
, st
->ss
->name
);
1571 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1573 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1574 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1575 /* spare container found */
1576 struct supertype
*sst
=
1577 super_imsm
.match_metadata_desc("imsm");
1578 struct mdinfo
*sinfo
;
1579 unsigned long long min_size
= 0;
1580 if (st
->ss
->min_acceptable_spare_size
)
1581 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1582 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1584 sinfo
= container_choose_spares(sst
, min_size
,
1587 sst
->ss
->free_super(sst
);
1590 struct mdinfo
*disks
= sinfo
->devs
;
1592 /* move spare from spare
1593 * container to currently
1599 makedev(disks
->disk
.major
,
1600 disks
->disk
.minor
)))
1602 disks
= disks
->next
;
1605 fprintf(stderr
, Name
1606 ": Added %d spare%s to %s\n",
1607 count
, count
>1?"s":"", devname
);
1613 domain_free(domains
);
1618 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1619 * raid arrays, and if so first fail (if needed) and then remove the device.
1621 * @devname - The device we want to remove
1622 * @id_path - name as found in /dev/disk/by-path for this device
1624 * Note: the device name must be a kernel name like "sda", so
1625 * that we can find it in /proc/mdstat
1627 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1631 struct mdstat_ent
*ent
;
1632 struct mddev_dev devlist
;
1635 dprintf(Name
": incremental removal without --path <id_path> "
1636 "lacks the possibility to re-add new device in this "
1639 if (strchr(devname
, '/')) {
1640 fprintf(stderr
, Name
": incremental removal requires a "
1641 "kernel device name, not a file: %s\n", devname
);
1644 ent
= mdstat_by_component(devname
);
1646 fprintf(stderr
, Name
": %s does not appear to be a component "
1647 "of any array\n", devname
);
1650 mdfd
= open_dev(ent
->devnum
);
1652 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1658 struct map_ent
*map
= NULL
, *me
;
1659 me
= map_by_devnum(&map
, ent
->devnum
);
1661 policy_save_path(id_path
, me
);
1665 memset(&devlist
, 0, sizeof(devlist
));
1666 devlist
.devname
= devname
;
1667 devlist
.disposition
= 'f';
1668 /* for a container, we must fail each member array */
1669 if (ent
->metadata_version
&&
1670 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1671 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1672 struct mdstat_ent
*memb
;
1673 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1674 if (is_container_member(memb
, ent
->dev
)) {
1675 int subfd
= open_dev(memb
->devnum
);
1677 Manage_subdevs(memb
->dev
, subfd
,
1678 &devlist
, verbose
, 0,
1683 free_mdstat(mdstat
);
1685 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1686 devlist
.disposition
= 'r';
1687 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);