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
,
48 int Incremental(char *devname
, struct context
*c
,
51 /* Add this device to an array, creating the array if necessary
52 * and starting the array if sensible or - if runstop>0 - if possible.
54 * This has several steps:
56 * 1/ Check if device is permitted by mdadm.conf, reject if not.
57 * 2/ Find metadata, reject if none appropriate (check
58 * version/name from args)
59 * 3/ Check if there is a match in mdadm.conf
60 * 3a/ if not, check for homehost match. If no match, assemble as
62 * 4/ Determine device number.
63 * - If in mdadm.conf with std name, use that
64 * - UUID in /var/run/mdadm.map use that
65 * - If name is suggestive, use that. unless in use with different uuid.
66 * - Choose a free, high number.
67 * - Use a partitioned device unless strong suggestion not to.
69 * Don't choose partitioned for containers.
70 * 5/ Find out if array already exists
72 * - choose a name, from mdadm.conf or 'name' field in array.
76 * - check one drive in array to make sure metadata is a reasonably
77 * close match. Reject if not (e.g. different type)
79 * 6/ Make sure /var/run/mdadm.map contains this array.
80 * 7/ Is there enough devices to possibly start the array?
81 * For a container, this means running Incremental_container.
82 * 7a/ if not, finish with success.
84 * - read all metadata and arrange devices like -A does
85 * - if number of OK devices match expected, or -R and there are enough,
86 * start the array (auto-readonly).
89 struct mdinfo info
, dinfo
;
90 struct mdinfo
*sra
= NULL
, *d
;
91 struct mddev_ident
*match
;
92 char chosen_name
[1024];
94 struct map_ent
*mp
, *map
= NULL
;
95 int dfd
= -1, mdfd
= -1;
100 mdu_array_info_t ainf
;
101 struct dev_policy
*policy
= NULL
;
102 struct map_ent target_array
;
105 struct createinfo
*ci
= conf_get_create_info();
107 if (stat(devname
, &stb
) < 0) {
109 pr_err("stat failed for %s: %s.\n",
110 devname
, strerror(errno
));
113 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
115 pr_err("%s is not a block device.\n",
119 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
122 pr_err("cannot open %s: %s.\n",
123 devname
, strerror(errno
));
126 /* If the device is a container, we do something very different */
127 if (must_be_container(dfd
)) {
129 st
= super_by_fd(dfd
, NULL
);
130 if (st
&& st
->ss
->load_container
)
131 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
134 if (!rv
&& st
->ss
->container_content
) {
136 pr_err("failed to get "
137 "exclusive lock on mapfile\n");
138 rv
= Incremental_container(st
, devname
, c
);
143 pr_err("%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
)) {
152 pr_err("%s not permitted by mdadm.conf.\n",
157 /* 2/ Find metadata, reject if none appropriate (check
158 * version/name from args) */
160 if (fstat(dfd
, &stb
) < 0) {
162 pr_err("fstat failed for %s: %s.\n",
163 devname
, strerror(errno
));
166 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
168 pr_err("%s is not a block device.\n",
173 dinfo
.disk
.major
= major(stb
.st_rdev
);
174 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
176 policy
= disk_policy(&dinfo
);
177 have_target
= policy_check_path(&dinfo
, &target_array
);
179 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
181 pr_err("no recognisable superblock on %s.\n",
183 rv
= try_spare(devname
, &dfd
, policy
,
184 have_target
? &target_array
: NULL
,
188 if (st
->ss
->compare_super
== NULL
||
189 st
->ss
->load_super(st
, dfd
, NULL
)) {
191 pr_err("no RAID superblock on %s.\n",
193 rv
= try_spare(devname
, &dfd
, policy
,
194 have_target
? &target_array
: NULL
,
199 close (dfd
); dfd
= -1;
201 st
->ss
->getinfo_super(st
, &info
, NULL
);
203 /* 3/ Check if there is a match in mdadm.conf */
204 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
205 if (!match
&& rv
== 2)
208 if (match
&& match
->devname
209 && strcasecmp(match
->devname
, "<ignore>") == 0) {
211 pr_err("array containing %s is explicitly"
212 " ignored by mdadm.conf\n",
217 /* 3a/ if not, check for homehost match. If no match, continue
218 * but don't trust the 'name' in the array. Thus a 'random' minor
219 * number will be assigned, and the device name will be based
223 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
225 else if (st
->ss
->match_home(st
, "any") == 1)
226 trustworthy
= LOCAL_ANY
;
228 trustworthy
= FOREIGN
;
231 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
232 (trustworthy
== LOCAL
))) {
234 pr_err("%s has metadata type %s for which "
235 "auto-assembly is disabled\n",
236 devname
, st
->ss
->name
);
239 if (trustworthy
== LOCAL_ANY
)
242 /* There are three possible sources for 'autof': command line,
243 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
244 * ARRAY takes precedence, then command line, then
247 if (match
&& match
->autof
)
248 c
->autof
= match
->autof
;
250 c
->autof
= ci
->autof
;
252 name_to_use
= info
.name
;
253 if (name_to_use
[0] == 0 &&
254 info
.array
.level
== LEVEL_CONTAINER
) {
255 name_to_use
= info
.text_version
;
256 trustworthy
= METADATA
;
258 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
259 ! c
->require_homehost
&&
260 conf_name_is_free(name_to_use
))
263 /* strip "hostname:" prefix from name if we have decided
264 * to treat it as LOCAL
266 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
267 name_to_use
= strchr(name_to_use
, ':')+1;
269 /* 4/ Check if array exists.
272 pr_err("failed to get exclusive lock on "
274 mp
= map_by_uuid(&map
, info
.uuid
);
276 mdfd
= open_dev(mp
->devnum
);
282 /* Couldn't find an existing array, maybe make a new one */
283 mdfd
= create_mddev(match
? match
->devname
: NULL
,
284 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
289 sysfs_init(&info
, mdfd
, 0);
291 if (set_array_info(mdfd
, st
, &info
) != 0) {
292 pr_err("failed to set array info for %s: %s\n",
293 chosen_name
, strerror(errno
));
299 dinfo
.disk
.major
= major(stb
.st_rdev
);
300 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
301 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
302 pr_err("failed to add %s to %s: %s.\n",
303 devname
, chosen_name
, strerror(errno
));
304 ioctl(mdfd
, STOP_ARRAY
, 0);
308 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
309 GET_OFFSET
| GET_SIZE
));
311 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
312 /* It really should be 'none' - must be old buggy
313 * kernel, and mdadm -I may not be able to complete.
316 ioctl(mdfd
, STOP_ARRAY
, NULL
);
317 pr_err("You have an old buggy kernel which cannot support\n"
318 " --incremental reliably. Aborting.\n");
322 info
.array
.working_disks
= 1;
323 /* 6/ Make sure /var/run/mdadm.map contains this array. */
324 map_update(&map
, fd2devnum(mdfd
),
326 info
.uuid
, chosen_name
);
329 /* - check one drive in array to make sure metadata is a reasonably */
330 /* close match. Reject if not (e.g. different type) */
331 /* - add the device */
335 struct supertype
*st2
;
336 struct mdinfo info2
, *d
;
338 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
339 GET_OFFSET
| GET_SIZE
));
342 strcpy(chosen_name
, mp
->path
);
344 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
346 /* It is generally not OK to add non-spare drives to a
347 * running array as they are probably missing because
348 * they failed. However if runstop is 1, then the
349 * array was possibly started early and our best bet is
350 * to add this anyway.
351 * Also if action policy is re-add or better we allow
353 * This doesn't apply to containers as the 'non-spare'
354 * flag has a different meaning. The test has to happen
355 * at the device level there
357 if (!st
->ss
->external
358 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
359 && ! policy_action_allows(policy
, st
->ss
->name
,
362 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
363 pr_err("not adding %s to active array (without --run) %s\n",
364 devname
, chosen_name
);
374 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
375 sra
->devs
->disk
.minor
);
376 dfd2
= dev_open(dn
, O_RDONLY
);
378 pr_err("unable to open %s\n", devname
);
383 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
384 st
->ss
->compare_super(st
, st2
) != 0) {
385 pr_err("metadata mismatch between %s and "
387 devname
, chosen_name
);
393 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
394 st2
->ss
->free_super(st2
);
395 if (info
.array
.level
!= info2
.array
.level
||
396 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
397 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
398 pr_err("unexpected difference between %s and %s.\n",
399 chosen_name
, devname
);
404 info
.disk
.major
= major(stb
.st_rdev
);
405 info
.disk
.minor
= minor(stb
.st_rdev
);
406 /* add disk needs to know about containers */
407 if (st
->ss
->external
)
408 sra
->array
.level
= LEVEL_CONTAINER
;
409 err
= add_disk(mdfd
, st
, sra
, &info
);
410 if (err
< 0 && errno
== EBUSY
) {
411 /* could be another device present with the same
412 * disk.number. Find and reject any such
414 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
415 info
.events
, c
->verbose
, chosen_name
);
416 err
= add_disk(mdfd
, st
, sra
, &info
);
419 pr_err("failed to add %s to %s: %s.\n",
420 devname
, chosen_name
, strerror(errno
));
424 info
.array
.working_disks
= 0;
425 for (d
= sra
->devs
; d
; d
=d
->next
)
426 info
.array
.working_disks
++;
430 /* 7/ Is there enough devices to possibly start the array? */
431 /* 7a/ if not, finish with success. */
432 if (info
.array
.level
== LEVEL_CONTAINER
) {
433 int devnum
= devnum
; /* defined and used iff ->external */
434 /* Try to assemble within the container */
435 sysfs_uevent(sra
, "change");
437 pr_err("container %s now has %d device%s\n",
438 chosen_name
, info
.array
.working_disks
,
439 info
.array
.working_disks
==1?"":"s");
440 wait_for(chosen_name
, mdfd
);
441 if (st
->ss
->external
)
442 devnum
= fd2devnum(mdfd
);
443 if (st
->ss
->load_container
)
444 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
448 rv
= Incremental_container(st
, chosen_name
, c
);
451 /* Don't fail the whole -I if a subarray didn't
452 * have enough devices to start yet
455 /* after spare is added, ping monitor for external metadata
456 * so that it can eg. try to rebuild degraded array */
457 if (st
->ss
->external
)
458 ping_monitor_by_id(devnum
);
462 /* We have added something to the array, so need to re-read the
463 * state. Eventually this state should be kept up-to-date as
467 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
468 GET_OFFSET
| GET_SIZE
));
469 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
470 if (enough(info
.array
.level
, info
.array
.raid_disks
,
471 info
.array
.layout
, info
.array
.state
& 1,
474 pr_err("%s attached to %s, not enough to start (%d).\n",
475 devname
, chosen_name
, active_disks
);
481 /* - if number of OK devices match expected, or -R and there */
483 /* + add any bitmap file */
484 /* + start the array (auto-readonly). */
486 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
488 pr_err("%s attached to %s which is already active.\n",
489 devname
, chosen_name
);
495 if (c
->runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
497 /* Let's try to start it */
498 if (match
&& match
->bitmap_file
) {
499 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
501 pr_err("Could not open bitmap file %s.\n",
505 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
507 pr_err("Failed to set bitmapfile for %s.\n",
513 /* Need to remove from the array any devices which
514 * 'count_active' discerned were too old or inappropriate
516 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
517 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
518 remove_disk(mdfd
, st
, sra
, d
);
520 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
521 && trustworthy
!= FOREIGN
)
522 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
524 rv
= sysfs_set_str(sra
, NULL
,
525 "array_state", "read-auto");
528 pr_err("%s attached to %s, which has been started.\n",
529 devname
, chosen_name
);
531 wait_for(chosen_name
, mdfd
);
532 /* We just started the array, so some devices
533 * might have been evicted from the array
534 * because their event counts were too old.
535 * If the action=re-add policy is in-force for
536 * those devices we should re-add them now.
538 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
539 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
540 add_disk(mdfd
, st
, sra
, dsk
) == 0)
541 pr_err("%s re-added to %s\n",
542 dsk
->sys_name
, chosen_name
);
545 pr_err("%s attached to %s, but failed to start: %s.\n",
546 devname
, chosen_name
, strerror(errno
));
551 pr_err("%s attached to %s, not enough to start safely.\n",
552 devname
, chosen_name
);
562 dev_policy_free(policy
);
571 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
572 int number
, __u64 events
, int verbose
,
575 /* Find a device attached to this array with a disk.number of number
576 * and events less than the passed events, and remove the device.
581 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
582 return; /* not safe to remove from active arrays
583 * without thinking more */
585 for (d
= sra
->devs
; d
; d
= d
->next
) {
589 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
590 dfd
= dev_open(dn
, O_RDONLY
);
593 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
597 st
->ss
->getinfo_super(st
, &info
, NULL
);
598 st
->ss
->free_super(st
);
601 if (info
.disk
.number
!= number
||
602 info
.events
>= events
)
605 if (d
->disk
.raid_disk
> -1)
606 sysfs_set_str(sra
, d
, "slot", "none");
607 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
609 pr_err("removing old device %s from %s\n",
610 d
->sys_name
+4, array_name
);
614 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
615 int mdfd
, char **availp
,
616 struct mdinfo
*bestinfo
)
618 /* count how many devices in sra think they are active */
621 __u64 max_events
= 0;
633 for (d
= sra
->devs
; d
; d
= d
->next
)
635 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
641 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
642 dfd
= dev_open(dn
, O_RDONLY
);
645 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
649 info
.array
.raid_disks
= raid_disks
;
650 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
652 raid_disks
= info
.array
.raid_disks
;
653 avail
= xcalloc(raid_disks
, 1);
656 best
= xcalloc(raid_disks
, sizeof(int));
657 devmap
= xcalloc(raid_disks
, numdevs
);
659 st
->ss
->getinfo_super(st
, &info
, devmap
);
662 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
666 max_events
= info
.events
;
667 avail
[info
.disk
.raid_disk
] = 2;
668 best
[info
.disk
.raid_disk
] = devnum
;
669 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
670 } else if (info
.events
== max_events
) {
671 avail
[info
.disk
.raid_disk
] = 2;
672 best
[info
.disk
.raid_disk
] = devnum
;
673 } else if (info
.events
== max_events
-1) {
674 if (avail
[info
.disk
.raid_disk
] == 0) {
675 avail
[info
.disk
.raid_disk
] = 1;
676 best
[info
.disk
.raid_disk
] = devnum
;
678 } else if (info
.events
< max_events
- 1)
680 else if (info
.events
== max_events
+1) {
682 max_events
= info
.events
;
683 for (i
=0; i
< raid_disks
; i
++)
686 avail
[info
.disk
.raid_disk
] = 2;
687 best
[info
.disk
.raid_disk
] = devnum
;
688 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
689 } else { /* info.events much bigger */
690 memset(avail
, 0, raid_disks
);
691 max_events
= info
.events
;
692 avail
[info
.disk
.raid_disk
] = 2;
693 best
[info
.disk
.raid_disk
] = devnum
;
694 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
697 st
->ss
->free_super(st
);
701 /* We need to reject any device that thinks the best device is
702 * failed or missing */
703 for (b
= 0; b
< raid_disks
; b
++)
707 for (i
= 0 ; i
< raid_disks
; i
++) {
708 if (i
!= b
&& avail
[i
])
709 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
710 /* This device thinks 'b' is failed -
713 for (d
=sra
->devs
; devnum
; d
= d
->next
)
715 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
726 /* test if container has degraded member(s) */
727 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
729 mdu_array_info_t array
;
731 int max_degraded
= 0;
733 for(; map
; map
= map
->next
) {
734 if (!is_subarray(map
->metadata
) ||
735 devname2devnum(map
->metadata
+1) != me
->devnum
)
737 afd
= open_dev(map
->devnum
);
740 /* most accurate information regarding array degradation */
741 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
742 int degraded
= array
.raid_disks
- array
.active_disks
-
744 if (degraded
> max_degraded
)
745 max_degraded
= degraded
;
749 return (max_degraded
);
752 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
753 struct map_ent
*target
, int bare
,
754 struct supertype
*st
, int verbose
)
756 /* This device doesn't have any md metadata
757 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
758 * If 'st' is not set, then we only know that some metadata allows this,
759 * others possibly don't.
760 * So look for a container or array to attach the device to.
761 * Prefer 'target' if that is set and the array is found.
763 * If st is set, then only arrays of that type are considered
764 * Return 0 on success, or some exit code on failure, probably 1.
768 struct map_ent
*mp
, *map
= NULL
;
769 struct mdinfo
*chosen
= NULL
;
772 if (fstat(dfd
, &stb
) != 0)
776 * Now we need to find a suitable array to add this to.
777 * We only accept arrays that:
779 * - are in the same domains as the device
780 * - are of an size for which the device will be useful
781 * and we choose the one that is the most degraded
784 if (map_lock(&map
)) {
785 pr_err("failed to get exclusive lock on "
789 for (mp
= map
; mp
; mp
= mp
->next
) {
790 struct supertype
*st2
;
791 struct domainlist
*dl
= NULL
;
793 unsigned long long devsize
;
794 unsigned long long component_size
= 0;
796 if (is_subarray(mp
->metadata
))
799 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
801 (st
->minor_version
>= 0 &&
802 st
->minor_version
!= st2
->minor_version
)) {
804 pr_err("not adding %s to %s as metadata type doesn't match\n",
811 sra
= sysfs_read(-1, mp
->devnum
,
812 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
813 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
815 /* Probably a container - no degraded info */
816 sra
= sysfs_read(-1, mp
->devnum
,
817 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
818 GET_COMPONENT
|GET_VERSION
);
820 sra
->array
.failed_disks
= -1;
827 for(i
=0; !st2
&& superlist
[i
]; i
++)
828 st2
= superlist
[i
]->match_metadata_desc(
832 pr_err("not adding %s to %s"
833 " as metadata not recognised.\n",
837 /* Need to double check the 'act_spare' permissions applies
840 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
842 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
843 act_spare_same_slot
))
847 /* update number of failed disks for mostly degraded
848 * container member */
849 if (sra
->array
.failed_disks
== -1)
850 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
852 get_dev_size(dfd
, NULL
, &devsize
);
853 if (sra
->component_size
== 0) {
854 /* true for containers, here we must read superblock
855 * to obtain minimum spare size */
856 struct supertype
*st3
= dup_super(st2
);
857 int mdfd
= open_dev(mp
->devnum
);
862 if (st3
->ss
->load_container
&&
863 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
864 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
865 st3
->ss
->free_super(st3
);
870 if ((sra
->component_size
> 0 &&
871 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
873 (sra
->component_size
== 0 && devsize
< component_size
)) {
875 pr_err("not adding %s to %s as it is too small\n",
879 /* test against target.
880 * If 'target' is set and 'bare' is false, we only accept
881 * arrays/containers that match 'target'.
882 * If 'target' is set and 'bare' is true, we prefer the
883 * array which matches 'target'.
884 * target is considered only if we deal with degraded array
886 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
887 act_spare_same_slot
)) {
888 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
889 memcmp(target
->uuid
, mp
->uuid
,
890 sizeof(target
->uuid
)) == 0 &&
891 sra
->array
.failed_disks
> 0) {
892 /* This is our target!! */
897 /* skip to end so we don't check any more */
907 dl
= domain_from_array(sra
, st2
->ss
->name
);
908 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
909 /* domain test fails */
911 pr_err("not adding %s to %s as"
912 " it is not in a compatible domain\n",
917 /* all tests passed, OK to add to this array */
921 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
935 /* add current device to chosen array as a spare */
936 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
938 struct mddev_dev devlist
;
942 devlist
.writemostly
= 0;
943 devlist
.devname
= devname
;
944 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
946 devlist
.disposition
= 'a';
949 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
955 pr_err("added %s as spare for %s\n",
956 devname
, chosen
->sys_name
);
958 pr_err("failed to add %s as spare for %s\n",
959 devname
, chosen
->sys_name
);
967 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
968 struct supertype
*st
, int verbose
)
970 /* we know that at least one partition virtual-metadata is
971 * allowed to incorporate spares like this device. We need to
972 * find a suitable device to copy partition information from.
974 * Getting a list of all disk (not partition) devices is
975 * slightly non-trivial. We could look at /sys/block, but
976 * that is theoretically due to be removed. Maybe best to use
977 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
978 * we depend on this directory of 'path' info. But that fails
979 * to find loop devices and probably others. Maybe don't
980 * worry about that, they aren't the real target.
982 * So: check things in /dev/disk/by-path to see if they are in
983 * a compatible domain, then load the partition table and see
984 * if it is OK for the new device, and choose the largest
985 * partition table that fits.
990 unsigned long long chosen_size
= 0;
991 struct supertype
*chosen_st
= NULL
;
994 dir
= opendir("/dev/disk/by-path");
997 while ((de
= readdir(dir
)) != NULL
) {
999 struct dev_policy
*pol2
= NULL
;
1000 struct domainlist
*domlist
= NULL
;
1003 struct supertype
*st2
= NULL
;
1004 char *devname
= NULL
;
1005 unsigned long long devsectors
;
1007 if (de
->d_ino
== 0 ||
1008 de
->d_name
[0] == '.' ||
1009 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1012 ep
= de
->d_name
+ strlen(de
->d_name
);
1013 while (ep
> de
->d_name
&&
1016 if (ep
> de
->d_name
+ 5 &&
1017 strncmp(ep
-5, "-part", 5) == 0)
1018 /* This is a partition - skip it */
1021 pol2
= path_policy(de
->d_name
, type_disk
);
1023 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1024 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1025 /* new device is incompatible with this device. */
1028 domain_free(domlist
);
1031 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1035 fd
= open(devname
, O_RDONLY
);
1038 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1043 st2
= dup_super(st
);
1045 st2
= guess_super_type(fd
, guess_partitions
);
1047 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1051 /* Check domain policy again, this time referring to metadata */
1052 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1053 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1054 /* Incompatible devices for this metadata type */
1056 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1057 /* Some partition types allow sparing, but not
1063 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1064 if (info
.component_size
> devsectors
)
1065 /* This partitioning doesn't fit in the device */
1068 /* This is an acceptable device to copy partition
1069 * metadata from. We could just stop here, but I
1070 * think I want to keep looking incase a larger
1071 * metadata which makes better use of the device can
1074 if (chosen
== NULL
||
1075 chosen_size
< info
.component_size
) {
1076 chosen_size
= info
.component_size
;
1081 chosen_st
->ss
->free_super(chosen_st
);
1090 domain_free(domlist
);
1091 dev_policy_free(pol2
);
1093 st2
->ss
->free_super(st2
);
1105 /* 'chosen' is the best device we can find. Let's write its
1106 * metadata to devname dfd is read-only so don't use that
1108 fd
= open(devname
, O_RDWR
);
1110 chosen_st
->ss
->store_super(chosen_st
, fd
);
1114 chosen_st
->ss
->free_super(chosen_st
);
1119 static int is_bare(int dfd
)
1121 unsigned long long size
= 0;
1122 char bufpad
[4096 + 4096];
1123 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1125 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1126 read(dfd
, buf
, 4096) != 4096)
1129 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1131 if (memcmp(buf
, buf
+1, 4095) != 0)
1134 /* OK, first 4K appear blank, try the end. */
1135 get_dev_size(dfd
, NULL
, &size
);
1136 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1137 read(dfd
, buf
, 4096) != 4096)
1140 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1142 if (memcmp(buf
, buf
+1, 4095) != 0)
1148 /* adding a spare to a regular array is quite different from adding one to
1149 * a set-of-partitions virtual array.
1150 * This function determines which is worth trying and tries as appropriate.
1151 * Arrays are given priority over partitions.
1153 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1154 struct map_ent
*target
,
1155 struct supertype
*st
, int verbose
)
1160 int partitions_ok
= 0;
1164 /* Can only add a spare if device has at least one domain */
1165 if (pol_find(pol
, pol_domain
) == NULL
)
1167 /* And only if some action allows spares */
1168 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1171 /* Now check if the device is bare.
1172 * bare devices can always be added as a spare
1173 * non-bare devices can only be added if spare-same-slot is permitted,
1174 * and this device is replacing a previous device - in which case 'target'
1177 if (!is_bare(dfd
)) {
1178 /* Must have a target and allow same_slot */
1179 /* Later - may allow force_spare without target */
1181 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1182 act_spare_same_slot
)) {
1184 pr_err("%s is not bare, so not "
1185 "considering as a spare\n",
1193 /* It might be OK to add this device to an array - need to see
1194 * what arrays might be candidates.
1197 /* just try try 'array' or 'partition' based on this metadata */
1198 if (st
->ss
->add_to_super
)
1199 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1202 return partition_try_spare(devname
, dfdp
, pol
,
1205 /* No metadata was specified or found so options are open.
1206 * Check for whether any array metadata, or any partition metadata
1207 * might allow adding the spare. This check is just help to avoid
1208 * a more costly scan of all arrays when we can be sure that will
1211 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1212 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1213 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1215 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1216 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1221 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1223 if (rv
!= 0 && partitions_ok
)
1224 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1228 int IncrementalScan(int verbose
)
1230 /* look at every device listed in the 'map' file.
1231 * If one is found that is not running then:
1232 * look in mdadm.conf for bitmap file.
1233 * if one exists, but array has none, add it.
1234 * try to start array in auto-readonly mode
1236 struct map_ent
*mapl
= NULL
;
1238 struct mddev_ident
*devs
, *mddev
;
1242 devs
= conf_get_ident(NULL
);
1244 for (me
= mapl
; me
; me
= me
->next
) {
1245 mdu_array_info_t array
;
1246 mdu_bitmap_file_t bmf
;
1248 int mdfd
= open_dev(me
->devnum
);
1252 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1257 /* Ok, we can try this one. Maybe it needs a bitmap */
1258 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1259 if (mddev
->devname
&& me
->path
1260 && devname_matches(mddev
->devname
, me
->path
))
1262 if (mddev
&& mddev
->bitmap_file
) {
1264 * Note: early kernels will wrongly fail this, so it
1268 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1269 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1271 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1278 pr_err("Added bitmap %s to %s\n",
1279 mddev
->bitmap_file
, me
->path
);
1280 else if (errno
!= EEXIST
)
1281 pr_err("Failed to add bitmap to %s: %s\n",
1282 me
->path
, strerror(errno
));
1285 sra
= sysfs_read(mdfd
, 0, 0);
1287 if (sysfs_set_str(sra
, NULL
,
1288 "array_state", "read-auto") == 0) {
1290 pr_err("started array %s\n",
1291 me
->path
?: devnum2devname(me
->devnum
));
1293 pr_err("failed to start array %s: %s\n",
1294 me
->path
?: devnum2devname(me
->devnum
),
1304 static char *container2devname(char *devname
)
1306 char *mdname
= NULL
;
1308 if (devname
[0] == '/') {
1309 int fd
= open(devname
, O_RDONLY
);
1311 mdname
= devnum2devname(fd2devnum(fd
));
1316 struct map_ent
*mp
, *map
= NULL
;
1318 if (!parse_uuid(devname
, uuid
))
1320 mp
= map_by_uuid(&map
, uuid
);
1322 mdname
= devnum2devname(mp
->devnum
);
1329 static int Incremental_container(struct supertype
*st
, char *devname
,
1332 /* Collect the contents of this container and for each
1333 * array, choose a device name and assemble the array.
1336 struct mdinfo
*list
;
1338 struct map_ent
*map
= NULL
;
1341 struct mddev_ident
*match
;
1343 struct domainlist
*domains
;
1344 struct map_ent
*smp
;
1350 st
->ss
->getinfo_super(st
, &info
, NULL
);
1352 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1353 info
.container_enough
> 0)
1357 pr_err("not enough devices to start the container\n");
1361 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1362 if (match
== NULL
&& rv
== 2)
1365 /* Need to compute 'trustworthy' */
1367 trustworthy
= LOCAL
;
1368 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1369 trustworthy
= LOCAL
;
1370 else if (st
->ss
->match_home(st
, "any") == 1)
1371 trustworthy
= LOCAL
;
1373 trustworthy
= FOREIGN
;
1375 list
= st
->ss
->container_content(st
, NULL
);
1376 /* when nothing to activate - quit */
1379 for (ra
= list
; ra
; ra
= ra
->next
) {
1381 char chosen_name
[1024];
1383 struct mddev_ident
*match
= NULL
;
1386 /* do not activate arrays blocked by metadata handler */
1387 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1388 pr_err("Cannot activate array %s in %s.\n",
1389 ra
->text_version
, devname
);
1393 mp
= map_by_uuid(&map
, ra
->uuid
);
1396 mdfd
= open_dev(mp
->devnum
);
1398 strcpy(chosen_name
, mp
->path
);
1400 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1403 /* Check in mdadm.conf for container == devname and
1404 * member == ra->text_version after second slash.
1406 char *sub
= strchr(ra
->text_version
+1, '/');
1407 struct mddev_ident
*array_list
;
1410 array_list
= conf_get_ident(NULL
);
1413 for(; array_list
; array_list
= array_list
->next
) {
1415 if (array_list
->member
== NULL
||
1416 array_list
->container
== NULL
)
1418 if (strcmp(array_list
->member
, sub
) != 0)
1420 if (array_list
->uuid_set
&&
1421 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1423 dn
= container2devname(array_list
->container
);
1426 if (strncmp(dn
, ra
->text_version
+1,
1428 ra
->text_version
[strlen(dn
)+1] != '/') {
1433 /* we have a match */
1436 pr_err("match found for member %s\n",
1437 array_list
->member
);
1441 if (match
&& match
->devname
&&
1442 strcasecmp(match
->devname
, "<ignore>") == 0) {
1444 pr_err("array %s/%s is "
1445 "explicitly ignored by mdadm.conf\n",
1446 match
->container
, match
->member
);
1450 trustworthy
= LOCAL
;
1452 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1460 pr_err("failed to open %s: %s.\n",
1461 chosen_name
, strerror(errno
));
1465 assemble_container_content(st
, mdfd
, ra
, c
,
1470 /* don't move spares to container with volume being activated
1471 when all volumes are blocked */
1472 if (ra_all
== ra_blocked
)
1475 /* Now move all suitable spares from spare container */
1476 domains
= domain_from_array(list
, st
->ss
->name
);
1477 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1479 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1480 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1481 /* spare container found */
1482 struct supertype
*sst
=
1483 super_imsm
.match_metadata_desc("imsm");
1484 struct mdinfo
*sinfo
;
1485 unsigned long long min_size
= 0;
1486 if (st
->ss
->min_acceptable_spare_size
)
1487 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1488 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1490 sinfo
= container_choose_spares(sst
, min_size
,
1493 sst
->ss
->free_super(sst
);
1496 struct mdinfo
*disks
= sinfo
->devs
;
1498 /* move spare from spare
1499 * container to currently
1505 makedev(disks
->disk
.major
,
1506 disks
->disk
.minor
)))
1508 disks
= disks
->next
;
1511 pr_err("Added %d spare%s to %s\n",
1512 count
, count
>1?"s":"", devname
);
1518 domain_free(domains
);
1523 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1524 * raid arrays, and if so first fail (if needed) and then remove the device.
1526 * @devname - The device we want to remove
1527 * @id_path - name as found in /dev/disk/by-path for this device
1529 * Note: the device name must be a kernel name like "sda", so
1530 * that we can find it in /proc/mdstat
1532 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1536 struct mdstat_ent
*ent
;
1537 struct mddev_dev devlist
;
1540 dprintf(Name
": incremental removal without --path <id_path> "
1541 "lacks the possibility to re-add new device in this "
1544 if (strchr(devname
, '/')) {
1545 pr_err("incremental removal requires a "
1546 "kernel device name, not a file: %s\n", devname
);
1549 ent
= mdstat_by_component(devname
);
1551 pr_err("%s does not appear to be a component "
1552 "of any array\n", devname
);
1555 mdfd
= open_dev(ent
->devnum
);
1557 pr_err("Cannot open array %s!!\n", ent
->dev
);
1563 struct map_ent
*map
= NULL
, *me
;
1564 me
= map_by_devnum(&map
, ent
->devnum
);
1566 policy_save_path(id_path
, me
);
1570 memset(&devlist
, 0, sizeof(devlist
));
1571 devlist
.devname
= devname
;
1572 devlist
.disposition
= 'f';
1573 /* for a container, we must fail each member array */
1574 if (ent
->metadata_version
&&
1575 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1576 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1577 struct mdstat_ent
*memb
;
1578 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1579 if (is_container_member(memb
, ent
->dev
)) {
1580 int subfd
= open_dev(memb
->devnum
);
1582 Manage_subdevs(memb
->dev
, subfd
,
1583 &devlist
, verbose
, 0,
1588 free_mdstat(mdstat
);
1590 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1591 devlist
.disposition
= 'r';
1592 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);