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
35 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
36 int mdfd
, char **availp
,
38 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
39 int number
, __u64 events
, int verbose
,
41 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
42 struct map_ent
*target
,
43 struct supertype
*st
, int verbose
);
45 static int Incremental_container(struct supertype
*st
, char *devname
,
48 int Incremental(char *devname
, struct context
*c
,
51 /* Add this device to an array, creating the array if necessary
52 * and starting the array if sensible or - if runstop>0 - if possible.
54 * This has several steps:
56 * 1/ Check if device is permitted by mdadm.conf, reject if not.
57 * 2/ Find metadata, reject if none appropriate (check
58 * version/name from args)
59 * 3/ Check if there is a match in mdadm.conf
60 * 3a/ if not, check for homehost match. If no match, assemble as
62 * 4/ Determine device number.
63 * - If in mdadm.conf with std name, use that
64 * - UUID in /var/run/mdadm.map use that
65 * - If name is suggestive, use that. unless in use with different uuid.
66 * - Choose a free, high number.
67 * - Use a partitioned device unless strong suggestion not to.
69 * Don't choose partitioned for containers.
70 * 5/ Find out if array already exists
72 * - choose a name, from mdadm.conf or 'name' field in array.
76 * - check one drive in array to make sure metadata is a reasonably
77 * close match. Reject if not (e.g. different type)
79 * 6/ Make sure /var/run/mdadm.map contains this array.
80 * 7/ Is there enough devices to possibly start the array?
81 * For a container, this means running Incremental_container.
82 * 7a/ if not, finish with success.
84 * - read all metadata and arrange devices like -A does
85 * - if number of OK devices match expected, or -R and there are enough,
86 * start the array (auto-readonly).
89 struct mdinfo info
, dinfo
;
90 struct mdinfo
*sra
= NULL
, *d
;
91 struct mddev_ident
*match
;
92 char chosen_name
[1024];
94 struct map_ent
*mp
, *map
= NULL
;
95 int dfd
= -1, mdfd
= -1;
100 mdu_array_info_t ainf
;
101 struct dev_policy
*policy
= NULL
;
102 struct map_ent target_array
;
105 struct createinfo
*ci
= conf_get_create_info();
107 if (stat(devname
, &stb
) < 0) {
109 pr_err("stat failed for %s: %s.\n",
110 devname
, strerror(errno
));
113 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
115 pr_err("%s is not a block device.\n",
119 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
122 pr_err("cannot open %s: %s.\n",
123 devname
, strerror(errno
));
126 /* If the device is a container, we do something very different */
127 if (must_be_container(dfd
)) {
129 st
= super_by_fd(dfd
, NULL
);
130 if (st
&& st
->ss
->load_container
)
131 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
134 if (!rv
&& st
->ss
->container_content
) {
136 pr_err("failed to get "
137 "exclusive lock on mapfile\n");
138 rv
= Incremental_container(st
, devname
, c
);
143 pr_err("%s is not part of an md array.\n",
148 /* 1/ Check if device is permitted by mdadm.conf */
150 if (!conf_test_dev(devname
)) {
152 pr_err("%s not permitted by mdadm.conf.\n",
157 /* 2/ Find metadata, reject if none appropriate (check
158 * version/name from args) */
160 if (fstat(dfd
, &stb
) < 0) {
162 pr_err("fstat failed for %s: %s.\n",
163 devname
, strerror(errno
));
166 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
168 pr_err("%s is not a block device.\n",
173 dinfo
.disk
.major
= major(stb
.st_rdev
);
174 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
176 policy
= disk_policy(&dinfo
);
177 have_target
= policy_check_path(&dinfo
, &target_array
);
179 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
181 pr_err("no recognisable superblock on %s.\n",
183 rv
= try_spare(devname
, &dfd
, policy
,
184 have_target
? &target_array
: NULL
,
188 if (st
->ss
->compare_super
== NULL
||
189 st
->ss
->load_super(st
, dfd
, NULL
)) {
191 pr_err("no RAID superblock on %s.\n",
193 rv
= try_spare(devname
, &dfd
, policy
,
194 have_target
? &target_array
: NULL
,
199 close (dfd
); dfd
= -1;
201 st
->ss
->getinfo_super(st
, &info
, NULL
);
203 /* 3/ Check if there is a match in mdadm.conf */
204 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
205 if (!match
&& rv
== 2)
208 if (match
&& match
->devname
209 && strcasecmp(match
->devname
, "<ignore>") == 0) {
211 pr_err("array containing %s is explicitly"
212 " ignored by mdadm.conf\n",
217 /* 3a/ if not, check for homehost match. If no match, continue
218 * but don't trust the 'name' in the array. Thus a 'random' minor
219 * number will be assigned, and the device name will be based
223 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
225 else if (st
->ss
->match_home(st
, "any") == 1)
226 trustworthy
= LOCAL_ANY
;
228 trustworthy
= FOREIGN
;
230 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
231 (trustworthy
== LOCAL
))) {
233 pr_err("%s has metadata type %s for which "
234 "auto-assembly is disabled\n",
235 devname
, st
->ss
->name
);
238 if (trustworthy
== LOCAL_ANY
)
241 /* There are three possible sources for 'autof': command line,
242 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
243 * ARRAY takes precedence, then command line, then
246 if (match
&& match
->autof
)
247 c
->autof
= match
->autof
;
249 c
->autof
= ci
->autof
;
251 name_to_use
= info
.name
;
252 if (name_to_use
[0] == 0 &&
253 info
.array
.level
== LEVEL_CONTAINER
) {
254 name_to_use
= info
.text_version
;
255 trustworthy
= METADATA
;
257 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
258 ! c
->require_homehost
&&
259 conf_name_is_free(name_to_use
))
262 /* strip "hostname:" prefix from name if we have decided
263 * to treat it as LOCAL
265 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
266 name_to_use
= strchr(name_to_use
, ':')+1;
268 /* 4/ Check if array exists.
271 pr_err("failed to get exclusive lock on "
273 mp
= map_by_uuid(&map
, info
.uuid
);
275 mdfd
= open_dev(mp
->devnum
);
281 /* Couldn't find an existing array, maybe make a new one */
282 mdfd
= create_mddev(match
? match
->devname
: NULL
,
283 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
288 sysfs_init(&info
, mdfd
, 0);
290 if (set_array_info(mdfd
, st
, &info
) != 0) {
291 pr_err("failed to set array info for %s: %s\n",
292 chosen_name
, strerror(errno
));
298 dinfo
.disk
.major
= major(stb
.st_rdev
);
299 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
300 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
301 pr_err("failed to add %s to %s: %s.\n",
302 devname
, chosen_name
, strerror(errno
));
303 ioctl(mdfd
, STOP_ARRAY
, 0);
307 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
308 GET_OFFSET
| GET_SIZE
));
310 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
311 /* It really should be 'none' - must be old buggy
312 * kernel, and mdadm -I may not be able to complete.
315 ioctl(mdfd
, STOP_ARRAY
, NULL
);
316 pr_err("You have an old buggy kernel which cannot support\n"
317 " --incremental reliably. Aborting.\n");
321 info
.array
.working_disks
= 1;
322 /* 6/ Make sure /var/run/mdadm.map contains this array. */
323 map_update(&map
, fd2devnum(mdfd
),
325 info
.uuid
, chosen_name
);
328 /* - check one drive in array to make sure metadata is a reasonably */
329 /* close match. Reject if not (e.g. different type) */
330 /* - add the device */
334 struct supertype
*st2
;
335 struct mdinfo info2
, *d
;
337 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
338 GET_OFFSET
| GET_SIZE
));
341 strcpy(chosen_name
, mp
->path
);
343 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
345 /* It is generally not OK to add non-spare drives to a
346 * running array as they are probably missing because
347 * they failed. However if runstop is 1, then the
348 * array was possibly started early and our best bet is
349 * to add this anyway.
350 * Also if action policy is re-add or better we allow
352 * This doesn't apply to containers as the 'non-spare'
353 * flag has a different meaning. The test has to happen
354 * at the device level there
356 if (!st
->ss
->external
357 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
358 && ! policy_action_allows(policy
, st
->ss
->name
,
361 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
362 pr_err("not adding %s to active array (without --run) %s\n",
363 devname
, chosen_name
);
373 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
374 sra
->devs
->disk
.minor
);
375 dfd2
= dev_open(dn
, O_RDONLY
);
377 pr_err("unable to open %s\n", devname
);
382 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
383 st
->ss
->compare_super(st
, st2
) != 0) {
384 pr_err("metadata mismatch between %s and "
386 devname
, chosen_name
);
392 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
393 st2
->ss
->free_super(st2
);
394 if (info
.array
.level
!= info2
.array
.level
||
395 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
396 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
397 pr_err("unexpected difference between %s and %s.\n",
398 chosen_name
, devname
);
403 info
.disk
.major
= major(stb
.st_rdev
);
404 info
.disk
.minor
= minor(stb
.st_rdev
);
405 /* add disk needs to know about containers */
406 if (st
->ss
->external
)
407 sra
->array
.level
= LEVEL_CONTAINER
;
408 err
= add_disk(mdfd
, st
, sra
, &info
);
409 if (err
< 0 && errno
== EBUSY
) {
410 /* could be another device present with the same
411 * disk.number. Find and reject any such
413 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
414 info
.events
, c
->verbose
, chosen_name
);
415 err
= add_disk(mdfd
, st
, sra
, &info
);
418 pr_err("failed to add %s to %s: %s.\n",
419 devname
, chosen_name
, strerror(errno
));
423 info
.array
.working_disks
= 0;
424 for (d
= sra
->devs
; d
; d
=d
->next
)
425 info
.array
.working_disks
++;
429 /* 7/ Is there enough devices to possibly start the array? */
430 /* 7a/ if not, finish with success. */
431 if (info
.array
.level
== LEVEL_CONTAINER
) {
432 int devnum
= devnum
; /* defined and used iff ->external */
433 /* Try to assemble within the container */
434 sysfs_uevent(sra
, "change");
436 pr_err("container %s now has %d device%s\n",
437 chosen_name
, info
.array
.working_disks
,
438 info
.array
.working_disks
== 1?"":"s");
439 wait_for(chosen_name
, mdfd
);
440 if (st
->ss
->external
)
441 devnum
= fd2devnum(mdfd
);
442 if (st
->ss
->load_container
)
443 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
447 rv
= Incremental_container(st
, chosen_name
, c
);
450 /* Don't fail the whole -I if a subarray didn't
451 * have enough devices to start yet
454 /* after spare is added, ping monitor for external metadata
455 * so that it can eg. try to rebuild degraded array */
456 if (st
->ss
->external
)
457 ping_monitor_by_id(devnum
);
461 /* We have added something to the array, so need to re-read the
462 * state. Eventually this state should be kept up-to-date as
466 sra
= sysfs_read(mdfd
, -1, (GET_DEVS
| GET_STATE
|
467 GET_OFFSET
| GET_SIZE
));
468 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
469 if (enough(info
.array
.level
, info
.array
.raid_disks
,
470 info
.array
.layout
, info
.array
.state
& 1,
473 pr_err("%s attached to %s, not enough to start (%d).\n",
474 devname
, chosen_name
, active_disks
);
480 /* - if number of OK devices match expected, or -R and there */
482 /* + add any bitmap file */
483 /* + start the array (auto-readonly). */
485 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
487 pr_err("%s attached to %s which is already active.\n",
488 devname
, chosen_name
);
494 if (c
->runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
496 /* Let's try to start it */
497 if (match
&& match
->bitmap_file
) {
498 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
500 pr_err("Could not open bitmap file %s.\n",
504 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
506 pr_err("Failed to set bitmapfile for %s.\n",
512 /* Need to remove from the array any devices which
513 * 'count_active' discerned were too old or inappropriate
515 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
516 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
517 remove_disk(mdfd
, st
, sra
, d
);
519 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
520 && trustworthy
!= FOREIGN
)
521 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
523 rv
= sysfs_set_str(sra
, NULL
,
524 "array_state", "read-auto");
527 pr_err("%s attached to %s, which has been started.\n",
528 devname
, chosen_name
);
530 wait_for(chosen_name
, mdfd
);
531 /* We just started the array, so some devices
532 * might have been evicted from the array
533 * because their event counts were too old.
534 * If the action=re-add policy is in-force for
535 * those devices we should re-add them now.
537 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
538 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
539 add_disk(mdfd
, st
, sra
, dsk
) == 0)
540 pr_err("%s re-added to %s\n",
541 dsk
->sys_name
, chosen_name
);
544 pr_err("%s attached to %s, but failed to start: %s.\n",
545 devname
, chosen_name
, strerror(errno
));
550 pr_err("%s attached to %s, not enough to start safely.\n",
551 devname
, chosen_name
);
561 dev_policy_free(policy
);
570 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
571 int number
, __u64 events
, int verbose
,
574 /* Find a device attached to this array with a disk.number of number
575 * and events less than the passed events, and remove the device.
580 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
581 return; /* not safe to remove from active arrays
582 * without thinking more */
584 for (d
= sra
->devs
; d
; d
= d
->next
) {
588 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
589 dfd
= dev_open(dn
, O_RDONLY
);
592 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
596 st
->ss
->getinfo_super(st
, &info
, NULL
);
597 st
->ss
->free_super(st
);
600 if (info
.disk
.number
!= number
||
601 info
.events
>= events
)
604 if (d
->disk
.raid_disk
> -1)
605 sysfs_set_str(sra
, d
, "slot", "none");
606 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
608 pr_err("removing old device %s from %s\n",
609 d
->sys_name
+4, array_name
);
613 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
614 int mdfd
, char **availp
,
615 struct mdinfo
*bestinfo
)
617 /* count how many devices in sra think they are active */
620 __u64 max_events
= 0;
632 for (d
= sra
->devs
; d
; d
= d
->next
)
634 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
640 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
641 dfd
= dev_open(dn
, O_RDONLY
);
644 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
648 info
.array
.raid_disks
= raid_disks
;
649 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
651 raid_disks
= info
.array
.raid_disks
;
652 avail
= xcalloc(raid_disks
, 1);
655 best
= xcalloc(raid_disks
, sizeof(int));
656 devmap
= xcalloc(raid_disks
, numdevs
);
658 st
->ss
->getinfo_super(st
, &info
, devmap
);
661 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
665 max_events
= info
.events
;
666 avail
[info
.disk
.raid_disk
] = 2;
667 best
[info
.disk
.raid_disk
] = devnum
;
668 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
669 } else if (info
.events
== max_events
) {
670 avail
[info
.disk
.raid_disk
] = 2;
671 best
[info
.disk
.raid_disk
] = devnum
;
672 } else if (info
.events
== max_events
-1) {
673 if (avail
[info
.disk
.raid_disk
] == 0) {
674 avail
[info
.disk
.raid_disk
] = 1;
675 best
[info
.disk
.raid_disk
] = devnum
;
677 } else if (info
.events
< max_events
- 1)
679 else if (info
.events
== max_events
+1) {
681 max_events
= info
.events
;
682 for (i
= 0; i
< raid_disks
; i
++)
685 avail
[info
.disk
.raid_disk
] = 2;
686 best
[info
.disk
.raid_disk
] = devnum
;
687 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
688 } else { /* info.events much bigger */
689 memset(avail
, 0, raid_disks
);
690 max_events
= info
.events
;
691 avail
[info
.disk
.raid_disk
] = 2;
692 best
[info
.disk
.raid_disk
] = devnum
;
693 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
696 st
->ss
->free_super(st
);
700 /* We need to reject any device that thinks the best device is
701 * failed or missing */
702 for (b
= 0; b
< raid_disks
; b
++)
706 for (i
= 0 ; i
< raid_disks
; i
++) {
707 if (i
!= b
&& avail
[i
])
708 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
709 /* This device thinks 'b' is failed -
712 for (d
=sra
->devs
; devnum
; d
= d
->next
)
714 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
725 /* test if container has degraded member(s) */
726 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
728 mdu_array_info_t array
;
730 int max_degraded
= 0;
732 for(; map
; map
= map
->next
) {
733 if (!is_subarray(map
->metadata
) ||
734 devname2devnum(map
->metadata
+1) != me
->devnum
)
736 afd
= open_dev(map
->devnum
);
739 /* most accurate information regarding array degradation */
740 if (ioctl(afd
, GET_ARRAY_INFO
, &array
) >= 0) {
741 int degraded
= array
.raid_disks
- array
.active_disks
-
743 if (degraded
> max_degraded
)
744 max_degraded
= degraded
;
748 return (max_degraded
);
751 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
752 struct map_ent
*target
, int bare
,
753 struct supertype
*st
, int verbose
)
755 /* This device doesn't have any md metadata
756 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
757 * If 'st' is not set, then we only know that some metadata allows this,
758 * others possibly don't.
759 * So look for a container or array to attach the device to.
760 * Prefer 'target' if that is set and the array is found.
762 * If st is set, then only arrays of that type are considered
763 * Return 0 on success, or some exit code on failure, probably 1.
767 struct map_ent
*mp
, *map
= NULL
;
768 struct mdinfo
*chosen
= NULL
;
771 if (fstat(dfd
, &stb
) != 0)
775 * Now we need to find a suitable array to add this to.
776 * We only accept arrays that:
778 * - are in the same domains as the device
779 * - are of an size for which the device will be useful
780 * and we choose the one that is the most degraded
783 if (map_lock(&map
)) {
784 pr_err("failed to get exclusive lock on "
788 for (mp
= map
; mp
; mp
= mp
->next
) {
789 struct supertype
*st2
;
790 struct domainlist
*dl
= NULL
;
792 unsigned long long devsize
;
793 unsigned long long component_size
= 0;
795 if (is_subarray(mp
->metadata
))
798 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
800 (st
->minor_version
>= 0 &&
801 st
->minor_version
!= st2
->minor_version
)) {
803 pr_err("not adding %s to %s as metadata type doesn't match\n",
810 sra
= sysfs_read(-1, mp
->devnum
,
811 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
812 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
814 /* Probably a container - no degraded info */
815 sra
= sysfs_read(-1, mp
->devnum
,
816 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
817 GET_COMPONENT
|GET_VERSION
);
819 sra
->array
.failed_disks
= -1;
826 for(i
= 0; !st2
&& superlist
[i
]; i
++)
827 st2
= superlist
[i
]->match_metadata_desc(
831 pr_err("not adding %s to %s"
832 " as metadata not recognised.\n",
836 /* Need to double check the 'act_spare' permissions applies
839 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
841 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
842 act_spare_same_slot
))
846 /* update number of failed disks for mostly degraded
847 * container member */
848 if (sra
->array
.failed_disks
== -1)
849 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
851 get_dev_size(dfd
, NULL
, &devsize
);
852 if (sra
->component_size
== 0) {
853 /* true for containers, here we must read superblock
854 * to obtain minimum spare size */
855 struct supertype
*st3
= dup_super(st2
);
856 int mdfd
= open_dev(mp
->devnum
);
861 if (st3
->ss
->load_container
&&
862 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
863 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
864 st3
->ss
->free_super(st3
);
869 if ((sra
->component_size
> 0 &&
870 st2
->ss
->avail_size(st2
, devsize
,
872 ? sra
->devs
->data_offset
874 < sra
->component_size
)
876 (sra
->component_size
== 0 && devsize
< component_size
)) {
878 pr_err("not adding %s to %s as it is too small\n",
882 /* test against target.
883 * If 'target' is set and 'bare' is false, we only accept
884 * arrays/containers that match 'target'.
885 * If 'target' is set and 'bare' is true, we prefer the
886 * array which matches 'target'.
887 * target is considered only if we deal with degraded array
889 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
890 act_spare_same_slot
)) {
891 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
892 memcmp(target
->uuid
, mp
->uuid
,
893 sizeof(target
->uuid
)) == 0 &&
894 sra
->array
.failed_disks
> 0) {
895 /* This is our target!! */
900 /* skip to end so we don't check any more */
910 dl
= domain_from_array(sra
, st2
->ss
->name
);
911 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
912 /* domain test fails */
914 pr_err("not adding %s to %s as"
915 " it is not in a compatible domain\n",
920 /* all tests passed, OK to add to this array */
924 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
938 /* add current device to chosen array as a spare */
939 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
941 struct mddev_dev devlist
;
945 devlist
.writemostly
= 0;
946 devlist
.devname
= devname
;
947 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
949 devlist
.disposition
= 'a';
952 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
958 pr_err("added %s as spare for %s\n",
959 devname
, chosen
->sys_name
);
961 pr_err("failed to add %s as spare for %s\n",
962 devname
, chosen
->sys_name
);
970 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
971 struct supertype
*st
, int verbose
)
973 /* we know that at least one partition virtual-metadata is
974 * allowed to incorporate spares like this device. We need to
975 * find a suitable device to copy partition information from.
977 * Getting a list of all disk (not partition) devices is
978 * slightly non-trivial. We could look at /sys/block, but
979 * that is theoretically due to be removed. Maybe best to use
980 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
981 * we depend on this directory of 'path' info. But that fails
982 * to find loop devices and probably others. Maybe don't
983 * worry about that, they aren't the real target.
985 * So: check things in /dev/disk/by-path to see if they are in
986 * a compatible domain, then load the partition table and see
987 * if it is OK for the new device, and choose the largest
988 * partition table that fits.
993 unsigned long long chosen_size
= 0;
994 struct supertype
*chosen_st
= NULL
;
997 dir
= opendir("/dev/disk/by-path");
1000 while ((de
= readdir(dir
)) != NULL
) {
1002 struct dev_policy
*pol2
= NULL
;
1003 struct domainlist
*domlist
= NULL
;
1006 struct supertype
*st2
= NULL
;
1007 char *devname
= NULL
;
1008 unsigned long long devsectors
;
1010 if (de
->d_ino
== 0 ||
1011 de
->d_name
[0] == '.' ||
1012 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1015 ep
= de
->d_name
+ strlen(de
->d_name
);
1016 while (ep
> de
->d_name
&&
1019 if (ep
> de
->d_name
+ 5 &&
1020 strncmp(ep
-5, "-part", 5) == 0)
1021 /* This is a partition - skip it */
1024 pol2
= path_policy(de
->d_name
, type_disk
);
1026 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1027 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1028 /* new device is incompatible with this device. */
1031 domain_free(domlist
);
1034 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1038 fd
= open(devname
, O_RDONLY
);
1041 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1046 st2
= dup_super(st
);
1048 st2
= guess_super_type(fd
, guess_partitions
);
1050 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1054 /* Check domain policy again, this time referring to metadata */
1055 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1056 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1057 /* Incompatible devices for this metadata type */
1059 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1060 /* Some partition types allow sparing, but not
1066 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1067 if (info
.component_size
> devsectors
)
1068 /* This partitioning doesn't fit in the device */
1071 /* This is an acceptable device to copy partition
1072 * metadata from. We could just stop here, but I
1073 * think I want to keep looking incase a larger
1074 * metadata which makes better use of the device can
1077 if (chosen
== NULL
||
1078 chosen_size
< info
.component_size
) {
1079 chosen_size
= info
.component_size
;
1084 chosen_st
->ss
->free_super(chosen_st
);
1093 domain_free(domlist
);
1094 dev_policy_free(pol2
);
1096 st2
->ss
->free_super(st2
);
1108 /* 'chosen' is the best device we can find. Let's write its
1109 * metadata to devname dfd is read-only so don't use that
1111 fd
= open(devname
, O_RDWR
);
1113 chosen_st
->ss
->store_super(chosen_st
, fd
);
1117 chosen_st
->ss
->free_super(chosen_st
);
1122 static int is_bare(int dfd
)
1124 unsigned long long size
= 0;
1125 char bufpad
[4096 + 4096];
1126 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1128 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1129 read(dfd
, buf
, 4096) != 4096)
1132 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1134 if (memcmp(buf
, buf
+1, 4095) != 0)
1137 /* OK, first 4K appear blank, try the end. */
1138 get_dev_size(dfd
, NULL
, &size
);
1139 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1140 read(dfd
, buf
, 4096) != 4096)
1143 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1145 if (memcmp(buf
, buf
+1, 4095) != 0)
1151 /* adding a spare to a regular array is quite different from adding one to
1152 * a set-of-partitions virtual array.
1153 * This function determines which is worth trying and tries as appropriate.
1154 * Arrays are given priority over partitions.
1156 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1157 struct map_ent
*target
,
1158 struct supertype
*st
, int verbose
)
1163 int partitions_ok
= 0;
1167 /* Can only add a spare if device has at least one domain */
1168 if (pol_find(pol
, pol_domain
) == NULL
)
1170 /* And only if some action allows spares */
1171 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1174 /* Now check if the device is bare.
1175 * bare devices can always be added as a spare
1176 * non-bare devices can only be added if spare-same-slot is permitted,
1177 * and this device is replacing a previous device - in which case 'target'
1180 if (!is_bare(dfd
)) {
1181 /* Must have a target and allow same_slot */
1182 /* Later - may allow force_spare without target */
1184 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1185 act_spare_same_slot
)) {
1187 pr_err("%s is not bare, so not "
1188 "considering as a spare\n",
1196 /* It might be OK to add this device to an array - need to see
1197 * what arrays might be candidates.
1200 /* just try try 'array' or 'partition' based on this metadata */
1201 if (st
->ss
->add_to_super
)
1202 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1205 return partition_try_spare(devname
, dfdp
, pol
,
1208 /* No metadata was specified or found so options are open.
1209 * Check for whether any array metadata, or any partition metadata
1210 * might allow adding the spare. This check is just help to avoid
1211 * a more costly scan of all arrays when we can be sure that will
1214 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1215 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1216 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1218 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1219 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1224 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1226 if (rv
!= 0 && partitions_ok
)
1227 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1231 int IncrementalScan(int verbose
)
1233 /* look at every device listed in the 'map' file.
1234 * If one is found that is not running then:
1235 * look in mdadm.conf for bitmap file.
1236 * if one exists, but array has none, add it.
1237 * try to start array in auto-readonly mode
1239 struct map_ent
*mapl
= NULL
;
1241 struct mddev_ident
*devs
, *mddev
;
1245 devs
= conf_get_ident(NULL
);
1247 for (me
= mapl
; me
; me
= me
->next
) {
1248 mdu_array_info_t array
;
1249 mdu_bitmap_file_t bmf
;
1251 int mdfd
= open_dev(me
->devnum
);
1255 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1260 /* Ok, we can try this one. Maybe it needs a bitmap */
1261 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1262 if (mddev
->devname
&& me
->path
1263 && devname_matches(mddev
->devname
, me
->path
))
1265 if (mddev
&& mddev
->bitmap_file
) {
1267 * Note: early kernels will wrongly fail this, so it
1271 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1272 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1274 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1281 pr_err("Added bitmap %s to %s\n",
1282 mddev
->bitmap_file
, me
->path
);
1283 else if (errno
!= EEXIST
)
1284 pr_err("Failed to add bitmap to %s: %s\n",
1285 me
->path
, strerror(errno
));
1288 sra
= sysfs_read(mdfd
, 0, 0);
1290 if (sysfs_set_str(sra
, NULL
,
1291 "array_state", "read-auto") == 0) {
1293 pr_err("started array %s\n",
1294 me
->path
?: devnum2devname(me
->devnum
));
1296 pr_err("failed to start array %s: %s\n",
1297 me
->path
?: devnum2devname(me
->devnum
),
1307 static char *container2devname(char *devname
)
1309 char *mdname
= NULL
;
1311 if (devname
[0] == '/') {
1312 int fd
= open(devname
, O_RDONLY
);
1314 mdname
= devnum2devname(fd2devnum(fd
));
1319 struct map_ent
*mp
, *map
= NULL
;
1321 if (!parse_uuid(devname
, uuid
))
1323 mp
= map_by_uuid(&map
, uuid
);
1325 mdname
= devnum2devname(mp
->devnum
);
1332 static int Incremental_container(struct supertype
*st
, char *devname
,
1335 /* Collect the contents of this container and for each
1336 * array, choose a device name and assemble the array.
1339 struct mdinfo
*list
;
1341 struct map_ent
*map
= NULL
;
1344 struct mddev_ident
*match
;
1346 struct domainlist
*domains
;
1347 struct map_ent
*smp
;
1353 st
->ss
->getinfo_super(st
, &info
, NULL
);
1355 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1356 info
.container_enough
> 0)
1360 pr_err("not enough devices to start the container\n");
1364 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1365 if (match
== NULL
&& rv
== 2)
1368 /* Need to compute 'trustworthy' */
1370 trustworthy
= LOCAL
;
1371 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1372 trustworthy
= LOCAL
;
1373 else if (st
->ss
->match_home(st
, "any") == 1)
1374 trustworthy
= LOCAL
;
1376 trustworthy
= FOREIGN
;
1378 list
= st
->ss
->container_content(st
, NULL
);
1379 /* when nothing to activate - quit */
1382 for (ra
= list
; ra
; ra
= ra
->next
) {
1384 char chosen_name
[1024];
1386 struct mddev_ident
*match
= NULL
;
1389 /* do not activate arrays blocked by metadata handler */
1390 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1391 pr_err("Cannot activate array %s in %s.\n",
1392 ra
->text_version
, devname
);
1396 mp
= map_by_uuid(&map
, ra
->uuid
);
1399 mdfd
= open_dev(mp
->devnum
);
1401 strcpy(chosen_name
, mp
->path
);
1403 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1406 /* Check in mdadm.conf for container == devname and
1407 * member == ra->text_version after second slash.
1409 char *sub
= strchr(ra
->text_version
+1, '/');
1410 struct mddev_ident
*array_list
;
1413 array_list
= conf_get_ident(NULL
);
1416 for(; array_list
; array_list
= array_list
->next
) {
1418 if (array_list
->member
== NULL
||
1419 array_list
->container
== NULL
)
1421 if (strcmp(array_list
->member
, sub
) != 0)
1423 if (array_list
->uuid_set
&&
1424 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1426 dn
= container2devname(array_list
->container
);
1429 if (strncmp(dn
, ra
->text_version
+1,
1431 ra
->text_version
[strlen(dn
)+1] != '/') {
1436 /* we have a match */
1439 pr_err("match found for member %s\n",
1440 array_list
->member
);
1444 if (match
&& match
->devname
&&
1445 strcasecmp(match
->devname
, "<ignore>") == 0) {
1447 pr_err("array %s/%s is "
1448 "explicitly ignored by mdadm.conf\n",
1449 match
->container
, match
->member
);
1453 trustworthy
= LOCAL
;
1455 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1463 pr_err("failed to open %s: %s.\n",
1464 chosen_name
, strerror(errno
));
1468 assemble_container_content(st
, mdfd
, ra
, c
,
1473 /* don't move spares to container with volume being activated
1474 when all volumes are blocked */
1475 if (ra_all
== ra_blocked
)
1478 /* Now move all suitable spares from spare container */
1479 domains
= domain_from_array(list
, st
->ss
->name
);
1480 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1482 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1483 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1484 /* spare container found */
1485 struct supertype
*sst
=
1486 super_imsm
.match_metadata_desc("imsm");
1487 struct mdinfo
*sinfo
;
1488 unsigned long long min_size
= 0;
1489 if (st
->ss
->min_acceptable_spare_size
)
1490 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1491 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1493 sinfo
= container_choose_spares(sst
, min_size
,
1496 sst
->ss
->free_super(sst
);
1499 struct mdinfo
*disks
= sinfo
->devs
;
1501 /* move spare from spare
1502 * container to currently
1508 makedev(disks
->disk
.major
,
1509 disks
->disk
.minor
)))
1511 disks
= disks
->next
;
1514 pr_err("Added %d spare%s to %s\n",
1515 count
, count
>1?"s":"", devname
);
1521 domain_free(domains
);
1526 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1527 * raid arrays, and if so first fail (if needed) and then remove the device.
1529 * @devname - The device we want to remove
1530 * @id_path - name as found in /dev/disk/by-path for this device
1532 * Note: the device name must be a kernel name like "sda", so
1533 * that we can find it in /proc/mdstat
1535 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1539 struct mdstat_ent
*ent
;
1540 struct mddev_dev devlist
;
1543 dprintf(Name
": incremental removal without --path <id_path> "
1544 "lacks the possibility to re-add new device in this "
1547 if (strchr(devname
, '/')) {
1548 pr_err("incremental removal requires a "
1549 "kernel device name, not a file: %s\n", devname
);
1552 ent
= mdstat_by_component(devname
);
1554 pr_err("%s does not appear to be a component "
1555 "of any array\n", devname
);
1558 mdfd
= open_dev(ent
->devnum
);
1560 pr_err("Cannot open array %s!!\n", ent
->dev
);
1566 struct map_ent
*map
= NULL
, *me
;
1567 me
= map_by_devnum(&map
, ent
->devnum
);
1569 policy_save_path(id_path
, me
);
1573 memset(&devlist
, 0, sizeof(devlist
));
1574 devlist
.devname
= devname
;
1575 devlist
.disposition
= 'f';
1576 /* for a container, we must fail each member array */
1577 if (ent
->metadata_version
&&
1578 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1579 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1580 struct mdstat_ent
*memb
;
1581 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1582 if (is_container_member(memb
, ent
->dev
)) {
1583 int subfd
= open_dev(memb
->devnum
);
1585 Manage_subdevs(memb
->dev
, subfd
,
1586 &devlist
, verbose
, 0,
1591 free_mdstat(mdstat
);
1593 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);
1594 devlist
.disposition
= 'r';
1595 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0, NULL
, 0);