]>
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
;
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 * ARRAY takes precedence, then command line, then
231 if (match
&& match
->autof
)
232 autof
= match
->autof
;
236 if (match
&& (rv
= is_standard(match
->devname
, &devnum
))) {
237 devnum
= (rv
> 0) ? (-1-devnum
) : devnum
;
238 } else if ((mp
= map_by_uuid(&map
, info
.uuid
)) != NULL
)
241 /* Have to guess a bit. */
242 int use_partitions
= 1;
244 if ((autof
&7) == 3 || (autof
&7) == 5)
246 np
= name
? strchr(name
, ':') : ":NONAME";
251 devnum
= strtoul(np
, &ep
, 10);
252 if (ep
> np
&& *ep
== 0) {
253 /* This is a number. Let check that it is unused. */
254 if (mddev_busy(use_partitions
? (-1-devnum
) : devnum
))
260 /* Haven't found anything yet, choose something free */
261 devnum
= find_free_devnum(use_partitions
);
263 if (devnum
== NoMdDev
) {
265 ": No spare md devices!!\n");
269 devnum
= use_partitions
? (-1-devnum
) : devnum
;
271 mdfd
= open_mddev_devnum(match
? match
->devname
: NULL
,
274 chosen_name
, autof
>> 3);
276 fprintf(stderr
, Name
": failed to open %s: %s.\n",
277 chosen_name
, strerror(errno
));
280 /* 5/ Find out if array already exists */
281 if (! mddev_busy(devnum
)) {
282 /* 5a/ if it does not */
283 /* - choose a name, from mdadm.conf or 'name' field in array. */
284 /* - create the array */
285 /* - add the device */
286 mdu_array_info_t ainf
;
287 mdu_disk_info_t disk
;
291 memset(&ainf
, 0, sizeof(ainf
));
292 ainf
.major_version
= st
->ss
->major
;
293 ainf
.minor_version
= st
->minor_version
;
294 if (ioctl(mdfd
, SET_ARRAY_INFO
, &ainf
) != 0) {
296 ": SET_ARRAY_INFO failed for %s: %s\b",
297 chosen_name
, strerror(errno
));
301 sprintf(md
, "%d.%d\n", st
->ss
->major
, st
->minor_version
);
302 sra
= sysfs_read(mdfd
, devnum
, GET_VERSION
);
303 sysfs_set_str(sra
, NULL
, "metadata_version", md
);
304 memset(&disk
, 0, sizeof(disk
));
305 disk
.major
= major(stb
.st_rdev
);
306 disk
.minor
= minor(stb
.st_rdev
);
308 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
) != 0) {
309 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
310 devname
, chosen_name
, strerror(errno
));
311 ioctl(mdfd
, STOP_ARRAY
, 0);
315 sra
= sysfs_read(mdfd
, devnum
, GET_DEVS
);
316 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
317 /* It really should be 'none' - must be old buggy
318 * kernel, and mdadm -I may not be able to complete.
321 ioctl(mdfd
, STOP_ARRAY
, NULL
);
323 ": You have an old buggy kernel which cannot support\n"
324 " --incremental reliably. Aborting.\n");
331 /* - check one drive in array to make sure metadata is a reasonably */
332 /* close match. Reject if not (e.g. different type) */
333 /* - add the device */
336 mdu_disk_info_t disk
;
339 struct supertype
*st2
;
340 sra
= sysfs_read(mdfd
, devnum
, (GET_VERSION
| GET_DEVS
|
343 if (sra
->array
.major_version
!= st
->ss
->major
||
344 sra
->array
.minor_version
!= st
->minor_version
) {
347 ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
348 devname
, chosen_name
,
349 sra
->array
.major_version
,
350 sra
->array
.minor_version
,
351 st
->ss
->major
, st
->minor_version
);
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
)) {
361 ": Strange error loading metadata for %s.\n",
368 st2
->ss
->getinfo_super(st2
, &info2
);
369 st2
->ss
->free_super(st2
);
370 if (info
.array
.level
!= info2
.array
.level
||
371 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
372 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
374 ": unexpected difference between %s and %s.\n",
375 chosen_name
, devname
);
379 memset(&disk
, 0, sizeof(disk
));
380 disk
.major
= major(stb
.st_rdev
);
381 disk
.minor
= minor(stb
.st_rdev
);
382 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
383 if (err
< 0 && errno
== EBUSY
) {
384 /* could be another device present with the same
385 * disk.number. Find and reject any such
387 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
388 info
.events
, verbose
, chosen_name
);
389 err
= ioctl(mdfd
, ADD_NEW_DISK
, &disk
);
392 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
393 devname
, chosen_name
, strerror(errno
));
398 /* 6/ Make sure /var/run/mdadm.map contains this array. */
399 map_update(&map
, devnum
,
400 info
.array
.major_version
,
401 info
.array
.minor_version
,
402 info
.uuid
, chosen_name
);
404 /* 7/ Is there enough devices to possibly start the array? */
405 /* 7a/ if not, finish with success. */
407 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
408 if (enough(info
.array
.level
, info
.array
.raid_disks
,
409 info
.array
.layout
, info
.array
.state
& 1,
410 avail
, active_disks
) == 0) {
414 ": %s attached to %s, not enough to start (%d).\n",
415 devname
, chosen_name
, active_disks
);
422 /* - if number of OK devices match expected, or -R and there */
424 /* + add any bitmap file */
425 /* + start the array (auto-readonly). */
427 mdu_array_info_t ainf
;
429 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
432 ": %s attached to %s which is already active.\n",
433 devname
, chosen_name
);
438 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
440 /* Let's try to start it */
441 if (match
&& match
->bitmap_file
) {
442 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
445 ": Could not open bitmap file %s.\n",
450 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
453 ": Failed to set bitmapfile for %s.\n",
460 sra
= sysfs_read(mdfd
, devnum
, 0);
461 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
);
560 avail
= malloc(info
.array
.raid_disks
);
562 fprintf(stderr
, Name
": out of memory.\n");
565 memset(avail
, 0, info
.array
.raid_disks
);
569 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
573 max_events
= info
.events
;
574 avail
[info
.disk
.raid_disk
] = 2;
575 st
->ss
->getinfo_super(st
, bestinfo
);
576 } else if (info
.events
== max_events
) {
578 avail
[info
.disk
.raid_disk
] = 2;
579 } else if (info
.events
== max_events
-1) {
581 avail
[info
.disk
.raid_disk
] = 1;
582 } else if (info
.events
< max_events
- 1)
584 else if (info
.events
== max_events
+1) {
588 max_events
= info
.events
;
589 for (i
=0; i
<info
.array
.raid_disks
; i
++)
592 avail
[info
.disk
.raid_disk
] = 2;
593 st
->ss
->getinfo_super(st
, bestinfo
);
594 } else { /* info.events much bigger */
596 memset(avail
, 0, info
.disk
.raid_disk
);
597 max_events
= info
.events
;
598 st
->ss
->getinfo_super(st
, bestinfo
);
601 st
->ss
->free_super(st
);
606 int IncrementalScan(int verbose
)
608 /* look at every device listed in the 'map' file.
609 * If one is found that is not running then:
610 * look in mdadm.conf for bitmap file.
611 * if one exists, but array has none, add it.
612 * try to start array in auto-readonly mode
614 struct map_ent
*mapl
= NULL
;
616 mddev_ident_t devs
, mddev
;
620 devs
= conf_get_ident(NULL
);
622 for (me
= mapl
; me
; me
= me
->next
) {
624 mdu_array_info_t array
;
625 mdu_bitmap_file_t bmf
;
627 int mdfd
= open_mddev_devnum(me
->path
, me
->devnum
,
631 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
636 /* Ok, we can try this one. Maybe it needs a bitmap */
637 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
638 if (strcmp(mddev
->devname
, me
->path
) == 0)
640 if (mddev
&& mddev
->bitmap_file
) {
642 * Note: early kernels will wrongly fail this, so it
646 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
647 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
649 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
657 ": Added bitmap %s to %s\n",
658 mddev
->bitmap_file
, me
->path
);
659 else if (errno
!= EEXIST
)
661 ": Failed to add bitmap to %s: %s\n",
662 me
->path
, strerror(errno
));
665 sra
= sysfs_read(mdfd
, 0, 0);
667 if (sysfs_set_str(sra
, NULL
,
668 "array_state", "read-auto") == 0) {
671 ": started array %s\n",
675 ": failed to start array %s: %s\n",
676 me
->path
, strerror(errno
));