]>
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
;
305 memset(&ainf
, 0, sizeof(ainf
));
306 ainf
.major_version
= info
.array
.major_version
;
307 ainf
.minor_version
= info
.array
.minor_version
;
308 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
310 ": SET_ARRAY_INFO failed for %s: %s\b",
311 chosen_name
, strerror(errno
));
315 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
316 sysfs_set_str(sra
, NULL
, "metadata_version", info
.text_version
);
317 memset(&disk
, 0, sizeof(disk
));
318 disk
.major
= major(stb
.st_rdev
);
319 disk
.minor
= minor(stb
.st_rdev
);
321 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
322 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
323 devname
, chosen_name
, strerror(errno
));
324 ioctl(mdfd
, STOP_ARRAY
, 0);
328 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
329 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
330 /* It really should be 'none' - must be old buggy
331 * kernel, and mdadm -I may not be able to complete.
334 ioctl(mdfd
, STOP_ARRAY
, NULL
);
336 ": You have an old buggy kernel which cannot support\n"
337 " --incremental reliably. Aborting.\n");
344 /* - check one drive in array to make sure metadata is a reasonably */
345 /* close match. Reject if not (e.g. different type) */
346 /* - add the device */
349 mdu_disk_info_t disk
;
352 struct supertype
*st2
;
353 sra
= sysfs_read(mdfd
, devnum
, (GET_DEVS
| GET_STATE
));
355 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
356 sra
->devs
->disk
.minor
);
357 dfd2
= dev_open(dn
, O_RDONLY
);
359 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
360 st
->ss
->compare_super(st
, st2
) != 0) {
362 ": metadata mismatch between %s and "
364 devname
, chosen_name
);
370 st2
->ss
->getinfo_super(st2
, &info2
);
371 st2
->ss
->free_super(st2
);
372 if (info
.array
.level
!= info2
.array
.level
||
373 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
374 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
376 ": unexpected difference between %s and %s.\n",
377 chosen_name
, devname
);
381 memset(&disk
, 0, sizeof(disk
));
382 disk
.major
= major(stb
.st_rdev
);
383 disk
.minor
= minor(stb
.st_rdev
);
384 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
385 if (err
< 0 && errno
== EBUSY
) {
386 /* could be another device present with the same
387 * disk.number. Find and reject any such
389 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
390 info
.events
, verbose
, chosen_name
);
391 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
394 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
395 devname
, chosen_name
, strerror(errno
));
400 /* 6/ Make sure /var/run/mdadm.map contains this array. */
401 map_update(&map
, devnum
,
403 info
.uuid
, chosen_name
);
405 /* 7/ Is there enough devices to possibly start the array? */
406 /* 7a/ if not, finish with success. */
408 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
409 if (enough(info
.array
.level
, info
.array
.raid_disks
,
410 info
.array
.layout
, info
.array
.state
& 1,
411 avail
, active_disks
) == 0) {
415 ": %s attached to %s, not enough to start (%d).\n",
416 devname
, chosen_name
, active_disks
);
423 /* - if number of OK devices match expected, or -R and there */
425 /* + add any bitmap file */
426 /* + start the array (auto-readonly). */
428 mdu_array_info_t ainf
;
430 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
433 ": %s attached to %s which is already active.\n",
434 devname
, chosen_name
);
439 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
441 /* Let's try to start it */
442 if (match
&& match
->bitmap_file
) {
443 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
446 ": Could not open bitmap file %s.\n",
451 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
454 ": Failed to set bitmapfile for %s.\n",
461 sra
= sysfs_read(mdfd
, devnum
, 0);
462 if (sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
463 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
465 rv
= sysfs_set_str(sra
, NULL
,
466 "array_state", "read-auto");
470 ": %s attached to %s, which has been started.\n",
471 devname
, chosen_name
);
475 ": %s attached to %s, but failed to start: %s.\n",
476 devname
, chosen_name
, strerror(errno
));
482 ": %s attached to %s, not enough to start safely.\n",
483 devname
, chosen_name
);
490 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
491 int number
, __u64 events
, int verbose
,
494 /* Find a device attached to this array with a disk.number of number
495 * and events less than the passed events, and remove the device.
500 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
501 return; /* not safe to remove from active arrays
502 * without thinking more */
504 for (d
= sra
->devs
; d
; d
= d
->next
) {
508 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
509 dfd
= dev_open(dn
, O_RDONLY
);
512 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
516 st
->ss
->getinfo_super(st
, &info
);
517 st
->ss
->free_super(st
);
520 if (info
.disk
.number
!= number
||
521 info
.events
>= events
)
524 if (d
->disk
.raid_disk
> -1)
525 sysfs_set_str(sra
, d
, "slot", "none");
526 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
529 ": removing old device %s from %s\n",
530 d
->sys_name
+4, array_name
);
534 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
535 struct mdinfo
*bestinfo
)
537 /* count how many devices in sra think they are active */
539 int cnt
= 0, cnt1
= 0;
540 __u64 max_events
= 0;
541 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
544 for (d
= sra
->devs
; d
; d
= d
->next
) {
550 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
551 dfd
= dev_open(dn
, O_RDONLY
);
554 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
558 st
->ss
->getinfo_super(st
, &info
);
559 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
562 avail
= malloc(info
.array
.raid_disks
);
563 memset(avail
, 0, info
.array
.raid_disks
);
567 max_events
= info
.events
;
568 avail
[info
.disk
.raid_disk
] = 2;
569 st
->ss
->getinfo_super(st
, bestinfo
);
570 } else if (info
.events
== max_events
) {
572 avail
[info
.disk
.raid_disk
] = 2;
573 } else if (info
.events
== max_events
-1) {
575 avail
[info
.disk
.raid_disk
] = 1;
576 } else if (info
.events
< max_events
- 1)
578 else if (info
.events
== max_events
+1) {
582 max_events
= info
.events
;
583 for (i
=0; i
<info
.array
.raid_disks
; i
++)
586 avail
[info
.disk
.raid_disk
] = 2;
587 st
->ss
->getinfo_super(st
, bestinfo
);
588 } else { /* info.events much bigger */
590 memset(avail
, 0, info
.disk
.raid_disk
);
591 max_events
= info
.events
;
592 st
->ss
->getinfo_super(st
, bestinfo
);
595 st
->ss
->free_super(st
);
600 void RebuildMap(void)
602 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
603 struct mdstat_ent
*md
;
604 struct map_ent
*map
= NULL
;
605 int mdp
= get_mdp_major();
607 for (md
= mdstat
; md
; md
= md
->next
) {
608 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
611 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
615 struct supertype
*st
;
619 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
620 dfd
= dev_open(dn
, O_RDONLY
);
623 st
= guess_super(dfd
);
627 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
631 st
->ss
->getinfo_super(st
, &info
);
633 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
635 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
636 map_add(&map
, md
->devnum
,
638 info
.uuid
, path
? : "/unknown");
639 st
->ss
->free_super(st
);
647 int IncrementalScan(int verbose
)
649 /* look at every device listed in the 'map' file.
650 * If one is found that is not running then:
651 * look in mdadm.conf for bitmap file.
652 * if one exists, but array has none, add it.
653 * try to start array in auto-readonly mode
655 struct map_ent
*mapl
= NULL
;
657 mddev_ident_t devs
, mddev
;
661 devs
= conf_get_ident(NULL
);
663 for (me
= mapl
; me
; me
= me
->next
) {
665 mdu_array_info_t array
;
666 mdu_bitmap_file_t bmf
;
668 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
,
672 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
677 /* Ok, we can try this one. Maybe it needs a bitmap */
678 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
679 if (strcmp(mddev
->devname
, me
->path
) == 0)
681 if (mddev
&& mddev
->bitmap_file
) {
683 * Note: early kernels will wrongly fail this, so it
687 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
688 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
690 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
698 ": Added bitmap %s to %s\n",
699 mddev
->bitmap_file
, me
->path
);
700 else if (errno
!= EEXIST
)
702 ": Failed to add bitmap to %s: %s\n",
703 me
->path
, strerror(errno
));
706 sra
= sysfs_read(mdfd
, 0, 0);
708 if (sysfs_set_str(sra
, NULL
,
709 "array_state", "read-auto") == 0) {
712 ": started array %s\n",
716 ": failed to start array %s: %s\n",
717 me
->path
, strerror(errno
));
725 static char *container2devname(char *devname
)
727 int fd
= open(devname
, O_RDONLY
);
731 mdname
= devnum2devname(fd2devnum(fd
));
738 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
739 int runstop
, int autof
)
741 /* Collect the contents of this container and for each
742 * array, choose a device name and assemble the array.
745 struct mdinfo
*list
= st
->ss
->container_content(st
);
747 char *mdname
= container2devname(devname
);
750 fprintf(stderr
, Name
": failed to determine device name\n");
754 for (ra
= list
; ra
; ra
= ra
->next
) {
759 char chosen_name
[1024];
765 if ((autof
&7) == 3 || (autof
&7) == 5)
772 devnum
= strtoul(n
, &n
, 10);
773 if (devnum
>= 0 && (*n
== 0 || *n
== ' ')) {
774 /* Use this devnum */
775 usepart
= (ra
->name
[0] == 'd');
776 if (mddev_busy(usepart
? (-1-devnum
) : devnum
))
787 nm
= strchr(nm
, ':')+1;
788 sprintf(nbuf
, "/dev/md/%s", nm
);
790 if (stat(nbuf
, &stb
) == 0 &&
791 S_ISBLK(stb
.st_mode
) &&
792 major(stb
.st_rdev
) == (usepart
?
793 get_mdp_major() : MD_MAJOR
)){
795 devnum
= minor(stb
.st_rdev
)
798 devnum
= minor(stb
.st_rdev
);
799 if (mddev_busy(usepart
? (-1-devnum
) : devnum
))
805 devnum
= usepart
? (-1-devnum
) : devnum
;
807 devnum
= find_free_devnum(usepart
);
808 mdfd
= open_mddev_devnum(NULL
, devnum
, ra
->name
,
809 chosen_name
, autof
>>3);
812 fprintf(stderr
, Name
": failed to open %s: %s.\n",
813 chosen_name
, strerror(errno
));
817 sra
= sysfs_read(mdfd
, 0, 0);
819 sprintf(ver
, "external:%s", ra
->text_version
);
820 sysfs_set_str(sra
, NULL
, "metadata_version", ver
);
822 sysfs_set_array(sra
, ra
);
823 for (dev
= ra
->devs
; dev
; dev
= dev
->next
)
824 if (sysfs_add_disk(sra
, dev
) == 0)
827 if (runstop
> 0 || working
>= ra
->array
.working_disks
) {
828 switch(ra
->array
.level
) {
830 case LEVEL_MULTIPATH
:
832 sysfs_set_str(sra
, NULL
, "array_state",
836 sysfs_set_str(sra
, NULL
, "array_state",
838 /* start mdmon if needed. */
839 if (!mdmon_running(st
->container_dev
))
840 start_mdmon(st
->container_dev
);
841 ping_monitor(devnum2devname(st
->container_dev
));
844 sysfs_set_safemode(sra
, ra
->safe_mode_delay
);
846 printf("Started %s with %d devices\n",
847 chosen_name
, working
);
848 /* FIXME should have an O_EXCL and wait for read-auto */
851 printf("%s assembled with %d devices but "
853 chosen_name
, working
);