]>
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
;
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",
143 st
->ss
->getinfo_super(st
, &info
);
146 /* 3/ Check if there is a match in mdadm.conf */
148 array_list
= conf_get_ident(NULL
);
150 for (; array_list
; array_list
= array_list
->next
) {
151 if (array_list
->uuid_set
&&
152 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
156 ": UUID differs from %s.\n",
157 array_list
->devname
);
160 if (array_list
->name
[0] &&
161 strcasecmp(array_list
->name
, info
.name
) != 0) {
164 ": Name differs from %s.\n",
165 array_list
->devname
);
168 if (array_list
->devices
&&
169 !match_oneof(array_list
->devices
, devname
)) {
172 ": Not a listed device for %s.\n",
173 array_list
->devname
);
176 if (array_list
->super_minor
!= UnSet
&&
177 array_list
->super_minor
!= info
.array
.md_minor
) {
180 ": Different super-minor to %s.\n",
181 array_list
->devname
);
184 if (!array_list
->uuid_set
&&
185 !array_list
->name
[0] &&
186 !array_list
->devices
&&
187 array_list
->super_minor
== UnSet
) {
190 ": %s doesn't have any identifying information.\n",
191 array_list
->devname
);
194 /* FIXME, should I check raid_disks and level too?? */
199 ": we match both %s and %s - cannot decide which to use.\n",
200 match
->devname
, array_list
->devname
);
206 /* 3a/ if not, check for homehost match. If no match, reject. */
208 if (homehost
== NULL
||
209 st
->ss
->match_home(st
, homehost
) == 0) {
212 ": not found in mdadm.conf and not identified by homehost.\n");
216 /* 4/ Determine device number. */
217 /* - If in mdadm.conf with std name, use that */
218 /* - UUID in /var/run/mdadm.map use that */
219 /* - If name is suggestive, use that. unless in use with */
220 /* different uuid. */
221 /* - Choose a free, high number. */
222 /* - Use a partitioned device unless strong suggestion not to. */
224 if (match
&& is_standard(match
->devname
, &devnum
))
225 /* We have devnum now */;
226 else if ((mp
= map_by_uuid(&map
, info
.uuid
)) != NULL
)
229 /* Have to guess a bit. */
230 int use_partitions
= 1;
232 if ((autof
&7) == 3 || (autof
&7) == 5)
234 np
= strchr(info
.name
, ':');
239 devnum
= strtoul(np
, &ep
, 10);
240 if (ep
> np
&& *ep
== 0) {
241 /* This is a number. Let check that it is unused. */
242 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
248 /* Haven't found anything yet, choose something free */
249 /* There is similar code in mdopen.c - should unify */
250 for (devnum
= 127 ; devnum
!= 128 ;
251 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
252 if (mddev_busy(use_partitions
?
253 (-1-devnum
) : devnum
))
258 ": No spare md devices!!\n");
262 devnum
= use_partitions
? (-1-devnum
) : devnum
;
264 mdfd
= open_mddev_devnum(match
? match
->devname
: NULL
,
269 fprintf(stderr
, Name
": failed to open %s: %s.\n",
270 chosen_name
, strerror(errno
));
273 /* 5/ Find out if array already exists */
274 if (! mddev_busy(devnum
)) {
275 /* 5a/ if it does not */
276 /* - choose a name, from mdadm.conf or 'name' field in array. */
277 /* - create the array */
278 /* - add the device */
279 mdu_array_info_t ainf
;
280 mdu_disk_info_t disk
;
284 memset(&ainf
, 0, sizeof(ainf
));
285 ainf
.major_version
= st
->ss
->major
;
286 ainf
.minor_version
= st
->minor_version
;
287 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
289 ": SET_ARRAY_INFO failed for %s: %s\b",
290 chosen_name
, strerror(errno
));
294 sprintf(md
, "%d.%d\n", st
->ss
->major
, st
->minor_version
);
295 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
296 sysfs_set_str(sra
, NULL
, "metadata_version", md
);
297 memset(&disk
, 0, sizeof(disk
));
298 disk
.major
= major(stb
.st_rdev
);
299 disk
.minor
= minor(stb
.st_rdev
);
301 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
302 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
303 devname
, chosen_name
, strerror(errno
));
304 ioctl(mdfd
, STOP_ARRAY
, 0);
308 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
309 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
310 /* It really should be 'none' - must be old buggy
311 * kernel, and mdadm -I may not be able to complete.
314 ioctl(mdfd
, STOP_ARRAY
, NULL
);
316 ": You have an old buggy kernel which cannot support\n"
317 " --incremental reliably. Aborting.\n");
324 /* - check one drive in array to make sure metadata is a reasonably */
325 /* close match. Reject if not (e.g. different type) */
326 /* - add the device */
329 mdu_disk_info_t disk
;
332 struct supertype
*st2
;
333 sra
= sysfs_read(mdfd
, devnum
, (GET_VERSION
| GET_DEVS
|
336 if (sra
->array
.major_version
!= st
->ss
->major
||
337 sra
->array
.minor_version
!= st
->minor_version
) {
340 ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
341 devname
, chosen_name
,
342 sra
->array
.major_version
,
343 sra
->array
.minor_version
,
344 st
->ss
->major
, st
->minor_version
);
348 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
349 sra
->devs
->disk
.minor
);
350 dfd2
= dev_open(dn
, O_RDONLY
);
352 if (st2
->ss
->load_super(st2
, dfd2
, NULL
)) {
354 ": Strange error loading metadata for %s.\n",
361 st2
->ss
->getinfo_super(st2
, &info2
);
362 st2
->ss
->free_super(st2
);
363 if (info
.array
.level
!= info2
.array
.level
||
364 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
365 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
367 ": unexpected difference between %s and %s.\n",
368 chosen_name
, devname
);
372 memset(&disk
, 0, sizeof(disk
));
373 disk
.major
= major(stb
.st_rdev
);
374 disk
.minor
= minor(stb
.st_rdev
);
375 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
376 if (err
< 0 && errno
== EBUSY
) {
377 /* could be another device present with the same
378 * disk.number. Find and reject any such
380 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
381 info
.events
, verbose
, chosen_name
);
382 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
385 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
386 devname
, chosen_name
, strerror(errno
));
391 /* 6/ Make sure /var/run/mdadm.map contains this array. */
392 map_update(&map
, devnum
,
393 info
.array
.major_version
,
394 info
.array
.minor_version
,
395 info
.uuid
, chosen_name
);
397 /* 7/ Is there enough devices to possibly start the array? */
398 /* 7a/ if not, finish with success. */
400 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
401 if (enough(info
.array
.level
, info
.array
.raid_disks
,
402 info
.array
.layout
, info
.array
.state
& 1,
403 avail
, active_disks
) == 0) {
407 ": %s attached to %s, not enough to start (%d).\n",
408 devname
, chosen_name
, active_disks
);
415 /* - if number of OK devices match expected, or -R and there */
417 /* + add any bitmap file */
418 /* + start the array (auto-readonly). */
420 mdu_array_info_t ainf
;
422 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
425 ": %s attached to %s which is already active.\n",
426 devname
, chosen_name
);
431 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
433 /* Let's try to start it */
434 if (match
&& match
->bitmap_file
) {
435 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
438 ": Could not open bitmap file %s.\n",
443 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
446 ": Failed to set bitmapfile for %s.\n",
453 sra
= sysfs_read(mdfd
, devnum
, 0);
454 if (sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
455 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
457 rv
= sysfs_set_str(sra
, NULL
,
458 "array_state", "read-auto");
462 ": %s attached to %s, which has been started.\n",
463 devname
, chosen_name
);
467 ": %s attached to %s, but failed to start: %s.\n",
468 devname
, chosen_name
, strerror(errno
));
474 ": %s attached to %s, not enough to start safely.\n",
475 devname
, chosen_name
);
482 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
483 int number
, __u64 events
, int verbose
,
486 /* Find a device attached to this array with a disk.number of number
487 * and events less than the passed events, and remove the device.
492 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
493 return; /* not safe to remove from active arrays
494 * without thinking more */
496 for (d
= sra
->devs
; d
; d
= d
->next
) {
500 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
501 dfd
= dev_open(dn
, O_RDONLY
);
504 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
508 st
->ss
->getinfo_super(st
, &info
);
509 st
->ss
->free_super(st
);
512 if (info
.disk
.number
!= number
||
513 info
.events
>= events
)
516 if (d
->disk
.raid_disk
> -1)
517 sysfs_set_str(sra
, d
, "slot", "none");
518 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
521 ": removing old device %s from %s\n",
522 d
->sys_name
+4, array_name
);
526 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
527 struct mdinfo
*bestinfo
)
529 /* count how many devices in sra think they are active */
531 int cnt
= 0, cnt1
= 0;
532 __u64 max_events
= 0;
533 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
536 for (d
= sra
->devs
; d
; d
= d
->next
) {
542 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
543 dfd
= dev_open(dn
, O_RDONLY
);
546 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
550 st
->ss
->getinfo_super(st
, &info
);
551 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
554 avail
= malloc(info
.array
.raid_disks
);
555 memset(avail
, 0, info
.array
.raid_disks
);
559 max_events
= info
.events
;
560 avail
[info
.disk
.raid_disk
] = 2;
561 st
->ss
->getinfo_super(st
, bestinfo
);
562 } else if (info
.events
== max_events
) {
564 avail
[info
.disk
.raid_disk
] = 2;
565 } else if (info
.events
== max_events
-1) {
567 avail
[info
.disk
.raid_disk
] = 1;
568 } else if (info
.events
< max_events
- 1)
570 else if (info
.events
== max_events
+1) {
574 max_events
= info
.events
;
575 for (i
=0; i
<info
.array
.raid_disks
; i
++)
578 avail
[info
.disk
.raid_disk
] = 2;
579 st
->ss
->getinfo_super(st
, bestinfo
);
580 } else { /* info.events much bigger */
582 memset(avail
, 0, info
.disk
.raid_disk
);
583 max_events
= info
.events
;
584 st
->ss
->getinfo_super(st
, bestinfo
);
587 st
->ss
->free_super(st
);
592 void RebuildMap(void)
594 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
595 struct mdstat_ent
*md
;
596 struct map_ent
*map
= NULL
;
597 int mdp
= get_mdp_major();
599 for (md
= mdstat
; md
; md
= md
->next
) {
600 struct mdinfo
*sra
= sysfs_read(-1, md
->devnum
, GET_DEVS
);
603 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
607 struct supertype
*st
;
611 sprintf(dn
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
612 dfd
= dev_open(dn
, O_RDONLY
);
615 st
= guess_super(dfd
);
619 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
623 st
->ss
->getinfo_super(st
, &info
);
625 path
= map_dev(MD_MAJOR
, md
->devnum
, 0);
627 path
= map_dev(mdp
, (-1-md
->devnum
)<< 6, 0);
628 map_add(&map
, md
->devnum
, st
->ss
->major
,
630 info
.uuid
, path
? : "/unknown");
631 st
->ss
->free_super(st
);
639 int IncrementalScan(int verbose
)
641 /* look at every device listed in the 'map' file.
642 * If one is found that is not running then:
643 * look in mdadm.conf for bitmap file.
644 * if one exists, but array has none, add it.
645 * try to start array in auto-readonly mode
647 struct map_ent
*mapl
= NULL
;
649 mddev_ident_t devs
, mddev
;
653 devs
= conf_get_ident(NULL
);
655 for (me
= mapl
; me
; me
= me
->next
) {
657 mdu_array_info_t array
;
658 mdu_bitmap_file_t bmf
;
660 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
, NULL
, path
);
663 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
668 /* Ok, we can try this one. Maybe it needs a bitmap */
669 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
670 if (strcmp(mddev
->devname
, me
->path
) == 0)
672 if (mddev
&& mddev
->bitmap_file
) {
674 * Note: early kernels will wrongly fail this, so it
678 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
679 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
681 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
689 ": Added bitmap %s to %s\n",
690 mddev
->bitmap_file
, me
->path
);
691 else if (errno
!= EEXIST
)
693 ": Failed to add bitmap to %s: %s\n",
694 me
->path
, strerror(errno
));
697 sra
= sysfs_read(mdfd
, 0, 0);
699 if (sysfs_set_str(sra
, NULL
,
700 "array_state", "read-auto") == 0) {
703 ": started array %s\n",
707 ": failed to start array %s: %s\n",
708 me
->path
, strerror(errno
));