[dm-devel] [PATCH v2 1/3] libmultipath: embed dm_info in multipath structure
Benjamin Marzinski
bmarzins at redhat.com
Wed Dec 15 17:44:08 UTC 2021
Instead of having multipath allocate its dm_info structure, it should
just embed it to save on the extra allocations. This also lets us have
only one function that all callers use to fill a dm_info structure.
Signed-off-by: Benjamin Marzinski <bmarzins at redhat.com>
---
libmultipath/configure.c | 7 +++--
libmultipath/devmapper.c | 51 +++++++++++--------------------
libmultipath/devmapper.h | 3 +-
libmultipath/libmultipath.version | 1 +
libmultipath/print.c | 12 +++++---
libmultipath/structs.c | 10 ++----
libmultipath/structs.h | 3 +-
multipathd/main.c | 2 +-
8 files changed, 37 insertions(+), 52 deletions(-)
diff --git a/libmultipath/configure.c b/libmultipath/configure.c
index 67459769..ec38f325 100644
--- a/libmultipath/configure.c
+++ b/libmultipath/configure.c
@@ -450,12 +450,12 @@ get_udev_for_mpp(const struct multipath *mpp)
dev_t devnum;
struct udev_device *udd;
- if (!mpp || !mpp->dmi) {
+ if (!mpp || !has_dm_info(mpp)) {
condlog(1, "%s called with empty mpp", __func__);
return NULL;
}
- devnum = makedev(mpp->dmi->major, mpp->dmi->minor);
+ devnum = makedev(mpp->dmi.major, mpp->dmi.minor);
udd = udev_device_new_from_devnum(udev, 'b', devnum);
if (!udd) {
condlog(1, "failed to get udev device for %s", mpp->alias);
@@ -574,7 +574,8 @@ sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)
return 0;
max_sectors_kb = mpp->max_sectors_kb;
if (is_reload) {
- if (!mpp->dmi && dm_get_info(mpp->alias, &mpp->dmi) != 0) {
+ if (!has_dm_info(mpp) &&
+ dm_get_info(mpp->alias, &mpp->dmi) != 0) {
condlog(1, "failed to get dm info for %s", mpp->alias);
return 1;
}
diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c
index c0eb3351..36038150 100644
--- a/libmultipath/devmapper.c
+++ b/libmultipath/devmapper.c
@@ -611,12 +611,21 @@ int dm_addmap_reload(struct multipath *mpp, char *params, int flush)
return 0;
}
-static int
-do_get_info(const char *name, struct dm_info *info)
+bool
+has_dm_info(const struct multipath *mpp)
+{
+ return (mpp && mpp->dmi.exists != 0);
+}
+
+int
+dm_get_info(const char *name, struct dm_info *info)
{
int r = -1;
struct dm_task *dmt;
+ if (!name || !info)
+ return r;
+
if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
return r;
@@ -646,7 +655,7 @@ int dm_map_present(const char * str)
{
struct dm_info info;
- return (do_get_info(str, &info) == 0);
+ return (dm_get_info(str, &info) == 0);
}
int dm_get_map(const char *name, unsigned long long *size, char **outparams)
@@ -969,7 +978,7 @@ dm_dev_t (const char * mapname, char * dev_t, int len)
{
struct dm_info info;
- if (do_get_info(mapname, &info) != 0)
+ if (dm_get_info(mapname, &info) != 0)
return 1;
if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
@@ -1013,7 +1022,7 @@ dm_get_major_minor(const char *name, int *major, int *minor)
{
struct dm_info info;
- if (do_get_info(name, &info) != 0)
+ if (dm_get_info(name, &info) != 0)
return -1;
*major = info.major;
@@ -1367,7 +1376,7 @@ dm_geteventnr (const char *name)
{
struct dm_info info;
- if (do_get_info(name, &info) != 0)
+ if (dm_get_info(name, &info) != 0)
return -1;
return info.event_nr;
@@ -1378,7 +1387,7 @@ dm_is_suspended(const char *name)
{
struct dm_info info;
- if (do_get_info(name, &info) != 0)
+ if (dm_get_info(name, &info) != 0)
return -1;
return info.suspended;
@@ -1542,7 +1551,7 @@ dm_get_deferred_remove (const char * mapname)
{
struct dm_info info;
- if (do_get_info(mapname, &info) != 0)
+ if (dm_get_info(mapname, &info) != 0)
return -1;
return info.deferred_remove;
@@ -1583,32 +1592,6 @@ dm_cancel_deferred_remove (struct multipath *mpp __attribute__((unused)))
#endif
-static struct dm_info *
-alloc_dminfo (void)
-{
- return calloc(1, sizeof(struct dm_info));
-}
-
-int
-dm_get_info (const char * mapname, struct dm_info ** dmi)
-{
- if (!mapname)
- return 1;
-
- if (!*dmi)
- *dmi = alloc_dminfo();
-
- if (!*dmi)
- return 1;
-
- if (do_get_info(mapname, *dmi) != 0) {
- free(*dmi);
- *dmi = NULL;
- return 1;
- }
- return 0;
-}
-
struct rename_data {
const char *old;
char *new;
diff --git a/libmultipath/devmapper.h b/libmultipath/devmapper.h
index 45a676de..703f3bf8 100644
--- a/libmultipath/devmapper.h
+++ b/libmultipath/devmapper.h
@@ -70,7 +70,8 @@ char * dm_mapname(int major, int minor);
int dm_remove_partmaps (const char * mapname, int need_sync,
int deferred_remove);
int dm_get_uuid(const char *name, char *uuid, int uuid_len);
-int dm_get_info (const char * mapname, struct dm_info ** dmi);
+bool has_dm_info(const struct multipath *mpp);
+int dm_get_info (const char * mapname, struct dm_info *dmi);
int dm_rename (const char * old, char * new, char * delim, int skip_kpartx);
int dm_reassign(const char * mapname);
int dm_reassign_table(const char *name, char *old, char *new);
diff --git a/libmultipath/libmultipath.version b/libmultipath/libmultipath.version
index 9c7ffa71..7cdce9fc 100644
--- a/libmultipath/libmultipath.version
+++ b/libmultipath/libmultipath.version
@@ -121,6 +121,7 @@ global:
get_used_hwes;
get_vpd_sgio;
group_by_prio;
+ has_dm_info;
init_checkers;
init_config;
init_foreign;
diff --git a/libmultipath/print.c b/libmultipath/print.c
index 221b515f..c93fffd4 100644
--- a/libmultipath/print.c
+++ b/libmultipath/print.c
@@ -171,8 +171,8 @@ snprint_name (struct strbuf *buff, const struct multipath * mpp)
static int
snprint_sysfs (struct strbuf *buff, const struct multipath * mpp)
{
- if (mpp->dmi)
- return print_strbuf(buff, "dm-%i", mpp->dmi->minor);
+ if (has_dm_info(mpp))
+ return print_strbuf(buff, "dm-%i", mpp->dmi.minor);
else
return append_strbuf_str(buff, "undef");
}
@@ -180,9 +180,9 @@ snprint_sysfs (struct strbuf *buff, const struct multipath * mpp)
static int
snprint_ro (struct strbuf *buff, const struct multipath * mpp)
{
- if (!mpp->dmi)
+ if (!has_dm_info(mpp))
return append_strbuf_str(buff, "undef");
- if (mpp->dmi->read_only)
+ if (mpp->dmi.read_only)
return append_strbuf_str(buff, "ro");
else
return append_strbuf_str(buff, "rw");
@@ -256,7 +256,9 @@ snprint_nb_paths (struct strbuf *buff, const struct multipath * mpp)
static int
snprint_dm_map_state (struct strbuf *buff, const struct multipath * mpp)
{
- if (mpp->dmi && mpp->dmi->suspended)
+ if (!has_dm_info(mpp))
+ return append_strbuf_str(buff, "undef");
+ else if (mpp->dmi.suspended)
return append_strbuf_str(buff, "suspend");
else
return append_strbuf_str(buff, "active");
diff --git a/libmultipath/structs.c b/libmultipath/structs.c
index d1b8aa33..17f4baf6 100644
--- a/libmultipath/structs.c
+++ b/libmultipath/structs.c
@@ -18,6 +18,7 @@
#include "prio.h"
#include "prioritizers/alua_spc3.h"
#include "dm-generic.h"
+#include "devmapper.h"
struct adapter_group *
alloc_adaptergroup(void)
@@ -278,11 +279,6 @@ free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
mpp->alias = NULL;
}
- if (mpp->dmi) {
- free(mpp->dmi);
- mpp->dmi = NULL;
- }
-
if (!free_paths && mpp->pg) {
struct pathgroup *pgp;
struct path *pp;
@@ -407,10 +403,10 @@ find_mp_by_minor (const struct _vector *mpvec, unsigned int minor)
return NULL;
vector_foreach_slot (mpvec, mpp, i) {
- if (!mpp->dmi)
+ if (!has_dm_info(mpp))
continue;
- if (mpp->dmi->minor == minor)
+ if (mpp->dmi.minor == minor)
return mpp;
}
return NULL;
diff --git a/libmultipath/structs.h b/libmultipath/structs.h
index c0f8929c..7f621941 100644
--- a/libmultipath/structs.h
+++ b/libmultipath/structs.h
@@ -4,6 +4,7 @@
#include <sys/types.h>
#include <inttypes.h>
#include <stdbool.h>
+#include <libdevmapper.h>
#include "prio.h"
#include "byteorder.h"
@@ -386,7 +387,7 @@ struct multipath {
unsigned long long size;
vector paths;
vector pg;
- struct dm_info * dmi;
+ struct dm_info dmi;
/* configlet pointers */
char * alias;
diff --git a/multipathd/main.c b/multipathd/main.c
index 7a57a798..fe40f6e3 100644
--- a/multipathd/main.c
+++ b/multipathd/main.c
@@ -908,7 +908,7 @@ ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
}
if (strcmp(mpp->alias, alias)) {
condlog(2, "%s: minor number mismatch (map %d, event %d)",
- mpp->alias, mpp->dmi->minor, minor);
+ mpp->alias, mpp->dmi.minor, minor);
return 1;
}
return flush_map(mpp, vecs, 0);
--
2.17.2
More information about the dm-devel
mailing list