]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
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
, int mdfd
, char **availp
,
37 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
38 int number
, __u64 events
, int verbose
,
40 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
41 struct supertype
*st
, int verbose
);
43 int Incremental(char *devname
, int verbose
, int runstop
,
44 struct supertype
*st
, char *homehost
, int require_homehost
,
47 /* Add this device to an array, creating the array if necessary
48 * and starting the array if sensible or - if runstop>0 - if possible.
50 * This has several steps:
52 * 1/ Check if device is permitted by mdadm.conf, reject if not.
53 * 2/ Find metadata, reject if none appropriate (check
54 * version/name from args)
55 * 3/ Check if there is a match in mdadm.conf
56 * 3a/ if not, check for homehost match. If no match, assemble as
58 * 4/ Determine device number.
59 * - If in mdadm.conf with std name, use that
60 * - UUID in /var/run/mdadm.map use that
61 * - If name is suggestive, use that. unless in use with different uuid.
62 * - Choose a free, high number.
63 * - Use a partitioned device unless strong suggestion not to.
65 * Don't choose partitioned for containers.
66 * 5/ Find out if array already exists
68 * - choose a name, from mdadm.conf or 'name' field in array.
72 * - check one drive in array to make sure metadata is a reasonably
73 * close match. Reject if not (e.g. different type)
75 * 6/ Make sure /var/run/mdadm.map contains this array.
76 * 7/ Is there enough devices to possibly start the array?
77 * For a container, this means running Incremental_container.
78 * 7a/ if not, finish with success.
80 * - read all metadata and arrange devices like -A does
81 * - if number of OK devices match expected, or -R and there are enough,
82 * start the array (auto-readonly).
85 struct mdinfo info
, dinfo
;
86 struct mddev_ident_s
*array_list
, *match
;
87 char chosen_name
[1024];
89 struct map_ent
*mp
, *map
= NULL
;
90 int dfd
= -1, mdfd
= -1;
93 int trustworthy
= FOREIGN
;
95 mdu_array_info_t ainf
;
96 struct dev_policy
*policy
= NULL
;
98 struct createinfo
*ci
= conf_get_create_info();
101 /* 1/ Check if device is permitted by mdadm.conf */
103 if (!conf_test_dev(devname
)) {
106 ": %s not permitted by mdadm.conf.\n",
111 /* 2/ Find metadata, reject if none appropriate (check
112 * version/name from args) */
114 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
117 fprintf(stderr
, Name
": cannot open %s: %s.\n",
118 devname
, strerror(errno
));
121 if (fstat(dfd
, &stb
) < 0) {
123 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
124 devname
, strerror(errno
));
127 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
129 fprintf(stderr
, Name
": %s is not a block device.\n",
134 dinfo
.disk
.major
= major(stb
.st_rdev
);
135 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
137 policy
= disk_policy(&dinfo
);
139 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
142 ": no recognisable superblock on %s.\n",
144 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
147 if (st
->ss
->compare_super
== NULL
||
148 st
->ss
->load_super(st
, dfd
, NULL
)) {
150 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
152 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
156 close (dfd
); dfd
= -1;
158 memset(&info
, 0, sizeof(info
));
159 st
->ss
->getinfo_super(st
, &info
, NULL
);
160 /* 3/ Check if there is a match in mdadm.conf */
162 array_list
= conf_get_ident(NULL
);
164 for (; array_list
; array_list
= array_list
->next
) {
165 if (array_list
->uuid_set
&&
166 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
168 if (verbose
>= 2 && array_list
->devname
)
170 ": UUID differs from %s.\n",
171 array_list
->devname
);
174 if (array_list
->name
[0] &&
175 strcasecmp(array_list
->name
, info
.name
) != 0) {
176 if (verbose
>= 2 && array_list
->devname
)
178 ": Name differs from %s.\n",
179 array_list
->devname
);
182 if (array_list
->devices
&&
183 !match_oneof(array_list
->devices
, devname
)) {
184 if (verbose
>= 2 && array_list
->devname
)
186 ": Not a listed device for %s.\n",
187 array_list
->devname
);
190 if (array_list
->super_minor
!= UnSet
&&
191 array_list
->super_minor
!= info
.array
.md_minor
) {
192 if (verbose
>= 2 && array_list
->devname
)
194 ": Different super-minor to %s.\n",
195 array_list
->devname
);
198 if (!array_list
->uuid_set
&&
199 !array_list
->name
[0] &&
200 !array_list
->devices
&&
201 array_list
->super_minor
== UnSet
) {
202 if (verbose
>= 2 && array_list
->devname
)
204 ": %s doesn't have any identifying information.\n",
205 array_list
->devname
);
208 /* FIXME, should I check raid_disks and level too?? */
212 if (match
->devname
&& array_list
->devname
)
214 ": we match both %s and %s - cannot decide which to use.\n",
215 match
->devname
, array_list
->devname
);
218 ": multiple lines in mdadm.conf match\n");
226 if (match
&& match
->devname
227 && strcasecmp(match
->devname
, "<ignore>") == 0) {
229 fprintf(stderr
, Name
": array containing %s is explicitly"
230 " ignored by mdadm.conf\n",
235 /* 3a/ if not, check for homehost match. If no match, continue
236 * but don't trust the 'name' in the array. Thus a 'random' minor
237 * number will be assigned, and the device name will be based
241 else if (st
->ss
->match_home(st
, homehost
) == 1)
243 else if (st
->ss
->match_home(st
, "any") == 1)
244 trustworthy
= LOCAL_ANY
;
246 trustworthy
= FOREIGN
;
249 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
250 (trustworthy
== LOCAL
))) {
253 ": %s has metadata type %s for which "
254 "auto-assembly is disabled\n",
255 devname
, st
->ss
->name
);
258 if (trustworthy
== LOCAL_ANY
)
261 /* There are three possible sources for 'autof': command line,
262 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
263 * ARRAY takes precedence, then command line, then
266 if (match
&& match
->autof
)
267 autof
= match
->autof
;
271 if (st
->ss
->container_content
&& st
->loaded_container
) {
272 if ((runstop
> 0 && info
.container_enough
>= 0) ||
273 info
.container_enough
> 0)
277 fprintf(stderr
, Name
": not enough devices to start the container\n");
282 /* This is a pre-built container array, so we do something
285 rv
= Incremental_container(st
, devname
, verbose
, runstop
,
290 name_to_use
= info
.name
;
291 if (name_to_use
[0] == 0 &&
292 info
.array
.level
== LEVEL_CONTAINER
&&
293 trustworthy
== LOCAL
) {
294 name_to_use
= info
.text_version
;
295 trustworthy
= METADATA
;
297 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
298 ! require_homehost
&&
299 conf_name_is_free(name_to_use
))
302 /* strip "hostname:" prefix from name if we have decided
303 * to treat it as LOCAL
305 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
306 name_to_use
= strchr(name_to_use
, ':')+1;
308 /* 4/ Check if array exists.
311 fprintf(stderr
, Name
": failed to get exclusive lock on "
313 mp
= map_by_uuid(&map
, info
.uuid
);
315 mdfd
= open_dev(mp
->devnum
);
322 /* Couldn't find an existing array, maybe make a new one */
323 mdfd
= create_mddev(match
? match
->devname
: NULL
,
324 name_to_use
, autof
, trustworthy
, chosen_name
);
329 sysfs_init(&info
, mdfd
, 0);
331 if (set_array_info(mdfd
, st
, &info
) != 0) {
332 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
333 chosen_name
, strerror(errno
));
339 dinfo
.disk
.major
= major(stb
.st_rdev
);
340 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
341 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
342 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
343 devname
, chosen_name
, strerror(errno
));
344 ioctl(mdfd
, STOP_ARRAY
, 0);
348 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
349 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
350 /* It really should be 'none' - must be old buggy
351 * kernel, and mdadm -I may not be able to complete.
354 ioctl(mdfd
, STOP_ARRAY
, NULL
);
356 ": You have an old buggy kernel which cannot support\n"
357 " --incremental reliably. Aborting.\n");
362 info
.array
.working_disks
= 1;
364 /* 6/ Make sure /var/run/mdadm.map contains this array. */
365 map_update(&map
, fd2devnum(mdfd
),
367 info
.uuid
, chosen_name
);
370 /* - check one drive in array to make sure metadata is a reasonably */
371 /* close match. Reject if not (e.g. different type) */
372 /* - add the device */
377 struct supertype
*st2
;
378 struct mdinfo info2
, *d
;
381 strcpy(chosen_name
, mp
->path
);
383 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
385 /* It is generally not OK to add non-spare drives to a
386 * running array as they are probably missing because
387 * they failed. However if runstop is 1, then the
388 * array was possibly started early and our best bet is
389 * to add this anyway.
390 * Also if action policy is re-add or better we allow
393 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
394 && ! policy_action_allows(policy
, st
->ss
->name
,
399 if (st
->ss
->external
) {
400 char *devname
= devnum2devname(fd2devnum(mdfd
));
402 active
= devname
&& is_container_active(devname
);
404 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
408 ": not adding %s to active array (without --run) %s\n",
409 devname
, chosen_name
);
414 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
420 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
421 sra
->devs
->disk
.minor
);
422 dfd2
= dev_open(dn
, O_RDONLY
);
424 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
425 st
->ss
->compare_super(st
, st2
) != 0) {
427 ": metadata mismatch between %s and "
429 devname
, chosen_name
);
435 memset(&info2
, 0, sizeof(info2
));
436 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
437 st2
->ss
->free_super(st2
);
438 if (info
.array
.level
!= info2
.array
.level
||
439 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
440 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
442 ": unexpected difference between %s and %s.\n",
443 chosen_name
, devname
);
448 info2
.disk
.major
= major(stb
.st_rdev
);
449 info2
.disk
.minor
= minor(stb
.st_rdev
);
450 /* add disk needs to know about containers */
451 if (st
->ss
->external
)
452 sra
->array
.level
= LEVEL_CONTAINER
;
453 err
= add_disk(mdfd
, st
, sra
, &info2
);
454 if (err
< 0 && errno
== EBUSY
) {
455 /* could be another device present with the same
456 * disk.number. Find and reject any such
458 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
459 info
.events
, verbose
, chosen_name
);
460 err
= add_disk(mdfd
, st
, sra
, &info2
);
463 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
464 devname
, chosen_name
, strerror(errno
));
468 info
.array
.working_disks
= 0;
469 for (d
= sra
->devs
; d
; d
=d
->next
)
470 info
.array
.working_disks
++;
474 /* 7/ Is there enough devices to possibly start the array? */
475 /* 7a/ if not, finish with success. */
476 if (info
.array
.level
== LEVEL_CONTAINER
) {
477 /* Try to assemble within the container */
479 sysfs_uevent(&info
, "change");
482 ": container %s now has %d devices\n",
483 chosen_name
, info
.array
.working_disks
);
484 wait_for(chosen_name
, mdfd
);
486 rv
= Incremental(chosen_name
, verbose
, runstop
,
487 NULL
, homehost
, require_homehost
, autof
);
489 /* Don't fail the whole -I if a subarray didn't
490 * have enough devices to start yet
496 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
497 if (enough(info
.array
.level
, info
.array
.raid_disks
,
498 info
.array
.layout
, info
.array
.state
& 1,
499 avail
, active_disks
) == 0) {
503 ": %s attached to %s, not enough to start (%d).\n",
504 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) {
520 ": %s attached to %s which is already active.\n",
521 devname
, chosen_name
);
528 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
529 struct mdinfo
*sra
, *dsk
;
530 /* Let's try to start it */
531 if (match
&& match
->bitmap_file
) {
532 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
535 ": Could not open bitmap file %s.\n",
539 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
542 ": Failed to set bitmapfile for %s.\n",
548 /* GET_* needed so add_disk works below */
549 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
),
550 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
551 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
552 && trustworthy
!= FOREIGN
)
553 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
555 rv
= sysfs_set_str(sra
, NULL
,
556 "array_state", "read-auto");
560 ": %s attached to %s, which has been started.\n",
561 devname
, chosen_name
);
563 wait_for(chosen_name
, mdfd
);
564 /* We just started the array, so some devices
565 * might have been evicted from the array
566 * because their event counts were too old.
567 * If the action=re-add policy is in-force for
568 * those devices we should re-add them now.
570 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
571 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
572 add_disk(mdfd
, st
, sra
, dsk
) == 0)
574 ": %s re-added to %s\n",
575 dsk
->sys_name
, chosen_name
);
579 ": %s attached to %s, but failed to start: %s.\n",
580 devname
, chosen_name
, strerror(errno
));
586 ": %s attached to %s, not enough to start safely.\n",
587 devname
, chosen_name
);
596 dev_policy_free(policy
);
600 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
601 int number
, __u64 events
, int verbose
,
604 /* Find a device attached to this array with a disk.number of number
605 * and events less than the passed events, and remove the device.
610 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
611 return; /* not safe to remove from active arrays
612 * without thinking more */
614 for (d
= sra
->devs
; d
; d
= d
->next
) {
618 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
619 dfd
= dev_open(dn
, O_RDONLY
);
622 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
626 st
->ss
->getinfo_super(st
, &info
, NULL
);
627 st
->ss
->free_super(st
);
630 if (info
.disk
.number
!= number
||
631 info
.events
>= events
)
634 if (d
->disk
.raid_disk
> -1)
635 sysfs_set_str(sra
, d
, "slot", "none");
636 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
639 ": removing old device %s from %s\n",
640 d
->sys_name
+4, array_name
);
644 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
645 struct mdinfo
*bestinfo
)
647 /* count how many devices in sra think they are active */
649 int cnt
= 0, cnt1
= 0;
650 __u64 max_events
= 0;
651 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
657 for (d
= sra
->devs
; d
; d
= d
->next
) {
663 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
664 dfd
= dev_open(dn
, O_RDONLY
);
667 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
671 st
->ss
->getinfo_super(st
, &info
, NULL
);
673 avail
= malloc(info
.array
.raid_disks
);
675 fprintf(stderr
, Name
": out of memory.\n");
678 memset(avail
, 0, info
.array
.raid_disks
);
682 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
686 max_events
= info
.events
;
687 avail
[info
.disk
.raid_disk
] = 2;
688 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
689 } else if (info
.events
== max_events
) {
691 avail
[info
.disk
.raid_disk
] = 2;
692 } else if (info
.events
== max_events
-1) {
694 avail
[info
.disk
.raid_disk
] = 1;
695 } else if (info
.events
< max_events
- 1)
697 else if (info
.events
== max_events
+1) {
701 max_events
= info
.events
;
702 for (i
=0; i
<info
.array
.raid_disks
; i
++)
705 avail
[info
.disk
.raid_disk
] = 2;
706 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
707 } else { /* info.events much bigger */
709 memset(avail
, 0, info
.disk
.raid_disk
);
710 max_events
= info
.events
;
711 avail
[info
.disk
.raid_disk
] = 2;
712 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
715 st
->ss
->free_super(st
);
720 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
721 struct supertype
*st
, int verbose
)
723 /* This device doesn't have any md metadata
724 * If it is 'bare' and theh device policy allows 'spare' look for
725 * an array or container to attach it to.
726 * If st is set, then only arrays of that type are considered
727 * Return 0 on success, or some exit code on failure, probably 1.
731 struct map_ent
*mp
, *map
= NULL
;
732 struct mdinfo
*chosen
= NULL
;
735 if (fstat(dfd
, &stb
) != 0)
739 * Now we need to find a suitable array to add this to.
740 * We only accept arrays that:
742 * - are in the same domains as the device
743 * - are of an size for which the device will be useful
744 * and we choose the one that is the most degraded
747 if (map_lock(&map
)) {
748 fprintf(stderr
, Name
": failed to get exclusive lock on "
752 for (mp
= map
; mp
; mp
= mp
->next
) {
753 struct supertype
*st2
;
754 struct domainlist
*dl
= NULL
;
756 unsigned long long devsize
;
758 if (is_subarray(mp
->metadata
))
761 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
763 (st
->minor_version
>= 0 &&
764 st
->minor_version
!= st2
->minor_version
)) {
766 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
773 sra
= sysfs_read(-1, mp
->devnum
,
774 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
775 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
777 /* Probably a container - no degraded info */
778 sra
= sysfs_read(-1, mp
->devnum
,
779 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
780 GET_COMPONENT
|GET_VERSION
);
782 sra
->array
.failed_disks
= 0;
789 for(i
=0; !st2
&& superlist
[i
]; i
++)
790 st2
= superlist
[i
]->match_metadata_desc(
794 get_dev_size(dfd
, NULL
, &devsize
);
795 if (st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
) {
797 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
801 dl
= domain_from_array(sra
, st2
->ss
->name
);
802 if (!domain_test(dl
, pol
, st2
->ss
->name
)) {
803 /* domain test fails */
805 fprintf(stderr
, Name
": not adding %s to %s as it is not in a compatible domain\n",
810 /* all tests passed, OK to add to this array */
814 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
828 /* add current device to chosen array as a spare */
829 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
831 struct mddev_dev_s devlist
;
836 devlist
.writemostly
= 0;
837 devlist
.devname
= devname
;
838 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
840 devlist
.disposition
= 'a';
843 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
849 fprintf(stderr
, Name
": added %s as spare for %s\n",
850 devname
, chosen
->sys_name
);
852 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
853 devname
, chosen
->sys_name
);
860 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
861 struct supertype
*st
, int verbose
)
863 /* we know that at least one partition virtual-metadata is
864 * allowed to incorporate spares like this device. We need to
865 * find a suitable device to copy partition information from.
867 * Getting a list of all disk (not partition) devices is
868 * slightly non-trivial. We could look at /sys/block, but
869 * that is theoretically due to be removed. Maybe best to use
870 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
871 * we depend on this directory of 'path' info. But that fails
872 * to find loop devices and probably others. Maybe don't
873 * worry about that, they aren't the real target.
875 * So: check things in /dev/disk/by-path to see if they are in
876 * a compatible domain, then load the partition table and see
877 * if it is OK for the new device, and choose the largest
878 * partition table that fits.
883 unsigned long long chosen_size
;
884 struct supertype
*chosen_st
= NULL
;
887 dir
= opendir("/dev/disk/by-path");
890 while ((de
= readdir(dir
)) != NULL
) {
892 struct dev_policy
*pol2
= NULL
;
893 struct domainlist
*domlist
= NULL
;
896 struct supertype
*st2
= NULL
;
897 char *devname
= NULL
;
898 unsigned long long devsectors
;
900 if (de
->d_ino
== 0 ||
901 de
->d_name
[0] == '.' ||
902 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
905 ep
= de
->d_name
+ strlen(de
->d_name
);
906 while (ep
> de
->d_name
&&
909 if (ep
> de
->d_name
+ 5 &&
910 strncmp(ep
-5, "-part", 5) == 0)
911 /* This is a partition - skip it */
914 pol2
= path_policy(de
->d_name
, type_disk
);
916 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
917 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) == 0)
918 /* new device is incompatible with this device. */
921 domain_free(domlist
);
924 asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
);
925 fd
= open(devname
, O_RDONLY
);
928 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
935 st2
= guess_super_type(fd
, guess_partitions
);
937 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
941 /* Check domain policy again, this time referring to metadata */
942 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
943 if (domain_test(domlist
, pol
, st2
->ss
->name
) == 0)
944 /* Incompatible devices for this metadata type */
948 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
949 if (info
.component_size
> devsectors
)
950 /* This partitioning doesn't fit in the device */
953 /* This is an acceptable device to copy partition
954 * metadata from. We could just stop here, but I
955 * think I want to keep looking incase a larger
956 * metadata which makes better use of the device can
959 if (chosen
== NULL
||
960 chosen_size
< info
.component_size
) {
961 chosen_size
= info
.component_size
;
966 chosen_st
->ss
->free_super(chosen_st
);
975 domain_free(domlist
);
976 dev_policy_free(pol2
);
978 st2
->ss
->free_super(st2
);
988 /* 'chosen' is the best device we can find. Let's write its
989 * metadata to devname dfd is read-only so don't use that
991 fd
= open(devname
, O_RDWR
);
993 chosen_st
->ss
->store_super(chosen_st
, fd
);
997 chosen_st
->ss
->free_super(chosen_st
);
1003 /* adding a spare to a regular array is quite different from adding one to
1004 * a set-of-partitions virtual array.
1005 * This function determines which is worth trying and tries as appropriate.
1006 * Arrays are given priority over partitions.
1008 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1009 struct supertype
*st
, int verbose
)
1014 int partitions_ok
= 0;
1015 char bufpad
[4096 + 4096];
1016 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1019 /* Can only add a spare if device has at least one domains */
1020 if (pol_find(pol
, pol_domain
) == NULL
)
1022 /* And only if some action allows spares */
1023 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1026 /* Now check if the device is bare - we don't add non-bare devices
1027 * yet even if action=-spare
1030 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1031 read(dfd
, buf
, 4096) != 4096) {
1034 fprintf(stderr
, Name
": %s is not bare, so not considering as a spare\n",
1038 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1040 if (memcmp(buf
, buf
+1, 4095) != 0)
1043 /* OK, first 4K appear blank, try the end. */
1044 if (lseek(dfd
, -4096, SEEK_END
) < 0 ||
1045 read(dfd
, buf
, 4096) != 4096)
1048 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1050 if (memcmp(buf
, buf
+1, 4095) != 0)
1053 /* This device passes our test for 'is bare'.
1054 * Let's see what policy allows for such things.
1057 /* just try try 'array' or 'partition' based on this metadata */
1058 if (st
->ss
->add_to_super
)
1059 return array_try_spare(devname
, dfdp
, pol
,
1062 return partition_try_spare(devname
, dfdp
, pol
,
1065 /* Now see which metadata type support spare */
1066 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1067 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1068 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1070 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1071 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1076 rv
= array_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1077 if (rv
== 0 && partitions_ok
)
1078 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1082 int IncrementalScan(int verbose
)
1084 /* look at every device listed in the 'map' file.
1085 * If one is found that is not running then:
1086 * look in mdadm.conf for bitmap file.
1087 * if one exists, but array has none, add it.
1088 * try to start array in auto-readonly mode
1090 struct map_ent
*mapl
= NULL
;
1092 mddev_ident_t devs
, mddev
;
1096 devs
= conf_get_ident(NULL
);
1098 for (me
= mapl
; me
; me
= me
->next
) {
1099 mdu_array_info_t array
;
1100 mdu_bitmap_file_t bmf
;
1102 int mdfd
= open_dev(me
->devnum
);
1106 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1111 /* Ok, we can try this one. Maybe it needs a bitmap */
1112 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1113 if (mddev
->devname
&& me
->path
1114 && devname_matches(mddev
->devname
, me
->path
))
1116 if (mddev
&& mddev
->bitmap_file
) {
1118 * Note: early kernels will wrongly fail this, so it
1122 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1123 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1125 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1132 fprintf(stderr
, Name
1133 ": Added bitmap %s to %s\n",
1134 mddev
->bitmap_file
, me
->path
);
1135 else if (errno
!= EEXIST
)
1136 fprintf(stderr
, Name
1137 ": Failed to add bitmap to %s: %s\n",
1138 me
->path
, strerror(errno
));
1141 sra
= sysfs_read(mdfd
, 0, 0);
1143 if (sysfs_set_str(sra
, NULL
,
1144 "array_state", "read-auto") == 0) {
1146 fprintf(stderr
, Name
1147 ": started array %s\n",
1148 me
->path
?: devnum2devname(me
->devnum
));
1150 fprintf(stderr
, Name
1151 ": failed to start array %s: %s\n",
1152 me
->path
?: devnum2devname(me
->devnum
),
1161 static char *container2devname(char *devname
)
1163 char *mdname
= NULL
;
1165 if (devname
[0] == '/') {
1166 int fd
= open(devname
, O_RDONLY
);
1168 mdname
= devnum2devname(fd2devnum(fd
));
1173 struct map_ent
*mp
, *map
= NULL
;
1175 if (!parse_uuid(devname
, uuid
))
1177 mp
= map_by_uuid(&map
, uuid
);
1179 mdname
= devnum2devname(mp
->devnum
);
1186 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
1187 int runstop
, int autof
, int trustworthy
)
1189 /* Collect the contents of this container and for each
1190 * array, choose a device name and assemble the array.
1193 struct mdinfo
*list
= st
->ss
->container_content(st
, NULL
);
1195 struct map_ent
*map
= NULL
;
1198 fprintf(stderr
, Name
": failed to get exclusive lock on "
1201 for (ra
= list
; ra
; ra
= ra
->next
) {
1203 char chosen_name
[1024];
1205 struct mddev_ident_s
*match
= NULL
;
1207 mp
= map_by_uuid(&map
, ra
->uuid
);
1210 mdfd
= open_dev(mp
->devnum
);
1212 strcpy(chosen_name
, mp
->path
);
1214 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1217 /* Check in mdadm.conf for container == devname and
1218 * member == ra->text_version after second slash.
1220 char *sub
= strchr(ra
->text_version
+1, '/');
1221 struct mddev_ident_s
*array_list
;
1224 array_list
= conf_get_ident(NULL
);
1227 for(; array_list
; array_list
= array_list
->next
) {
1229 if (array_list
->member
== NULL
||
1230 array_list
->container
== NULL
)
1232 if (strcmp(array_list
->member
, sub
) != 0)
1234 if (array_list
->uuid_set
&&
1235 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1237 dn
= container2devname(array_list
->container
);
1240 if (strncmp(dn
, ra
->text_version
+1,
1242 ra
->text_version
[strlen(dn
)+1] != '/') {
1247 /* we have a match */
1250 fprintf(stderr
, Name
": match found for member %s\n",
1251 array_list
->member
);
1255 if (match
&& match
->devname
&&
1256 strcasecmp(match
->devname
, "<ignore>") == 0) {
1258 fprintf(stderr
, Name
": array %s/%s is "
1259 "explicitly ignored by mdadm.conf\n",
1260 match
->container
, match
->member
);
1264 trustworthy
= LOCAL
;
1266 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1274 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1275 chosen_name
, strerror(errno
));
1279 assemble_container_content(st
, mdfd
, ra
, runstop
,
1280 chosen_name
, verbose
);
1287 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1288 * raid arrays, and if so first fail (if needed) and then remove the device.
1290 * @devname - The device we want to remove
1292 * Note: the device name must be a kernel name like "sda", so
1293 * that we can find it in /proc/mdstat
1295 int IncrementalRemove(char *devname
, int verbose
)
1299 struct mdstat_ent
*ent
;
1300 struct mddev_dev_s devlist
;
1302 if (strchr(devname
, '/')) {
1303 fprintf(stderr
, Name
": incremental removal requires a "
1304 "kernel device name, not a file: %s\n", devname
);
1307 ent
= mdstat_by_component(devname
);
1309 fprintf(stderr
, Name
": %s does not appear to be a component "
1310 "of any array\n", devname
);
1313 mdfd
= open_dev(ent
->devnum
);
1315 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1318 memset(&devlist
, 0, sizeof(devlist
));
1319 devlist
.devname
= devname
;
1320 devlist
.disposition
= 'f';
1321 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
1322 devlist
.disposition
= 'r';
1323 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);