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()
293 mp
= map_by_uuid(&map
, info
.uuid
);
295 mdfd
= open_dev(mp
->devnm
);
301 /* Couldn't find an existing array, maybe make a new one */
302 mdfd
= create_mddev(match
? match
->devname
: NULL
,
303 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
308 sysfs_init(&info
, mdfd
, NULL
);
310 if (set_array_info(mdfd
, st
, &info
) != 0) {
311 pr_err("failed to set array info for %s: %s\n",
312 chosen_name
, strerror(errno
));
318 dinfo
.disk
.major
= major(stb
.st_rdev
);
319 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
320 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
321 pr_err("failed to add %s to new array %s: %s.\n",
322 devname
, chosen_name
, strerror(errno
));
323 ioctl(mdfd
, STOP_ARRAY
, 0);
327 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
328 GET_OFFSET
| GET_SIZE
));
330 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
331 /* It really should be 'none' - must be old buggy
332 * kernel, and mdadm -I may not be able to complete.
335 ioctl(mdfd
, STOP_ARRAY
, NULL
);
336 pr_err("You have an old buggy kernel which cannot support\n"
337 " --incremental reliably. Aborting.\n");
341 info
.array
.working_disks
= 1;
342 /* 6/ Make sure /var/run/mdadm.map contains this array. */
343 map_update(&map
, fd2devnm(mdfd
),
345 info
.uuid
, chosen_name
);
348 /* - check one drive in array to make sure metadata is a reasonably */
349 /* close match. Reject if not (e.g. different type) */
350 /* - add the device */
354 struct supertype
*st2
;
355 struct mdinfo info2
, *d
;
357 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
358 GET_OFFSET
| GET_SIZE
));
361 strcpy(chosen_name
, mp
->path
);
363 strcpy(chosen_name
, mp
->devnm
);
365 /* It is generally not OK to add non-spare drives to a
366 * running array as they are probably missing because
367 * they failed. However if runstop is 1, then the
368 * array was possibly started early and our best bet is
369 * to add this anyway.
370 * Also if action policy is re-add or better we allow
372 * This doesn't apply to containers as the 'non-spare'
373 * flag has a different meaning. The test has to happen
374 * at the device level there
376 if (!st
->ss
->external
377 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
378 && ! policy_action_allows(policy
, st
->ss
->name
,
381 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
382 pr_err("not adding %s to active array (without --run) %s\n",
383 devname
, chosen_name
);
393 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
394 sra
->devs
->disk
.minor
);
395 dfd2
= dev_open(dn
, O_RDONLY
);
397 pr_err("unable to open %s\n", devname
);
402 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
403 st
->ss
->compare_super(st
, st2
) != 0) {
404 pr_err("metadata mismatch between %s and "
406 devname
, chosen_name
);
412 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
413 st2
->ss
->free_super(st2
);
414 if (info
.array
.level
!= info2
.array
.level
||
415 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
416 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
417 pr_err("unexpected difference between %s and %s.\n",
418 chosen_name
, devname
);
423 info
.disk
.major
= major(stb
.st_rdev
);
424 info
.disk
.minor
= minor(stb
.st_rdev
);
425 /* add disk needs to know about containers */
426 if (st
->ss
->external
)
427 sra
->array
.level
= LEVEL_CONTAINER
;
428 err
= add_disk(mdfd
, st
, sra
, &info
);
429 if (err
< 0 && errno
== EBUSY
) {
430 /* could be another device present with the same
431 * disk.number. Find and reject any such
433 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
434 info
.events
, c
->verbose
, chosen_name
);
435 err
= add_disk(mdfd
, st
, sra
, &info
);
437 if (err
< 0 && errno
== EINVAL
&&
438 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
439 /* Maybe it needs to be added as a spare */
440 if (policy_action_allows(policy
, st
->ss
->name
,
442 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
443 err
= add_disk(mdfd
, st
, sra
, &info
);
446 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
447 devname
, chosen_name
);
450 pr_err("failed to add %s to existing array %s: %s.\n",
451 devname
, chosen_name
, strerror(errno
));
455 info
.array
.working_disks
= 0;
456 for (d
= sra
->devs
; d
; d
=d
->next
)
457 info
.array
.working_disks
++;
461 /* 7/ Is there enough devices to possibly start the array? */
462 /* 7a/ if not, finish with success. */
463 if (info
.array
.level
== LEVEL_CONTAINER
) {
465 /* Try to assemble within the container */
466 sysfs_uevent(sra
, "change");
468 pr_err("container %s now has %d device%s\n",
469 chosen_name
, info
.array
.working_disks
,
470 info
.array
.working_disks
== 1?"":"s");
471 wait_for(chosen_name
, mdfd
);
472 if (st
->ss
->external
)
473 strcpy(devnm
, fd2devnm(mdfd
));
474 if (st
->ss
->load_container
)
475 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
479 rv
= Incremental_container(st
, chosen_name
, c
);
482 /* Don't fail the whole -I if a subarray didn't
483 * have enough devices to start yet
486 /* after spare is added, ping monitor for external metadata
487 * so that it can eg. try to rebuild degraded array */
488 if (st
->ss
->external
)
493 /* We have added something to the array, so need to re-read the
494 * state. Eventually this state should be kept up-to-date as
498 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
499 GET_OFFSET
| GET_SIZE
));
500 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
501 if (enough(info
.array
.level
, info
.array
.raid_disks
,
502 info
.array
.layout
, info
.array
.state
& 1,
505 pr_err("%s attached to %s, not enough to start (%d).\n",
506 devname
, chosen_name
, active_disks
);
512 /* - if number of OK devices match expected, or -R and there */
514 /* + add any bitmap file */
515 /* + start the array (auto-readonly). */
517 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
519 pr_err("%s attached to %s which is already active.\n",
520 devname
, chosen_name
);
526 if (c
->runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
528 /* Let's try to start it */
530 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
532 ": %s: This array is being reshaped and cannot be started\n"
533 " by --incremental. Please use --assemble\n",
537 if (match
&& match
->bitmap_file
) {
538 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
540 pr_err("Could not open bitmap file %s.\n",
544 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
546 pr_err("Failed to set bitmapfile for %s.\n",
552 /* Need to remove from the array any devices which
553 * 'count_active' discerned were too old or inappropriate
555 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
556 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
557 remove_disk(mdfd
, st
, sra
, d
);
559 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
560 && trustworthy
!= FOREIGN
)
561 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
563 rv
= sysfs_set_str(sra
, NULL
,
564 "array_state", "read-auto");
567 pr_err("%s attached to %s, which has been started.\n",
568 devname
, chosen_name
);
570 wait_for(chosen_name
, mdfd
);
571 /* We just started the array, so some devices
572 * might have been evicted from the array
573 * because their event counts were too old.
574 * If the action=re-add policy is in-force for
575 * those devices we should re-add them now.
577 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
578 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
579 add_disk(mdfd
, st
, sra
, dsk
) == 0)
580 pr_err("%s re-added to %s\n",
581 dsk
->sys_name
, chosen_name
);
584 pr_err("%s attached to %s, but failed to start: %s.\n",
585 devname
, chosen_name
, strerror(errno
));
590 pr_err("%s attached to %s, not enough to start safely.\n",
591 devname
, chosen_name
);
601 dev_policy_free(policy
);
610 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
611 int number
, __u64 events
, int verbose
,
614 /* Find a device attached to this array with a disk.number of number
615 * and events less than the passed events, and remove the device.
620 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
621 return; /* not safe to remove from active arrays
622 * without thinking more */
624 for (d
= sra
->devs
; d
; d
= d
->next
) {
628 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
629 dfd
= dev_open(dn
, O_RDONLY
);
632 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
636 st
->ss
->getinfo_super(st
, &info
, NULL
);
637 st
->ss
->free_super(st
);
640 if (info
.disk
.number
!= number
||
641 info
.events
>= events
)
644 if (d
->disk
.raid_disk
> -1)
645 sysfs_set_str(sra
, d
, "slot", "none");
646 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
648 pr_err("removing old device %s from %s\n",
649 d
->sys_name
+4, array_name
);
653 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
654 int mdfd
, char **availp
,
655 struct mdinfo
*bestinfo
)
657 /* count how many devices in sra think they are active */
661 __u64 max_events
= 0;
673 for (d
= sra
->devs
; d
; d
= d
->next
)
675 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
681 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
682 dfd
= dev_open(dn
, O_RDONLY
);
685 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
689 info
.array
.raid_disks
= raid_disks
;
690 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
692 raid_disks
= info
.array
.raid_disks
;
693 avail
= xcalloc(raid_disks
, 1);
696 best
= xcalloc(raid_disks
, sizeof(int));
697 devmap
= xcalloc(raid_disks
, numdevs
);
699 st
->ss
->getinfo_super(st
, &info
, devmap
);
702 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
706 max_events
= info
.events
;
707 avail
[info
.disk
.raid_disk
] = 2;
708 best
[info
.disk
.raid_disk
] = devnum
;
709 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
710 } else if (info
.events
== max_events
) {
711 avail
[info
.disk
.raid_disk
] = 2;
712 best
[info
.disk
.raid_disk
] = devnum
;
713 } else if (info
.events
== max_events
-1) {
714 if (avail
[info
.disk
.raid_disk
] == 0) {
715 avail
[info
.disk
.raid_disk
] = 1;
716 best
[info
.disk
.raid_disk
] = devnum
;
718 } else if (info
.events
< max_events
- 1)
720 else if (info
.events
== max_events
+1) {
722 max_events
= info
.events
;
723 for (i
= 0; i
< raid_disks
; i
++)
726 avail
[info
.disk
.raid_disk
] = 2;
727 best
[info
.disk
.raid_disk
] = devnum
;
728 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
729 } else { /* info.events much bigger */
730 memset(avail
, 0, raid_disks
);
731 max_events
= info
.events
;
732 avail
[info
.disk
.raid_disk
] = 2;
733 best
[info
.disk
.raid_disk
] = devnum
;
734 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
736 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
738 st
->ss
->free_super(st
);
742 /* We need to reject any device that thinks the best device is
743 * failed or missing */
744 for (b
= 0; b
< raid_disks
; b
++)
748 for (i
= 0 ; i
< raid_disks
; i
++) {
749 if (i
!= b
&& avail
[i
])
750 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
751 /* This device thinks 'b' is failed -
754 for (d
=sra
->devs
; devnum
; d
= d
->next
)
756 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
764 return cnt
+ replcnt
;
767 /* test if container has degraded member(s) */
768 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
770 mdu_array_info_t array
;
772 int max_degraded
= 0;
774 for(; map
; map
= map
->next
) {
775 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
777 afd
= open_dev(map
->devnm
);
780 /* most accurate information regarding array degradation */
781 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
782 int degraded
= array
.raid_disks
- array
.active_disks
-
784 if (degraded
> max_degraded
)
785 max_degraded
= degraded
;
789 return (max_degraded
);
792 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
793 struct map_ent
*target
, int bare
,
794 struct supertype
*st
, int verbose
)
796 /* This device doesn't have any md metadata
797 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
798 * If 'st' is not set, then we only know that some metadata allows this,
799 * others possibly don't.
800 * So look for a container or array to attach the device to.
801 * Prefer 'target' if that is set and the array is found.
803 * If st is set, then only arrays of that type are considered
804 * Return 0 on success, or some exit code on failure, probably 1.
808 struct map_ent
*mp
, *map
= NULL
;
809 struct mdinfo
*chosen
= NULL
;
812 if (fstat(dfd
, &stb
) != 0)
816 * Now we need to find a suitable array to add this to.
817 * We only accept arrays that:
819 * - are in the same domains as the device
820 * - are of an size for which the device will be useful
821 * and we choose the one that is the most degraded
824 if (map_lock(&map
)) {
825 pr_err("failed to get exclusive lock on "
829 for (mp
= map
; mp
; mp
= mp
->next
) {
830 struct supertype
*st2
;
831 struct domainlist
*dl
= NULL
;
833 unsigned long long devsize
;
834 unsigned long long component_size
= 0;
836 if (is_subarray(mp
->metadata
))
839 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
841 (st
->minor_version
>= 0 &&
842 st
->minor_version
!= st2
->minor_version
)) {
844 pr_err("not adding %s to %s as metadata type doesn't match\n",
851 sra
= sysfs_read(-1, mp
->devnm
,
852 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
853 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
855 /* Probably a container - no degraded info */
856 sra
= sysfs_read(-1, mp
->devnm
,
857 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
858 GET_COMPONENT
|GET_VERSION
);
860 sra
->array
.failed_disks
= -1;
867 for(i
= 0; !st2
&& superlist
[i
]; i
++)
868 st2
= superlist
[i
]->match_metadata_desc(
872 pr_err("not adding %s to %s"
873 " as metadata not recognised.\n",
877 /* Need to double check the 'act_spare' permissions applies
880 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
882 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
883 act_spare_same_slot
))
887 /* update number of failed disks for mostly degraded
888 * container member */
889 if (sra
->array
.failed_disks
== -1)
890 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
892 get_dev_size(dfd
, NULL
, &devsize
);
893 if (sra
->component_size
== 0) {
894 /* true for containers, here we must read superblock
895 * to obtain minimum spare size */
896 struct supertype
*st3
= dup_super(st2
);
897 int mdfd
= open_dev(mp
->devnm
);
902 if (st3
->ss
->load_container
&&
903 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
904 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
905 st3
->ss
->free_super(st3
);
910 if ((sra
->component_size
> 0 &&
911 st2
->ss
->avail_size(st2
, devsize
,
913 ? sra
->devs
->data_offset
915 < sra
->component_size
)
917 (sra
->component_size
== 0 && devsize
< component_size
)) {
919 pr_err("not adding %s to %s as it is too small\n",
923 /* test against target.
924 * If 'target' is set and 'bare' is false, we only accept
925 * arrays/containers that match 'target'.
926 * If 'target' is set and 'bare' is true, we prefer the
927 * array which matches 'target'.
928 * target is considered only if we deal with degraded array
930 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
931 act_spare_same_slot
)) {
932 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
933 memcmp(target
->uuid
, mp
->uuid
,
934 sizeof(target
->uuid
)) == 0 &&
935 sra
->array
.failed_disks
> 0) {
936 /* This is our target!! */
941 /* skip to end so we don't check any more */
951 dl
= domain_from_array(sra
, st2
->ss
->name
);
952 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
953 /* domain test fails */
955 pr_err("not adding %s to %s as"
956 " it is not in a compatible domain\n",
961 /* all tests passed, OK to add to this array */
965 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
979 /* add current device to chosen array as a spare */
980 int mdfd
= open_dev(chosen
->sys_name
);
982 struct mddev_dev devlist
;
986 devlist
.writemostly
= 0;
987 devlist
.devname
= devname
;
988 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
990 devlist
.disposition
= 'a';
993 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
999 pr_err("added %s as spare for %s\n",
1000 devname
, chosen
->sys_name
);
1002 pr_err("failed to add %s as spare for %s\n",
1003 devname
, chosen
->sys_name
);
1011 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1012 struct supertype
*st
, int verbose
)
1014 /* we know that at least one partition virtual-metadata is
1015 * allowed to incorporate spares like this device. We need to
1016 * find a suitable device to copy partition information from.
1018 * Getting a list of all disk (not partition) devices is
1019 * slightly non-trivial. We could look at /sys/block, but
1020 * that is theoretically due to be removed. Maybe best to use
1021 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1022 * we depend on this directory of 'path' info. But that fails
1023 * to find loop devices and probably others. Maybe don't
1024 * worry about that, they aren't the real target.
1026 * So: check things in /dev/disk/by-path to see if they are in
1027 * a compatible domain, then load the partition table and see
1028 * if it is OK for the new device, and choose the largest
1029 * partition table that fits.
1033 char *chosen
= NULL
;
1034 unsigned long long chosen_size
= 0;
1035 struct supertype
*chosen_st
= NULL
;
1038 dir
= opendir("/dev/disk/by-path");
1041 while ((de
= readdir(dir
)) != NULL
) {
1043 struct dev_policy
*pol2
= NULL
;
1044 struct domainlist
*domlist
= NULL
;
1047 struct supertype
*st2
= NULL
;
1048 char *devname
= NULL
;
1049 unsigned long long devsectors
;
1051 if (de
->d_ino
== 0 ||
1052 de
->d_name
[0] == '.' ||
1053 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1056 ep
= de
->d_name
+ strlen(de
->d_name
);
1057 while (ep
> de
->d_name
&&
1060 if (ep
> de
->d_name
+ 5 &&
1061 strncmp(ep
-5, "-part", 5) == 0)
1062 /* This is a partition - skip it */
1065 pol2
= path_policy(de
->d_name
, type_disk
);
1067 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1068 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1069 /* new device is incompatible with this device. */
1072 domain_free(domlist
);
1075 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1079 fd
= open(devname
, O_RDONLY
);
1082 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1087 st2
= dup_super(st
);
1089 st2
= guess_super_type(fd
, guess_partitions
);
1091 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1095 /* Check domain policy again, this time referring to metadata */
1096 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1097 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1098 /* Incompatible devices for this metadata type */
1100 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1101 /* Some partition types allow sparing, but not
1107 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1108 if (info
.component_size
> devsectors
)
1109 /* This partitioning doesn't fit in the device */
1112 /* This is an acceptable device to copy partition
1113 * metadata from. We could just stop here, but I
1114 * think I want to keep looking incase a larger
1115 * metadata which makes better use of the device can
1118 if (chosen
== NULL
||
1119 chosen_size
< info
.component_size
) {
1120 chosen_size
= info
.component_size
;
1125 chosen_st
->ss
->free_super(chosen_st
);
1134 domain_free(domlist
);
1135 dev_policy_free(pol2
);
1137 st2
->ss
->free_super(st2
);
1149 /* 'chosen' is the best device we can find. Let's write its
1150 * metadata to devname dfd is read-only so don't use that
1152 fd
= open(devname
, O_RDWR
);
1154 chosen_st
->ss
->store_super(chosen_st
, fd
);
1158 chosen_st
->ss
->free_super(chosen_st
);
1163 static int is_bare(int dfd
)
1165 unsigned long long size
= 0;
1166 char bufpad
[4096 + 4096];
1167 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1169 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1170 read(dfd
, buf
, 4096) != 4096)
1173 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1175 if (memcmp(buf
, buf
+1, 4095) != 0)
1178 /* OK, first 4K appear blank, try the end. */
1179 get_dev_size(dfd
, NULL
, &size
);
1180 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1181 read(dfd
, buf
, 4096) != 4096)
1184 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1186 if (memcmp(buf
, buf
+1, 4095) != 0)
1192 /* adding a spare to a regular array is quite different from adding one to
1193 * a set-of-partitions virtual array.
1194 * This function determines which is worth trying and tries as appropriate.
1195 * Arrays are given priority over partitions.
1197 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1198 struct map_ent
*target
,
1199 struct supertype
*st
, int verbose
)
1204 int partitions_ok
= 0;
1208 /* Can only add a spare if device has at least one domain */
1209 if (pol_find(pol
, pol_domain
) == NULL
)
1211 /* And only if some action allows spares */
1212 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1215 /* Now check if the device is bare.
1216 * bare devices can always be added as a spare
1217 * non-bare devices can only be added if spare-same-slot is permitted,
1218 * and this device is replacing a previous device - in which case 'target'
1221 if (!is_bare(dfd
)) {
1222 /* Must have a target and allow same_slot */
1223 /* Later - may allow force_spare without target */
1225 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1226 act_spare_same_slot
)) {
1228 pr_err("%s is not bare, so not "
1229 "considering as a spare\n",
1237 /* It might be OK to add this device to an array - need to see
1238 * what arrays might be candidates.
1241 /* just try try 'array' or 'partition' based on this metadata */
1242 if (st
->ss
->add_to_super
)
1243 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1246 return partition_try_spare(devname
, dfdp
, pol
,
1249 /* No metadata was specified or found so options are open.
1250 * Check for whether any array metadata, or any partition metadata
1251 * might allow adding the spare. This check is just help to avoid
1252 * a more costly scan of all arrays when we can be sure that will
1255 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1256 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1257 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1259 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1260 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1265 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1267 if (rv
!= 0 && partitions_ok
)
1268 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1272 int IncrementalScan(int verbose
)
1274 /* look at every device listed in the 'map' file.
1275 * If one is found that is not running then:
1276 * look in mdadm.conf for bitmap file.
1277 * if one exists, but array has none, add it.
1278 * try to start array in auto-readonly mode
1280 struct map_ent
*mapl
= NULL
;
1282 struct mddev_ident
*devs
, *mddev
;
1286 devs
= conf_get_ident(NULL
);
1288 for (me
= mapl
; me
; me
= me
->next
) {
1289 mdu_array_info_t array
;
1290 mdu_bitmap_file_t bmf
;
1292 int mdfd
= open_dev(me
->devnm
);
1296 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1301 /* Ok, we can try this one. Maybe it needs a bitmap */
1302 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1303 if (mddev
->devname
&& me
->path
1304 && devname_matches(mddev
->devname
, me
->path
))
1306 if (mddev
&& mddev
->bitmap_file
) {
1308 * Note: early kernels will wrongly fail this, so it
1312 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1313 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1315 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1322 pr_err("Added bitmap %s to %s\n",
1323 mddev
->bitmap_file
, me
->path
);
1324 else if (errno
!= EEXIST
)
1325 pr_err("Failed to add bitmap to %s: %s\n",
1326 me
->path
, strerror(errno
));
1329 /* FIXME check for reshape_active and consider not
1332 sra
= sysfs_read(mdfd
, NULL
, 0);
1334 if (sysfs_set_str(sra
, NULL
,
1335 "array_state", "read-auto") == 0) {
1337 pr_err("started array %s\n",
1338 me
->path
?: me
->devnm
);
1340 pr_err("failed to start array %s: %s\n",
1341 me
->path
?: me
->devnm
,
1351 static char *container2devname(char *devname
)
1353 char *mdname
= NULL
;
1355 if (devname
[0] == '/') {
1356 int fd
= open(devname
, O_RDONLY
);
1358 mdname
= xstrdup(fd2devnm(fd
));
1363 struct map_ent
*mp
, *map
= NULL
;
1365 if (!parse_uuid(devname
, uuid
))
1367 mp
= map_by_uuid(&map
, uuid
);
1369 mdname
= xstrdup(mp
->devnm
);
1376 static int Incremental_container(struct supertype
*st
, char *devname
,
1379 /* Collect the contents of this container and for each
1380 * array, choose a device name and assemble the array.
1383 struct mdinfo
*list
;
1385 struct map_ent
*map
= NULL
;
1388 struct mddev_ident
*match
;
1390 struct domainlist
*domains
;
1391 struct map_ent
*smp
;
1397 st
->ss
->getinfo_super(st
, &info
, NULL
);
1399 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1400 info
.container_enough
> 0)
1404 pr_err("not enough devices to start the container\n");
1408 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1409 if (match
== NULL
&& rv
== 2)
1412 /* Need to compute 'trustworthy' */
1414 trustworthy
= LOCAL
;
1415 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1416 trustworthy
= LOCAL
;
1417 else if (st
->ss
->match_home(st
, "any") == 1)
1418 trustworthy
= LOCAL
;
1420 trustworthy
= FOREIGN
;
1422 list
= st
->ss
->container_content(st
, NULL
);
1423 /* when nothing to activate - quit */
1426 for (ra
= list
; ra
; ra
= ra
->next
) {
1428 char chosen_name
[1024];
1430 struct mddev_ident
*match
= NULL
;
1433 /* do not activate arrays blocked by metadata handler */
1434 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1435 pr_err("Cannot activate array %s in %s.\n",
1436 ra
->text_version
, devname
);
1440 mp
= map_by_uuid(&map
, ra
->uuid
);
1443 mdfd
= open_dev(mp
->devnm
);
1445 strcpy(chosen_name
, mp
->path
);
1447 strcpy(chosen_name
, mp
->devnm
);
1450 /* Check in mdadm.conf for container == devname and
1451 * member == ra->text_version after second slash.
1453 char *sub
= strchr(ra
->text_version
+1, '/');
1454 struct mddev_ident
*array_list
;
1457 array_list
= conf_get_ident(NULL
);
1460 for(; array_list
; array_list
= array_list
->next
) {
1462 if (array_list
->member
== NULL
||
1463 array_list
->container
== NULL
)
1465 if (strcmp(array_list
->member
, sub
) != 0)
1467 if (array_list
->uuid_set
&&
1468 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1470 dn
= container2devname(array_list
->container
);
1473 if (strncmp(dn
, ra
->text_version
+1,
1475 ra
->text_version
[strlen(dn
)+1] != '/') {
1480 /* we have a match */
1483 pr_err("match found for member %s\n",
1484 array_list
->member
);
1488 if (match
&& match
->devname
&&
1489 strcasecmp(match
->devname
, "<ignore>") == 0) {
1491 pr_err("array %s/%s is "
1492 "explicitly ignored by mdadm.conf\n",
1493 match
->container
, match
->member
);
1497 trustworthy
= LOCAL
;
1499 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1507 pr_err("failed to open %s: %s.\n",
1508 chosen_name
, strerror(errno
));
1512 assemble_container_content(st
, mdfd
, ra
, c
,
1517 /* don't move spares to container with volume being activated
1518 when all volumes are blocked */
1519 if (ra_all
== ra_blocked
)
1522 /* Now move all suitable spares from spare container */
1523 domains
= domain_from_array(list
, st
->ss
->name
);
1524 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1526 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1527 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1528 /* spare container found */
1529 struct supertype
*sst
=
1530 super_imsm
.match_metadata_desc("imsm");
1531 struct mdinfo
*sinfo
;
1532 unsigned long long min_size
= 0;
1533 if (st
->ss
->min_acceptable_spare_size
)
1534 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1535 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1537 sinfo
= container_choose_spares(sst
, min_size
,
1540 sst
->ss
->free_super(sst
);
1543 struct mdinfo
*disks
= sinfo
->devs
;
1545 /* move spare from spare
1546 * container to currently
1552 makedev(disks
->disk
.major
,
1553 disks
->disk
.minor
)))
1555 disks
= disks
->next
;
1558 pr_err("Added %d spare%s to %s\n",
1559 count
, count
>1?"s":"", devname
);
1565 domain_free(domains
);
1569 static void run_udisks(char *arg1
, char *arg2
)
1574 execl("/usr/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1575 execl("/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1578 while (pid
> 0 && wait(&status
) != pid
)
1583 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1584 * raid arrays, and if so first fail (if needed) and then remove the device.
1586 * @devname - The device we want to remove
1587 * @id_path - name as found in /dev/disk/by-path for this device
1589 * Note: the device name must be a kernel name like "sda", so
1590 * that we can find it in /proc/mdstat
1592 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1596 struct mdstat_ent
*ent
;
1597 struct mddev_dev devlist
;
1602 dprintf(Name
": incremental removal without --path <id_path> "
1603 "lacks the possibility to re-add new device in this "
1606 if (strchr(devname
, '/')) {
1607 pr_err("incremental removal requires a "
1608 "kernel device name, not a file: %s\n", devname
);
1611 ent
= mdstat_by_component(devname
);
1613 pr_err("%s does not appear to be a component "
1614 "of any array\n", devname
);
1617 sysfs_init(&mdi
, -1, ent
->devnm
);
1618 if (sysfs_get_str(&mdi
, NULL
, "array_state",
1619 buf
, sizeof(buf
)) > 0) {
1620 if (strncmp(buf
, "active", 6) == 0 ||
1621 strncmp(buf
, "clean", 5) == 0)
1622 sysfs_set_str(&mdi
, NULL
,
1623 "array_state", "read-auto");
1625 mdfd
= open_dev(ent
->devnm
);
1627 pr_err("Cannot open array %s!!\n", ent
->dev
);
1633 struct map_ent
*map
= NULL
, *me
;
1634 me
= map_by_devnm(&map
, ent
->devnm
);
1636 policy_save_path(id_path
, me
);
1640 memset(&devlist
, 0, sizeof(devlist
));
1641 devlist
.devname
= devname
;
1642 devlist
.disposition
= 'f';
1643 /* for a container, we must fail each member array */
1644 if (ent
->metadata_version
&&
1645 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1646 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1647 struct mdstat_ent
*memb
;
1648 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1649 if (is_container_member(memb
, ent
->dev
)) {
1650 int subfd
= open_dev(memb
->devnm
);
1652 rv
|= Manage_subdevs(
1654 &devlist
, verbose
, 0,
1659 free_mdstat(mdstat
);
1661 rv
|= Manage_subdevs(ent
->dev
, mdfd
, &devlist
,
1662 verbose
, 0, NULL
, 0);
1664 /* Failed due to EBUSY, try to stop the array.
1665 * Give udisks a chance to unmount it first.
1667 int devid
= devnm2devid(ent
->devnm
);
1668 run_udisks("--unmount", map_dev(major(devid
),minor(devid
), 0));
1669 rv
= Manage_runstop(ent
->dev
, mdfd
, -1,
1672 /* At least we can try to trigger a 'remove' */
1673 sysfs_uevent(&mdi
, "remove");
1676 pr_err("Fail to stop %s too.\n", ent
->devnm
);
1679 devlist
.disposition
= 'r';
1680 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
,
1681 verbose
, 0, NULL
, 0);