]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
c5ec6340e0efe5c9a4ae865ebee27de1a379eea8
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 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 if (!match
&& !conf_test_metadata(st
->ss
->name
)) {
227 ": %s has metadata type %s for which "
228 "auto-assembly is disabled\n",
229 devname
, st
->ss
->name
);
233 /* 3a/ if not, check for homehost match. If no match, continue
234 * but don't trust the 'name' in the array. Thus a 'random' minor
235 * number will be assigned, and the device name will be based
239 else if ((homehost
== NULL
||
240 st
->ss
->match_home(st
, homehost
) != 1) &&
241 st
->ss
->match_home(st
, "any") != 1)
242 trustworthy
= FOREIGN
;
246 /* There are three possible sources for 'autof': command line,
247 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
248 * ARRAY takes precedence, then command line, then
251 if (match
&& match
->autof
)
252 autof
= match
->autof
;
256 if (st
->ss
->container_content
&& st
->loaded_container
) {
257 /* This is a pre-built container array, so we do something
260 return Incremental_container(st
, devname
, verbose
, runstop
,
263 name_to_use
= strchr(info
.name
, ':');
267 name_to_use
= info
.name
;
269 if (name_to_use
[0] == 0 &&
270 info
.array
.level
== LEVEL_CONTAINER
&&
271 trustworthy
== LOCAL
) {
272 name_to_use
= info
.text_version
;
273 trustworthy
= METADATA
;
275 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
276 ! require_homehost
&&
277 conf_name_is_free(name_to_use
))
280 /* 4/ Check if array exists.
283 mp
= map_by_uuid(&map
, info
.uuid
);
285 mdfd
= open_mddev(mp
->path
, 0);
286 if (mdfd
< 0 && mddev_busy(mp
->devnum
)) {
287 /* maybe udev hasn't created it yet. */
289 sprintf(buf
, "%d:%d", dev2major(mp
->devnum
),
290 dev2minor(mp
->devnum
));
291 mdfd
= dev_open(buf
, O_RDWR
);
300 /* Couldn't find an existing array, maybe make a new one */
301 mdfd
= create_mddev(match
? match
->devname
: NULL
,
302 name_to_use
, autof
, trustworthy
, chosen_name
);
307 sysfs_init(&info
, mdfd
, 0);
309 if (set_array_info(mdfd
, st
, &info
) != 0) {
310 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
311 chosen_name
, strerror(errno
));
317 dinfo
.disk
.major
= major(stb
.st_rdev
);
318 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
319 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
320 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
321 devname
, chosen_name
, strerror(errno
));
322 ioctl(mdfd
, STOP_ARRAY
, 0);
326 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
327 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
328 /* It really should be 'none' - must be old buggy
329 * kernel, and mdadm -I may not be able to complete.
332 ioctl(mdfd
, STOP_ARRAY
, NULL
);
334 ": You have an old buggy kernel which cannot support\n"
335 " --incremental reliably. Aborting.\n");
340 info
.array
.working_disks
= 1;
342 /* 6/ Make sure /var/run/mdadm.map contains this array. */
343 map_update(&map
, fd2devnum(mdfd
),
345 info
.uuid
, chosen_name
);
348 /* - check one drive in array to make sure metadata is a reasonably */
349 /* close match. Reject if not (e.g. different type) */
350 /* - add the device */
355 struct supertype
*st2
;
356 struct mdinfo info2
, *d
;
358 strcpy(chosen_name
, mp
->path
);
360 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
363 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
364 sra
->devs
->disk
.minor
);
365 dfd2
= dev_open(dn
, O_RDONLY
);
367 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
368 st
->ss
->compare_super(st
, st2
) != 0) {
370 ": metadata mismatch between %s and "
372 devname
, chosen_name
);
378 memset(&info2
, 0, sizeof(info2
));
379 st2
->ss
->getinfo_super(st2
, &info2
);
380 st2
->ss
->free_super(st2
);
381 if (info
.array
.level
!= info2
.array
.level
||
382 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
383 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
385 ": unexpected difference between %s and %s.\n",
386 chosen_name
, devname
);
391 info2
.disk
.major
= major(stb
.st_rdev
);
392 info2
.disk
.minor
= minor(stb
.st_rdev
);
393 /* add disk needs to know about containers */
394 if (st
->ss
->external
)
395 sra
->array
.level
= LEVEL_CONTAINER
;
396 err
= add_disk(mdfd
, st2
, sra
, &info2
);
397 if (err
< 0 && errno
== EBUSY
) {
398 /* could be another device present with the same
399 * disk.number. Find and reject any such
401 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
402 info
.events
, verbose
, chosen_name
);
403 err
= add_disk(mdfd
, st2
, sra
, &info2
);
406 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
407 devname
, chosen_name
, strerror(errno
));
411 info
.array
.working_disks
= 0;
412 for (d
= sra
->devs
; d
; d
=d
->next
)
413 info
.array
.working_disks
++;
417 /* 7/ Is there enough devices to possibly start the array? */
418 /* 7a/ if not, finish with success. */
419 if (info
.array
.level
== LEVEL_CONTAINER
) {
420 /* Try to assemble within the container */
422 sysfs_uevent(&info
, "change");
425 ": container %s now has %d devices\n",
426 chosen_name
, info
.array
.working_disks
);
427 wait_for(chosen_name
, mdfd
);
430 return 0; /* don't try to assemble */
431 rv
= Incremental(chosen_name
, verbose
, runstop
,
432 NULL
, homehost
, require_homehost
, autof
);
434 /* Don't fail the whole -I if a subarray didn't
435 * have enough devices to start yet
441 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
442 if (enough(info
.array
.level
, info
.array
.raid_disks
,
443 info
.array
.layout
, info
.array
.state
& 1,
444 avail
, active_disks
) == 0 ||
445 (runstop
< 0 && active_disks
< info
.array
.raid_disks
)) {
449 ": %s attached to %s, not enough to start (%d).\n",
450 devname
, chosen_name
, active_disks
);
458 /* - if number of OK devices match expected, or -R and there */
460 /* + add any bitmap file */
461 /* + start the array (auto-readonly). */
463 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
466 ": %s attached to %s which is already active.\n",
467 devname
, chosen_name
);
474 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
476 /* Let's try to start it */
477 if (match
&& match
->bitmap_file
) {
478 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
481 ": Could not open bitmap file %s.\n",
486 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
489 ": Failed to set bitmapfile for %s.\n",
496 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), 0);
497 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
498 && trustworthy
!= FOREIGN
)
499 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
501 rv
= sysfs_set_str(sra
, NULL
,
502 "array_state", "read-auto");
506 ": %s attached to %s, which has been started.\n",
507 devname
, chosen_name
);
509 wait_for(chosen_name
, mdfd
);
512 ": %s attached to %s, but failed to start: %s.\n",
513 devname
, chosen_name
, strerror(errno
));
519 ": %s attached to %s, not enough to start safely.\n",
520 devname
, chosen_name
);
527 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
528 int number
, __u64 events
, int verbose
,
531 /* Find a device attached to this array with a disk.number of number
532 * and events less than the passed events, and remove the device.
537 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
538 return; /* not safe to remove from active arrays
539 * without thinking more */
541 for (d
= sra
->devs
; d
; d
= d
->next
) {
545 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
546 dfd
= dev_open(dn
, O_RDONLY
);
549 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
553 st
->ss
->getinfo_super(st
, &info
);
554 st
->ss
->free_super(st
);
557 if (info
.disk
.number
!= number
||
558 info
.events
>= events
)
561 if (d
->disk
.raid_disk
> -1)
562 sysfs_set_str(sra
, d
, "slot", "none");
563 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
566 ": removing old device %s from %s\n",
567 d
->sys_name
+4, array_name
);
571 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
572 struct mdinfo
*bestinfo
)
574 /* count how many devices in sra think they are active */
576 int cnt
= 0, cnt1
= 0;
577 __u64 max_events
= 0;
578 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
581 for (d
= sra
->devs
; d
; d
= d
->next
) {
587 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
588 dfd
= dev_open(dn
, O_RDONLY
);
591 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
595 st
->ss
->getinfo_super(st
, &info
);
597 avail
= malloc(info
.array
.raid_disks
);
599 fprintf(stderr
, Name
": out of memory.\n");
602 memset(avail
, 0, info
.array
.raid_disks
);
606 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
610 max_events
= info
.events
;
611 avail
[info
.disk
.raid_disk
] = 2;
612 st
->ss
->getinfo_super(st
, bestinfo
);
613 } else if (info
.events
== max_events
) {
615 avail
[info
.disk
.raid_disk
] = 2;
616 } else if (info
.events
== max_events
-1) {
618 avail
[info
.disk
.raid_disk
] = 1;
619 } else if (info
.events
< max_events
- 1)
621 else if (info
.events
== max_events
+1) {
625 max_events
= info
.events
;
626 for (i
=0; i
<info
.array
.raid_disks
; i
++)
629 avail
[info
.disk
.raid_disk
] = 2;
630 st
->ss
->getinfo_super(st
, bestinfo
);
631 } else { /* info.events much bigger */
633 memset(avail
, 0, info
.disk
.raid_disk
);
634 max_events
= info
.events
;
635 st
->ss
->getinfo_super(st
, bestinfo
);
638 st
->ss
->free_super(st
);
643 int IncrementalScan(int verbose
)
645 /* look at every device listed in the 'map' file.
646 * If one is found that is not running then:
647 * look in mdadm.conf for bitmap file.
648 * if one exists, but array has none, add it.
649 * try to start array in auto-readonly mode
651 struct map_ent
*mapl
= NULL
;
653 mddev_ident_t devs
, mddev
;
657 devs
= conf_get_ident(NULL
);
659 for (me
= mapl
; me
; me
= me
->next
) {
660 mdu_array_info_t array
;
661 mdu_bitmap_file_t bmf
;
663 int mdfd
= open_mddev(me
->path
, 0);
667 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
672 /* Ok, we can try this one. Maybe it needs a bitmap */
673 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
675 && strcmp(mddev
->devname
, me
->path
) == 0)
677 if (mddev
&& mddev
->bitmap_file
) {
679 * Note: early kernels will wrongly fail this, so it
683 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
684 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
686 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
694 ": Added bitmap %s to %s\n",
695 mddev
->bitmap_file
, me
->path
);
696 else if (errno
!= EEXIST
)
698 ": Failed to add bitmap to %s: %s\n",
699 me
->path
, strerror(errno
));
702 sra
= sysfs_read(mdfd
, 0, 0);
704 if (sysfs_set_str(sra
, NULL
,
705 "array_state", "read-auto") == 0) {
708 ": started array %s\n",
712 ": failed to start array %s: %s\n",
713 me
->path
, strerror(errno
));
721 static char *container2devname(char *devname
)
725 if (devname
[0] == '/') {
726 int fd
= open(devname
, O_RDONLY
);
728 mdname
= devnum2devname(fd2devnum(fd
));
733 struct map_ent
*mp
, *map
= NULL
;
735 if (!parse_uuid(devname
, uuid
))
737 mp
= map_by_uuid(&map
, uuid
);
739 mdname
= devnum2devname(mp
->devnum
);
746 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
747 int runstop
, int autof
, int trustworthy
)
749 /* Collect the contents of this container and for each
750 * array, choose a device name and assemble the array.
753 struct mdinfo
*list
= st
->ss
->container_content(st
);
755 struct map_ent
*map
= NULL
;
759 for (ra
= list
; ra
; ra
= ra
->next
) {
761 char chosen_name
[1024];
763 struct mddev_ident_s
*match
= NULL
;
765 mp
= map_by_uuid(&map
, ra
->uuid
);
768 mdfd
= open_dev(mp
->devnum
);
769 strcpy(chosen_name
, mp
->path
);
772 /* Check in mdadm.conf for container == 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",
810 if (match
&& match
->devname
&&
811 strcasecmp(match
->devname
, "<ignore>") == 0) {
813 fprintf(stderr
, Name
": array %s/%s is "
814 "explicitly ignored by mdadm.conf\n",
815 match
->container
, match
->member
);
821 mdfd
= create_mddev(match
? match
->devname
: NULL
,
829 fprintf(stderr
, Name
": failed to open %s: %s.\n",
830 chosen_name
, strerror(errno
));
834 assemble_container_content(st
, mdfd
, ra
, runstop
,
835 chosen_name
, verbose
);