]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
37a762a1b1a900ef61f297c06bceda11e1776243
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006 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 autof
)
42 /* Add this device to an array, creating the array if necessary
43 * and starting the array if sensible or - if runstop>0 - if possible.
45 * This has several steps:
47 * 1/ Check if device is permitted by mdadm.conf, reject if not.
48 * 2/ Find metadata, reject if none appropriate (check
49 * version/name from args)
50 * 3/ Check if there is a match in mdadm.conf
51 * 3a/ if not, check for homehost match. If no match, assemble as
53 * 4/ Determine device number.
54 * - If in mdadm.conf with std name, use that
55 * - UUID in /var/run/mdadm.map use that
56 * - If name is suggestive, use that. unless in use with different uuid.
57 * - Choose a free, high number.
58 * - Use a partitioned device unless strong suggestion not to.
60 * Don't choose partitioned for containers.
61 * 5/ Find out if array already exists
63 * - choose a name, from mdadm.conf or 'name' field in array.
67 * - check one drive in array to make sure metadata is a reasonably
68 * close match. Reject if not (e.g. different type)
70 * 6/ Make sure /var/run/mdadm.map contains this array.
71 * 7/ Is there enough devices to possibly start the array?
72 * For a container, this means running Incremental_container.
73 * 7a/ if not, finish with success.
75 * - read all metadata and arrange devices like -A does
76 * - if number of OK devices match expected, or -R and there are enough,
77 * start the array (auto-readonly).
81 struct mddev_ident_s
*array_list
, *match
;
82 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 /* 3a/ if not, check for homehost match. If no match, continue
216 * but don't trust the 'name' in the array. Thus a 'random' minor
217 * number will be assigned, and the device name will be based
221 else if (homehost
== NULL
||
222 st
->ss
->match_home(st
, homehost
) != 1)
223 trustworthy
= FOREIGN
;
224 name_to_use
= strchr(info
.name
, ':');
228 name_to_use
= info
.name
;
230 /* There are three possible sources for 'autof': command line,
231 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
232 * ARRAY takes precedence, then command line, then
235 if (match
&& match
->autof
)
236 autof
= match
->autof
;
240 if (st
->ss
->container_content
&& st
->loaded_container
) {
241 /* This is a pre-built container array, so we do something
244 return Incremental_container(st
, devname
, verbose
, runstop
,
247 /* 4/ Check if array exists.
250 mp
= map_by_uuid(&map
, info
.uuid
);
252 mdfd
= open_mddev(mp
->path
, 0);
260 /* Couldn't find an existing array, maybe make a new one */
261 mdfd
= create_mddev(mp
? mp
->path
: match
? match
->devname
: NULL
,
262 info
.name
, autof
, trustworthy
, chosen_name
);
268 sysfs_init(&info
, mdfd
, 0);
270 if (set_array_info(mdfd
, st
, &info
) != 0) {
271 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
272 chosen_name
, strerror(errno
));
278 dinfo
.disk
.major
= major(stb
.st_rdev
);
279 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
280 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
281 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
282 devname
, chosen_name
, strerror(errno
));
283 ioctl(mdfd
, STOP_ARRAY
, 0);
287 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
288 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
289 /* It really should be 'none' - must be old buggy
290 * kernel, and mdadm -I may not be able to complete.
293 ioctl(mdfd
, STOP_ARRAY
, NULL
);
295 ": You have an old buggy kernel which cannot support\n"
296 " --incremental reliably. Aborting.\n");
301 info
.array
.working_disks
= 1;
303 /* 6/ Make sure /var/run/mdadm.map contains this array. */
304 map_update(&map
, fd2devnum(mdfd
),
306 info
.uuid
, chosen_name
);
309 /* - check one drive in array to make sure metadata is a reasonably */
310 /* close match. Reject if not (e.g. different type) */
311 /* - add the device */
316 struct supertype
*st2
;
317 struct mdinfo info2
, *d
;
319 strcpy(chosen_name
, mp
->path
);
321 sra
= sysfs_read(mdfd
, devnum
, (GET_DEVS
| GET_STATE
));
323 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
324 sra
->devs
->disk
.minor
);
325 dfd2
= dev_open(dn
, O_RDONLY
);
327 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
328 st
->ss
->compare_super(st
, st2
) != 0) {
330 ": metadata mismatch between %s and "
332 devname
, chosen_name
);
338 memset(&info2
, 0, sizeof(info2
));
339 st2
->ss
->getinfo_super(st2
, &info2
);
340 st2
->ss
->free_super(st2
);
341 if (info
.array
.level
!= info2
.array
.level
||
342 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
343 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
345 ": unexpected difference between %s and %s.\n",
346 chosen_name
, devname
);
350 info2
.disk
.major
= major(stb
.st_rdev
);
351 info2
.disk
.minor
= minor(stb
.st_rdev
);
352 /* add disk needs to know about containers */
353 if (st
->ss
->external
)
354 sra
->array
.level
= LEVEL_CONTAINER
;
355 err
= add_disk(mdfd
, st2
, sra
, &info2
);
356 if (err
< 0 && errno
== EBUSY
) {
357 /* could be another device present with the same
358 * disk.number. Find and reject any such
360 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
361 info
.events
, verbose
, chosen_name
);
362 err
= add_disk(mdfd
, st2
, sra
, &info2
);
365 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
366 devname
, chosen_name
, strerror(errno
));
370 info
.array
.working_disks
= 0;
371 for (d
= sra
->devs
; d
; d
=d
->next
)
372 info
.array
.working_disks
++;
376 /* 7/ Is there enough devices to possibly start the array? */
377 /* 7a/ if not, finish with success. */
378 if (info
.array
.level
== LEVEL_CONTAINER
) {
379 /* Try to assemble within the container */
382 sysfs_uevent(&info
, "change");
385 ": container %s now has %d devices\n",
386 chosen_name
, info
.array
.working_disks
);
387 return Incremental(chosen_name
, verbose
, runstop
,
388 NULL
, homehost
, autof
);
391 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
392 if (enough(info
.array
.level
, info
.array
.raid_disks
,
393 info
.array
.layout
, info
.array
.state
& 1,
394 avail
, active_disks
) == 0) {
398 ": %s attached to %s, not enough to start (%d).\n",
399 devname
, chosen_name
, active_disks
);
407 /* - if number of OK devices match expected, or -R and there */
409 /* + add any bitmap file */
410 /* + start the array (auto-readonly). */
412 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
415 ": %s attached to %s which is already active.\n",
416 devname
, chosen_name
);
423 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
425 /* Let's try to start it */
426 if (match
&& match
->bitmap_file
) {
427 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
430 ": Could not open bitmap file %s.\n",
435 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
438 ": Failed to set bitmapfile for %s.\n",
445 sra
= sysfs_read(mdfd
, devnum
, 0);
446 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
447 && trustworthy
!= FOREIGN
)
448 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
450 rv
= sysfs_set_str(sra
, NULL
,
451 "array_state", "read-auto");
455 ": %s attached to %s, which has been started.\n",
456 devname
, chosen_name
);
460 ": %s attached to %s, but failed to start: %s.\n",
461 devname
, chosen_name
, strerror(errno
));
467 ": %s attached to %s, not enough to start safely.\n",
468 devname
, chosen_name
);
475 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
476 int number
, __u64 events
, int verbose
,
479 /* Find a device attached to this array with a disk.number of number
480 * and events less than the passed events, and remove the device.
485 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
486 return; /* not safe to remove from active arrays
487 * without thinking more */
489 for (d
= sra
->devs
; d
; d
= d
->next
) {
493 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
494 dfd
= dev_open(dn
, O_RDONLY
);
497 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
501 st
->ss
->getinfo_super(st
, &info
);
502 st
->ss
->free_super(st
);
505 if (info
.disk
.number
!= number
||
506 info
.events
>= events
)
509 if (d
->disk
.raid_disk
> -1)
510 sysfs_set_str(sra
, d
, "slot", "none");
511 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
514 ": removing old device %s from %s\n",
515 d
->sys_name
+4, array_name
);
519 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
520 struct mdinfo
*bestinfo
)
522 /* count how many devices in sra think they are active */
524 int cnt
= 0, cnt1
= 0;
525 __u64 max_events
= 0;
526 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
529 for (d
= sra
->devs
; d
; d
= d
->next
) {
535 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
536 dfd
= dev_open(dn
, O_RDONLY
);
539 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
543 st
->ss
->getinfo_super(st
, &info
);
545 avail
= malloc(info
.array
.raid_disks
);
547 fprintf(stderr
, Name
": out of memory.\n");
550 memset(avail
, 0, info
.array
.raid_disks
);
554 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
558 max_events
= info
.events
;
559 avail
[info
.disk
.raid_disk
] = 2;
560 st
->ss
->getinfo_super(st
, bestinfo
);
561 } else if (info
.events
== max_events
) {
563 avail
[info
.disk
.raid_disk
] = 2;
564 } else if (info
.events
== max_events
-1) {
566 avail
[info
.disk
.raid_disk
] = 1;
567 } else if (info
.events
< max_events
- 1)
569 else if (info
.events
== max_events
+1) {
573 max_events
= info
.events
;
574 for (i
=0; i
<info
.array
.raid_disks
; i
++)
577 avail
[info
.disk
.raid_disk
] = 2;
578 st
->ss
->getinfo_super(st
, bestinfo
);
579 } else { /* info.events much bigger */
581 memset(avail
, 0, info
.disk
.raid_disk
);
582 max_events
= info
.events
;
583 st
->ss
->getinfo_super(st
, bestinfo
);
586 st
->ss
->free_super(st
);
591 void RebuildMap(void)
593 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
594 struct mdstat_ent
*md
;
595 struct map_ent
*map
= NULL
;
596 int mdp
= get_mdp_major();
598 for (md
= mdstat
; md
; md
= md
->next
) {
599 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
602 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
606 struct supertype
*st
;
610 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
611 dfd
= dev_open(dn
, O_RDONLY
);
614 st
= guess_super(dfd
);
618 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
622 st
->ss
->getinfo_super(st
, &info
);
624 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
626 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
627 map_add(&map
, md
->devnum
,
629 info
.uuid
, path
? : "/unknown");
630 st
->ss
->free_super(st
);
637 for (md
= mdstat
; md
; md
= md
->next
) {
638 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_VERSION
);
639 sysfs_uevent(sra
, "change");
644 int IncrementalScan(int verbose
)
646 /* look at every device listed in the 'map' file.
647 * If one is found that is not running then:
648 * look in mdadm.conf for bitmap file.
649 * if one exists, but array has none, add it.
650 * try to start array in auto-readonly mode
652 struct map_ent
*mapl
= NULL
;
654 mddev_ident_t devs
, mddev
;
658 devs
= conf_get_ident(NULL
);
660 for (me
= mapl
; me
; me
= me
->next
) {
661 mdu_array_info_t array
;
662 mdu_bitmap_file_t bmf
;
664 int mdfd
= open_mddev(me
->path
, 0);
668 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
673 /* Ok, we can try this one. Maybe it needs a bitmap */
674 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
676 && strcmp(mddev
->devname
, me
->path
) == 0)
678 if (mddev
&& mddev
->bitmap_file
) {
680 * Note: early kernels will wrongly fail this, so it
684 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
685 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
687 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
695 ": Added bitmap %s to %s\n",
696 mddev
->bitmap_file
, me
->path
);
697 else if (errno
!= EEXIST
)
699 ": Failed to add bitmap to %s: %s\n",
700 me
->path
, strerror(errno
));
703 sra
= sysfs_read(mdfd
, 0, 0);
705 if (sysfs_set_str(sra
, NULL
,
706 "array_state", "read-auto") == 0) {
709 ": started array %s\n",
713 ": failed to start array %s: %s\n",
714 me
->path
, strerror(errno
));
722 static char *container2devname(char *devname
)
726 if (devname
[0] == '/') {
727 int fd
= open(devname
, O_RDONLY
);
729 mdname
= devnum2devname(fd2devnum(fd
));
734 struct map_ent
*mp
, *map
= NULL
;
736 if (!parse_uuid(devname
, uuid
))
738 mp
= map_by_uuid(&map
, uuid
);
740 mdname
= devnum2devname(mp
->devnum
);
747 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
748 int runstop
, int autof
, int trustworthy
)
750 /* Collect the contents of this container and for each
751 * array, choose a device name and assemble the array.
754 struct mdinfo
*list
= st
->ss
->container_content(st
);
756 struct map_ent
*map
= NULL
;
760 for (ra
= list
; ra
; ra
= ra
->next
) {
761 struct mdinfo
*dev
, *sra
;
763 char chosen_name
[1024];
764 int working
= 0, preexist
= 0;
766 struct mddev_ident_s
*match
= NULL
;
768 mp
= map_by_uuid(&map
, ra
->uuid
);
772 /* Check in mdadm.conf for devices == devname and
773 * member == ra->text_version after second slash.
775 char *sub
= strchr(ra
->text_version
+1, '/');
776 struct mddev_ident_s
*array_list
;
779 array_list
= conf_get_ident(NULL
);
782 for(; array_list
; array_list
= array_list
->next
) {
784 if (array_list
->member
== NULL
||
785 array_list
->container
== NULL
)
787 if (strcmp(array_list
->member
, sub
) != 0)
789 if (array_list
->uuid_set
&&
790 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
792 dn
= container2devname(array_list
->container
);
795 if (strncmp(dn
, ra
->text_version
+1,
797 ra
->text_version
[strlen(dn
)+1] != '/') {
802 /* we have a match */
805 fprintf(stderr
, Name
": match found for member %s\n",
811 mdfd
= create_mddev(match
? match
->devname
: NULL
,
818 fprintf(stderr
, Name
": failed to open %s: %s.\n",
819 chosen_name
, strerror(errno
));
824 sysfs_init(ra
, mdfd
, 0);
826 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
827 if (sra
== NULL
|| strcmp(sra
->text_version
, ra
->text_version
) != 0)
828 if (sysfs_set_array(ra
, md_get_version(mdfd
)) != 0)
833 for (dev
= ra
->devs
; dev
; dev
= dev
->next
)
834 if (sysfs_add_disk(ra
, dev
) == 0)
836 else if (errno
== EEXIST
)
839 /* Nothing new, don't try to start */ ;
840 else if (runstop
> 0 ||
841 (working
+ preexist
) >= ra
->array
.working_disks
) {
842 switch(ra
->array
.level
) {
844 case LEVEL_MULTIPATH
:
846 sysfs_set_str(ra
, NULL
, "array_state",
850 sysfs_set_str(ra
, NULL
, "array_state",
852 /* start mdmon if needed. */
853 if (!mdmon_running(st
->container_dev
))
854 start_mdmon(st
->container_dev
);
855 ping_monitor(devnum2devname(st
->container_dev
));
858 sysfs_set_safemode(ra
, ra
->safe_mode_delay
);
861 ": Started %s with %d devices",
862 chosen_name
, working
+ preexist
);
864 fprintf(stderr
, " (%d new)", working
);
865 fprintf(stderr
, "\n");
867 /* FIXME should have an O_EXCL and wait for read-auto */
871 ": %s assembled with %d devices but "
873 chosen_name
, working
);
874 map_update(&map
, fd2devnum(mdfd
),
876 ra
->uuid
, chosen_name
);