1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (c) 2020 Benjamin Marzinski, IBM
*/
#include <stddef.h>
#include <errno.h>
#include <libudev.h>
#include <dirent.h>
#include <libmount/libmount.h>
#include "vector.h"
#include "config.h"
#include "debug.h"
#include "util.h"
#include "devmapper.h"
#include "discovery.h"
#include "wwids.h"
#include "sysfs.h"
#include "blacklist.h"
#include "mpath_cmd.h"
#include "valid.h"
static int subdir_filter(const struct dirent *ent)
{
unsigned int j;
static char const *const skip[] = {
".",
"..",
"holders",
"integrity",
"mq",
"power",
"queue",
"slaves",
"trace",
};
if (ent->d_type != DT_DIR)
return 0;
for (j = 0; j < ARRAY_SIZE(skip); j++)
if (!strcmp(skip[j], ent->d_name))
return 0;
return 1;
}
static int read_partitions(const char *syspath, vector parts)
{
struct scandir_result sr = { .n = 0 };
char path[PATH_MAX], *last;
char *prop;
int i;
strlcpy(path, syspath, sizeof(path));
sr.n = scandir(path, &sr.di, subdir_filter, NULL);
if (sr.n == -1)
return -errno;
pthread_cleanup_push_cast(free_scandir_result, &sr);
/* parts[0] is the whole disk */
if ((prop = strdup(strrchr(path, '/') + 1)) != NULL) {
if (vector_alloc_slot(parts))
vector_set_slot(parts, prop);
else
free(prop);
}
last = path + strlen(path);
for (i = 0; i < sr.n; i++) {
struct stat st;
/* only add dirs that have the "partition" attribute */
snprintf(last, sizeof(path) - (last - path), "/%s/partition",
sr.di[i]->d_name);
if (stat(path, &st) == 0 &&
(prop = strdup(sr.di[i]->d_name)) != NULL) {
if (vector_alloc_slot(parts))
vector_set_slot(parts, prop);
else
free(prop);
}
}
pthread_cleanup_pop(1);
return 0;
}
static int no_dots(const struct dirent *ent)
{
const char *name = ent->d_name;
if (name[0] == '.' &&
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
return 0;
return 1;
}
static int check_holders(const char *syspath)
{
struct scandir_result __attribute__((cleanup(free_scandir_result)))
sr = { .n = 0 };
sr.n = scandir(syspath, &sr.di, no_dots, NULL);
if (sr.n > 0)
condlog(4, "%s: found holders under %s", __func__, syspath);
return sr.n;
}
static int check_all_holders(const struct vector_s *parts)
{
char syspath[PATH_MAX];
const char *sysname;
unsigned int j;
if (VECTOR_SIZE(parts) == 0)
return 0;
if (safe_sprintf(syspath, "/sys/class/block/%s/holders",
(const char *)VECTOR_SLOT(parts, 0)))
return -EOVERFLOW;
if (check_holders(syspath) > 0)
return 1;
j = 1;
vector_foreach_slot_after(parts, sysname, j) {
if (safe_sprintf(syspath, "/sys/class/block/%s/%s/holders",
(const char *)VECTOR_SLOT(parts, 0), sysname))
return -EOVERFLOW;
if (check_holders(syspath) > 0)
return 1;
}
return 0;
}
static void cleanup_table(void *arg)
{
if (arg)
mnt_free_table((struct libmnt_table *)arg);
}
static void cleanup_cache(void *arg)
{
if (arg)
#ifdef LIBMOUNT_HAS_MNT_UNREF_CACHE
mnt_unref_cache((struct libmnt_cache *)arg);
#else
mnt_free_cache((struct libmnt_cache *)arg);
#endif
}
/*
* Passed a vector of partitions and a libmount table,
* check if any of the partitions in the vector is referenced in the table.
* Note that mnt_table_find_srcpath() also resolves mounts by symlinks.
*/
static int check_mnt_table(const struct vector_s *parts,
struct libmnt_table *tbl,
const char *table_name)
{
unsigned int i;
const char *sysname;
char devpath[PATH_MAX];
vector_foreach_slot(parts, sysname, i) {
if (!safe_sprintf(devpath, "/dev/%s", sysname) &&
mnt_table_find_srcpath(tbl, devpath,
MNT_ITER_FORWARD) != NULL) {
condlog(4, "%s: found %s in %s", __func__,
sysname, table_name);
return 1;
}
}
return 0;
}
static int check_mountinfo(const struct vector_s *parts)
{
static const char mountinfo[] = "/proc/self/mountinfo";
struct libmnt_table *tbl;
struct libmnt_cache *cache;
FILE *stream;
int used = 0, ret;
tbl = mnt_new_table();
if (!tbl )
return -errno;
pthread_cleanup_push(cleanup_table, tbl);
cache = mnt_new_cache();
if (cache) {
pthread_cleanup_push(cleanup_cache, cache);
if (mnt_table_set_cache(tbl, cache) == 0) {
stream = fopen(mountinfo, "r");
if (stream != NULL) {
pthread_cleanup_push(cleanup_fclose, stream);
ret = mnt_table_parse_stream(tbl, stream, mountinfo);
pthread_cleanup_pop(1);
if (ret == 0)
used = check_mnt_table(parts, tbl,
"mountinfo");
}
}
pthread_cleanup_pop(1);
}
pthread_cleanup_pop(1);
return used;
}
#ifdef LIBMOUNT_SUPPORTS_SWAP
static int check_swaps(const struct vector_s *parts)
{
struct libmnt_table *tbl;
struct libmnt_cache *cache;
int used = 0, ret;
tbl = mnt_new_table();
if (!tbl )
return -errno;
pthread_cleanup_push(cleanup_table, tbl);
cache = mnt_new_cache();
if (cache) {
pthread_cleanup_push(cleanup_cache, cache);
if (mnt_table_set_cache(tbl, cache) == 0) {
ret = mnt_table_parse_swaps(tbl, NULL);
if (ret == 0)
used = check_mnt_table(parts, tbl, "swaps");
}
pthread_cleanup_pop(1);
}
pthread_cleanup_pop(1);
return used;
}
#else
static int check_swaps(const struct vector_s *parts __attribute__((unused)))
{
return 0;
}
#endif
/*
* Given a block device, check if the device itself or any of its
* partitions is in use
* - by sysfs holders (e.g. LVM)
* - mounted according to /proc/self/mountinfo
* - used as swap
*/
static int is_device_in_use(struct udev_device *udevice)
{
const char *syspath;
vector parts;
int used = 0, ret;
syspath = udev_device_get_syspath(udevice);
if (!syspath)
return -ENOMEM;
parts = vector_alloc();
if (!parts)
return -ENOMEM;
pthread_cleanup_push_cast(free_strvec, parts);
if ((ret = read_partitions(syspath, parts)) == 0)
used = check_all_holders(parts) > 0 ||
check_mountinfo(parts) > 0 ||
check_swaps(parts) > 0;
pthread_cleanup_pop(1);
if (ret < 0)
return ret;
condlog(3, "%s: %s is %sin use", __func__, syspath, used ? "" : "not ");
return used;
}
int
is_path_valid(const char *name, struct config *conf, struct path *pp,
bool check_multipathd)
{
int r;
int fd;
const char *prop;
if (!pp || !name || !conf)
return PATH_IS_ERROR;
if (conf->find_multipaths <= FIND_MULTIPATHS_UNDEF ||
conf->find_multipaths >= FIND_MULTIPATHS_LAST__)
return PATH_IS_ERROR;
if (safe_sprintf(pp->dev, "%s", name))
return PATH_IS_ERROR;
if (sysfs_is_multipathed(pp, true)) {
if (pp->wwid[0] == '\0')
return PATH_IS_ERROR;
return PATH_IS_VALID_NO_CHECK;
}
if (check_multipathd) {
fd = mpath_connect__(1);
if (fd < 0) {
if (errno != EAGAIN) {
condlog(3, "multipathd not running");
return PATH_IS_NOT_VALID;
}
} else
mpath_disconnect(fd);
}
pp->udev = udev_device_new_from_subsystem_sysname(udev, "block", name);
if (!pp->udev)
return PATH_IS_ERROR;
prop = udev_device_get_property_value(pp->udev, "DEVTYPE");
if (prop == NULL || strcmp(prop, "disk"))
return PATH_IS_NOT_VALID;
r = pathinfo(pp, conf, DI_SYSFS | DI_WWID | DI_BLACKLIST);
if (r == PATHINFO_SKIPPED)
return PATH_IS_NOT_VALID;
else if (r)
return PATH_IS_ERROR;
if (pp->wwid[0] == '\0')
return PATH_IS_NOT_VALID;
r = is_failed_wwid(pp->wwid);
if (r != WWID_IS_NOT_FAILED) {
if (r == WWID_IS_FAILED)
return PATH_IS_NOT_VALID;
return PATH_IS_ERROR;
}
if ((conf->find_multipaths == FIND_MULTIPATHS_GREEDY ||
conf->find_multipaths == FIND_MULTIPATHS_SMART) &&
is_device_in_use(pp->udev) > 0)
return PATH_IS_NOT_VALID;
if (conf->find_multipaths == FIND_MULTIPATHS_GREEDY)
return PATH_IS_VALID;
if (check_wwids_file(pp->wwid, 0) == 0)
return PATH_IS_VALID_NO_CHECK;
if (dm_find_map_by_wwid(pp->wwid, NULL, NULL) == DMP_OK)
return PATH_IS_VALID;
/* all these act like FIND_MULTIPATHS_STRICT for finding if a
* path is valid */
if (conf->find_multipaths != FIND_MULTIPATHS_SMART)
return PATH_IS_NOT_VALID;
return PATH_IS_MAYBE_VALID;
}
|