]>
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-2009 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
35 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
37 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
38 int number
, __u64 events
, int verbose
,
40 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
41 struct supertype
*st
, int verbose
);
43 static int Incremental_container(struct supertype
*st
, char *devname
,
45 int verbose
, int runstop
, int autof
);
47 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
50 int verbose
, int *rvp
);
52 int Incremental(char *devname
, int verbose
, int runstop
,
53 struct supertype
*st
, char *homehost
, int require_homehost
,
56 /* Add this device to an array, creating the array if necessary
57 * and starting the array if sensible or - if runstop>0 - if possible.
59 * This has several steps:
61 * 1/ Check if device is permitted by mdadm.conf, reject if not.
62 * 2/ Find metadata, reject if none appropriate (check
63 * version/name from args)
64 * 3/ Check if there is a match in mdadm.conf
65 * 3a/ if not, check for homehost match. If no match, assemble as
67 * 4/ Determine device number.
68 * - If in mdadm.conf with std name, use that
69 * - UUID in /var/run/mdadm.map use that
70 * - If name is suggestive, use that. unless in use with different uuid.
71 * - Choose a free, high number.
72 * - Use a partitioned device unless strong suggestion not to.
74 * Don't choose partitioned for containers.
75 * 5/ Find out if array already exists
77 * - choose a name, from mdadm.conf or 'name' field in array.
81 * - check one drive in array to make sure metadata is a reasonably
82 * close match. Reject if not (e.g. different type)
84 * 6/ Make sure /var/run/mdadm.map contains this array.
85 * 7/ Is there enough devices to possibly start the array?
86 * For a container, this means running Incremental_container.
87 * 7a/ if not, finish with success.
89 * - read all metadata and arrange devices like -A does
90 * - if number of OK devices match expected, or -R and there are enough,
91 * start the array (auto-readonly).
94 struct mdinfo info
, dinfo
;
95 struct mddev_ident
*match
;
96 char chosen_name
[1024];
98 struct map_ent
*mp
, *map
= NULL
;
99 int dfd
= -1, mdfd
= -1;
104 mdu_array_info_t ainf
;
105 struct dev_policy
*policy
= NULL
;
107 struct createinfo
*ci
= conf_get_create_info();
109 if (stat(devname
, &stb
) < 0) {
111 fprintf(stderr
, Name
": stat failed for %s: %s.\n",
112 devname
, strerror(errno
));
115 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
117 fprintf(stderr
, Name
": %s is not a block device.\n",
121 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
124 fprintf(stderr
, Name
": cannot open %s: %s.\n",
125 devname
, strerror(errno
));
128 /* If the device is a container, we do something very different */
129 if (must_be_container(dfd
)) {
131 st
= super_by_fd(dfd
, NULL
);
133 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
136 if (!rv
&& st
->ss
->container_content
)
137 return Incremental_container(st
, devname
, homehost
,
138 verbose
, runstop
, autof
);
140 fprintf(stderr
, Name
": %s is not part of an md array.\n",
145 /* 1/ Check if device is permitted by mdadm.conf */
147 if (!conf_test_dev(devname
)) {
150 ": %s not permitted by mdadm.conf.\n",
155 /* 2/ Find metadata, reject if none appropriate (check
156 * version/name from args) */
158 if (fstat(dfd
, &stb
) < 0) {
160 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
161 devname
, strerror(errno
));
164 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
166 fprintf(stderr
, Name
": %s is not a block device.\n",
171 dinfo
.disk
.major
= major(stb
.st_rdev
);
172 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
174 policy
= disk_policy(&dinfo
);
176 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
179 ": no recognisable superblock on %s.\n",
181 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
184 if (st
->ss
->compare_super
== NULL
||
185 st
->ss
->load_super(st
, dfd
, NULL
)) {
187 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
189 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
193 close (dfd
); dfd
= -1;
195 memset(&info
, 0, sizeof(info
));
196 st
->ss
->getinfo_super(st
, &info
, NULL
);
198 /* 3/ Check if there is a match in mdadm.conf */
199 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
200 if (!match
&& rv
== 2)
203 if (match
&& match
->devname
204 && strcasecmp(match
->devname
, "<ignore>") == 0) {
206 fprintf(stderr
, Name
": array containing %s is explicitly"
207 " ignored by mdadm.conf\n",
212 /* 3a/ if not, check for homehost match. If no match, continue
213 * but don't trust the 'name' in the array. Thus a 'random' minor
214 * number will be assigned, and the device name will be based
218 else if (st
->ss
->match_home(st
, homehost
) == 1)
220 else if (st
->ss
->match_home(st
, "any") == 1)
221 trustworthy
= LOCAL_ANY
;
223 trustworthy
= FOREIGN
;
226 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
227 (trustworthy
== LOCAL
))) {
230 ": %s has metadata type %s for which "
231 "auto-assembly is disabled\n",
232 devname
, st
->ss
->name
);
235 if (trustworthy
== LOCAL_ANY
)
238 /* There are three possible sources for 'autof': command line,
239 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
240 * ARRAY takes precedence, then command line, then
243 if (match
&& match
->autof
)
244 autof
= match
->autof
;
248 name_to_use
= info
.name
;
249 if (name_to_use
[0] == 0 &&
250 info
.array
.level
== LEVEL_CONTAINER
&&
251 trustworthy
== LOCAL
) {
252 name_to_use
= info
.text_version
;
253 trustworthy
= METADATA
;
255 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
256 ! require_homehost
&&
257 conf_name_is_free(name_to_use
))
260 /* strip "hostname:" prefix from name if we have decided
261 * to treat it as LOCAL
263 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
264 name_to_use
= strchr(name_to_use
, ':')+1;
266 /* 4/ Check if array exists.
269 fprintf(stderr
, Name
": failed to get exclusive lock on "
271 mp
= map_by_uuid(&map
, info
.uuid
);
273 mdfd
= open_dev(mp
->devnum
);
280 /* Couldn't find an existing array, maybe make a new one */
281 mdfd
= create_mddev(match
? match
->devname
: NULL
,
282 name_to_use
, autof
, trustworthy
, chosen_name
);
287 sysfs_init(&info
, mdfd
, 0);
289 if (set_array_info(mdfd
, st
, &info
) != 0) {
290 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
291 chosen_name
, strerror(errno
));
297 dinfo
.disk
.major
= major(stb
.st_rdev
);
298 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
299 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
300 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
301 devname
, chosen_name
, strerror(errno
));
302 ioctl(mdfd
, STOP_ARRAY
, 0);
306 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
307 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
308 /* It really should be 'none' - must be old buggy
309 * kernel, and mdadm -I may not be able to complete.
312 ioctl(mdfd
, STOP_ARRAY
, NULL
);
314 ": You have an old buggy kernel which cannot support\n"
315 " --incremental reliably. Aborting.\n");
320 info
.array
.working_disks
= 1;
322 /* 6/ Make sure /var/run/mdadm.map contains this array. */
323 map_update(&map
, fd2devnum(mdfd
),
325 info
.uuid
, chosen_name
);
328 /* - check one drive in array to make sure metadata is a reasonably */
329 /* close match. Reject if not (e.g. different type) */
330 /* - add the device */
335 struct supertype
*st2
;
336 struct mdinfo info2
, *d
;
339 strcpy(chosen_name
, mp
->path
);
341 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
343 /* It is generally not OK to add non-spare drives to a
344 * running array as they are probably missing because
345 * they failed. However if runstop is 1, then the
346 * array was possibly started early and our best bet is
347 * to add this anyway.
348 * Also if action policy is re-add or better we allow
351 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
352 && ! policy_action_allows(policy
, st
->ss
->name
,
357 if (st
->ss
->external
) {
358 char *devname
= devnum2devname(fd2devnum(mdfd
));
360 active
= devname
&& is_container_active(devname
);
362 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
366 ": not adding %s to active array (without --run) %s\n",
367 devname
, chosen_name
);
372 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
378 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
379 sra
->devs
->disk
.minor
);
380 dfd2
= dev_open(dn
, O_RDONLY
);
382 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
383 st
->ss
->compare_super(st
, st2
) != 0) {
385 ": metadata mismatch between %s and "
387 devname
, chosen_name
);
393 memset(&info2
, 0, sizeof(info2
));
394 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
395 st2
->ss
->free_super(st2
);
396 if (info
.array
.level
!= info2
.array
.level
||
397 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
398 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
400 ": unexpected difference between %s and %s.\n",
401 chosen_name
, devname
);
406 info2
.disk
.major
= major(stb
.st_rdev
);
407 info2
.disk
.minor
= minor(stb
.st_rdev
);
408 /* add disk needs to know about containers */
409 if (st
->ss
->external
)
410 sra
->array
.level
= LEVEL_CONTAINER
;
411 err
= add_disk(mdfd
, st
, sra
, &info2
);
412 if (err
< 0 && errno
== EBUSY
) {
413 /* could be another device present with the same
414 * disk.number. Find and reject any such
416 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
417 info
.events
, verbose
, chosen_name
);
418 err
= add_disk(mdfd
, st
, sra
, &info2
);
421 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
422 devname
, chosen_name
, strerror(errno
));
426 info
.array
.working_disks
= 0;
427 for (d
= sra
->devs
; d
; d
=d
->next
)
428 info
.array
.working_disks
++;
432 /* 7/ Is there enough devices to possibly start the array? */
433 /* 7a/ if not, finish with success. */
434 if (info
.array
.level
== LEVEL_CONTAINER
) {
435 /* Try to assemble within the container */
437 sysfs_uevent(&info
, "change");
440 ": container %s now has %d devices\n",
441 chosen_name
, info
.array
.working_disks
);
442 wait_for(chosen_name
, mdfd
);
444 rv
= Incremental(chosen_name
, verbose
, runstop
,
445 NULL
, homehost
, require_homehost
, autof
);
447 /* Don't fail the whole -I if a subarray didn't
448 * have enough devices to start yet
454 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
455 if (enough(info
.array
.level
, info
.array
.raid_disks
,
456 info
.array
.layout
, info
.array
.state
& 1,
457 avail
, active_disks
) == 0) {
461 ": %s attached to %s, not enough to start (%d).\n",
462 devname
, chosen_name
, active_disks
);
470 /* - if number of OK devices match expected, or -R and there */
472 /* + add any bitmap file */
473 /* + start the array (auto-readonly). */
475 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
478 ": %s attached to %s which is already active.\n",
479 devname
, chosen_name
);
486 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
487 struct mdinfo
*sra
, *dsk
;
488 /* Let's try to start it */
489 if (match
&& match
->bitmap_file
) {
490 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
493 ": Could not open bitmap file %s.\n",
497 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
500 ": Failed to set bitmapfile for %s.\n",
506 /* GET_* needed so add_disk works below */
507 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
),
508 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
509 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
510 && trustworthy
!= FOREIGN
)
511 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
513 rv
= sysfs_set_str(sra
, NULL
,
514 "array_state", "read-auto");
518 ": %s attached to %s, which has been started.\n",
519 devname
, chosen_name
);
521 wait_for(chosen_name
, mdfd
);
522 /* We just started the array, so some devices
523 * might have been evicted from the array
524 * because their event counts were too old.
525 * If the action=re-add policy is in-force for
526 * those devices we should re-add them now.
528 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
529 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
530 add_disk(mdfd
, st
, sra
, dsk
) == 0)
532 ": %s re-added to %s\n",
533 dsk
->sys_name
, chosen_name
);
537 ": %s attached to %s, but failed to start: %s.\n",
538 devname
, chosen_name
, strerror(errno
));
544 ": %s attached to %s, not enough to start safely.\n",
545 devname
, chosen_name
);
554 dev_policy_free(policy
);
558 static struct mddev_ident
*search_mdstat(struct supertype
*st
,
561 int verbose
, int *rvp
)
563 struct mddev_ident
*array_list
, *match
;
564 array_list
= conf_get_ident(NULL
);
566 for (; array_list
; array_list
= array_list
->next
) {
567 if (array_list
->uuid_set
&&
568 same_uuid(array_list
->uuid
, info
->uuid
, st
->ss
->swapuuid
)
570 if (verbose
>= 2 && array_list
->devname
)
572 ": UUID differs from %s.\n",
573 array_list
->devname
);
576 if (array_list
->name
[0] &&
577 strcasecmp(array_list
->name
, info
->name
) != 0) {
578 if (verbose
>= 2 && array_list
->devname
)
580 ": Name differs from %s.\n",
581 array_list
->devname
);
584 if (array_list
->devices
&&
585 !match_oneof(array_list
->devices
, devname
)) {
586 if (verbose
>= 2 && array_list
->devname
)
588 ": Not a listed device for %s.\n",
589 array_list
->devname
);
592 if (array_list
->super_minor
!= UnSet
&&
593 array_list
->super_minor
!= info
->array
.md_minor
) {
594 if (verbose
>= 2 && array_list
->devname
)
596 ": Different super-minor to %s.\n",
597 array_list
->devname
);
600 if (!array_list
->uuid_set
&&
601 !array_list
->name
[0] &&
602 !array_list
->devices
&&
603 array_list
->super_minor
== UnSet
) {
604 if (verbose
>= 2 && array_list
->devname
)
606 ": %s doesn't have any identifying information.\n",
607 array_list
->devname
);
610 /* FIXME, should I check raid_disks and level too?? */
614 if (match
->devname
&& array_list
->devname
)
616 ": we match both %s and %s - cannot decide which to use.\n",
617 match
->devname
, array_list
->devname
);
620 ": multiple lines in mdadm.conf match\n");
631 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
632 int number
, __u64 events
, int verbose
,
635 /* Find a device attached to this array with a disk.number of number
636 * and events less than the passed events, and remove the device.
641 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
642 return; /* not safe to remove from active arrays
643 * without thinking more */
645 for (d
= sra
->devs
; d
; d
= d
->next
) {
649 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
650 dfd
= dev_open(dn
, O_RDONLY
);
653 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
657 st
->ss
->getinfo_super(st
, &info
, NULL
);
658 st
->ss
->free_super(st
);
661 if (info
.disk
.number
!= number
||
662 info
.events
>= events
)
665 if (d
->disk
.raid_disk
> -1)
666 sysfs_set_str(sra
, d
, "slot", "none");
667 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
670 ": removing old device %s from %s\n",
671 d
->sys_name
+4, array_name
);
675 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
676 struct mdinfo
*bestinfo
)
678 /* count how many devices in sra think they are active */
680 int cnt
= 0, cnt1
= 0;
681 __u64 max_events
= 0;
682 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
688 for (d
= sra
->devs
; d
; d
= d
->next
) {
694 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
695 dfd
= dev_open(dn
, O_RDONLY
);
698 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
702 st
->ss
->getinfo_super(st
, &info
, NULL
);
704 avail
= malloc(info
.array
.raid_disks
);
706 fprintf(stderr
, Name
": out of memory.\n");
709 memset(avail
, 0, info
.array
.raid_disks
);
713 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
717 max_events
= info
.events
;
718 avail
[info
.disk
.raid_disk
] = 2;
719 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
720 } else if (info
.events
== max_events
) {
722 avail
[info
.disk
.raid_disk
] = 2;
723 } else if (info
.events
== max_events
-1) {
725 avail
[info
.disk
.raid_disk
] = 1;
726 } else if (info
.events
< max_events
- 1)
728 else if (info
.events
== max_events
+1) {
732 max_events
= info
.events
;
733 for (i
=0; i
<info
.array
.raid_disks
; i
++)
736 avail
[info
.disk
.raid_disk
] = 2;
737 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
738 } else { /* info.events much bigger */
740 memset(avail
, 0, info
.disk
.raid_disk
);
741 max_events
= info
.events
;
742 avail
[info
.disk
.raid_disk
] = 2;
743 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
746 st
->ss
->free_super(st
);
751 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
752 struct supertype
*st
, int verbose
)
754 /* This device doesn't have any md metadata
755 * If it is 'bare' and theh device policy allows 'spare' look for
756 * an array or container to attach it to.
757 * If st is set, then only arrays of that type are considered
758 * Return 0 on success, or some exit code on failure, probably 1.
762 struct map_ent
*mp
, *map
= NULL
;
763 struct mdinfo
*chosen
= NULL
;
766 if (fstat(dfd
, &stb
) != 0)
770 * Now we need to find a suitable array to add this to.
771 * We only accept arrays that:
773 * - are in the same domains as the device
774 * - are of an size for which the device will be useful
775 * and we choose the one that is the most degraded
778 if (map_lock(&map
)) {
779 fprintf(stderr
, Name
": failed to get exclusive lock on "
783 for (mp
= map
; mp
; mp
= mp
->next
) {
784 struct supertype
*st2
;
785 struct domainlist
*dl
= NULL
;
787 unsigned long long devsize
;
789 if (is_subarray(mp
->metadata
))
792 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
794 (st
->minor_version
>= 0 &&
795 st
->minor_version
!= st2
->minor_version
)) {
797 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
804 sra
= sysfs_read(-1, mp
->devnum
,
805 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
806 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
808 /* Probably a container - no degraded info */
809 sra
= sysfs_read(-1, mp
->devnum
,
810 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
811 GET_COMPONENT
|GET_VERSION
);
813 sra
->array
.failed_disks
= 0;
820 for(i
=0; !st2
&& superlist
[i
]; i
++)
821 st2
= superlist
[i
]->match_metadata_desc(
825 get_dev_size(dfd
, NULL
, &devsize
);
826 if (st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
) {
828 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
832 dl
= domain_from_array(sra
, st2
->ss
->name
);
833 if (!domain_test(dl
, pol
, st2
->ss
->name
)) {
834 /* domain test fails */
836 fprintf(stderr
, Name
": not adding %s to %s as it is not in a compatible domain\n",
841 /* all tests passed, OK to add to this array */
845 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
859 /* add current device to chosen array as a spare */
860 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
862 struct mddev_dev devlist
;
867 devlist
.writemostly
= 0;
868 devlist
.devname
= devname
;
869 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
871 devlist
.disposition
= 'a';
874 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
880 fprintf(stderr
, Name
": added %s as spare for %s\n",
881 devname
, chosen
->sys_name
);
883 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
884 devname
, chosen
->sys_name
);
891 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
892 struct supertype
*st
, int verbose
)
894 /* we know that at least one partition virtual-metadata is
895 * allowed to incorporate spares like this device. We need to
896 * find a suitable device to copy partition information from.
898 * Getting a list of all disk (not partition) devices is
899 * slightly non-trivial. We could look at /sys/block, but
900 * that is theoretically due to be removed. Maybe best to use
901 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
902 * we depend on this directory of 'path' info. But that fails
903 * to find loop devices and probably others. Maybe don't
904 * worry about that, they aren't the real target.
906 * So: check things in /dev/disk/by-path to see if they are in
907 * a compatible domain, then load the partition table and see
908 * if it is OK for the new device, and choose the largest
909 * partition table that fits.
914 unsigned long long chosen_size
;
915 struct supertype
*chosen_st
= NULL
;
918 dir
= opendir("/dev/disk/by-path");
921 while ((de
= readdir(dir
)) != NULL
) {
923 struct dev_policy
*pol2
= NULL
;
924 struct domainlist
*domlist
= NULL
;
927 struct supertype
*st2
= NULL
;
928 char *devname
= NULL
;
929 unsigned long long devsectors
;
931 if (de
->d_ino
== 0 ||
932 de
->d_name
[0] == '.' ||
933 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
936 ep
= de
->d_name
+ strlen(de
->d_name
);
937 while (ep
> de
->d_name
&&
940 if (ep
> de
->d_name
+ 5 &&
941 strncmp(ep
-5, "-part", 5) == 0)
942 /* This is a partition - skip it */
945 pol2
= path_policy(de
->d_name
, type_disk
);
947 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
948 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) == 0)
949 /* new device is incompatible with this device. */
952 domain_free(domlist
);
955 asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
);
956 fd
= open(devname
, O_RDONLY
);
959 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
966 st2
= guess_super_type(fd
, guess_partitions
);
968 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
972 /* Check domain policy again, this time referring to metadata */
973 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
974 if (domain_test(domlist
, pol
, st2
->ss
->name
) == 0)
975 /* Incompatible devices for this metadata type */
979 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
980 if (info
.component_size
> devsectors
)
981 /* This partitioning doesn't fit in the device */
984 /* This is an acceptable device to copy partition
985 * metadata from. We could just stop here, but I
986 * think I want to keep looking incase a larger
987 * metadata which makes better use of the device can
990 if (chosen
== NULL
||
991 chosen_size
< info
.component_size
) {
992 chosen_size
= info
.component_size
;
997 chosen_st
->ss
->free_super(chosen_st
);
1006 domain_free(domlist
);
1007 dev_policy_free(pol2
);
1009 st2
->ss
->free_super(st2
);
1019 /* 'chosen' is the best device we can find. Let's write its
1020 * metadata to devname dfd is read-only so don't use that
1022 fd
= open(devname
, O_RDWR
);
1024 chosen_st
->ss
->store_super(chosen_st
, fd
);
1028 chosen_st
->ss
->free_super(chosen_st
);
1034 /* adding a spare to a regular array is quite different from adding one to
1035 * a set-of-partitions virtual array.
1036 * This function determines which is worth trying and tries as appropriate.
1037 * Arrays are given priority over partitions.
1039 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1040 struct supertype
*st
, int verbose
)
1045 int partitions_ok
= 0;
1046 char bufpad
[4096 + 4096];
1047 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1050 /* Can only add a spare if device has at least one domains */
1051 if (pol_find(pol
, pol_domain
) == NULL
)
1053 /* And only if some action allows spares */
1054 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1057 /* Now check if the device is bare - we don't add non-bare devices
1058 * yet even if action=-spare
1061 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1062 read(dfd
, buf
, 4096) != 4096) {
1065 fprintf(stderr
, Name
": %s is not bare, so not considering as a spare\n",
1069 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1071 if (memcmp(buf
, buf
+1, 4095) != 0)
1074 /* OK, first 4K appear blank, try the end. */
1075 if (lseek(dfd
, -4096, SEEK_END
) < 0 ||
1076 read(dfd
, buf
, 4096) != 4096)
1079 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1081 if (memcmp(buf
, buf
+1, 4095) != 0)
1084 /* This device passes our test for 'is bare'.
1085 * Let's see what policy allows for such things.
1088 /* just try try 'array' or 'partition' based on this metadata */
1089 if (st
->ss
->add_to_super
)
1090 return array_try_spare(devname
, dfdp
, pol
,
1093 return partition_try_spare(devname
, dfdp
, pol
,
1096 /* Now see which metadata type support spare */
1097 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1098 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1099 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1101 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1102 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1107 rv
= array_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1108 if (rv
== 0 && partitions_ok
)
1109 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1113 int IncrementalScan(int verbose
)
1115 /* look at every device listed in the 'map' file.
1116 * If one is found that is not running then:
1117 * look in mdadm.conf for bitmap file.
1118 * if one exists, but array has none, add it.
1119 * try to start array in auto-readonly mode
1121 struct map_ent
*mapl
= NULL
;
1123 struct mddev_ident
*devs
, *mddev
;
1127 devs
= conf_get_ident(NULL
);
1129 for (me
= mapl
; me
; me
= me
->next
) {
1130 mdu_array_info_t array
;
1131 mdu_bitmap_file_t bmf
;
1133 int mdfd
= open_dev(me
->devnum
);
1137 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
1142 /* Ok, we can try this one. Maybe it needs a bitmap */
1143 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1144 if (mddev
->devname
&& me
->path
1145 && devname_matches(mddev
->devname
, me
->path
))
1147 if (mddev
&& mddev
->bitmap_file
) {
1149 * Note: early kernels will wrongly fail this, so it
1153 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1154 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1156 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1163 fprintf(stderr
, Name
1164 ": Added bitmap %s to %s\n",
1165 mddev
->bitmap_file
, me
->path
);
1166 else if (errno
!= EEXIST
)
1167 fprintf(stderr
, Name
1168 ": Failed to add bitmap to %s: %s\n",
1169 me
->path
, strerror(errno
));
1172 sra
= sysfs_read(mdfd
, 0, 0);
1174 if (sysfs_set_str(sra
, NULL
,
1175 "array_state", "read-auto") == 0) {
1177 fprintf(stderr
, Name
1178 ": started array %s\n",
1179 me
->path
?: devnum2devname(me
->devnum
));
1181 fprintf(stderr
, Name
1182 ": failed to start array %s: %s\n",
1183 me
->path
?: devnum2devname(me
->devnum
),
1192 static char *container2devname(char *devname
)
1194 char *mdname
= NULL
;
1196 if (devname
[0] == '/') {
1197 int fd
= open(devname
, O_RDONLY
);
1199 mdname
= devnum2devname(fd2devnum(fd
));
1204 struct map_ent
*mp
, *map
= NULL
;
1206 if (!parse_uuid(devname
, uuid
))
1208 mp
= map_by_uuid(&map
, uuid
);
1210 mdname
= devnum2devname(mp
->devnum
);
1217 static int Incremental_container(struct supertype
*st
, char *devname
,
1218 char *homehost
, int verbose
,
1219 int runstop
, int autof
)
1221 /* Collect the contents of this container and for each
1222 * array, choose a device name and assemble the array.
1225 struct mdinfo
*list
;
1227 struct map_ent
*map
= NULL
;
1230 struct mddev_ident
*match
;
1233 memset(&info
, 0, sizeof(info
));
1234 st
->ss
->getinfo_super(st
, &info
, NULL
);
1236 if ((runstop
> 0 && info
.container_enough
>= 0) ||
1237 info
.container_enough
> 0)
1241 fprintf(stderr
, Name
": not enough devices to start the container\n");
1245 match
= search_mdstat(st
, &info
, devname
, verbose
, &rv
);
1246 if (match
== NULL
&& rv
== 2)
1249 /* Need to compute 'trustworthy' */
1251 trustworthy
= LOCAL
;
1252 else if (st
->ss
->match_home(st
, homehost
) == 1)
1253 trustworthy
= LOCAL
;
1254 else if (st
->ss
->match_home(st
, "any") == 1)
1255 trustworthy
= LOCAL
;
1257 trustworthy
= FOREIGN
;
1259 list
= st
->ss
->container_content(st
, NULL
);
1261 fprintf(stderr
, Name
": failed to get exclusive lock on "
1264 for (ra
= list
; ra
; ra
= ra
->next
) {
1266 char chosen_name
[1024];
1268 struct mddev_ident
*match
= NULL
;
1270 mp
= map_by_uuid(&map
, ra
->uuid
);
1273 mdfd
= open_dev(mp
->devnum
);
1275 strcpy(chosen_name
, mp
->path
);
1277 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1280 /* Check in mdadm.conf for container == devname and
1281 * member == ra->text_version after second slash.
1283 char *sub
= strchr(ra
->text_version
+1, '/');
1284 struct mddev_ident
*array_list
;
1287 array_list
= conf_get_ident(NULL
);
1290 for(; array_list
; array_list
= array_list
->next
) {
1292 if (array_list
->member
== NULL
||
1293 array_list
->container
== NULL
)
1295 if (strcmp(array_list
->member
, sub
) != 0)
1297 if (array_list
->uuid_set
&&
1298 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1300 dn
= container2devname(array_list
->container
);
1303 if (strncmp(dn
, ra
->text_version
+1,
1305 ra
->text_version
[strlen(dn
)+1] != '/') {
1310 /* we have a match */
1313 fprintf(stderr
, Name
": match found for member %s\n",
1314 array_list
->member
);
1318 if (match
&& match
->devname
&&
1319 strcasecmp(match
->devname
, "<ignore>") == 0) {
1321 fprintf(stderr
, Name
": array %s/%s is "
1322 "explicitly ignored by mdadm.conf\n",
1323 match
->container
, match
->member
);
1327 trustworthy
= LOCAL
;
1329 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1337 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1338 chosen_name
, strerror(errno
));
1342 assemble_container_content(st
, mdfd
, ra
, runstop
,
1343 chosen_name
, verbose
);
1350 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1351 * raid arrays, and if so first fail (if needed) and then remove the device.
1353 * @devname - The device we want to remove
1354 * @id_path - name as found in /dev/disk/by-path for this device
1356 * Note: the device name must be a kernel name like "sda", so
1357 * that we can find it in /proc/mdstat
1359 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1363 struct mdstat_ent
*ent
;
1364 struct mddev_dev devlist
;
1367 dprintf(Name
": incremental removal without --path <id_path> "
1368 "lacks the possibility to re-add new device in this "
1371 if (strchr(devname
, '/')) {
1372 fprintf(stderr
, Name
": incremental removal requires a "
1373 "kernel device name, not a file: %s\n", devname
);
1376 ent
= mdstat_by_component(devname
);
1378 fprintf(stderr
, Name
": %s does not appear to be a component "
1379 "of any array\n", devname
);
1382 mdfd
= open_dev(ent
->devnum
);
1384 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1388 memset(&devlist
, 0, sizeof(devlist
));
1389 devlist
.devname
= devname
;
1390 devlist
.disposition
= 'f';
1391 /* for a container, we must fail each member array */
1392 if (ent
->metadata_version
&&
1393 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1394 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1395 struct mdstat_ent
*memb
;
1396 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1397 if (is_container_member(memb
, ent
->dev
)) {
1398 int subfd
= open_dev(memb
->devnum
);
1400 Manage_subdevs(memb
->dev
, subfd
,
1401 &devlist
, verbose
, 0);
1405 free_mdstat(mdstat
);
1407 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
1408 devlist
.disposition
= 'r';
1409 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);