]>
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];
85 struct map_ent
*mp
, *map
= NULL
;
89 int trustworthy
= FOREIGN
;
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
)
159 ": UUID differs from %s.\n",
160 array_list
->devname
);
163 if (array_list
->name
[0] &&
164 strcasecmp(array_list
->name
, info
.name
) != 0) {
167 ": Name differs from %s.\n",
168 array_list
->devname
);
171 if (array_list
->devices
&&
172 !match_oneof(array_list
->devices
, 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
) {
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
) {
193 ": %s doesn't have any identifying information.\n",
194 array_list
->devname
);
197 /* FIXME, should I check raid_disks and level too?? */
202 ": we match both %s and %s - cannot decide which to use.\n",
203 match
->devname
, array_list
->devname
);
209 /* 3a/ if not, check for homehost match. If no match, continue
210 * but don't trust the 'name' in the array. Thus a 'random' minor
211 * number will be assigned, and the device name will be based
215 else if (homehost
== NULL
||
216 st
->ss
->match_home(st
, homehost
) != 1)
217 trustworthy
= FOREIGN
;
218 name_to_use
= strchr(info
.name
, ':');
222 name_to_use
= info
.name
;
224 /* There are three possible sources for 'autof': command line,
225 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
226 * ARRAY takes precedence, then command line, then
229 if (match
&& match
->autof
)
230 autof
= match
->autof
;
234 if (st
->ss
->container_content
&& st
->loaded_container
) {
235 /* This is a pre-built container array, so we do something
238 return Incremental_container(st
, devname
, verbose
, runstop
,
241 /* 4/ Check is array exists.
243 mp
= map_by_uuid(&map
, info
.uuid
);
245 mdfd
= open_mddev(mp
->path
, 0);
253 /* Couldn't find an existing array, maybe make a new one */
254 mdfd
= create_mddev(mp
? mp
->path
: match
? match
->devname
: NULL
,
255 info
.name
, autof
, trustworthy
, chosen_name
);
261 sysfs_init(&info
, mdfd
, 0);
263 if (set_array_info(mdfd
, st
, &info
) != 0) {
264 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
265 chosen_name
, strerror(errno
));
271 dinfo
.disk
.major
= major(stb
.st_rdev
);
272 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
273 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
274 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
275 devname
, chosen_name
, strerror(errno
));
276 ioctl(mdfd
, STOP_ARRAY
, 0);
280 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
281 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
282 /* It really should be 'none' - must be old buggy
283 * kernel, and mdadm -I may not be able to complete.
286 ioctl(mdfd
, STOP_ARRAY
, NULL
);
288 ": You have an old buggy kernel which cannot support\n"
289 " --incremental reliably. Aborting.\n");
294 info
.array
.working_disks
= 1;
298 /* - check one drive in array to make sure metadata is a reasonably */
299 /* close match. Reject if not (e.g. different type) */
300 /* - add the device */
305 struct supertype
*st2
;
306 struct mdinfo info2
, *d
;
308 strcpy(chosen_name
, mp
->path
);
310 sra
= sysfs_read(mdfd
, devnum
, (GET_DEVS
| GET_STATE
));
312 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
313 sra
->devs
->disk
.minor
);
314 dfd2
= dev_open(dn
, O_RDONLY
);
316 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
317 st
->ss
->compare_super(st
, st2
) != 0) {
319 ": metadata mismatch between %s and "
321 devname
, chosen_name
);
327 memset(&info2
, 0, sizeof(info2
));
328 st2
->ss
->getinfo_super(st2
, &info2
);
329 st2
->ss
->free_super(st2
);
330 if (info
.array
.level
!= info2
.array
.level
||
331 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
332 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
334 ": unexpected difference between %s and %s.\n",
335 chosen_name
, devname
);
339 info2
.disk
.major
= major(stb
.st_rdev
);
340 info2
.disk
.minor
= minor(stb
.st_rdev
);
341 /* add disk needs to know about containers */
342 if (st
->ss
->external
)
343 sra
->array
.level
= LEVEL_CONTAINER
;
344 err
= add_disk(mdfd
, st2
, sra
, &info2
);
345 if (err
< 0 && errno
== EBUSY
) {
346 /* could be another device present with the same
347 * disk.number. Find and reject any such
349 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
350 info
.events
, verbose
, chosen_name
);
351 err
= add_disk(mdfd
, st2
, sra
, &info2
);
354 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
355 devname
, chosen_name
, strerror(errno
));
359 info
.array
.working_disks
= 0;
360 for (d
= sra
->devs
; d
; d
=d
->next
)
361 info
.array
.working_disks
++;
364 /* 6/ Make sure /var/run/mdadm.map contains this array. */
365 map_update(&map
, fd2devnum(mdfd
),
367 info
.uuid
, chosen_name
);
369 /* 7/ Is there enough devices to possibly start the array? */
370 /* 7a/ if not, finish with success. */
371 if (info
.array
.level
== LEVEL_CONTAINER
) {
372 /* Try to assemble within the container */
376 ": container %s now has %d devices\n",
377 chosen_name
, info
.array
.working_disks
);
378 return Incremental(chosen_name
, verbose
, runstop
,
379 NULL
, homehost
, autof
);
382 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
383 if (enough(info
.array
.level
, info
.array
.raid_disks
,
384 info
.array
.layout
, info
.array
.state
& 1,
385 avail
, active_disks
) == 0) {
389 ": %s attached to %s, not enough to start (%d).\n",
390 devname
, chosen_name
, active_disks
);
397 /* - if number of OK devices match expected, or -R and there */
399 /* + add any bitmap file */
400 /* + start the array (auto-readonly). */
402 mdu_array_info_t ainf
;
404 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
407 ": %s attached to %s which is already active.\n",
408 devname
, chosen_name
);
413 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
415 /* Let's try to start it */
416 if (match
&& match
->bitmap_file
) {
417 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
420 ": Could not open bitmap file %s.\n",
425 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
428 ": Failed to set bitmapfile for %s.\n",
435 sra
= sysfs_read(mdfd
, devnum
, 0);
436 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
437 && trustworthy
!= FOREIGN
)
438 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
440 rv
= sysfs_set_str(sra
, NULL
,
441 "array_state", "read-auto");
445 ": %s attached to %s, which has been started.\n",
446 devname
, chosen_name
);
450 ": %s attached to %s, but failed to start: %s.\n",
451 devname
, chosen_name
, strerror(errno
));
457 ": %s attached to %s, not enough to start safely.\n",
458 devname
, chosen_name
);
465 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
466 int number
, __u64 events
, int verbose
,
469 /* Find a device attached to this array with a disk.number of number
470 * and events less than the passed events, and remove the device.
475 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
476 return; /* not safe to remove from active arrays
477 * without thinking more */
479 for (d
= sra
->devs
; d
; d
= d
->next
) {
483 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
484 dfd
= dev_open(dn
, O_RDONLY
);
487 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
491 st
->ss
->getinfo_super(st
, &info
);
492 st
->ss
->free_super(st
);
495 if (info
.disk
.number
!= number
||
496 info
.events
>= events
)
499 if (d
->disk
.raid_disk
> -1)
500 sysfs_set_str(sra
, d
, "slot", "none");
501 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
504 ": removing old device %s from %s\n",
505 d
->sys_name
+4, array_name
);
509 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
510 struct mdinfo
*bestinfo
)
512 /* count how many devices in sra think they are active */
514 int cnt
= 0, cnt1
= 0;
515 __u64 max_events
= 0;
516 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
519 for (d
= sra
->devs
; d
; d
= d
->next
) {
525 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
526 dfd
= dev_open(dn
, O_RDONLY
);
529 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
533 st
->ss
->getinfo_super(st
, &info
);
535 avail
= malloc(info
.array
.raid_disks
);
537 fprintf(stderr
, Name
": out of memory.\n");
540 memset(avail
, 0, info
.array
.raid_disks
);
544 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
548 max_events
= info
.events
;
549 avail
[info
.disk
.raid_disk
] = 2;
550 st
->ss
->getinfo_super(st
, bestinfo
);
551 } else if (info
.events
== max_events
) {
553 avail
[info
.disk
.raid_disk
] = 2;
554 } else if (info
.events
== max_events
-1) {
556 avail
[info
.disk
.raid_disk
] = 1;
557 } else if (info
.events
< max_events
- 1)
559 else if (info
.events
== max_events
+1) {
563 max_events
= info
.events
;
564 for (i
=0; i
<info
.array
.raid_disks
; i
++)
567 avail
[info
.disk
.raid_disk
] = 2;
568 st
->ss
->getinfo_super(st
, bestinfo
);
569 } else { /* info.events much bigger */
571 memset(avail
, 0, info
.disk
.raid_disk
);
572 max_events
= info
.events
;
573 st
->ss
->getinfo_super(st
, bestinfo
);
576 st
->ss
->free_super(st
);
581 void RebuildMap(void)
583 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
584 struct mdstat_ent
*md
;
585 struct map_ent
*map
= NULL
;
586 int mdp
= get_mdp_major();
588 for (md
= mdstat
; md
; md
= md
->next
) {
589 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
592 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
596 struct supertype
*st
;
600 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
601 dfd
= dev_open(dn
, O_RDONLY
);
604 st
= guess_super(dfd
);
608 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
612 st
->ss
->getinfo_super(st
, &info
);
614 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
616 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
617 map_add(&map
, md
->devnum
,
619 info
.uuid
, path
? : "/unknown");
620 st
->ss
->free_super(st
);
628 int IncrementalScan(int verbose
)
630 /* look at every device listed in the 'map' file.
631 * If one is found that is not running then:
632 * look in mdadm.conf for bitmap file.
633 * if one exists, but array has none, add it.
634 * try to start array in auto-readonly mode
636 struct map_ent
*mapl
= NULL
;
638 mddev_ident_t devs
, mddev
;
642 devs
= conf_get_ident(NULL
);
644 for (me
= mapl
; me
; me
= me
->next
) {
645 mdu_array_info_t array
;
646 mdu_bitmap_file_t bmf
;
648 int mdfd
= open_mddev(me
->path
, 0);
652 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
657 /* Ok, we can try this one. Maybe it needs a bitmap */
658 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
659 if (strcmp(mddev
->devname
, me
->path
) == 0)
661 if (mddev
&& mddev
->bitmap_file
) {
663 * Note: early kernels will wrongly fail this, so it
667 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
668 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
670 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
678 ": Added bitmap %s to %s\n",
679 mddev
->bitmap_file
, me
->path
);
680 else if (errno
!= EEXIST
)
682 ": Failed to add bitmap to %s: %s\n",
683 me
->path
, strerror(errno
));
686 sra
= sysfs_read(mdfd
, 0, 0);
688 if (sysfs_set_str(sra
, NULL
,
689 "array_state", "read-auto") == 0) {
692 ": started array %s\n",
696 ": failed to start array %s: %s\n",
697 me
->path
, strerror(errno
));
705 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
706 int runstop
, int autof
, int trustworthy
)
708 /* Collect the contents of this container and for each
709 * array, choose a device name and assemble the array.
712 struct mdinfo
*list
= st
->ss
->container_content(st
);
715 for (ra
= list
; ra
; ra
= ra
->next
) {
716 struct mdinfo
*dev
, *sra
;
718 char chosen_name
[1024];
719 int working
= 0, preexist
= 0;
720 struct map_ent
*mp
, *map
= NULL
;
721 struct mddev_ident_s
*match
= NULL
;
723 mp
= map_by_uuid(&map
, ra
->uuid
);
727 /* Check in mdadm.conf for devices == devname and
728 * member == ra->text_version after second slash.
730 char *sub
= strchr(ra
->text_version
+1, '/');
731 struct mddev_ident_s
*array_list
;
734 array_list
= conf_get_ident(NULL
);
737 for(; array_list
; array_list
= array_list
->next
) {
740 if (array_list
->member
== NULL
||
741 array_list
->container
== NULL
)
743 if (strcmp(array_list
->member
, sub
) != 0)
745 if (array_list
->uuid_set
&&
746 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
748 fd
= open(array_list
->container
, O_RDONLY
);
751 dn
= devnum2devname(fd2devnum(fd
));
753 if (strncmp(dn
, ra
->text_version
+1,
755 ra
->text_version
[strlen(dn
)+1] != '/') {
760 /* we have a match */
763 fprintf(stderr
, Name
": match found for member %s\n",
769 mdfd
= create_mddev(match
? match
->devname
: NULL
,
776 fprintf(stderr
, Name
": failed to open %s: %s.\n",
777 chosen_name
, strerror(errno
));
782 sysfs_init(ra
, mdfd
, 0);
784 sra
= sysfs_read(mdfd
, 0, GET_VERSION
);
785 if (sra
== NULL
|| strcmp(sra
->text_version
, ra
->text_version
) != 0)
786 if (sysfs_set_array(ra
, md_get_version(mdfd
)) != 0)
791 for (dev
= ra
->devs
; dev
; dev
= dev
->next
)
792 if (sysfs_add_disk(ra
, dev
) == 0)
794 else if (errno
== EEXIST
)
797 /* Nothing new, don't try to start */ ;
798 else if (runstop
> 0 ||
799 (working
+ preexist
) >= ra
->array
.working_disks
) {
800 switch(ra
->array
.level
) {
802 case LEVEL_MULTIPATH
:
804 sysfs_set_str(ra
, NULL
, "array_state",
808 sysfs_set_str(ra
, NULL
, "array_state",
810 /* start mdmon if needed. */
811 if (!mdmon_running(st
->container_dev
))
812 start_mdmon(st
->container_dev
);
813 ping_monitor(devnum2devname(st
->container_dev
));
816 sysfs_set_safemode(ra
, ra
->safe_mode_delay
);
819 ": Started %s with %d devices",
820 chosen_name
, working
+ preexist
);
822 fprintf(stderr
, " (%d new)", working
);
823 fprintf(stderr
, "\n");
825 /* FIXME should have an O_EXCL and wait for read-auto */
829 ": %s assembled with %d devices but "
831 chosen_name
, working
);
832 map_update(&map
, fd2devnum(mdfd
),
834 ra
->uuid
, chosen_name
);