2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2013 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
36 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
37 int mdfd
, char **availp
,
39 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
40 int number
, __u64 events
, int verbose
,
42 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
43 struct map_ent
*target
,
44 struct supertype
*st
, int verbose
);
46 static int Incremental_container(struct supertype
*st
, char *devname
,
47 struct context
*c
, char *only
);
49 int Incremental(struct mddev_dev
*devlist
, struct context
*c
,
52 /* Add this device to an array, creating the array if necessary
53 * and starting the array if sensible or - if runstop>0 - if possible.
55 * This has several steps:
57 * 1/ Check if device is permitted by mdadm.conf, reject if not.
58 * 2/ Find metadata, reject if none appropriate (check
59 * version/name from args)
60 * 3/ Check if there is a match in mdadm.conf
61 * 3a/ if not, check for homehost match. If no match, assemble as
63 * 4/ Determine device number.
64 * - If in mdadm.conf with std name, use that
65 * - UUID in /var/run/mdadm.map use that
66 * - If name is suggestive, use that. unless in use with different uuid.
67 * - Choose a free, high number.
68 * - Use a partitioned device unless strong suggestion not to.
70 * Don't choose partitioned for containers.
71 * 5/ Find out if array already exists
73 * - choose a name, from mdadm.conf or 'name' field in array.
77 * - check one drive in array to make sure metadata is a reasonably
78 * close match. Reject if not (e.g. different type)
80 * 6/ Make sure /var/run/mdadm.map contains this array.
81 * 7/ Is there enough devices to possibly start the array?
82 * For a container, this means running Incremental_container.
83 * 7a/ if not, finish with success.
85 * - read all metadata and arrange devices like -A does
86 * - if number of OK devices match expected, or -R and there are enough,
87 * start the array (auto-readonly).
90 struct mdinfo info
, dinfo
;
91 struct mdinfo
*sra
= NULL
, *d
;
92 struct mddev_ident
*match
;
93 char chosen_name
[1024];
96 struct map_ent
*mp
, *map
= NULL
;
97 int dfd
= -1, mdfd
= -1;
102 mdu_array_info_t ainf
;
103 struct dev_policy
*policy
= NULL
;
104 struct map_ent target_array
;
106 char *devname
= devlist
->devname
;
107 int journal_device_missing
= 0;
109 struct createinfo
*ci
= conf_get_create_info();
111 if (stat(devname
, &stb
) < 0) {
113 pr_err("stat failed for %s: %s.\n",
114 devname
, strerror(errno
));
117 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
119 pr_err("%s is not a block device.\n",
123 dfd
= dev_open(devname
, O_RDONLY
);
126 pr_err("cannot open %s: %s.\n",
127 devname
, strerror(errno
));
130 /* If the device is a container, we do something very different */
131 if (must_be_container(dfd
)) {
133 st
= super_by_fd(dfd
, NULL
);
134 if (st
&& st
->ss
->load_container
)
135 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
138 if (!rv
&& st
->ss
->container_content
) {
140 pr_err("failed to get exclusive lock on mapfile\n");
142 printf("MD_DEVNAME=%s\n", devname
);
143 rv
= Incremental_container(st
, devname
, c
, NULL
);
148 pr_err("%s is not part of an md array.\n",
153 /* 1/ Check if device is permitted by mdadm.conf */
155 for (;devlist
; devlist
= devlist
->next
)
156 if (conf_test_dev(devlist
->devname
))
159 devlist
= conf_get_devs();
160 for (;devlist
; devlist
= devlist
->next
) {
162 if (stat(devlist
->devname
, &st2
) == 0 &&
163 (st2
.st_mode
& S_IFMT
) == S_IFBLK
&&
164 st2
.st_rdev
== stb
.st_rdev
)
170 pr_err("%s not permitted by mdadm.conf.\n",
175 /* 2/ Find metadata, reject if none appropriate (check
176 * version/name from args) */
178 if (fstat(dfd
, &stb
) < 0) {
180 pr_err("fstat failed for %s: %s.\n",
181 devname
, strerror(errno
));
184 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
186 pr_err("%s is not a block device.\n",
191 dinfo
.disk
.major
= major(stb
.st_rdev
);
192 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
194 policy
= disk_policy(&dinfo
);
195 have_target
= policy_check_path(&dinfo
, &target_array
);
197 if (st
== NULL
&& (st
= guess_super_type(dfd
, guess_array
)) == NULL
) {
199 pr_err("no recognisable superblock on %s.\n",
201 rv
= try_spare(devname
, &dfd
, policy
,
202 have_target
? &target_array
: NULL
,
206 st
->ignore_hw_compat
= 0;
208 if (st
->ss
->compare_super
== NULL
||
209 st
->ss
->load_super(st
, dfd
, c
->verbose
>= 0 ? devname
: NULL
)) {
211 pr_err("no RAID superblock on %s.\n",
213 rv
= try_spare(devname
, &dfd
, policy
,
214 have_target
? &target_array
: NULL
,
219 close (dfd
); dfd
= -1;
221 st
->ss
->getinfo_super(st
, &info
, NULL
);
223 /* 3/ Check if there is a match in mdadm.conf */
224 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
225 if (!match
&& rv
== 2)
228 if (match
&& match
->devname
229 && strcasecmp(match
->devname
, "<ignore>") == 0) {
231 pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
236 /* 3a/ if not, check for homehost match. If no match, continue
237 * but don't trust the 'name' in the array. Thus a 'random' minor
238 * number will be assigned, and the device name will be based
242 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
244 else if (st
->ss
->match_home(st
, "any") == 1)
245 trustworthy
= LOCAL_ANY
;
247 trustworthy
= FOREIGN
;
249 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
250 (trustworthy
== LOCAL
))) {
252 pr_err("%s has metadata type %s for which 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 c
->autof
= match
->autof
;
267 c
->autof
= ci
->autof
;
269 name_to_use
= info
.name
;
270 if (name_to_use
[0] == 0 &&
271 info
.array
.level
== LEVEL_CONTAINER
) {
272 name_to_use
= info
.text_version
;
273 trustworthy
= METADATA
;
275 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
276 ! c
->require_homehost
&&
277 conf_name_is_free(name_to_use
))
280 /* strip "hostname:" prefix from name if we have decided
281 * to treat it as LOCAL
283 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
284 name_to_use
= strchr(name_to_use
, ':')+1;
286 /* 4/ Check if array exists.
289 pr_err("failed to get exclusive lock on mapfile\n");
290 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
293 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
296 pr_err("cannot reopen %s: %s.\n",
297 devname
, strerror(errno
));
300 /* Cannot hold it open while we add the device to the array,
301 * so we must release the O_EXCL and depend on the map_lock()
302 * So now is the best time to remove any partitions.
304 remove_partitions(dfd
);
308 mp
= map_by_uuid(&map
, info
.uuid
);
310 mdfd
= open_dev(mp
->devnm
);
316 /* Skip the clustered ones. This should be started by
317 * clustering resource agents
319 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
322 /* Couldn't find an existing array, maybe make a new one */
323 mdfd
= create_mddev(match
? match
->devname
: NULL
,
324 name_to_use
, c
->autof
, trustworthy
, chosen_name
);
329 sysfs_init(&info
, mdfd
, NULL
);
331 if (set_array_info(mdfd
, st
, &info
) != 0) {
332 pr_err("failed to set array info for %s: %s\n",
333 chosen_name
, strerror(errno
));
339 dinfo
.disk
.major
= major(stb
.st_rdev
);
340 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
341 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
342 pr_err("failed to add %s to new array %s: %s.\n",
343 devname
, chosen_name
, strerror(errno
));
344 ioctl(mdfd
, STOP_ARRAY
, 0);
348 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
349 GET_OFFSET
| GET_SIZE
));
351 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
352 /* It really should be 'none' - must be old buggy
353 * kernel, and mdadm -I may not be able to complete.
356 ioctl(mdfd
, STOP_ARRAY
, NULL
);
357 pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n");
361 info
.array
.working_disks
= 1;
362 /* 6/ Make sure /var/run/mdadm.map contains this array. */
363 map_update(&map
, fd2devnm(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 */
374 struct supertype
*st2
;
375 struct mdinfo info2
, *d
;
377 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
378 GET_OFFSET
| GET_SIZE
));
381 strcpy(chosen_name
, mp
->path
);
383 strcpy(chosen_name
, mp
->devnm
);
385 /* It is generally not OK to add non-spare drives to a
386 * running array as they are probably missing because
387 * they failed. However if runstop is 1, then the
388 * array was possibly started early and our best bet is
389 * to add this anyway.
390 * Also if action policy is re-add or better we allow
392 * This doesn't apply to containers as the 'non-spare'
393 * flag has a different meaning. The test has to happen
394 * at the device level there
396 if (!st
->ss
->external
397 && (info
.disk
.state
& (1<<MD_DISK_SYNC
)) != 0
398 && ! policy_action_allows(policy
, st
->ss
->name
,
401 if (md_get_array_info(mdfd
, &ainf
) == 0) {
402 pr_err("not adding %s to active array (without --run) %s\n",
403 devname
, chosen_name
);
413 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
414 sra
->devs
->disk
.minor
);
415 dfd2
= dev_open(dn
, O_RDONLY
);
417 pr_err("unable to open %s\n", devname
);
422 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
423 st
->ss
->compare_super(st
, st2
) != 0) {
424 pr_err("metadata mismatch between %s and chosen array %s\n",
425 devname
, chosen_name
);
431 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
432 st2
->ss
->free_super(st2
);
433 if (info
.array
.level
!= info2
.array
.level
||
434 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
435 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
436 pr_err("unexpected difference between %s and %s.\n",
437 chosen_name
, devname
);
442 info
.disk
.major
= major(stb
.st_rdev
);
443 info
.disk
.minor
= minor(stb
.st_rdev
);
444 /* add disk needs to know about containers */
445 if (st
->ss
->external
)
446 sra
->array
.level
= LEVEL_CONTAINER
;
448 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
449 info
.disk
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
451 err
= add_disk(mdfd
, st
, sra
, &info
);
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
, c
->verbose
, chosen_name
);
458 err
= add_disk(mdfd
, st
, sra
, &info
);
460 if (err
< 0 && errno
== EINVAL
&&
461 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
462 /* Maybe it needs to be added as a spare */
463 if (policy_action_allows(policy
, st
->ss
->name
,
465 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
466 err
= add_disk(mdfd
, st
, sra
, &info
);
469 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
470 devname
, chosen_name
);
473 pr_err("failed to add %s to existing array %s: %s.\n",
474 devname
, chosen_name
, strerror(errno
));
478 info
.array
.working_disks
= 0;
479 for (d
= sra
->devs
; d
; d
=d
->next
)
480 info
.array
.working_disks
++;
483 if (strncmp(chosen_name
, "/dev/md/", 8) == 0)
484 md_devname
= chosen_name
+8;
486 md_devname
= chosen_name
;
488 printf("MD_DEVICE=%s\n", fd2devnm(mdfd
));
489 printf("MD_DEVNAME=%s\n", md_devname
);
490 printf("MD_FOREIGN=%s\n", trustworthy
== FOREIGN
? "yes" : "no");
493 /* 7/ Is there enough devices to possibly start the array? */
494 /* 7a/ if not, finish with success. */
495 if (info
.array
.level
== LEVEL_CONTAINER
) {
497 /* Try to assemble within the container */
498 sysfs_uevent(sra
, "change");
499 if (!c
->export
&& c
->verbose
>= 0)
500 pr_err("container %s now has %d device%s\n",
501 chosen_name
, info
.array
.working_disks
,
502 info
.array
.working_disks
== 1?"":"s");
503 wait_for(chosen_name
, mdfd
);
504 if (st
->ss
->external
)
505 strcpy(devnm
, fd2devnm(mdfd
));
506 if (st
->ss
->load_container
)
507 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
511 rv
= Incremental_container(st
, chosen_name
, c
, NULL
);
513 /* after spare is added, ping monitor for external metadata
514 * so that it can eg. try to rebuild degraded array */
515 if (st
->ss
->external
)
520 /* We have added something to the array, so need to re-read the
521 * state. Eventually this state should be kept up-to-date as
525 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
526 GET_OFFSET
| GET_SIZE
));
527 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
529 journal_device_missing
= (info
.journal_device_required
) && (info
.journal_clean
== 0);
531 if (info
.consistency_policy
== CONSISTENCY_POLICY_PPL
)
532 info
.array
.state
|= 1;
534 if (enough(info
.array
.level
, info
.array
.raid_disks
,
535 info
.array
.layout
, info
.array
.state
& 1,
538 printf("MD_STARTED=no\n");
539 } else if (c
->verbose
>= 0)
540 pr_err("%s attached to %s, not enough to start (%d).\n",
541 devname
, chosen_name
, active_disks
);
547 /* - if number of OK devices match expected, or -R and there */
549 /* + add any bitmap file */
550 /* + start the array (auto-readonly). */
552 if (md_get_array_info(mdfd
, &ainf
) == 0) {
554 printf("MD_STARTED=already\n");
555 } else if (c
->verbose
>= 0)
556 pr_err("%s attached to %s which is already active.\n",
557 devname
, chosen_name
);
563 if (c
->runstop
> 0 || (!journal_device_missing
&& active_disks
>= info
.array
.working_disks
)) {
565 /* Let's try to start it */
567 if (journal_device_missing
)
568 pr_err("Trying to run with missing journal device\n");
569 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
570 pr_err("%s: This array is being reshaped and cannot be started\n",
572 cont_err("by --incremental. Please use --assemble\n");
575 if (match
&& match
->bitmap_file
) {
576 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
578 pr_err("Could not open bitmap file %s.\n",
582 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
584 pr_err("Failed to set bitmapfile for %s.\n",
590 /* Need to remove from the array any devices which
591 * 'count_active' discerned were too old or inappropriate
593 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
594 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
595 remove_disk(mdfd
, st
, sra
, d
);
597 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
)
598 && trustworthy
!= FOREIGN
)
599 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
601 rv
= sysfs_set_str(sra
, NULL
,
602 "array_state", "read-auto");
603 /* Array might be O_EXCL which will interfere with
604 * fsck and mount. So re-open without O_EXCL.
609 printf("MD_STARTED=yes\n");
610 } else if (c
->verbose
>= 0)
611 pr_err("%s attached to %s, which has been started.\n",
612 devname
, chosen_name
);
614 wait_for(chosen_name
, mdfd
);
615 /* We just started the array, so some devices
616 * might have been evicted from the array
617 * because their event counts were too old.
618 * If the action=re-add policy is in-force for
619 * those devices we should re-add them now.
621 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
622 if (disk_action_allows(dsk
, st
->ss
->name
, act_re_add
) &&
623 add_disk(mdfd
, st
, sra
, dsk
) == 0)
624 pr_err("%s re-added to %s\n",
625 dsk
->sys_name
, chosen_name
);
628 pr_err("%s attached to %s, but failed to start: %s.\n",
629 devname
, chosen_name
, strerror(errno
));
634 printf("MD_STARTED=unsafe\n");
635 } else if (journal_device_missing
) {
636 pr_err("Journal device is missing, not safe to start yet.\n");
637 } else if (c
->verbose
>= 0)
638 pr_err("%s attached to %s, not enough to start safely.\n",
639 devname
, chosen_name
);
649 dev_policy_free(policy
);
657 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
658 int number
, __u64 events
, int verbose
,
661 /* Find a device attached to this array with a disk.number of number
662 * and events less than the passed events, and remove the device.
667 if (md_get_array_info(mdfd
, &ra
) == 0)
668 return; /* not safe to remove from active arrays
669 * without thinking more */
671 for (d
= sra
->devs
; d
; d
= d
->next
) {
672 char dn
[24]; // 2*11 bytes for ints (including sign) + colon + null byte
675 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
676 dfd
= dev_open(dn
, O_RDONLY
);
679 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
683 st
->ss
->getinfo_super(st
, &info
, NULL
);
684 st
->ss
->free_super(st
);
687 if (info
.disk
.number
!= number
||
688 info
.events
>= events
)
691 if (d
->disk
.raid_disk
> -1)
692 sysfs_set_str(sra
, d
, "slot", "none");
693 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
695 pr_err("removing old device %s from %s\n",
696 d
->sys_name
+4, array_name
);
700 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
701 int mdfd
, char **availp
,
702 struct mdinfo
*bestinfo
)
704 /* count how many devices in sra think they are active */
708 __u64 max_events
= 0;
720 for (d
= sra
->devs
; d
; d
= d
->next
)
722 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
728 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
729 dfd
= dev_open(dn
, O_RDONLY
);
732 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
737 info
.array
.raid_disks
= raid_disks
;
738 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
739 if (info
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
)
740 bestinfo
->journal_clean
= 1;
742 raid_disks
= info
.array
.raid_disks
;
743 avail
= xcalloc(raid_disks
, 1);
746 best
= xcalloc(raid_disks
, sizeof(int));
747 devmap
= xcalloc(raid_disks
, numdevs
);
749 st
->ss
->getinfo_super(st
, &info
, devmap
);
752 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
756 max_events
= info
.events
;
757 avail
[info
.disk
.raid_disk
] = 2;
758 best
[info
.disk
.raid_disk
] = devnum
;
759 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
760 } else if (info
.events
== max_events
) {
761 avail
[info
.disk
.raid_disk
] = 2;
762 best
[info
.disk
.raid_disk
] = devnum
;
763 } else if (info
.events
== max_events
-1) {
764 if (avail
[info
.disk
.raid_disk
] == 0) {
765 avail
[info
.disk
.raid_disk
] = 1;
766 best
[info
.disk
.raid_disk
] = devnum
;
768 } else if (info
.events
< max_events
- 1)
770 else if (info
.events
== max_events
+1) {
772 max_events
= info
.events
;
773 for (i
= 0; i
< raid_disks
; i
++)
776 avail
[info
.disk
.raid_disk
] = 2;
777 best
[info
.disk
.raid_disk
] = devnum
;
778 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
779 } else { /* info.events much bigger */
780 memset(avail
, 0, raid_disks
);
781 max_events
= info
.events
;
782 avail
[info
.disk
.raid_disk
] = 2;
783 best
[info
.disk
.raid_disk
] = devnum
;
784 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
786 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
788 st
->ss
->free_super(st
);
793 /* We need to reject any device that thinks the best device is
794 * failed or missing */
795 for (b
= 0; b
< raid_disks
; b
++)
799 for (i
= 0 ; i
< raid_disks
; i
++) {
800 if (i
!= b
&& avail
[i
])
801 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
802 /* This device thinks 'b' is failed -
805 for (d
=sra
->devs
; devnum
; d
= d
->next
)
807 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
813 /* Also need to reject any spare device with an event count that
816 for (d
= sra
->devs
; d
; d
= d
->next
) {
817 if (!(d
->disk
.state
& (1<<MD_DISK_SYNC
)) &&
818 d
->events
> max_events
)
819 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
823 return cnt
+ replcnt
;
826 /* test if container has degraded member(s) */
827 static int container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
829 mdu_array_info_t array
;
831 int max_degraded
= 0;
833 for(; map
; map
= map
->next
) {
834 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
836 afd
= open_dev(map
->devnm
);
839 /* most accurate information regarding array degradation */
840 if (md_get_array_info(afd
, &array
) >= 0) {
841 int degraded
= array
.raid_disks
- array
.active_disks
-
843 if (degraded
> max_degraded
)
844 max_degraded
= degraded
;
848 return (max_degraded
);
851 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
852 struct map_ent
*target
, int bare
,
853 struct supertype
*st
, int verbose
)
855 /* This device doesn't have any md metadata
856 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
857 * If 'st' is not set, then we only know that some metadata allows this,
858 * others possibly don't.
859 * So look for a container or array to attach the device to.
860 * Prefer 'target' if that is set and the array is found.
862 * If st is set, then only arrays of that type are considered
863 * Return 0 on success, or some exit code on failure, probably 1.
867 struct map_ent
*mp
, *map
= NULL
;
868 struct mdinfo
*chosen
= NULL
;
871 if (fstat(dfd
, &stb
) != 0)
875 * Now we need to find a suitable array to add this to.
876 * We only accept arrays that:
878 * - are in the same domains as the device
879 * - are of an size for which the device will be useful
880 * and we choose the one that is the most degraded
883 if (map_lock(&map
)) {
884 pr_err("failed to get exclusive lock on mapfile\n");
887 for (mp
= map
; mp
; mp
= mp
->next
) {
888 struct supertype
*st2
;
889 struct domainlist
*dl
= NULL
;
891 unsigned long long devsize
;
892 unsigned long long component_size
= 0;
894 if (is_subarray(mp
->metadata
))
897 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
899 (st
->minor_version
>= 0 &&
900 st
->minor_version
!= st2
->minor_version
)) {
902 pr_err("not adding %s to %s as metadata type doesn't match\n",
909 sra
= sysfs_read(-1, mp
->devnm
,
910 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
911 GET_DEGRADED
|GET_COMPONENT
|GET_VERSION
);
913 /* Probably a container - no degraded info */
914 sra
= sysfs_read(-1, mp
->devnm
,
915 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
916 GET_COMPONENT
|GET_VERSION
);
918 sra
->array
.failed_disks
= -1;
925 for(i
= 0; !st2
&& superlist
[i
]; i
++)
926 st2
= superlist
[i
]->match_metadata_desc(
930 pr_err("not adding %s to %s as metadata not recognised.\n",
934 /* Need to double check the 'act_spare' permissions applies
937 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
939 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
940 act_spare_same_slot
))
944 /* update number of failed disks for mostly degraded
945 * container member */
946 if (sra
->array
.failed_disks
== -1)
947 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
949 get_dev_size(dfd
, NULL
, &devsize
);
950 if (sra
->component_size
== 0) {
951 /* true for containers, here we must read superblock
952 * to obtain minimum spare size */
953 struct supertype
*st3
= dup_super(st2
);
954 int mdfd
= open_dev(mp
->devnm
);
959 if (st3
->ss
->load_container
&&
960 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
961 component_size
= st3
->ss
->min_acceptable_spare_size(st3
);
962 st3
->ss
->free_super(st3
);
967 if ((sra
->component_size
> 0 &&
968 st2
->ss
->avail_size(st2
, devsize
,
970 ? sra
->devs
->data_offset
972 < sra
->component_size
)
974 (sra
->component_size
== 0 && devsize
< component_size
)) {
976 pr_err("not adding %s to %s as it is too small\n",
980 /* test against target.
981 * If 'target' is set and 'bare' is false, we only accept
982 * arrays/containers that match 'target'.
983 * If 'target' is set and 'bare' is true, we prefer the
984 * array which matches 'target'.
985 * target is considered only if we deal with degraded array
987 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
988 act_spare_same_slot
)) {
989 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
990 memcmp(target
->uuid
, mp
->uuid
,
991 sizeof(target
->uuid
)) == 0 &&
992 sra
->array
.failed_disks
> 0) {
993 /* This is our target!! */
997 /* skip to end so we don't check any more */
1002 /* not our target */
1007 dl
= domain_from_array(sra
, st2
->ss
->name
);
1008 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
1009 /* domain test fails */
1011 pr_err("not adding %s to %s as it is not in a compatible domain\n",
1016 /* all tests passed, OK to add to this array */
1020 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1033 /* add current device to chosen array as a spare */
1034 int mdfd
= open_dev(chosen
->sys_name
);
1036 struct mddev_dev devlist
;
1037 char chosen_devname
[24]; // 2*11 for int (including signs) + colon + null
1038 devlist
.next
= NULL
;
1040 devlist
.writemostly
= FlagDefault
;
1041 devlist
.failfast
= FlagDefault
;
1042 devlist
.devname
= chosen_devname
;
1043 sprintf(chosen_devname
, "%d:%d", major(stb
.st_rdev
),
1044 minor(stb
.st_rdev
));
1045 devlist
.disposition
= 'a';
1048 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1054 pr_err("added %s as spare for %s\n",
1055 devname
, chosen
->sys_name
);
1057 pr_err("failed to add %s as spare for %s\n",
1058 devname
, chosen
->sys_name
);
1066 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1067 struct supertype
*st
, int verbose
)
1069 /* we know that at least one partition virtual-metadata is
1070 * allowed to incorporate spares like this device. We need to
1071 * find a suitable device to copy partition information from.
1073 * Getting a list of all disk (not partition) devices is
1074 * slightly non-trivial. We could look at /sys/block, but
1075 * that is theoretically due to be removed. Maybe best to use
1076 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1077 * we depend on this directory of 'path' info. But that fails
1078 * to find loop devices and probably others. Maybe don't
1079 * worry about that, they aren't the real target.
1081 * So: check things in /dev/disk/by-path to see if they are in
1082 * a compatible domain, then load the partition table and see
1083 * if it is OK for the new device, and choose the largest
1084 * partition table that fits.
1088 char *chosen
= NULL
;
1089 unsigned long long chosen_size
= 0;
1090 struct supertype
*chosen_st
= NULL
;
1093 dir
= opendir("/dev/disk/by-path");
1096 while ((de
= readdir(dir
)) != NULL
) {
1098 struct dev_policy
*pol2
= NULL
;
1099 struct domainlist
*domlist
= NULL
;
1102 struct supertype
*st2
= NULL
;
1103 char *devname
= NULL
;
1104 unsigned long long devsectors
;
1106 if (de
->d_ino
== 0 ||
1107 de
->d_name
[0] == '.' ||
1108 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1111 ep
= de
->d_name
+ strlen(de
->d_name
);
1112 while (ep
> de
->d_name
&&
1115 if (ep
> de
->d_name
+ 5 &&
1116 strncmp(ep
-5, "-part", 5) == 0)
1117 /* This is a partition - skip it */
1120 pol2
= path_policy(de
->d_name
, type_disk
);
1122 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1123 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1124 /* new device is incompatible with this device. */
1127 domain_free(domlist
);
1130 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1134 fd
= open(devname
, O_RDONLY
);
1137 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1142 st2
= dup_super(st
);
1144 st2
= guess_super_type(fd
, guess_partitions
);
1146 st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1148 st2
->ignore_hw_compat
= 0;
1151 /* Check domain policy again, this time referring to metadata */
1152 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1153 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1154 /* Incompatible devices for this metadata type */
1156 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1157 /* Some partition types allow sparing, but not
1163 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1164 if (info
.component_size
> devsectors
)
1165 /* This partitioning doesn't fit in the device */
1168 /* This is an acceptable device to copy partition
1169 * metadata from. We could just stop here, but I
1170 * think I want to keep looking incase a larger
1171 * metadata which makes better use of the device can
1174 if (chosen
== NULL
||
1175 chosen_size
< info
.component_size
) {
1176 chosen_size
= info
.component_size
;
1181 chosen_st
->ss
->free_super(chosen_st
);
1190 domain_free(domlist
);
1191 dev_policy_free(pol2
);
1193 st2
->ss
->free_super(st2
);
1205 /* 'chosen' is the best device we can find. Let's write its
1206 * metadata to devname dfd is read-only so don't use that
1208 fd
= open(devname
, O_RDWR
);
1210 chosen_st
->ss
->store_super(chosen_st
, fd
);
1214 chosen_st
->ss
->free_super(chosen_st
);
1219 static int is_bare(int dfd
)
1221 unsigned long long size
= 0;
1222 char bufpad
[4096 + 4096];
1223 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1225 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1226 read(dfd
, buf
, 4096) != 4096)
1229 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1231 if (memcmp(buf
, buf
+1, 4095) != 0)
1234 /* OK, first 4K appear blank, try the end. */
1235 get_dev_size(dfd
, NULL
, &size
);
1236 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1237 read(dfd
, buf
, 4096) != 4096)
1240 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1242 if (memcmp(buf
, buf
+1, 4095) != 0)
1248 /* adding a spare to a regular array is quite different from adding one to
1249 * a set-of-partitions virtual array.
1250 * This function determines which is worth trying and tries as appropriate.
1251 * Arrays are given priority over partitions.
1253 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1254 struct map_ent
*target
,
1255 struct supertype
*st
, int verbose
)
1260 int partitions_ok
= 0;
1264 /* Can only add a spare if device has at least one domain */
1265 if (pol_find(pol
, pol_domain
) == NULL
)
1267 /* And only if some action allows spares */
1268 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1271 /* Now check if the device is bare.
1272 * bare devices can always be added as a spare
1273 * non-bare devices can only be added if spare-same-slot is permitted,
1274 * and this device is replacing a previous device - in which case 'target'
1277 if (!is_bare(dfd
)) {
1278 /* Must have a target and allow same_slot */
1279 /* Later - may allow force_spare without target */
1281 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1282 act_spare_same_slot
)) {
1284 pr_err("%s is not bare, so not considering as a spare\n",
1292 /* It might be OK to add this device to an array - need to see
1293 * what arrays might be candidates.
1296 /* just try try 'array' or 'partition' based on this metadata */
1297 if (st
->ss
->add_to_super
)
1298 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1301 return partition_try_spare(devname
, dfdp
, pol
,
1304 /* No metadata was specified or found so options are open.
1305 * Check for whether any array metadata, or any partition metadata
1306 * might allow adding the spare. This check is just help to avoid
1307 * a more costly scan of all arrays when we can be sure that will
1310 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1311 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1312 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1314 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1315 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1320 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1322 if (rv
!= 0 && partitions_ok
)
1323 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1327 int IncrementalScan(struct context
*c
, char *devnm
)
1329 /* look at every device listed in the 'map' file.
1330 * If one is found that is not running then:
1331 * look in mdadm.conf for bitmap file.
1332 * if one exists, but array has none, add it.
1333 * try to start array in auto-readonly mode
1335 struct map_ent
*mapl
= NULL
;
1337 struct mddev_ident
*devs
, *mddev
;
1343 devs
= conf_get_ident(NULL
);
1346 for (me
= mapl
; me
; me
= me
->next
) {
1347 mdu_array_info_t array
;
1348 mdu_bitmap_file_t bmf
;
1352 if (devnm
&& strcmp(devnm
, me
->devnm
) != 0)
1354 if (me
->metadata
[0] == '/') {
1360 /* member array, need to work on container */
1361 strncpy(container
, me
->metadata
+1, 32);
1363 sl
= strchr(container
, '/');
1370 mdfd
= open_dev(me
->devnm
);
1374 if (!isdigit(me
->metadata
[0])) {
1375 /* must be a container */
1376 struct supertype
*st
= super_by_fd(mdfd
, NULL
);
1378 struct map_ent
*map
= NULL
;
1380 if (st
&& st
->ss
->load_container
)
1381 ret
= st
->ss
->load_container(st
, mdfd
, NULL
);
1383 if (!ret
&& st
&& st
->ss
->container_content
) {
1385 pr_err("failed to get exclusive lock on mapfile\n");
1386 ret
= Incremental_container(st
, me
->path
, c
, only
);
1393 if (md_get_array_info(mdfd
, &array
) == 0 || errno
!= ENODEV
) {
1397 /* Ok, we can try this one. Maybe it needs a bitmap */
1398 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1399 if (mddev
->devname
&& me
->path
1400 && devname_matches(mddev
->devname
, me
->path
))
1402 if (mddev
&& mddev
->bitmap_file
) {
1404 * Note: early kernels will wrongly fail this, so it
1408 if (ioctl(mdfd
, GET_ARRAY_INFO
, &bmf
) < 0) {
1409 int bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1411 added
= ioctl(mdfd
, SET_BITMAP_FILE
,
1416 if (c
->verbose
>= 0) {
1418 pr_err("Added bitmap %s to %s\n",
1419 mddev
->bitmap_file
, me
->path
);
1420 else if (errno
!= EEXIST
)
1421 pr_err("Failed to add bitmap to %s: %s\n",
1422 me
->path
, strerror(errno
));
1425 /* FIXME check for reshape_active and consider not
1428 sra
= sysfs_read(mdfd
, NULL
, 0);
1430 if (sysfs_set_str(sra
, NULL
,
1431 "array_state", "read-auto") == 0) {
1432 if (c
->verbose
>= 0)
1433 pr_err("started array %s\n",
1434 me
->path
?: me
->devnm
);
1436 pr_err("failed to start array %s: %s\n",
1437 me
->path
?: me
->devnm
,
1447 static char *container2devname(char *devname
)
1449 char *mdname
= NULL
;
1451 if (devname
[0] == '/') {
1452 int fd
= open(devname
, O_RDONLY
);
1454 mdname
= xstrdup(fd2devnm(fd
));
1459 struct map_ent
*mp
, *map
= NULL
;
1461 if (!parse_uuid(devname
, uuid
))
1463 mp
= map_by_uuid(&map
, uuid
);
1465 mdname
= xstrdup(mp
->devnm
);
1472 static int Incremental_container(struct supertype
*st
, char *devname
,
1473 struct context
*c
, char *only
)
1475 /* Collect the contents of this container and for each
1476 * array, choose a device name and assemble the array.
1479 struct mdinfo
*list
;
1481 struct map_ent
*map
= NULL
;
1484 struct mddev_ident
*match
;
1486 struct domainlist
*domains
;
1487 struct map_ent
*smp
;
1494 st
->ss
->getinfo_super(st
, &info
, NULL
);
1496 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1497 info
.container_enough
> 0)
1501 printf("MD_STARTED=no\n");
1502 } else if (c
->verbose
)
1503 pr_err("not enough devices to start the container\n");
1507 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1508 if (match
== NULL
&& rv
== 2)
1511 /* Need to compute 'trustworthy' */
1513 trustworthy
= LOCAL
;
1514 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1515 trustworthy
= LOCAL
;
1516 else if (st
->ss
->match_home(st
, "any") == 1)
1517 trustworthy
= LOCAL
;
1519 trustworthy
= FOREIGN
;
1521 list
= st
->ss
->container_content(st
, NULL
);
1522 /* when nothing to activate - quit */
1525 printf("MD_STARTED=nothing\n");
1529 for (ra
= list
; ra
; ra
= ra
->next
) {
1531 char chosen_name
[1024];
1533 struct mddev_ident
*match
= NULL
;
1536 /* do not activate arrays blocked by metadata handler */
1537 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1538 pr_err("Cannot activate array %s in %s.\n",
1539 ra
->text_version
, devname
);
1543 mp
= map_by_uuid(&map
, ra
->uuid
);
1546 mdfd
= open_dev(mp
->devnm
);
1548 strcpy(chosen_name
, mp
->path
);
1550 strcpy(chosen_name
, mp
->devnm
);
1553 /* Check in mdadm.conf for container == devname and
1554 * member == ra->text_version after second slash.
1556 char *sub
= strchr(ra
->text_version
+1, '/');
1557 struct mddev_ident
*array_list
;
1560 array_list
= conf_get_ident(NULL
);
1563 for(; array_list
; array_list
= array_list
->next
) {
1565 if (array_list
->member
== NULL
||
1566 array_list
->container
== NULL
)
1568 if (strcmp(array_list
->member
, sub
) != 0)
1570 if (array_list
->uuid_set
&&
1571 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1573 dn
= container2devname(array_list
->container
);
1576 if (strncmp(dn
, ra
->text_version
+1,
1578 ra
->text_version
[strlen(dn
)+1] != '/') {
1583 /* we have a match */
1586 pr_err("match found for member %s\n",
1587 array_list
->member
);
1591 if (match
&& match
->devname
&&
1592 strcasecmp(match
->devname
, "<ignore>") == 0) {
1594 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1595 match
->container
, match
->member
);
1599 trustworthy
= LOCAL
;
1601 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1607 if (only
&& (!mp
|| strcmp(mp
->devnm
, only
) != 0))
1611 pr_err("failed to open %s: %s.\n",
1612 chosen_name
, strerror(errno
));
1616 assemble_container_content(st
, mdfd
, ra
, c
,
1617 chosen_name
, &result
);
1620 if (c
->export
&& result
) {
1622 printf("MD_STARTED");
1623 if (result
& INCR_NO
) {
1624 printf("%cno", sep
);
1627 if (result
& INCR_UNSAFE
) {
1628 printf("%cunsafe", sep
);
1631 if (result
& INCR_ALREADY
) {
1632 printf("%calready", sep
);
1635 if (result
& INCR_YES
) {
1636 printf("%cyes", sep
);
1642 /* don't move spares to container with volume being activated
1643 when all volumes are blocked */
1644 if (ra_all
== ra_blocked
)
1647 /* Now move all suitable spares from spare container */
1648 domains
= domain_from_array(list
, st
->ss
->name
);
1649 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1651 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1652 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1653 /* spare container found */
1654 struct supertype
*sst
=
1655 super_imsm
.match_metadata_desc("imsm");
1656 struct mdinfo
*sinfo
;
1657 unsigned long long min_size
= 0;
1658 if (st
->ss
->min_acceptable_spare_size
)
1659 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1660 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1662 sinfo
= container_choose_spares(sst
, min_size
,
1665 sst
->ss
->free_super(sst
);
1668 struct mdinfo
*disks
= sinfo
->devs
;
1670 /* move spare from spare
1671 * container to currently
1677 makedev(disks
->disk
.major
,
1678 disks
->disk
.minor
)))
1680 disks
= disks
->next
;
1683 pr_err("Added %d spare%s to %s\n",
1684 count
, count
>1?"s":"", devname
);
1690 domain_free(domains
);
1694 static void run_udisks(char *arg1
, char *arg2
)
1699 execl("/usr/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1700 execl("/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1703 while (pid
> 0 && wait(&status
) != pid
)
1708 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1709 * raid arrays, and if so first fail (if needed) and then remove the device.
1711 * @devname - The device we want to remove
1712 * @id_path - name as found in /dev/disk/by-path for this device
1714 * Note: the device name must be a kernel name like "sda", so
1715 * that we can find it in /proc/mdstat
1717 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1721 struct mdstat_ent
*ent
;
1722 struct mddev_dev devlist
;
1727 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1729 if (strchr(devname
, '/')) {
1730 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname
);
1733 ent
= mdstat_by_component(devname
);
1736 pr_err("%s does not appear to be a component of any array\n", devname
);
1739 sysfs_init(&mdi
, -1, ent
->devnm
);
1740 mdfd
= open_dev_excl(ent
->devnm
);
1743 if (sysfs_get_str(&mdi
, NULL
, "array_state",
1744 buf
, sizeof(buf
)) > 0) {
1745 if (strncmp(buf
, "active", 6) == 0 ||
1746 strncmp(buf
, "clean", 5) == 0)
1747 sysfs_set_str(&mdi
, NULL
,
1748 "array_state", "read-auto");
1751 mdfd
= open_dev(ent
->devnm
);
1754 pr_err("Cannot open array %s!!\n", ent
->devnm
);
1760 struct map_ent
*map
= NULL
, *me
;
1761 me
= map_by_devnm(&map
, ent
->devnm
);
1763 policy_save_path(id_path
, me
);
1767 memset(&devlist
, 0, sizeof(devlist
));
1768 devlist
.devname
= devname
;
1769 devlist
.disposition
= 'f';
1770 /* for a container, we must fail each member array */
1771 if (ent
->metadata_version
&&
1772 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1773 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1774 struct mdstat_ent
*memb
;
1775 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1776 if (is_container_member(memb
, ent
->devnm
)) {
1777 int subfd
= open_dev(memb
->devnm
);
1779 rv
|= Manage_subdevs(
1781 &devlist
, verbose
, 0,
1786 free_mdstat(mdstat
);
1788 rv
|= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1789 verbose
, 0, NULL
, 0);
1791 /* Failed due to EBUSY, try to stop the array.
1792 * Give udisks a chance to unmount it first.
1794 int devid
= devnm2devid(ent
->devnm
);
1795 run_udisks("--unmount", map_dev(major(devid
),minor(devid
), 0));
1796 rv
= Manage_stop(ent
->devnm
, mdfd
, verbose
, 1);
1798 /* At least we can try to trigger a 'remove' */
1799 sysfs_uevent(&mdi
, "remove");
1802 pr_err("Fail to stop %s too.\n", ent
->devnm
);
1805 devlist
.disposition
= 'r';
1806 rv
= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1807 verbose
, 0, NULL
, 0);