]>
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, reject.
52 * 4/ Determine device number.
53 * - If in mdadm.conf with std name, use that
54 * - UUID in /var/run/mdadm.map use that
55 * - If name is suggestive, use that. unless in use with different uuid.
56 * - Choose a free, high number.
57 * - Use a partitioned device unless strong suggestion not to.
59 * 5/ Find out if array already exists
61 * - choose a name, from mdadm.conf or 'name' field in array.
65 * - check one drive in array to make sure metadata is a reasonably
66 * close match. Reject if not (e.g. different type)
68 * 6/ Make sure /var/run/mdadm.map contains this array.
69 * 7/ Is there enough devices to possibly start the array?
70 * 7a/ if not, finish with success.
72 * - read all metadata and arrange devices like -A does
73 * - if number of OK devices match expected, or -R and there are enough,
74 * start the array (auto-readonly).
77 struct mdinfo info
, info2
;
78 struct mddev_ident_s
*array_list
, *match
;
79 char chosen_name
[1024];
82 struct map_ent
*mp
, *map
= NULL
;
88 struct createinfo
*ci
= conf_get_create_info();
93 /* 1/ Check if devices is permitted by mdadm.conf */
95 if (!conf_test_dev(devname
)) {
98 ": %s not permitted by mdadm.conf.\n",
103 /* 2/ Find metadata, reject if none appropriate (check
104 * version/name from args) */
106 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
109 fprintf(stderr
, Name
": cannot open %s: %s.\n",
110 devname
, strerror(errno
));
113 if (fstat(dfd
, &stb
) < 0) {
115 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
116 devname
, strerror(errno
));
120 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
122 fprintf(stderr
, Name
": %s is not a block device.\n",
128 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
131 ": no recognisable superblock on %s.\n",
136 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
138 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
145 if (st
->ss
->container_content
) {
146 /* This is a pre-built container array, so we do something
149 return Incremental_container(st
, devname
, verbose
, runstop
,
153 st
->ss
->getinfo_super(st
, &info
);
154 /* 3/ Check if there is a match in mdadm.conf */
156 array_list
= conf_get_ident(NULL
);
158 for (; array_list
; array_list
= array_list
->next
) {
159 if (array_list
->uuid_set
&&
160 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
164 ": UUID differs from %s.\n",
165 array_list
->devname
);
168 if (array_list
->name
[0] &&
169 strcasecmp(array_list
->name
, info
.name
) != 0) {
172 ": Name differs from %s.\n",
173 array_list
->devname
);
176 if (array_list
->devices
&&
177 !match_oneof(array_list
->devices
, devname
)) {
180 ": Not a listed device for %s.\n",
181 array_list
->devname
);
184 if (array_list
->super_minor
!= UnSet
&&
185 array_list
->super_minor
!= info
.array
.md_minor
) {
188 ": Different super-minor to %s.\n",
189 array_list
->devname
);
192 if (!array_list
->uuid_set
&&
193 !array_list
->name
[0] &&
194 !array_list
->devices
&&
195 array_list
->super_minor
== UnSet
) {
198 ": %s doesn't have any identifying information.\n",
199 array_list
->devname
);
202 /* FIXME, should I check raid_disks and level too?? */
207 ": we match both %s and %s - cannot decide which to use.\n",
208 match
->devname
, array_list
->devname
);
214 /* 3a/ if not, check for homehost match. If no match, reject. */
216 if (homehost
== NULL
||
217 st
->ss
->match_home(st
, homehost
) == 0) {
220 ": not found in mdadm.conf and not identified by homehost.\n");
224 /* 4/ Determine device number. */
225 /* - If in mdadm.conf with std name, use that */
226 /* - UUID in /var/run/mdadm.map use that */
227 /* - If name is suggestive, use that. unless in use with */
228 /* different uuid. */
229 /* - Choose a free, high number. */
230 /* - Use a partitioned device unless strong suggestion not to. */
232 if (match
&& is_standard(match
->devname
, &devnum
))
233 /* We have devnum now */;
234 else if ((mp
= map_by_uuid(&map
, info
.uuid
)) != NULL
)
237 /* Have to guess a bit. */
238 int use_partitions
= 1;
240 char *nm
, nbuf
[1024];
241 if ((autof
&7) == 3 || (autof
&7) == 5)
243 np
= strchr(info
.name
, ':');
248 devnum
= strtoul(np
, &ep
, 10);
249 if (ep
> np
&& *ep
== 0) {
250 /* This is a number. Let check that it is unused. */
251 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
259 sprintf(nbuf
, "/dev/md/%s", np
);
262 if (stat(nm
, &stb
) == 0 &&
263 S_ISBLK(stb
.st_mode
) &&
264 major(stb
.st_rdev
) == (use_partitions
?
265 get_mdp_major() : MD_MAJOR
)) {
267 devnum
= minor(stb
.st_rdev
) >> MdpMinorShift
;
269 devnum
= minor(stb
.st_rdev
);
270 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
275 /* Haven't found anything yet, choose something free */
276 devnum
= find_free_devnum(use_partitions
);
278 if (devnum
== NoMdDev
) {
280 ": No spare md devices!!\n");
284 devnum
= use_partitions
? (-1-devnum
) : devnum
;
286 mdfd
= open_mddev_devnum(match
? match
->devname
: NULL
,
289 chosen_name
, autof
>> 3);
291 fprintf(stderr
, Name
": failed to open %s: %s.\n",
292 chosen_name
, strerror(errno
));
295 /* 5/ Find out if array already exists */
296 if (! mddev_busy(devnum
)) {
297 /* 5a/ if it does not */
298 /* - choose a name, from mdadm.conf or 'name' field in array. */
299 /* - create the array */
300 /* - add the device */
301 mdu_array_info_t ainf
;
302 mdu_disk_info_t disk
;
306 memset(&ainf
, 0, sizeof(ainf
));
307 ainf
.major_version
= st
->ss
->major
;
308 ainf
.minor_version
= st
->minor_version
;
309 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
311 ": SET_ARRAY_INFO failed for %s: %s\b",
312 chosen_name
, strerror(errno
));
316 sprintf(md
, "%d.%d\n", st
->ss
->major
, st
->minor_version
);
317 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
318 sysfs_set_str(sra
, NULL
, "metadata_version", md
);
319 memset(&disk
, 0, sizeof(disk
));
320 disk
.major
= major(stb
.st_rdev
);
321 disk
.minor
= minor(stb
.st_rdev
);
323 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
324 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
325 devname
, chosen_name
, strerror(errno
));
326 ioctl(mdfd
, STOP_ARRAY
, 0);
330 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
331 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
332 /* It really should be 'none' - must be old buggy
333 * kernel, and mdadm -I may not be able to complete.
336 ioctl(mdfd
, STOP_ARRAY
, NULL
);
338 ": You have an old buggy kernel which cannot support\n"
339 " --incremental reliably. Aborting.\n");
346 /* - check one drive in array to make sure metadata is a reasonably */
347 /* close match. Reject if not (e.g. different type) */
348 /* - add the device */
351 mdu_disk_info_t disk
;
354 struct supertype
*st2
;
355 sra
= sysfs_read(mdfd
, devnum
, (GET_VERSION
| GET_DEVS
|
358 if (sra
->array
.major_version
!= st
->ss
->major
||
359 sra
->array
.minor_version
!= st
->minor_version
) {
362 ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
363 devname
, chosen_name
,
364 sra
->array
.major_version
,
365 sra
->array
.minor_version
,
366 st
->ss
->major
, st
->minor_version
);
370 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
371 sra
->devs
->disk
.minor
);
372 dfd2
= dev_open(dn
, O_RDONLY
);
374 if (st2
->ss
->load_super(st2
, dfd2
, NULL
)) {
376 ": Strange error loading metadata for %s.\n",
383 st2
->ss
->getinfo_super(st2
, &info2
);
384 st2
->ss
->free_super(st2
);
385 if (info
.array
.level
!= info2
.array
.level
||
386 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
387 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
389 ": unexpected difference between %s and %s.\n",
390 chosen_name
, devname
);
394 memset(&disk
, 0, sizeof(disk
));
395 disk
.major
= major(stb
.st_rdev
);
396 disk
.minor
= minor(stb
.st_rdev
);
397 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
398 if (err
< 0 && errno
== EBUSY
) {
399 /* could be another device present with the same
400 * disk.number. Find and reject any such
402 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
403 info
.events
, verbose
, chosen_name
);
404 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
407 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
408 devname
, chosen_name
, strerror(errno
));
413 /* 6/ Make sure /var/run/mdadm.map contains this array. */
414 map_update(&map
, devnum
,
415 info
.array
.major_version
,
416 info
.array
.minor_version
,
417 info
.uuid
, chosen_name
);
419 /* 7/ Is there enough devices to possibly start the array? */
420 /* 7a/ if not, finish with success. */
422 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
423 if (enough(info
.array
.level
, info
.array
.raid_disks
,
424 info
.array
.layout
, info
.array
.state
& 1,
425 avail
, active_disks
) == 0) {
429 ": %s attached to %s, not enough to start (%d).\n",
430 devname
, chosen_name
, active_disks
);
437 /* - if number of OK devices match expected, or -R and there */
439 /* + add any bitmap file */
440 /* + start the array (auto-readonly). */
442 mdu_array_info_t ainf
;
444 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
447 ": %s attached to %s which is already active.\n",
448 devname
, chosen_name
);
453 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
455 /* Let's try to start it */
456 if (match
&& match
->bitmap_file
) {
457 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
460 ": Could not open bitmap file %s.\n",
465 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
468 ": Failed to set bitmapfile for %s.\n",
475 sra
= sysfs_read(mdfd
, devnum
, 0);
476 if (sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
477 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
479 rv
= sysfs_set_str(sra
, NULL
,
480 "array_state", "read-auto");
484 ": %s attached to %s, which has been started.\n",
485 devname
, chosen_name
);
489 ": %s attached to %s, but failed to start: %s.\n",
490 devname
, chosen_name
, strerror(errno
));
496 ": %s attached to %s, not enough to start safely.\n",
497 devname
, chosen_name
);
504 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
505 int number
, __u64 events
, int verbose
,
508 /* Find a device attached to this array with a disk.number of number
509 * and events less than the passed events, and remove the device.
514 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
515 return; /* not safe to remove from active arrays
516 * without thinking more */
518 for (d
= sra
->devs
; d
; d
= d
->next
) {
522 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
523 dfd
= dev_open(dn
, O_RDONLY
);
526 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
530 st
->ss
->getinfo_super(st
, &info
);
531 st
->ss
->free_super(st
);
534 if (info
.disk
.number
!= number
||
535 info
.events
>= events
)
538 if (d
->disk
.raid_disk
> -1)
539 sysfs_set_str(sra
, d
, "slot", "none");
540 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
543 ": removing old device %s from %s\n",
544 d
->sys_name
+4, array_name
);
548 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
549 struct mdinfo
*bestinfo
)
551 /* count how many devices in sra think they are active */
553 int cnt
= 0, cnt1
= 0;
554 __u64 max_events
= 0;
555 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
558 for (d
= sra
->devs
; d
; d
= d
->next
) {
564 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
565 dfd
= dev_open(dn
, O_RDONLY
);
568 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
572 st
->ss
->getinfo_super(st
, &info
);
573 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
576 avail
= malloc(info
.array
.raid_disks
);
577 memset(avail
, 0, info
.array
.raid_disks
);
581 max_events
= info
.events
;
582 avail
[info
.disk
.raid_disk
] = 2;
583 st
->ss
->getinfo_super(st
, bestinfo
);
584 } else if (info
.events
== max_events
) {
586 avail
[info
.disk
.raid_disk
] = 2;
587 } else if (info
.events
== max_events
-1) {
589 avail
[info
.disk
.raid_disk
] = 1;
590 } else if (info
.events
< max_events
- 1)
592 else if (info
.events
== max_events
+1) {
596 max_events
= info
.events
;
597 for (i
=0; i
<info
.array
.raid_disks
; i
++)
600 avail
[info
.disk
.raid_disk
] = 2;
601 st
->ss
->getinfo_super(st
, bestinfo
);
602 } else { /* info.events much bigger */
604 memset(avail
, 0, info
.disk
.raid_disk
);
605 max_events
= info
.events
;
606 st
->ss
->getinfo_super(st
, bestinfo
);
609 st
->ss
->free_super(st
);
614 void RebuildMap(void)
616 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
617 struct mdstat_ent
*md
;
618 struct map_ent
*map
= NULL
;
619 int mdp
= get_mdp_major();
621 for (md
= mdstat
; md
; md
= md
->next
) {
622 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
625 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
629 struct supertype
*st
;
633 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
634 dfd
= dev_open(dn
, O_RDONLY
);
637 st
= guess_super(dfd
);
641 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
645 st
->ss
->getinfo_super(st
, &info
);
647 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
649 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
650 map_add(&map
, md
->devnum
, st
->ss
->major
,
652 info
.uuid
, path
? : "/unknown");
653 st
->ss
->free_super(st
);
661 int IncrementalScan(int verbose
)
663 /* look at every device listed in the 'map' file.
664 * If one is found that is not running then:
665 * look in mdadm.conf for bitmap file.
666 * if one exists, but array has none, add it.
667 * try to start array in auto-readonly mode
669 struct map_ent
*mapl
= NULL
;
671 mddev_ident_t devs
, mddev
;
675 devs
= conf_get_ident(NULL
);
677 for (me
= mapl
; me
; me
= me
->next
) {
679 mdu_array_info_t array
;
680 mdu_bitmap_file_t bmf
;
682 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
,
686 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
691 /* Ok, we can try this one. Maybe it needs a bitmap */
692 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
693 if (strcmp(mddev
->devname
, me
->path
) == 0)
695 if (mddev
&& mddev
->bitmap_file
) {
697 * Note: early kernels will wrongly fail this, so it
701 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
702 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
704 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
712 ": Added bitmap %s to %s\n",
713 mddev
->bitmap_file
, me
->path
);
714 else if (errno
!= EEXIST
)
716 ": Failed to add bitmap to %s: %s\n",
717 me
->path
, strerror(errno
));
720 sra
= sysfs_read(mdfd
, 0, 0);
722 if (sysfs_set_str(sra
, NULL
,
723 "array_state", "read-auto") == 0) {
726 ": started array %s\n",
730 ": failed to start array %s: %s\n",
731 me
->path
, strerror(errno
));
739 static char *container2devname(char *devname
)
741 int fd
= open(devname
, O_RDONLY
);
745 mdname
= devnum2devname(fd2devnum(fd
));
752 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
753 int runstop
, int autof
)
755 /* Collect the contents of this container and for each
756 * array, choose a device name and assemble the array.
759 struct mdinfo
*list
= st
->ss
->container_content(st
);
761 char *mdname
= container2devname(devname
);
764 fprintf(stderr
, Name
": failed to determine device name\n");
768 for (ra
= list
; ra
; ra
= ra
->next
) {
773 char chosen_name
[1024];
779 if ((autof
&7) == 3 || (autof
&7) == 5)
786 devnum
= strtoul(n
, &n
, 10);
787 if (devnum
>= 0 && (*n
== 0 || *n
== ' ')) {
788 /* Use this devnum */
789 usepart
= (ra
->name
[0] == 'd');
790 if (mddev_busy(usepart
? (-1-devnum
) : devnum
))
801 nm
= strchr(nm
, ':')+1;
802 sprintf(nbuf
, "/dev/md/%s", nm
);
804 if (stat(nbuf
, &stb
) == 0 &&
805 S_ISBLK(stb
.st_mode
) &&
806 major(stb
.st_rdev
) == (usepart
?
807 get_mdp_major() : MD_MAJOR
)){
809 devnum
= minor(stb
.st_rdev
)
812 devnum
= minor(stb
.st_rdev
);
813 if (mddev_busy(usepart
? (-1-devnum
) : devnum
))
819 devnum
= usepart
? (-1-devnum
) : devnum
;
821 devnum
= find_free_devnum(usepart
);
822 mdfd
= open_mddev_devnum(NULL
, devnum
, ra
->name
,
823 chosen_name
, autof
>>3);
826 fprintf(stderr
, Name
": failed to open %s: %s.\n",
827 chosen_name
, strerror(errno
));
831 sra
= sysfs_read(mdfd
, 0, 0);
833 sprintf(ver
, "external:%s", ra
->text_version
);
834 sysfs_set_str(sra
, NULL
, "metadata_version", ver
);
836 sysfs_set_array(sra
, ra
);
837 for (dev
= ra
->devs
; dev
; dev
= dev
->next
)
838 if (sysfs_add_disk(sra
, dev
) == 0)
841 if (runstop
> 0 || working
>= ra
->array
.working_disks
) {
842 switch(ra
->array
.level
) {
844 case LEVEL_MULTIPATH
:
846 sysfs_set_str(sra
, NULL
, "array_state",
850 sysfs_set_str(sra
, NULL
, "array_state",
852 /* start mdmon if needed. */
853 if (mdmon_running(st
->container_dev
))
854 signal_mdmon(st
->container_dev
);
856 int dn
= st
->container_dev
;
860 /* FIXME yuk. CLOSE_EXEC?? */
861 for (i
=3; i
< 100; i
++)
863 execl("./mdmon", "mdmon",
864 map_dev(dev2major(dn
),
868 case -1: fprintf(stderr
, Name
870 "Array remains readonly\n");
872 default: ; /* parent - good */
878 printf("Started %s with %d devices\n",
879 chosen_name
, working
);
880 /* FIXME should have an O_EXCL and wait for read-auto */
883 printf("%s assembled with %d devices but "
885 chosen_name
, working
);