]>
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 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];
84 struct map_ent
*mp
, *map
= NULL
;
88 int trustworthy
= FOREIGN
;
90 mdu_array_info_t ainf
;
92 struct createinfo
*ci
= conf_get_create_info();
95 /* 1/ Check if device is permitted by mdadm.conf */
97 if (!conf_test_dev(devname
)) {
100 ": %s not permitted by mdadm.conf.\n",
105 /* 2/ Find metadata, reject if none appropriate (check
106 * version/name from args) */
108 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
111 fprintf(stderr
, Name
": cannot open %s: %s.\n",
112 devname
, strerror(errno
));
115 if (fstat(dfd
, &stb
) < 0) {
117 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
118 devname
, strerror(errno
));
122 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
124 fprintf(stderr
, Name
": %s is not a block device.\n",
130 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
133 ": no recognisable superblock on %s.\n",
138 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
140 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
147 memset(&info
, 0, sizeof(info
));
148 st
->ss
->getinfo_super(st
, &info
);
149 /* 3/ Check if there is a match in mdadm.conf */
151 array_list
= conf_get_ident(NULL
);
153 for (; array_list
; array_list
= array_list
->next
) {
154 if (array_list
->uuid_set
&&
155 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
157 if (verbose
>= 2 && array_list
->devname
)
159 ": UUID differs from %s.\n",
160 array_list
->devname
);
163 if (array_list
->name
[0] &&
164 strcasecmp(array_list
->name
, info
.name
) != 0) {
165 if (verbose
>= 2 && array_list
->devname
)
167 ": Name differs from %s.\n",
168 array_list
->devname
);
171 if (array_list
->devices
&&
172 !match_oneof(array_list
->devices
, devname
)) {
173 if (verbose
>= 2 && array_list
->devname
)
175 ": Not a listed device for %s.\n",
176 array_list
->devname
);
179 if (array_list
->super_minor
!= UnSet
&&
180 array_list
->super_minor
!= info
.array
.md_minor
) {
181 if (verbose
>= 2 && array_list
->devname
)
183 ": Different super-minor to %s.\n",
184 array_list
->devname
);
187 if (!array_list
->uuid_set
&&
188 !array_list
->name
[0] &&
189 !array_list
->devices
&&
190 array_list
->super_minor
== UnSet
) {
191 if (verbose
>= 2 && array_list
->devname
)
193 ": %s doesn't have any identifying information.\n",
194 array_list
->devname
);
197 /* FIXME, should I check raid_disks and level too?? */
201 if (match
->devname
&& array_list
->devname
)
203 ": we match both %s and %s - cannot decide which to use.\n",
204 match
->devname
, array_list
->devname
);
207 ": multiple lines in mdadm.conf match\n");
214 if (match
&& match
->devname
215 && strcasecmp(match
->devname
, "<ignore>") == 0) {
217 fprintf(stderr
, Name
": array containing %s is explicitly"
218 " ignored by mdadm.conf\n",
223 /* 3a/ if not, check for homehost match. If no match, continue
224 * but don't trust the 'name' in the array. Thus a 'random' minor
225 * number will be assigned, and the device name will be based
229 else if ((homehost
== NULL
||
230 st
->ss
->match_home(st
, homehost
) != 1) &&
231 st
->ss
->match_home(st
, "any") != 1)
232 trustworthy
= FOREIGN
;
236 /* There are three possible sources for 'autof': command line,
237 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
238 * ARRAY takes precedence, then command line, then
241 if (match
&& match
->autof
)
242 autof
= match
->autof
;
246 if (st
->ss
->container_content
&& st
->loaded_container
) {
247 /* This is a pre-built container array, so we do something
250 return Incremental_container(st
, devname
, verbose
, runstop
,
253 name_to_use
= strchr(info
.name
, ':');
257 name_to_use
= info
.name
;
259 if ((!name_to_use
|| name_to_use
[0] == 0) &&
260 info
.array
.level
== LEVEL_CONTAINER
&&
261 trustworthy
== LOCAL
) {
262 name_to_use
= info
.text_version
;
263 trustworthy
= METADATA
;
266 /* 4/ Check if array exists.
269 mp
= map_by_uuid(&map
, info
.uuid
);
271 mdfd
= open_mddev(mp
->path
, 0);
272 if (mdfd
< 0 && mddev_busy(mp
->devnum
)) {
273 /* maybe udev hasn't created it yet. */
275 sprintf(buf
, "%d:%d", dev2major(mp
->devnum
),
276 dev2minor(mp
->devnum
));
277 mdfd
= dev_open(buf
, O_RDWR
);
286 /* Couldn't find an existing array, maybe make a new one */
287 mdfd
= create_mddev(match
? match
->devname
: NULL
,
288 name_to_use
, autof
, trustworthy
, chosen_name
);
293 sysfs_init(&info
, mdfd
, 0);
295 if (set_array_info(mdfd
, st
, &info
) != 0) {
296 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
297 chosen_name
, strerror(errno
));
303 dinfo
.disk
.major
= major(stb
.st_rdev
);
304 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
305 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
306 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
307 devname
, chosen_name
, strerror(errno
));
308 ioctl(mdfd
, STOP_ARRAY
, 0);
312 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
313 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
314 /* It really should be 'none' - must be old buggy
315 * kernel, and mdadm -I may not be able to complete.
318 ioctl(mdfd
, STOP_ARRAY
, NULL
);
320 ": You have an old buggy kernel which cannot support\n"
321 " --incremental reliably. Aborting.\n");
326 info
.array
.working_disks
= 1;
328 /* 6/ Make sure /var/run/mdadm.map contains this array. */
329 map_update(&map
, fd2devnum(mdfd
),
331 info
.uuid
, chosen_name
);
334 /* - check one drive in array to make sure metadata is a reasonably */
335 /* close match. Reject if not (e.g. different type) */
336 /* - add the device */
341 struct supertype
*st2
;
342 struct mdinfo info2
, *d
;
344 strcpy(chosen_name
, mp
->path
);
346 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
349 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
350 sra
->devs
->disk
.minor
);
351 dfd2
= dev_open(dn
, O_RDONLY
);
353 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
354 st
->ss
->compare_super(st
, st2
) != 0) {
356 ": metadata mismatch between %s and "
358 devname
, chosen_name
);
364 memset(&info2
, 0, sizeof(info2
));
365 st2
->ss
->getinfo_super(st2
, &info2
);
366 st2
->ss
->free_super(st2
);
367 if (info
.array
.level
!= info2
.array
.level
||
368 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
369 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
371 ": unexpected difference between %s and %s.\n",
372 chosen_name
, devname
);
377 info2
.disk
.major
= major(stb
.st_rdev
);
378 info2
.disk
.minor
= minor(stb
.st_rdev
);
379 /* add disk needs to know about containers */
380 if (st
->ss
->external
)
381 sra
->array
.level
= LEVEL_CONTAINER
;
382 err
= add_disk(mdfd
, st2
, sra
, &info2
);
383 if (err
< 0 && errno
== EBUSY
) {
384 /* could be another device present with the same
385 * disk.number. Find and reject any such
387 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
388 info
.events
, verbose
, chosen_name
);
389 err
= add_disk(mdfd
, st2
, sra
, &info2
);
392 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
393 devname
, chosen_name
, strerror(errno
));
397 info
.array
.working_disks
= 0;
398 for (d
= sra
->devs
; d
; d
=d
->next
)
399 info
.array
.working_disks
++;
403 /* 7/ Is there enough devices to possibly start the array? */
404 /* 7a/ if not, finish with success. */
405 if (info
.array
.level
== LEVEL_CONTAINER
) {
406 /* Try to assemble within the container */
408 sysfs_uevent(&info
, "change");
411 ": container %s now has %d devices\n",
412 chosen_name
, info
.array
.working_disks
);
413 wait_for(chosen_name
, mdfd
);
416 return 0; /* don't try to assemble */
417 rv
= Incremental(chosen_name
, verbose
, runstop
,
418 NULL
, homehost
, autof
);
420 /* Don't fail the whole -I if a subarray didn't
421 * have enough devices to start yet
427 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
428 if (enough(info
.array
.level
, info
.array
.raid_disks
,
429 info
.array
.layout
, info
.array
.state
& 1,
430 avail
, active_disks
) == 0 ||
431 (runstop
< 0 && active_disks
< info
.array
.raid_disks
)) {
435 ": %s attached to %s, not enough to start (%d).\n",
436 devname
, chosen_name
, active_disks
);
444 /* - if number of OK devices match expected, or -R and there */
446 /* + add any bitmap file */
447 /* + start the array (auto-readonly). */
449 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
452 ": %s attached to %s which is already active.\n",
453 devname
, chosen_name
);
460 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
462 /* Let's try to start it */
463 if (match
&& match
->bitmap_file
) {
464 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
467 ": Could not open bitmap file %s.\n",
472 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
475 ": Failed to set bitmapfile for %s.\n",
482 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), 0);
483 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
484 && trustworthy
!= FOREIGN
)
485 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
487 rv
= sysfs_set_str(sra
, NULL
,
488 "array_state", "read-auto");
492 ": %s attached to %s, which has been started.\n",
493 devname
, chosen_name
);
495 wait_for(chosen_name
, mdfd
);
498 ": %s attached to %s, but failed to start: %s.\n",
499 devname
, chosen_name
, strerror(errno
));
505 ": %s attached to %s, not enough to start safely.\n",
506 devname
, chosen_name
);
513 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
514 int number
, __u64 events
, int verbose
,
517 /* Find a device attached to this array with a disk.number of number
518 * and events less than the passed events, and remove the device.
523 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
524 return; /* not safe to remove from active arrays
525 * without thinking more */
527 for (d
= sra
->devs
; d
; d
= d
->next
) {
531 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
532 dfd
= dev_open(dn
, O_RDONLY
);
535 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
539 st
->ss
->getinfo_super(st
, &info
);
540 st
->ss
->free_super(st
);
543 if (info
.disk
.number
!= number
||
544 info
.events
>= events
)
547 if (d
->disk
.raid_disk
> -1)
548 sysfs_set_str(sra
, d
, "slot", "none");
549 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
552 ": removing old device %s from %s\n",
553 d
->sys_name
+4, array_name
);
557 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
558 struct mdinfo
*bestinfo
)
560 /* count how many devices in sra think they are active */
562 int cnt
= 0, cnt1
= 0;
563 __u64 max_events
= 0;
564 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
567 for (d
= sra
->devs
; d
; d
= d
->next
) {
573 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
574 dfd
= dev_open(dn
, O_RDONLY
);
577 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
581 st
->ss
->getinfo_super(st
, &info
);
583 avail
= malloc(info
.array
.raid_disks
);
585 fprintf(stderr
, Name
": out of memory.\n");
588 memset(avail
, 0, info
.array
.raid_disks
);
592 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
596 max_events
= info
.events
;
597 avail
[info
.disk
.raid_disk
] = 2;
598 st
->ss
->getinfo_super(st
, bestinfo
);
599 } else if (info
.events
== max_events
) {
601 avail
[info
.disk
.raid_disk
] = 2;
602 } else if (info
.events
== max_events
-1) {
604 avail
[info
.disk
.raid_disk
] = 1;
605 } else if (info
.events
< max_events
- 1)
607 else if (info
.events
== max_events
+1) {
611 max_events
= info
.events
;
612 for (i
=0; i
<info
.array
.raid_disks
; i
++)
615 avail
[info
.disk
.raid_disk
] = 2;
616 st
->ss
->getinfo_super(st
, bestinfo
);
617 } else { /* info.events much bigger */
619 memset(avail
, 0, info
.disk
.raid_disk
);
620 max_events
= info
.events
;
621 st
->ss
->getinfo_super(st
, bestinfo
);
624 st
->ss
->free_super(st
);
629 int IncrementalScan(int verbose
)
631 /* look at every device listed in the 'map' file.
632 * If one is found that is not running then:
633 * look in mdadm.conf for bitmap file.
634 * if one exists, but array has none, add it.
635 * try to start array in auto-readonly mode
637 struct map_ent
*mapl
= NULL
;
639 mddev_ident_t devs
, mddev
;
643 devs
= conf_get_ident(NULL
);
645 for (me
= mapl
; me
; me
= me
->next
) {
646 mdu_array_info_t array
;
647 mdu_bitmap_file_t bmf
;
649 int mdfd
= open_mddev(me
->path
, 0);
653 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
658 /* Ok, we can try this one. Maybe it needs a bitmap */
659 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
661 && strcmp(mddev
->devname
, me
->path
) == 0)
663 if (mddev
&& mddev
->bitmap_file
) {
665 * Note: early kernels will wrongly fail this, so it
669 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
670 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
672 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
680 ": Added bitmap %s to %s\n",
681 mddev
->bitmap_file
, me
->path
);
682 else if (errno
!= EEXIST
)
684 ": Failed to add bitmap to %s: %s\n",
685 me
->path
, strerror(errno
));
688 sra
= sysfs_read(mdfd
, 0, 0);
690 if (sysfs_set_str(sra
, NULL
,
691 "array_state", "read-auto") == 0) {
694 ": started array %s\n",
698 ": failed to start array %s: %s\n",
699 me
->path
, strerror(errno
));
707 static char *container2devname(char *devname
)
711 if (devname
[0] == '/') {
712 int fd
= open(devname
, O_RDONLY
);
714 mdname
= devnum2devname(fd2devnum(fd
));
719 struct map_ent
*mp
, *map
= NULL
;
721 if (!parse_uuid(devname
, uuid
))
723 mp
= map_by_uuid(&map
, uuid
);
725 mdname
= devnum2devname(mp
->devnum
);
732 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
733 int runstop
, int autof
, int trustworthy
)
735 /* Collect the contents of this container and for each
736 * array, choose a device name and assemble the array.
739 struct mdinfo
*list
= st
->ss
->container_content(st
);
741 struct map_ent
*map
= NULL
;
745 for (ra
= list
; ra
; ra
= ra
->next
) {
747 char chosen_name
[1024];
749 struct mddev_ident_s
*match
= NULL
;
751 mp
= map_by_uuid(&map
, ra
->uuid
);
754 mdfd
= open_dev(mp
->devnum
);
755 strcpy(chosen_name
, mp
->path
);
758 /* Check in mdadm.conf for container == devname and
759 * member == ra->text_version after second slash.
761 char *sub
= strchr(ra
->text_version
+1, '/');
762 struct mddev_ident_s
*array_list
;
765 array_list
= conf_get_ident(NULL
);
768 for(; array_list
; array_list
= array_list
->next
) {
770 if (array_list
->member
== NULL
||
771 array_list
->container
== NULL
)
773 if (strcmp(array_list
->member
, sub
) != 0)
775 if (array_list
->uuid_set
&&
776 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
778 dn
= container2devname(array_list
->container
);
781 if (strncmp(dn
, ra
->text_version
+1,
783 ra
->text_version
[strlen(dn
)+1] != '/') {
788 /* we have a match */
791 fprintf(stderr
, Name
": match found for member %s\n",
796 if (match
&& match
->devname
&&
797 strcasecmp(match
->devname
, "<ignore>") == 0) {
799 fprintf(stderr
, Name
": array %s/%s is "
800 "explicitly ignored by mdadm.conf\n",
801 match
->container
, match
->member
);
805 mdfd
= create_mddev(match
? match
->devname
: NULL
,
813 fprintf(stderr
, Name
": failed to open %s: %s.\n",
814 chosen_name
, strerror(errno
));
818 assemble_container_content(st
, mdfd
, ra
, runstop
,
819 chosen_name
, verbose
);