]>
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 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).
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
;
86 struct createinfo
*ci
= conf_get_create_info();
90 /* 1/ Check if devices is permitted by mdadm.conf */
92 if (!conf_test_dev(devname
)) {
95 ": %s not permitted by mdadm.conf.\n",
100 /* 2/ Find metadata, reject if none appropriate (check
101 * version/name from args) */
103 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
106 fprintf(stderr
, Name
": cannot open %s: %s.\n",
107 devname
, strerror(errno
));
110 if (fstat(dfd
, &stb
) < 0) {
112 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
113 devname
, strerror(errno
));
117 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
119 fprintf(stderr
, Name
": %s is not a block device.\n",
125 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
128 ": no recognisable superblock on %s.\n",
133 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
135 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
140 st
->ss
->getinfo_super(st
, &info
);
143 /* 3/ Check if there is a match in mdadm.conf */
145 array_list
= conf_get_ident(NULL
);
147 for (; array_list
; array_list
= array_list
->next
) {
148 if (array_list
->uuid_set
&&
149 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
153 ": UUID differs from %s.\n",
154 array_list
->devname
);
157 if (array_list
->name
[0] &&
158 strcasecmp(array_list
->name
, info
.name
) != 0) {
161 ": Name differs from %s.\n",
162 array_list
->devname
);
165 if (array_list
->devices
&&
166 !match_oneof(array_list
->devices
, devname
)) {
169 ": Not a listed device for %s.\n",
170 array_list
->devname
);
173 if (array_list
->super_minor
!= UnSet
&&
174 array_list
->super_minor
!= info
.array
.md_minor
) {
177 ": Different super-minor to %s.\n",
178 array_list
->devname
);
181 if (!array_list
->uuid_set
&&
182 !array_list
->name
[0] &&
183 !array_list
->devices
&&
184 array_list
->super_minor
== UnSet
) {
187 ": %s doesn't have any identifying information.\n",
188 array_list
->devname
);
191 /* FIXME, should I check raid_disks and level too?? */
196 ": we match both %s and %s - cannot decide which to use.\n",
197 match
->devname
, array_list
->devname
);
203 /* 3a/ if not, check for homehost match. If no match, continue
204 * but don't trust the 'name' in the array. Thus a 'random' minor
205 * number will be assigned, and the device name will be based
209 if (homehost
== NULL
||
210 st
->ss
->match_home(st
, 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, get number from name. */
219 /* - UUID in /var/run/mdadm.map get number from mapping */
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. */
226 /* There are three possible sources for 'autof': command line,
227 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
228 * They have precedence in that order.
230 if (autof
== 0 && match
)
231 autof
= match
->autof
;
235 if (match
&& (rv
= is_standard(match
->devname
, &devnum
))) {
236 devnum
= (rv
> 0) ? (-1-devnum
) : devnum
;
237 } else if ((mp
= map_by_uuid(&map
, info
.uuid
)) != NULL
)
240 /* Have to guess a bit. */
241 int use_partitions
= 1;
243 if ((autof
&7) == 3 || (autof
&7) == 5)
245 np
= name
? strchr(name
, ':') : ":NONAME";
250 devnum
= strtoul(np
, &ep
, 10);
251 if (ep
> np
&& *ep
== 0) {
252 /* This is a number. Let check that it is unused. */
253 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
259 /* Haven't found anything yet, choose something free */
260 devnum
= find_free_devnum(use_partitions
);
262 if (devnum
== NoMdDev
) {
264 ": No spare md devices!!\n");
268 devnum
= use_partitions
? (-1-devnum
) : devnum
;
270 mdfd
= open_mddev_devnum(match
? match
->devname
: NULL
,
273 chosen_name
, autof
>> 3);
275 fprintf(stderr
, Name
": failed to open %s: %s.\n",
276 chosen_name
, strerror(errno
));
279 /* 5/ Find out if array already exists */
280 if (! mddev_busy(devnum
)) {
281 /* 5a/ if it does not */
282 /* - choose a name, from mdadm.conf or 'name' field in array. */
283 /* - create the array */
284 /* - add the device */
285 mdu_array_info_t ainf
;
286 mdu_disk_info_t disk
;
290 memset(&ainf
, 0, sizeof(ainf
));
291 ainf
.major_version
= st
->ss
->major
;
292 ainf
.minor_version
= st
->minor_version
;
293 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
295 ": SET_ARRAY_INFO failed for %s: %s\b",
296 chosen_name
, strerror(errno
));
300 sprintf(md
, "%d.%d\n", st
->ss
->major
, st
->minor_version
);
301 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
302 sysfs_set_str(sra
, NULL
, "metadata_version", md
);
303 memset(&disk
, 0, sizeof(disk
));
304 disk
.major
= major(stb
.st_rdev
);
305 disk
.minor
= minor(stb
.st_rdev
);
307 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
308 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
309 devname
, chosen_name
, strerror(errno
));
310 ioctl(mdfd
, STOP_ARRAY
, 0);
314 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
315 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
316 /* It really should be 'none' - must be old buggy
317 * kernel, and mdadm -I may not be able to complete.
320 ioctl(mdfd
, STOP_ARRAY
, NULL
);
322 ": You have an old buggy kernel which cannot support\n"
323 " --incremental reliably. Aborting.\n");
330 /* - check one drive in array to make sure metadata is a reasonably */
331 /* close match. Reject if not (e.g. different type) */
332 /* - add the device */
335 mdu_disk_info_t disk
;
338 struct supertype
*st2
;
339 sra
= sysfs_read(mdfd
, devnum
, (GET_VERSION
| GET_DEVS
|
342 if (sra
->array
.major_version
!= st
->ss
->major
||
343 sra
->array
.minor_version
!= st
->minor_version
) {
346 ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
347 devname
, chosen_name
,
348 sra
->array
.major_version
,
349 sra
->array
.minor_version
,
350 st
->ss
->major
, st
->minor_version
);
354 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
355 sra
->devs
->disk
.minor
);
356 dfd2
= dev_open(dn
, O_RDONLY
);
358 if (st2
->ss
->load_super(st2
, dfd2
, NULL
)) {
360 ": Strange error loading metadata for %s.\n",
367 st2
->ss
->getinfo_super(st2
, &info2
);
368 st2
->ss
->free_super(st2
);
369 if (info
.array
.level
!= info2
.array
.level
||
370 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
371 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
373 ": unexpected difference between %s and %s.\n",
374 chosen_name
, devname
);
378 memset(&disk
, 0, sizeof(disk
));
379 disk
.major
= major(stb
.st_rdev
);
380 disk
.minor
= minor(stb
.st_rdev
);
381 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
382 if (err
< 0 && errno
== EBUSY
) {
383 /* could be another device present with the same
384 * disk.number. Find and reject any such
386 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
387 info
.events
, verbose
, chosen_name
);
388 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
391 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
392 devname
, chosen_name
, strerror(errno
));
397 /* 6/ Make sure /var/run/mdadm.map contains this array. */
398 map_update(&map
, devnum
,
399 info
.array
.major_version
,
400 info
.array
.minor_version
,
401 info
.uuid
, chosen_name
);
403 /* 7/ Is there enough devices to possibly start the array? */
404 /* 7a/ if not, finish with success. */
406 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
407 if (enough(info
.array
.level
, info
.array
.raid_disks
,
408 info
.array
.layout
, info
.array
.state
& 1,
409 avail
, active_disks
) == 0) {
413 ": %s attached to %s, not enough to start (%d).\n",
414 devname
, chosen_name
, active_disks
);
421 /* - if number of OK devices match expected, or -R and there */
423 /* + add any bitmap file */
424 /* + start the array (auto-readonly). */
426 mdu_array_info_t ainf
;
428 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
431 ": %s attached to %s which is already active.\n",
432 devname
, chosen_name
);
437 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
439 /* Let's try to start it */
440 if (match
&& match
->bitmap_file
) {
441 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
444 ": Could not open bitmap file %s.\n",
449 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
452 ": Failed to set bitmapfile for %s.\n",
459 sra
= sysfs_read(mdfd
, devnum
, 0);
460 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
462 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
464 rv
= sysfs_set_str(sra
, NULL
,
465 "array_state", "read-auto");
469 ": %s attached to %s, which has been started.\n",
470 devname
, chosen_name
);
474 ": %s attached to %s, but failed to start: %s.\n",
475 devname
, chosen_name
, strerror(errno
));
481 ": %s attached to %s, not enough to start safely.\n",
482 devname
, chosen_name
);
489 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
490 int number
, __u64 events
, int verbose
,
493 /* Find a device attached to this array with a disk.number of number
494 * and events less than the passed events, and remove the device.
499 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
500 return; /* not safe to remove from active arrays
501 * without thinking more */
503 for (d
= sra
->devs
; d
; d
= d
->next
) {
507 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
508 dfd
= dev_open(dn
, O_RDONLY
);
511 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
515 st
->ss
->getinfo_super(st
, &info
);
516 st
->ss
->free_super(st
);
519 if (info
.disk
.number
!= number
||
520 info
.events
>= events
)
523 if (d
->disk
.raid_disk
> -1)
524 sysfs_set_str(sra
, d
, "slot", "none");
525 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
528 ": removing old device %s from %s\n",
529 d
->sys_name
+4, array_name
);
533 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
534 struct mdinfo
*bestinfo
)
536 /* count how many devices in sra think they are active */
538 int cnt
= 0, cnt1
= 0;
539 __u64 max_events
= 0;
540 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
543 for (d
= sra
->devs
; d
; d
= d
->next
) {
549 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
550 dfd
= dev_open(dn
, O_RDONLY
);
553 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
557 st
->ss
->getinfo_super(st
, &info
);
559 avail
= malloc(info
.array
.raid_disks
);
561 fprintf(stderr
, Name
": out of memory.\n");
564 memset(avail
, 0, info
.array
.raid_disks
);
568 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
572 max_events
= info
.events
;
573 avail
[info
.disk
.raid_disk
] = 2;
574 st
->ss
->getinfo_super(st
, bestinfo
);
575 } else if (info
.events
== max_events
) {
577 avail
[info
.disk
.raid_disk
] = 2;
578 } else if (info
.events
== max_events
-1) {
580 avail
[info
.disk
.raid_disk
] = 1;
581 } else if (info
.events
< max_events
- 1)
583 else if (info
.events
== max_events
+1) {
587 max_events
= info
.events
;
588 for (i
=0; i
<info
.array
.raid_disks
; i
++)
591 avail
[info
.disk
.raid_disk
] = 2;
592 st
->ss
->getinfo_super(st
, bestinfo
);
593 } else { /* info.events much bigger */
595 memset(avail
, 0, info
.disk
.raid_disk
);
596 max_events
= info
.events
;
597 st
->ss
->getinfo_super(st
, bestinfo
);
600 st
->ss
->free_super(st
);
605 void RebuildMap(void)
607 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
608 struct mdstat_ent
*md
;
609 struct map_ent
*map
= NULL
;
610 int mdp
= get_mdp_major();
612 for (md
= mdstat
; md
; md
= md
->next
) {
613 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
616 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
620 struct supertype
*st
;
624 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
625 dfd
= dev_open(dn
, O_RDONLY
);
628 st
= guess_super(dfd
);
632 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
636 st
->ss
->getinfo_super(st
, &info
);
638 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
640 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
641 map_add(&map
, md
->devnum
, st
->ss
->major
,
643 info
.uuid
, path
? : "/unknown");
644 st
->ss
->free_super(st
);
652 int IncrementalScan(int verbose
)
654 /* look at every device listed in the 'map' file.
655 * If one is found that is not running then:
656 * look in mdadm.conf for bitmap file.
657 * if one exists, but array has none, add it.
658 * try to start array in auto-readonly mode
660 struct map_ent
*mapl
= NULL
;
662 mddev_ident_t devs
, mddev
;
666 devs
= conf_get_ident(NULL
);
668 for (me
= mapl
; me
; me
= me
->next
) {
670 mdu_array_info_t array
;
671 mdu_bitmap_file_t bmf
;
673 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
,
677 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
682 /* Ok, we can try this one. Maybe it needs a bitmap */
683 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
684 if (strcmp(mddev
->devname
, me
->path
) == 0)
686 if (mddev
&& mddev
->bitmap_file
) {
688 * Note: early kernels will wrongly fail this, so it
692 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
693 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
695 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
703 ": Added bitmap %s to %s\n",
704 mddev
->bitmap_file
, me
->path
);
705 else if (errno
!= EEXIST
)
707 ": Failed to add bitmap to %s: %s\n",
708 me
->path
, strerror(errno
));
711 sra
= sysfs_read(mdfd
, 0, 0);
713 if (sysfs_set_str(sra
, NULL
,
714 "array_state", "read-auto") == 0) {
717 ": started array %s\n",
721 ": failed to start array %s: %s\n",
722 me
->path
, strerror(errno
));