]>
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
33 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
35 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
36 int number
, __u64 events
, int verbose
,
39 int Incremental(char *devname
, int verbose
, int runstop
,
40 struct supertype
*st
, char *homehost
, int require_homehost
,
43 /* Add this device to an array, creating the array if necessary
44 * and starting the array if sensible or - if runstop>0 - if possible.
46 * This has several steps:
48 * 1/ Check if device is permitted by mdadm.conf, reject if not.
49 * 2/ Find metadata, reject if none appropriate (check
50 * version/name from args)
51 * 3/ Check if there is a match in mdadm.conf
52 * 3a/ if not, check for homehost match. If no match, assemble as
54 * 4/ Determine device number.
55 * - If in mdadm.conf with std name, use that
56 * - UUID in /var/run/mdadm.map use that
57 * - If name is suggestive, use that. unless in use with different uuid.
58 * - Choose a free, high number.
59 * - Use a partitioned device unless strong suggestion not to.
61 * Don't choose partitioned for containers.
62 * 5/ Find out if array already exists
64 * - choose a name, from mdadm.conf or 'name' field in array.
68 * - check one drive in array to make sure metadata is a reasonably
69 * close match. Reject if not (e.g. different type)
71 * 6/ Make sure /var/run/mdadm.map contains this array.
72 * 7/ Is there enough devices to possibly start the array?
73 * For a container, this means running Incremental_container.
74 * 7a/ if not, finish with success.
76 * - read all metadata and arrange devices like -A does
77 * - if number of OK devices match expected, or -R and there are enough,
78 * start the array (auto-readonly).
82 struct mddev_ident_s
*array_list
, *match
;
83 char chosen_name
[1024];
85 struct map_ent
*mp
, *map
= NULL
;
86 int dfd
= -1, mdfd
= -1;
89 int trustworthy
= FOREIGN
;
91 mdu_array_info_t ainf
;
93 struct createinfo
*ci
= conf_get_create_info();
96 /* 1/ Check if device is permitted by mdadm.conf */
98 if (!conf_test_dev(devname
)) {
101 ": %s not permitted by mdadm.conf.\n",
106 /* 2/ Find metadata, reject if none appropriate (check
107 * version/name from args) */
109 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
112 fprintf(stderr
, Name
": cannot open %s: %s.\n",
113 devname
, strerror(errno
));
116 if (fstat(dfd
, &stb
) < 0) {
118 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
119 devname
, strerror(errno
));
122 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
124 fprintf(stderr
, Name
": %s is not a block device.\n",
129 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
132 ": no recognisable superblock on %s.\n",
136 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
138 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
142 close (dfd
); dfd
= -1;
144 memset(&info
, 0, sizeof(info
));
145 st
->ss
->getinfo_super(st
, &info
);
146 /* 3/ Check if there is a match in mdadm.conf */
148 array_list
= conf_get_ident(NULL
);
150 for (; array_list
; array_list
= array_list
->next
) {
151 if (array_list
->uuid_set
&&
152 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
154 if (verbose
>= 2 && array_list
->devname
)
156 ": UUID differs from %s.\n",
157 array_list
->devname
);
160 if (array_list
->name
[0] &&
161 strcasecmp(array_list
->name
, info
.name
) != 0) {
162 if (verbose
>= 2 && array_list
->devname
)
164 ": Name differs from %s.\n",
165 array_list
->devname
);
168 if (array_list
->devices
&&
169 !match_oneof(array_list
->devices
, devname
)) {
170 if (verbose
>= 2 && array_list
->devname
)
172 ": Not a listed device for %s.\n",
173 array_list
->devname
);
176 if (array_list
->super_minor
!= UnSet
&&
177 array_list
->super_minor
!= info
.array
.md_minor
) {
178 if (verbose
>= 2 && array_list
->devname
)
180 ": Different super-minor to %s.\n",
181 array_list
->devname
);
184 if (!array_list
->uuid_set
&&
185 !array_list
->name
[0] &&
186 !array_list
->devices
&&
187 array_list
->super_minor
== UnSet
) {
188 if (verbose
>= 2 && array_list
->devname
)
190 ": %s doesn't have any identifying information.\n",
191 array_list
->devname
);
194 /* FIXME, should I check raid_disks and level too?? */
198 if (match
->devname
&& array_list
->devname
)
200 ": we match both %s and %s - cannot decide which to use.\n",
201 match
->devname
, array_list
->devname
);
204 ": multiple lines in mdadm.conf match\n");
212 if (match
&& match
->devname
213 && strcasecmp(match
->devname
, "<ignore>") == 0) {
215 fprintf(stderr
, Name
": array containing %s is explicitly"
216 " ignored by mdadm.conf\n",
221 /* 3a/ if not, check for homehost match. If no match, continue
222 * but don't trust the 'name' in the array. Thus a 'random' minor
223 * number will be assigned, and the device name will be based
227 else if (st
->ss
->match_home(st
, homehost
) == 1)
229 else if (st
->ss
->match_home(st
, "any") == 1)
230 trustworthy
= LOCAL_ANY
;
232 trustworthy
= FOREIGN
;
235 if (!match
&& !conf_test_metadata(st
->ss
->name
,
236 (trustworthy
== LOCAL
))) {
239 ": %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 if (st
->ss
->container_content
&& st
->loaded_container
) {
258 if ((runstop
> 0 && info
.container_enough
>= 0) ||
259 info
.container_enough
> 0)
263 fprintf(stderr
, Name
": not enough devices to start the container\n");
268 /* This is a pre-built container array, so we do something
271 rv
= Incremental_container(st
, devname
, verbose
, runstop
,
276 name_to_use
= info
.name
;
277 if (name_to_use
[0] == 0 &&
278 info
.array
.level
== LEVEL_CONTAINER
&&
279 trustworthy
== LOCAL
) {
280 name_to_use
= info
.text_version
;
281 trustworthy
= METADATA
;
283 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
284 ! require_homehost
&&
285 conf_name_is_free(name_to_use
))
288 /* strip "hostname:" prefix from name if we have decided
289 * to treat it as LOCAL
291 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
292 name_to_use
= strchr(name_to_use
, ':')+1;
294 /* 4/ Check if array exists.
297 fprintf(stderr
, Name
": failed to get exclusive lock on "
299 mp
= map_by_uuid(&map
, info
.uuid
);
301 mdfd
= open_dev(mp
->devnum
);
309 /* Couldn't find an existing array, maybe make a new one */
310 mdfd
= create_mddev(match
? match
->devname
: NULL
,
311 name_to_use
, autof
, trustworthy
, chosen_name
);
316 sysfs_init(&info
, mdfd
, 0);
318 if (set_array_info(mdfd
, st
, &info
) != 0) {
319 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
320 chosen_name
, strerror(errno
));
326 dinfo
.disk
.major
= major(stb
.st_rdev
);
327 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
328 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
329 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
330 devname
, chosen_name
, strerror(errno
));
331 ioctl(mdfd
, STOP_ARRAY
, 0);
335 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
336 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
337 /* It really should be 'none' - must be old buggy
338 * kernel, and mdadm -I may not be able to complete.
341 ioctl(mdfd
, STOP_ARRAY
, NULL
);
343 ": You have an old buggy kernel which cannot support\n"
344 " --incremental reliably. Aborting.\n");
349 info
.array
.working_disks
= 1;
351 /* 6/ Make sure /var/run/mdadm.map contains this array. */
352 map_update(&map
, fd2devnum(mdfd
),
354 info
.uuid
, chosen_name
);
357 /* - check one drive in array to make sure metadata is a reasonably */
358 /* close match. Reject if not (e.g. different type) */
359 /* - add the device */
364 struct supertype
*st2
;
365 struct mdinfo info2
, *d
;
368 strcpy(chosen_name
, mp
->path
);
370 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
372 /* It is generally not OK to add non-spare drives to a
373 * running array as they are probably missing because
374 * they failed. However if runstop is 1, then the
375 * array was possibly started early and our best be is
376 * to add this anyway. It would probably be good to
377 * allow explicit policy statement about this.
379 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
383 if (st
->ss
->external
) {
384 char *devname
= devnum2devname(fd2devnum(mdfd
));
386 active
= devname
&& is_container_active(devname
);
388 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
392 ": not adding %s to active array (without --run) %s\n",
393 devname
, chosen_name
);
398 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
404 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
405 sra
->devs
->disk
.minor
);
406 dfd2
= dev_open(dn
, O_RDONLY
);
408 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
409 st
->ss
->compare_super(st
, st2
) != 0) {
411 ": metadata mismatch between %s and "
413 devname
, chosen_name
);
419 memset(&info2
, 0, sizeof(info2
));
420 st2
->ss
->getinfo_super(st2
, &info2
);
421 st2
->ss
->free_super(st2
);
422 if (info
.array
.level
!= info2
.array
.level
||
423 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
424 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
426 ": unexpected difference between %s and %s.\n",
427 chosen_name
, devname
);
432 info2
.disk
.major
= major(stb
.st_rdev
);
433 info2
.disk
.minor
= minor(stb
.st_rdev
);
434 /* add disk needs to know about containers */
435 if (st
->ss
->external
)
436 sra
->array
.level
= LEVEL_CONTAINER
;
437 err
= add_disk(mdfd
, st
, sra
, &info2
);
438 if (err
< 0 && errno
== EBUSY
) {
439 /* could be another device present with the same
440 * disk.number. Find and reject any such
442 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
443 info
.events
, verbose
, chosen_name
);
444 err
= add_disk(mdfd
, st
, sra
, &info2
);
447 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
448 devname
, chosen_name
, strerror(errno
));
452 info
.array
.working_disks
= 0;
453 for (d
= sra
->devs
; d
; d
=d
->next
)
454 info
.array
.working_disks
++;
458 /* 7/ Is there enough devices to possibly start the array? */
459 /* 7a/ if not, finish with success. */
460 if (info
.array
.level
== LEVEL_CONTAINER
) {
461 /* Try to assemble within the container */
463 sysfs_uevent(&info
, "change");
466 ": container %s now has %d devices\n",
467 chosen_name
, info
.array
.working_disks
);
468 wait_for(chosen_name
, mdfd
);
470 rv
= Incremental(chosen_name
, verbose
, runstop
,
471 NULL
, homehost
, require_homehost
, autof
);
473 /* Don't fail the whole -I if a subarray didn't
474 * have enough devices to start yet
480 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
481 if (enough(info
.array
.level
, info
.array
.raid_disks
,
482 info
.array
.layout
, info
.array
.state
& 1,
483 avail
, active_disks
) == 0) {
487 ": %s attached to %s, not enough to start (%d).\n",
488 devname
, chosen_name
, active_disks
);
496 /* - if number of OK devices match expected, or -R and there */
498 /* + add any bitmap file */
499 /* + start the array (auto-readonly). */
501 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
504 ": %s attached to %s which is already active.\n",
505 devname
, chosen_name
);
512 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
514 /* Let's try to start it */
515 if (match
&& match
->bitmap_file
) {
516 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
519 ": Could not open bitmap file %s.\n",
523 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
526 ": Failed to set bitmapfile for %s.\n",
532 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), 0);
533 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
534 && trustworthy
!= FOREIGN
)
535 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
537 rv
= sysfs_set_str(sra
, NULL
,
538 "array_state", "read-auto");
542 ": %s attached to %s, which has been started.\n",
543 devname
, chosen_name
);
545 wait_for(chosen_name
, mdfd
);
548 ": %s attached to %s, but failed to start: %s.\n",
549 devname
, chosen_name
, strerror(errno
));
555 ": %s attached to %s, not enough to start safely.\n",
556 devname
, chosen_name
);
567 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
568 int number
, __u64 events
, int verbose
,
571 /* Find a device attached to this array with a disk.number of number
572 * and events less than the passed events, and remove the device.
577 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
578 return; /* not safe to remove from active arrays
579 * without thinking more */
581 for (d
= sra
->devs
; d
; d
= d
->next
) {
585 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
586 dfd
= dev_open(dn
, O_RDONLY
);
589 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
593 st
->ss
->getinfo_super(st
, &info
);
594 st
->ss
->free_super(st
);
597 if (info
.disk
.number
!= number
||
598 info
.events
>= events
)
601 if (d
->disk
.raid_disk
> -1)
602 sysfs_set_str(sra
, d
, "slot", "none");
603 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
606 ": removing old device %s from %s\n",
607 d
->sys_name
+4, array_name
);
611 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
612 struct mdinfo
*bestinfo
)
614 /* count how many devices in sra think they are active */
616 int cnt
= 0, cnt1
= 0;
617 __u64 max_events
= 0;
618 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
624 for (d
= sra
->devs
; d
; d
= d
->next
) {
630 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
631 dfd
= dev_open(dn
, O_RDONLY
);
634 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
638 st
->ss
->getinfo_super(st
, &info
);
640 avail
= malloc(info
.array
.raid_disks
);
642 fprintf(stderr
, Name
": out of memory.\n");
645 memset(avail
, 0, info
.array
.raid_disks
);
649 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
653 max_events
= info
.events
;
654 avail
[info
.disk
.raid_disk
] = 2;
655 st
->ss
->getinfo_super(st
, bestinfo
);
656 } else if (info
.events
== max_events
) {
658 avail
[info
.disk
.raid_disk
] = 2;
659 } else if (info
.events
== max_events
-1) {
661 avail
[info
.disk
.raid_disk
] = 1;
662 } else if (info
.events
< max_events
- 1)
664 else if (info
.events
== max_events
+1) {
668 max_events
= info
.events
;
669 for (i
=0; i
<info
.array
.raid_disks
; i
++)
672 avail
[info
.disk
.raid_disk
] = 2;
673 st
->ss
->getinfo_super(st
, bestinfo
);
674 } else { /* info.events much bigger */
676 memset(avail
, 0, info
.disk
.raid_disk
);
677 max_events
= info
.events
;
678 st
->ss
->getinfo_super(st
, bestinfo
);
681 st
->ss
->free_super(st
);
686 int IncrementalScan(int verbose
)
688 /* look at every device listed in the 'map' file.
689 * If one is found that is not running then:
690 * look in mdadm.conf for bitmap file.
691 * if one exists, but array has none, add it.
692 * try to start array in auto-readonly mode
694 struct map_ent
*mapl
= NULL
;
696 mddev_ident_t devs
, mddev
;
700 devs
= conf_get_ident(NULL
);
702 for (me
= mapl
; me
; me
= me
->next
) {
703 mdu_array_info_t array
;
704 mdu_bitmap_file_t bmf
;
706 int mdfd
= open_dev(me
->devnum
);
710 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
715 /* Ok, we can try this one. Maybe it needs a bitmap */
716 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
717 if (mddev
->devname
&& me
->path
718 && devname_matches(mddev
->devname
, me
->path
))
720 if (mddev
&& mddev
->bitmap_file
) {
722 * Note: early kernels will wrongly fail this, so it
726 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
727 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
729 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
737 ": Added bitmap %s to %s\n",
738 mddev
->bitmap_file
, me
->path
);
739 else if (errno
!= EEXIST
)
741 ": Failed to add bitmap to %s: %s\n",
742 me
->path
, strerror(errno
));
745 sra
= sysfs_read(mdfd
, 0, 0);
747 if (sysfs_set_str(sra
, NULL
,
748 "array_state", "read-auto") == 0) {
751 ": started array %s\n",
752 me
->path
?: devnum2devname(me
->devnum
));
755 ": failed to start array %s: %s\n",
756 me
->path
?: devnum2devname(me
->devnum
),
765 static char *container2devname(char *devname
)
769 if (devname
[0] == '/') {
770 int fd
= open(devname
, O_RDONLY
);
772 mdname
= devnum2devname(fd2devnum(fd
));
777 struct map_ent
*mp
, *map
= NULL
;
779 if (!parse_uuid(devname
, uuid
))
781 mp
= map_by_uuid(&map
, uuid
);
783 mdname
= devnum2devname(mp
->devnum
);
790 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
791 int runstop
, int autof
, int trustworthy
)
793 /* Collect the contents of this container and for each
794 * array, choose a device name and assemble the array.
797 struct mdinfo
*list
= st
->ss
->container_content(st
);
799 struct map_ent
*map
= NULL
;
802 fprintf(stderr
, Name
": failed to get exclusive lock on "
805 for (ra
= list
; ra
; ra
= ra
->next
) {
807 char chosen_name
[1024];
809 struct mddev_ident_s
*match
= NULL
;
811 mp
= map_by_uuid(&map
, ra
->uuid
);
814 mdfd
= open_dev(mp
->devnum
);
816 strcpy(chosen_name
, mp
->path
);
818 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
821 /* Check in mdadm.conf for container == devname and
822 * member == ra->text_version after second slash.
824 char *sub
= strchr(ra
->text_version
+1, '/');
825 struct mddev_ident_s
*array_list
;
828 array_list
= conf_get_ident(NULL
);
831 for(; array_list
; array_list
= array_list
->next
) {
833 if (array_list
->member
== NULL
||
834 array_list
->container
== NULL
)
836 if (strcmp(array_list
->member
, sub
) != 0)
838 if (array_list
->uuid_set
&&
839 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
841 dn
= container2devname(array_list
->container
);
844 if (strncmp(dn
, ra
->text_version
+1,
846 ra
->text_version
[strlen(dn
)+1] != '/') {
851 /* we have a match */
854 fprintf(stderr
, Name
": match found for member %s\n",
859 if (match
&& match
->devname
&&
860 strcasecmp(match
->devname
, "<ignore>") == 0) {
862 fprintf(stderr
, Name
": array %s/%s is "
863 "explicitly ignored by mdadm.conf\n",
864 match
->container
, match
->member
);
870 mdfd
= create_mddev(match
? match
->devname
: NULL
,
878 fprintf(stderr
, Name
": failed to open %s: %s.\n",
879 chosen_name
, strerror(errno
));
883 assemble_container_content(st
, mdfd
, ra
, runstop
,
884 chosen_name
, verbose
);
891 * IncrementalRemove - Attempt to see if the passed in device belongs to any
892 * raid arrays, and if so first fail (if needed) and then remove the device.
894 * @devname - The device we want to remove
896 * Note: the device name must be a kernel name like "sda", so
897 * that we can find it in /proc/mdstat
899 int IncrementalRemove(char *devname
, int verbose
)
903 struct mdstat_ent
*ent
;
904 struct mddev_dev_s devlist
;
906 if (strchr(devname
, '/')) {
907 fprintf(stderr
, Name
": incremental removal requires a "
908 "kernel device name, not a file: %s\n", devname
);
911 ent
= mdstat_by_component(devname
);
913 fprintf(stderr
, Name
": %s does not appear to be a component "
914 "of any array\n", devname
);
917 mdfd
= open_dev(ent
->devnum
);
919 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
922 memset(&devlist
, 0, sizeof(devlist
));
923 devlist
.devname
= devname
;
924 devlist
.disposition
= 'f';
925 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
926 devlist
.disposition
= 'r';
927 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);