2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
35 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
36 int mdfd
, char **availp
,
38 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
39 int number
, __u64 events
, int verbose
,
41 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
42 struct map_ent
*target
,
43 struct supertype
*st
, int verbose
);
45 static int Incremental_container(struct supertype
*st
, char *devname
,
47 int verbose
, int runstop
, int autof
,
50 int Incremental(char *devname
, int verbose
, int runstop
,
51 struct supertype
*st
, char *homehost
, int require_homehost
,
52 int autof
, int freeze_reshape
)
54 /* Add this device to an array, creating the array if necessary
55 * and starting the array if sensible or - if runstop>0 - if possible.
57 * This has several steps:
59 * 1/ Check if device is permitted by mdadm.conf, reject if not.
60 * 2/ Find metadata, reject if none appropriate (check
61 * version/name from args)
62 * 3/ Check if there is a match in mdadm.conf
63 * 3a/ if not, check for homehost match. If no match, assemble as
65 * 4/ Determine device number.
66 * - If in mdadm.conf with std name, use that
67 * - UUID in /var/run/mdadm.map use that
68 * - If name is suggestive, use that. unless in use with different uuid.
69 * - Choose a free, high number.
70 * - Use a partitioned device unless strong suggestion not to.
72 * Don't choose partitioned for containers.
73 * 5/ Find out if array already exists
75 * - choose a name, from mdadm.conf or 'name' field in array.
79 * - check one drive in array to make sure metadata is a reasonably
80 * close match. Reject if not (e.g. different type)
82 * 6/ Make sure /var/run/mdadm.map contains this array.
83 * 7/ Is there enough devices to possibly start the array?
84 * For a container, this means running Incremental_container.
85 * 7a/ if not, finish with success.
87 * - read all metadata and arrange devices like -A does
88 * - if number of OK devices match expected, or -R and there are enough,
89 * start the array (auto-readonly).
92 struct mdinfo info
, dinfo
;
93 struct mdinfo
*sra
= NULL
, *d
;
94 struct mddev_ident
*match
;
95 char chosen_name
[1024];
97 struct map_ent
*mp
, *map
= NULL
;
98 int dfd
= -1, mdfd
= -1;
103 mdu_array_info_t ainf
;
104 struct dev_policy
*policy
= NULL
;
105 struct map_ent target_array
;
108 struct createinfo
*ci
= conf_get_create_info();
110 if (stat(devname
, &stb
) < 0) {
112 pr_err("stat failed for %s: %s.\n",
113 devname
, strerror(errno
));
116 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
118 pr_err("%s is not a block device.\n",
122 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
125 pr_err("cannot open %s: %s.\n",
126 devname
, strerror(errno
));
129 /* If the device is a container, we do something very different */
130 if (must_be_container(dfd
)) {
132 st
= super_by_fd(dfd
, NULL
);
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
, homehost
,
142 verbose
, runstop
, autof
,
148 pr_err("%s is not part of an md array.\n",
153 /* 1/ Check if device is permitted by mdadm.conf */
155 if (!conf_test_dev(devname
)) {
157 pr_err("%s not permitted by mdadm.conf.\n",
162 /* 2/ Find metadata, reject if none appropriate (check
163 * version/name from args) */
165 if (fstat(dfd
, &stb
) < 0) {
167 pr_err("fstat failed for %s: %s.\n",
168 devname
, strerror(errno
));
171 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
173 pr_err("%s is not a block device.\n",
178 dinfo
.disk
.major
= major(stb
.st_rdev
);
179 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
181 policy
= disk_policy(&dinfo
);
182 have_target
= policy_check_path(&dinfo
, &target_array
);
184 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
186 pr_err("no recognisable superblock on %s.\n",
188 rv
= try_spare(devname
, &dfd
, policy
,
189 have_target
? &target_array
: NULL
,
193 if (st
->ss
->compare_super
== NULL
||
194 st
->ss
->load_super(st
, dfd
, NULL
)) {
196 pr_err("no RAID superblock on %s.\n",
198 rv
= try_spare(devname
, &dfd
, policy
,
199 have_target
? &target_array
: NULL
,
204 close (dfd
); dfd
= -1;
206 st
->ss
->getinfo_super(st
, &info
, NULL
);
208 /* 3/ Check if there is a match in mdadm.conf */
209 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
210 if (!match
&& rv
== 2)
213 if (match
&& match
->devname
214 && strcasecmp(match
->devname
, "<ignore>") == 0) {
216 pr_err("array containing %s is explicitly"
217 " ignored by mdadm.conf\n",
222 /* 3a/ if not, check for homehost match. If no match, continue
223 * but don't trust the 'name' in the array. Thus a 'random' minor
224 * number will be assigned, and the device name will be based
228 else if (st
->ss
->match_home(st
, homehost
) == 1)
230 else if (st
->ss
->match_home(st
, "any") == 1)
231 trustworthy
= LOCAL_ANY
;
233 trustworthy
= FOREIGN
;
236 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
237 (trustworthy
== LOCAL
))) {
239 pr_err("%s has metadata type %s for which "
240 "auto-assembly is disabled\n",
241 devname
, st
->ss
->name
);
244 if (trustworthy
== LOCAL_ANY
)
247 /* There are three possible sources for 'autof': command line,
248 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
249 * ARRAY takes precedence, then command line, then
252 if (match
&& match
->autof
)
253 autof
= match
->autof
;
257 name_to_use
= info
.name
;
258 if (name_to_use
[0] == 0 &&
259 info
.array
.level
== LEVEL_CONTAINER
) {
260 name_to_use
= info
.text_version
;
261 trustworthy
= METADATA
;
263 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
264 ! require_homehost
&&
265 conf_name_is_free(name_to_use
))
268 /* strip "hostname:" prefix from name if we have decided
269 * to treat it as LOCAL
271 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
272 name_to_use
= strchr(name_to_use
, ':')+1;
274 /* 4/ Check if array exists.
277 pr_err("failed to get exclusive lock on "
279 mp
= map_by_uuid(&map
, info
.uuid
);
281 mdfd
= open_dev(mp
->devnum
);
287 /* Couldn't find an existing array, maybe make a new one */
288 mdfd
= create_mddev(match
? match
->devname
: NULL
,
289 name_to_use
, autof
, trustworthy
, chosen_name
);
294 sysfs_init(&info
, mdfd
, 0);
296 if (set_array_info(mdfd
, st
, &info
) != 0) {
297 pr_err("failed to set array info for %s: %s\n",
298 chosen_name
, strerror(errno
));
304 dinfo
.disk
.major
= major(stb
.st_rdev
);
305 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
306 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
307 pr_err("failed to add %s to %s: %s.\n",
308 devname
, chosen_name
, strerror(errno
));
309 ioctl(mdfd
, STOP_ARRAY
, 0);
313 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
314 GET_OFFSET
| GET_SIZE
));
316 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
317 /* It really should be 'none' - must be old buggy
318 * kernel, and mdadm -I may not be able to complete.
321 ioctl(mdfd
, STOP_ARRAY
, NULL
);
322 pr_err("You have an old buggy kernel which cannot support\n"
323 " --incremental reliably. Aborting.\n");
327 info
.array
.working_disks
= 1;
328 /* 6/ Make sure /var/run/mdadm.map contains this array. */
329 map_update(&map
, fd2devnum(mdfd
),
331 info
.uuid
, chosen_name
);
334 /* - check one drive in array to make sure metadata is a reasonably */
335 /* close match. Reject if not (e.g. different type) */
336 /* - add the device */
340 struct supertype
*st2
;
341 struct mdinfo info2
, *d
;
343 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
344 GET_OFFSET
| GET_SIZE
));
347 strcpy(chosen_name
, mp
->path
);
349 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
351 /* It is generally not OK to add non-spare drives to a
352 * running array as they are probably missing because
353 * they failed. However if runstop is 1, then the
354 * array was possibly started early and our best bet is
355 * to add this anyway.
356 * Also if action policy is re-add or better we allow
358 * This doesn't apply to containers as the 'non-spare'
359 * flag has a different meaning. The test has to happen
360 * at the device level there
362 if (!st
->ss
->external
363 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
364 && ! policy_action_allows(policy
, st
->ss
->name
,
367 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
368 pr_err("not adding %s to active array (without --run) %s\n",
369 devname
, chosen_name
);
379 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
380 sra
->devs
->disk
.minor
);
381 dfd2
= dev_open(dn
, O_RDONLY
);
383 pr_err("unable to open %s\n", devname
);
388 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
389 st
->ss
->compare_super(st
, st2
) != 0) {
390 pr_err("metadata mismatch between %s and "
392 devname
, chosen_name
);
398 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
399 st2
->ss
->free_super(st2
);
400 if (info
.array
.level
!= info2
.array
.level
||
401 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
402 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
403 pr_err("unexpected difference between %s and %s.\n",
404 chosen_name
, devname
);
409 info
.disk
.major
= major(stb
.st_rdev
);
410 info
.disk
.minor
= minor(stb
.st_rdev
);
411 /* add disk needs to know about containers */
412 if (st
->ss
->external
)
413 sra
->array
.level
= LEVEL_CONTAINER
;
414 err
= add_disk(mdfd
, st
, sra
, &info
);
415 if (err
< 0 && errno
== EBUSY
) {
416 /* could be another device present with the same
417 * disk.number. Find and reject any such
419 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
420 info
.events
, verbose
, chosen_name
);
421 err
= add_disk(mdfd
, st
, sra
, &info
);
424 pr_err("failed to add %s to %s: %s.\n",
425 devname
, chosen_name
, strerror(errno
));
429 info
.array
.working_disks
= 0;
430 for (d
= sra
->devs
; d
; d
=d
->next
)
431 info
.array
.working_disks
++;
435 /* 7/ Is there enough devices to possibly start the array? */
436 /* 7a/ if not, finish with success. */
437 if (info
.array
.level
== LEVEL_CONTAINER
) {
438 int devnum
= devnum
; /* defined and used iff ->external */
439 /* Try to assemble within the container */
440 sysfs_uevent(sra
, "change");
442 pr_err("container %s now has %d device%s\n",
443 chosen_name
, info
.array
.working_disks
,
444 info
.array
.working_disks
==1?"":"s");
445 wait_for(chosen_name
, mdfd
);
446 if (st
->ss
->external
)
447 devnum
= fd2devnum(mdfd
);
448 if (st
->ss
->load_container
)
449 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
453 rv
= Incremental_container(st
, chosen_name
, homehost
,
454 verbose
, runstop
, autof
,
458 /* Don't fail the whole -I if a subarray didn't
459 * have enough devices to start yet
462 /* after spare is added, ping monitor for external metadata
463 * so that it can eg. try to rebuild degraded array */
464 if (st
->ss
->external
)
465 ping_monitor_by_id(devnum
);
469 /* We have added something to the array, so need to re-read the
470 * state. Eventually this state should be kept up-to-date as
474 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
475 GET_OFFSET
| GET_SIZE
));
476 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
477 if (enough(info
.array
.level
, info
.array
.raid_disks
,
478 info
.array
.layout
, info
.array
.state
& 1,
481 pr_err("%s attached to %s, not enough to start (%d).\n",
482 devname
, chosen_name
, active_disks
);
488 /* - if number of OK devices match expected, or -R and there */
490 /* + add any bitmap file */
491 /* + start the array (auto-readonly). */
493 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
495 pr_err("%s attached to %s which is already active.\n",
496 devname
, chosen_name
);
502 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
504 /* Let's try to start it */
505 if (match
&& match
->bitmap_file
) {
506 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
508 pr_err("Could not open bitmap file %s.\n",
512 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
514 pr_err("Failed to set bitmapfile for %s.\n",
520 /* Need to remove from the array any devices which
521 * 'count_active' discerned were too old or inappropriate
523 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
524 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
525 remove_disk(mdfd
, st
, sra
, d
);
527 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
528 && trustworthy
!= FOREIGN
)
529 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
531 rv
= sysfs_set_str(sra
, NULL
,
532 "array_state", "read-auto");
535 pr_err("%s attached to %s, which has been started.\n",
536 devname
, chosen_name
);
538 wait_for(chosen_name
, mdfd
);
539 /* We just started the array, so some devices
540 * might have been evicted from the array
541 * because their event counts were too old.
542 * If the action=re-add policy is in-force for
543 * those devices we should re-add them now.
545 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
546 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
547 add_disk(mdfd
, st
, sra
, dsk
) == 0)
548 pr_err("%s re-added to %s\n",
549 dsk
->sys_name
, chosen_name
);
552 pr_err("%s attached to %s, but failed to start: %s.\n",
553 devname
, chosen_name
, strerror(errno
));
558 pr_err("%s attached to %s, not enough to start safely.\n",
559 devname
, chosen_name
);
569 dev_policy_free(policy
);
578 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
579 int number
, __u64 events
, int verbose
,
582 /* Find a device attached to this array with a disk.number of number
583 * and events less than the passed events, and remove the device.
588 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
589 return; /* not safe to remove from active arrays
590 * without thinking more */
592 for (d
= sra
->devs
; d
; d
= d
->next
) {
596 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
597 dfd
= dev_open(dn
, O_RDONLY
);
600 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
604 st
->ss
->getinfo_super(st
, &info
, NULL
);
605 st
->ss
->free_super(st
);
608 if (info
.disk
.number
!= number
||
609 info
.events
>= events
)
612 if (d
->disk
.raid_disk
> -1)
613 sysfs_set_str(sra
, d
, "slot", "none");
614 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
616 pr_err("removing old device %s from %s\n",
617 d
->sys_name
+4, array_name
);
621 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
622 int mdfd
, char **availp
,
623 struct mdinfo
*bestinfo
)
625 /* count how many devices in sra think they are active */
628 __u64 max_events
= 0;
640 for (d
= sra
->devs
; d
; d
= d
->next
)
642 for (d
= sra
->devs
, devnum
=0 ; d
; d
= d
->next
, devnum
++) {
648 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
649 dfd
= dev_open(dn
, O_RDONLY
);
652 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
656 info
.array
.raid_disks
= raid_disks
;
657 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
659 raid_disks
= info
.array
.raid_disks
;
660 avail
= calloc(raid_disks
, 1);
662 pr_err("out of memory.\n");
667 best
= calloc(raid_disks
, sizeof(int));
668 devmap
= calloc(raid_disks
* numdevs
, 1);
670 st
->ss
->getinfo_super(st
, &info
, devmap
);
673 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
677 max_events
= info
.events
;
678 avail
[info
.disk
.raid_disk
] = 2;
679 best
[info
.disk
.raid_disk
] = devnum
;
680 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
681 } else if (info
.events
== max_events
) {
682 avail
[info
.disk
.raid_disk
] = 2;
683 best
[info
.disk
.raid_disk
] = devnum
;
684 } else if (info
.events
== max_events
-1) {
685 if (avail
[info
.disk
.raid_disk
] == 0) {
686 avail
[info
.disk
.raid_disk
] = 1;
687 best
[info
.disk
.raid_disk
] = devnum
;
689 } else if (info
.events
< max_events
- 1)
691 else if (info
.events
== max_events
+1) {
693 max_events
= info
.events
;
694 for (i
=0; i
< raid_disks
; i
++)
697 avail
[info
.disk
.raid_disk
] = 2;
698 best
[info
.disk
.raid_disk
] = devnum
;
699 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
700 } else { /* info.events much bigger */
701 memset(avail
, 0, raid_disks
);
702 max_events
= info
.events
;
703 avail
[info
.disk
.raid_disk
] = 2;
704 best
[info
.disk
.raid_disk
] = devnum
;
705 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
708 st
->ss
->free_super(st
);
712 /* We need to reject any device that thinks the best device is
713 * failed or missing */
714 for (b
= 0; b
< raid_disks
; b
++)
718 for (i
= 0 ; i
< raid_disks
; i
++) {
719 if (i
!= b
&& avail
[i
])
720 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
721 /* This device thinks 'b' is failed -
724 for (d
=sra
->devs
; devnum
; d
= d
->next
)
726 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
737 /* test if container has degraded member(s) */
738 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
740 mdu_array_info_t array
;
742 int max_degraded
= 0;
744 for(; map
; map
= map
->next
) {
745 if (!is_subarray(map
->metadata
) ||
746 devname2devnum(map
->metadata
+1) != me
->devnum
)
748 afd
= open_dev(map
->devnum
);
751 /* most accurate information regarding array degradation */
752 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
753 int degraded
= array
.raid_disks
- array
.active_disks
-
755 if (degraded
> max_degraded
)
756 max_degraded
= degraded
;
760 return (max_degraded
);
763 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
764 struct map_ent
*target
, int bare
,
765 struct supertype
*st
, int verbose
)
767 /* This device doesn't have any md metadata
768 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
769 * If 'st' is not set, then we only know that some metadata allows this,
770 * others possibly don't.
771 * So look for a container or array to attach the device to.
772 * Prefer 'target' if that is set and the array is found.
774 * If st is set, then only arrays of that type are considered
775 * Return 0 on success, or some exit code on failure, probably 1.
779 struct map_ent
*mp
, *map
= NULL
;
780 struct mdinfo
*chosen
= NULL
;
783 if (fstat(dfd
, &stb
) != 0)
787 * Now we need to find a suitable array to add this to.
788 * We only accept arrays that:
790 * - are in the same domains as the device
791 * - are of an size for which the device will be useful
792 * and we choose the one that is the most degraded
795 if (map_lock(&map
)) {
796 pr_err("failed to get exclusive lock on "
800 for (mp
= map
; mp
; mp
= mp
->next
) {
801 struct supertype
*st2
;
802 struct domainlist
*dl
= NULL
;
804 unsigned long long devsize
;
805 unsigned long long component_size
= 0;
807 if (is_subarray(mp
->metadata
))
810 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
812 (st
->minor_version
>= 0 &&
813 st
->minor_version
!= st2
->minor_version
)) {
815 pr_err("not adding %s to %s as metadata type doesn't match\n",
822 sra
= sysfs_read(-1, mp
->devnum
,
823 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
824 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
826 /* Probably a container - no degraded info */
827 sra
= sysfs_read(-1, mp
->devnum
,
828 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
829 GET_COMPONENT
|GET_VERSION
);
831 sra
->array
.failed_disks
= -1;
838 for(i
=0; !st2
&& superlist
[i
]; i
++)
839 st2
= superlist
[i
]->match_metadata_desc(
843 pr_err("not adding %s to %s"
844 " as metadata not recognised.\n",
848 /* Need to double check the 'act_spare' permissions applies
851 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
853 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
854 act_spare_same_slot
))
858 /* update number of failed disks for mostly degraded
859 * container member */
860 if (sra
->array
.failed_disks
== -1)
861 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
863 get_dev_size(dfd
, NULL
, &devsize
);
864 if (sra
->component_size
== 0) {
865 /* true for containers, here we must read superblock
866 * to obtain minimum spare size */
867 struct supertype
*st3
= dup_super(st2
);
868 int mdfd
= open_dev(mp
->devnum
);
873 if (st3
->ss
->load_container
&&
874 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
875 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
876 st3
->ss
->free_super(st3
);
881 if ((sra
->component_size
> 0 &&
882 st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
)
884 (sra
->component_size
== 0 && devsize
< component_size
)) {
886 pr_err("not adding %s to %s as it is too small\n",
890 /* test against target.
891 * If 'target' is set and 'bare' is false, we only accept
892 * arrays/containers that match 'target'.
893 * If 'target' is set and 'bare' is true, we prefer the
894 * array which matches 'target'.
895 * target is considered only if we deal with degraded array
897 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
898 act_spare_same_slot
)) {
899 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
900 memcmp(target
->uuid
, mp
->uuid
,
901 sizeof(target
->uuid
)) == 0 &&
902 sra
->array
.failed_disks
> 0) {
903 /* This is our target!! */
908 /* skip to end so we don't check any more */
918 dl
= domain_from_array(sra
, st2
->ss
->name
);
919 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
920 /* domain test fails */
922 pr_err("not adding %s to %s as"
923 " it is not in a compatible domain\n",
928 /* all tests passed, OK to add to this array */
932 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
946 /* add current device to chosen array as a spare */
947 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
949 struct mddev_dev devlist
;
954 devlist
.writemostly
= 0;
955 devlist
.devname
= devname
;
956 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
958 devlist
.disposition
= 'a';
961 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
967 pr_err("added %s as spare for %s\n",
968 devname
, chosen
->sys_name
);
970 pr_err("failed to add %s as spare for %s\n",
971 devname
, chosen
->sys_name
);
979 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
980 struct supertype
*st
, int verbose
)
982 /* we know that at least one partition virtual-metadata is
983 * allowed to incorporate spares like this device. We need to
984 * find a suitable device to copy partition information from.
986 * Getting a list of all disk (not partition) devices is
987 * slightly non-trivial. We could look at /sys/block, but
988 * that is theoretically due to be removed. Maybe best to use
989 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
990 * we depend on this directory of 'path' info. But that fails
991 * to find loop devices and probably others. Maybe don't
992 * worry about that, they aren't the real target.
994 * So: check things in /dev/disk/by-path to see if they are in
995 * a compatible domain, then load the partition table and see
996 * if it is OK for the new device, and choose the largest
997 * partition table that fits.
1001 char *chosen
= NULL
;
1002 unsigned long long chosen_size
= 0;
1003 struct supertype
*chosen_st
= NULL
;
1006 dir
= opendir("/dev/disk/by-path");
1009 while ((de
= readdir(dir
)) != NULL
) {
1011 struct dev_policy
*pol2
= NULL
;
1012 struct domainlist
*domlist
= NULL
;
1015 struct supertype
*st2
= NULL
;
1016 char *devname
= NULL
;
1017 unsigned long long devsectors
;
1019 if (de
->d_ino
== 0 ||
1020 de
->d_name
[0] == '.' ||
1021 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1024 ep
= de
->d_name
+ strlen(de
->d_name
);
1025 while (ep
> de
->d_name
&&
1028 if (ep
> de
->d_name
+ 5 &&
1029 strncmp(ep
-5, "-part", 5) == 0)
1030 /* This is a partition - skip it */
1033 pol2
= path_policy(de
->d_name
, type_disk
);
1035 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1036 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1037 /* new device is incompatible with this device. */
1040 domain_free(domlist
);
1043 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1047 fd
= open(devname
, O_RDONLY
);
1050 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1055 st2
= dup_super(st
);
1057 st2
= guess_super_type(fd
, guess_partitions
);
1059 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1063 /* Check domain policy again, this time referring to metadata */
1064 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1065 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1066 /* Incompatible devices for this metadata type */
1068 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1069 /* Some partition types allow sparing, but not
1075 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1076 if (info
.component_size
> devsectors
)
1077 /* This partitioning doesn't fit in the device */
1080 /* This is an acceptable device to copy partition
1081 * metadata from. We could just stop here, but I
1082 * think I want to keep looking incase a larger
1083 * metadata which makes better use of the device can
1086 if (chosen
== NULL
||
1087 chosen_size
< info
.component_size
) {
1088 chosen_size
= info
.component_size
;
1093 chosen_st
->ss
->free_super(chosen_st
);
1102 domain_free(domlist
);
1103 dev_policy_free(pol2
);
1105 st2
->ss
->free_super(st2
);
1117 /* 'chosen' is the best device we can find. Let's write its
1118 * metadata to devname dfd is read-only so don't use that
1120 fd
= open(devname
, O_RDWR
);
1122 chosen_st
->ss
->store_super(chosen_st
, fd
);
1126 chosen_st
->ss
->free_super(chosen_st
);
1131 static int is_bare(int dfd
)
1133 unsigned long long size
= 0;
1134 char bufpad
[4096 + 4096];
1135 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1137 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1138 read(dfd
, buf
, 4096) != 4096)
1141 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1143 if (memcmp(buf
, buf
+1, 4095) != 0)
1146 /* OK, first 4K appear blank, try the end. */
1147 get_dev_size(dfd
, NULL
, &size
);
1148 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1149 read(dfd
, buf
, 4096) != 4096)
1152 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1154 if (memcmp(buf
, buf
+1, 4095) != 0)
1160 /* adding a spare to a regular array is quite different from adding one to
1161 * a set-of-partitions virtual array.
1162 * This function determines which is worth trying and tries as appropriate.
1163 * Arrays are given priority over partitions.
1165 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1166 struct map_ent
*target
,
1167 struct supertype
*st
, int verbose
)
1172 int partitions_ok
= 0;
1176 /* Can only add a spare if device has at least one domain */
1177 if (pol_find(pol
, pol_domain
) == NULL
)
1179 /* And only if some action allows spares */
1180 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1183 /* Now check if the device is bare.
1184 * bare devices can always be added as a spare
1185 * non-bare devices can only be added if spare-same-slot is permitted,
1186 * and this device is replacing a previous device - in which case 'target'
1189 if (!is_bare(dfd
)) {
1190 /* Must have a target and allow same_slot */
1191 /* Later - may allow force_spare without target */
1193 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1194 act_spare_same_slot
)) {
1196 pr_err("%s is not bare, so not "
1197 "considering as a spare\n",
1205 /* It might be OK to add this device to an array - need to see
1206 * what arrays might be candidates.
1209 /* just try try 'array' or 'partition' based on this metadata */
1210 if (st
->ss
->add_to_super
)
1211 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1214 return partition_try_spare(devname
, dfdp
, pol
,
1217 /* No metadata was specified or found so options are open.
1218 * Check for whether any array metadata, or any partition metadata
1219 * might allow adding the spare. This check is just help to avoid
1220 * a more costly scan of all arrays when we can be sure that will
1223 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1224 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1225 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1227 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1228 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1233 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1235 if (rv
!= 0 && partitions_ok
)
1236 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1240 int IncrementalScan(int verbose
)
1242 /* look at every device listed in the 'map' file.
1243 * If one is found that is not running then:
1244 * look in mdadm.conf for bitmap file.
1245 * if one exists, but array has none, add it.
1246 * try to start array in auto-readonly mode
1248 struct map_ent
*mapl
= NULL
;
1250 struct mddev_ident
*devs
, *mddev
;
1254 devs
= conf_get_ident(NULL
);
1256 for (me
= mapl
; me
; me
= me
->next
) {
1257 mdu_array_info_t array
;
1258 mdu_bitmap_file_t bmf
;
1260 int mdfd
= open_dev(me
->devnum
);
1264 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1269 /* Ok, we can try this one. Maybe it needs a bitmap */
1270 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1271 if (mddev
->devname
&& me
->path
1272 && devname_matches(mddev
->devname
, me
->path
))
1274 if (mddev
&& mddev
->bitmap_file
) {
1276 * Note: early kernels will wrongly fail this, so it
1280 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1281 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1283 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1290 pr_err("Added bitmap %s to %s\n",
1291 mddev
->bitmap_file
, me
->path
);
1292 else if (errno
!= EEXIST
)
1293 pr_err("Failed to add bitmap to %s: %s\n",
1294 me
->path
, strerror(errno
));
1297 sra
= sysfs_read(mdfd
, 0, 0);
1299 if (sysfs_set_str(sra
, NULL
,
1300 "array_state", "read-auto") == 0) {
1302 pr_err("started array %s\n",
1303 me
->path
?: devnum2devname(me
->devnum
));
1305 pr_err("failed to start array %s: %s\n",
1306 me
->path
?: devnum2devname(me
->devnum
),
1316 static char *container2devname(char *devname
)
1318 char *mdname
= NULL
;
1320 if (devname
[0] == '/') {
1321 int fd
= open(devname
, O_RDONLY
);
1323 mdname
= devnum2devname(fd2devnum(fd
));
1328 struct map_ent
*mp
, *map
= NULL
;
1330 if (!parse_uuid(devname
, uuid
))
1332 mp
= map_by_uuid(&map
, uuid
);
1334 mdname
= devnum2devname(mp
->devnum
);
1341 static int Incremental_container(struct supertype
*st
, char *devname
,
1342 char *homehost
, int verbose
,
1343 int runstop
, int autof
, int freeze_reshape
)
1345 /* Collect the contents of this container and for each
1346 * array, choose a device name and assemble the array.
1349 struct mdinfo
*list
;
1351 struct map_ent
*map
= NULL
;
1354 struct mddev_ident
*match
;
1356 struct domainlist
*domains
;
1357 struct map_ent
*smp
;
1363 st
->ss
->getinfo_super(st
, &info
, NULL
);
1365 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1366 info
.container_enough
> 0)
1370 pr_err("not enough devices to start the container\n");
1374 match
= conf_match(st
, &info
, devname
, verbose
, &rv
);
1375 if (match
== NULL
&& rv
== 2)
1378 /* Need to compute 'trustworthy' */
1380 trustworthy
= LOCAL
;
1381 else if (st
->ss
->match_home(st
, homehost
) == 1)
1382 trustworthy
= LOCAL
;
1383 else if (st
->ss
->match_home(st
, "any") == 1)
1384 trustworthy
= LOCAL
;
1386 trustworthy
= FOREIGN
;
1388 list
= st
->ss
->container_content(st
, NULL
);
1389 /* when nothing to activate - quit */
1392 for (ra
= list
; ra
; ra
= ra
->next
) {
1394 char chosen_name
[1024];
1396 struct mddev_ident
*match
= NULL
;
1399 /* do not activate arrays blocked by metadata handler */
1400 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1401 pr_err("Cannot activate array %s in %s.\n",
1402 ra
->text_version
, devname
);
1406 mp
= map_by_uuid(&map
, ra
->uuid
);
1409 mdfd
= open_dev(mp
->devnum
);
1411 strcpy(chosen_name
, mp
->path
);
1413 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1416 /* Check in mdadm.conf for container == devname and
1417 * member == ra->text_version after second slash.
1419 char *sub
= strchr(ra
->text_version
+1, '/');
1420 struct mddev_ident
*array_list
;
1423 array_list
= conf_get_ident(NULL
);
1426 for(; array_list
; array_list
= array_list
->next
) {
1428 if (array_list
->member
== NULL
||
1429 array_list
->container
== NULL
)
1431 if (strcmp(array_list
->member
, sub
) != 0)
1433 if (array_list
->uuid_set
&&
1434 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1436 dn
= container2devname(array_list
->container
);
1439 if (strncmp(dn
, ra
->text_version
+1,
1441 ra
->text_version
[strlen(dn
)+1] != '/') {
1446 /* we have a match */
1449 pr_err("match found for member %s\n",
1450 array_list
->member
);
1454 if (match
&& match
->devname
&&
1455 strcasecmp(match
->devname
, "<ignore>") == 0) {
1457 pr_err("array %s/%s is "
1458 "explicitly ignored by mdadm.conf\n",
1459 match
->container
, match
->member
);
1463 trustworthy
= LOCAL
;
1465 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1473 pr_err("failed to open %s: %s.\n",
1474 chosen_name
, strerror(errno
));
1478 assemble_container_content(st
, mdfd
, ra
, runstop
,
1479 chosen_name
, verbose
, NULL
,
1484 /* don't move spares to container with volume being activated
1485 when all volumes are blocked */
1486 if (ra_all
== ra_blocked
)
1489 /* Now move all suitable spares from spare container */
1490 domains
= domain_from_array(list
, st
->ss
->name
);
1491 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1493 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1494 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1495 /* spare container found */
1496 struct supertype
*sst
=
1497 super_imsm
.match_metadata_desc("imsm");
1498 struct mdinfo
*sinfo
;
1499 unsigned long long min_size
= 0;
1500 if (st
->ss
->min_acceptable_spare_size
)
1501 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1502 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1504 sinfo
= container_choose_spares(sst
, min_size
,
1507 sst
->ss
->free_super(sst
);
1510 struct mdinfo
*disks
= sinfo
->devs
;
1512 /* move spare from spare
1513 * container to currently
1519 makedev(disks
->disk
.major
,
1520 disks
->disk
.minor
)))
1522 disks
= disks
->next
;
1525 pr_err("Added %d spare%s to %s\n",
1526 count
, count
>1?"s":"", devname
);
1532 domain_free(domains
);
1537 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1538 * raid arrays, and if so first fail (if needed) and then remove the device.
1540 * @devname - The device we want to remove
1541 * @id_path - name as found in /dev/disk/by-path for this device
1543 * Note: the device name must be a kernel name like "sda", so
1544 * that we can find it in /proc/mdstat
1546 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1550 struct mdstat_ent
*ent
;
1551 struct mddev_dev devlist
;
1554 dprintf(Name
": incremental removal without --path <id_path> "
1555 "lacks the possibility to re-add new device in this "
1558 if (strchr(devname
, '/')) {
1559 pr_err("incremental removal requires a "
1560 "kernel device name, not a file: %s\n", devname
);
1563 ent
= mdstat_by_component(devname
);
1565 pr_err("%s does not appear to be a component "
1566 "of any array\n", devname
);
1569 mdfd
= open_dev(ent
->devnum
);
1571 pr_err("Cannot open array %s!!\n", ent
->dev
);
1577 struct map_ent
*map
= NULL
, *me
;
1578 me
= map_by_devnum(&map
, ent
->devnum
);
1580 policy_save_path(id_path
, me
);
1584 memset(&devlist
, 0, sizeof(devlist
));
1585 devlist
.devname
= devname
;
1586 devlist
.disposition
= 'f';
1587 /* for a container, we must fail each member array */
1588 if (ent
->metadata_version
&&
1589 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1590 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1591 struct mdstat_ent
*memb
;
1592 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1593 if (is_container_member(memb
, ent
->dev
)) {
1594 int subfd
= open_dev(memb
->devnum
);
1596 Manage_subdevs(memb
->dev
, subfd
,
1597 &devlist
, verbose
, 0,
1602 free_mdstat(mdstat
);
1604 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1605 devlist
.disposition
= 'r';
1606 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);