]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
ba3810e6157faaf64ccea63253735883c119080f
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
39 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
40 int mdfd
, char **availp
,
42 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
43 int number
, __u64 events
, int verbose
,
45 static int try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
46 struct map_ent
*target
,
47 struct supertype
*st
, int verbose
);
49 static int Incremental_container(struct supertype
*st
, char *devname
,
50 struct context
*c
, char *only
);
52 int Incremental(struct mddev_dev
*devlist
, struct context
*c
,
55 /* Add this device to an array, creating the array if necessary
56 * and starting the array if sensible or - if runstop>0 - if possible.
58 * This has several steps:
60 * 1/ Check if device is permitted by mdadm.conf, reject if not.
61 * 2/ Find metadata, reject if none appropriate (check
62 * version/name from args)
63 * 3/ Check if there is a match in mdadm.conf
64 * 3a/ if not, check for homehost match. If no match, assemble as
66 * 4/ Determine device number.
67 * - If in mdadm.conf with std name, use that
68 * - UUID in /var/run/mdadm.map use that
69 * - If name is suggestive, use that. unless in use with different uuid.
70 * - Choose a free, high number.
71 * - Use a partitioned device unless strong suggestion not to.
73 * Don't choose partitioned for containers.
74 * 5/ Find out if array already exists
76 * - choose a name, from mdadm.conf or 'name' field in array.
80 * - check one drive in array to make sure metadata is a reasonably
81 * close match. Reject if not (e.g. different type)
83 * 6/ Make sure /var/run/mdadm.map contains this array.
84 * 7/ Is there enough devices to possibly start the array?
85 * For a container, this means running Incremental_container.
86 * 7a/ if not, finish with success.
88 * - read all metadata and arrange devices like -A does
89 * - if number of OK devices match expected, or -R and there are enough,
90 * start the array (auto-readonly).
93 struct mdinfo info
, dinfo
;
94 struct mdinfo
*sra
= NULL
, *d
;
95 struct mddev_ident
*match
;
96 char chosen_name
[1024];
99 struct map_ent
*mp
, *map
= NULL
;
100 int dfd
= -1, mdfd
= -1;
105 struct dev_policy
*policy
= NULL
;
106 struct map_ent target_array
;
108 char *devname
= devlist
->devname
;
109 int journal_device_missing
= 0;
111 if (!stat_is_blkdev(devname
, &rdev
))
113 dfd
= dev_open(devname
, O_RDONLY
);
116 pr_err("cannot open %s: %s.\n",
117 devname
, strerror(errno
));
120 /* If the device is a container, we do something very different */
121 if (must_be_container(dfd
)) {
123 st
= super_by_fd(dfd
, NULL
);
124 if (st
&& st
->ss
->load_container
)
125 rv
= st
->ss
->load_container(st
, dfd
, NULL
);
128 if (!rv
&& st
->ss
->container_content
) {
130 pr_err("failed to get exclusive lock on mapfile\n");
132 printf("MD_DEVNAME=%s\n", devname
);
133 rv
= Incremental_container(st
, devname
, c
, NULL
);
138 pr_err("%s is not part of an md array.\n",
143 /* 1/ Check if device is permitted by mdadm.conf */
145 for (;devlist
; devlist
= devlist
->next
)
146 if (conf_test_dev(devlist
->devname
))
149 devlist
= conf_get_devs();
150 for (;devlist
; devlist
= devlist
->next
) {
151 if (stat_is_blkdev(devlist
->devname
, &rdev2
) &&
158 pr_err("%s not permitted by mdadm.conf.\n",
163 /* 2/ Find metadata, reject if none appropriate (check
164 * version/name from args) */
166 if (!fstat_is_blkdev(dfd
, devname
, &rdev
))
169 dinfo
.disk
.major
= major(rdev
);
170 dinfo
.disk
.minor
= minor(rdev
);
172 policy
= disk_policy(&dinfo
);
173 have_target
= policy_check_path(&dinfo
, &target_array
);
175 if (st
== NULL
&& (st
= guess_super_type(dfd
, guess_array
)) == NULL
) {
177 pr_err("no recognisable superblock on %s.\n",
179 rv
= try_spare(devname
, &dfd
, policy
,
180 have_target
? &target_array
: NULL
,
184 st
->ignore_hw_compat
= 0;
186 if (st
->ss
->compare_super
== NULL
||
187 st
->ss
->load_super(st
, dfd
, c
->verbose
>= 0 ? devname
: NULL
)) {
189 pr_err("no RAID superblock on %s.\n",
191 rv
= try_spare(devname
, &dfd
, policy
,
192 have_target
? &target_array
: NULL
,
197 close (dfd
); dfd
= -1;
199 st
->ss
->getinfo_super(st
, &info
, NULL
);
201 /* 3/ Check if there is a match in mdadm.conf */
202 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
203 if (!match
&& rv
== 2)
206 if (match
&& match
->devname
&& is_devname_ignore(match
->devname
) == true) {
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 name_to_use
= info
.name
;
237 if (name_to_use
[0] == 0 && is_container(info
.array
.level
)) {
238 name_to_use
= info
.text_version
;
239 trustworthy
= METADATA
;
241 if (name_to_use
[0] && trustworthy
!= LOCAL
&&
242 ! c
->require_homehost
&&
243 conf_name_is_free(name_to_use
))
246 /* strip "hostname:" prefix from name if we have decided
247 * to treat it as LOCAL
249 if (trustworthy
== LOCAL
&& strchr(name_to_use
, ':') != NULL
)
250 name_to_use
= strchr(name_to_use
, ':')+1;
252 /* 4/ Check if array exists.
255 pr_err("failed to get exclusive lock on mapfile\n");
256 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
259 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
262 pr_err("cannot reopen %s: %s.\n",
263 devname
, strerror(errno
));
266 /* Cannot hold it open while we add the device to the array,
267 * so we must release the O_EXCL and depend on the map_lock()
268 * So now is the best time to remove any partitions.
270 remove_partitions(dfd
);
274 mp
= map_by_uuid(&map
, info
.uuid
);
276 mdfd
= open_dev(mp
->devnm
);
282 /* Skip the clustered ones. This should be started by
283 * clustering resource agents
285 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
288 /* Couldn't find an existing array, maybe make a new one */
289 mdfd
= create_mddev(match
? match
->devname
: NULL
, name_to_use
, trustworthy
,
295 if (sysfs_init(&info
, mdfd
, NULL
)) {
296 pr_err("unable to initialize sysfs for %s\n",
302 if (set_array_info(mdfd
, st
, &info
) != 0) {
303 pr_err("failed to set array info for %s: %s\n",
304 chosen_name
, strerror(errno
));
310 dinfo
.disk
.major
= major(rdev
);
311 dinfo
.disk
.minor
= minor(rdev
);
312 if (add_disk(mdfd
, st
, &info
, &dinfo
) != 0) {
313 pr_err("failed to add %s to new array %s: %s.\n",
314 devname
, chosen_name
, strerror(errno
));
315 ioctl(mdfd
, STOP_ARRAY
, 0);
319 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
320 GET_OFFSET
| GET_SIZE
));
322 if (!sra
|| !sra
->devs
|| sra
->devs
->disk
.raid_disk
>= 0) {
323 /* It really should be 'none' - must be old buggy
324 * kernel, and mdadm -I may not be able to complete.
327 ioctl(mdfd
, STOP_ARRAY
, NULL
);
328 pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n");
332 info
.array
.working_disks
= 1;
333 /* 6/ Make sure /var/run/mdadm.map contains this array. */
334 map_update(&map
, fd2devnm(mdfd
),
336 info
.uuid
, chosen_name
);
339 /* - check one drive in array to make sure metadata is a reasonably */
340 /* close match. Reject if not (e.g. different type) */
341 /* - add the device */
345 struct supertype
*st2
;
346 struct mdinfo info2
, *d
;
348 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
349 GET_OFFSET
| GET_SIZE
));
352 strcpy(chosen_name
, mp
->path
);
354 strcpy(chosen_name
, mp
->devnm
);
356 /* It is generally not OK to add non-spare drives to a
357 * running array as they are probably missing because
358 * they failed. However if runstop is 1, then the
359 * array was possibly started early and our best bet is
360 * to add this anyway.
361 * Also if action policy is re-add or better we allow
363 * This doesn't apply to containers as the 'non-spare'
364 * flag has a different meaning. The test has to happen
365 * at the device level there
367 if (!st
->ss
->external
&&
368 (info
.disk
.state
& (1 << MD_DISK_SYNC
)) != 0 &&
369 !policy_action_allows(policy
, st
->ss
->name
, act_re_add
) &&
371 if (md_array_active(mdfd
)) {
372 pr_err("not adding %s to active array (without --run) %s\n",
373 devname
, chosen_name
);
383 sprintf(dn
, "%d:%d", sra
->devs
->disk
.major
,
384 sra
->devs
->disk
.minor
);
385 dfd2
= dev_open(dn
, O_RDONLY
);
387 pr_err("unable to open %s\n", devname
);
392 if (st2
->ss
->load_super(st2
, dfd2
, NULL
) ||
393 st
->ss
->compare_super(st
, st2
, 1) != 0) {
394 pr_err("metadata mismatch between %s and chosen array %s\n",
395 devname
, chosen_name
);
401 st2
->ss
->getinfo_super(st2
, &info2
, NULL
);
402 st2
->ss
->free_super(st2
);
403 if (info
.array
.level
!= info2
.array
.level
||
404 memcmp(info
.uuid
, info2
.uuid
, 16) != 0 ||
405 info
.array
.raid_disks
!= info2
.array
.raid_disks
) {
406 pr_err("unexpected difference between %s and %s.\n",
407 chosen_name
, devname
);
412 info
.disk
.major
= major(rdev
);
413 info
.disk
.minor
= minor(rdev
);
414 /* add disk needs to know about containers */
415 if (st
->ss
->external
)
416 sra
->array
.level
= LEVEL_CONTAINER
;
418 if (info
.array
.state
& (1 << MD_SB_CLUSTERED
))
419 info
.disk
.state
|= (1 << MD_DISK_CLUSTER_ADD
);
421 err
= add_disk(mdfd
, st
, sra
, &info
);
422 if (err
< 0 && errno
== EBUSY
) {
423 /* could be another device present with the same
424 * disk.number. Find and reject any such
426 find_reject(mdfd
, st
, sra
, info
.disk
.number
,
427 info
.events
, c
->verbose
, chosen_name
);
428 err
= add_disk(mdfd
, st
, sra
, &info
);
430 if (err
< 0 && errno
== EINVAL
&&
431 info
.disk
.state
& (1<<MD_DISK_SYNC
)) {
432 /* Maybe it needs to be added as a spare */
433 if (policy_action_allows(policy
, st
->ss
->name
,
435 info
.disk
.state
&= ~(1<<MD_DISK_SYNC
);
436 err
= add_disk(mdfd
, st
, sra
, &info
);
439 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
440 devname
, chosen_name
);
443 pr_err("failed to add %s to existing array %s: %s.\n",
444 devname
, chosen_name
, strerror(errno
));
448 info
.array
.working_disks
= 0;
449 for (d
= sra
->devs
; d
; d
=d
->next
)
450 info
.array
.working_disks
++;
452 if (strncmp(chosen_name
, DEV_MD_DIR
, DEV_MD_DIR_LEN
) == 0)
453 md_devname
= chosen_name
+ DEV_MD_DIR_LEN
;
455 md_devname
= chosen_name
;
457 printf("MD_DEVICE=%s\n", fd2devnm(mdfd
));
458 printf("MD_DEVNAME=%s\n", md_devname
);
459 printf("MD_FOREIGN=%s\n", trustworthy
== FOREIGN
? "yes" : "no");
462 /* 7/ Is there enough devices to possibly start the array? */
463 /* 7a/ if not, finish with success. */
464 if (is_container(info
.array
.level
)) {
466 /* Try to assemble within the container */
467 if (!c
->export
&& c
->verbose
>= 0)
468 pr_err("container %s now has %d device%s\n",
469 chosen_name
, info
.array
.working_disks
,
470 info
.array
.working_disks
== 1?"":"s");
471 sysfs_rules_apply(chosen_name
, &info
);
472 wait_for(chosen_name
, mdfd
);
473 if (st
->ss
->external
)
474 strcpy(devnm
, fd2devnm(mdfd
));
475 if (st
->ss
->load_container
)
476 rv
= st
->ss
->load_container(st
, mdfd
, NULL
);
479 sysfs_uevent(sra
, "change");
482 rv
= Incremental_container(st
, chosen_name
, c
, NULL
);
484 /* after spare is added, ping monitor for external metadata
485 * so that it can eg. try to rebuild degraded array */
486 if (st
->ss
->external
)
492 /* We have added something to the array, so need to re-read the
493 * state. Eventually this state should be kept up-to-date as
497 sra
= sysfs_read(mdfd
, NULL
, (GET_DEVS
| GET_STATE
|
498 GET_OFFSET
| GET_SIZE
));
499 active_disks
= count_active(st
, sra
, mdfd
, &avail
, &info
);
504 journal_device_missing
= (info
.journal_device_required
) && (info
.journal_clean
== 0);
506 if (info
.consistency_policy
== CONSISTENCY_POLICY_PPL
)
507 info
.array
.state
|= 1;
509 if (enough(info
.array
.level
, info
.array
.raid_disks
,
510 info
.array
.layout
, info
.array
.state
& 1, avail
) == 0) {
512 printf("MD_STARTED=no\n");
513 } else if (c
->verbose
>= 0)
514 pr_err("%s attached to %s, not enough to start (%d).\n",
515 devname
, chosen_name
, active_disks
);
521 /* - if number of OK devices match expected, or -R and there */
523 /* + add any bitmap file */
524 /* + start the array (auto-readonly). */
526 if (md_array_active(mdfd
)) {
528 printf("MD_STARTED=already\n");
529 } else if (c
->verbose
>= 0)
530 pr_err("%s attached to %s which is already active.\n",
531 devname
, chosen_name
);
537 if (c
->runstop
> 0 || (!journal_device_missing
&& active_disks
>= info
.array
.working_disks
)) {
539 /* Let's try to start it */
541 if (journal_device_missing
)
542 pr_err("Trying to run with missing journal device\n");
543 if (info
.reshape_active
&& !(info
.reshape_active
& RESHAPE_NO_BACKUP
)) {
544 pr_err("%s: This array is being reshaped and cannot be started\n",
546 cont_err("by --incremental. Please use --assemble\n");
550 /* Need to remove from the array any devices which
551 * 'count_active' discerned were too old or inappropriate
553 for (d
= sra
? sra
->devs
: NULL
; d
; d
= d
->next
)
554 if (d
->disk
.state
& (1<<MD_DISK_REMOVED
))
555 remove_disk(mdfd
, st
, sra
, d
);
557 if ((sra
== NULL
|| active_disks
>= info
.array
.working_disks
) &&
558 trustworthy
!= FOREIGN
)
559 rv
= ioctl(mdfd
, RUN_ARRAY
, NULL
);
561 rv
= sysfs_set_str(sra
, NULL
,
562 "array_state", "read-auto");
563 /* Array might be O_EXCL which will interfere with
564 * fsck and mount. So re-open without O_EXCL.
569 printf("MD_STARTED=yes\n");
570 } else if (c
->verbose
>= 0)
571 pr_err("%s attached to %s, which has been started.\n",
572 devname
, chosen_name
);
574 wait_for(chosen_name
, mdfd
);
575 /* We just started the array, so some devices
576 * might have been evicted from the array
577 * because their event counts were too old.
578 * If the action=re-add policy is in-force for
579 * those devices we should re-add them now.
581 for (dsk
= sra
->devs
; dsk
; dsk
= dsk
->next
) {
582 if (disk_action_allows(dsk
, st
->ss
->name
,
584 add_disk(mdfd
, st
, sra
, dsk
) == 0)
585 pr_err("%s re-added to %s\n",
586 dsk
->sys_name
, chosen_name
);
589 pr_err("%s attached to %s, but failed to start: %s.\n",
590 devname
, chosen_name
, strerror(errno
));
595 printf("MD_STARTED=unsafe\n");
596 } else if (journal_device_missing
) {
597 pr_err("Journal device is missing, not safe to start yet.\n");
598 } else if (c
->verbose
>= 0)
599 pr_err("%s attached to %s, not enough to start safely.\n",
600 devname
, chosen_name
);
610 dev_policy_free(policy
);
613 sysfs_uevent(sra
, "change");
622 static void find_reject(int mdfd
, struct supertype
*st
, struct mdinfo
*sra
,
623 int number
, __u64 events
, int verbose
,
626 /* Find a device attached to this array with a disk.number of number
627 * and events less than the passed events, and remove the device.
631 if (md_array_active(mdfd
))
632 return; /* not safe to remove from active arrays
633 * without thinking more */
635 for (d
= sra
->devs
; d
; d
= d
->next
) {
636 char dn
[24]; // 2*11 bytes for ints (including sign) + colon + null byte
639 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
640 dfd
= dev_open(dn
, O_RDONLY
);
643 if (st
->ss
->load_super(st
, dfd
, NULL
)) {
647 st
->ss
->getinfo_super(st
, &info
, NULL
);
648 st
->ss
->free_super(st
);
651 if (info
.disk
.number
!= number
|| info
.events
>= events
)
654 if (d
->disk
.raid_disk
> -1)
655 sysfs_set_str(sra
, d
, "slot", STR_COMMON_NONE
);
656 if (sysfs_set_str(sra
, d
, "state", "remove") == 0)
658 pr_err("removing old device %s from %s\n",
659 d
->sys_name
+4, array_name
);
663 static int count_active(struct supertype
*st
, struct mdinfo
*sra
,
664 int mdfd
, char **availp
,
665 struct mdinfo
*bestinfo
)
667 /* count how many devices in sra think they are active */
671 __u64 max_events
= 0;
672 __u64 max_journal_events
= 0;
684 for (d
= sra
->devs
; d
; d
= d
->next
)
686 for (d
= sra
->devs
, devnum
= 0 ; d
; d
= d
->next
, devnum
++) {
692 sprintf(dn
, "%d:%d", d
->disk
.major
, d
->disk
.minor
);
693 dfd
= dev_open(dn
, O_RDONLY
);
696 ok
= st
->ss
->load_super(st
, dfd
, NULL
);
701 info
.array
.raid_disks
= raid_disks
;
702 st
->ss
->getinfo_super(st
, &info
, devmap
+ raid_disks
* devnum
);
703 if (info
.disk
.raid_disk
== MD_DISK_ROLE_JOURNAL
&&
704 info
.events
> max_journal_events
)
705 max_journal_events
= info
.events
;
707 raid_disks
= info
.array
.raid_disks
;
708 avail
= xcalloc(raid_disks
, 1);
711 best
= xcalloc(raid_disks
, sizeof(int));
712 devmap
= xcalloc(raid_disks
, numdevs
);
714 st
->ss
->getinfo_super(st
, &info
, devmap
);
717 if (info
.disk
.state
& (1<<MD_DISK_SYNC
))
721 max_events
= info
.events
;
722 avail
[info
.disk
.raid_disk
] = 2;
723 best
[info
.disk
.raid_disk
] = devnum
;
724 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
725 } else if (info
.events
== max_events
) {
726 avail
[info
.disk
.raid_disk
] = 2;
727 best
[info
.disk
.raid_disk
] = devnum
;
728 } else if (info
.events
== max_events
-1) {
729 if (avail
[info
.disk
.raid_disk
] == 0) {
730 avail
[info
.disk
.raid_disk
] = 1;
731 best
[info
.disk
.raid_disk
] = devnum
;
733 } else if (info
.events
< max_events
- 1)
735 else if (info
.events
== max_events
+1) {
737 max_events
= info
.events
;
738 for (i
= 0; i
< raid_disks
; i
++)
741 avail
[info
.disk
.raid_disk
] = 2;
742 best
[info
.disk
.raid_disk
] = devnum
;
743 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
744 } else { /* info.events much bigger */
745 memset(avail
, 0, raid_disks
);
746 max_events
= info
.events
;
747 avail
[info
.disk
.raid_disk
] = 2;
748 best
[info
.disk
.raid_disk
] = devnum
;
749 st
->ss
->getinfo_super(st
, bestinfo
, NULL
);
751 } else if (info
.disk
.state
& (1<<MD_DISK_REPLACEMENT
))
753 st
->ss
->free_super(st
);
755 if (max_events
> 0 && max_journal_events
>= max_events
- 1)
756 bestinfo
->journal_clean
= 1;
760 /* We need to reject any device that thinks the best device is
761 * failed or missing */
762 for (b
= 0; b
< raid_disks
; b
++)
766 for (i
= 0 ; i
< raid_disks
; i
++) {
767 if (i
!= b
&& avail
[i
])
768 if (devmap
[raid_disks
* best
[i
] + b
] == 0) {
769 /* This device thinks 'b' is failed -
772 for (d
=sra
->devs
; devnum
; d
= d
->next
)
774 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
780 /* Also need to reject any spare device with an event count that
783 for (d
= sra
->devs
; d
; d
= d
->next
) {
784 if (!(d
->disk
.state
& (1<<MD_DISK_SYNC
)) &&
785 d
->events
> max_events
)
786 d
->disk
.state
|= (1 << MD_DISK_REMOVED
);
790 return cnt
+ replcnt
;
793 /* test if container has degraded member(s) */
795 container_members_max_degradation(struct map_ent
*map
, struct map_ent
*me
)
798 int degraded
, max_degraded
= 0;
800 for(; map
; map
= map
->next
) {
801 if (!metadata_container_matches(map
->metadata
, me
->devnm
))
803 /* most accurate information regarding array degradation */
804 sra
= sysfs_read(-1, map
->devnm
,
805 GET_DISKS
| GET_DEVS
| GET_STATE
);
808 degraded
= sra
->array
.raid_disks
- sra
->array
.active_disks
-
809 sra
->array
.spare_disks
;
810 if (degraded
> max_degraded
)
811 max_degraded
= degraded
;
819 * incremental_external_test_spare_criteria() - helper to test spare criteria.
820 * @st: supertype, must be not NULL, it is duplicated here.
821 * @container_devnm: devnm of the container.
822 * @disk_fd: file descriptor of device to tested.
823 * @verbose: verbose flag.
825 * The function is used on new drive verification path to check if it can be added to external
826 * container. To test spare criteria, metadata must be loaded. It duplicates super to not mess in
828 * Function is executed if superblock supports get_spare_criteria(), otherwise success is returned.
830 mdadm_status_t
incremental_external_test_spare_criteria(struct supertype
*st
, char *container_devnm
,
831 int disk_fd
, int verbose
)
833 mdadm_status_t rv
= MDADM_STATUS_ERROR
;
834 char container_devname
[PATH_MAX
];
835 struct spare_criteria sc
= {0};
836 struct supertype
*dup
;
838 if (!st
->ss
->get_spare_criteria
)
839 return MDADM_STATUS_SUCCESS
;
842 snprintf(container_devname
, PATH_MAX
, "/dev/%s", container_devnm
);
844 if (dup
->ss
->get_spare_criteria(dup
, container_devname
, &sc
) != 0) {
846 pr_err("Failed to get spare criteria for %s\n", container_devname
);
850 if (!disk_fd_matches_criteria(dup
, disk_fd
, &sc
)) {
852 pr_err("Disk does not match spare criteria for %s\n", container_devname
);
856 rv
= MDADM_STATUS_SUCCESS
;
859 dev_policy_free(sc
.pols
);
860 dup
->ss
->free_super(dup
);
866 static int array_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
867 struct map_ent
*target
, int bare
,
868 struct supertype
*st
, int verbose
)
870 /* This device doesn't have any md metadata
871 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
872 * If 'st' is not set, then we only know that some metadata allows this,
873 * others possibly don't.
874 * So look for a container or array to attach the device to.
875 * Prefer 'target' if that is set and the array is found.
877 * If st is set, then only arrays of that type are considered
878 * Return 0 on success, or some exit code on failure, probably 1.
882 struct map_ent
*mp
, *map
= NULL
;
883 struct mdinfo
*chosen
= NULL
;
886 if (!fstat_is_blkdev(dfd
, devname
, &rdev
))
890 * Now we need to find a suitable array to add this to.
891 * We only accept arrays that:
893 * - are in the same domains as the device
894 * - are of an size for which the device will be useful
895 * and we choose the one that is the most degraded
898 if (map_lock(&map
)) {
899 pr_err("failed to get exclusive lock on mapfile\n");
902 for (mp
= map
; mp
; mp
= mp
->next
) {
903 struct supertype
*st2
;
904 struct domainlist
*dl
= NULL
;
906 unsigned long long freesize
= 0;
908 if (is_subarray(mp
->metadata
))
911 st2
= st
->ss
->match_metadata_desc(mp
->metadata
);
913 (st
->minor_version
>= 0 &&
914 st
->minor_version
!= st2
->minor_version
)) {
916 pr_err("not adding %s to %s as metadata type doesn't match\n",
923 sra
= sysfs_read(-1, mp
->devnm
,
924 GET_DEVS
|GET_OFFSET
|GET_SIZE
|GET_STATE
|
925 GET_COMPONENT
|GET_VERSION
);
927 sra
->array
.failed_disks
= -1;
933 for(i
= 0; !st2
&& superlist
[i
]; i
++)
934 st2
= superlist
[i
]->match_metadata_desc(
938 pr_err("not adding %s to %s as metadata not recognised.\n",
942 /* Need to double check the 'act_spare' permissions applies
945 if (!policy_action_allows(pol
, st2
->ss
->name
, act_spare
))
947 if (!bare
&& !policy_action_allows(pol
, st2
->ss
->name
,
948 act_spare_same_slot
))
952 /* update number of failed disks for mostly degraded
953 * container member */
954 if (sra
->array
.failed_disks
== -1)
955 sra
->array
.failed_disks
= container_members_max_degradation(map
, mp
);
957 if (sra
->component_size
== 0) {
958 /* true for containers */
959 if (incremental_external_test_spare_criteria(st2
, mp
->devnm
, dfd
, verbose
))
963 if (sra
->component_size
> 0 &&
964 st2
->ss
->validate_geometry(st2
, sra
->array
.level
, sra
->array
.layout
,
965 sra
->array
.raid_disks
, &sra
->array
.chunk_size
,
967 sra
->devs
? sra
->devs
->data_offset
: INVALID_SECTORS
,
968 devname
, &freesize
, sra
->consistency_policy
,
969 0) && freesize
< sra
->component_size
) {
971 pr_err("not adding %s to %s as it is too small\n",
975 /* test against target.
976 * If 'target' is set and 'bare' is false, we only accept
977 * arrays/containers that match 'target'.
978 * If 'target' is set and 'bare' is true, we prefer the
979 * array which matches 'target'.
980 * target is considered only if we deal with degraded array
982 if (target
&& policy_action_allows(pol
, st2
->ss
->name
,
983 act_spare_same_slot
)) {
984 if (strcmp(target
->metadata
, mp
->metadata
) == 0 &&
985 memcmp(target
->uuid
, mp
->uuid
,
986 sizeof(target
->uuid
)) == 0 &&
987 sra
->array
.failed_disks
> 0) {
988 /* This is our target!! */
992 /* skip to end so we don't check any more */
1002 dl
= domain_from_array(sra
, st2
->ss
->name
);
1003 if (domain_test(dl
, pol
, st2
->ss
->name
) != 1) {
1004 /* domain test fails */
1006 pr_err("not adding %s to %s as it is not in a compatible domain\n",
1011 /* all tests passed, OK to add to this array */
1015 } else if (chosen
->array
.failed_disks
< sra
->array
.failed_disks
) {
1028 /* add current device to chosen array as a spare */
1029 int mdfd
= open_dev(chosen
->sys_name
);
1031 struct mddev_dev devlist
;
1032 char chosen_devname
[24]; // 2*11 for int (including signs) + colon + null
1033 devlist
.next
= NULL
;
1035 devlist
.writemostly
= FlagDefault
;
1036 devlist
.failfast
= FlagDefault
;
1037 devlist
.devname
= chosen_devname
;
1038 sprintf(chosen_devname
, "%d:%d", major(rdev
),
1040 devlist
.disposition
= 'a';
1043 rv
= Manage_subdevs(chosen
->sys_name
, mdfd
, &devlist
,
1044 -1, 0, UOPT_UNDEFINED
, 0);
1049 pr_err("added %s as spare for %s\n",
1050 devname
, chosen
->sys_name
);
1052 pr_err("failed to add %s as spare for %s\n",
1053 devname
, chosen
->sys_name
);
1061 static int partition_try_spare(char *devname
, int *dfdp
, struct dev_policy
*pol
,
1062 struct supertype
*st
, int verbose
)
1064 /* we know that at least one partition virtual-metadata is
1065 * allowed to incorporate spares like this device. We need to
1066 * find a suitable device to copy partition information from.
1068 * Getting a list of all disk (not partition) devices is
1069 * slightly non-trivial. We could look at /sys/block, but
1070 * that is theoretically due to be removed. Maybe best to use
1071 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1072 * we depend on this directory of 'path' info. But that fails
1073 * to find loop devices and probably others. Maybe don't
1074 * worry about that, they aren't the real target.
1076 * So: check things in /dev/disk/by-path to see if they are in
1077 * a compatible domain, then load the partition table and see
1078 * if it is OK for the new device, and choose the largest
1079 * partition table that fits.
1083 char *chosen
= NULL
;
1084 unsigned long long chosen_size
= 0;
1085 struct supertype
*chosen_st
= NULL
;
1088 dir
= opendir("/dev/disk/by-path");
1091 while ((de
= readdir(dir
)) != NULL
) {
1093 struct dev_policy
*pol2
= NULL
;
1094 struct domainlist
*domlist
= NULL
;
1097 struct supertype
*st2
= NULL
;
1098 char *dev_path_name
= NULL
;
1099 unsigned long long devsectors
;
1102 if (de
->d_ino
== 0 || de
->d_name
[0] == '.' ||
1103 (de
->d_type
!= DT_LNK
&& de
->d_type
!= DT_UNKNOWN
))
1106 ep
= de
->d_name
+ strlen(de
->d_name
);
1107 while (ep
> de
->d_name
&&
1110 if (ep
> de
->d_name
+ 5 &&
1111 strncmp(ep
-5, "-part", 5) == 0)
1112 /* This is a partition - skip it */
1115 pathlist
[0] = de
->d_name
;
1117 pol2
= path_policy(pathlist
, 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(&dev_path_name
, "/dev/disk/by-path/%s", de
->d_name
) != 1) {
1128 dev_path_name
= NULL
;
1131 fd
= open(dev_path_name
, O_RDONLY
);
1134 if (get_dev_size(fd
, dev_path_name
, &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
;
1173 chosen
= dev_path_name
;
1174 dev_path_name
= NULL
;
1176 chosen_st
->ss
->free_super(chosen_st
);
1184 free(dev_path_name
);
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 ((size
>= 4096 && 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 to add '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
) {
1345 if (devnm
&& strcmp(devnm
, me
->devnm
) != 0)
1347 if (me
->metadata
[0] == '/') {
1353 /* member array, need to work on container */
1354 strncpy(container
, me
->metadata
+1, 32);
1356 sl
= strchr(container
, '/');
1363 mdfd
= open_dev(me
->devnm
);
1365 if (!is_fd_valid(mdfd
))
1367 if (!isdigit(me
->metadata
[0])) {
1368 /* must be a container */
1369 struct supertype
*st
= super_by_fd(mdfd
, NULL
);
1371 struct map_ent
*map
= NULL
;
1373 if (st
&& st
->ss
->load_container
)
1374 ret
= st
->ss
->load_container(st
, mdfd
, NULL
);
1376 if (!ret
&& st
&& st
->ss
->container_content
) {
1378 pr_err("failed to get exclusive lock on mapfile\n");
1379 ret
= Incremental_container(st
, me
->path
, c
, only
);
1386 if (md_array_active(mdfd
)) {
1390 /* Ok, we can try this one. Maybe it needs a bitmap */
1391 for (mddev
= devs
; mddev
; mddev
= mddev
->next
)
1392 if (mddev
->devname
&& me
->path
&&
1393 devname_matches(mddev
->devname
, me
->path
))
1396 /* FIXME check for reshape_active and consider not
1399 sra
= sysfs_read(mdfd
, NULL
, 0);
1401 if (sysfs_set_str(sra
, NULL
,
1402 "array_state", "read-auto") == 0) {
1403 if (c
->verbose
>= 0)
1404 pr_err("started array %s\n",
1405 me
->path
?: me
->devnm
);
1407 pr_err("failed to start array %s: %s\n",
1408 me
->path
?: me
->devnm
,
1420 static char *container2devname(char *devname
)
1422 char *mdname
= NULL
;
1424 if (devname
[0] == '/') {
1425 int fd
= open(devname
, O_RDONLY
);
1427 mdname
= xstrdup(fd2devnm(fd
));
1432 struct map_ent
*mp
, *map
= NULL
;
1434 if (!parse_uuid(devname
, uuid
))
1436 mp
= map_by_uuid(&map
, uuid
);
1438 mdname
= xstrdup(mp
->devnm
);
1445 static int Incremental_container(struct supertype
*st
, char *devname
,
1446 struct context
*c
, char *only
)
1448 /* Collect the contents of this container and for each
1449 * array, choose a device name and assemble the array.
1452 struct mdinfo
*list
;
1454 struct map_ent
*map
= NULL
;
1457 struct mddev_ident
*match
;
1461 st
->ss
->getinfo_super(st
, &info
, NULL
);
1463 if (info
.container_enough
< 0 || (info
.container_enough
== 0 && c
->runstop
< 1)) {
1465 printf("MD_STARTED=no\n");
1466 else if (c
->verbose
)
1467 pr_err("Not enough devices to start the container.\n");
1472 match
= conf_match(st
, &info
, devname
, c
->verbose
, &rv
);
1473 if (match
== NULL
&& rv
== 2)
1476 /* Need to compute 'trustworthy' */
1478 trustworthy
= LOCAL
;
1479 else if (st
->ss
->match_home(st
, c
->homehost
) == 1)
1480 trustworthy
= LOCAL
;
1481 else if (st
->ss
->match_home(st
, "any") == 1)
1482 trustworthy
= LOCAL
;
1484 trustworthy
= FOREIGN
;
1486 list
= st
->ss
->container_content(st
, NULL
);
1487 /* when nothing to activate - quit */
1490 printf("MD_STARTED=nothing\n");
1494 for (ra
= list
; ra
; ra
= ra
->next
) {
1496 char chosen_name
[1024];
1498 struct mddev_ident
*match
= NULL
;
1500 /* do not activate arrays blocked by metadata handler */
1501 if (ra
->array
.state
& (1 << MD_SB_BLOCK_VOLUME
)) {
1502 pr_err("Cannot activate array %s in %s.\n",
1503 ra
->text_version
, devname
);
1506 mp
= map_by_uuid(&map
, ra
->uuid
);
1509 mdfd
= open_dev(mp
->devnm
);
1510 if (!is_fd_valid(mdfd
)) {
1511 pr_err("failed to open %s: %s.\n",
1512 mp
->devnm
, strerror(errno
));
1517 strcpy(chosen_name
, mp
->path
);
1519 strcpy(chosen_name
, mp
->devnm
);
1522 /* Check in mdadm.conf for container == devname and
1523 * member == ra->text_version after second slash.
1525 char *sub
= strchr(ra
->text_version
+1, '/');
1526 struct mddev_ident
*array_list
;
1529 array_list
= conf_get_ident(NULL
);
1532 for(; array_list
; array_list
= array_list
->next
) {
1534 if (array_list
->member
== NULL
||
1535 array_list
->container
== NULL
)
1537 if (strcmp(array_list
->member
, sub
) != 0)
1539 if (array_list
->uuid_set
&&
1540 !same_uuid(ra
->uuid
, array_list
->uuid
, st
->ss
->swapuuid
))
1542 dn
= container2devname(array_list
->container
);
1545 if (strncmp(dn
, ra
->text_version
+1,
1547 ra
->text_version
[strlen(dn
)+1] != '/') {
1552 /* we have a match */
1555 pr_err("match found for member %s\n",
1556 array_list
->member
);
1560 if (match
&& match
->devname
&& is_devname_ignore(match
->devname
) == true) {
1562 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1563 match
->container
, match
->member
);
1567 trustworthy
= LOCAL
;
1569 mdfd
= create_mddev(match
? match
->devname
: NULL
, ra
->name
, trustworthy
,
1572 if (!is_fd_valid(mdfd
)) {
1573 pr_err("create_mddev failed with chosen name %s: %s.\n",
1574 chosen_name
, strerror(errno
));
1580 if (only
&& (!mp
|| strcmp(mp
->devnm
, only
) != 0)) {
1585 assemble_container_content(st
, mdfd
, ra
, c
,
1586 chosen_name
, &result
);
1591 sysfs_uevent(&info
, "change");
1593 if (c
->export
&& result
) {
1595 printf("MD_STARTED");
1596 if (result
& INCR_NO
) {
1597 printf("%cno", sep
);
1600 if (result
& INCR_UNSAFE
) {
1601 printf("%cunsafe", sep
);
1604 if (result
& INCR_ALREADY
) {
1605 printf("%calready", sep
);
1608 if (result
& INCR_YES
) {
1609 printf("%cyes", sep
);
1619 sysfs_uevent(&info
, "change");
1624 * is_devnode_path() - check if the devname passed might be devnode path.
1625 * @devnode: the path to check.
1627 * Devnode must be located directly in /dev directory. It is not checking existence of the file
1628 * because the device might no longer exist during removal from raid array.
1630 static bool is_devnode_path(char *devnode
)
1632 char *devnm
= strrchr(devnode
, '/');
1634 if (!devnm
|| *(devnm
+ 1) == 0)
1637 if (strncmp(devnode
, DEV_DIR
, DEV_DIR_LEN
) == 0 && devnode
+ DEV_DIR_LEN
- 1 == devnm
)
1644 * Incremental_remove_external() - Remove the device from external container.
1645 * @device_devnm: block device to remove.
1646 * @container_devnm: the parent container
1647 * @mdstat: mdstat file content.
1648 * @verbose: verbose flag.
1650 * Fail member device in each subarray and remove member device from external container.
1651 * The resposibility of removing member disks from external subararys belongs to mdmon.
1653 static mdadm_status_t
Incremental_remove_external(char *device_devnm
, char *container_devnm
,
1654 struct mdstat_ent
*mdstat
, int verbose
)
1656 mdadm_status_t rv
= MDADM_STATUS_SUCCESS
;
1657 struct mdstat_ent
*memb
;
1659 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
1660 mdadm_status_t ret
= MDADM_STATUS_SUCCESS
;
1663 if (!is_container_member(memb
, container_devnm
))
1667 * Checking mdstat is pointles because it might be outdated, try open descriptor
1668 * instead. If it fails, we are fine with that, device is already gone.
1670 state_fd
= sysfs_open_memb_attr(memb
->devnm
, device_devnm
, "state", O_RDWR
);
1671 if (!is_fd_valid(state_fd
))
1674 ret
= sysfs_set_memb_state_fd(state_fd
, MEMB_STATE_FAULTY
, NULL
);
1675 if (ret
&& verbose
>= 0)
1676 pr_err("Cannot fail member device %s in external subarray %s.\n",
1677 device_devnm
, memb
->devnm
);
1679 close_fd(&state_fd
);
1682 * Don't remove member device from container if it failed to remove it
1683 * from any member array.
1688 if (rv
== MDADM_STATUS_SUCCESS
)
1689 rv
= sysfs_set_memb_state(container_devnm
, device_devnm
, MEMB_STATE_REMOVE
);
1691 if (rv
&& verbose
>= 0)
1692 pr_err("Cannot remove member device %s from container %s.\n", device_devnm
,
1699 * Incremental_remove() - Remove the device from all raid arrays.
1700 * @devname: the device we want to remove, it could be kernel device name or devnode.
1701 * @id_path: optional, /dev/disk/by-path path to save for bare scenarios support.
1702 * @verbose: verbose flag.
1704 * First, fail the device (if needed) and then remove the device. This code is critical for system
1705 * funtionality and that is why it is keept as simple as possible. We do not load devices using
1706 * sysfs_read() because any unerelated failure may lead us to abort. We also do not call
1709 int Incremental_remove(char *devname
, char *id_path
, int verbose
)
1711 mdadm_status_t rv
= MDADM_STATUS_SUCCESS
;
1712 char *devnm
= basename(devname
);
1713 char buf
[SYSFS_MAX_BUF_SIZE
];
1714 struct mdstat_ent
*mdstat
;
1715 struct mdstat_ent
*ent
;
1719 if (strcmp(devnm
, devname
) != 0)
1720 if (!is_devnode_path(devname
)) {
1721 pr_err("Cannot remove \"%s\", devnode path or kernel device name is allowed.\n",
1726 mdstat
= mdstat_read(0, 0);
1728 pr_err("Cannot read /proc/mdstat file, aborting\n");
1732 ent
= mdstat_find_by_member_name(mdstat
, devnm
);
1735 pr_vrb("%s does not appear to be a component of any array\n", devnm
);
1739 if (sysfs_init(&mdi
, -1, ent
->devnm
)) {
1740 pr_err("unable to initialize sysfs for: %s\n", devnm
);
1744 mdfd
= open_dev_excl(ent
->devnm
);
1745 if (is_fd_valid(mdfd
)) {
1746 char *array_state_file
= "array_state";
1749 * This is a workaround for the old issue.
1750 * Incremental_remove() triggered from udev rule when disk is removed from OS
1751 * tries to set array in auto-read-only mode. This can interrupt rebuild
1752 * process which is started automatically, e.g. if array is mounted and
1753 * spare disk is available (I/O errors limit might be achieved faster than disk is
1754 * removed by mdadm). Prevent Incremental_remove() from setting array
1755 * into "auto-read-only", by requiring exclusive open to succeed.
1759 if (sysfs_get_str(&mdi
, NULL
, array_state_file
, buf
, sizeof(buf
)) > 0) {
1760 char *str_read_auto
= map_num_s(sysfs_array_states
, ARRAY_READ_AUTO
);
1761 char *str_active
= map_num_s(sysfs_array_states
, ARRAY_ACTIVE
);
1762 char *str_clean
= map_num_s(sysfs_array_states
, ARRAY_CLEAN
);
1764 if (strncmp(buf
, str_active
, strlen(str_active
)) == 0 ||
1765 strncmp(buf
, str_clean
, strlen(str_clean
)) == 0)
1766 sysfs_set_str(&mdi
, NULL
, array_state_file
, str_read_auto
);
1770 mdfd
= open_dev(ent
->devnm
);
1773 pr_err("Cannot open array %s!!\n", ent
->devnm
);
1778 struct map_ent
*map
= NULL
, *me
;
1780 me
= map_by_devnm(&map
, ent
->devnm
);
1782 policy_save_path(id_path
, me
);
1786 if (is_mdstat_ent_external(ent
)) {
1787 rv
= Incremental_remove_external(devnm
, ent
->devnm
, mdstat
, verbose
);
1791 /* Native arrays are handled separatelly to provide more detailed error handling */
1792 rv
= sysfs_set_memb_state(ent
->devnm
, devnm
, MEMB_STATE_FAULTY
);
1793 if (rv
&& verbose
>= 0)
1794 pr_err("Cannot fail member device %s in array %s.\n", devnm
, ent
->devnm
);
1796 if (rv
== MDADM_STATUS_SUCCESS
)
1797 rv
= sysfs_set_memb_state(ent
->devnm
, devnm
, MEMB_STATE_REMOVE
);
1799 if (rv
&& verbose
>= 0)
1800 pr_err("Cannot remove member device %s from %s.\n", devnm
, ent
->devnm
);
1804 free_mdstat(mdstat
);