| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2010 ProFUSION embedded systems |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <linux/major.h> /* IWYU pragma: keep */ |
| 7 |
#include <linux/raid/md_u.h> |
| 8 |
#include <sys/ioctl.h> |
| 9 |
|
| 10 |
#include "sd-device.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "blockdev-util.h" |
| 14 |
#include "detach-md.h" |
| 15 |
#include "device-util.h" |
| 16 |
#include "devnum-util.h" |
| 17 |
#include "errno-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "list.h" |
| 20 |
#include "shutdown.h" |
| 21 |
#include "string-util.h" |
| 22 |
|
| 23 |
typedef struct RaidDevice { |
| 24 |
char *path; |
| 25 |
dev_t devnum; |
| 26 |
LIST_FIELDS(struct RaidDevice, raid_device); |
| 27 |
} RaidDevice; |
| 28 |
|
| 29 |
static void raid_device_free(RaidDevice **head, RaidDevice *m) { |
| 30 |
assert(head); |
| 31 |
assert(m); |
| 32 |
|
| 33 |
LIST_REMOVE(raid_device, *head, m); |
| 34 |
|
| 35 |
free(m->path); |
| 36 |
free(m); |
| 37 |
} |
| 38 |
|
| 39 |
static void raid_device_list_free(RaidDevice **head) { |
| 40 |
assert(head); |
| 41 |
|
| 42 |
while (*head) |
| 43 |
raid_device_free(head, *head); |
| 44 |
} |
| 45 |
|
| 46 |
static int md_list_get(RaidDevice **head) { |
| 47 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
assert(head); |
| 51 |
|
| 52 |
r = sd_device_enumerator_new(&e); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
r = sd_device_enumerator_add_match_sysname(e, "md*"); |
| 65 |
if (r < 0) |
| 66 |
return r; |
| 67 |
|
| 68 |
/* Filter out partitions. */ |
| 69 |
r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "disk"); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
|
| 73 |
FOREACH_DEVICE(e, d) { |
| 74 |
_cleanup_free_ char *p = NULL; |
| 75 |
const char *dn, *md_level; |
| 76 |
RaidDevice *m; |
| 77 |
dev_t devnum; |
| 78 |
|
| 79 |
r = sd_device_get_devname(d, &dn); |
| 80 |
if (r < 0) { |
| 81 |
log_device_warning_errno(d, r, "Failed to get name of enumerated device, ignoring: %m"); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
r = sd_device_get_devnum(d, &devnum); |
| 86 |
if (r < 0) { |
| 87 |
log_device_warning_errno(d, r, "Failed to get devno of enumerated device '%s', ignoring device: %m", dn); |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
/* MD "containers" are a special type of MD devices, used for external metadata. Since they |
| 92 |
* don't provide RAID functionality in themselves we don't need to stop them. Note that the |
| 93 |
* MD_LEVEL udev property is set by mdadm in userspace, which is an optional package. Hence |
| 94 |
* let's handle gracefully if the property is missing. */ |
| 95 |
|
| 96 |
r = sd_device_get_property_value(d, "MD_LEVEL", &md_level); |
| 97 |
if (r < 0) |
| 98 |
log_device_full_errno(d, |
| 99 |
r == -ENOENT ? LOG_DEBUG : LOG_WARNING, |
| 100 |
r, |
| 101 |
"Failed to get MD_LEVEL property for %s, assuming regular MD device, not a container: %m", dn); |
| 102 |
else if (streq(md_level, "container")) { |
| 103 |
log_device_debug(d, "Skipping MD device '%s' because it is a container MD device.", dn); |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
p = strdup(dn); |
| 108 |
if (!p) |
| 109 |
return -ENOMEM; |
| 110 |
|
| 111 |
m = new(RaidDevice, 1); |
| 112 |
if (!m) |
| 113 |
return -ENOMEM; |
| 114 |
|
| 115 |
*m = (RaidDevice) { |
| 116 |
.path = TAKE_PTR(p), |
| 117 |
.devnum = devnum, |
| 118 |
}; |
| 119 |
|
| 120 |
LIST_PREPEND(raid_device, *head, m); |
| 121 |
} |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int delete_md(RaidDevice *m) { |
| 127 |
_cleanup_close_ int fd = -EBADF; |
| 128 |
|
| 129 |
assert(m); |
| 130 |
assert(major(m->devnum) != 0); |
| 131 |
assert(m->path); |
| 132 |
|
| 133 |
fd = open(m->path, O_RDONLY|O_CLOEXEC|O_EXCL); |
| 134 |
if (fd < 0) { |
| 135 |
if (ERRNO_IS_DEVICE_ABSENT(errno)) { |
| 136 |
log_debug_errno(errno, "Tried to open MD device '%s', but device disappeared by now, ignoring: %m", m->path); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
return log_debug_errno(errno, "Failed to open MD device '%s': %m", m->path); |
| 141 |
} |
| 142 |
|
| 143 |
(void) sync_with_progress(fd); |
| 144 |
|
| 145 |
if (ioctl(fd, STOP_ARRAY, NULL) < 0) |
| 146 |
return log_debug_errno(errno, "Failed to issue STOP_ARRAY on MD device '%s': %m", m->path); |
| 147 |
|
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
static int md_points_list_detach(RaidDevice **head, bool *changed, bool last_try) { |
| 152 |
int n_failed = 0, r; |
| 153 |
dev_t rootdev = 0, usrdev = 0; |
| 154 |
|
| 155 |
assert(head); |
| 156 |
assert(changed); |
| 157 |
|
| 158 |
(void) get_block_device("/", &rootdev); |
| 159 |
(void) get_block_device("/usr", &usrdev); |
| 160 |
|
| 161 |
LIST_FOREACH(raid_device, m, *head) { |
| 162 |
if ((major(rootdev) != 0 && rootdev == m->devnum) || |
| 163 |
(major(usrdev) != 0 && usrdev == m->devnum)) { |
| 164 |
log_debug("Not detaching MD %s that backs the OS itself, skipping.", m->path); |
| 165 |
n_failed++; |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
log_info("Stopping MD %s (" DEVNUM_FORMAT_STR ").", m->path, DEVNUM_FORMAT_VAL(m->devnum)); |
| 170 |
r = delete_md(m); |
| 171 |
if (r < 0) { |
| 172 |
log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not stop MD %s: %m", m->path); |
| 173 |
n_failed++; |
| 174 |
continue; |
| 175 |
} |
| 176 |
if (r > 0) |
| 177 |
*changed = true; |
| 178 |
|
| 179 |
raid_device_free(head, m); |
| 180 |
} |
| 181 |
|
| 182 |
return n_failed; |
| 183 |
} |
| 184 |
|
| 185 |
int md_detach_all(bool *changed, bool last_try) { |
| 186 |
_cleanup_(raid_device_list_free) LIST_HEAD(RaidDevice, md_list_head); |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(changed); |
| 190 |
|
| 191 |
LIST_HEAD_INIT(md_list_head); |
| 192 |
|
| 193 |
r = md_list_get(&md_list_head); |
| 194 |
if (r < 0) |
| 195 |
return r; |
| 196 |
|
| 197 |
return md_points_list_detach(&md_list_head, changed, last_try); |
| 198 |
} |