]>
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
;
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
));
123 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
125 fprintf(stderr
, Name
": %s is not a block device.\n",
131 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
134 ": no recognisable superblock on %s.\n",
139 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
141 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
148 memset(&info
, 0, sizeof(info
));
149 st
->ss
->getinfo_super(st
, &info
);
150 /* 3/ Check if there is a match in mdadm.conf */
152 array_list
= conf_get_ident(NULL
);
154 for (; array_list
; array_list
= array_list
->next
) {
155 if (array_list
->uuid_set
&&
156 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
158 if (verbose
>= 2 && array_list
->devname
)
160 ": UUID differs from %s.\n",
161 array_list
->devname
);
164 if (array_list
->name
[0] &&
165 strcasecmp(array_list
->name
, info
.name
) != 0) {
166 if (verbose
>= 2 && array_list
->devname
)
168 ": Name differs from %s.\n",
169 array_list
->devname
);
172 if (array_list
->devices
&&
173 !match_oneof(array_list
->devices
, devname
)) {
174 if (verbose
>= 2 && array_list
->devname
)
176 ": Not a listed device for %s.\n",
177 array_list
->devname
);
180 if (array_list
->super_minor
!= UnSet
&&
181 array_list
->super_minor
!= info
.array
.md_minor
) {
182 if (verbose
>= 2 && array_list
->devname
)
184 ": Different super-minor to %s.\n",
185 array_list
->devname
);
188 if (!array_list
->uuid_set
&&
189 !array_list
->name
[0] &&
190 !array_list
->devices
&&
191 array_list
->super_minor
== UnSet
) {
192 if (verbose
>= 2 && array_list
->devname
)
194 ": %s doesn't have any identifying information.\n",
195 array_list
->devname
);
198 /* FIXME, should I check raid_disks and level too?? */
202 if (match
->devname
&& array_list
->devname
)
204 ": we match both %s and %s - cannot decide which to use.\n",
205 match
->devname
, array_list
->devname
);
208 ": multiple lines in mdadm.conf match\n");
215 if (match
&& match
->devname
216 && strcasecmp(match
->devname
, "<ignore>") == 0) {
218 fprintf(stderr
, Name
": array containing %s is explicitly"
219 " ignored by mdadm.conf\n",
224 /* 3a/ if not, check for homehost match. If no match, continue
225 * but don't trust the 'name' in the array. Thus a 'random' minor
226 * number will be assigned, and the device name will be based
230 else if (st
->ss
->match_home(st
, homehost
) == 1)
232 else if (st
->ss
->match_home(st
, "any") == 1)
233 trustworthy
= LOCAL_ANY
;
235 trustworthy
= FOREIGN
;
238 if (!match
&& !conf_test_metadata(st
->ss
->name
,
239 (trustworthy
== LOCAL
))) {
242 ": %s has metadata type %s for which "
243 "auto-assembly is disabled\n",
244 devname
, st
->ss
->name
);
247 if (trustworthy
== LOCAL_ANY
)
250 /* There are three possible sources for 'autof': command line,
251 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
252 * ARRAY takes precedence, then command line, then
255 if (match
&& match
->autof
)
256 autof
= match
->autof
;
260 if (st
->ss
->container_content
&& st
->loaded_container
) {
261 if ((runstop
> 0 && info
.container_enough
>= 0) ||
262 info
.container_enough
> 0)
266 fprintf(stderr
, Name
": not enough devices to start the container\n");
270 /* This is a pre-built container array, so we do something
273 return Incremental_container(st
, devname
, verbose
, runstop
,
277 name_to_use
= info
.name
;
278 if (name_to_use
[0] == 0 &&
279 info
.array
.level
== LEVEL_CONTAINER
&&
280 trustworthy
== LOCAL
) {
281 name_to_use
= info
.text_version
;
282 trustworthy
= METADATA
;
284 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
285 ! require_homehost
&&
286 conf_name_is_free(name_to_use
))
289 /* strip "hostname:" prefix from name if we have decided
290 * to treat it as LOCAL
292 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
293 name_to_use
= strchr(name_to_use
, ':')+1;
295 /* 4/ Check if array exists.
298 mp
= map_by_uuid(&map
, info
.uuid
);
300 mdfd
= open_dev(mp
->devnum
);
308 /* Couldn't find an existing array, maybe make a new one */
309 mdfd
= create_mddev(match
? match
->devname
: NULL
,
310 name_to_use
, autof
, trustworthy
, chosen_name
);
315 sysfs_init(&info
, mdfd
, 0);
317 if (set_array_info(mdfd
, st
, &info
) != 0) {
318 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
319 chosen_name
, strerror(errno
));
325 dinfo
.disk
.major
= major(stb
.st_rdev
);
326 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
327 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
328 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
329 devname
, chosen_name
, strerror(errno
));
330 ioctl(mdfd
, STOP_ARRAY
, 0);
334 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
335 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
336 /* It really should be 'none' - must be old buggy
337 * kernel, and mdadm -I may not be able to complete.
340 ioctl(mdfd
, STOP_ARRAY
, NULL
);
342 ": You have an old buggy kernel which cannot support\n"
343 " --incremental reliably. Aborting.\n");
348 info
.array
.working_disks
= 1;
350 /* 6/ Make sure /var/run/mdadm.map contains this array. */
351 map_update(&map
, fd2devnum(mdfd
),
353 info
.uuid
, chosen_name
);
356 /* - check one drive in array to make sure metadata is a reasonably */
357 /* close match. Reject if not (e.g. different type) */
358 /* - add the device */
363 struct supertype
*st2
;
364 struct mdinfo info2
, *d
;
367 strcpy(chosen_name
, mp
->path
);
369 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
371 /* It is generally not OK to add drives to a running array
372 * as they are probably missing because they failed.
373 * However if runstop is 1, then the array was possibly
374 * started early and our best be is to add this anyway.
375 * It would probably be good to allow explicit policy
376 * statement about this.
381 if (st
->ss
->external
) {
382 char *devname
= devnum2devname(fd2devnum(mdfd
));
384 active
= devname
&& is_container_active(devname
);
386 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
390 ": not adding %s to active array (without --run) %s\n",
391 devname
, chosen_name
);
396 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
401 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
402 sra
->devs
->disk
.minor
);
403 dfd2
= dev_open(dn
, O_RDONLY
);
405 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
406 st
->ss
->compare_super(st
, st2
) != 0) {
408 ": metadata mismatch between %s and "
410 devname
, chosen_name
);
416 memset(&info2
, 0, sizeof(info2
));
417 st2
->ss
->getinfo_super(st2
, &info2
);
418 st2
->ss
->free_super(st2
);
419 if (info
.array
.level
!= info2
.array
.level
||
420 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
421 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
423 ": unexpected difference between %s and %s.\n",
424 chosen_name
, devname
);
429 info2
.disk
.major
= major(stb
.st_rdev
);
430 info2
.disk
.minor
= minor(stb
.st_rdev
);
431 /* add disk needs to know about containers */
432 if (st
->ss
->external
)
433 sra
->array
.level
= LEVEL_CONTAINER
;
434 err
= add_disk(mdfd
, st
, sra
, &info2
);
435 if (err
< 0 && errno
== EBUSY
) {
436 /* could be another device present with the same
437 * disk.number. Find and reject any such
439 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
440 info
.events
, verbose
, chosen_name
);
441 err
= add_disk(mdfd
, st
, sra
, &info2
);
444 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
445 devname
, chosen_name
, strerror(errno
));
449 info
.array
.working_disks
= 0;
450 for (d
= sra
->devs
; d
; d
=d
->next
)
451 info
.array
.working_disks
++;
455 /* 7/ Is there enough devices to possibly start the array? */
456 /* 7a/ if not, finish with success. */
457 if (info
.array
.level
== LEVEL_CONTAINER
) {
458 /* Try to assemble within the container */
460 sysfs_uevent(&info
, "change");
463 ": container %s now has %d devices\n",
464 chosen_name
, info
.array
.working_disks
);
465 wait_for(chosen_name
, mdfd
);
467 rv
= Incremental(chosen_name
, verbose
, runstop
,
468 NULL
, homehost
, require_homehost
, autof
);
470 /* Don't fail the whole -I if a subarray didn't
471 * have enough devices to start yet
477 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
478 if (enough(info
.array
.level
, info
.array
.raid_disks
,
479 info
.array
.layout
, info
.array
.state
& 1,
480 avail
, active_disks
) == 0) {
484 ": %s attached to %s, not enough to start (%d).\n",
485 devname
, chosen_name
, active_disks
);
493 /* - if number of OK devices match expected, or -R and there */
495 /* + add any bitmap file */
496 /* + start the array (auto-readonly). */
498 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
501 ": %s attached to %s which is already active.\n",
502 devname
, chosen_name
);
509 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
511 /* Let's try to start it */
512 if (match
&& match
->bitmap_file
) {
513 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
516 ": Could not open bitmap file %s.\n",
521 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
524 ": Failed to set bitmapfile for %s.\n",
531 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), 0);
532 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
533 && trustworthy
!= FOREIGN
)
534 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
536 rv
= sysfs_set_str(sra
, NULL
,
537 "array_state", "read-auto");
541 ": %s attached to %s, which has been started.\n",
542 devname
, chosen_name
);
544 wait_for(chosen_name
, mdfd
);
547 ": %s attached to %s, but failed to start: %s.\n",
548 devname
, chosen_name
, strerror(errno
));
554 ": %s attached to %s, not enough to start safely.\n",
555 devname
, chosen_name
);
562 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
563 int number
, __u64 events
, int verbose
,
566 /* Find a device attached to this array with a disk.number of number
567 * and events less than the passed events, and remove the device.
572 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
573 return; /* not safe to remove from active arrays
574 * without thinking more */
576 for (d
= sra
->devs
; d
; d
= d
->next
) {
580 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
581 dfd
= dev_open(dn
, O_RDONLY
);
584 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
588 st
->ss
->getinfo_super(st
, &info
);
589 st
->ss
->free_super(st
);
592 if (info
.disk
.number
!= number
||
593 info
.events
>= events
)
596 if (d
->disk
.raid_disk
> -1)
597 sysfs_set_str(sra
, d
, "slot", "none");
598 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
601 ": removing old device %s from %s\n",
602 d
->sys_name
+4, array_name
);
606 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
607 struct mdinfo
*bestinfo
)
609 /* count how many devices in sra think they are active */
611 int cnt
= 0, cnt1
= 0;
612 __u64 max_events
= 0;
613 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
619 for (d
= sra
->devs
; d
; d
= d
->next
) {
625 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
626 dfd
= dev_open(dn
, O_RDONLY
);
629 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
633 st
->ss
->getinfo_super(st
, &info
);
635 avail
= malloc(info
.array
.raid_disks
);
637 fprintf(stderr
, Name
": out of memory.\n");
640 memset(avail
, 0, info
.array
.raid_disks
);
644 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
648 max_events
= info
.events
;
649 avail
[info
.disk
.raid_disk
] = 2;
650 st
->ss
->getinfo_super(st
, bestinfo
);
651 } else if (info
.events
== max_events
) {
653 avail
[info
.disk
.raid_disk
] = 2;
654 } else if (info
.events
== max_events
-1) {
656 avail
[info
.disk
.raid_disk
] = 1;
657 } else if (info
.events
< max_events
- 1)
659 else if (info
.events
== max_events
+1) {
663 max_events
= info
.events
;
664 for (i
=0; i
<info
.array
.raid_disks
; i
++)
667 avail
[info
.disk
.raid_disk
] = 2;
668 st
->ss
->getinfo_super(st
, bestinfo
);
669 } else { /* info.events much bigger */
671 memset(avail
, 0, info
.disk
.raid_disk
);
672 max_events
= info
.events
;
673 st
->ss
->getinfo_super(st
, bestinfo
);
676 st
->ss
->free_super(st
);
681 int IncrementalScan(int verbose
)
683 /* look at every device listed in the 'map' file.
684 * If one is found that is not running then:
685 * look in mdadm.conf for bitmap file.
686 * if one exists, but array has none, add it.
687 * try to start array in auto-readonly mode
689 struct map_ent
*mapl
= NULL
;
691 mddev_ident_t devs
, mddev
;
695 devs
= conf_get_ident(NULL
);
697 for (me
= mapl
; me
; me
= me
->next
) {
698 mdu_array_info_t array
;
699 mdu_bitmap_file_t bmf
;
701 int mdfd
= open_dev(me
->devnum
);
705 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
710 /* Ok, we can try this one. Maybe it needs a bitmap */
711 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
712 if (mddev
->devname
&& me
->path
713 && devname_matches(mddev
->devname
, me
->path
))
715 if (mddev
&& mddev
->bitmap_file
) {
717 * Note: early kernels will wrongly fail this, so it
721 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
722 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
724 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
732 ": Added bitmap %s to %s\n",
733 mddev
->bitmap_file
, me
->path
);
734 else if (errno
!= EEXIST
)
736 ": Failed to add bitmap to %s: %s\n",
737 me
->path
, strerror(errno
));
740 sra
= sysfs_read(mdfd
, 0, 0);
742 if (sysfs_set_str(sra
, NULL
,
743 "array_state", "read-auto") == 0) {
746 ": started array %s\n",
747 me
->path
?: devnum2devname(me
->devnum
));
750 ": failed to start array %s: %s\n",
751 me
->path
?: devnum2devname(me
->devnum
),
760 static char *container2devname(char *devname
)
764 if (devname
[0] == '/') {
765 int fd
= open(devname
, O_RDONLY
);
767 mdname
= devnum2devname(fd2devnum(fd
));
772 struct map_ent
*mp
, *map
= NULL
;
774 if (!parse_uuid(devname
, uuid
))
776 mp
= map_by_uuid(&map
, uuid
);
778 mdname
= devnum2devname(mp
->devnum
);
785 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
786 int runstop
, int autof
, int trustworthy
)
788 /* Collect the contents of this container and for each
789 * array, choose a device name and assemble the array.
792 struct mdinfo
*list
= st
->ss
->container_content(st
);
794 struct map_ent
*map
= NULL
;
798 for (ra
= list
; ra
; ra
= ra
->next
) {
800 char chosen_name
[1024];
802 struct mddev_ident_s
*match
= NULL
;
804 mp
= map_by_uuid(&map
, ra
->uuid
);
807 mdfd
= open_dev(mp
->devnum
);
809 strcpy(chosen_name
, mp
->path
);
811 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
814 /* Check in mdadm.conf for container == devname and
815 * member == ra->text_version after second slash.
817 char *sub
= strchr(ra
->text_version
+1, '/');
818 struct mddev_ident_s
*array_list
;
821 array_list
= conf_get_ident(NULL
);
824 for(; array_list
; array_list
= array_list
->next
) {
826 if (array_list
->member
== NULL
||
827 array_list
->container
== NULL
)
829 if (strcmp(array_list
->member
, sub
) != 0)
831 if (array_list
->uuid_set
&&
832 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
834 dn
= container2devname(array_list
->container
);
837 if (strncmp(dn
, ra
->text_version
+1,
839 ra
->text_version
[strlen(dn
)+1] != '/') {
844 /* we have a match */
847 fprintf(stderr
, Name
": match found for member %s\n",
852 if (match
&& match
->devname
&&
853 strcasecmp(match
->devname
, "<ignore>") == 0) {
855 fprintf(stderr
, Name
": array %s/%s is "
856 "explicitly ignored by mdadm.conf\n",
857 match
->container
, match
->member
);
863 mdfd
= create_mddev(match
? match
->devname
: NULL
,
871 fprintf(stderr
, Name
": failed to open %s: %s.\n",
872 chosen_name
, strerror(errno
));
876 assemble_container_content(st
, mdfd
, ra
, runstop
,
877 chosen_name
, verbose
);
884 * IncrementalRemove - Attempt to see if the passed in device belongs to any
885 * raid arrays, and if so first fail (if needed) and then remove the device.
887 * @devname - The device we want to remove
889 * Note: the device name must be a kernel name like "sda", so
890 * that we can find it in /proc/mdstat
892 int IncrementalRemove(char *devname
, int verbose
)
895 struct mdstat_ent
*ent
;
896 struct mddev_dev_s devlist
;
898 if (strchr(devname
, '/')) {
899 fprintf(stderr
, Name
": incremental removal requires a "
900 "kernel device name, not a file: %s\n", devname
);
903 ent
= mdstat_by_component(devname
);
905 fprintf(stderr
, Name
": %s does not appear to be a component "
906 "of any array\n", devname
);
909 mdfd
= open_dev(ent
->devnum
);
911 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
914 memset(&devlist
, 0, sizeof(devlist
));
915 devlist
.devname
= devname
;
916 devlist
.disposition
= 'f';
917 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
918 devlist
.disposition
= 'r';
919 return Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);