]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-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_is_blkdev(devname
, &rdev
))
112 dfd
= dev_open(devname
, O_RDONLY
);
115 pr_err("cannot open %s: %s.\n",
116 devname
, strerror(errno
));
119 /* If the device is a container, we do something very different */
120 if (must_be_container(dfd
)) {
122 st
= super_by_fd(dfd
, NULL
);
123 if (st
&& st
->ss
->load_container
)
124 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
127 if (!rv
&& st
->ss
->container_content
) {
129 pr_err("failed to get exclusive lock on mapfile\n");
131 printf("MD_DEVNAME=%s\n", devname
);
132 rv
= Incremental_container(st
, devname
, c
, NULL
);
137 pr_err("%s is not part of an md array.\n",
142 /* 1/ Check if device is permitted by mdadm.conf */
144 for (;devlist
; devlist
= devlist
->next
)
145 if (conf_test_dev(devlist
->devname
))
148 devlist
= conf_get_devs();
149 for (;devlist
; devlist
= devlist
->next
) {
150 if (stat_is_blkdev(devlist
->devname
, &rdev2
) &&
157 pr_err("%s not permitted by mdadm.conf.\n",
162 /* 2/ Find metadata, reject if none appropriate (check
163 * version/name from args) */
165 if (!fstat_is_blkdev(dfd
, devname
, &rdev
))
168 dinfo
.disk
.major
= major(rdev
);
169 dinfo
.disk
.minor
= minor(rdev
);
171 policy
= disk_policy(&dinfo
);
172 have_target
= policy_check_path(&dinfo
, &target_array
);
174 if (st
== NULL
&& (st
= guess_super_type(dfd
, guess_array
)) == NULL
) {
176 pr_err("no recognisable superblock on %s.\n",
178 rv
= try_spare(devname
, &dfd
, policy
,
179 have_target
? &target_array
: NULL
,
183 st
->ignore_hw_compat
= 0;
185 if (st
->ss
->compare_super
== NULL
||
186 st
->ss
->load_super(st
, dfd
, c
->verbose
>= 0 ? devname
: NULL
)) {
188 pr_err("no RAID superblock on %s.\n",
190 rv
= try_spare(devname
, &dfd
, policy
,
191 have_target
? &target_array
: NULL
,
196 close (dfd
); dfd
= -1;
198 st
->ss
->getinfo_super(st
, &info
, NULL
);
200 /* 3/ Check if there is a match in mdadm.conf */
201 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
202 if (!match
&& rv
== 2)
205 if (match
&& match
->devname
&&
206 strcasecmp(match
->devname
, "<ignore>") == 0) {
208 pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
213 /* 3a/ if not, check for homehost match. If no match, continue
214 * but don't trust the 'name' in the array. Thus a 'random' minor
215 * number will be assigned, and the device name will be based
219 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
221 else if (st
->ss
->match_home(st
, "any") == 1)
222 trustworthy
= LOCAL_ANY
;
224 trustworthy
= FOREIGN
;
226 if (!match
&& !conf_test_metadata(st
->ss
->name
, policy
,
227 (trustworthy
== LOCAL
))) {
229 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
230 devname
, st
->ss
->name
);
233 if (trustworthy
== LOCAL_ANY
)
236 /* There are three possible sources for 'autof': command line,
237 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
238 * ARRAY takes precedence, then command line, then
241 if (match
&& match
->autof
)
242 c
->autof
= match
->autof
;
244 c
->autof
= ci
->autof
;
246 name_to_use
= info
.name
;
247 if (name_to_use
[0] == 0 && info
.array
.level
== LEVEL_CONTAINER
) {
248 name_to_use
= info
.text_version
;
249 trustworthy
= METADATA
;
251 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
252 ! c
->require_homehost
&&
253 conf_name_is_free(name_to_use
))
256 /* strip "hostname:" prefix from name if we have decided
257 * to treat it as LOCAL
259 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
260 name_to_use
= strchr(name_to_use
, ':')+1;
262 /* 4/ Check if array exists.
265 pr_err("failed to get exclusive lock on mapfile\n");
266 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
269 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
272 pr_err("cannot reopen %s: %s.\n",
273 devname
, strerror(errno
));
276 /* Cannot hold it open while we add the device to the array,
277 * so we must release the O_EXCL and depend on the map_lock()
278 * So now is the best time to remove any partitions.
280 remove_partitions(dfd
);
284 mp
= map_by_uuid(&map
, info
.uuid
);
286 mdfd
= open_dev(mp
->devnm
);
292 /* Skip the clustered ones. This should be started by
293 * clustering resource agents
295 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
298 /* Couldn't find an existing array, maybe make a new one */
299 mdfd
= create_mddev(match
? match
->devname
: NULL
,
300 name_to_use
, c
->autof
, trustworthy
, chosen_name
, 0);
305 if (sysfs_init(&info
, mdfd
, NULL
)) {
306 pr_err("unable to initialize sysfs for %s\n",
312 if (set_array_info(mdfd
, st
, &info
) != 0) {
313 pr_err("failed to set array info for %s: %s\n",
314 chosen_name
, strerror(errno
));
320 dinfo
.disk
.major
= major(rdev
);
321 dinfo
.disk
.minor
= minor(rdev
);
322 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
323 pr_err("failed to add %s to new array %s: %s.\n",
324 devname
, chosen_name
, strerror(errno
));
325 ioctl(mdfd
, STOP_ARRAY
, 0);
329 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
330 GET_OFFSET
| GET_SIZE
));
332 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
333 /* It really should be 'none' - must be old buggy
334 * kernel, and mdadm -I may not be able to complete.
337 ioctl(mdfd
, STOP_ARRAY
, NULL
);
338 pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n");
342 info
.array
.working_disks
= 1;
343 /* 6/ Make sure /var/run/mdadm.map contains this array. */
344 map_update(&map
, fd2devnm(mdfd
),
346 info
.uuid
, chosen_name
);
349 /* - check one drive in array to make sure metadata is a reasonably */
350 /* close match. Reject if not (e.g. different type) */
351 /* - add the device */
355 struct supertype
*st2
;
356 struct mdinfo info2
, *d
;
358 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
359 GET_OFFSET
| GET_SIZE
));
362 strcpy(chosen_name
, mp
->path
);
364 strcpy(chosen_name
, mp
->devnm
);
366 /* It is generally not OK to add non-spare drives to a
367 * running array as they are probably missing because
368 * they failed. However if runstop is 1, then the
369 * array was possibly started early and our best bet is
370 * to add this anyway.
371 * Also if action policy is re-add or better we allow
373 * This doesn't apply to containers as the 'non-spare'
374 * flag has a different meaning. The test has to happen
375 * at the device level there
377 if (!st
->ss
->external
&&
378 (info
.disk
.state
& (1 << MD_DISK_SYNC
)) != 0 &&
379 !policy_action_allows(policy
, st
->ss
->name
, act_re_add
) &&
381 if (md_array_active(mdfd
)) {
382 pr_err("not adding %s to active array (without --run) %s\n",
383 devname
, chosen_name
);
393 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
394 sra
->devs
->disk
.minor
);
395 dfd2
= dev_open(dn
, O_RDONLY
);
397 pr_err("unable to open %s\n", devname
);
402 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
403 st
->ss
->compare_super(st
, st2
) != 0) {
404 pr_err("metadata mismatch between %s and chosen array %s\n",
405 devname
, chosen_name
);
411 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
412 st2
->ss
->free_super(st2
);
413 if (info
.array
.level
!= info2
.array
.level
||
414 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
415 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
416 pr_err("unexpected difference between %s and %s.\n",
417 chosen_name
, devname
);
422 info
.disk
.major
= major(rdev
);
423 info
.disk
.minor
= minor(rdev
);
424 /* add disk needs to know about containers */
425 if (st
->ss
->external
)
426 sra
->array
.level
= LEVEL_CONTAINER
;
428 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
429 info
.disk
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
431 err
= add_disk(mdfd
, st
, sra
, &info
);
432 if (err
< 0 && errno
== EBUSY
) {
433 /* could be another device present with the same
434 * disk.number. Find and reject any such
436 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
437 info
.events
, c
->verbose
, chosen_name
);
438 err
= add_disk(mdfd
, st
, sra
, &info
);
440 if (err
< 0 && errno
== EINVAL
&&
441 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
442 /* Maybe it needs to be added as a spare */
443 if (policy_action_allows(policy
, st
->ss
->name
,
445 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
446 err
= add_disk(mdfd
, st
, sra
, &info
);
449 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
450 devname
, chosen_name
);
453 pr_err("failed to add %s to existing array %s: %s.\n",
454 devname
, chosen_name
, strerror(errno
));
458 info
.array
.working_disks
= 0;
459 for (d
= sra
->devs
; d
; d
=d
->next
)
460 info
.array
.working_disks
++;
463 if (strncmp(chosen_name
, "/dev/md/", 8) == 0)
464 md_devname
= chosen_name
+8;
466 md_devname
= chosen_name
;
468 printf("MD_DEVICE=%s\n", fd2devnm(mdfd
));
469 printf("MD_DEVNAME=%s\n", md_devname
);
470 printf("MD_FOREIGN=%s\n", trustworthy
== FOREIGN
? "yes" : "no");
473 /* 7/ Is there enough devices to possibly start the array? */
474 /* 7a/ if not, finish with success. */
475 if (info
.array
.level
== LEVEL_CONTAINER
) {
477 /* Try to assemble within the container */
478 sysfs_uevent(sra
, "change");
479 if (!c
->export
&& c
->verbose
>= 0)
480 pr_err("container %s now has %d device%s\n",
481 chosen_name
, info
.array
.working_disks
,
482 info
.array
.working_disks
== 1?"":"s");
483 sysfs_rules_apply(chosen_name
, &info
);
484 wait_for(chosen_name
, mdfd
);
485 if (st
->ss
->external
)
486 strcpy(devnm
, fd2devnm(mdfd
));
487 if (st
->ss
->load_container
)
488 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
492 rv
= Incremental_container(st
, chosen_name
, c
, NULL
);
494 /* after spare is added, ping monitor for external metadata
495 * so that it can eg. try to rebuild degraded array */
496 if (st
->ss
->external
)
501 /* We have added something to the array, so need to re-read the
502 * state. Eventually this state should be kept up-to-date as
506 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
507 GET_OFFSET
| GET_SIZE
));
508 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
510 journal_device_missing
= (info
.journal_device_required
) && (info
.journal_clean
== 0);
512 if (info
.consistency_policy
== CONSISTENCY_POLICY_PPL
)
513 info
.array
.state
|= 1;
515 if (enough(info
.array
.level
, info
.array
.raid_disks
,
516 info
.array
.layout
, info
.array
.state
& 1, avail
) == 0) {
518 printf("MD_STARTED=no\n");
519 } else if (c
->verbose
>= 0)
520 pr_err("%s attached to %s, not enough to start (%d).\n",
521 devname
, chosen_name
, active_disks
);
527 /* - if number of OK devices match expected, or -R and there */
529 /* + add any bitmap file */
530 /* + start the array (auto-readonly). */
532 if (md_array_active(mdfd
)) {
534 printf("MD_STARTED=already\n");
535 } else if (c
->verbose
>= 0)
536 pr_err("%s attached to %s which is already active.\n",
537 devname
, chosen_name
);
543 if (c
->runstop
> 0 || (!journal_device_missing
&& active_disks
>= info
.array
.working_disks
)) {
545 /* Let's try to start it */
547 if (journal_device_missing
)
548 pr_err("Trying to run with missing journal device\n");
549 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
550 pr_err("%s: This array is being reshaped and cannot be started\n",
552 cont_err("by --incremental. Please use --assemble\n");
555 if (match
&& match
->bitmap_file
) {
556 int bmfd
= open(match
->bitmap_file
, O_RDWR
);
558 pr_err("Could not open bitmap file %s.\n",
562 if (ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
) != 0) {
564 pr_err("Failed to set bitmapfile for %s.\n",
570 /* Need to remove from the array any devices which
571 * 'count_active' discerned were too old or inappropriate
573 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
574 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
575 remove_disk(mdfd
, st
, sra
, d
);
577 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
) &&
578 trustworthy
!= FOREIGN
)
579 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
581 rv
= sysfs_set_str(sra
, NULL
,
582 "array_state", "read-auto");
583 /* Array might be O_EXCL which will interfere with
584 * fsck and mount. So re-open without O_EXCL.
589 printf("MD_STARTED=yes\n");
590 } else if (c
->verbose
>= 0)
591 pr_err("%s attached to %s, which has been started.\n",
592 devname
, chosen_name
);
594 wait_for(chosen_name
, mdfd
);
595 /* We just started the array, so some devices
596 * might have been evicted from the array
597 * because their event counts were too old.
598 * If the action=re-add policy is in-force for
599 * those devices we should re-add them now.
601 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
602 if (disk_action_allows(dsk
, st
->ss
->name
,
604 add_disk(mdfd
, st
, sra
, dsk
) == 0)
605 pr_err("%s re-added to %s\n",
606 dsk
->sys_name
, chosen_name
);
609 pr_err("%s attached to %s, but failed to start: %s.\n",
610 devname
, chosen_name
, strerror(errno
));
615 printf("MD_STARTED=unsafe\n");
616 } else if (journal_device_missing
) {
617 pr_err("Journal device is missing, not safe to start yet.\n");
618 } else if (c
->verbose
>= 0)
619 pr_err("%s attached to %s, not enough to start safely.\n",
620 devname
, chosen_name
);
630 dev_policy_free(policy
);
638 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
639 int number
, __u64 events
, int verbose
,
642 /* Find a device attached to this array with a disk.number of number
643 * and events less than the passed events, and remove the device.
647 if (md_array_active(mdfd
))
648 return; /* not safe to remove from active arrays
649 * without thinking more */
651 for (d
= sra
->devs
; d
; d
= d
->next
) {
652 char dn
[24]; // 2*11 bytes for ints (including sign) + colon + null byte
655 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
656 dfd
= dev_open(dn
, O_RDONLY
);
659 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
663 st
->ss
->getinfo_super(st
, &info
, NULL
);
664 st
->ss
->free_super(st
);
667 if (info
.disk
.number
!= number
|| info
.events
>= events
)
670 if (d
->disk
.raid_disk
> -1)
671 sysfs_set_str(sra
, d
, "slot", "none");
672 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
674 pr_err("removing old device %s from %s\n",
675 d
->sys_name
+4, array_name
);
679 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
680 int mdfd
, char **availp
,
681 struct mdinfo
*bestinfo
)
683 /* count how many devices in sra think they are active */
687 __u64 max_events
= 0;
688 __u64 max_journal_events
= 0;
700 for (d
= sra
->devs
; d
; d
= d
->next
)
702 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
708 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
709 dfd
= dev_open(dn
, O_RDONLY
);
712 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
717 info
.array
.raid_disks
= raid_disks
;
718 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
719 if (info
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
&&
720 info
.events
> max_journal_events
)
721 max_journal_events
= info
.events
;
723 raid_disks
= info
.array
.raid_disks
;
724 avail
= xcalloc(raid_disks
, 1);
727 best
= xcalloc(raid_disks
, sizeof(int));
728 devmap
= xcalloc(raid_disks
, numdevs
);
730 st
->ss
->getinfo_super(st
, &info
, devmap
);
733 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
737 max_events
= info
.events
;
738 avail
[info
.disk
.raid_disk
] = 2;
739 best
[info
.disk
.raid_disk
] = devnum
;
740 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
741 } else if (info
.events
== max_events
) {
742 avail
[info
.disk
.raid_disk
] = 2;
743 best
[info
.disk
.raid_disk
] = devnum
;
744 } else if (info
.events
== max_events
-1) {
745 if (avail
[info
.disk
.raid_disk
] == 0) {
746 avail
[info
.disk
.raid_disk
] = 1;
747 best
[info
.disk
.raid_disk
] = devnum
;
749 } else if (info
.events
< max_events
- 1)
751 else if (info
.events
== max_events
+1) {
753 max_events
= info
.events
;
754 for (i
= 0; i
< raid_disks
; i
++)
757 avail
[info
.disk
.raid_disk
] = 2;
758 best
[info
.disk
.raid_disk
] = devnum
;
759 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
760 } else { /* info.events much bigger */
761 memset(avail
, 0, raid_disks
);
762 max_events
= info
.events
;
763 avail
[info
.disk
.raid_disk
] = 2;
764 best
[info
.disk
.raid_disk
] = devnum
;
765 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
767 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
769 st
->ss
->free_super(st
);
771 if (max_journal_events
>= max_events
- 1)
772 bestinfo
->journal_clean
= 1;
776 /* We need to reject any device that thinks the best device is
777 * failed or missing */
778 for (b
= 0; b
< raid_disks
; b
++)
782 for (i
= 0 ; i
< raid_disks
; i
++) {
783 if (i
!= b
&& avail
[i
])
784 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
785 /* This device thinks 'b' is failed -
788 for (d
=sra
->devs
; devnum
; d
= d
->next
)
790 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
796 /* Also need to reject any spare device with an event count that
799 for (d
= sra
->devs
; d
; d
= d
->next
) {
800 if (!(d
->disk
.state
& (1<<MD_DISK_SYNC
)) &&
801 d
->events
> max_events
)
802 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
806 return cnt
+ replcnt
;
809 /* test if container has degraded member(s) */
811 container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
814 int degraded
, max_degraded
= 0;
816 for(; map
; map
= map
->next
) {
817 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
819 /* most accurate information regarding array degradation */
820 sra
= sysfs_read(-1, map
->devnm
,
821 GET_DISKS
| GET_DEVS
| GET_STATE
);
824 degraded
= sra
->array
.raid_disks
- sra
->array
.active_disks
-
825 sra
->array
.spare_disks
;
826 if (degraded
> max_degraded
)
827 max_degraded
= degraded
;
834 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
835 struct map_ent
*target
, int bare
,
836 struct supertype
*st
, int verbose
)
838 /* This device doesn't have any md metadata
839 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
840 * If 'st' is not set, then we only know that some metadata allows this,
841 * others possibly don't.
842 * So look for a container or array to attach the device to.
843 * Prefer 'target' if that is set and the array is found.
845 * If st is set, then only arrays of that type are considered
846 * Return 0 on success, or some exit code on failure, probably 1.
850 struct map_ent
*mp
, *map
= NULL
;
851 struct mdinfo
*chosen
= NULL
;
854 if (!fstat_is_blkdev(dfd
, devname
, &rdev
))
858 * Now we need to find a suitable array to add this to.
859 * We only accept arrays that:
861 * - are in the same domains as the device
862 * - are of an size for which the device will be useful
863 * and we choose the one that is the most degraded
866 if (map_lock(&map
)) {
867 pr_err("failed to get exclusive lock on mapfile\n");
870 for (mp
= map
; mp
; mp
= mp
->next
) {
871 struct supertype
*st2
;
872 struct domainlist
*dl
= NULL
;
874 unsigned long long devsize
, freesize
= 0;
875 struct spare_criteria sc
= {0, 0};
877 if (is_subarray(mp
->metadata
))
880 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
882 (st
->minor_version
>= 0 &&
883 st
->minor_version
!= st2
->minor_version
)) {
885 pr_err("not adding %s to %s as metadata type doesn't match\n",
892 sra
= sysfs_read(-1, mp
->devnm
,
893 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
894 GET_COMPONENT
|GET_VERSION
);
896 sra
->array
.failed_disks
= -1;
902 for(i
= 0; !st2
&& superlist
[i
]; i
++)
903 st2
= superlist
[i
]->match_metadata_desc(
907 pr_err("not adding %s to %s as metadata not recognised.\n",
911 /* Need to double check the 'act_spare' permissions applies
914 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
916 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
917 act_spare_same_slot
))
921 /* update number of failed disks for mostly degraded
922 * container member */
923 if (sra
->array
.failed_disks
== -1)
924 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
926 get_dev_size(dfd
, NULL
, &devsize
);
927 if (sra
->component_size
== 0) {
928 /* true for containers, here we must read superblock
929 * to obtain minimum spare size */
930 struct supertype
*st3
= dup_super(st2
);
931 int mdfd
= open_dev(mp
->devnm
);
936 if (st3
->ss
->load_container
&&
937 !st3
->ss
->load_container(st3
, mdfd
, mp
->path
)) {
938 if (st3
->ss
->get_spare_criteria
)
939 st3
->ss
->get_spare_criteria(st3
, &sc
);
940 st3
->ss
->free_super(st3
);
945 if ((sra
->component_size
> 0 &&
946 st2
->ss
->validate_geometry(st2
, sra
->array
.level
, sra
->array
.layout
,
947 sra
->array
.raid_disks
, &sra
->array
.chunk_size
,
949 sra
->devs
? sra
->devs
->data_offset
: INVALID_SECTORS
,
950 devname
, &freesize
, sra
->consistency_policy
,
952 freesize
< sra
->component_size
) ||
953 (sra
->component_size
== 0 && devsize
< sc
.min_size
)) {
955 pr_err("not adding %s to %s as it is too small\n",
959 /* test against target.
960 * If 'target' is set and 'bare' is false, we only accept
961 * arrays/containers that match 'target'.
962 * If 'target' is set and 'bare' is true, we prefer the
963 * array which matches 'target'.
964 * target is considered only if we deal with degraded array
966 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
967 act_spare_same_slot
)) {
968 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
969 memcmp(target
->uuid
, mp
->uuid
,
970 sizeof(target
->uuid
)) == 0 &&
971 sra
->array
.failed_disks
> 0) {
972 /* This is our target!! */
976 /* skip to end so we don't check any more */
986 dl
= domain_from_array(sra
, st2
->ss
->name
);
987 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
988 /* domain test fails */
990 pr_err("not adding %s to %s as it is not in a compatible domain\n",
995 /* all tests passed, OK to add to this array */
999 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1012 /* add current device to chosen array as a spare */
1013 int mdfd
= open_dev(chosen
->sys_name
);
1015 struct mddev_dev devlist
;
1016 char chosen_devname
[24]; // 2*11 for int (including signs) + colon + null
1017 devlist
.next
= NULL
;
1019 devlist
.writemostly
= FlagDefault
;
1020 devlist
.failfast
= FlagDefault
;
1021 devlist
.devname
= chosen_devname
;
1022 sprintf(chosen_devname
, "%d:%d", major(rdev
),
1024 devlist
.disposition
= 'a';
1027 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1033 pr_err("added %s as spare for %s\n",
1034 devname
, chosen
->sys_name
);
1036 pr_err("failed to add %s as spare for %s\n",
1037 devname
, chosen
->sys_name
);
1045 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1046 struct supertype
*st
, int verbose
)
1048 /* we know that at least one partition virtual-metadata is
1049 * allowed to incorporate spares like this device. We need to
1050 * find a suitable device to copy partition information from.
1052 * Getting a list of all disk (not partition) devices is
1053 * slightly non-trivial. We could look at /sys/block, but
1054 * that is theoretically due to be removed. Maybe best to use
1055 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1056 * we depend on this directory of 'path' info. But that fails
1057 * to find loop devices and probably others. Maybe don't
1058 * worry about that, they aren't the real target.
1060 * So: check things in /dev/disk/by-path to see if they are in
1061 * a compatible domain, then load the partition table and see
1062 * if it is OK for the new device, and choose the largest
1063 * partition table that fits.
1067 char *chosen
= NULL
;
1068 unsigned long long chosen_size
= 0;
1069 struct supertype
*chosen_st
= NULL
;
1072 dir
= opendir("/dev/disk/by-path");
1075 while ((de
= readdir(dir
)) != NULL
) {
1077 struct dev_policy
*pol2
= NULL
;
1078 struct domainlist
*domlist
= NULL
;
1081 struct supertype
*st2
= NULL
;
1082 char *devname
= NULL
;
1083 unsigned long long devsectors
;
1086 if (de
->d_ino
== 0 || de
->d_name
[0] == '.' ||
1087 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1090 ep
= de
->d_name
+ strlen(de
->d_name
);
1091 while (ep
> de
->d_name
&&
1094 if (ep
> de
->d_name
+ 5 &&
1095 strncmp(ep
-5, "-part", 5) == 0)
1096 /* This is a partition - skip it */
1099 pathlist
[0] = de
->d_name
;
1101 pol2
= path_policy(pathlist
, type_disk
);
1103 domain_merge(&domlist
, pol2
, st
? st
->ss
->name
: NULL
);
1104 if (domain_test(domlist
, pol
, st
? st
->ss
->name
: NULL
) != 1)
1105 /* new device is incompatible with this device. */
1108 domain_free(domlist
);
1111 if (asprintf(&devname
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1115 fd
= open(devname
, O_RDONLY
);
1118 if (get_dev_size(fd
, devname
, &devsectors
) == 0)
1123 st2
= dup_super(st
);
1125 st2
= guess_super_type(fd
, guess_partitions
);
1126 if (st2
== NULL
|| st2
->ss
->load_super(st2
, fd
, NULL
) < 0)
1128 st2
->ignore_hw_compat
= 0;
1131 /* Check domain policy again, this time referring to metadata */
1132 domain_merge(&domlist
, pol2
, st2
->ss
->name
);
1133 if (domain_test(domlist
, pol
, st2
->ss
->name
) != 1)
1134 /* Incompatible devices for this metadata type */
1136 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
1137 /* Some partition types allow sparing, but not
1143 st2
->ss
->getinfo_super(st2
, &info
, NULL
);
1144 if (info
.component_size
> devsectors
)
1145 /* This partitioning doesn't fit in the device */
1148 /* This is an acceptable device to copy partition
1149 * metadata from. We could just stop here, but I
1150 * think I want to keep looking incase a larger
1151 * metadata which makes better use of the device can
1154 if (chosen
== NULL
|| chosen_size
< info
.component_size
) {
1155 chosen_size
= info
.component_size
;
1160 chosen_st
->ss
->free_super(chosen_st
);
1169 domain_free(domlist
);
1170 dev_policy_free(pol2
);
1172 st2
->ss
->free_super(st2
);
1184 /* 'chosen' is the best device we can find. Let's write its
1185 * metadata to devname dfd is read-only so don't use that
1187 fd
= open(devname
, O_RDWR
);
1189 chosen_st
->ss
->store_super(chosen_st
, fd
);
1193 chosen_st
->ss
->free_super(chosen_st
);
1198 static int is_bare(int dfd
)
1200 unsigned long long size
= 0;
1201 char bufpad
[4096 + 4096];
1202 char *buf
= (char*)(((long)bufpad
+ 4096) & ~4095);
1204 if (lseek(dfd
, 0, SEEK_SET
) != 0 ||
1205 read(dfd
, buf
, 4096) != 4096)
1208 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1210 if (memcmp(buf
, buf
+1, 4095) != 0)
1213 /* OK, first 4K appear blank, try the end. */
1214 get_dev_size(dfd
, NULL
, &size
);
1215 if (lseek(dfd
, size
-4096, SEEK_SET
) < 0 ||
1216 read(dfd
, buf
, 4096) != 4096)
1219 if (buf
[0] != '\0' && buf
[0] != '\x5a' && buf
[0] != '\xff')
1221 if (memcmp(buf
, buf
+1, 4095) != 0)
1227 /* adding a spare to a regular array is quite different from adding one to
1228 * a set-of-partitions virtual array.
1229 * This function determines which is worth trying and tries as appropriate.
1230 * Arrays are given priority over partitions.
1232 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1233 struct map_ent
*target
,
1234 struct supertype
*st
, int verbose
)
1239 int partitions_ok
= 0;
1243 /* Can only add a spare if device has at least one domain */
1244 if (pol_find(pol
, pol_domain
) == NULL
)
1246 /* And only if some action allows spares */
1247 if (!policy_action_allows(pol
, st
?st
->ss
->name
:NULL
, act_spare
))
1250 /* Now check if the device is bare.
1251 * bare devices can always be added as a spare
1252 * non-bare devices can only be added if spare-same-slot is permitted,
1253 * and this device is replacing a previous device - in which case 'target'
1256 if (!is_bare(dfd
)) {
1257 /* Must have a target and allow same_slot */
1258 /* Later - may allow force_spare without target */
1260 !policy_action_allows(pol
, st
?st
->ss
->name
:NULL
,
1261 act_spare_same_slot
)) {
1263 pr_err("%s is not bare, so not considering as a spare\n",
1271 /* It might be OK to add this device to an array - need to see
1272 * what arrays might be candidates.
1275 /* just try to add 'array' or 'partition' based on this metadata */
1276 if (st
->ss
->add_to_super
)
1277 return array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1280 return partition_try_spare(devname
, dfdp
, pol
,
1283 /* No metadata was specified or found so options are open.
1284 * Check for whether any array metadata, or any partition metadata
1285 * might allow adding the spare. This check is just help to avoid
1286 * a more costly scan of all arrays when we can be sure that will
1289 for (i
= 0; (!arrays_ok
|| !partitions_ok
) && superlist
[i
] ; i
++) {
1290 if (superlist
[i
]->add_to_super
&& !arrays_ok
&&
1291 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1293 if (superlist
[i
]->add_to_super
== NULL
&& !partitions_ok
&&
1294 policy_action_allows(pol
, superlist
[i
]->name
, act_spare
))
1299 rv
= array_try_spare(devname
, dfdp
, pol
, target
, bare
,
1301 if (rv
!= 0 && partitions_ok
)
1302 rv
= partition_try_spare(devname
, dfdp
, pol
, st
, verbose
);
1306 int IncrementalScan(struct context
*c
, char *devnm
)
1308 /* look at every device listed in the 'map' file.
1309 * If one is found that is not running then:
1310 * look in mdadm.conf for bitmap file.
1311 * if one exists, but array has none, add it.
1312 * try to start array in auto-readonly mode
1314 struct map_ent
*mapl
= NULL
;
1316 struct mddev_ident
*devs
, *mddev
;
1322 devs
= conf_get_ident(NULL
);
1325 for (me
= mapl
; me
; me
= me
->next
) {
1329 if (devnm
&& strcmp(devnm
, me
->devnm
) != 0)
1331 if (me
->metadata
[0] == '/') {
1337 /* member array, need to work on container */
1338 strncpy(container
, me
->metadata
+1, 32);
1340 sl
= strchr(container
, '/');
1347 mdfd
= open_dev(me
->devnm
);
1351 if (!isdigit(me
->metadata
[0])) {
1352 /* must be a container */
1353 struct supertype
*st
= super_by_fd(mdfd
, NULL
);
1355 struct map_ent
*map
= NULL
;
1357 if (st
&& st
->ss
->load_container
)
1358 ret
= st
->ss
->load_container(st
, mdfd
, NULL
);
1360 if (!ret
&& st
&& st
->ss
->container_content
) {
1362 pr_err("failed to get exclusive lock on mapfile\n");
1363 ret
= Incremental_container(st
, me
->path
, c
, only
);
1370 if (md_array_active(mdfd
)) {
1374 /* Ok, we can try this one. Maybe it needs a bitmap */
1375 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1376 if (mddev
->devname
&& me
->path
&&
1377 devname_matches(mddev
->devname
, me
->path
))
1379 if (mddev
&& mddev
->bitmap_file
) {
1381 * Note: early kernels will wrongly fail this, so it
1387 bmfd
= open(mddev
->bitmap_file
, O_RDWR
);
1389 added
= ioctl(mdfd
, SET_BITMAP_FILE
, bmfd
);
1392 if (c
->verbose
>= 0) {
1394 pr_err("Added bitmap %s to %s\n",
1395 mddev
->bitmap_file
, me
->path
);
1396 else if (errno
!= EEXIST
)
1397 pr_err("Failed to add bitmap to %s: %s\n",
1398 me
->path
, strerror(errno
));
1401 /* FIXME check for reshape_active and consider not
1404 sra
= sysfs_read(mdfd
, NULL
, 0);
1406 if (sysfs_set_str(sra
, NULL
,
1407 "array_state", "read-auto") == 0) {
1408 if (c
->verbose
>= 0)
1409 pr_err("started array %s\n",
1410 me
->path
?: me
->devnm
);
1412 pr_err("failed to start array %s: %s\n",
1413 me
->path
?: me
->devnm
,
1424 static char *container2devname(char *devname
)
1426 char *mdname
= NULL
;
1428 if (devname
[0] == '/') {
1429 int fd
= open(devname
, O_RDONLY
);
1431 mdname
= xstrdup(fd2devnm(fd
));
1436 struct map_ent
*mp
, *map
= NULL
;
1438 if (!parse_uuid(devname
, uuid
))
1440 mp
= map_by_uuid(&map
, uuid
);
1442 mdname
= xstrdup(mp
->devnm
);
1449 static int Incremental_container(struct supertype
*st
, char *devname
,
1450 struct context
*c
, char *only
)
1452 /* Collect the contents of this container and for each
1453 * array, choose a device name and assemble the array.
1456 struct mdinfo
*list
;
1458 struct map_ent
*map
= NULL
;
1461 struct mddev_ident
*match
;
1463 struct domainlist
*domains
;
1464 struct map_ent
*smp
;
1471 st
->ss
->getinfo_super(st
, &info
, NULL
);
1473 if ((c
->runstop
> 0 && info
.container_enough
>= 0) ||
1474 info
.container_enough
> 0)
1478 printf("MD_STARTED=no\n");
1479 } else if (c
->verbose
)
1480 pr_err("not enough devices to start the container\n");
1484 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1485 if (match
== NULL
&& rv
== 2)
1488 /* Need to compute 'trustworthy' */
1490 trustworthy
= LOCAL
;
1491 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1492 trustworthy
= LOCAL
;
1493 else if (st
->ss
->match_home(st
, "any") == 1)
1494 trustworthy
= LOCAL
;
1496 trustworthy
= FOREIGN
;
1498 list
= st
->ss
->container_content(st
, NULL
);
1499 /* when nothing to activate - quit */
1502 printf("MD_STARTED=nothing\n");
1506 for (ra
= list
; ra
; ra
= ra
->next
) {
1508 char chosen_name
[1024];
1510 struct mddev_ident
*match
= NULL
;
1513 /* do not activate arrays blocked by metadata handler */
1514 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1515 pr_err("Cannot activate array %s in %s.\n",
1516 ra
->text_version
, devname
);
1520 mp
= map_by_uuid(&map
, ra
->uuid
);
1523 mdfd
= open_dev(mp
->devnm
);
1525 strcpy(chosen_name
, mp
->path
);
1527 strcpy(chosen_name
, mp
->devnm
);
1530 /* Check in mdadm.conf for container == devname and
1531 * member == ra->text_version after second slash.
1533 char *sub
= strchr(ra
->text_version
+1, '/');
1534 struct mddev_ident
*array_list
;
1537 array_list
= conf_get_ident(NULL
);
1540 for(; array_list
; array_list
= array_list
->next
) {
1542 if (array_list
->member
== NULL
||
1543 array_list
->container
== NULL
)
1545 if (strcmp(array_list
->member
, sub
) != 0)
1547 if (array_list
->uuid_set
&&
1548 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1550 dn
= container2devname(array_list
->container
);
1553 if (strncmp(dn
, ra
->text_version
+1,
1555 ra
->text_version
[strlen(dn
)+1] != '/') {
1560 /* we have a match */
1563 pr_err("match found for member %s\n",
1564 array_list
->member
);
1568 if (match
&& match
->devname
&&
1569 strcasecmp(match
->devname
, "<ignore>") == 0) {
1571 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1572 match
->container
, match
->member
);
1576 trustworthy
= LOCAL
;
1578 mdfd
= create_mddev(match
? match
->devname
: NULL
,
1584 if (only
&& (!mp
|| strcmp(mp
->devnm
, only
) != 0))
1588 pr_err("failed to open %s: %s.\n",
1589 chosen_name
, strerror(errno
));
1593 assemble_container_content(st
, mdfd
, ra
, c
,
1594 chosen_name
, &result
);
1599 if (c
->export
&& result
) {
1601 printf("MD_STARTED");
1602 if (result
& INCR_NO
) {
1603 printf("%cno", sep
);
1606 if (result
& INCR_UNSAFE
) {
1607 printf("%cunsafe", sep
);
1610 if (result
& INCR_ALREADY
) {
1611 printf("%calready", sep
);
1614 if (result
& INCR_YES
) {
1615 printf("%cyes", sep
);
1621 /* don't move spares to container with volume being activated
1622 when all volumes are blocked */
1623 if (ra_all
== ra_blocked
)
1626 /* Now move all suitable spares from spare container */
1627 domains
= domain_from_array(list
, st
->ss
->name
);
1628 memcpy(suuid
, uuid_zero
, sizeof(int[4]));
1630 (smp
= map_by_uuid(&map
, suuid
)) != NULL
&&
1631 (sfd
= open(smp
->path
, O_RDONLY
)) >= 0) {
1632 /* spare container found */
1633 struct supertype
*sst
=
1634 super_imsm
.match_metadata_desc("imsm");
1635 struct mdinfo
*sinfo
;
1637 if (!sst
->ss
->load_container(sst
, sfd
, NULL
)) {
1638 struct spare_criteria sc
= {0, 0};
1640 if (st
->ss
->get_spare_criteria
)
1641 st
->ss
->get_spare_criteria(st
, &sc
);
1644 sinfo
= container_choose_spares(sst
, &sc
,
1647 sst
->ss
->free_super(sst
);
1650 struct mdinfo
*disks
= sinfo
->devs
;
1652 /* move spare from spare
1653 * container to currently
1659 makedev(disks
->disk
.major
,
1660 disks
->disk
.minor
)))
1662 disks
= disks
->next
;
1665 pr_err("Added %d spare%s to %s\n",
1666 count
, count
>1?"s":"", devname
);
1672 domain_free(domains
);
1677 static void run_udisks(char *arg1
, char *arg2
)
1682 execl("/usr/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1683 execl("/bin/udisks", "udisks", arg1
, arg2
, NULL
);
1686 while (pid
> 0 && wait(&status
) != pid
)
1690 static int force_remove(char *devnm
, int fd
, struct mdinfo
*mdi
, int verbose
)
1693 int devid
= devnm2devid(devnm
);
1695 run_udisks("--unmount", map_dev(major(devid
), minor(devid
), 0));
1696 rv
= Manage_stop(devnm
, fd
, verbose
, 1);
1698 /* At least we can try to trigger a 'remove' */
1699 sysfs_uevent(mdi
, "remove");
1701 pr_err("Fail to stop %s too.\n", devnm
);
1706 static void remove_from_member_array(struct mdstat_ent
*memb
,
1707 struct mddev_dev
*devlist
, int verbose
)
1711 int subfd
= open_dev(memb
->devnm
);
1714 rv
= Manage_subdevs(memb
->devnm
, subfd
, devlist
, verbose
,
1717 if (sysfs_init(&mmdi
, -1, memb
->devnm
))
1718 pr_err("unable to initialize sysfs for: %s\n",
1721 force_remove(memb
->devnm
, subfd
, &mmdi
,
1729 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1730 * raid arrays, and if so first fail (if needed) and then remove the device.
1732 * @devname - The device we want to remove
1733 * @id_path - name as found in /dev/disk/by-path for this device
1735 * Note: the device name must be a kernel name like "sda", so
1736 * that we can find it in /proc/mdstat
1738 int IncrementalRemove(char *devname
, char *id_path
, int verbose
)
1742 struct mdstat_ent
*ent
;
1743 struct mddev_dev devlist
;
1748 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1750 if (strchr(devname
, '/')) {
1751 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname
);
1754 ent
= mdstat_by_component(devname
);
1757 pr_err("%s does not appear to be a component of any array\n", devname
);
1760 if (sysfs_init(&mdi
, -1, ent
->devnm
)) {
1761 pr_err("unable to initialize sysfs for: %s\n", devname
);
1764 mdfd
= open_dev_excl(ent
->devnm
);
1767 if (sysfs_get_str(&mdi
, NULL
, "array_state",
1768 buf
, sizeof(buf
)) > 0) {
1769 if (strncmp(buf
, "active", 6) == 0 ||
1770 strncmp(buf
, "clean", 5) == 0)
1771 sysfs_set_str(&mdi
, NULL
,
1772 "array_state", "read-auto");
1775 mdfd
= open_dev(ent
->devnm
);
1778 pr_err("Cannot open array %s!!\n", ent
->devnm
);
1784 struct map_ent
*map
= NULL
, *me
;
1785 me
= map_by_devnm(&map
, ent
->devnm
);
1787 policy_save_path(id_path
, me
);
1791 memset(&devlist
, 0, sizeof(devlist
));
1792 devlist
.devname
= devname
;
1793 devlist
.disposition
= 'f';
1794 /* for a container, we must fail each member array */
1795 if (ent
->metadata_version
&&
1796 strncmp(ent
->metadata_version
, "external:", 9) == 0) {
1797 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1798 struct mdstat_ent
*memb
;
1799 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
1800 if (is_container_member(memb
, ent
->devnm
))
1801 remove_from_member_array(memb
,
1804 free_mdstat(mdstat
);
1806 rv
|= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1807 verbose
, 0, NULL
, 0);
1809 /* Failed due to EBUSY, try to stop the array.
1810 * Give udisks a chance to unmount it first.
1812 rv
= force_remove(ent
->devnm
, mdfd
, &mdi
, verbose
);
1817 devlist
.disposition
= 'r';
1818 rv
= Manage_subdevs(ent
->devnm
, mdfd
, &devlist
,
1819 verbose
, 0, NULL
, 0);