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 struct dev_policy
*policy
= NULL
;
103 struct map_ent target_array
;
105 char *devname
= devlist
->devname
;
106 int journal_device_missing
= 0;
108 struct createinfo
*ci
= conf_get_create_info();
110 if (stat(devname
, &stb
) < 0) {
112 pr_err("stat failed for %s: %s.\n",
113 devname
, strerror(errno
));
116 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
118 pr_err("%s is not a block device.\n",
122 dfd
= dev_open(devname
, O_RDONLY
);
125 pr_err("cannot open %s: %s.\n",
126 devname
, strerror(errno
));
129 /* If the device is a container, we do something very different */
130 if (must_be_container(dfd
)) {
132 st
= super_by_fd(dfd
, NULL
);
133 if (st
&& st
->ss
->load_container
)
134 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
137 if (!rv
&& st
->ss
->container_content
) {
139 pr_err("failed to get exclusive lock on mapfile\n");
141 printf("MD_DEVNAME=%s\n", devname
);
142 rv
= Incremental_container(st
, devname
, c
, NULL
);
147 pr_err("%s is not part of an md array.\n",
152 /* 1/ Check if device is permitted by mdadm.conf */
154 for (;devlist
; devlist
= devlist
->next
)
155 if (conf_test_dev(devlist
->devname
))
158 devlist
= conf_get_devs();
159 for (;devlist
; devlist
= devlist
->next
) {
161 if (stat(devlist
->devname
, &st2
) == 0 &&
162 (st2
.st_mode
& S_IFMT
) == S_IFBLK
&&
163 st2
.st_rdev
== stb
.st_rdev
)
169 pr_err("%s not permitted by mdadm.conf.\n",
174 /* 2/ Find metadata, reject if none appropriate (check
175 * version/name from args) */
177 if (fstat(dfd
, &stb
) < 0) {
179 pr_err("fstat failed for %s: %s.\n",
180 devname
, strerror(errno
));
183 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
185 pr_err("%s is not a block device.\n",
190 dinfo
.disk
.major
= major(stb
.st_rdev
);
191 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
193 policy
= disk_policy(&dinfo
);
194 have_target
= policy_check_path(&dinfo
, &target_array
);
196 if (st
== NULL
&& (st
= guess_super_type(dfd
, guess_array
)) == NULL
) {
198 pr_err("no recognisable superblock on %s.\n",
200 rv
= try_spare(devname
, &dfd
, policy
,
201 have_target
? &target_array
: NULL
,
205 st
->ignore_hw_compat
= 0;
207 if (st
->ss
->compare_super
== NULL
||
208 st
->ss
->load_super(st
, dfd
, c
->verbose
>= 0 ? devname
: NULL
)) {
210 pr_err("no RAID superblock on %s.\n",
212 rv
= try_spare(devname
, &dfd
, policy
,
213 have_target
? &target_array
: NULL
,
218 close (dfd
); dfd
= -1;
220 st
->ss
->getinfo_super(st
, &info
, NULL
);
222 /* 3/ Check if there is a match in mdadm.conf */
223 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
224 if (!match
&& rv
== 2)
227 if (match
&& match
->devname
&&
228 strcasecmp(match
->devname
, "<ignore>") == 0) {
230 pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
235 /* 3a/ if not, check for homehost match. If no match, continue
236 * but don't trust the 'name' in the array. Thus a 'random' minor
237 * number will be assigned, and the device name will be based
241 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
243 else if (st
->ss
->match_home(st
, "any") == 1)
244 trustworthy
= LOCAL_ANY
;
246 trustworthy
= FOREIGN
;
248 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
249 (trustworthy
== LOCAL
))) {
251 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
252 devname
, st
->ss
->name
);
255 if (trustworthy
== LOCAL_ANY
)
258 /* There are three possible sources for 'autof': command line,
259 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
260 * ARRAY takes precedence, then command line, then
263 if (match
&& match
->autof
)
264 c
->autof
= match
->autof
;
266 c
->autof
= ci
->autof
;
268 name_to_use
= info
.name
;
269 if (name_to_use
[0] == 0 && info
.array
.level
== LEVEL_CONTAINER
) {
270 name_to_use
= info
.text_version
;
271 trustworthy
= METADATA
;
273 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
274 ! c
->require_homehost
&&
275 conf_name_is_free(name_to_use
))
278 /* strip "hostname:" prefix from name if we have decided
279 * to treat it as LOCAL
281 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
282 name_to_use
= strchr(name_to_use
, ':')+1;
284 /* 4/ Check if array exists.
287 pr_err("failed to get exclusive lock on mapfile\n");
288 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
291 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
294 pr_err("cannot reopen %s: %s.\n",
295 devname
, strerror(errno
));
298 /* Cannot hold it open while we add the device to the array,
299 * so we must release the O_EXCL and depend on the map_lock()
300 * So now is the best time to remove any partitions.
302 remove_partitions(dfd
);
306 mp
= map_by_uuid(&map
, info
.uuid
);
308 mdfd
= open_dev(mp
->devnm
);
314 /* Skip the clustered ones. This should be started by
315 * clustering resource agents
317 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
320 /* Couldn't find an existing array, maybe make a new one */
321 mdfd
= create_mddev(match
? match
->devname
: NULL
,
322 name_to_use
, c
->autof
, trustworthy
, chosen_name
, 0);
327 if (sysfs_init(&info
, mdfd
, NULL
)) {
328 pr_err("unable to initialize sysfs for %s\n",
334 if (set_array_info(mdfd
, st
, &info
) != 0) {
335 pr_err("failed to set array info for %s: %s\n",
336 chosen_name
, strerror(errno
));
342 dinfo
.disk
.major
= major(stb
.st_rdev
);
343 dinfo
.disk
.minor
= minor(stb
.st_rdev
);
344 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
345 pr_err("failed to add %s to new array %s: %s.\n",
346 devname
, chosen_name
, strerror(errno
));
347 ioctl(mdfd
, STOP_ARRAY
, 0);
351 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
352 GET_OFFSET
| GET_SIZE
));
354 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
355 /* It really should be 'none' - must be old buggy
356 * kernel, and mdadm -I may not be able to complete.
359 ioctl(mdfd
, STOP_ARRAY
, NULL
);
360 pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n");
364 info
.array
.working_disks
= 1;
365 /* 6/ Make sure /var/run/mdadm.map contains this array. */
366 map_update(&map
, fd2devnm(mdfd
),
368 info
.uuid
, chosen_name
);
371 /* - check one drive in array to make sure metadata is a reasonably */
372 /* close match. Reject if not (e.g. different type) */
373 /* - add the device */
377 struct supertype
*st2
;
378 struct mdinfo info2
, *d
;
380 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
381 GET_OFFSET
| GET_SIZE
));
384 strcpy(chosen_name
, mp
->path
);
386 strcpy(chosen_name
, mp
->devnm
);
388 /* It is generally not OK to add non-spare drives to a
389 * running array as they are probably missing because
390 * they failed. However if runstop is 1, then the
391 * array was possibly started early and our best bet is
392 * to add this anyway.
393 * Also if action policy is re-add or better we allow
395 * This doesn't apply to containers as the 'non-spare'
396 * flag has a different meaning. The test has to happen
397 * at the device level there
399 if (!st
->ss
->external
&&
400 (info
.disk
.state
& (1 << MD_DISK_SYNC
)) != 0 &&
401 !policy_action_allows(policy
, st
->ss
->name
, act_re_add
) &&
403 if (md_array_active(mdfd
)) {
404 pr_err("not adding %s to active array (without --run) %s\n",
405 devname
, chosen_name
);
415 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
416 sra
->devs
->disk
.minor
);
417 dfd2
= dev_open(dn
, O_RDONLY
);
419 pr_err("unable to open %s\n", devname
);
424 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
425 st
->ss
->compare_super(st
, st2
) != 0) {
426 pr_err("metadata mismatch between %s and chosen array %s\n",
427 devname
, chosen_name
);
433 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
434 st2
->ss
->free_super(st2
);
435 if (info
.array
.level
!= info2
.array
.level
||
436 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
437 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
438 pr_err("unexpected difference between %s and %s.\n",
439 chosen_name
, devname
);
444 info
.disk
.major
= major(stb
.st_rdev
);
445 info
.disk
.minor
= minor(stb
.st_rdev
);
446 /* add disk needs to know about containers */
447 if (st
->ss
->external
)
448 sra
->array
.level
= LEVEL_CONTAINER
;
450 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
451 info
.disk
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
453 err
= add_disk(mdfd
, st
, sra
, &info
);
454 if (err
< 0 && errno
== EBUSY
) {
455 /* could be another device present with the same
456 * disk.number. Find and reject any such
458 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
459 info
.events
, c
->verbose
, chosen_name
);
460 err
= add_disk(mdfd
, st
, sra
, &info
);
462 if (err
< 0 && errno
== EINVAL
&&
463 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
464 /* Maybe it needs to be added as a spare */
465 if (policy_action_allows(policy
, st
->ss
->name
,
467 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
468 err
= add_disk(mdfd
, st
, sra
, &info
);
471 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
472 devname
, chosen_name
);
475 pr_err("failed to add %s to existing array %s: %s.\n",
476 devname
, chosen_name
, strerror(errno
));
480 info
.array
.working_disks
= 0;
481 for (d
= sra
->devs
; d
; d
=d
->next
)
482 info
.array
.working_disks
++;
485 if (strncmp(chosen_name
, "/dev/md/", 8) == 0)
486 md_devname
= chosen_name
+8;
488 md_devname
= chosen_name
;
490 printf("MD_DEVICE=%s\n", fd2devnm(mdfd
));
491 printf("MD_DEVNAME=%s\n", md_devname
);
492 printf("MD_FOREIGN=%s\n", trustworthy
== FOREIGN
? "yes" : "no");
495 /* 7/ Is there enough devices to possibly start the array? */
496 /* 7a/ if not, finish with success. */
497 if (info
.array
.level
== LEVEL_CONTAINER
) {
499 /* Try to assemble within the container */
500 sysfs_uevent(sra
, "change");
501 if (!c
->export
&& c
->verbose
>= 0)
502 pr_err("container %s now has %d device%s\n",
503 chosen_name
, info
.array
.working_disks
,
504 info
.array
.working_disks
== 1?"":"s");
505 wait_for(chosen_name
, mdfd
);
506 if (st
->ss
->external
)
507 strcpy(devnm
, fd2devnm(mdfd
));
508 if (st
->ss
->load_container
)
509 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
513 rv
= Incremental_container(st
, chosen_name
, c
, NULL
);
515 /* after spare is added, ping monitor for external metadata
516 * so that it can eg. try to rebuild degraded array */
517 if (st
->ss
->external
)
522 /* We have added something to the array, so need to re-read the
523 * state. Eventually this state should be kept up-to-date as
527 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
528 GET_OFFSET
| GET_SIZE
));
529 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
531 journal_device_missing
= (info
.journal_device_required
) && (info
.journal_clean
== 0);
533 if (info
.consistency_policy
== CONSISTENCY_POLICY_PPL
)
534 info
.array
.state
|= 1;
536 if (enough(info
.array
.level
, info
.array
.raid_disks
,
537 info
.array
.layout
, info
.array
.state
& 1, avail
) == 0) {
539 printf("MD_STARTED=no\n");
540 } else if (c
->verbose
>= 0)
541 pr_err("%s attached to %s, not enough to start (%d).\n",
542 devname
, chosen_name
, active_disks
);
548 /* - if number of OK devices match expected, or -R and there */
550 /* + add any bitmap file */
551 /* + start the array (auto-readonly). */
553 if (md_array_active(mdfd
)) {
555 printf("MD_STARTED=already\n");
556 } else if (c
->verbose
>= 0)
557 pr_err("%s attached to %s which is already active.\n",
558 devname
, chosen_name
);
564 if (c
->runstop
> 0 || (!journal_device_missing
&& active_disks
>= info
.array
.working_disks
)) {
566 /* Let's try to start it */
568 if (journal_device_missing
)
569 pr_err("Trying to run with missing journal device\n");
570 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
571 pr_err("%s: This array is being reshaped and cannot be started\n",
573 cont_err("by --incremental. Please use --assemble\n");
576 if (match
&& match
->bitmap_file
) {
577 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
579 pr_err("Could not open bitmap file %s.\n",
583 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
585 pr_err("Failed to set bitmapfile for %s.\n",
591 /* Need to remove from the array any devices which
592 * 'count_active' discerned were too old or inappropriate
594 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
595 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
596 remove_disk(mdfd
, st
, sra
, d
);
598 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
) &&
599 trustworthy
!= FOREIGN
)
600 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
602 rv
= sysfs_set_str(sra
, NULL
,
603 "array_state", "read-auto");
604 /* Array might be O_EXCL which will interfere with
605 * fsck and mount. So re-open without O_EXCL.
610 printf("MD_STARTED=yes\n");
611 } else if (c
->verbose
>= 0)
612 pr_err("%s attached to %s, which has been started.\n",
613 devname
, chosen_name
);
615 wait_for(chosen_name
, mdfd
);
616 /* We just started the array, so some devices
617 * might have been evicted from the array
618 * because their event counts were too old.
619 * If the action=re-add policy is in-force for
620 * those devices we should re-add them now.
622 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
623 if (disk_action_allows(dsk
, st
->ss
->name
,
625 add_disk(mdfd
, st
, sra
, dsk
) == 0)
626 pr_err("%s re-added to %s\n",
627 dsk
->sys_name
, chosen_name
);
630 pr_err("%s attached to %s, but failed to start: %s.\n",
631 devname
, chosen_name
, strerror(errno
));
636 printf("MD_STARTED=unsafe\n");
637 } else if (journal_device_missing
) {
638 pr_err("Journal device is missing, not safe to start yet.\n");
639 } else if (c
->verbose
>= 0)
640 pr_err("%s attached to %s, not enough to start safely.\n",
641 devname
, chosen_name
);
651 dev_policy_free(policy
);
659 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
660 int number
, __u64 events
, int verbose
,
663 /* Find a device attached to this array with a disk.number of number
664 * and events less than the passed events, and remove the device.
668 if (md_array_active(mdfd
))
669 return; /* not safe to remove from active arrays
670 * without thinking more */
672 for (d
= sra
->devs
; d
; d
= d
->next
) {
673 char dn
[24]; // 2*11 bytes for ints (including sign) + colon + null byte
676 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
677 dfd
= dev_open(dn
, O_RDONLY
);
680 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
684 st
->ss
->getinfo_super(st
, &info
, NULL
);
685 st
->ss
->free_super(st
);
688 if (info
.disk
.number
!= number
|| 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
,
969 sra
->devs
? sra
->devs
->data_offset
:
971 sra
->component_size
) ||
972 (sra
->component_size
== 0 && devsize
< component_size
)) {
974 pr_err("not adding %s to %s as it is too small\n",
978 /* test against target.
979 * If 'target' is set and 'bare' is false, we only accept
980 * arrays/containers that match 'target'.
981 * If 'target' is set and 'bare' is true, we prefer the
982 * array which matches 'target'.
983 * target is considered only if we deal with degraded array
985 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
986 act_spare_same_slot
)) {
987 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
988 memcmp(target
->uuid
, mp
->uuid
,
989 sizeof(target
->uuid
)) == 0 &&
990 sra
->array
.failed_disks
> 0) {
991 /* This is our target!! */
995 /* skip to end so we don't check any more */
1000 /* not our target */
1005 dl
= domain_from_array(sra
, st2
->ss
->name
);
1006 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
1007 /* domain test fails */
1009 pr_err("not adding %s to %s as it is not in a compatible domain\n",
1014 /* all tests passed, OK to add to this array */
1018 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1031 /* add current device to chosen array as a spare */
1032 int mdfd
= open_dev(chosen
->sys_name
);
1034 struct mddev_dev devlist
;
1035 char chosen_devname
[24]; // 2*11 for int (including signs) + colon + null
1036 devlist
.next
= NULL
;
1038 devlist
.writemostly
= FlagDefault
;
1039 devlist
.failfast
= FlagDefault
;
1040 devlist
.devname
= chosen_devname
;
1041 sprintf(chosen_devname
, "%d:%d", major(stb
.st_rdev
),
1042 minor(stb
.st_rdev
));
1043 devlist
.disposition
= 'a';
1046 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1052 pr_err("added %s as spare for %s\n",
1053 devname
, chosen
->sys_name
);
1055 pr_err("failed to add %s as spare for %s\n",
1056 devname
, chosen
->sys_name
);
1064 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1065 struct supertype
*st
, int verbose
)
1067 /* we know that at least one partition virtual-metadata is
1068 * allowed to incorporate spares like this device. We need to
1069 * find a suitable device to copy partition information from.
1071 * Getting a list of all disk (not partition) devices is
1072 * slightly non-trivial. We could look at /sys/block, but
1073 * that is theoretically due to be removed. Maybe best to use
1074 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1075 * we depend on this directory of 'path' info. But that fails
1076 * to find loop devices and probably others. Maybe don't
1077 * worry about that, they aren't the real target.
1079 * So: check things in /dev/disk/by-path to see if they are in
1080 * a compatible domain, then load the partition table and see
1081 * if it is OK for the new device, and choose the largest
1082 * partition table that fits.
1086 char *chosen
= NULL
;
1087 unsigned long long chosen_size
= 0;
1088 struct supertype
*chosen_st
= NULL
;
1091 dir
= opendir("/dev/disk/by-path");
1094 while ((de
= readdir(dir
)) != NULL
) {
1096 struct dev_policy
*pol2
= NULL
;
1097 struct domainlist
*domlist
= NULL
;
1100 struct supertype
*st2
= NULL
;
1101 char *devname
= NULL
;
1102 unsigned long long devsectors
;
1104 if (de
->d_ino
== 0 || de
->d_name
[0] == '.' ||
1105 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1108 ep
= de
->d_name
+ strlen(de
->d_name
);
1109 while (ep
> de
->d_name
&&
1112 if (ep
> de
->d_name
+ 5 &&
1113 strncmp(ep
-5, "-part", 5) == 0)
1114 /* This is a partition - skip it */
1117 pol2
= path_policy(de
->d_name
, type_disk
);
1119 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1120 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1121 /* new device is incompatible with this device. */
1124 domain_free(domlist
);
1127 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1131 fd
= open(devname
, O_RDONLY
);
1134 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1139 st2
= dup_super(st
);
1141 st2
= guess_super_type(fd
, guess_partitions
);
1142 if (st2
== NULL
|| st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1144 st2
->ignore_hw_compat
= 0;
1147 /* Check domain policy again, this time referring to metadata */
1148 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1149 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1150 /* Incompatible devices for this metadata type */
1152 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1153 /* Some partition types allow sparing, but not
1159 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1160 if (info
.component_size
> devsectors
)
1161 /* This partitioning doesn't fit in the device */
1164 /* This is an acceptable device to copy partition
1165 * metadata from. We could just stop here, but I
1166 * think I want to keep looking incase a larger
1167 * metadata which makes better use of the device can
1170 if (chosen
== NULL
|| chosen_size
< info
.component_size
) {
1171 chosen_size
= info
.component_size
;
1176 chosen_st
->ss
->free_super(chosen_st
);
1185 domain_free(domlist
);
1186 dev_policy_free(pol2
);
1188 st2
->ss
->free_super(st2
);
1200 /* 'chosen' is the best device we can find. Let's write its
1201 * metadata to devname dfd is read-only so don't use that
1203 fd
= open(devname
, O_RDWR
);
1205 chosen_st
->ss
->store_super(chosen_st
, fd
);
1209 chosen_st
->ss
->free_super(chosen_st
);
1214 static int is_bare(int dfd
)
1216 unsigned long long size
= 0;
1217 char bufpad
[4096 + 4096];
1218 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1220 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1221 read(dfd
, buf
, 4096) != 4096)
1224 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1226 if (memcmp(buf
, buf
+1, 4095) != 0)
1229 /* OK, first 4K appear blank, try the end. */
1230 get_dev_size(dfd
, NULL
, &size
);
1231 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1232 read(dfd
, buf
, 4096) != 4096)
1235 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1237 if (memcmp(buf
, buf
+1, 4095) != 0)
1243 /* adding a spare to a regular array is quite different from adding one to
1244 * a set-of-partitions virtual array.
1245 * This function determines which is worth trying and tries as appropriate.
1246 * Arrays are given priority over partitions.
1248 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1249 struct map_ent
*target
,
1250 struct supertype
*st
, int verbose
)
1255 int partitions_ok
= 0;
1259 /* Can only add a spare if device has at least one domain */
1260 if (pol_find(pol
, pol_domain
) == NULL
)
1262 /* And only if some action allows spares */
1263 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1266 /* Now check if the device is bare.
1267 * bare devices can always be added as a spare
1268 * non-bare devices can only be added if spare-same-slot is permitted,
1269 * and this device is replacing a previous device - in which case 'target'
1272 if (!is_bare(dfd
)) {
1273 /* Must have a target and allow same_slot */
1274 /* Later - may allow force_spare without target */
1276 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1277 act_spare_same_slot
)) {
1279 pr_err("%s is not bare, so not considering as a spare\n",
1287 /* It might be OK to add this device to an array - need to see
1288 * what arrays might be candidates.
1291 /* just try try 'array' or 'partition' based on this metadata */
1292 if (st
->ss
->add_to_super
)
1293 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1296 return partition_try_spare(devname
, dfdp
, pol
,
1299 /* No metadata was specified or found so options are open.
1300 * Check for whether any array metadata, or any partition metadata
1301 * might allow adding the spare. This check is just help to avoid
1302 * a more costly scan of all arrays when we can be sure that will
1305 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1306 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1307 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1309 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1310 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1315 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1317 if (rv
!= 0 && partitions_ok
)
1318 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1322 int IncrementalScan(struct context
*c
, char *devnm
)
1324 /* look at every device listed in the 'map' file.
1325 * If one is found that is not running then:
1326 * look in mdadm.conf for bitmap file.
1327 * if one exists, but array has none, add it.
1328 * try to start array in auto-readonly mode
1330 struct map_ent
*mapl
= NULL
;
1332 struct mddev_ident
*devs
, *mddev
;
1338 devs
= conf_get_ident(NULL
);
1341 for (me
= mapl
; me
; me
= me
->next
) {
1342 mdu_array_info_t array
;
1346 if (devnm
&& strcmp(devnm
, me
->devnm
) != 0)
1348 if (me
->metadata
[0] == '/') {
1354 /* member array, need to work on container */
1355 strncpy(container
, me
->metadata
+1, 32);
1357 sl
= strchr(container
, '/');
1364 mdfd
= open_dev(me
->devnm
);
1368 if (!isdigit(me
->metadata
[0])) {
1369 /* must be a container */
1370 struct supertype
*st
= super_by_fd(mdfd
, NULL
);
1372 struct map_ent
*map
= NULL
;
1374 if (st
&& st
->ss
->load_container
)
1375 ret
= st
->ss
->load_container(st
, mdfd
, NULL
);
1377 if (!ret
&& st
&& st
->ss
->container_content
) {
1379 pr_err("failed to get exclusive lock on mapfile\n");
1380 ret
= Incremental_container(st
, me
->path
, c
, only
);
1387 if (md_get_array_info(mdfd
, &array
) == 0 || errno
!= ENODEV
) {
1391 /* Ok, we can try this one. Maybe it needs a bitmap */
1392 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1393 if (mddev
->devname
&& me
->path
&&
1394 devname_matches(mddev
->devname
, me
->path
))
1396 if (mddev
&& mddev
->bitmap_file
) {
1398 * Note: early kernels will wrongly fail this, so it
1404 bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1406 added
= ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
);
1409 if (c
->verbose
>= 0) {
1411 pr_err("Added bitmap %s to %s\n",
1412 mddev
->bitmap_file
, me
->path
);
1413 else if (errno
!= EEXIST
)
1414 pr_err("Failed to add bitmap to %s: %s\n",
1415 me
->path
, strerror(errno
));
1418 /* FIXME check for reshape_active and consider not
1421 sra
= sysfs_read(mdfd
, NULL
, 0);
1423 if (sysfs_set_str(sra
, NULL
,
1424 "array_state", "read-auto") == 0) {
1425 if (c
->verbose
>= 0)
1426 pr_err("started array %s\n",
1427 me
->path
?: me
->devnm
);
1429 pr_err("failed to start array %s: %s\n",
1430 me
->path
?: me
->devnm
,
1440 static char *container2devname(char *devname
)
1442 char *mdname
= NULL
;
1444 if (devname
[0] == '/') {
1445 int fd
= open(devname
, O_RDONLY
);
1447 mdname
= xstrdup(fd2devnm(fd
));
1452 struct map_ent
*mp
, *map
= NULL
;
1454 if (!parse_uuid(devname
, uuid
))
1456 mp
= map_by_uuid(&map
, uuid
);
1458 mdname
= xstrdup(mp
->devnm
);
1465 static int Incremental_container(struct supertype
*st
, char *devname
,
1466 struct context
*c
, char *only
)
1468 /* Collect the contents of this container and for each
1469 * array, choose a device name and assemble the array.
1472 struct mdinfo
*list
;
1474 struct map_ent
*map
= NULL
;
1477 struct mddev_ident
*match
;
1479 struct domainlist
*domains
;
1480 struct map_ent
*smp
;
1487 st
->ss
->getinfo_super(st
, &info
, NULL
);
1489 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1490 info
.container_enough
> 0)
1494 printf("MD_STARTED=no\n");
1495 } else if (c
->verbose
)
1496 pr_err("not enough devices to start the container\n");
1500 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1501 if (match
== NULL
&& rv
== 2)
1504 /* Need to compute 'trustworthy' */
1506 trustworthy
= LOCAL
;
1507 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1508 trustworthy
= LOCAL
;
1509 else if (st
->ss
->match_home(st
, "any") == 1)
1510 trustworthy
= LOCAL
;
1512 trustworthy
= FOREIGN
;
1514 list
= st
->ss
->container_content(st
, NULL
);
1515 /* when nothing to activate - quit */
1518 printf("MD_STARTED=nothing\n");
1522 for (ra
= list
; ra
; ra
= ra
->next
) {
1524 char chosen_name
[1024];
1526 struct mddev_ident
*match
= NULL
;
1529 /* do not activate arrays blocked by metadata handler */
1530 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1531 pr_err("Cannot activate array %s in %s.\n",
1532 ra
->text_version
, devname
);
1536 mp
= map_by_uuid(&map
, ra
->uuid
);
1539 mdfd
= open_dev(mp
->devnm
);
1541 strcpy(chosen_name
, mp
->path
);
1543 strcpy(chosen_name
, mp
->devnm
);
1546 /* Check in mdadm.conf for container == devname and
1547 * member == ra->text_version after second slash.
1549 char *sub
= strchr(ra
->text_version
+1, '/');
1550 struct mddev_ident
*array_list
;
1553 array_list
= conf_get_ident(NULL
);
1556 for(; array_list
; array_list
= array_list
->next
) {
1558 if (array_list
->member
== NULL
||
1559 array_list
->container
== NULL
)
1561 if (strcmp(array_list
->member
, sub
) != 0)
1563 if (array_list
->uuid_set
&&
1564 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1566 dn
= container2devname(array_list
->container
);
1569 if (strncmp(dn
, ra
->text_version
+1,
1571 ra
->text_version
[strlen(dn
)+1] != '/') {
1576 /* we have a match */
1579 pr_err("match found for member %s\n",
1580 array_list
->member
);
1584 if (match
&& match
->devname
&&
1585 strcasecmp(match
->devname
, "<ignore>") == 0) {
1587 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1588 match
->container
, match
->member
);
1592 trustworthy
= LOCAL
;
1594 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1600 if (only
&& (!mp
|| strcmp(mp
->devnm
, only
) != 0))
1604 pr_err("failed to open %s: %s.\n",
1605 chosen_name
, strerror(errno
));
1609 assemble_container_content(st
, mdfd
, ra
, c
,
1610 chosen_name
, &result
);
1613 if (c
->export
&& result
) {
1615 printf("MD_STARTED");
1616 if (result
& INCR_NO
) {
1617 printf("%cno", sep
);
1620 if (result
& INCR_UNSAFE
) {
1621 printf("%cunsafe", sep
);
1624 if (result
& INCR_ALREADY
) {
1625 printf("%calready", sep
);
1628 if (result
& INCR_YES
) {
1629 printf("%cyes", sep
);
1635 /* don't move spares to container with volume being activated
1636 when all volumes are blocked */
1637 if (ra_all
== ra_blocked
)
1640 /* Now move all suitable spares from spare container */
1641 domains
= domain_from_array(list
, st
->ss
->name
);
1642 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1644 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1645 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1646 /* spare container found */
1647 struct supertype
*sst
=
1648 super_imsm
.match_metadata_desc("imsm");
1649 struct mdinfo
*sinfo
;
1650 unsigned long long min_size
= 0;
1651 if (st
->ss
->min_acceptable_spare_size
)
1652 min_size
= st
->ss
->min_acceptable_spare_size(st
);
1653 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1655 sinfo
= container_choose_spares(sst
, min_size
,
1658 sst
->ss
->free_super(sst
);
1661 struct mdinfo
*disks
= sinfo
->devs
;
1663 /* move spare from spare
1664 * container to currently
1670 makedev(disks
->disk
.major
,
1671 disks
->disk
.minor
)))
1673 disks
= disks
->next
;
1676 pr_err("Added %d spare%s to %s\n",
1677 count
, count
>1?"s":"", devname
);
1683 domain_free(domains
);
1687 static void run_udisks(char *arg1
, char *arg2
)
1692 execl("/usr/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1693 execl("/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1696 while (pid
> 0 && wait(&status
) != pid
)
1701 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1702 * raid arrays, and if so first fail (if needed) and then remove the device.
1704 * @devname - The device we want to remove
1705 * @id_path - name as found in /dev/disk/by-path for this device
1707 * Note: the device name must be a kernel name like "sda", so
1708 * that we can find it in /proc/mdstat
1710 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1714 struct mdstat_ent
*ent
;
1715 struct mddev_dev devlist
;
1720 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1722 if (strchr(devname
, '/')) {
1723 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname
);
1726 ent
= mdstat_by_component(devname
);
1729 pr_err("%s does not appear to be a component of any array\n", devname
);
1732 if (sysfs_init(&mdi
, -1, ent
->devnm
)) {
1733 pr_err("unable to initialize sysfs for: %s\n", devname
);
1736 mdfd
= open_dev_excl(ent
->devnm
);
1739 if (sysfs_get_str(&mdi
, NULL
, "array_state",
1740 buf
, sizeof(buf
)) > 0) {
1741 if (strncmp(buf
, "active", 6) == 0 ||
1742 strncmp(buf
, "clean", 5) == 0)
1743 sysfs_set_str(&mdi
, NULL
,
1744 "array_state", "read-auto");
1747 mdfd
= open_dev(ent
->devnm
);
1750 pr_err("Cannot open array %s!!\n", ent
->devnm
);
1756 struct map_ent
*map
= NULL
, *me
;
1757 me
= map_by_devnm(&map
, ent
->devnm
);
1759 policy_save_path(id_path
, me
);
1763 memset(&devlist
, 0, sizeof(devlist
));
1764 devlist
.devname
= devname
;
1765 devlist
.disposition
= 'f';
1766 /* for a container, we must fail each member array */
1767 if (ent
->metadata_version
&&
1768 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1769 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1770 struct mdstat_ent
*memb
;
1771 for (memb
= mdstat
; memb
; memb
= memb
->next
)
1772 if (is_container_member(memb
, ent
->devnm
)) {
1773 int subfd
= open_dev(memb
->devnm
);
1775 rv
|= Manage_subdevs(
1777 &devlist
, verbose
, 0,
1782 free_mdstat(mdstat
);
1784 rv
|= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1785 verbose
, 0, NULL
, 0);
1787 /* Failed due to EBUSY, try to stop the array.
1788 * Give udisks a chance to unmount it first.
1790 int devid
= devnm2devid(ent
->devnm
);
1791 run_udisks("--unmount", map_dev(major(devid
),minor(devid
), 0));
1792 rv
= Manage_stop(ent
->devnm
, mdfd
, verbose
, 1);
1794 /* At least we can try to trigger a 'remove' */
1795 sysfs_uevent(&mdi
, "remove");
1798 pr_err("Fail to stop %s too.\n", ent
->devnm
);
1801 devlist
.disposition
= 'r';
1802 rv
= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1803 verbose
, 0, NULL
, 0);