2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2012 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
36 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
37 int mdfd
, char **availp
,
39 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
40 int number
, __u64 events
, int verbose
,
42 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
43 struct map_ent
*target
,
44 struct supertype
*st
, int verbose
);
46 static int Incremental_container(struct supertype
*st
, char *devname
,
49 int Incremental(char *devname
, struct context
*c
,
52 /* Add this device to an array, creating the array if necessary
53 * and starting the array if sensible or - if runstop>0 - if possible.
55 * This has several steps:
57 * 1/ Check if device is permitted by mdadm.conf, reject if not.
58 * 2/ Find metadata, reject if none appropriate (check
59 * version/name from args)
60 * 3/ Check if there is a match in mdadm.conf
61 * 3a/ if not, check for homehost match. If no match, assemble as
63 * 4/ Determine device number.
64 * - If in mdadm.conf with std name, use that
65 * - UUID in /var/run/mdadm.map use that
66 * - If name is suggestive, use that. unless in use with different uuid.
67 * - Choose a free, high number.
68 * - Use a partitioned device unless strong suggestion not to.
70 * Don't choose partitioned for containers.
71 * 5/ Find out if array already exists
73 * - choose a name, from mdadm.conf or 'name' field in array.
77 * - check one drive in array to make sure metadata is a reasonably
78 * close match. Reject if not (e.g. different type)
80 * 6/ Make sure /var/run/mdadm.map contains this array.
81 * 7/ Is there enough devices to possibly start the array?
82 * For a container, this means running Incremental_container.
83 * 7a/ if not, finish with success.
85 * - read all metadata and arrange devices like -A does
86 * - if number of OK devices match expected, or -R and there are enough,
87 * start the array (auto-readonly).
90 struct mdinfo info
, dinfo
;
91 struct mdinfo
*sra
= NULL
, *d
;
92 struct mddev_ident
*match
;
93 char chosen_name
[1024];
95 struct map_ent
*mp
, *map
= NULL
;
96 int dfd
= -1, mdfd
= -1;
101 mdu_array_info_t ainf
;
102 struct dev_policy
*policy
= NULL
;
103 struct map_ent target_array
;
106 struct createinfo
*ci
= conf_get_create_info();
108 if (stat(devname
, &stb
) < 0) {
110 pr_err("stat failed for %s: %s.\n",
111 devname
, strerror(errno
));
114 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
116 pr_err("%s is not a block device.\n",
120 dfd
= dev_open(devname
, O_RDONLY
);
123 pr_err("cannot open %s: %s.\n",
124 devname
, strerror(errno
));
127 /* If the device is a container, we do something very different */
128 if (must_be_container(dfd
)) {
130 st
= super_by_fd(dfd
, NULL
);
132 st
->ignore_hw_compat
= 1;
133 if (st
&& st
->ss
->load_container
)
134 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
137 if (!rv
&& st
->ss
->container_content
) {
139 pr_err("failed to get "
140 "exclusive lock on mapfile\n");
141 rv
= Incremental_container(st
, devname
, c
);
146 pr_err("%s is not part of an md array.\n",
151 /* 1/ Check if device is permitted by mdadm.conf */
153 if (!conf_test_dev(devname
)) {
155 pr_err("%s not permitted by mdadm.conf.\n",
160 /* 2/ Find metadata, reject if none appropriate (check
161 * version/name from args) */
163 if (fstat(dfd
, &stb
) < 0) {
165 pr_err("fstat failed for %s: %s.\n",
166 devname
, strerror(errno
));
169 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
171 pr_err("%s is not a block device.\n",
176 dinfo
.disk
.major
= major(stb
.st_rdev
);
177 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
179 policy
= disk_policy(&dinfo
);
180 have_target
= policy_check_path(&dinfo
, &target_array
);
182 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
184 pr_err("no recognisable superblock on %s.\n",
186 rv
= try_spare(devname
, &dfd
, policy
,
187 have_target
? &target_array
: NULL
,
191 st
->ignore_hw_compat
= 1;
192 if (st
->ss
->compare_super
== NULL
||
193 st
->ss
->load_super(st
, dfd
, NULL
)) {
195 pr_err("no RAID superblock on %s.\n",
197 rv
= try_spare(devname
, &dfd
, policy
,
198 have_target
? &target_array
: NULL
,
203 close (dfd
); dfd
= -1;
205 st
->ss
->getinfo_super(st
, &info
, NULL
);
207 /* 3/ Check if there is a match in mdadm.conf */
208 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
209 if (!match
&& rv
== 2)
212 if (match
&& match
->devname
213 && strcasecmp(match
->devname
, "<ignore>") == 0) {
215 pr_err("array containing %s is explicitly"
216 " ignored by mdadm.conf\n",
221 /* 3a/ if not, check for homehost match. If no match, continue
222 * but don't trust the 'name' in the array. Thus a 'random' minor
223 * number will be assigned, and the device name will be based
227 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
229 else if (st
->ss
->match_home(st
, "any") == 1)
230 trustworthy
= LOCAL_ANY
;
232 trustworthy
= FOREIGN
;
234 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
235 (trustworthy
== LOCAL
))) {
237 pr_err("%s has metadata type %s for which "
238 "auto-assembly is disabled\n",
239 devname
, st
->ss
->name
);
242 if (trustworthy
== LOCAL_ANY
)
245 /* There are three possible sources for 'autof': command line,
246 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
247 * ARRAY takes precedence, then command line, then
250 if (match
&& match
->autof
)
251 c
->autof
= match
->autof
;
253 c
->autof
= ci
->autof
;
255 name_to_use
= info
.name
;
256 if (name_to_use
[0] == 0 &&
257 info
.array
.level
== LEVEL_CONTAINER
) {
258 name_to_use
= info
.text_version
;
259 trustworthy
= METADATA
;
261 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
262 ! c
->require_homehost
&&
263 conf_name_is_free(name_to_use
))
266 /* strip "hostname:" prefix from name if we have decided
267 * to treat it as LOCAL
269 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
270 name_to_use
= strchr(name_to_use
, ':')+1;
272 /* 4/ Check if array exists.
275 pr_err("failed to get exclusive lock on "
277 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
280 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
283 pr_err("cannot reopen %s: %s.\n",
284 devname
, strerror(errno
));
287 /* Cannot hold it open while we add the device to the array,
288 * so we must release the O_EXCL and depend on the map_lock()
289 * So now is the best time to remove any partitions.
291 remove_partitions(dfd
);
295 mp
= map_by_uuid(&map
, info
.uuid
);
297 mdfd
= open_dev(mp
->devnm
);
303 /* Couldn't find an existing array, maybe make a new one */
304 mdfd
= create_mddev(match
? match
->devname
: NULL
,
305 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
310 sysfs_init(&info
, mdfd
, NULL
);
312 if (set_array_info(mdfd
, st
, &info
) != 0) {
313 pr_err("failed to set array info for %s: %s\n",
314 chosen_name
, strerror(errno
));
320 dinfo
.disk
.major
= major(stb
.st_rdev
);
321 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
322 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
323 pr_err("failed to add %s to new array %s: %s.\n",
324 devname
, chosen_name
, strerror(errno
));
325 ioctl(mdfd
, STOP_ARRAY
, 0);
329 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
330 GET_OFFSET
| GET_SIZE
));
332 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
333 /* It really should be 'none' - must be old buggy
334 * kernel, and mdadm -I may not be able to complete.
337 ioctl(mdfd
, STOP_ARRAY
, NULL
);
338 pr_err("You have an old buggy kernel which cannot support\n"
339 " --incremental reliably. Aborting.\n");
343 info
.array
.working_disks
= 1;
344 /* 6/ Make sure /var/run/mdadm.map contains this array. */
345 map_update(&map
, fd2devnm(mdfd
),
347 info
.uuid
, chosen_name
);
350 /* - check one drive in array to make sure metadata is a reasonably */
351 /* close match. Reject if not (e.g. different type) */
352 /* - add the device */
356 struct supertype
*st2
;
357 struct mdinfo info2
, *d
;
359 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
360 GET_OFFSET
| GET_SIZE
));
363 strcpy(chosen_name
, mp
->path
);
365 strcpy(chosen_name
, mp
->devnm
);
367 /* It is generally not OK to add non-spare drives to a
368 * running array as they are probably missing because
369 * they failed. However if runstop is 1, then the
370 * array was possibly started early and our best bet is
371 * to add this anyway.
372 * Also if action policy is re-add or better we allow
374 * This doesn't apply to containers as the 'non-spare'
375 * flag has a different meaning. The test has to happen
376 * at the device level there
378 if (!st
->ss
->external
379 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
380 && ! policy_action_allows(policy
, st
->ss
->name
,
383 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
384 pr_err("not adding %s to active array (without --run) %s\n",
385 devname
, chosen_name
);
395 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
396 sra
->devs
->disk
.minor
);
397 dfd2
= dev_open(dn
, O_RDONLY
);
399 pr_err("unable to open %s\n", devname
);
404 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
405 st
->ss
->compare_super(st
, st2
) != 0) {
406 pr_err("metadata mismatch between %s and "
408 devname
, chosen_name
);
414 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
415 st2
->ss
->free_super(st2
);
416 if (info
.array
.level
!= info2
.array
.level
||
417 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
418 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
419 pr_err("unexpected difference between %s and %s.\n",
420 chosen_name
, devname
);
425 info
.disk
.major
= major(stb
.st_rdev
);
426 info
.disk
.minor
= minor(stb
.st_rdev
);
427 /* add disk needs to know about containers */
428 if (st
->ss
->external
)
429 sra
->array
.level
= LEVEL_CONTAINER
;
430 err
= add_disk(mdfd
, st
, sra
, &info
);
431 if (err
< 0 && errno
== EBUSY
) {
432 /* could be another device present with the same
433 * disk.number. Find and reject any such
435 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
436 info
.events
, c
->verbose
, chosen_name
);
437 err
= add_disk(mdfd
, st
, sra
, &info
);
439 if (err
< 0 && errno
== EINVAL
&&
440 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
441 /* Maybe it needs to be added as a spare */
442 if (policy_action_allows(policy
, st
->ss
->name
,
444 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
445 err
= add_disk(mdfd
, st
, sra
, &info
);
448 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
449 devname
, chosen_name
);
452 pr_err("failed to add %s to existing array %s: %s.\n",
453 devname
, chosen_name
, strerror(errno
));
457 info
.array
.working_disks
= 0;
458 for (d
= sra
->devs
; d
; d
=d
->next
)
459 info
.array
.working_disks
++;
463 /* 7/ Is there enough devices to possibly start the array? */
464 /* 7a/ if not, finish with success. */
465 if (info
.array
.level
== LEVEL_CONTAINER
) {
467 /* Try to assemble within the container */
468 sysfs_uevent(sra
, "change");
470 pr_err("container %s now has %d device%s\n",
471 chosen_name
, info
.array
.working_disks
,
472 info
.array
.working_disks
== 1?"":"s");
473 wait_for(chosen_name
, mdfd
);
474 if (st
->ss
->external
)
475 strcpy(devnm
, fd2devnm(mdfd
));
476 if (st
->ss
->load_container
)
477 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
481 rv
= Incremental_container(st
, chosen_name
, c
);
484 /* Don't fail the whole -I if a subarray didn't
485 * have enough devices to start yet
488 /* after spare is added, ping monitor for external metadata
489 * so that it can eg. try to rebuild degraded array */
490 if (st
->ss
->external
)
495 /* We have added something to the array, so need to re-read the
496 * state. Eventually this state should be kept up-to-date as
500 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
501 GET_OFFSET
| GET_SIZE
));
502 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
503 if (enough(info
.array
.level
, info
.array
.raid_disks
,
504 info
.array
.layout
, info
.array
.state
& 1,
507 pr_err("%s attached to %s, not enough to start (%d).\n",
508 devname
, chosen_name
, active_disks
);
514 /* - if number of OK devices match expected, or -R and there */
516 /* + add any bitmap file */
517 /* + start the array (auto-readonly). */
519 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
521 pr_err("%s attached to %s which is already active.\n",
522 devname
, chosen_name
);
528 if (c
->runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
530 /* Let's try to start it */
532 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
533 pr_err("%s: This array is being reshaped and cannot be started\n",
535 cont_err("by --incremental. Please use --assemble\n");
538 if (match
&& match
->bitmap_file
) {
539 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
541 pr_err("Could not open bitmap file %s.\n",
545 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
547 pr_err("Failed to set bitmapfile for %s.\n",
553 /* Need to remove from the array any devices which
554 * 'count_active' discerned were too old or inappropriate
556 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
557 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
558 remove_disk(mdfd
, st
, sra
, d
);
560 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
561 && trustworthy
!= FOREIGN
)
562 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
564 rv
= sysfs_set_str(sra
, NULL
,
565 "array_state", "read-auto");
568 pr_err("%s attached to %s, which has been started.\n",
569 devname
, chosen_name
);
571 wait_for(chosen_name
, mdfd
);
572 /* We just started the array, so some devices
573 * might have been evicted from the array
574 * because their event counts were too old.
575 * If the action=re-add policy is in-force for
576 * those devices we should re-add them now.
578 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
579 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
580 add_disk(mdfd
, st
, sra
, dsk
) == 0)
581 pr_err("%s re-added to %s\n",
582 dsk
->sys_name
, chosen_name
);
585 pr_err("%s attached to %s, but failed to start: %s.\n",
586 devname
, chosen_name
, strerror(errno
));
591 pr_err("%s attached to %s, not enough to start safely.\n",
592 devname
, chosen_name
);
602 dev_policy_free(policy
);
611 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
612 int number
, __u64 events
, int verbose
,
615 /* Find a device attached to this array with a disk.number of number
616 * and events less than the passed events, and remove the device.
621 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
622 return; /* not safe to remove from active arrays
623 * without thinking more */
625 for (d
= sra
->devs
; d
; d
= d
->next
) {
629 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
630 dfd
= dev_open(dn
, O_RDONLY
);
633 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
637 st
->ss
->getinfo_super(st
, &info
, NULL
);
638 st
->ss
->free_super(st
);
641 if (info
.disk
.number
!= number
||
642 info
.events
>= events
)
645 if (d
->disk
.raid_disk
> -1)
646 sysfs_set_str(sra
, d
, "slot", "none");
647 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
649 pr_err("removing old device %s from %s\n",
650 d
->sys_name
+4, array_name
);
654 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
655 int mdfd
, char **availp
,
656 struct mdinfo
*bestinfo
)
658 /* count how many devices in sra think they are active */
662 __u64 max_events
= 0;
674 for (d
= sra
->devs
; d
; d
= d
->next
)
676 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
682 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
683 dfd
= dev_open(dn
, O_RDONLY
);
686 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
690 info
.array
.raid_disks
= raid_disks
;
691 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
693 raid_disks
= info
.array
.raid_disks
;
694 avail
= xcalloc(raid_disks
, 1);
697 best
= xcalloc(raid_disks
, sizeof(int));
698 devmap
= xcalloc(raid_disks
, numdevs
);
700 st
->ss
->getinfo_super(st
, &info
, devmap
);
703 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
707 max_events
= info
.events
;
708 avail
[info
.disk
.raid_disk
] = 2;
709 best
[info
.disk
.raid_disk
] = devnum
;
710 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
711 } else if (info
.events
== max_events
) {
712 avail
[info
.disk
.raid_disk
] = 2;
713 best
[info
.disk
.raid_disk
] = devnum
;
714 } else if (info
.events
== max_events
-1) {
715 if (avail
[info
.disk
.raid_disk
] == 0) {
716 avail
[info
.disk
.raid_disk
] = 1;
717 best
[info
.disk
.raid_disk
] = devnum
;
719 } else if (info
.events
< max_events
- 1)
721 else if (info
.events
== max_events
+1) {
723 max_events
= info
.events
;
724 for (i
= 0; i
< raid_disks
; i
++)
727 avail
[info
.disk
.raid_disk
] = 2;
728 best
[info
.disk
.raid_disk
] = devnum
;
729 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
730 } else { /* info.events much bigger */
731 memset(avail
, 0, raid_disks
);
732 max_events
= info
.events
;
733 avail
[info
.disk
.raid_disk
] = 2;
734 best
[info
.disk
.raid_disk
] = devnum
;
735 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
737 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
739 st
->ss
->free_super(st
);
743 /* We need to reject any device that thinks the best device is
744 * failed or missing */
745 for (b
= 0; b
< raid_disks
; b
++)
749 for (i
= 0 ; i
< raid_disks
; i
++) {
750 if (i
!= b
&& avail
[i
])
751 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
752 /* This device thinks 'b' is failed -
755 for (d
=sra
->devs
; devnum
; d
= d
->next
)
757 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
763 /* Also need to reject any spare device with an event count that
766 for (d
= sra
->devs
; d
; d
= d
->next
) {
767 if (!(d
->disk
.state
& (1<<MD_DISK_SYNC
)) &&
768 d
->events
> max_events
)
769 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
773 return cnt
+ replcnt
;
776 /* test if container has degraded member(s) */
777 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
779 mdu_array_info_t array
;
781 int max_degraded
= 0;
783 for(; map
; map
= map
->next
) {
784 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
786 afd
= open_dev(map
->devnm
);
789 /* most accurate information regarding array degradation */
790 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
791 int degraded
= array
.raid_disks
- array
.active_disks
-
793 if (degraded
> max_degraded
)
794 max_degraded
= degraded
;
798 return (max_degraded
);
801 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
802 struct map_ent
*target
, int bare
,
803 struct supertype
*st
, int verbose
)
805 /* This device doesn't have any md metadata
806 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
807 * If 'st' is not set, then we only know that some metadata allows this,
808 * others possibly don't.
809 * So look for a container or array to attach the device to.
810 * Prefer 'target' if that is set and the array is found.
812 * If st is set, then only arrays of that type are considered
813 * Return 0 on success, or some exit code on failure, probably 1.
817 struct map_ent
*mp
, *map
= NULL
;
818 struct mdinfo
*chosen
= NULL
;
821 if (fstat(dfd
, &stb
) != 0)
825 * Now we need to find a suitable array to add this to.
826 * We only accept arrays that:
828 * - are in the same domains as the device
829 * - are of an size for which the device will be useful
830 * and we choose the one that is the most degraded
833 if (map_lock(&map
)) {
834 pr_err("failed to get exclusive lock on "
838 for (mp
= map
; mp
; mp
= mp
->next
) {
839 struct supertype
*st2
;
840 struct domainlist
*dl
= NULL
;
842 unsigned long long devsize
;
843 unsigned long long component_size
= 0;
845 if (is_subarray(mp
->metadata
))
848 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
850 (st
->minor_version
>= 0 &&
851 st
->minor_version
!= st2
->minor_version
)) {
853 pr_err("not adding %s to %s as metadata type doesn't match\n",
860 sra
= sysfs_read(-1, mp
->devnm
,
861 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
862 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
864 /* Probably a container - no degraded info */
865 sra
= sysfs_read(-1, mp
->devnm
,
866 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
867 GET_COMPONENT
|GET_VERSION
);
869 sra
->array
.failed_disks
= -1;
876 for(i
= 0; !st2
&& superlist
[i
]; i
++)
877 st2
= superlist
[i
]->match_metadata_desc(
881 pr_err("not adding %s to %s"
882 " as metadata not recognised.\n",
886 /* Need to double check the 'act_spare' permissions applies
889 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
891 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
892 act_spare_same_slot
))
896 /* update number of failed disks for mostly degraded
897 * container member */
898 if (sra
->array
.failed_disks
== -1)
899 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
901 get_dev_size(dfd
, NULL
, &devsize
);
902 if (sra
->component_size
== 0) {
903 /* true for containers, here we must read superblock
904 * to obtain minimum spare size */
905 struct supertype
*st3
= dup_super(st2
);
906 int mdfd
= open_dev(mp
->devnm
);
911 if (st3
->ss
->load_container
&&
912 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
913 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
914 st3
->ss
->free_super(st3
);
919 if ((sra
->component_size
> 0 &&
920 st2
->ss
->avail_size(st2
, devsize
,
922 ? sra
->devs
->data_offset
924 < sra
->component_size
)
926 (sra
->component_size
== 0 && devsize
< component_size
)) {
928 pr_err("not adding %s to %s as it is too small\n",
932 /* test against target.
933 * If 'target' is set and 'bare' is false, we only accept
934 * arrays/containers that match 'target'.
935 * If 'target' is set and 'bare' is true, we prefer the
936 * array which matches 'target'.
937 * target is considered only if we deal with degraded array
939 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
940 act_spare_same_slot
)) {
941 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
942 memcmp(target
->uuid
, mp
->uuid
,
943 sizeof(target
->uuid
)) == 0 &&
944 sra
->array
.failed_disks
> 0) {
945 /* This is our target!! */
950 /* skip to end so we don't check any more */
960 dl
= domain_from_array(sra
, st2
->ss
->name
);
961 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
962 /* domain test fails */
964 pr_err("not adding %s to %s as"
965 " it is not in a compatible domain\n",
970 /* all tests passed, OK to add to this array */
974 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
988 /* add current device to chosen array as a spare */
989 int mdfd
= open_dev(chosen
->sys_name
);
991 struct mddev_dev devlist
;
995 devlist
.writemostly
= 0;
996 devlist
.devname
= devname
;
997 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
999 devlist
.disposition
= 'a';
1002 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1008 pr_err("added %s as spare for %s\n",
1009 devname
, chosen
->sys_name
);
1011 pr_err("failed to add %s as spare for %s\n",
1012 devname
, chosen
->sys_name
);
1020 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1021 struct supertype
*st
, int verbose
)
1023 /* we know that at least one partition virtual-metadata is
1024 * allowed to incorporate spares like this device. We need to
1025 * find a suitable device to copy partition information from.
1027 * Getting a list of all disk (not partition) devices is
1028 * slightly non-trivial. We could look at /sys/block, but
1029 * that is theoretically due to be removed. Maybe best to use
1030 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1031 * we depend on this directory of 'path' info. But that fails
1032 * to find loop devices and probably others. Maybe don't
1033 * worry about that, they aren't the real target.
1035 * So: check things in /dev/disk/by-path to see if they are in
1036 * a compatible domain, then load the partition table and see
1037 * if it is OK for the new device, and choose the largest
1038 * partition table that fits.
1042 char *chosen
= NULL
;
1043 unsigned long long chosen_size
= 0;
1044 struct supertype
*chosen_st
= NULL
;
1047 dir
= opendir("/dev/disk/by-path");
1050 while ((de
= readdir(dir
)) != NULL
) {
1052 struct dev_policy
*pol2
= NULL
;
1053 struct domainlist
*domlist
= NULL
;
1056 struct supertype
*st2
= NULL
;
1057 char *devname
= NULL
;
1058 unsigned long long devsectors
;
1060 if (de
->d_ino
== 0 ||
1061 de
->d_name
[0] == '.' ||
1062 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1065 ep
= de
->d_name
+ strlen(de
->d_name
);
1066 while (ep
> de
->d_name
&&
1069 if (ep
> de
->d_name
+ 5 &&
1070 strncmp(ep
-5, "-part", 5) == 0)
1071 /* This is a partition - skip it */
1074 pol2
= path_policy(de
->d_name
, type_disk
);
1076 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1077 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1078 /* new device is incompatible with this device. */
1081 domain_free(domlist
);
1084 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1088 fd
= open(devname
, O_RDONLY
);
1091 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1096 st2
= dup_super(st
);
1098 st2
= guess_super_type(fd
, guess_partitions
);
1100 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1104 /* Check domain policy again, this time referring to metadata */
1105 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1106 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1107 /* Incompatible devices for this metadata type */
1109 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1110 /* Some partition types allow sparing, but not
1116 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1117 if (info
.component_size
> devsectors
)
1118 /* This partitioning doesn't fit in the device */
1121 /* This is an acceptable device to copy partition
1122 * metadata from. We could just stop here, but I
1123 * think I want to keep looking incase a larger
1124 * metadata which makes better use of the device can
1127 if (chosen
== NULL
||
1128 chosen_size
< info
.component_size
) {
1129 chosen_size
= info
.component_size
;
1134 chosen_st
->ss
->free_super(chosen_st
);
1143 domain_free(domlist
);
1144 dev_policy_free(pol2
);
1146 st2
->ss
->free_super(st2
);
1158 /* 'chosen' is the best device we can find. Let's write its
1159 * metadata to devname dfd is read-only so don't use that
1161 fd
= open(devname
, O_RDWR
);
1163 chosen_st
->ss
->store_super(chosen_st
, fd
);
1167 chosen_st
->ss
->free_super(chosen_st
);
1172 static int is_bare(int dfd
)
1174 unsigned long long size
= 0;
1175 char bufpad
[4096 + 4096];
1176 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1178 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1179 read(dfd
, buf
, 4096) != 4096)
1182 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1184 if (memcmp(buf
, buf
+1, 4095) != 0)
1187 /* OK, first 4K appear blank, try the end. */
1188 get_dev_size(dfd
, NULL
, &size
);
1189 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1190 read(dfd
, buf
, 4096) != 4096)
1193 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1195 if (memcmp(buf
, buf
+1, 4095) != 0)
1201 /* adding a spare to a regular array is quite different from adding one to
1202 * a set-of-partitions virtual array.
1203 * This function determines which is worth trying and tries as appropriate.
1204 * Arrays are given priority over partitions.
1206 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1207 struct map_ent
*target
,
1208 struct supertype
*st
, int verbose
)
1213 int partitions_ok
= 0;
1217 /* Can only add a spare if device has at least one domain */
1218 if (pol_find(pol
, pol_domain
) == NULL
)
1220 /* And only if some action allows spares */
1221 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1224 /* Now check if the device is bare.
1225 * bare devices can always be added as a spare
1226 * non-bare devices can only be added if spare-same-slot is permitted,
1227 * and this device is replacing a previous device - in which case 'target'
1230 if (!is_bare(dfd
)) {
1231 /* Must have a target and allow same_slot */
1232 /* Later - may allow force_spare without target */
1234 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1235 act_spare_same_slot
)) {
1237 pr_err("%s is not bare, so not "
1238 "considering as a spare\n",
1246 /* It might be OK to add this device to an array - need to see
1247 * what arrays might be candidates.
1250 /* just try try 'array' or 'partition' based on this metadata */
1251 if (st
->ss
->add_to_super
)
1252 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1255 return partition_try_spare(devname
, dfdp
, pol
,
1258 /* No metadata was specified or found so options are open.
1259 * Check for whether any array metadata, or any partition metadata
1260 * might allow adding the spare. This check is just help to avoid
1261 * a more costly scan of all arrays when we can be sure that will
1264 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1265 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1266 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1268 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1269 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1274 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1276 if (rv
!= 0 && partitions_ok
)
1277 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1281 int IncrementalScan(int verbose
)
1283 /* look at every device listed in the 'map' file.
1284 * If one is found that is not running then:
1285 * look in mdadm.conf for bitmap file.
1286 * if one exists, but array has none, add it.
1287 * try to start array in auto-readonly mode
1289 struct map_ent
*mapl
= NULL
;
1291 struct mddev_ident
*devs
, *mddev
;
1295 devs
= conf_get_ident(NULL
);
1297 for (me
= mapl
; me
; me
= me
->next
) {
1298 mdu_array_info_t array
;
1299 mdu_bitmap_file_t bmf
;
1301 int mdfd
= open_dev(me
->devnm
);
1305 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1310 /* Ok, we can try this one. Maybe it needs a bitmap */
1311 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1312 if (mddev
->devname
&& me
->path
1313 && devname_matches(mddev
->devname
, me
->path
))
1315 if (mddev
&& mddev
->bitmap_file
) {
1317 * Note: early kernels will wrongly fail this, so it
1321 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1322 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1324 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1331 pr_err("Added bitmap %s to %s\n",
1332 mddev
->bitmap_file
, me
->path
);
1333 else if (errno
!= EEXIST
)
1334 pr_err("Failed to add bitmap to %s: %s\n",
1335 me
->path
, strerror(errno
));
1338 /* FIXME check for reshape_active and consider not
1341 sra
= sysfs_read(mdfd
, NULL
, 0);
1343 if (sysfs_set_str(sra
, NULL
,
1344 "array_state", "read-auto") == 0) {
1346 pr_err("started array %s\n",
1347 me
->path
?: me
->devnm
);
1349 pr_err("failed to start array %s: %s\n",
1350 me
->path
?: me
->devnm
,
1360 static char *container2devname(char *devname
)
1362 char *mdname
= NULL
;
1364 if (devname
[0] == '/') {
1365 int fd
= open(devname
, O_RDONLY
);
1367 mdname
= xstrdup(fd2devnm(fd
));
1372 struct map_ent
*mp
, *map
= NULL
;
1374 if (!parse_uuid(devname
, uuid
))
1376 mp
= map_by_uuid(&map
, uuid
);
1378 mdname
= xstrdup(mp
->devnm
);
1385 static int Incremental_container(struct supertype
*st
, char *devname
,
1388 /* Collect the contents of this container and for each
1389 * array, choose a device name and assemble the array.
1392 struct mdinfo
*list
;
1394 struct map_ent
*map
= NULL
;
1397 struct mddev_ident
*match
;
1399 struct domainlist
*domains
;
1400 struct map_ent
*smp
;
1406 st
->ss
->getinfo_super(st
, &info
, NULL
);
1408 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1409 info
.container_enough
> 0)
1413 pr_err("not enough devices to start the container\n");
1417 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1418 if (match
== NULL
&& rv
== 2)
1421 /* Need to compute 'trustworthy' */
1423 trustworthy
= LOCAL
;
1424 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1425 trustworthy
= LOCAL
;
1426 else if (st
->ss
->match_home(st
, "any") == 1)
1427 trustworthy
= LOCAL
;
1429 trustworthy
= FOREIGN
;
1431 list
= st
->ss
->container_content(st
, NULL
);
1432 /* when nothing to activate - quit */
1435 for (ra
= list
; ra
; ra
= ra
->next
) {
1437 char chosen_name
[1024];
1439 struct mddev_ident
*match
= NULL
;
1442 /* do not activate arrays blocked by metadata handler */
1443 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1444 pr_err("Cannot activate array %s in %s.\n",
1445 ra
->text_version
, devname
);
1449 mp
= map_by_uuid(&map
, ra
->uuid
);
1452 mdfd
= open_dev(mp
->devnm
);
1454 strcpy(chosen_name
, mp
->path
);
1456 strcpy(chosen_name
, mp
->devnm
);
1459 /* Check in mdadm.conf for container == devname and
1460 * member == ra->text_version after second slash.
1462 char *sub
= strchr(ra
->text_version
+1, '/');
1463 struct mddev_ident
*array_list
;
1466 array_list
= conf_get_ident(NULL
);
1469 for(; array_list
; array_list
= array_list
->next
) {
1471 if (array_list
->member
== NULL
||
1472 array_list
->container
== NULL
)
1474 if (strcmp(array_list
->member
, sub
) != 0)
1476 if (array_list
->uuid_set
&&
1477 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1479 dn
= container2devname(array_list
->container
);
1482 if (strncmp(dn
, ra
->text_version
+1,
1484 ra
->text_version
[strlen(dn
)+1] != '/') {
1489 /* we have a match */
1492 pr_err("match found for member %s\n",
1493 array_list
->member
);
1497 if (match
&& match
->devname
&&
1498 strcasecmp(match
->devname
, "<ignore>") == 0) {
1500 pr_err("array %s/%s is "
1501 "explicitly ignored by mdadm.conf\n",
1502 match
->container
, match
->member
);
1506 trustworthy
= LOCAL
;
1508 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1516 pr_err("failed to open %s: %s.\n",
1517 chosen_name
, strerror(errno
));
1521 assemble_container_content(st
, mdfd
, ra
, c
,
1526 /* don't move spares to container with volume being activated
1527 when all volumes are blocked */
1528 if (ra_all
== ra_blocked
)
1531 /* Now move all suitable spares from spare container */
1532 domains
= domain_from_array(list
, st
->ss
->name
);
1533 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1535 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1536 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1537 /* spare container found */
1538 struct supertype
*sst
=
1539 super_imsm
.match_metadata_desc("imsm");
1540 struct mdinfo
*sinfo
;
1541 unsigned long long min_size
= 0;
1542 if (st
->ss
->min_acceptable_spare_size
)
1543 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1544 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1546 sinfo
= container_choose_spares(sst
, min_size
,
1549 sst
->ss
->free_super(sst
);
1552 struct mdinfo
*disks
= sinfo
->devs
;
1554 /* move spare from spare
1555 * container to currently
1561 makedev(disks
->disk
.major
,
1562 disks
->disk
.minor
)))
1564 disks
= disks
->next
;
1567 pr_err("Added %d spare%s to %s\n",
1568 count
, count
>1?"s":"", devname
);
1574 domain_free(domains
);
1578 static void run_udisks(char *arg1
, char *arg2
)
1583 execl("/usr/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1584 execl("/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1587 while (pid
> 0 && wait(&status
) != pid
)
1592 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1593 * raid arrays, and if so first fail (if needed) and then remove the device.
1595 * @devname - The device we want to remove
1596 * @id_path - name as found in /dev/disk/by-path for this device
1598 * Note: the device name must be a kernel name like "sda", so
1599 * that we can find it in /proc/mdstat
1601 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1605 struct mdstat_ent
*ent
;
1606 struct mddev_dev devlist
;
1611 dprintf(Name
": incremental removal without --path <id_path> "
1612 "lacks the possibility to re-add new device in this "
1615 if (strchr(devname
, '/')) {
1616 pr_err("incremental removal requires a "
1617 "kernel device name, not a file: %s\n", devname
);
1620 ent
= mdstat_by_component(devname
);
1623 pr_err("%s does not appear to be a component "
1624 "of any array\n", devname
);
1627 sysfs_init(&mdi
, -1, ent
->devnm
);
1628 if (sysfs_get_str(&mdi
, NULL
, "array_state",
1629 buf
, sizeof(buf
)) > 0) {
1630 if (strncmp(buf
, "active", 6) == 0 ||
1631 strncmp(buf
, "clean", 5) == 0)
1632 sysfs_set_str(&mdi
, NULL
,
1633 "array_state", "read-auto");
1635 mdfd
= open_dev(ent
->devnm
);
1638 pr_err("Cannot open array %s!!\n", ent
->dev
);
1644 struct map_ent
*map
= NULL
, *me
;
1645 me
= map_by_devnm(&map
, ent
->devnm
);
1647 policy_save_path(id_path
, me
);
1651 memset(&devlist
, 0, sizeof(devlist
));
1652 devlist
.devname
= devname
;
1653 devlist
.disposition
= 'f';
1654 /* for a container, we must fail each member array */
1655 if (ent
->metadata_version
&&
1656 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1657 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1658 struct mdstat_ent
*memb
;
1659 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1660 if (is_container_member(memb
, ent
->dev
)) {
1661 int subfd
= open_dev(memb
->devnm
);
1663 rv
|= Manage_subdevs(
1665 &devlist
, verbose
, 0,
1670 free_mdstat(mdstat
);
1672 rv
|= Manage_subdevs(ent
->dev
, mdfd
, &devlist
,
1673 verbose
, 0, NULL
, 0);
1675 /* Failed due to EBUSY, try to stop the array.
1676 * Give udisks a chance to unmount it first.
1678 int devid
= devnm2devid(ent
->devnm
);
1679 run_udisks("--unmount", map_dev(major(devid
),minor(devid
), 0));
1680 rv
= Manage_stop(ent
->dev
, mdfd
, verbose
, 1);
1682 /* At least we can try to trigger a 'remove' */
1683 sysfs_uevent(&mdi
, "remove");
1686 pr_err("Fail to stop %s too.\n", ent
->devnm
);
1689 devlist
.disposition
= 'r';
1690 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
,
1691 verbose
, 0, NULL
, 0);