]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
bcbd78d0b4abe67ec2069922936be39447a609c5
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
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
,
38 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
39 struct supertype
*st
, int verbose
);
41 int Incremental(char *devname
, int verbose
, int runstop
,
42 struct supertype
*st
, char *homehost
, int require_homehost
,
45 /* Add this device to an array, creating the array if necessary
46 * and starting the array if sensible or - if runstop>0 - if possible.
48 * This has several steps:
50 * 1/ Check if device is permitted by mdadm.conf, reject if not.
51 * 2/ Find metadata, reject if none appropriate (check
52 * version/name from args)
53 * 3/ Check if there is a match in mdadm.conf
54 * 3a/ if not, check for homehost match. If no match, assemble as
56 * 4/ Determine device number.
57 * - If in mdadm.conf with std name, use that
58 * - UUID in /var/run/mdadm.map use that
59 * - If name is suggestive, use that. unless in use with different uuid.
60 * - Choose a free, high number.
61 * - Use a partitioned device unless strong suggestion not to.
63 * Don't choose partitioned for containers.
64 * 5/ Find out if array already exists
66 * - choose a name, from mdadm.conf or 'name' field in array.
70 * - check one drive in array to make sure metadata is a reasonably
71 * close match. Reject if not (e.g. different type)
73 * 6/ Make sure /var/run/mdadm.map contains this array.
74 * 7/ Is there enough devices to possibly start the array?
75 * For a container, this means running Incremental_container.
76 * 7a/ if not, finish with success.
78 * - read all metadata and arrange devices like -A does
79 * - if number of OK devices match expected, or -R and there are enough,
80 * start the array (auto-readonly).
83 struct mdinfo info
, dinfo
;
84 struct mddev_ident_s
*array_list
, *match
;
85 char chosen_name
[1024];
87 struct map_ent
*mp
, *map
= NULL
;
88 int dfd
= -1, mdfd
= -1;
91 int trustworthy
= FOREIGN
;
93 mdu_array_info_t ainf
;
94 struct dev_policy
*policy
= NULL
;
96 struct createinfo
*ci
= conf_get_create_info();
99 /* 1/ Check if device is permitted by mdadm.conf */
101 if (!conf_test_dev(devname
)) {
104 ": %s not permitted by mdadm.conf.\n",
109 /* 2/ Find metadata, reject if none appropriate (check
110 * version/name from args) */
112 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
115 fprintf(stderr
, Name
": cannot open %s: %s.\n",
116 devname
, strerror(errno
));
119 if (fstat(dfd
, &stb
) < 0) {
121 fprintf(stderr
, Name
": fstat failed for %s: %s.\n",
122 devname
, strerror(errno
));
125 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
127 fprintf(stderr
, Name
": %s is not a block device.\n",
132 dinfo
.disk
.major
= major(stb
.st_rdev
);
133 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
135 policy
= disk_policy(&dinfo
);
137 if (st
== NULL
&& (st
= guess_super(dfd
)) == NULL
) {
140 ": no recognisable superblock on %s.\n",
142 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
145 if (st
->ss
->compare_super
== NULL
||
146 st
->ss
->load_super(st
, dfd
, NULL
)) {
148 fprintf(stderr
, Name
": no RAID superblock on %s.\n",
150 rv
= try_spare(devname
, &dfd
, policy
, st
, verbose
);
154 close (dfd
); dfd
= -1;
156 memset(&info
, 0, sizeof(info
));
157 st
->ss
->getinfo_super(st
, &info
);
158 /* 3/ Check if there is a match in mdadm.conf */
160 array_list
= conf_get_ident(NULL
);
162 for (; array_list
; array_list
= array_list
->next
) {
163 if (array_list
->uuid_set
&&
164 same_uuid(array_list
->uuid
, info
.uuid
, st
->ss
->swapuuid
)
166 if (verbose
>= 2 && array_list
->devname
)
168 ": UUID differs from %s.\n",
169 array_list
->devname
);
172 if (array_list
->name
[0] &&
173 strcasecmp(array_list
->name
, info
.name
) != 0) {
174 if (verbose
>= 2 && array_list
->devname
)
176 ": Name differs from %s.\n",
177 array_list
->devname
);
180 if (array_list
->devices
&&
181 !match_oneof(array_list
->devices
, devname
)) {
182 if (verbose
>= 2 && array_list
->devname
)
184 ": Not a listed device for %s.\n",
185 array_list
->devname
);
188 if (array_list
->super_minor
!= UnSet
&&
189 array_list
->super_minor
!= info
.array
.md_minor
) {
190 if (verbose
>= 2 && array_list
->devname
)
192 ": Different super-minor to %s.\n",
193 array_list
->devname
);
196 if (!array_list
->uuid_set
&&
197 !array_list
->name
[0] &&
198 !array_list
->devices
&&
199 array_list
->super_minor
== UnSet
) {
200 if (verbose
>= 2 && array_list
->devname
)
202 ": %s doesn't have any identifying information.\n",
203 array_list
->devname
);
206 /* FIXME, should I check raid_disks and level too?? */
210 if (match
->devname
&& array_list
->devname
)
212 ": we match both %s and %s - cannot decide which to use.\n",
213 match
->devname
, array_list
->devname
);
216 ": multiple lines in mdadm.conf match\n");
224 if (match
&& match
->devname
225 && strcasecmp(match
->devname
, "<ignore>") == 0) {
227 fprintf(stderr
, Name
": array containing %s is explicitly"
228 " ignored by mdadm.conf\n",
233 /* 3a/ if not, check for homehost match. If no match, continue
234 * but don't trust the 'name' in the array. Thus a 'random' minor
235 * number will be assigned, and the device name will be based
239 else if (st
->ss
->match_home(st
, homehost
) == 1)
241 else if (st
->ss
->match_home(st
, "any") == 1)
242 trustworthy
= LOCAL_ANY
;
244 trustworthy
= FOREIGN
;
247 if (!match
&& !conf_test_metadata(st
->ss
->name
,
248 (trustworthy
== LOCAL
))) {
251 ": %s has metadata type %s for which "
252 "auto-assembly is disabled\n",
253 devname
, st
->ss
->name
);
256 if (trustworthy
== LOCAL_ANY
)
259 /* There are three possible sources for 'autof': command line,
260 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
261 * ARRAY takes precedence, then command line, then
264 if (match
&& match
->autof
)
265 autof
= match
->autof
;
269 if (st
->ss
->container_content
&& st
->loaded_container
) {
270 if ((runstop
> 0 && info
.container_enough
>= 0) ||
271 info
.container_enough
> 0)
275 fprintf(stderr
, Name
": not enough devices to start the container\n");
280 /* This is a pre-built container array, so we do something
283 rv
= Incremental_container(st
, devname
, verbose
, runstop
,
288 name_to_use
= info
.name
;
289 if (name_to_use
[0] == 0 &&
290 info
.array
.level
== LEVEL_CONTAINER
&&
291 trustworthy
== LOCAL
) {
292 name_to_use
= info
.text_version
;
293 trustworthy
= METADATA
;
295 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
296 ! require_homehost
&&
297 conf_name_is_free(name_to_use
))
300 /* strip "hostname:" prefix from name if we have decided
301 * to treat it as LOCAL
303 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
304 name_to_use
= strchr(name_to_use
, ':')+1;
306 /* 4/ Check if array exists.
309 fprintf(stderr
, Name
": failed to get exclusive lock on "
311 mp
= map_by_uuid(&map
, info
.uuid
);
313 mdfd
= open_dev(mp
->devnum
);
320 /* Couldn't find an existing array, maybe make a new one */
321 mdfd
= create_mddev(match
? match
->devname
: NULL
,
322 name_to_use
, autof
, trustworthy
, chosen_name
);
327 sysfs_init(&info
, mdfd
, 0);
329 if (set_array_info(mdfd
, st
, &info
) != 0) {
330 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
331 chosen_name
, strerror(errno
));
337 dinfo
.disk
.major
= major(stb
.st_rdev
);
338 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
339 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
340 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
341 devname
, chosen_name
, strerror(errno
));
342 ioctl(mdfd
, STOP_ARRAY
, 0);
346 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), GET_DEVS
);
347 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
348 /* It really should be 'none' - must be old buggy
349 * kernel, and mdadm -I may not be able to complete.
352 ioctl(mdfd
, STOP_ARRAY
, NULL
);
354 ": You have an old buggy kernel which cannot support\n"
355 " --incremental reliably. Aborting.\n");
360 info
.array
.working_disks
= 1;
362 /* 6/ Make sure /var/run/mdadm.map contains this array. */
363 map_update(&map
, fd2devnum(mdfd
),
365 info
.uuid
, chosen_name
);
368 /* - check one drive in array to make sure metadata is a reasonably */
369 /* close match. Reject if not (e.g. different type) */
370 /* - add the device */
375 struct supertype
*st2
;
376 struct mdinfo info2
, *d
;
379 strcpy(chosen_name
, mp
->path
);
381 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
383 /* It is generally not OK to add non-spare drives to a
384 * running array as they are probably missing because
385 * they failed. However if runstop is 1, then the
386 * array was possibly started early and our best bet is
387 * to add this anyway.
388 * Also if action policy is re-add or better we allow
391 if ((info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
392 && ! policy_action_allows(policy
, st
->ss
->name
,
397 if (st
->ss
->external
) {
398 char *devname
= devnum2devname(fd2devnum(mdfd
));
400 active
= devname
&& is_container_active(devname
);
402 } else if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0)
406 ": not adding %s to active array (without --run) %s\n",
407 devname
, chosen_name
);
412 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
), (GET_DEVS
| GET_STATE
));
418 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
419 sra
->devs
->disk
.minor
);
420 dfd2
= dev_open(dn
, O_RDONLY
);
422 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
423 st
->ss
->compare_super(st
, st2
) != 0) {
425 ": metadata mismatch between %s and "
427 devname
, chosen_name
);
433 memset(&info2
, 0, sizeof(info2
));
434 st2
->ss
->getinfo_super(st2
, &info2
);
435 st2
->ss
->free_super(st2
);
436 if (info
.array
.level
!= info2
.array
.level
||
437 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
438 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
440 ": unexpected difference between %s and %s.\n",
441 chosen_name
, devname
);
446 info2
.disk
.major
= major(stb
.st_rdev
);
447 info2
.disk
.minor
= minor(stb
.st_rdev
);
448 /* add disk needs to know about containers */
449 if (st
->ss
->external
)
450 sra
->array
.level
= LEVEL_CONTAINER
;
451 err
= add_disk(mdfd
, st
, sra
, &info2
);
452 if (err
< 0 && errno
== EBUSY
) {
453 /* could be another device present with the same
454 * disk.number. Find and reject any such
456 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
457 info
.events
, verbose
, chosen_name
);
458 err
= add_disk(mdfd
, st
, sra
, &info2
);
461 fprintf(stderr
, Name
": failed to add %s to %s: %s.\n",
462 devname
, chosen_name
, strerror(errno
));
466 info
.array
.working_disks
= 0;
467 for (d
= sra
->devs
; d
; d
=d
->next
)
468 info
.array
.working_disks
++;
472 /* 7/ Is there enough devices to possibly start the array? */
473 /* 7a/ if not, finish with success. */
474 if (info
.array
.level
== LEVEL_CONTAINER
) {
475 /* Try to assemble within the container */
477 sysfs_uevent(&info
, "change");
480 ": container %s now has %d devices\n",
481 chosen_name
, info
.array
.working_disks
);
482 wait_for(chosen_name
, mdfd
);
484 rv
= Incremental(chosen_name
, verbose
, runstop
,
485 NULL
, homehost
, require_homehost
, autof
);
487 /* Don't fail the whole -I if a subarray didn't
488 * have enough devices to start yet
494 active_disks
= count_active(st
, mdfd
, &avail
, &info
);
495 if (enough(info
.array
.level
, info
.array
.raid_disks
,
496 info
.array
.layout
, info
.array
.state
& 1,
497 avail
, active_disks
) == 0) {
501 ": %s attached to %s, not enough to start (%d).\n",
502 devname
, chosen_name
, active_disks
);
510 /* - if number of OK devices match expected, or -R and there */
512 /* + add any bitmap file */
513 /* + start the array (auto-readonly). */
515 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ainf
) == 0) {
518 ": %s attached to %s which is already active.\n",
519 devname
, chosen_name
);
526 if (runstop
> 0 || active_disks
>= info
.array
.working_disks
) {
527 struct mdinfo
*sra
, *dsk
;
528 /* Let's try to start it */
529 if (match
&& match
->bitmap_file
) {
530 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
533 ": Could not open bitmap file %s.\n",
537 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
540 ": Failed to set bitmapfile for %s.\n",
546 /* GET_* needed so add_disk works below */
547 sra
= sysfs_read(mdfd
, fd2devnum(mdfd
),
548 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
);
549 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
550 && trustworthy
!= FOREIGN
)
551 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
553 rv
= sysfs_set_str(sra
, NULL
,
554 "array_state", "read-auto");
558 ": %s attached to %s, which has been started.\n",
559 devname
, chosen_name
);
561 wait_for(chosen_name
, mdfd
);
562 /* We just started the array, so some devices
563 * might have been evicted from the array
564 * because their event counts were too old.
565 * If the action=re-add policy is in-force for
566 * those devices we should re-add them now.
568 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
569 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
570 add_disk(mdfd
, st
, sra
, dsk
) == 0)
572 ": %s re-added to %s\n",
573 dsk
->sys_name
, chosen_name
);
577 ": %s attached to %s, but failed to start: %s.\n",
578 devname
, chosen_name
, strerror(errno
));
584 ": %s attached to %s, not enough to start safely.\n",
585 devname
, chosen_name
);
594 dev_policy_free(policy
);
598 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
599 int number
, __u64 events
, int verbose
,
602 /* Find a device attached to this array with a disk.number of number
603 * and events less than the passed events, and remove the device.
608 if (ioctl(mdfd
, GET_ARRAY_INFO
, &ra
) == 0)
609 return; /* not safe to remove from active arrays
610 * without thinking more */
612 for (d
= sra
->devs
; d
; d
= d
->next
) {
616 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
617 dfd
= dev_open(dn
, O_RDONLY
);
620 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
624 st
->ss
->getinfo_super(st
, &info
);
625 st
->ss
->free_super(st
);
628 if (info
.disk
.number
!= number
||
629 info
.events
>= events
)
632 if (d
->disk
.raid_disk
> -1)
633 sysfs_set_str(sra
, d
, "slot", "none");
634 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
637 ": removing old device %s from %s\n",
638 d
->sys_name
+4, array_name
);
642 static int count_active(struct supertype
*st
, int mdfd
, char **availp
,
643 struct mdinfo
*bestinfo
)
645 /* count how many devices in sra think they are active */
647 int cnt
= 0, cnt1
= 0;
648 __u64 max_events
= 0;
649 struct mdinfo
*sra
= sysfs_read(mdfd
, -1, GET_DEVS
| GET_STATE
);
655 for (d
= sra
->devs
; d
; d
= d
->next
) {
661 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
662 dfd
= dev_open(dn
, O_RDONLY
);
665 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
669 st
->ss
->getinfo_super(st
, &info
);
671 avail
= malloc(info
.array
.raid_disks
);
673 fprintf(stderr
, Name
": out of memory.\n");
676 memset(avail
, 0, info
.array
.raid_disks
);
680 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
684 max_events
= info
.events
;
685 avail
[info
.disk
.raid_disk
] = 2;
686 st
->ss
->getinfo_super(st
, bestinfo
);
687 } else if (info
.events
== max_events
) {
689 avail
[info
.disk
.raid_disk
] = 2;
690 } else if (info
.events
== max_events
-1) {
692 avail
[info
.disk
.raid_disk
] = 1;
693 } else if (info
.events
< max_events
- 1)
695 else if (info
.events
== max_events
+1) {
699 max_events
= info
.events
;
700 for (i
=0; i
<info
.array
.raid_disks
; i
++)
703 avail
[info
.disk
.raid_disk
] = 2;
704 st
->ss
->getinfo_super(st
, bestinfo
);
705 } else { /* info.events much bigger */
707 memset(avail
, 0, info
.disk
.raid_disk
);
708 max_events
= info
.events
;
709 st
->ss
->getinfo_super(st
, bestinfo
);
712 st
->ss
->free_super(st
);
717 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
718 struct supertype
*st
, int verbose
)
720 /* This device doesn't have any md metadata
721 * If it is 'bare' and theh device policy allows 'spare' look for
722 * an array or container to attach it to.
723 * If st is set, then only arrays of that type are considered
724 * Return 0 on success, or some exit code on failure, probably 1.
727 char bufpad
[4096 + 4096];
728 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
730 struct map_ent
*mp
, *map
= NULL
;
731 struct mdinfo
*chosen
= NULL
;
734 /* First check policy */
735 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
738 if (fstat(dfd
, &stb
) != 0)
740 /* Now check if the device is bare - we don't add non-bare devices
741 * yet even if action=-spare
744 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
745 read(dfd
, buf
, 4096) != 4096) {
748 fprintf(stderr
, Name
": %s is not bare, so not considering as a spare\n",
752 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
754 if (memcmp(buf
, buf
+1, 4095) != 0)
757 /* OK, first 4K appear blank, try the end. */
758 if (lseek(dfd
, -4096, SEEK_END
) < 0 ||
759 read(dfd
, buf
, 4096) != 4096)
762 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
764 if (memcmp(buf
, buf
+1, 4095) != 0)
767 /* This device passes our test for 'is bare'.
768 * Now we need to find a suitable array to add this to.
769 * We only accept arrays that:
771 * - are in the same domains as the device
772 * - are of an size for which the device will be useful
773 * and we choose the one that is the most degraded
776 if (map_lock(&map
)) {
777 fprintf(stderr
, Name
": failed to get exclusive lock on "
781 for (mp
= map
; mp
; mp
= mp
->next
) {
782 struct supertype
*st2
;
783 struct domainlist
*dl
= NULL
;
785 unsigned long long devsize
;
787 if (is_subarray(mp
->metadata
))
790 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
792 (st
->minor_version
>= 0 &&
793 st
->minor_version
!= st2
->minor_version
)) {
795 fprintf(stderr
, Name
": not adding %s to %s as metadata type doesn't match\n",
802 sra
= sysfs_read(-1, mp
->devnum
,
803 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
804 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
806 /* Probably a container - no degraded info */
807 sra
= sysfs_read(-1, mp
->devnum
,
808 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
809 GET_COMPONENT
|GET_VERSION
);
811 sra
->array
.failed_disks
= 0;
818 for(i
=0; !st2
&& superlist
[i
]; i
++)
819 st2
= superlist
[i
]->match_metadata_desc(
823 get_dev_size(dfd
, NULL
, &devsize
);
824 if (st2
->ss
->avail_size(st2
, devsize
) < sra
->component_size
) {
826 fprintf(stderr
, Name
": not adding %s to %s as it is too small\n",
830 dl
= domain_from_array(sra
, st2
->ss
->name
);
831 if (!domain_test(dl
, pol
, st2
->ss
->name
)) {
832 /* domain test fails */
834 fprintf(stderr
, Name
": not adding %s to %s as it is not in a compatible domain\n",
839 /* all tests passed, OK to add to this array */
843 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
857 /* add current device to chosen array as a spare */
858 int mdfd
= open_dev(devname2devnum(chosen
->sys_name
));
860 struct mddev_dev_s devlist
;
865 devlist
.writemostly
= 0;
866 devlist
.devname
= devname
;
867 sprintf(devname
, "%d:%d", major(stb
.st_rdev
),
869 devlist
.disposition
= 'a';
872 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
878 fprintf(stderr
, Name
": added %s as spare for %s\n",
879 devname
, chosen
->sys_name
);
881 fprintf(stderr
, Name
": failed to add %s as spare for %s\n",
882 devname
, chosen
->sys_name
);
889 int IncrementalScan(int verbose
)
891 /* look at every device listed in the 'map' file.
892 * If one is found that is not running then:
893 * look in mdadm.conf for bitmap file.
894 * if one exists, but array has none, add it.
895 * try to start array in auto-readonly mode
897 struct map_ent
*mapl
= NULL
;
899 mddev_ident_t devs
, mddev
;
903 devs
= conf_get_ident(NULL
);
905 for (me
= mapl
; me
; me
= me
->next
) {
906 mdu_array_info_t array
;
907 mdu_bitmap_file_t bmf
;
909 int mdfd
= open_dev(me
->devnum
);
913 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
) == 0 ||
918 /* Ok, we can try this one. Maybe it needs a bitmap */
919 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
920 if (mddev
->devname
&& me
->path
921 && devname_matches(mddev
->devname
, me
->path
))
923 if (mddev
&& mddev
->bitmap_file
) {
925 * Note: early kernels will wrongly fail this, so it
929 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
930 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
932 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
940 ": Added bitmap %s to %s\n",
941 mddev
->bitmap_file
, me
->path
);
942 else if (errno
!= EEXIST
)
944 ": Failed to add bitmap to %s: %s\n",
945 me
->path
, strerror(errno
));
948 sra
= sysfs_read(mdfd
, 0, 0);
950 if (sysfs_set_str(sra
, NULL
,
951 "array_state", "read-auto") == 0) {
954 ": started array %s\n",
955 me
->path
?: devnum2devname(me
->devnum
));
958 ": failed to start array %s: %s\n",
959 me
->path
?: devnum2devname(me
->devnum
),
968 static char *container2devname(char *devname
)
972 if (devname
[0] == '/') {
973 int fd
= open(devname
, O_RDONLY
);
975 mdname
= devnum2devname(fd2devnum(fd
));
980 struct map_ent
*mp
, *map
= NULL
;
982 if (!parse_uuid(devname
, uuid
))
984 mp
= map_by_uuid(&map
, uuid
);
986 mdname
= devnum2devname(mp
->devnum
);
993 int Incremental_container(struct supertype
*st
, char *devname
, int verbose
,
994 int runstop
, int autof
, int trustworthy
)
996 /* Collect the contents of this container and for each
997 * array, choose a device name and assemble the array.
1000 struct mdinfo
*list
= st
->ss
->container_content(st
);
1002 struct map_ent
*map
= NULL
;
1005 fprintf(stderr
, Name
": failed to get exclusive lock on "
1008 for (ra
= list
; ra
; ra
= ra
->next
) {
1010 char chosen_name
[1024];
1012 struct mddev_ident_s
*match
= NULL
;
1014 mp
= map_by_uuid(&map
, ra
->uuid
);
1017 mdfd
= open_dev(mp
->devnum
);
1019 strcpy(chosen_name
, mp
->path
);
1021 strcpy(chosen_name
, devnum2devname(mp
->devnum
));
1024 /* Check in mdadm.conf for container == devname and
1025 * member == ra->text_version after second slash.
1027 char *sub
= strchr(ra
->text_version
+1, '/');
1028 struct mddev_ident_s
*array_list
;
1031 array_list
= conf_get_ident(NULL
);
1034 for(; array_list
; array_list
= array_list
->next
) {
1036 if (array_list
->member
== NULL
||
1037 array_list
->container
== NULL
)
1039 if (strcmp(array_list
->member
, sub
) != 0)
1041 if (array_list
->uuid_set
&&
1042 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1044 dn
= container2devname(array_list
->container
);
1047 if (strncmp(dn
, ra
->text_version
+1,
1049 ra
->text_version
[strlen(dn
)+1] != '/') {
1054 /* we have a match */
1057 fprintf(stderr
, Name
": match found for member %s\n",
1058 array_list
->member
);
1062 if (match
&& match
->devname
&&
1063 strcasecmp(match
->devname
, "<ignore>") == 0) {
1065 fprintf(stderr
, Name
": array %s/%s is "
1066 "explicitly ignored by mdadm.conf\n",
1067 match
->container
, match
->member
);
1071 trustworthy
= LOCAL
;
1073 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1081 fprintf(stderr
, Name
": failed to open %s: %s.\n",
1082 chosen_name
, strerror(errno
));
1086 assemble_container_content(st
, mdfd
, ra
, runstop
,
1087 chosen_name
, verbose
);
1094 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1095 * raid arrays, and if so first fail (if needed) and then remove the device.
1097 * @devname - The device we want to remove
1099 * Note: the device name must be a kernel name like "sda", so
1100 * that we can find it in /proc/mdstat
1102 int IncrementalRemove(char *devname
, int verbose
)
1106 struct mdstat_ent
*ent
;
1107 struct mddev_dev_s devlist
;
1109 if (strchr(devname
, '/')) {
1110 fprintf(stderr
, Name
": incremental removal requires a "
1111 "kernel device name, not a file: %s\n", devname
);
1114 ent
= mdstat_by_component(devname
);
1116 fprintf(stderr
, Name
": %s does not appear to be a component "
1117 "of any array\n", devname
);
1120 mdfd
= open_dev(ent
->devnum
);
1122 fprintf(stderr
, Name
": Cannot open array %s!!\n", ent
->dev
);
1125 memset(&devlist
, 0, sizeof(devlist
));
1126 devlist
.devname
= devname
;
1127 devlist
.disposition
= 'f';
1128 Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);
1129 devlist
.disposition
= 'r';
1130 rv
= Manage_subdevs(ent
->dev
, mdfd
, &devlist
, verbose
, 0);