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
,
48 int readonly
, int autof
,
51 int Incremental(char *devname
, int verbose
, int runstop
,
52 struct supertype
*st
, char *homehost
, int require_homehost
,
53 int autof
, int freeze_reshape
)
55 /* Add this device to an array, creating the array if necessary
56 * and starting the array if sensible or - if runstop>0 - if possible.
58 * This has several steps:
60 * 1/ Check if device is permitted by mdadm.conf, reject if not.
61 * 2/ Find metadata, reject if none appropriate (check
62 * version/name from args)
63 * 3/ Check if there is a match in mdadm.conf
64 * 3a/ if not, check for homehost match. If no match, assemble as
66 * 4/ Determine device number.
67 * - If in mdadm.conf with std name, use that
68 * - UUID in /var/run/mdadm.map use that
69 * - If name is suggestive, use that. unless in use with different uuid.
70 * - Choose a free, high number.
71 * - Use a partitioned device unless strong suggestion not to.
73 * Don't choose partitioned for containers.
74 * 5/ Find out if array already exists
76 * - choose a name, from mdadm.conf or 'name' field in array.
80 * - check one drive in array to make sure metadata is a reasonably
81 * close match. Reject if not (e.g. different type)
83 * 6/ Make sure /var/run/mdadm.map contains this array.
84 * 7/ Is there enough devices to possibly start the array?
85 * For a container, this means running Incremental_container.
86 * 7a/ if not, finish with success.
88 * - read all metadata and arrange devices like -A does
89 * - if number of OK devices match expected, or -R and there are enough,
90 * start the array (auto-readonly).
93 struct mdinfo info
, dinfo
;
94 struct mdinfo
*sra
= NULL
, *d
;
95 struct mddev_ident
*match
;
96 char chosen_name
[1024];
98 struct map_ent
*mp
, *map
= NULL
;
99 int dfd
= -1, mdfd
= -1;
104 mdu_array_info_t ainf
;
105 struct dev_policy
*policy
= NULL
;
106 struct map_ent target_array
;
109 struct createinfo
*ci
= conf_get_create_info();
111 if (stat(devname
, &stb
) < 0) {
113 pr_err("stat failed for %s: %s.\n",
114 devname
, strerror(errno
));
117 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
119 pr_err("%s is not a block device.\n",
123 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
126 pr_err("cannot open %s: %s.\n",
127 devname
, strerror(errno
));
130 /* If the device is a container, we do something very different */
131 if (must_be_container(dfd
)) {
133 st
= super_by_fd(dfd
, NULL
);
134 if (st
&& st
->ss
->load_container
)
135 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
138 if (!rv
&& st
->ss
->container_content
) {
140 pr_err("failed to get "
141 "exclusive lock on mapfile\n");
142 rv
= Incremental_container(st
, devname
, homehost
,
150 pr_err("%s is not part of an md array.\n",
155 /* 1/ Check if device is permitted by mdadm.conf */
157 if (!conf_test_dev(devname
)) {
159 pr_err("%s not permitted by mdadm.conf.\n",
164 /* 2/ Find metadata, reject if none appropriate (check
165 * version/name from args) */
167 if (fstat(dfd
, &stb
) < 0) {
169 pr_err("fstat failed for %s: %s.\n",
170 devname
, strerror(errno
));
173 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
175 pr_err("%s is not a block device.\n",
180 dinfo
.disk
.major
= major(stb
.st_rdev
);
181 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
183 policy
= disk_policy(&dinfo
);
184 have_target
= policy_check_path(&dinfo
, &target_array
);
186 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
188 pr_err("no recognisable superblock on %s.\n",
190 rv
= try_spare(devname
, &dfd
, policy
,
191 have_target
? &target_array
: NULL
,
195 if (st
->ss
->compare_super
== NULL
||
196 st
->ss
->load_super(st
, dfd
, NULL
)) {
198 pr_err("no RAID superblock on %s.\n",
200 rv
= try_spare(devname
, &dfd
, policy
,
201 have_target
? &target_array
: NULL
,
206 close (dfd
); dfd
= -1;
208 st
->ss
->getinfo_super(st
, &info
, NULL
);
210 /* 3/ Check if there is a match in mdadm.conf */
211 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
212 if (!match
&& rv
== 2)
215 if (match
&& match
->devname
216 && strcasecmp(match
->devname
, "<ignore>") == 0) {
218 pr_err("array containing %s is explicitly"
219 " ignored by mdadm.conf\n",
224 /* 3a/ if not, check for homehost match. If no match, continue
225 * but don't trust the 'name' in the array. Thus a 'random' minor
226 * number will be assigned, and the device name will be based
230 else if (st
->ss
->match_home(st
, homehost
) == 1)
232 else if (st
->ss
->match_home(st
, "any") == 1)
233 trustworthy
= LOCAL_ANY
;
235 trustworthy
= FOREIGN
;
238 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
239 (trustworthy
== LOCAL
))) {
241 pr_err("%s has metadata type %s for which "
242 "auto-assembly is disabled\n",
243 devname
, st
->ss
->name
);
246 if (trustworthy
== LOCAL_ANY
)
249 /* There are three possible sources for 'autof': command line,
250 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
251 * ARRAY takes precedence, then command line, then
254 if (match
&& match
->autof
)
255 autof
= match
->autof
;
259 name_to_use
= info
.name
;
260 if (name_to_use
[0] == 0 &&
261 info
.array
.level
== LEVEL_CONTAINER
) {
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 pr_err("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 pr_err("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 pr_err("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
);
324 pr_err("You have an old buggy kernel which cannot support\n"
325 " --incremental reliably. Aborting.\n");
329 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 */
342 struct supertype
*st2
;
343 struct mdinfo info2
, *d
;
345 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
346 GET_OFFSET
| GET_SIZE
));
349 strcpy(chosen_name
, mp
->path
);
351 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
353 /* It is generally not OK to add non-spare drives to a
354 * running array as they are probably missing because
355 * they failed. However if runstop is 1, then the
356 * array was possibly started early and our best bet is
357 * to add this anyway.
358 * Also if action policy is re-add or better we allow
360 * This doesn't apply to containers as the 'non-spare'
361 * flag has a different meaning. The test has to happen
362 * at the device level there
364 if (!st
->ss
->external
365 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
366 && ! policy_action_allows(policy
, st
->ss
->name
,
369 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
370 pr_err("not adding %s to active array (without --run) %s\n",
371 devname
, chosen_name
);
381 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
382 sra
->devs
->disk
.minor
);
383 dfd2
= dev_open(dn
, O_RDONLY
);
385 pr_err("unable to open %s\n", devname
);
390 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
391 st
->ss
->compare_super(st
, st2
) != 0) {
392 pr_err("metadata mismatch between %s and "
394 devname
, chosen_name
);
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
) {
405 pr_err("unexpected difference between %s and %s.\n",
406 chosen_name
, devname
);
411 info
.disk
.major
= major(stb
.st_rdev
);
412 info
.disk
.minor
= minor(stb
.st_rdev
);
413 /* add disk needs to know about containers */
414 if (st
->ss
->external
)
415 sra
->array
.level
= LEVEL_CONTAINER
;
416 err
= add_disk(mdfd
, st
, sra
, &info
);
417 if (err
< 0 && errno
== EBUSY
) {
418 /* could be another device present with the same
419 * disk.number. Find and reject any such
421 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
422 info
.events
, verbose
, chosen_name
);
423 err
= add_disk(mdfd
, st
, sra
, &info
);
426 pr_err("failed to add %s to %s: %s.\n",
427 devname
, chosen_name
, strerror(errno
));
431 info
.array
.working_disks
= 0;
432 for (d
= sra
->devs
; d
; d
=d
->next
)
433 info
.array
.working_disks
++;
437 /* 7/ Is there enough devices to possibly start the array? */
438 /* 7a/ if not, finish with success. */
439 if (info
.array
.level
== LEVEL_CONTAINER
) {
440 int devnum
= devnum
; /* defined and used iff ->external */
441 /* Try to assemble within the container */
442 sysfs_uevent(sra
, "change");
444 pr_err("container %s now has %d device%s\n",
445 chosen_name
, info
.array
.working_disks
,
446 info
.array
.working_disks
==1?"":"s");
447 wait_for(chosen_name
, mdfd
);
448 if (st
->ss
->external
)
449 devnum
= fd2devnum(mdfd
);
450 if (st
->ss
->load_container
)
451 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
455 rv
= Incremental_container(st
, chosen_name
, homehost
,
456 verbose
, runstop
, 0, autof
,
460 /* Don't fail the whole -I if a subarray didn't
461 * have enough devices to start yet
464 /* after spare is added, ping monitor for external metadata
465 * so that it can eg. try to rebuild degraded array */
466 if (st
->ss
->external
)
467 ping_monitor_by_id(devnum
);
471 /* We have added something to the array, so need to re-read the
472 * state. Eventually this state should be kept up-to-date as
476 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
477 GET_OFFSET
| GET_SIZE
));
478 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
479 if (enough(info
.array
.level
, info
.array
.raid_disks
,
480 info
.array
.layout
, info
.array
.state
& 1,
483 pr_err("%s attached to %s, not enough to start (%d).\n",
484 devname
, chosen_name
, active_disks
);
490 /* - if number of OK devices match expected, or -R and there */
492 /* + add any bitmap file */
493 /* + start the array (auto-readonly). */
495 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
497 pr_err("%s attached to %s which is already active.\n",
498 devname
, chosen_name
);
504 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
506 /* Let's try to start it */
507 if (match
&& match
->bitmap_file
) {
508 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
510 pr_err("Could not open bitmap file %s.\n",
514 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
516 pr_err("Failed to set bitmapfile for %s.\n",
522 /* Need to remove from the array any devices which
523 * 'count_active' discerned were too old or inappropriate
525 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
526 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
527 remove_disk(mdfd
, st
, sra
, d
);
529 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
530 && trustworthy
!= FOREIGN
)
531 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
533 rv
= sysfs_set_str(sra
, NULL
,
534 "array_state", "read-auto");
537 pr_err("%s attached to %s, which has been started.\n",
538 devname
, chosen_name
);
540 wait_for(chosen_name
, mdfd
);
541 /* We just started the array, so some devices
542 * might have been evicted from the array
543 * because their event counts were too old.
544 * If the action=re-add policy is in-force for
545 * those devices we should re-add them now.
547 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
548 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
549 add_disk(mdfd
, st
, sra
, dsk
) == 0)
550 pr_err("%s re-added to %s\n",
551 dsk
->sys_name
, chosen_name
);
554 pr_err("%s attached to %s, but failed to start: %s.\n",
555 devname
, chosen_name
, strerror(errno
));
560 pr_err("%s attached to %s, not enough to start safely.\n",
561 devname
, chosen_name
);
571 dev_policy_free(policy
);
580 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
581 int number
, __u64 events
, int verbose
,
584 /* Find a device attached to this array with a disk.number of number
585 * and events less than the passed events, and remove the device.
590 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
591 return; /* not safe to remove from active arrays
592 * without thinking more */
594 for (d
= sra
->devs
; d
; d
= d
->next
) {
598 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
599 dfd
= dev_open(dn
, O_RDONLY
);
602 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
606 st
->ss
->getinfo_super(st
, &info
, NULL
);
607 st
->ss
->free_super(st
);
610 if (info
.disk
.number
!= number
||
611 info
.events
>= events
)
614 if (d
->disk
.raid_disk
> -1)
615 sysfs_set_str(sra
, d
, "slot", "none");
616 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
618 pr_err("removing old device %s from %s\n",
619 d
->sys_name
+4, array_name
);
623 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
624 int mdfd
, char **availp
,
625 struct mdinfo
*bestinfo
)
627 /* count how many devices in sra think they are active */
630 __u64 max_events
= 0;
642 for (d
= sra
->devs
; d
; d
= d
->next
)
644 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
650 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
651 dfd
= dev_open(dn
, O_RDONLY
);
654 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
658 info
.array
.raid_disks
= raid_disks
;
659 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
661 raid_disks
= info
.array
.raid_disks
;
662 avail
= xcalloc(raid_disks
, 1);
665 best
= xcalloc(raid_disks
, sizeof(int));
666 devmap
= xcalloc(raid_disks
, numdevs
);
668 st
->ss
->getinfo_super(st
, &info
, devmap
);
671 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
675 max_events
= info
.events
;
676 avail
[info
.disk
.raid_disk
] = 2;
677 best
[info
.disk
.raid_disk
] = devnum
;
678 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
679 } else if (info
.events
== max_events
) {
680 avail
[info
.disk
.raid_disk
] = 2;
681 best
[info
.disk
.raid_disk
] = devnum
;
682 } else if (info
.events
== max_events
-1) {
683 if (avail
[info
.disk
.raid_disk
] == 0) {
684 avail
[info
.disk
.raid_disk
] = 1;
685 best
[info
.disk
.raid_disk
] = devnum
;
687 } else if (info
.events
< max_events
- 1)
689 else if (info
.events
== max_events
+1) {
691 max_events
= info
.events
;
692 for (i
=0; i
< raid_disks
; i
++)
695 avail
[info
.disk
.raid_disk
] = 2;
696 best
[info
.disk
.raid_disk
] = devnum
;
697 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
698 } else { /* info.events much bigger */
699 memset(avail
, 0, raid_disks
);
700 max_events
= info
.events
;
701 avail
[info
.disk
.raid_disk
] = 2;
702 best
[info
.disk
.raid_disk
] = devnum
;
703 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
706 st
->ss
->free_super(st
);
710 /* We need to reject any device that thinks the best device is
711 * failed or missing */
712 for (b
= 0; b
< raid_disks
; b
++)
716 for (i
= 0 ; i
< raid_disks
; i
++) {
717 if (i
!= b
&& avail
[i
])
718 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
719 /* This device thinks 'b' is failed -
722 for (d
=sra
->devs
; devnum
; d
= d
->next
)
724 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
735 /* test if container has degraded member(s) */
736 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
738 mdu_array_info_t array
;
740 int max_degraded
= 0;
742 for(; map
; map
= map
->next
) {
743 if (!is_subarray(map
->metadata
) ||
744 devname2devnum(map
->metadata
+1) != me
->devnum
)
746 afd
= open_dev(map
->devnum
);
749 /* most accurate information regarding array degradation */
750 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
751 int degraded
= array
.raid_disks
- array
.active_disks
-
753 if (degraded
> max_degraded
)
754 max_degraded
= degraded
;
758 return (max_degraded
);
761 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
762 struct map_ent
*target
, int bare
,
763 struct supertype
*st
, int verbose
)
765 /* This device doesn't have any md metadata
766 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
767 * If 'st' is not set, then we only know that some metadata allows this,
768 * others possibly don't.
769 * So look for a container or array to attach the device to.
770 * Prefer 'target' if that is set and the array is found.
772 * If st is set, then only arrays of that type are considered
773 * Return 0 on success, or some exit code on failure, probably 1.
777 struct map_ent
*mp
, *map
= NULL
;
778 struct mdinfo
*chosen
= NULL
;
781 if (fstat(dfd
, &stb
) != 0)
785 * Now we need to find a suitable array to add this to.
786 * We only accept arrays that:
788 * - are in the same domains as the device
789 * - are of an size for which the device will be useful
790 * and we choose the one that is the most degraded
793 if (map_lock(&map
)) {
794 pr_err("failed to get exclusive lock on "
798 for (mp
= map
; mp
; mp
= mp
->next
) {
799 struct supertype
*st2
;
800 struct domainlist
*dl
= NULL
;
802 unsigned long long devsize
;
803 unsigned long long component_size
= 0;
805 if (is_subarray(mp
->metadata
))
808 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
810 (st
->minor_version
>= 0 &&
811 st
->minor_version
!= st2
->minor_version
)) {
813 pr_err("not adding %s to %s as metadata type doesn't match\n",
820 sra
= sysfs_read(-1, mp
->devnum
,
821 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
822 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
824 /* Probably a container - no degraded info */
825 sra
= sysfs_read(-1, mp
->devnum
,
826 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
827 GET_COMPONENT
|GET_VERSION
);
829 sra
->array
.failed_disks
= -1;
836 for(i
=0; !st2
&& superlist
[i
]; i
++)
837 st2
= superlist
[i
]->match_metadata_desc(
841 pr_err("not adding %s to %s"
842 " as metadata not recognised.\n",
846 /* Need to double check the 'act_spare' permissions applies
849 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
851 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
852 act_spare_same_slot
))
856 /* update number of failed disks for mostly degraded
857 * container member */
858 if (sra
->array
.failed_disks
== -1)
859 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
861 get_dev_size(dfd
, NULL
, &devsize
);
862 if (sra
->component_size
== 0) {
863 /* true for containers, here we must read superblock
864 * to obtain minimum spare size */
865 struct supertype
*st3
= dup_super(st2
);
866 int mdfd
= open_dev(mp
->devnum
);
871 if (st3
->ss
->load_container
&&
872 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
873 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
874 st3
->ss
->free_super(st3
);
879 if ((sra
->component_size
> 0 &&
880 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
882 (sra
->component_size
== 0 && devsize
< component_size
)) {
884 pr_err("not adding %s to %s as it is too small\n",
888 /* test against target.
889 * If 'target' is set and 'bare' is false, we only accept
890 * arrays/containers that match 'target'.
891 * If 'target' is set and 'bare' is true, we prefer the
892 * array which matches 'target'.
893 * target is considered only if we deal with degraded array
895 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
896 act_spare_same_slot
)) {
897 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
898 memcmp(target
->uuid
, mp
->uuid
,
899 sizeof(target
->uuid
)) == 0 &&
900 sra
->array
.failed_disks
> 0) {
901 /* This is our target!! */
906 /* skip to end so we don't check any more */
916 dl
= domain_from_array(sra
, st2
->ss
->name
);
917 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
918 /* domain test fails */
920 pr_err("not adding %s to %s as"
921 " it is not in a compatible domain\n",
926 /* all tests passed, OK to add to this array */
930 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
944 /* add current device to chosen array as a spare */
945 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
947 struct mddev_dev devlist
;
951 devlist
.writemostly
= 0;
952 devlist
.devname
= devname
;
953 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
955 devlist
.disposition
= 'a';
958 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
964 pr_err("added %s as spare for %s\n",
965 devname
, chosen
->sys_name
);
967 pr_err("failed to add %s as spare for %s\n",
968 devname
, chosen
->sys_name
);
976 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
977 struct supertype
*st
, int verbose
)
979 /* we know that at least one partition virtual-metadata is
980 * allowed to incorporate spares like this device. We need to
981 * find a suitable device to copy partition information from.
983 * Getting a list of all disk (not partition) devices is
984 * slightly non-trivial. We could look at /sys/block, but
985 * that is theoretically due to be removed. Maybe best to use
986 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
987 * we depend on this directory of 'path' info. But that fails
988 * to find loop devices and probably others. Maybe don't
989 * worry about that, they aren't the real target.
991 * So: check things in /dev/disk/by-path to see if they are in
992 * a compatible domain, then load the partition table and see
993 * if it is OK for the new device, and choose the largest
994 * partition table that fits.
999 unsigned long long chosen_size
= 0;
1000 struct supertype
*chosen_st
= NULL
;
1003 dir
= opendir("/dev/disk/by-path");
1006 while ((de
= readdir(dir
)) != NULL
) {
1008 struct dev_policy
*pol2
= NULL
;
1009 struct domainlist
*domlist
= NULL
;
1012 struct supertype
*st2
= NULL
;
1013 char *devname
= NULL
;
1014 unsigned long long devsectors
;
1016 if (de
->d_ino
== 0 ||
1017 de
->d_name
[0] == '.' ||
1018 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1021 ep
= de
->d_name
+ strlen(de
->d_name
);
1022 while (ep
> de
->d_name
&&
1025 if (ep
> de
->d_name
+ 5 &&
1026 strncmp(ep
-5, "-part", 5) == 0)
1027 /* This is a partition - skip it */
1030 pol2
= path_policy(de
->d_name
, type_disk
);
1032 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1033 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1034 /* new device is incompatible with this device. */
1037 domain_free(domlist
);
1040 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1044 fd
= open(devname
, O_RDONLY
);
1047 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1052 st2
= dup_super(st
);
1054 st2
= guess_super_type(fd
, guess_partitions
);
1056 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1060 /* Check domain policy again, this time referring to metadata */
1061 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1062 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1063 /* Incompatible devices for this metadata type */
1065 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1066 /* Some partition types allow sparing, but not
1072 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1073 if (info
.component_size
> devsectors
)
1074 /* This partitioning doesn't fit in the device */
1077 /* This is an acceptable device to copy partition
1078 * metadata from. We could just stop here, but I
1079 * think I want to keep looking incase a larger
1080 * metadata which makes better use of the device can
1083 if (chosen
== NULL
||
1084 chosen_size
< info
.component_size
) {
1085 chosen_size
= info
.component_size
;
1090 chosen_st
->ss
->free_super(chosen_st
);
1099 domain_free(domlist
);
1100 dev_policy_free(pol2
);
1102 st2
->ss
->free_super(st2
);
1114 /* 'chosen' is the best device we can find. Let's write its
1115 * metadata to devname dfd is read-only so don't use that
1117 fd
= open(devname
, O_RDWR
);
1119 chosen_st
->ss
->store_super(chosen_st
, fd
);
1123 chosen_st
->ss
->free_super(chosen_st
);
1128 static int is_bare(int dfd
)
1130 unsigned long long size
= 0;
1131 char bufpad
[4096 + 4096];
1132 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1134 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1135 read(dfd
, buf
, 4096) != 4096)
1138 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1140 if (memcmp(buf
, buf
+1, 4095) != 0)
1143 /* OK, first 4K appear blank, try the end. */
1144 get_dev_size(dfd
, NULL
, &size
);
1145 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1146 read(dfd
, buf
, 4096) != 4096)
1149 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1151 if (memcmp(buf
, buf
+1, 4095) != 0)
1157 /* adding a spare to a regular array is quite different from adding one to
1158 * a set-of-partitions virtual array.
1159 * This function determines which is worth trying and tries as appropriate.
1160 * Arrays are given priority over partitions.
1162 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1163 struct map_ent
*target
,
1164 struct supertype
*st
, int verbose
)
1169 int partitions_ok
= 0;
1173 /* Can only add a spare if device has at least one domain */
1174 if (pol_find(pol
, pol_domain
) == NULL
)
1176 /* And only if some action allows spares */
1177 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1180 /* Now check if the device is bare.
1181 * bare devices can always be added as a spare
1182 * non-bare devices can only be added if spare-same-slot is permitted,
1183 * and this device is replacing a previous device - in which case 'target'
1186 if (!is_bare(dfd
)) {
1187 /* Must have a target and allow same_slot */
1188 /* Later - may allow force_spare without target */
1190 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1191 act_spare_same_slot
)) {
1193 pr_err("%s is not bare, so not "
1194 "considering as a spare\n",
1202 /* It might be OK to add this device to an array - need to see
1203 * what arrays might be candidates.
1206 /* just try try 'array' or 'partition' based on this metadata */
1207 if (st
->ss
->add_to_super
)
1208 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1211 return partition_try_spare(devname
, dfdp
, pol
,
1214 /* No metadata was specified or found so options are open.
1215 * Check for whether any array metadata, or any partition metadata
1216 * might allow adding the spare. This check is just help to avoid
1217 * a more costly scan of all arrays when we can be sure that will
1220 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1221 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1222 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1224 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1225 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1230 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1232 if (rv
!= 0 && partitions_ok
)
1233 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1237 int IncrementalScan(int verbose
)
1239 /* look at every device listed in the 'map' file.
1240 * If one is found that is not running then:
1241 * look in mdadm.conf for bitmap file.
1242 * if one exists, but array has none, add it.
1243 * try to start array in auto-readonly mode
1245 struct map_ent
*mapl
= NULL
;
1247 struct mddev_ident
*devs
, *mddev
;
1251 devs
= conf_get_ident(NULL
);
1253 for (me
= mapl
; me
; me
= me
->next
) {
1254 mdu_array_info_t array
;
1255 mdu_bitmap_file_t bmf
;
1257 int mdfd
= open_dev(me
->devnum
);
1261 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1266 /* Ok, we can try this one. Maybe it needs a bitmap */
1267 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1268 if (mddev
->devname
&& me
->path
1269 && devname_matches(mddev
->devname
, me
->path
))
1271 if (mddev
&& mddev
->bitmap_file
) {
1273 * Note: early kernels will wrongly fail this, so it
1277 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1278 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1280 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1287 pr_err("Added bitmap %s to %s\n",
1288 mddev
->bitmap_file
, me
->path
);
1289 else if (errno
!= EEXIST
)
1290 pr_err("Failed to add bitmap to %s: %s\n",
1291 me
->path
, strerror(errno
));
1294 sra
= sysfs_read(mdfd
, 0, 0);
1296 if (sysfs_set_str(sra
, NULL
,
1297 "array_state", "read-auto") == 0) {
1299 pr_err("started array %s\n",
1300 me
->path
?: devnum2devname(me
->devnum
));
1302 pr_err("failed to start array %s: %s\n",
1303 me
->path
?: devnum2devname(me
->devnum
),
1313 static char *container2devname(char *devname
)
1315 char *mdname
= NULL
;
1317 if (devname
[0] == '/') {
1318 int fd
= open(devname
, O_RDONLY
);
1320 mdname
= devnum2devname(fd2devnum(fd
));
1325 struct map_ent
*mp
, *map
= NULL
;
1327 if (!parse_uuid(devname
, uuid
))
1329 mp
= map_by_uuid(&map
, uuid
);
1331 mdname
= devnum2devname(mp
->devnum
);
1338 static int Incremental_container(struct supertype
*st
, char *devname
,
1339 char *homehost
, int verbose
,
1340 int runstop
, int readonly
,
1341 int autof
, int freeze_reshape
)
1343 /* Collect the contents of this container and for each
1344 * array, choose a device name and assemble the array.
1347 struct mdinfo
*list
;
1349 struct map_ent
*map
= NULL
;
1352 struct mddev_ident
*match
;
1354 struct domainlist
*domains
;
1355 struct map_ent
*smp
;
1361 st
->ss
->getinfo_super(st
, &info
, NULL
);
1363 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1364 info
.container_enough
> 0)
1368 pr_err("not enough devices to start the container\n");
1372 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
1373 if (match
== NULL
&& rv
== 2)
1376 /* Need to compute 'trustworthy' */
1378 trustworthy
= LOCAL
;
1379 else if (st
->ss
->match_home(st
, homehost
) == 1)
1380 trustworthy
= LOCAL
;
1381 else if (st
->ss
->match_home(st
, "any") == 1)
1382 trustworthy
= LOCAL
;
1384 trustworthy
= FOREIGN
;
1386 list
= st
->ss
->container_content(st
, NULL
);
1387 /* when nothing to activate - quit */
1390 for (ra
= list
; ra
; ra
= ra
->next
) {
1392 char chosen_name
[1024];
1394 struct mddev_ident
*match
= NULL
;
1397 /* do not activate arrays blocked by metadata handler */
1398 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1399 pr_err("Cannot activate array %s in %s.\n",
1400 ra
->text_version
, devname
);
1404 mp
= map_by_uuid(&map
, ra
->uuid
);
1407 mdfd
= open_dev(mp
->devnum
);
1409 strcpy(chosen_name
, mp
->path
);
1411 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1414 /* Check in mdadm.conf for container == devname and
1415 * member == ra->text_version after second slash.
1417 char *sub
= strchr(ra
->text_version
+1, '/');
1418 struct mddev_ident
*array_list
;
1421 array_list
= conf_get_ident(NULL
);
1424 for(; array_list
; array_list
= array_list
->next
) {
1426 if (array_list
->member
== NULL
||
1427 array_list
->container
== NULL
)
1429 if (strcmp(array_list
->member
, sub
) != 0)
1431 if (array_list
->uuid_set
&&
1432 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1434 dn
= container2devname(array_list
->container
);
1437 if (strncmp(dn
, ra
->text_version
+1,
1439 ra
->text_version
[strlen(dn
)+1] != '/') {
1444 /* we have a match */
1447 pr_err("match found for member %s\n",
1448 array_list
->member
);
1452 if (match
&& match
->devname
&&
1453 strcasecmp(match
->devname
, "<ignore>") == 0) {
1455 pr_err("array %s/%s is "
1456 "explicitly ignored by mdadm.conf\n",
1457 match
->container
, match
->member
);
1461 trustworthy
= LOCAL
;
1463 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1471 pr_err("failed to open %s: %s.\n",
1472 chosen_name
, strerror(errno
));
1476 assemble_container_content(st
, mdfd
, ra
, runstop
, readonly
,
1477 chosen_name
, verbose
, NULL
,
1482 /* don't move spares to container with volume being activated
1483 when all volumes are blocked */
1484 if (ra_all
== ra_blocked
)
1487 /* Now move all suitable spares from spare container */
1488 domains
= domain_from_array(list
, st
->ss
->name
);
1489 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1491 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1492 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1493 /* spare container found */
1494 struct supertype
*sst
=
1495 super_imsm
.match_metadata_desc("imsm");
1496 struct mdinfo
*sinfo
;
1497 unsigned long long min_size
= 0;
1498 if (st
->ss
->min_acceptable_spare_size
)
1499 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1500 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1502 sinfo
= container_choose_spares(sst
, min_size
,
1505 sst
->ss
->free_super(sst
);
1508 struct mdinfo
*disks
= sinfo
->devs
;
1510 /* move spare from spare
1511 * container to currently
1517 makedev(disks
->disk
.major
,
1518 disks
->disk
.minor
)))
1520 disks
= disks
->next
;
1523 pr_err("Added %d spare%s to %s\n",
1524 count
, count
>1?"s":"", devname
);
1530 domain_free(domains
);
1535 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1536 * raid arrays, and if so first fail (if needed) and then remove the device.
1538 * @devname - The device we want to remove
1539 * @id_path - name as found in /dev/disk/by-path for this device
1541 * Note: the device name must be a kernel name like "sda", so
1542 * that we can find it in /proc/mdstat
1544 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1548 struct mdstat_ent
*ent
;
1549 struct mddev_dev devlist
;
1552 dprintf(Name
": incremental removal without --path <id_path> "
1553 "lacks the possibility to re-add new device in this "
1556 if (strchr(devname
, '/')) {
1557 pr_err("incremental removal requires a "
1558 "kernel device name, not a file: %s\n", devname
);
1561 ent
= mdstat_by_component(devname
);
1563 pr_err("%s does not appear to be a component "
1564 "of any array\n", devname
);
1567 mdfd
= open_dev(ent
->devnum
);
1569 pr_err("Cannot open array %s!!\n", ent
->dev
);
1575 struct map_ent
*map
= NULL
, *me
;
1576 me
= map_by_devnum(&map
, ent
->devnum
);
1578 policy_save_path(id_path
, me
);
1582 memset(&devlist
, 0, sizeof(devlist
));
1583 devlist
.devname
= devname
;
1584 devlist
.disposition
= 'f';
1585 /* for a container, we must fail each member array */
1586 if (ent
->metadata_version
&&
1587 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1588 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1589 struct mdstat_ent
*memb
;
1590 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1591 if (is_container_member(memb
, ent
->dev
)) {
1592 int subfd
= open_dev(memb
->devnum
);
1594 Manage_subdevs(memb
->dev
, subfd
,
1595 &devlist
, verbose
, 0,
1600 free_mdstat(mdstat
);
1602 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1603 devlist
.disposition
= 'r';
1604 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);