1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

devices: fix dev_name assumptions

dev_name(dev) returns "[unknown]" if there are no names
on dev->aliases.  It's meant mainly for log messages.

Many places assume a valid path name is returned, and
use it directly.  A caller that wants to use the path
from dev_name() must first check if the dev has any
paths with dm_list_empty(&dev->aliases).
This commit is contained in:
David Teigland 2022-02-22 15:03:11 -06:00
parent 00c3069872
commit 4eb04c8c05
10 changed files with 75 additions and 8 deletions

View File

@ -2947,6 +2947,10 @@ int add_areas_line(struct dev_manager *dm, struct lv_segment *seg,
/* FIXME Avoid repeating identical stat in dm_tree_node_add_target_area */ /* FIXME Avoid repeating identical stat in dm_tree_node_add_target_area */
for (s = start_area; s < areas; s++) { for (s = start_area; s < areas; s++) {
/* FIXME: dev_name() does not return NULL! It needs to check if dm_list_empty(&dev->aliases)
but this knot of logic is too complex to pull apart without careful deconstruction. */
if ((seg_type(seg, s) == AREA_PV && if ((seg_type(seg, s) == AREA_PV &&
(!seg_pvseg(seg, s) || !seg_pv(seg, s) || !seg_dev(seg, s) || (!seg_pvseg(seg, s) || !seg_pv(seg, s) || !seg_dev(seg, s) ||
!(name = dev_name(seg_dev(seg, s))) || !*name || !(name = dev_name(seg_dev(seg, s))) || !*name ||
@ -2965,7 +2969,10 @@ int add_areas_line(struct dev_manager *dm, struct lv_segment *seg,
return_0; return_0;
num_error_areas++; num_error_areas++;
} else if (seg_type(seg, s) == AREA_PV) { } else if (seg_type(seg, s) == AREA_PV) {
if (!dm_tree_node_add_target_area(node, dev_name(seg_dev(seg, s)), NULL, struct device *dev = seg_dev(seg, s);
name = dm_list_empty(&dev->aliases) ? NULL : dev_name(dev);
if (!dm_tree_node_add_target_area(node, name, NULL,
(seg_pv(seg, s)->pe_start + (extent_size * seg_pe(seg, s))))) (seg_pv(seg, s)->pe_start + (extent_size * seg_pe(seg, s)))))
return_0; return_0;
num_existing_areas++; num_existing_areas++;

View File

@ -966,6 +966,9 @@ static int _wipe_known_signatures_with_blkid(struct device *dev, const char *nam
/* TODO: Should we check for valid dev - _dev_is_valid(dev)? */ /* TODO: Should we check for valid dev - _dev_is_valid(dev)? */
if (dm_list_empty(&dev->aliases))
goto_out;
if (!(probe = blkid_new_probe_from_filename(dev_name(dev)))) { if (!(probe = blkid_new_probe_from_filename(dev_name(dev)))) {
log_error("Failed to create a new blkid probe for device %s.", dev_name(dev)); log_error("Failed to create a new blkid probe for device %s.", dev_name(dev));
goto out; goto out;

View File

@ -347,6 +347,8 @@ const char *device_id_system_read(struct cmd_context *cmd, struct device *dev, u
} }
else if (idtype == DEV_ID_TYPE_DEVNAME) { else if (idtype == DEV_ID_TYPE_DEVNAME) {
if (dm_list_empty(&dev->aliases))
goto_bad;
if (!(idname = strdup(dev_name(dev)))) if (!(idname = strdup(dev_name(dev))))
goto_bad; goto_bad;
return idname; return idname;
@ -955,6 +957,10 @@ int device_id_add(struct cmd_context *cmd, struct device *dev, const char *pvid_
if (!dev_get_partition_number(dev, &part)) if (!dev_get_partition_number(dev, &part))
return_0; return_0;
/* Ensure valid dev_name(dev) below. */
if (dm_list_empty(&dev->aliases))
return_0;
/* /*
* When enable_devices_file=0 and pending_devices_file=1 we let * When enable_devices_file=0 and pending_devices_file=1 we let
* pvcreate/vgcreate add new du's to cmd->use_devices. These du's may * pvcreate/vgcreate add new du's to cmd->use_devices. These du's may
@ -1842,6 +1848,9 @@ void device_ids_validate(struct cmd_context *cmd, struct dm_list *scanned_devs,
if (dev->flags & DEV_SCAN_NOT_READ) if (dev->flags & DEV_SCAN_NOT_READ)
continue; continue;
if (dm_list_empty(&dev->aliases))
continue;
if (!cmd->filter->passes_filter(cmd, cmd->filter, dev, "persistent")) { if (!cmd->filter->passes_filter(cmd, cmd->filter, dev, "persistent")) {
log_warn("Devices file %s is excluded by filter: %s.", log_warn("Devices file %s is excluded by filter: %s.",
dev_name(dev), dev_filtered_reason(dev)); dev_name(dev), dev_filtered_reason(dev));
@ -2225,14 +2234,14 @@ void device_ids_find_renamed_devs(struct cmd_context *cmd, struct dm_list *dev_l
dm_list_iterate_items(dil, &search_pvids) { dm_list_iterate_items(dil, &search_pvids) {
char *dup_devname1, *dup_devname2, *dup_devname3; char *dup_devname1, *dup_devname2, *dup_devname3;
if (!dil->dev) { if (!dil->dev || dm_list_empty(&dil->dev->aliases)) {
not_found++; not_found++;
continue; continue;
} }
found++;
dev = dil->dev; dev = dil->dev;
devname = dev_name(dev); devname = dev_name(dev);
found++;
if (!(du = get_du_for_pvid(cmd, dil->pvid))) { if (!(du = get_du_for_pvid(cmd, dil->pvid))) {
/* shouldn't happen */ /* shouldn't happen */

View File

@ -500,6 +500,8 @@ int validate_hints(struct cmd_context *cmd, struct dm_list *hints)
if (!(iter = dev_iter_create(NULL, 0))) if (!(iter = dev_iter_create(NULL, 0)))
return 0; return 0;
while ((dev = dev_iter_get(cmd, iter))) { while ((dev = dev_iter_get(cmd, iter))) {
if (dm_list_empty(&dev->aliases))
continue;
if (!(hint = _find_hint_name(hints, dev_name(dev)))) if (!(hint = _find_hint_name(hints, dev_name(dev))))
continue; continue;

View File

@ -1130,6 +1130,7 @@ int label_scan_vg_online(struct cmd_context *cmd, const char *vgname,
* sure to find the device. * sure to find the device.
*/ */
if (try_dev_scan) { if (try_dev_scan) {
log_debug("Repeat dev cache scan to translate devnos.");
dev_cache_scan(cmd); dev_cache_scan(cmd);
dm_list_iterate_items(po, &pvs_online) { dm_list_iterate_items(po, &pvs_online) {
if (po->dev) if (po->dev)
@ -1736,6 +1737,12 @@ void label_scan_invalidate_lvs(struct cmd_context *cmd, struct dm_list *lvs)
struct lv_list *lvl; struct lv_list *lvl;
dev_t devt; dev_t devt;
/*
* FIXME: this is all unnecessary unless there are PVs stacked on LVs,
* so we can skip all of this if scan_lvs=0.
*/
log_debug("invalidating devs for any pvs on lvs");
if (get_device_list(NULL, &devs, &devs_features)) { if (get_device_list(NULL, &devs, &devs_features)) {
if (devs_features & DM_DEVICE_LIST_HAS_UUID) { if (devs_features & DM_DEVICE_LIST_HAS_UUID) {
dm_list_iterate_items(dm_dev, devs) dm_list_iterate_items(dm_dev, devs)
@ -1879,10 +1886,24 @@ int label_scan_open_rw(struct device *dev)
int label_scan_reopen_rw(struct device *dev) int label_scan_reopen_rw(struct device *dev)
{ {
const char *name;
int flags = 0; int flags = 0;
int prev_fd = dev->bcache_fd; int prev_fd = dev->bcache_fd;
int fd; int fd;
if (dm_list_empty(&dev->aliases)) {
log_error("Cannot reopen rw device %d:%d with no valid paths di %d fd %d.",
(int)MAJOR(dev->dev), (int)MINOR(dev->dev), dev->bcache_di, dev->bcache_fd);
return 0;
}
name = dev_name(dev);
if (!name || name[0] != '/') {
log_error("Cannot reopen rw device %d:%d with no valid name di %d fd %d.",
(int)MAJOR(dev->dev), (int)MINOR(dev->dev), dev->bcache_di, dev->bcache_fd);
return 0;
}
if (!(dev->flags & DEV_IN_BCACHE)) { if (!(dev->flags & DEV_IN_BCACHE)) {
if ((dev->bcache_fd != -1) || (dev->bcache_di != -1)) { if ((dev->bcache_fd != -1) || (dev->bcache_di != -1)) {
/* shouldn't happen */ /* shouldn't happen */
@ -1912,7 +1933,7 @@ int label_scan_reopen_rw(struct device *dev)
flags |= O_NOATIME; flags |= O_NOATIME;
flags |= O_RDWR; flags |= O_RDWR;
fd = open(dev_name(dev), flags, 0777); fd = open(name, flags, 0777);
if (fd < 0) { if (fd < 0) {
log_error("Failed to open rw %s errno %d di %d fd %d.", log_error("Failed to open rw %s errno %d di %d fd %d.",
dev_name(dev), errno, dev->bcache_di, dev->bcache_fd); dev_name(dev), errno, dev->bcache_di, dev->bcache_fd);

View File

@ -272,6 +272,8 @@ static void _lockd_retrive_vg_pv_list(struct volume_group *vg,
i = 0; i = 0;
dm_list_iterate_items(pvl, &vg->pvs) { dm_list_iterate_items(pvl, &vg->pvs) {
if (!pvl->pv->dev || dm_list_empty(&pvl->pv->dev->aliases))
continue;
lock_pvs->path[i] = strdup(pv_dev_name(pvl->pv)); lock_pvs->path[i] = strdup(pv_dev_name(pvl->pv));
if (!lock_pvs->path[i]) { if (!lock_pvs->path[i]) {
log_error("Fail to allocate PV path for VG %s", vg->name); log_error("Fail to allocate PV path for VG %s", vg->name);
@ -341,6 +343,8 @@ static void _lockd_retrive_lv_pv_list(struct volume_group *vg,
dm_list_iterate_items(pvl, &vg->pvs) { dm_list_iterate_items(pvl, &vg->pvs) {
if (lv_is_on_pv(lv, pvl->pv)) { if (lv_is_on_pv(lv, pvl->pv)) {
if (!pvl->pv->dev || dm_list_empty(&pvl->pv->dev->aliases))
continue;
lock_pvs->path[i] = strdup(pv_dev_name(pvl->pv)); lock_pvs->path[i] = strdup(pv_dev_name(pvl->pv));
if (!lock_pvs->path[i]) { if (!lock_pvs->path[i]) {
log_error("Fail to allocate PV path for LV %s/%s", log_error("Fail to allocate PV path for LV %s/%s",

View File

@ -1231,14 +1231,23 @@ int remove_mirrors_from_segments(struct logical_volume *lv,
const char *get_pvmove_pvname_from_lv_mirr(const struct logical_volume *lv_mirr) const char *get_pvmove_pvname_from_lv_mirr(const struct logical_volume *lv_mirr)
{ {
struct lv_segment *seg; struct lv_segment *seg;
struct device *dev;
dm_list_iterate_items(seg, &lv_mirr->segments) { dm_list_iterate_items(seg, &lv_mirr->segments) {
if (!seg_is_mirrored(seg)) if (!seg_is_mirrored(seg))
continue; continue;
if (seg_type(seg, 0) == AREA_PV) if (seg_type(seg, 0) == AREA_PV) {
return dev_name(seg_dev(seg, 0)); dev = seg_dev(seg, 0);
if (seg_type(seg, 0) == AREA_LV) if (!dev || dm_list_empty(&dev->aliases))
return dev_name(seg_dev(first_seg(seg_lv(seg, 0)), 0)); return NULL;
return dev_name(dev);
}
if (seg_type(seg, 0) == AREA_LV) {
dev = seg_dev(first_seg(seg_lv(seg, 0)), 0);
if (!dev || dm_list_empty(&dev->aliases))
return NULL;
return dev_name(dev);
}
} }
return NULL; return NULL;

View File

@ -152,6 +152,11 @@ static int _create_pv_entry(struct dm_pool *mem, struct pv_list *pvl,
struct pv_list *new_pvl = NULL, *pvl2; struct pv_list *new_pvl = NULL, *pvl2;
struct dm_list *pe_ranges; struct dm_list *pe_ranges;
if (!pvl->pv->dev || dm_list_empty(&pvl->pv->dev->aliases)) {
log_error("Failed to create PV entry for missing device.");
return 0;
}
pvname = pv_dev_name(pvl->pv); pvname = pv_dev_name(pvl->pv);
if (allocatable_only && !(pvl->pv->status & ALLOCATABLE_PV)) { if (allocatable_only && !(pvl->pv->status & ALLOCATABLE_PV)) {
log_warn("WARNING: Physical volume %s not allocatable.", pvname); log_warn("WARNING: Physical volume %s not allocatable.", pvname);

View File

@ -679,6 +679,11 @@ int vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
return r; return r;
} }
if (!pv->dev || dm_list_empty(&pv->dev->aliases)) {
log_error("No device found for PV.");
return r;
}
log_debug("vgreduce_single VG %s PV %s", vg->name, pv_dev_name(pv)); log_debug("vgreduce_single VG %s PV %s", vg->name, pv_dev_name(pv));
if (pv_pe_alloc_count(pv)) { if (pv_pe_alloc_count(pv)) {

View File

@ -33,6 +33,8 @@ aux udev_wait
ls -la "${LOOP}"* ls -la "${LOOP}"*
test -e "${LOOP}p1" test -e "${LOOP}p1"
aux lvmconf 'devices/scan = "/dev"'
aux extend_filter "a|$LOOP|" aux extend_filter "a|$LOOP|"
aux extend_devices "$LOOP" aux extend_devices "$LOOP"