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 sysarray
*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 sensibe 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).
78 struct mdinfo info
, info2
;
79 struct mddev_ident_s
*array_list
, *match
;
80 char chosen_name
[1024];
83 struct map_ent
*mp
, *map
= NULL
;
89 struct createinfo
*ci
= conf_get_create_info();
94 /* 1/ Check if devices is permitted by mdadm.conf */
96 if (!conf_test_dev(devname
)) {
99 ": %s not permitted by mdadm.conf.\n",
104 /* 2/ Find metadata, reject if none appropriate (check
105 * version/name from args) */
107 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
110 fprintf(stderr
, Name
": cannot open %s: %s.\n",
111 devname
, strerror(errno
));
114 if (fstat(dfd
, &stb
) < 0) {
116 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
117 devname
, strerror(errno
));
121 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
123 fprintf(stderr
, Name
": %s is not a block device.\n",
129 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
132 ": no recognisable superblock on %s.\n",
137 if (st
->ss
->load_super(st
, dfd
, &super
, NULL
)) {
139 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
144 st
->ss
->getinfo_super(&info
, super
);
147 /* 3/ Check if there is a match in mdadm.conf */
149 array_list
= conf_get_ident(NULL
);
151 for (; array_list
; array_list
= array_list
->next
) {
152 if (array_list
->uuid_set
&&
153 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
157 ": UUID differs from %s.\n",
158 array_list
->devname
);
161 if (array_list
->name
[0] &&
162 strcasecmp(array_list
->name
, info
.name
) != 0) {
165 ": Name differs from %s.\n",
166 array_list
->devname
);
169 if (array_list
->devices
&&
170 !match_oneof(array_list
->devices
, devname
)) {
173 ": Not a listed device for %s.\n",
174 array_list
->devname
);
177 if (array_list
->super_minor
!= UnSet
&&
178 array_list
->super_minor
!= info
.array
.md_minor
) {
181 ": Different super-minor to %s.\n",
182 array_list
->devname
);
185 if (!array_list
->uuid_set
&&
186 !array_list
->name
[0] &&
187 !array_list
->devices
&&
188 array_list
->super_minor
== UnSet
) {
191 ": %s doesn't have any identifying information.\n",
192 array_list
->devname
);
195 /* FIXME, should I check raid_disks and level too?? */
200 ": we match both %s and %s - cannot decide which to use.\n",
201 match
->devname
, array_list
->devname
);
207 /* 3a/ if not, check for homehost match. If no match, reject. */
209 if (homehost
== NULL
||
210 st
->ss
->match_home(super
, homehost
) == 0) {
213 ": not found in mdadm.conf and not identified by homehost.\n");
217 /* 4/ Determine device number. */
218 /* - If in mdadm.conf with std name, use that */
219 /* - UUID in /var/run/mdadm.map use that */
220 /* - If name is suggestive, use that. unless in use with */
221 /* different uuid. */
222 /* - Choose a free, high number. */
223 /* - Use a partitioned device unless strong suggestion not to. */
225 if (match
&& is_standard(match
->devname
, &devnum
))
226 /* We have devnum now */;
227 else if ((mp
= map_by_uuid(&map
, info
.uuid
)) != NULL
)
230 /* Have to guess a bit. */
231 int use_partitions
= 1;
233 if ((autof
&7) == 3 || (autof
&7) == 5)
235 np
= strchr(info
.name
, ':');
240 devnum
= strtoul(np
, &ep
, 10);
241 if (ep
> np
&& *ep
== 0) {
242 /* This is a number. Let check that it is unused. */
243 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
249 /* Haven't found anything yet, choose something free */
250 /* There is similar code in mdopen.c - should unify */
251 for (devnum
= 127 ; devnum
!= 128 ;
252 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
253 if (mddev_busy(use_partitions
?
254 (-1-devnum
) : devnum
))
259 ": No spare md devices!!\n");
263 devnum
= use_partitions
? (-1-devnum
) : devnum
;
265 mdfd
= open_mddev_devnum(match
? match
->devname
: NULL
,
270 fprintf(stderr
, Name
": failed to open %s: %s.\n",
271 chosen_name
, strerror(errno
));
274 /* 5/ Find out if array already exists */
275 if (! mddev_busy(devnum
)) {
276 /* 5a/ if it does not */
277 /* - choose a name, from mdadm.conf or 'name' field in array. */
278 /* - create the array */
279 /* - add the device */
280 mdu_array_info_t ainf
;
281 mdu_disk_info_t disk
;
283 struct sysarray
*sra
;
285 memset(&ainf
, 0, sizeof(ainf
));
286 ainf
.major_version
= st
->ss
->major
;
287 ainf
.minor_version
= st
->minor_version
;
288 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
290 ": SET_ARRAY_INFO failed for %s: %s\b",
291 chosen_name
, strerror(errno
));
295 sprintf(md
, "%d.%d\n", st
->ss
->major
, st
->minor_version
);
296 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
297 sysfs_set_str(sra
, NULL
, "metadata_version", md
);
298 memset(&disk
, 0, sizeof(disk
));
299 disk
.major
= major(stb
.st_rdev
);
300 disk
.minor
= minor(stb
.st_rdev
);
302 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
303 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
304 devname
, chosen_name
, strerror(errno
));
305 ioctl(mdfd
, STOP_ARRAY
, 0);
309 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
310 if (!sra
|| !sra
->devs
|| sra
->devs
->role
>= 0) {
311 /* It really should be 'none' - must be old buggy
312 * kernel, and mdadm -I may not be able to complete.
315 ioctl(mdfd
, STOP_ARRAY
, NULL
);
317 ": You have an old buggy kernel which cannot support\n"
318 " --incremental reliably. Aborting.\n");
325 /* - check one drive in array to make sure metadata is a reasonably */
326 /* close match. Reject if not (e.g. different type) */
327 /* - add the device */
330 mdu_disk_info_t disk
;
332 struct sysarray
*sra
;
333 sra
= sysfs_read(mdfd
, devnum
, (GET_VERSION
| GET_DEVS
|
335 if (sra
->major_version
!= st
->ss
->major
||
336 sra
->minor_version
!= st
->minor_version
) {
339 ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
340 devname
, chosen_name
,
341 sra
->major_version
, sra
->minor_version
,
342 st
->ss
->major
, st
->minor_version
);
346 sprintf(dn
, "%d:%d", sra
->devs
->major
, sra
->devs
->minor
);
347 dfd2
= dev_open(dn
, O_RDONLY
);
348 if (st
->ss
->load_super(st
, dfd2
,&super2
, NULL
)) {
350 ": Strange error loading metadata for %s.\n",
357 st
->ss
->getinfo_super(&info2
, super2
);
358 if (info
.array
.level
!= info2
.array
.level
||
359 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
360 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
362 ": unexpected difference between %s and %s.\n",
363 chosen_name
, devname
);
367 memset(&disk
, 0, sizeof(disk
));
368 disk
.major
= major(stb
.st_rdev
);
369 disk
.minor
= minor(stb
.st_rdev
);
370 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
371 if (err
< 0 && errno
== EBUSY
) {
372 /* could be another device present with the same
373 * disk.number. Find and reject any such
375 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
376 info
.events
, verbose
, chosen_name
);
377 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
380 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
381 devname
, chosen_name
, strerror(errno
));
386 /* 6/ Make sure /var/run/mdadm.map contains this array. */
387 map_update(&map
, devnum
,
388 info
.array
.major_version
,
389 info
.array
.minor_version
,
390 info
.uuid
, chosen_name
);
392 /* 7/ Is there enough devices to possibly start the array? */
393 /* 7a/ if not, finish with success. */
395 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
396 if (enough(info
.array
.level
, info
.array
.raid_disks
,
397 info
.array
.layout
, info
.array
.state
& 1,
398 avail
, active_disks
) == 0) {
402 ": %s attached to %s, not enough to start (%d).\n",
403 devname
, chosen_name
, active_disks
);
410 /* - if number of OK devices match expected, or -R and there */
412 /* + add any bitmap file */
413 /* + start the array (auto-readonly). */
415 mdu_array_info_t ainf
;
417 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
420 ": %s attached to %s which is already active.\n",
421 devname
, chosen_name
);
426 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
427 struct sysarray
*sra
;
428 /* Let's try to start it */
429 if (match
&& match
->bitmap_file
) {
430 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
433 ": Could not open bitmap file %s.\n",
438 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
441 ": Failed to set bitmapfile for %s.\n",
448 sra
= sysfs_read(mdfd
, devnum
, 0);
449 if (sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
450 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
452 rv
= sysfs_set_str(sra
, NULL
,
453 "array_state", "read-auto");
457 ": %s attached to %s, which has been started.\n",
458 devname
, chosen_name
);
462 ": %s attached to %s, but failed to start: %s.\n",
463 devname
, chosen_name
, strerror(errno
));
469 ": %s attached to %s, not enough to start safely.\n",
470 devname
, chosen_name
);
477 static void find_reject(int mdfd
, struct supertype
*st
, struct sysarray
*sra
,
478 int number
, __u64 events
, int verbose
,
481 /* Find an device attached to this array with a disk.number of number
482 * and events less than the passed events, and remove the device.
487 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
488 return; /* not safe to remove from active arrays
489 * without thinking more */
491 for (d
= sra
->devs
; d
; d
= d
->next
) {
496 sprintf(dn
, "%d:%d", d
->major
, d
->minor
);
497 dfd
= dev_open(dn
, O_RDONLY
);
500 if (st
->ss
->load_super(st
, dfd
, &super
, NULL
)) {
504 st
->ss
->getinfo_super(&info
, super
);
508 if (info
.disk
.number
!= number
||
509 info
.events
>= events
)
513 sysfs_set_str(sra
, d
, "slot", "none");
514 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
517 ": removing old device %s from %s\n",
518 d
->name
+4, array_name
);
522 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
523 struct mdinfo
*bestinfo
)
525 /* count how many devices in sra think they are active */
527 int cnt
= 0, cnt1
= 0;
528 __u64 max_events
= 0;
529 void *best_super
= NULL
;
530 struct sysarray
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
533 for (d
= sra
->devs
; d
; d
= d
->next
) {
540 sprintf(dn
, "%d:%d", d
->major
, d
->minor
);
541 dfd
= dev_open(dn
, O_RDONLY
);
544 ok
= st
->ss
->load_super(st
, dfd
, &super
, NULL
);
548 st
->ss
->getinfo_super(&info
, super
);
549 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
552 avail
= malloc(info
.array
.raid_disks
);
553 memset(avail
, 0, info
.array
.raid_disks
);
557 max_events
= info
.events
;
558 avail
[info
.disk
.raid_disk
] = 2;
559 best_super
= super
; super
= NULL
;
560 } else if (info
.events
== max_events
) {
562 avail
[info
.disk
.raid_disk
] = 2;
563 } else if (info
.events
== max_events
-1) {
565 avail
[info
.disk
.raid_disk
] = 1;
566 } else if (info
.events
< max_events
- 1)
568 else if (info
.events
== max_events
+1) {
572 max_events
= info
.events
;
573 for (i
=0; i
<info
.array
.raid_disks
; i
++)
576 avail
[info
.disk
.raid_disk
] = 2;
580 } else { /* info.events much bigger */
582 memset(avail
, 0, info
.disk
.raid_disk
);
583 max_events
= info
.events
;
593 st
->ss
->getinfo_super(bestinfo
,best_super
);
599 void RebuildMap(void)
601 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
602 struct mdstat_ent
*md
;
603 struct map_ent
*map
= NULL
;
604 int mdp
= get_mdp_major();
606 for (md
= mdstat
; md
; md
= md
->next
) {
607 struct sysarray
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
610 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
614 struct supertype
*st
;
619 sprintf(dn
, "%d:%d", sd
->major
, sd
->minor
);
620 dfd
= dev_open(dn
, O_RDONLY
);
623 st
= guess_super(dfd
);
627 ok
= st
->ss
->load_super(st
, dfd
, &super
, NULL
);
631 st
->ss
->getinfo_super(&info
, super
);
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
, st
->ss
->major
,
638 info
.uuid
, path
? : "/unknown");
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
;
667 struct sysarray
*sra
;
668 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
, NULL
, path
);
671 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
676 /* Ok, we can try this one. Maybe it needs a bitmap */
677 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
678 if (strcmp(mddev
->devname
, me
->path
) == 0)
680 if (mddev
&& mddev
->bitmap_file
) {
682 * Note: early kernels will wrongly fail this, so it
686 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
687 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
689 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
697 ": Added bitmap %s to %s\n",
698 mddev
->bitmap_file
, me
->path
);
699 else if (errno
!= EEXIST
)
701 ": Failed to add bitmap to %s: %s\n",
702 me
->path
, strerror(errno
));
705 sra
= sysfs_read(mdfd
, 0, 0);
707 if (sysfs_set_str(sra
, NULL
,
708 "array_state", "read-auto") == 0) {
711 ": started array %s\n",
715 ": failed to start array %s: %s\n",
716 me
->path
, strerror(errno
));