Skip to contentSkip to conversation

multipath-tools: improvements for pretty-printing code

PATCH In review [email protected] 2 participants 15 messages Nov 29, 2021 – Nov 30, 2021 Device mapper

lore
Share & tools
lore
#0 lore raw

v2 cover multipath-tools: improvements for pretty-printing code

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

this series attempts to improve the robustness of the code by making the lookup tables used for pretty printing and wildcard handling static const arrays. This requires getting rid of the variable "width" field in these arrays. I could have simply split of "width" into a separate global array variable, but as the field width are designed to change depending on the data being printed, it makes more sense to use local variables for it and pass them to the actual table-formatting code.

A couple of minor cleanups are done along the way.

Changes in v2:

- 10/13: fix a compilation error with clang because of duplicate typedef for fieldwidth_t
- whitespace fixes
- 12/13: new, fix a compile error with clang-13
- 13/13: new, fix an asan error reported by Lixiaokeng

As always, feedback welcome
Martin

Martin Wilck (13):
libmultipath: make multipath_data etc. static
libmultipath: move path_data etc. to print.c
libmultipath: make pd_lookup() etc. return an index
libmultipath: use ARRAY_SIZE for iterating over wildcard arrays
libmultipath: drop padding code in _snprint_pathgroup()
libmultipath: snprint_foreign_topology(): split out lockless variant
multipathd: drop unnecessary path layout calls
libmultipath: add a cleanup function for unsigned char *
libmultipath: make sprint_path_marginal() static
libmultipath: introduce width argument for pretty-printing functions
libmultipath: change wildcard handler tables to static const
libmultipath: fix compilation error with clang 13
libmultipath: remove_map(): make sure orphaned paths aren't referenced

 libmultipath/foreign.c                   |  74 +++--
 libmultipath/foreign.h                   |  23 +-
 libmultipath/generic.h                   |   8 +
 libmultipath/libmultipath.version        |   5 +-
 libmultipath/print.c                     | 356 +++++++++++++----------
 libmultipath/print.h                     |  58 ++--
 libmultipath/prioritizers/weightedpath.c |   3 +-
 libmultipath/structs_vec.c               |   4 +
 libmultipath/util.c                      |   5 +
 libmultipath/util.h                      |   1 +
 multipath/main.c                         |   7 +-
 multipathd/cli_handlers.c                |  75 +++--
 12 files changed, 367 insertions(+), 252 deletions(-)
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#1 lore raw

PATCH v2 1/13 libmultipath: make multipath_data etc. static

From: Martin Wilck <[email protected]>

These variables are only used in print.c

---
 libmultipath/print.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libmultipath/print.c +3 -3
index e61349f..938eaad 100644
@@ -771,7 +771,7 @@ snprint_path_vpd_data(struct strbuf *buff, const struct path * pp)
 	return append_strbuf_str(buff, "[undef]");
 }
 
-struct multipath_data mpd[] = {
+static struct multipath_data mpd[] = {
 	{'n', "name",          0, snprint_name},
 	{'w', "uuid",          0, snprint_multipath_uuid},
 	{'d', "sysfs",         0, snprint_sysfs},
@@ -799,7 +799,7 @@ struct multipath_data mpd[] = {
 	{0, NULL, 0 , NULL}
 };
 
-struct path_data pd[] = {
+static struct path_data pd[] = {
 	{'w', "uuid",          0, snprint_path_uuid},
 	{'i', "hcil",          0, snprint_hcil},
 	{'d', "dev",           0, snprint_dev},
@@ -828,7 +828,7 @@ struct path_data pd[] = {
 	{0, NULL, 0 , NULL}
 };
 
-struct pathgroup_data pgd[] = {
+static struct pathgroup_data pgd[] = {
 	{'s', "selector",      0, snprint_pg_selector},
 	{'p', "pri",           0, snprint_pg_pri},
 	{'t', "dm_st",         0, snprint_pg_state},
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#2 lore raw

PATCH v2 2/13 libmultipath: move path_data etc. to print.c

In reply to [email protected]

From: Martin Wilck <[email protected]>

These structs are only used in print.c

---
 libmultipath/print.c | 21 +++++++++++++++++++++
 libmultipath/print.h | 21 ---------------------
 2 files changed, 21 insertions(+), 21 deletions(-)
libmultipath/print.c +21 -0
index 938eaad..4992cc8 100644
@@ -100,6 +100,27 @@
 
 #define PROGRESS_LEN  10
 
+struct path_data {
+	char wildcard;
+	char * header;
+	unsigned int width;
+	int (*snprint)(struct strbuf *, const struct path * pp);
+};
+
+struct multipath_data {
+	char wildcard;
+	char * header;
+	unsigned int width;
+	int (*snprint)(struct strbuf *, const struct multipath * mpp);
+};
+
+struct pathgroup_data {
+	char wildcard;
+	char * header;
+	unsigned int width;
+	int (*snprint)(struct strbuf *, const struct pathgroup * pgp);
+};
+
 #define MAX(x,y) (((x) > (y)) ? (x) : (y))
 #define MIN(x,y) (((x) > (y)) ? (y) : (x))
 /*
libmultipath/print.h +0 -21
index b149275..4322f6e 100644
@@ -9,27 +9,6 @@
 
 struct strbuf;
 
-struct path_data {
-	char wildcard;
-	char * header;
-	unsigned int width;
-	int (*snprint)(struct strbuf *, const struct path * pp);
-};
-
-struct multipath_data {
-	char wildcard;
-	char * header;
-	unsigned int width;
-	int (*snprint)(struct strbuf *, const struct multipath * mpp);
-};
-
-struct pathgroup_data {
-	char wildcard;
-	char * header;
-	unsigned int width;
-	int (*snprint)(struct strbuf *, const struct pathgroup * pgp);
-};
-
 enum layout_reset {
 	LAYOUT_RESET_NOT,
 	LAYOUT_RESET_ZERO,
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#3 lore raw

PATCH v2 3/13 libmultipath: make pd_lookup() etc. return an index

In reply to [email protected]

From: Martin Wilck <[email protected]>

This will allow us to remove the mutable width field from path_data.

---
 libmultipath/print.c | 64 ++++++++++++++++++++++++++------------------
 1 file changed, 38 insertions(+), 26 deletions(-)
libmultipath/print.c +38 -26
index 4992cc8..37222bf 100644
@@ -976,73 +976,70 @@ _get_multipath_layout (const struct _vector *gmvec,
 	}
 }
 
-static struct multipath_data *
-mpd_lookup(char wildcard)
+static int mpd_lookup(char wildcard)
 {
 	int i;
 
 	for (i = 0; mpd[i].header; i++)
 		if (mpd[i].wildcard == wildcard)
-			return &mpd[i];
+			return i;
 
-	return NULL;
+	return -1;
 }
 
 int snprint_multipath_attr(const struct gen_multipath* gm,
 			   struct strbuf *buf, char wildcard)
 {
 	const struct multipath *mpp = gen_multipath_to_dm(gm);
-	struct multipath_data *mpd = mpd_lookup(wildcard);
+	int i = mpd_lookup(wildcard);
 
-	if (mpd == NULL)
+	if (i == -1)
 		return 0;
-	return mpd->snprint(buf, mpp);
+	return mpd[i].snprint(buf, mpp);
 }
 
-static struct path_data *
-pd_lookup(char wildcard)
+static int pd_lookup(char wildcard)
 {
 	int i;
 
 	for (i = 0; pd[i].header; i++)
 		if (pd[i].wildcard == wildcard)
-			return &pd[i];
+			return i;
 
-	return NULL;
+	return -1;
 }
 
 int snprint_path_attr(const struct gen_path* gp,
 		      struct strbuf *buf, char wildcard)
 {
 	const struct path *pp = gen_path_to_dm(gp);
-	struct path_data *pd = pd_lookup(wildcard);
+	int i = pd_lookup(wildcard);
 
-	if (pd == NULL)
+	if (i == -1)
 		return 0;
-	return pd->snprint(buf, pp);
+	return pd[i].snprint(buf, pp);
 }
 
-static struct pathgroup_data *
-pgd_lookup(char wildcard)
+static int pgd_lookup(char wildcard)
 {
 	int i;
 
 	for (i = 0; pgd[i].header; i++)
 		if (pgd[i].wildcard == wildcard)
-			return &pgd[i];
+			return i;
 
-	return NULL;
+	return -1;
 }
 
 int snprint_pathgroup_attr(const struct gen_pathgroup* gpg,
 			   struct strbuf *buf, char wildcard)
 {
 	const struct pathgroup *pg = gen_pathgroup_to_dm(gpg);
-	struct pathgroup_data *pdg = pgd_lookup(wildcard);
+	int i = pgd_lookup(wildcard);
 
-	if (pdg == NULL)
+	if (i == -1)
 		return 0;
-	return pdg->snprint(buf, pg);
+	return pgd[i].snprint(buf, pg);
 }
 
 int snprint_multipath_header(struct strbuf *line, const char *format)
@@ -1053,12 +1050,15 @@ int snprint_multipath_header(struct strbuf *line, const char *format)
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
+		int iwc;
+
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if (!(data = mpd_lookup(*format)))
+		if ((iwc = mpd_lookup(*format)) == -1)
 			continue; /* unknown wildcard */
+		data = &mpd[iwc];
 
 		if ((rc = append_strbuf_str(line, data->header)) < 0)
 			return rc;
@@ -1081,12 +1081,15 @@ int _snprint_multipath(const struct gen_multipath *gmp,
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
+		int iwc;
+
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if (!(data = mpd_lookup(*format)))
+		if ((iwc = mpd_lookup(*format)) == -1)
 			continue; /* unknown wildcard */
+		data = &mpd[iwc];
 
 		if ((rc = gmp->ops->snprint(gmp, line, *format)) < 0)
 			return rc;
@@ -1108,12 +1111,15 @@ int snprint_path_header(struct strbuf *line, const char *format)
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
+		int iwc;
+
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if (!(data = pd_lookup(*format)))
+		if ((iwc = pd_lookup(*format)) == -1)
 			continue; /* unknown wildcard */
+		data = &pd[iwc];
 
 		if ((rc = append_strbuf_str(line, data->header)) < 0)
 			return rc;
@@ -1136,12 +1142,15 @@ int _snprint_path(const struct gen_path *gp, struct strbuf *line,
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
+		int iwc;
+
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if (!(data = pd_lookup(*format)))
+		if ((iwc = pd_lookup(*format)) == -1)
 			continue; /* unknown wildcard */
+		data = &pd[iwc];
 
 		if ((rc = gp->ops->snprint(gp, line, *format)) < 0)
 			return rc;
@@ -1164,12 +1173,15 @@ int _snprint_pathgroup(const struct gen_pathgroup *ggp, struct strbuf *line,
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
+		int iwc;
+
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if (!(data = pgd_lookup(*format)))
+		if ((iwc = pgd_lookup(*format)) == -1)
 			continue; /* unknown wildcard */
+		data = &pgd[iwc];
 
 		if ((rc = ggp->ops->snprint(ggp, line, *format)) < 0)
 			return rc;
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#4 lore raw

PATCH v2 4/13 libmultipath: use ARRAY_SIZE for iterating over wildcard arrays

In reply to [email protected]

From: Martin Wilck <[email protected]>

Avoid the extra empty element at the end of the array, and use the ARRAY_SIZE paradigm that we've adopted elsewhere in multipath-tools.

---
 libmultipath/print.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
libmultipath/print.c +17 -19
index 37222bf..1667d3e 100644
@@ -817,7 +817,6 @@ static struct multipath_data mpd[] = {
 	{'e', "rev",           0, snprint_multipath_rev},
 	{'G', "foreign",       0, snprint_multipath_foreign},
 	{'g', "vpd page data", 0, snprint_multipath_vpd_data},
-	{0, NULL, 0 , NULL}
 };
 
 static struct path_data pd[] = {
@@ -846,7 +845,6 @@ static struct path_data pd[] = {
 	{'0', "failures",      0, snprint_path_failures},
 	{'P', "protocol",      0, snprint_path_protocol},
 	{'I', "init_st",       0, snprint_initialized},
-	{0, NULL, 0 , NULL}
 };
 
 static struct pathgroup_data pgd[] = {
@@ -854,31 +852,31 @@ static struct pathgroup_data pgd[] = {
 	{'p', "pri",           0, snprint_pg_pri},
 	{'t', "dm_st",         0, snprint_pg_state},
 	{'M', "marginal_st",   0, snprint_pg_marginal},
-	{0, NULL, 0 , NULL}
 };
 
 int snprint_wildcards(struct strbuf *buff)
 {
 	int initial_len = get_strbuf_len(buff);
-	int i, rc;
+	unsigned int i;
+	int rc;
 
 	if ((rc = append_strbuf_str(buff, "multipath format wildcards:\n")) < 0)
 		return rc;
-	for (i = 0; mpd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(mpd); i++)
 		if ((rc = print_strbuf(buff, "%%%c  %s\n",
 				       mpd[i].wildcard, mpd[i].header)) < 0)
 			return rc;
 
 	if ((rc = append_strbuf_str(buff, "\npath format wildcards:\n")) < 0)
 		return rc;
-	for (i = 0; pd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(pd); i++)
 		if ((rc = print_strbuf(buff, "%%%c  %s\n",
 				       pd[i].wildcard, pd[i].header)) < 0)
 			return rc;
 
 	if ((rc = append_strbuf_str(buff, "\npathgroup format wildcards:\n")) < 0)
 		return rc;
-	for (i = 0; pgd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(pgd); i++)
 		if ((rc = print_strbuf(buff, "%%%c  %s\n",
 				       pgd[i].wildcard, pgd[i].header)) < 0)
 			return rc;
@@ -915,10 +913,10 @@ reset_width(unsigned int *width, enum layout_reset reset, const char *header)
 void
 _get_path_layout (const struct _vector *gpvec, enum layout_reset reset)
 {
-	int i, j;
+	unsigned int i, j;
 	const struct gen_path *gp;
 
-	for (j = 0; pd[j].header; j++) {
+	for (j = 0; j < ARRAY_SIZE(pd); j++) {
 		STRBUF_ON_STACK(buff);
 
 		reset_width(&pd[j].width, reset, pd[j].header);
@@ -937,9 +935,9 @@ _get_path_layout (const struct _vector *gpvec, enum layout_reset reset)
 static void
 reset_multipath_layout (void)
 {
-	int i;
+	unsigned int i;
 
-	for (i = 0; mpd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(mpd); i++)
 		mpd[i].width = 0;
 }
 
@@ -956,10 +954,10 @@ void
 _get_multipath_layout (const struct _vector *gmvec,
 			    enum layout_reset reset)
 {
-	int i, j;
+	unsigned int i, j;
 	const struct gen_multipath * gm;
 
-	for (j = 0; mpd[j].header; j++) {
+	for (j = 0; j < ARRAY_SIZE(mpd); j++) {
 		STRBUF_ON_STACK(buff);
 
 		reset_width(&mpd[j].width, reset, mpd[j].header);
@@ -978,9 +976,9 @@ _get_multipath_layout (const struct _vector *gmvec,
 
 static int mpd_lookup(char wildcard)
 {
-	int i;
+	unsigned int i;
 
-	for (i = 0; mpd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(mpd); i++)
 		if (mpd[i].wildcard == wildcard)
 			return i;
 
@@ -1000,9 +998,9 @@ int snprint_multipath_attr(const struct gen_multipath* gm,
 
 static int pd_lookup(char wildcard)
 {
-	int i;
+	unsigned int i;
 
-	for (i = 0; pd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(pd); i++)
 		if (pd[i].wildcard == wildcard)
 			return i;
 
@@ -1022,9 +1020,9 @@ int snprint_path_attr(const struct gen_path* gp,
 
 static int pgd_lookup(char wildcard)
 {
-	int i;
+	unsigned int i;
 
-	for (i = 0; pgd[i].header; i++)
+	for (i = 0; i < ARRAY_SIZE(pgd); i++)
 		if (pgd[i].wildcard == wildcard)
 			return i;
 
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#5 lore raw

PATCH v2 5/13 libmultipath: drop padding code in _snprint_pathgroup()

In reply to [email protected]

From: Martin Wilck <[email protected]>

We don't use padding for pathgroup fields, field width is always 0. Remove this dead code.

---
 libmultipath/print.c | 9 ---------
 1 file changed, 9 deletions(-)
libmultipath/print.c +0 -9
index 1667d3e..48562ce 100644
@@ -1167,25 +1167,16 @@ int _snprint_pathgroup(const struct gen_pathgroup *ggp, struct strbuf *line,
 {
 	int initial_len = get_strbuf_len(line);
 	const char *f;
-	struct pathgroup_data *data;
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
-		int iwc;
-
 		if ((rc = __append_strbuf_str(line, format, f - format)) < 0)
 			return rc;
 
 		format = f + 1;
-		if ((iwc = pgd_lookup(*format)) == -1)
-			continue; /* unknown wildcard */
-		data = &pgd[iwc];
 
 		if ((rc = ggp->ops->snprint(ggp, line, *format)) < 0)
 			return rc;
-		else if ((unsigned int)rc < data->width)
-			if ((rc = fill_strbuf(line, ' ', data->width - rc)) < 0)
-				return rc;
 	}
 
 	if ((rc = print_strbuf(line, "%s\n", format)) < 0)
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#6 lore raw

PATCH v2 6/13 libmultipath: snprint_foreign_topology(): split out lockless variant

In reply to [email protected]

From: Martin Wilck <[email protected]>

Factor out the lockless part, which we'll call from elsewhere in a future patch.

---
 libmultipath/foreign.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
libmultipath/foreign.c +16 -9
index e091a1d..c69c5c3 100644
@@ -498,19 +498,12 @@ void foreign_multipath_layout(void)
 	pthread_cleanup_pop(1);
 }
 
-int snprint_foreign_topology(struct strbuf *buf, int verbosity)
+static int __snprint_foreign_topology(struct strbuf *buf, int verbosity)
 {
 	struct foreign *fgn;
 	int i;
 	size_t initial_len = get_strbuf_len(buf);
 
-	rdlock_foreigns();
-	if (foreigns == NULL) {
-		unlock_foreigns(NULL);
-		return 0;
-	}
-	pthread_cleanup_push(unlock_foreigns, NULL);
-
 	vector_foreach_slot(foreigns, fgn, i) {
 		const struct _vector *vec;
 		const struct gen_multipath *gm;
@@ -531,10 +524,24 @@ int snprint_foreign_topology(struct strbuf *buf, int verbosity)
 		pthread_cleanup_pop(1);
 	}
 
-	pthread_cleanup_pop(1);
 	return get_strbuf_len(buf) - initial_len;
 }
 
+int snprint_foreign_topology(struct strbuf *buf, int verbosity)
+{
+	int rc;
+
+	rdlock_foreigns();
+	if (foreigns == NULL) {
+		unlock_foreigns(NULL);
+		return 0;
+	}
+	pthread_cleanup_push(unlock_foreigns, NULL);
+	rc = __snprint_foreign_topology(buf, verbosity);
+	pthread_cleanup_pop(1);
+	return rc;
+}
+
 void print_foreign_topology(int verbosity)
 {
 	STRBUF_ON_STACK(buf);
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#7 lore raw

PATCH v2 7/13 multipathd: drop unnecessary path layout calls

In reply to [email protected]

From: Martin Wilck <[email protected]>

In cli_list_map_fmt(), we only need field widths for map properties, not for path properties. In json ouput, we need no field width padding at all. Likewise in the prstatus related functions.

---
 multipathd/cli_handlers.c | 5 -----
 1 file changed, 5 deletions(-)
multipathd/cli_handlers.c +0 -5
index 1b563e7..1b7f258 100644
@@ -278,7 +278,6 @@ cli_list_map_json (void *v, struct strbuf *reply, void *data)
 	char * param = get_keyparam(v, MAP);
 
 	param = convert_dev(param, 0);
-	get_path_layout(vecs->pathvec, 0);
 	mpp = find_mp_by_str(vecs->mpvec, param);
 
 	if (!mpp)
@@ -400,7 +399,6 @@ cli_list_map_fmt (void *v, struct strbuf *reply, void *data)
 	char * fmt = get_keyparam(v, FMT);
 
 	param = convert_dev(param, 0);
-	get_path_layout(vecs->pathvec, 0);
 	get_multipath_layout(vecs->mpvec, 1);
 	mpp = find_mp_by_str(vecs->mpvec, param);
 	if (!mpp)
@@ -1267,7 +1265,6 @@ cli_getprstatus (void * v, struct strbuf *reply, void * data)
 	char * param = get_keyparam(v, MAP);
 
 	param = convert_dev(param, 0);
-	get_path_layout(vecs->pathvec, 0);
 	mpp = find_mp_by_str(vecs->mpvec, param);
 
 	if (!mpp)
@@ -1291,7 +1288,6 @@ cli_setprstatus(void * v, struct strbuf *reply, void * data)
 	char * param = get_keyparam(v, MAP);
 
 	param = convert_dev(param, 0);
-	get_path_layout(vecs->pathvec, 0);
 	mpp = find_mp_by_str(vecs->mpvec, param);
 
 	if (!mpp)
@@ -1314,7 +1310,6 @@ cli_unsetprstatus(void * v, struct strbuf *reply, void * data)
 	char * param = get_keyparam(v, MAP);
 
 	param = convert_dev(param, 0);
-	get_path_layout(vecs->pathvec, 0);
 	mpp = find_mp_by_str(vecs->mpvec, param);
 
 	if (!mpp)
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#8 lore raw

PATCH v2 8/13 libmultipath: add a cleanup function for unsigned char *

In reply to [email protected]

From: Martin Wilck <[email protected]>

---
 libmultipath/util.c | 5 +++++
 libmultipath/util.h | 1 +
 2 files changed, 6 insertions(+)
libmultipath/util.c +5 -0
index b7952a5..ce5ea73 100644
@@ -466,3 +466,8 @@ void cleanup_charp(char **p)
 {
 	free(*p);
 }
+
+void cleanup_ucharp(unsigned char **p)
+{
+	free(*p);
+}
libmultipath/util.h +1 -0
index 421842a..79d9f32 100644
@@ -125,4 +125,5 @@ static inline void clear_bit_in_bitfield(unsigned int bit, struct bitfield *bf)
 	})
 
 void cleanup_charp(char **p);
+void cleanup_ucharp(unsigned char **p);
 #endif /* _UTIL_H */
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#9 lore raw

PATCH v2 9/13 libmultipath: make sprint_path_marginal() static

In reply to [email protected]

From: Martin Wilck <[email protected]>

---
 libmultipath/print.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/print.c +1 -1
index 48562ce..b5aeca8 100644
@@ -776,7 +776,7 @@ snprint_path_protocol(struct strbuf *buff, const struct path * pp)
 	}
 }
 
-int
+static int
 snprint_path_marginal(struct strbuf *buff, const struct path * pp)
 {
 	if (pp->marginal)
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#10 lore raw

PATCH v2 10/13 libmultipath: introduce width argument for pretty-printing functions

In reply to [email protected]

From: Martin Wilck <[email protected]>

Instead of modifying the global variables to store the field width for pretty-printed table-formatted output, use field-width arrays passed down from the caller. The calling function needs to allocate the array using alloc_path_layout() or alloc_multipath_layout(), and initialize the layout with the data to be printed. This is no different than before, except that the field width is now stored in local variables of the caller.

The type used for column width, fieldwidth_t, is defined to unsigned char. This limits the size of fields (padding in table columns) to 255 characters. That's plenty even for lengthy WWID formats (as WWID_SIZE is 128). The typedef could be changed in the future if necessary.

In some functions, namely _print_multipath_topology() and print_foreign_topology(), field widths need to be initialized. This was true before this patch already, but it wasn't done as the necessity wasn't apparent. Essentially we were using pre-calculated field widths in these functions, or just 0 if no field width calculation had been done previously.

---
 libmultipath/foreign.c            |  55 +++++++++----
 libmultipath/foreign.h            |  23 +++---
 libmultipath/generic.h            |   8 ++
 libmultipath/libmultipath.version |   5 +-
 libmultipath/print.c              | 127 +++++++++++++++++++-----------
 libmultipath/print.h              |  37 +++++----
 multipath/main.c                  |   7 +-
 multipathd/cli_handlers.c         |  70 ++++++++++------
 8 files changed, 218 insertions(+), 114 deletions(-)
libmultipath/foreign.c +40 -15
index c69c5c3..e80eb3e 100644
@@ -30,10 +30,10 @@
 #include "vector.h"
 #include "debug.h"
 #include "util.h"
-#include "foreign.h"
 #include "structs.h"
 #include "structs_vec.h"
 #include "print.h"
+#include "foreign.h"
 #include "strbuf.h"
 
 static vector foreigns;
@@ -437,7 +437,7 @@ void check_foreign(void)
 }
 
 /* Call this after get_path_layout */
-void foreign_path_layout(void)
+void foreign_path_layout(fieldwidth_t *width)
 {
 	struct foreign *fgn;
 	int i;
@@ -457,7 +457,7 @@ void foreign_path_layout(void)
 
 		vec = fgn->get_paths(fgn->context);
 		if (vec != NULL) {
-			_get_path_layout(vec, LAYOUT_RESET_NOT);
+			_get_path_layout(vec, LAYOUT_RESET_NOT, width);
 		}
 		fgn->release_paths(fgn->context, vec);
 
@@ -468,7 +468,7 @@ void foreign_path_layout(void)
 }
 
 /* Call this after get_multipath_layout */
-void foreign_multipath_layout(void)
+void foreign_multipath_layout(fieldwidth_t *width)
 {
 	struct foreign *fgn;
 	int i;
@@ -488,7 +488,7 @@ void foreign_multipath_layout(void)
 
 		vec = fgn->get_multipaths(fgn->context);
 		if (vec != NULL) {
-			_get_multipath_layout(vec, LAYOUT_RESET_NOT);
+			_get_multipath_layout(vec, LAYOUT_RESET_NOT, width);
 		}
 		fgn->release_multipaths(fgn->context, vec);
 
@@ -498,7 +498,8 @@ void foreign_multipath_layout(void)
 	pthread_cleanup_pop(1);
 }
 
-static int __snprint_foreign_topology(struct strbuf *buf, int verbosity)
+static int __snprint_foreign_topology(struct strbuf *buf, int verbosity,
+				      const fieldwidth_t *width)
 {
 	struct foreign *fgn;
 	int i;
@@ -516,7 +517,7 @@ static int __snprint_foreign_topology(struct strbuf *buf, int verbosity)
 		if (vec != NULL) {
 			vector_foreach_slot(vec, gm, j) {
 				if (_snprint_multipath_topology(
-					    gm, buf, verbosity) < 0)
+					    gm, buf, verbosity, width) < 0)
 					break;
 			}
 		}
@@ -527,7 +528,8 @@ static int __snprint_foreign_topology(struct strbuf *buf, int verbosity)
 	return get_strbuf_len(buf) - initial_len;
 }
 
-int snprint_foreign_topology(struct strbuf *buf, int verbosity)
+int snprint_foreign_topology(struct strbuf *buf, int verbosity,
+			     const fieldwidth_t *width)
 {
 	int rc;
 
@@ -537,7 +539,7 @@ int snprint_foreign_topology(struct strbuf *buf, int verbosity)
 		return 0;
 	}
 	pthread_cleanup_push(unlock_foreigns, NULL);
-	rc = __snprint_foreign_topology(buf, verbosity);
+	rc = __snprint_foreign_topology(buf, verbosity, width);
 	pthread_cleanup_pop(1);
 	return rc;
 }
@@ -545,12 +547,35 @@ int snprint_foreign_topology(struct strbuf *buf, int verbosity)
 void print_foreign_topology(int verbosity)
 {
 	STRBUF_ON_STACK(buf);
+	struct foreign *fgn;
+	int i;
+	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
 
-	snprint_foreign_topology(&buf, verbosity);
+	if ((width = alloc_path_layout()) == NULL)
+		return;
+	rdlock_foreigns();
+	if (foreigns == NULL) {
+		unlock_foreigns(NULL);
+		return;
+	}
+	pthread_cleanup_push(unlock_foreigns, NULL);
+	vector_foreach_slot(foreigns, fgn, i) {
+		const struct _vector *vec;
+
+		fgn->lock(fgn->context);
+		pthread_cleanup_push(fgn->unlock, fgn->context);
+		vec = fgn->get_paths(fgn->context);
+		_get_multipath_layout(vec, LAYOUT_RESET_NOT, width);
+		fgn->release_paths(fgn->context, vec);
+		pthread_cleanup_pop(1);
+	}
+	__snprint_foreign_topology(&buf, verbosity, width);
+	pthread_cleanup_pop(1);
 	printf("%s", get_strbuf_str(&buf));
 }
 
-int snprint_foreign_paths(struct strbuf *buf, const char *style, int pretty)
+int snprint_foreign_paths(struct strbuf *buf, const char *style,
+			  const fieldwidth_t *width)
 {
 	struct foreign *fgn;
 	int i;
@@ -574,7 +599,7 @@ int snprint_foreign_paths(struct strbuf *buf, const char *style, int pretty)
 		vec = fgn->get_paths(fgn->context);
 		if (vec != NULL) {
 			vector_foreach_slot(vec, gp, j) {
-				ret = _snprint_path(gp, buf, style, pretty);
+				ret = _snprint_path(gp, buf, style, width);
 				if (ret < 0)
 					break;
 			}
@@ -589,8 +614,8 @@ int snprint_foreign_paths(struct strbuf *buf, const char *style, int pretty)
 	return get_strbuf_len(buf) - initial_len;
 }
 
-int snprint_foreign_multipaths(struct strbuf *buf,
-			       const char *style, int pretty)
+int snprint_foreign_multipaths(struct strbuf *buf, const char *style,
+			       const fieldwidth_t *width)
 {
 	struct foreign *fgn;
 	int i;
@@ -615,7 +640,7 @@ int snprint_foreign_multipaths(struct strbuf *buf,
 		if (vec != NULL) {
 			vector_foreach_slot(vec, gm, j) {
 				ret = _snprint_multipath(gm, buf,
-							 style, pretty);
+							 style, width);
 				if (ret < 0)
 					break;
 			}
libmultipath/foreign.h +14 -9
index 77fc485..cf8f570 100644
@@ -18,7 +18,7 @@
 #define _FOREIGN_H
 #include <stdbool.h>
 #include <libudev.h>
-#define LIBMP_FOREIGN_API ((1 << 8) | 1)
+#define LIBMP_FOREIGN_API ((1 << 8) | 2)
 
 struct strbuf;
 struct context;
@@ -252,15 +252,17 @@ void check_foreign(void);
  * foreign_path_layout()
  * call this before printing paths, after get_path_layout(), to determine
  * output field width.
+ * @param width: an array allocated by alloc_path_layout()
  */
-void foreign_path_layout(void);
+void foreign_path_layout(fieldwidth_t *width);
 
 /**
  * foreign_multipath_layout()
  * call this before printing maps, after get_multipath_layout(), to determine
  * output field width.
+ * @param width: an array allocated by alloc_multipath_layout()
  */
-void foreign_multipath_layout(void);
+void foreign_multipath_layout(fieldwidth_t *width);
 
 /**
  * snprint_foreign_topology(buf, len, verbosity);
@@ -268,9 +270,11 @@ void foreign_multipath_layout(void);
  * '\0' - terminated.
  * @param buf: output buffer
  * @param verbosity: verbosity level
+ * @param width: an array of field widths, initialized by _get_path_layout()
  * @returns: number of printed characters excluding trailing '\0'.
  */
-int snprint_foreign_topology(struct strbuf *buf, int verbosity);
+int snprint_foreign_topology(struct strbuf *buf, int verbosity,
+			     const fieldwidth_t *width);
 
 /**
  * snprint_foreign_paths(buf, len, style, pad);
@@ -278,10 +282,11 @@ int snprint_foreign_topology(struct strbuf *buf, int verbosity);
  * '\0' - terminated.
  * @param buf: output buffer
  * @param style: format string
- * @param pad: whether to pad field width
+ * @param width: array initialized with get_path_layout(), or NULL for no padding
  * @returns: number of printed characters excluding trailing '\0'.
  */
-int snprint_foreign_paths(struct strbuf *buf, const char *style, int pad);
+int snprint_foreign_paths(struct strbuf *buf, const char *style,
+			  const fieldwidth_t *width);
 
 /**
  * snprint_foreign_multipaths(buf, len, style, pad);
@@ -289,11 +294,11 @@ int snprint_foreign_paths(struct strbuf *buf, const char *style, int pad);
  * '\0' - terminated.
  * @param buf: output buffer
  * @param style: format string
- * @param pad: whether to pad field width
+ * @param width: array initialized with get_path_layout(), or NULL for no padding
  * @returns: number of printed characters excluding trailing '\0'.
  */
-int snprint_foreign_multipaths(struct strbuf *buf,
-			       const char *style, int pretty);
+int snprint_foreign_multipaths(struct strbuf *buf, const char *style,
+			       const fieldwidth_t *width);
 
 /**
  * print_foreign_topology(v)
libmultipath/generic.h +8 -0
index 57c123c..e89848f 100644
@@ -18,6 +18,14 @@
 #define _GENERIC_H
 #include "vector.h"
 
+/*
+ * fieldwidth_t is required in print.h and foreign.h.
+ * Defining it twice is not allowed before C11.
+ * So do it here.
+ */
+typedef unsigned char fieldwidth_t;
+#define MAX_FIELD_WIDTH UCHAR_MAX
+
 struct strbuf;
 struct gen_multipath;
 struct gen_pathgroup;
libmultipath/libmultipath.version +4 -1
index 4c22995..0c5417d 100644
@@ -31,14 +31,16 @@
  *   The new version inherits the previous ones.
  */
 
-LIBMULTIPATH_12.0.0 {
+LIBMULTIPATH_13.0.0 {
 global:
 	/* symbols referenced by multipath and multipathd */
 	add_foreign;
 	add_map_with_path;
 	adopt_paths;
 	alloc_multipath;
+	alloc_multipath_layout;
 	alloc_path;
+	alloc_path_layout;
 	alloc_path_with_pathinfo;
 	alloc_strvec;
 	change_foreign;
@@ -270,6 +272,7 @@ global:
 
 	/* added in 7.0.0 */
 	cleanup_charp;
+	cleanup_ucharp;
 
 	/* added in 8.1.0 */
 	reset_strbuf;
libmultipath/print.c +79 -48

304 lines not on this page · raw message

libmultipath/print.h +22 -15
index 4322f6e..52f5b25 100644
@@ -15,23 +15,30 @@ enum layout_reset {
 	LAYOUT_RESET_HEADER,
 };
 
-void _get_path_layout (const struct _vector *gpvec, enum layout_reset);
-void get_path_layout (vector pathvec, int header);
-void _get_multipath_layout (const struct _vector *gmvec, enum layout_reset);
-void get_multipath_layout (vector mpvec, int header);
-int snprint_path_header(struct strbuf *, const char *);
-int snprint_multipath_header(struct strbuf *, const char *);
-int _snprint_path (const struct gen_path *, struct strbuf *, const char *, int);
-#define snprint_path(buf, fmt, pp, v) \
-	_snprint_path(dm_path_to_gen(pp), buf, fmt,  v)
+/* fieldwidth_t is defined in generic.h */
+fieldwidth_t *alloc_path_layout(void);
+void _get_path_layout (const struct _vector *gpvec, enum layout_reset,
+		       fieldwidth_t *width);
+void get_path_layout (vector pathvec, int header, fieldwidth_t *width);
+fieldwidth_t *alloc_multipath_layout(void);
+void _get_multipath_layout (const struct _vector *gmvec, enum layout_reset,
+			    fieldwidth_t *width);
+void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width);
+int snprint_path_header(struct strbuf *, const char *, const fieldwidth_t *);
+int snprint_multipath_header(struct strbuf *, const char *,
+			     const fieldwidth_t *);
+int _snprint_path (const struct gen_path *, struct strbuf *, const char *,
+		   const fieldwidth_t *);
+#define snprint_path(buf, fmt, pp, w)		\
+	_snprint_path(dm_path_to_gen(pp), buf, fmt, w)
 int _snprint_multipath (const struct gen_multipath *, struct strbuf *,
-			const char *, int);
-#define snprint_multipath(buf, fmt, mp, v)				\
-	_snprint_multipath(dm_multipath_to_gen(mp), buf, fmt,  v)
+			const char *, const fieldwidth_t *);
+#define snprint_multipath(buf, fmt, mp, w)				\
+	_snprint_multipath(dm_multipath_to_gen(mp), buf, fmt, w)
 int _snprint_multipath_topology (const struct gen_multipath *, struct strbuf *,
-				 int verbosity);
-#define snprint_multipath_topology(buf, mpp, v) \
-	_snprint_multipath_topology (dm_multipath_to_gen(mpp), buf, v)
+				 int verbosity, const fieldwidth_t *);
+#define snprint_multipath_topology(buf, mpp, v, w)			\
+	_snprint_multipath_topology (dm_multipath_to_gen(mpp), buf, v, w)
 int snprint_multipath_topology_json(struct strbuf *, const struct vectors *vecs);
 int __snprint_config(const struct config *conf, struct strbuf *buff,
 		     const struct _vector *hwtable, const struct _vector *mpvec);
multipath/main.c +5 -2
index 5711acf..0a9377e 100644
@@ -460,6 +460,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	int di_flag = 0;
 	char * refwwid = NULL;
 	char * dev = NULL;
+	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
 
 	/*
 	 * allocate core vectors to store paths and multipaths
@@ -546,8 +547,10 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	if (libmp_verbosity > 2)
 		print_all_paths(pathvec, 1);
 
-	get_path_layout(pathvec, 0);
-	foreign_path_layout();
+	if ((width = alloc_path_layout()) == NULL)
+		goto out;
+	get_path_layout(pathvec, 0, width);
+	foreign_path_layout(width);
 
 	if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
 		goto out;
multipathd/cli_handlers.c +46 -24

164 lines not on this page · raw message

Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#11 lore raw

PATCH v2 11/13 libmultipath: change wildcard handler tables to static const

In reply to [email protected]

From: Martin Wilck <[email protected]>

These arrays are actually constant lookup tables. Now that we use local variables for field width, we can remove the width field from these tables and make them static const, as they should be.

---
 libmultipath/print.c | 119 +++++++++++++++++++++----------------------
 1 file changed, 58 insertions(+), 61 deletions(-)
libmultipath/print.c +58 -61
index 033d663..221b515 100644
@@ -103,21 +103,18 @@
 struct path_data {
 	char wildcard;
 	char * header;
-	unsigned int width;
 	int (*snprint)(struct strbuf *, const struct path * pp);
 };
 
 struct multipath_data {
 	char wildcard;
 	char * header;
-	unsigned int width;
 	int (*snprint)(struct strbuf *, const struct multipath * mpp);
 };
 
 struct pathgroup_data {
 	char wildcard;
 	char * header;
-	unsigned int width;
 	int (*snprint)(struct strbuf *, const struct pathgroup * pgp);
 };
 
@@ -792,66 +789,66 @@ snprint_path_vpd_data(struct strbuf *buff, const struct path * pp)
 	return append_strbuf_str(buff, "[undef]");
 }
 
-static struct multipath_data mpd[] = {
-	{'n', "name",          0, snprint_name},
-	{'w', "uuid",          0, snprint_multipath_uuid},
-	{'d', "sysfs",         0, snprint_sysfs},
-	{'F', "failback",      0, snprint_failback},
-	{'Q', "queueing",      0, snprint_queueing},
-	{'N', "paths",         0, snprint_nb_paths},
-	{'r', "write_prot",    0, snprint_ro},
-	{'t', "dm-st",         0, snprint_dm_map_state},
-	{'S', "size",          0, snprint_multipath_size},
-	{'f', "features",      0, snprint_features},
-	{'x', "failures",      0, snprint_map_failures},
-	{'h', "hwhandler",     0, snprint_hwhandler},
-	{'A', "action",        0, snprint_action},
-	{'0', "path_faults",   0, snprint_path_faults},
-	{'1', "switch_grp",    0, snprint_switch_grp},
-	{'2', "map_loads",     0, snprint_map_loads},
-	{'3', "total_q_time",  0, snprint_total_q_time},
-	{'4', "q_timeouts",    0, snprint_q_timeouts},
-	{'s', "vend/prod/rev", 0, snprint_multipath_vpr},
-	{'v', "vend",          0, snprint_multipath_vend},
-	{'p', "prod",          0, snprint_multipath_prod},
-	{'e', "rev",           0, snprint_multipath_rev},
-	{'G', "foreign",       0, snprint_multipath_foreign},
-	{'g', "vpd page data", 0, snprint_multipath_vpd_data},
+static const struct multipath_data mpd[] = {
+	{'n', "name",          snprint_name},
+	{'w', "uuid",          snprint_multipath_uuid},
+	{'d', "sysfs",         snprint_sysfs},
+	{'F', "failback",      snprint_failback},
+	{'Q', "queueing",      snprint_queueing},
+	{'N', "paths",         snprint_nb_paths},
+	{'r', "write_prot",    snprint_ro},
+	{'t', "dm-st",         snprint_dm_map_state},
+	{'S', "size",          snprint_multipath_size},
+	{'f', "features",      snprint_features},
+	{'x', "failures",      snprint_map_failures},
+	{'h', "hwhandler",     snprint_hwhandler},
+	{'A', "action",        snprint_action},
+	{'0', "path_faults",   snprint_path_faults},
+	{'1', "switch_grp",    snprint_switch_grp},
+	{'2', "map_loads",     snprint_map_loads},
+	{'3', "total_q_time",  snprint_total_q_time},
+	{'4', "q_timeouts",    snprint_q_timeouts},
+	{'s', "vend/prod/rev", snprint_multipath_vpr},
+	{'v', "vend",          snprint_multipath_vend},
+	{'p', "prod",          snprint_multipath_prod},
+	{'e', "rev",           snprint_multipath_rev},
+	{'G', "foreign",       snprint_multipath_foreign},
+	{'g', "vpd page data", snprint_multipath_vpd_data},
 };
 
-static struct path_data pd[] = {
-	{'w', "uuid",          0, snprint_path_uuid},
-	{'i', "hcil",          0, snprint_hcil},
-	{'d', "dev",           0, snprint_dev},
-	{'D', "dev_t",         0, snprint_dev_t},
-	{'t', "dm_st",         0, snprint_dm_path_state},
-	{'o', "dev_st",        0, snprint_offline},
-	{'T', "chk_st",        0, snprint_chk_state},
-	{'s', "vend/prod/rev", 0, snprint_vpr},
-	{'c', "checker",       0, snprint_path_checker},
-	{'C', "next_check",    0, snprint_next_check},
-	{'p', "pri",           0, snprint_pri},
-	{'S', "size",          0, snprint_path_size},
-	{'z', "serial",        0, snprint_path_serial},
-	{'M', "marginal_st",   0, snprint_path_marginal},
-	{'m', "multipath",     0, snprint_path_mpp},
-	{'N', "host WWNN",     0, snprint_host_wwnn},
-	{'n', "target WWNN",   0, snprint_tgt_wwnn},
-	{'R', "host WWPN",     0, snprint_host_wwpn},
-	{'r', "target WWPN",   0, snprint_tgt_wwpn},
-	{'a', "host adapter",  0, snprint_host_adapter},
-	{'G', "foreign",       0, snprint_path_foreign},
-	{'g', "vpd page data", 0, snprint_path_vpd_data},
-	{'0', "failures",      0, snprint_path_failures},
-	{'P', "protocol",      0, snprint_path_protocol},
-	{'I', "init_st",       0, snprint_initialized},
+static const struct path_data pd[] = {
+	{'w', "uuid",          snprint_path_uuid},
+	{'i', "hcil",          snprint_hcil},
+	{'d', "dev",           snprint_dev},
+	{'D', "dev_t",         snprint_dev_t},
+	{'t', "dm_st",         snprint_dm_path_state},
+	{'o', "dev_st",        snprint_offline},
+	{'T', "chk_st",        snprint_chk_state},
+	{'s', "vend/prod/rev", snprint_vpr},
+	{'c', "checker",       snprint_path_checker},
+	{'C', "next_check",    snprint_next_check},
+	{'p', "pri",           snprint_pri},
+	{'S', "size",          snprint_path_size},
+	{'z', "serial",        snprint_path_serial},
+	{'M', "marginal_st",   snprint_path_marginal},
+	{'m', "multipath",     snprint_path_mpp},
+	{'N', "host WWNN",     snprint_host_wwnn},
+	{'n', "target WWNN",   snprint_tgt_wwnn},
+	{'R', "host WWPN",     snprint_host_wwpn},
+	{'r', "target WWPN",   snprint_tgt_wwpn},
+	{'a', "host adapter",  snprint_host_adapter},
+	{'G', "foreign",       snprint_path_foreign},
+	{'g', "vpd page data", snprint_path_vpd_data},
+	{'0', "failures",      snprint_path_failures},
+	{'P', "protocol",      snprint_path_protocol},
+	{'I', "init_st",       snprint_initialized},
 };
 
-static struct pathgroup_data pgd[] = {
-	{'s', "selector",      0, snprint_pg_selector},
-	{'p', "pri",           0, snprint_pg_pri},
-	{'t', "dm_st",         0, snprint_pg_state},
-	{'M', "marginal_st",   0, snprint_pg_marginal},
+static const struct pathgroup_data pgd[] = {
+	{'s', "selector",      snprint_pg_selector},
+	{'p', "pri",           snprint_pg_pri},
+	{'t', "dm_st",         snprint_pg_state},
+	{'M', "marginal_st",   snprint_pg_marginal},
 };
 
 int snprint_wildcards(struct strbuf *buff)
@@ -1052,7 +1049,7 @@ int snprint_multipath_header(struct strbuf *line, const char *format,
 {
 	int initial_len = get_strbuf_len(line);
 	const char *f;
-	struct multipath_data * data;
+	const struct multipath_data * data;
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
@@ -1113,7 +1110,7 @@ int snprint_path_header(struct strbuf *line, const char *format,
 {
 	int initial_len = get_strbuf_len(line);
 	const char *f;
-	struct path_data *data;
+	const struct path_data *data;
 	int rc;
 
 	for (f = strchr(format, '%'); f; f = strchr(++format, '%')) {
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#12 lore raw

PATCH v2 12/13 libmultipath: fix compilation error with clang 13

In reply to [email protected]

From: Martin Wilck <[email protected]>

This fixes the following error emitted by clang 13:

weightedpath.c:67:8: error: variable 'arg' set but not used [-Werror,-Wunused-but-set-variable]

	char *arg __attribute__((cleanup(cleanup_charp))) = NULL;
---
 libmultipath/prioritizers/weightedpath.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libmultipath/prioritizers/weightedpath.c +2 -1
index 6f20e8f..561ebb4 100644
@@ -74,7 +74,8 @@ int prio_path_weight(struct path *pp, char *prio_args)
 	if (!prio_args)
 		return priority;
 
-	arg = temp = strdup(prio_args);
+	arg = strdup(prio_args);
+	temp = arg;
 
 	regex = get_next_string(&temp, split_char);
 
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#13 lore raw

PATCH v2 13/13 libmultipath: remove_map(): make sure orphaned paths aren't referenced

In reply to [email protected]

From: Martin Wilck <[email protected]>

... by the paths and pg vectors of the map to be removed.

Original bug report from Lixiaokeng ("libmultipath: clear removed path from mpp"):

multipathd[3525635]: ==3525635==ERROR: AddressSanitizer: heap-use-after-free on address 0xffffa4902fc0 at pc 0xffffac7d5b88 bp 0xffffa948dac0 sp 0xffffa948dae0 multipathd[3525635]: READ of size 8 at 0xffffa4902fc0 thread T7

multipathd[3525635]:    #0 0xffffac7d5b87 in free_multipath (/usr/lib64/libmultipath.so.0+0x4bb87)
multipathd[3525635]:    #1 0xaaaad6cf7057  (/usr/sbin/multipathd+0x17057)
multipathd[3525635]:    #2 0xaaaad6cf78eb  (/usr/sbin/multipathd+0x178eb)
multipathd[3525635]:    #3 0xaaaad6cff4df  (/usr/sbin/multipathd+0x1f4df)
multipathd[3525635]:    #4 0xaaaad6cfffe7  (/usr/sbin/multipathd+0x1ffe7)
multipathd[3525635]:    #5 0xffffac807be3 in uevent_dispatch (/usr/lib64/libmultipath.so.0+0x7dbe3)
multipathd[3525635]:    #6 0xaaaad6cf563f  (/usr/sbin/multipathd+0x1563f)
multipathd[3525635]:    #7 0xffffac6877af  (/usr/lib64/libpthread.so.0+0x87af)
multipathd[3525635]:    #8 0xffffac44118b  (/usr/lib64/libc.so.6+0xd518b)
multipathd[3525635]: 0xffffa4902fc0 is located 1344 bytes inside of 1440-byte region [0xffffa4902a80,0xffffa4903020)
multipathd[3525635]: freed by thread T7 here:
multipathd[3525635]:    #0 0xffffac97d703 in free (/usr/lib64/libasan.so.4+0xd0703)
multipathd[3525635]:    #1 0xffffac824827 in orphan_paths (/usr/lib64/libmultipath.so.0+0x9a827)
multipathd[3525635]:    #2 0xffffac824a43 in remove_map (/usr/lib64/libmultipath.so.0+0x9aa43)
multipathd[3525635]:    #3 0xaaaad6cf7057  (/usr/sbin/multipathd+0x17057)
multipathd[3525635]:    #4 0xaaaad6cf78eb  (/usr/sbin/multipathd+0x178eb)
multipathd[3525635]:    #5 0xaaaad6cff4df  (/usr/sbin/multipathd+0x1f4df)
multipathd[3525635]:    #6 0xaaaad6cfffe7  (/usr/sbin/multipathd+0x1ffe7)
multipathd[3525635]:    #7 0xffffac807be3 in uevent_dispatch (/usr/lib64/libmultipath.so.0+0x7dbe3)
multipathd[3525635]:    #8 0xaaaad6cf563f  (/usr/sbin/multipathd+0x1563f)
multipathd[3525635]:    #9 0xffffac6877af  (/usr/lib64/libpthread.so.0+0x87af)
multipathd[3525635]:    #10 0xffffac44118b  (/usr/lib64/libc.so.6+0xd518b)

When mpp only has one path and log out the path, there is an asan error. In remove_mpp, the pp is freed firstly in orphan_path but is accessed, changed in free_multipath later. Before free_path(pp), the pp should be cleared from pp->mpp.

---
 libmultipath/structs_vec.c | 4 ++++
 1 file changed, 4 insertions(+)
libmultipath/structs_vec.c +4 -0
index bfec840..61f8e1b 100644
@@ -341,6 +341,10 @@ remove_map(struct multipath *mpp, vector pathvec, vector mpvec)
 {
 	int i;
 
+	free_pathvec(mpp->paths, KEEP_PATHS);
+	free_pgvec(mpp->pg, KEEP_PATHS);
+	mpp->paths = mpp->pg = NULL;
+
 	/*
 	 * clear references to this map
 	 */
Signature · 7 lines
2.33.1


--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
maintainer Reviewed-by on the v2 series
#14 lore raw
In reply to [email protected]
Quoted 16 lines · On Mon, Nov 29, 2021 at 09:08:49PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

this series attempts to improve the robustness of the code by making the lookup tables used for pretty printing and wildcard handling static const arrays. This requires getting rid of the variable "width" field in these arrays. I could have simply split of "width" into a separate global array variable, but as the field width are designed to change depending on the data being printed, it makes more sense to use local variables for it and pass them to the actual table-formatting code.

A couple of minor cleanups are done along the way.

Changes in v2:

Quoted 9 lines

- 10/13: fix a compilation error with clang because of duplicate typedef for fieldwidth_t
- whitespace fixes
- 12/13: new, fix a compile error with clang-13
- 13/13: new, fix an asan error reported by Lixiaokeng

As always, feedback welcome
Martin

For the set:

Quoted 15 lines

Martin Wilck (13):
libmultipath: make multipath_data etc. static
libmultipath: move path_data etc. to print.c
libmultipath: make pd_lookup() etc. return an index
libmultipath: use ARRAY_SIZE for iterating over wildcard arrays
libmultipath: drop padding code in _snprint_pathgroup()
libmultipath: snprint_foreign_topology(): split out lockless variant
multipathd: drop unnecessary path layout calls
libmultipath: add a cleanup function for unsigned char *
libmultipath: make sprint_path_marginal() static
libmultipath: introduce width argument for pretty-printing functions
libmultipath: change wildcard handler tables to static const
libmultipath: fix compilation error with clang 13
libmultipath: remove_map(): make sure orphaned paths aren't referenced

Quoted 16 lines
 libmultipath/foreign.c                   |  74 +++--
 libmultipath/foreign.h                   |  23 +-
 libmultipath/generic.h                   |   8 +
 libmultipath/libmultipath.version        |   5 +-
 libmultipath/print.c                     | 356 +++++++++++++----------
 libmultipath/print.h                     |  58 ++--
 libmultipath/prioritizers/weightedpath.c |   3 +-
 libmultipath/structs_vec.c               |   4 +
 libmultipath/util.c                      |   5 +
 libmultipath/util.h                      |   1 +
 multipath/main.c                         |   7 +-
 multipathd/cli_handlers.c                |  75 +++--
 12 files changed, 367 insertions(+), 252 deletions(-)
Signature · 1 line
2.33.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel