From: Benjamin Marzinski <bmarzins@redhat.com>
To: mwilck@suse.com
Cc: dm-devel@redhat.com
Subject: Re: [dm-devel] [PATCH v2 22/37] libmultipath: sort aliases by length and strcmp
Date: Tue, 12 Sep 2023 18:00:48 -0500 [thread overview]
Message-ID: <20230912230048.GR7412@octiron.msp.redhat.com> (raw)
In-Reply-To: <20230911163846.27197-23-mwilck@suse.com>
On Mon, Sep 11, 2023 at 06:38:31PM +0200, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> The current sort order of aliases is alphabetical, which is does not match
> the actual order of aliases, where "mpathaa" > "mpathz". Change the ordering as
> follows: first sort by string length, then alphabetically. This will make
> sure that for aliases with the same prefix, alias order is correct ("mpathaaa"
> will be sorted after "mpathzz", etc). Even for mixed prefixes, the alias
> order will be correct for every individual prefix, even though aliases with
> different prefixes may alternate in the file.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
> libmultipath/alias.c | 45 +++++++++++++++++++++++++++++++++-----------
> 1 file changed, 34 insertions(+), 11 deletions(-)
>
> diff --git a/libmultipath/alias.c b/libmultipath/alias.c
> index 58436ec..af6565b 100644
> --- a/libmultipath/alias.c
> +++ b/libmultipath/alias.c
> @@ -117,6 +117,35 @@ static const struct binding *get_binding_for_wwid(const Bindings *bindings,
> return NULL;
> }
>
> +/*
> + * Sort order for aliases.
> + *
> + * The "numeric" ordering of aliases for a given prefix P is
> + * Pa, ..., Pz, Paa, ..., Paz, Pba, ... , Pzz, Paaa, ..., Pzzz, Paaaa, ...
> + * We use the fact that for equal prefix, longer strings are always
> + * higher than shorter ones. Strings of equal length are sorted alphabetically.
> + * This is achieved by sorting be length first, then using strcmp().
> + * If multiple prefixes are in use, the aliases with a given prefix will
> + * not necessarily be in a contiguous range of the vector, but they will
> + * be ordered such that for a given prefix, numercally higher aliases will
> + * always be sorted after lower ones.
> + */
> +static int alias_compar(const void *p1, const void *p2)
> +{
I'm confused as to why we need to pass p1 and p2 and pointers to
pointers to chars, instead of simply as pointers to chars. We always
derefence them immediately, and only use the dereferenced pointers. Am I
missing something?
-Ben
> + const char *alias1 = *((char * const *)p1);
> + const char *alias2 = *((char * const *)p2);
> +
> + if (alias1 && alias2) {
> + ssize_t ldif = strlen(alias1) - strlen(alias2);
> +
> + if (ldif)
> + return ldif;
> + return strcmp(alias1, alias2);
> + } else
> + /* Move NULL alias to the end */
> + return alias1 ? -1 : alias2 ? 1 : 0;
> +}
> +
> static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
> {
> struct binding *bdg;
> @@ -128,7 +157,7 @@ static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
> * sorted already.
> */
> vector_foreach_slot_backwards(bindings, bdg, i) {
> - if ((cmp = strcmp(bdg->alias, alias)) <= 0)
> + if ((cmp = alias_compar(&bdg->alias, &alias)) <= 0)
> break;
> }
>
> @@ -657,16 +686,10 @@ static int _check_bindings_file(const struct config *conf, FILE *file,
> return rc;
> }
>
> -static int alias_compar(const void *p1, const void *p2)
> +static int mp_alias_compar(const void *p1, const void *p2)
> {
> - const char *alias1 = (*(struct mpentry * const *)p1)->alias;
> - const char *alias2 = (*(struct mpentry * const *)p2)->alias;
> -
> - if (alias1 && alias2)
> - return strcmp(alias1, alias2);
> - else
> - /* Move NULL alias to the end */
> - return alias1 ? -1 : alias2 ? 1 : 0;
> + return alias_compar(&((*(struct mpentry * const *)p1)->alias),
> + &((*(struct mpentry * const *)p2)->alias));
> }
>
> /*
> @@ -700,7 +723,7 @@ int check_alias_settings(const struct config *conf)
> pthread_cleanup_push_cast(free_bindings, &bindings);
> pthread_cleanup_push(cleanup_vector_free, mptable);
>
> - vector_sort(mptable, alias_compar);
> + vector_sort(mptable, mp_alias_compar);
> vector_foreach_slot(mptable, mpe, i) {
> if (!mpe->alias)
> /*
> --
> 2.42.0
--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel
next prev parent reply other threads:[~2023-09-12 23:01 UTC|newest]
Thread overview: 68+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-11 16:38 [dm-devel] [PATCH v2 00/37] multipath-tools: user-friendly names rework mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 01/37] libmultipath: sysfs_set_scsi_tmo: do nothing for ACT_DRY_RUN mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 02/37] libmultipath: add alias_already_taken() mwilck
2023-09-12 22:59 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 03/37] libmultipath: unify use_existing_alias() and get_user_friendly_alias() mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 04/37] libmultipath: never allocate an alias that's already taken mwilck
2023-09-12 23:00 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 05/37] libmultipath: lookup_binding: add comment about the algorithm mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 06/37] multipath-tools test: simplify debugging for condlog mismatch mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 07/37] multipath-tools tests: add tests for get_user_friendly_alias() mwilck
2023-09-12 23:00 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 08/37] multipath-tools test: consistent use of macros in alias test mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 09/37] multipath-tools tests: convert mock_{failed, used}_alias to macros mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 10/37] multipath-tools test: use mock_bindings_file() consistently mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 11/37] libmultipath: add global variable for current bindings mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 12/37] libmultipath: rename fix_bindings_file() to update_bindings_file() mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 13/37] libmultipath: alias.c: move bindings related code up mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 14/37] libmultipath: update_bindings_file: take filename argument mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 15/37] libmultipath: update_bindings_file: use a single write() mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 16/37] libmultipath: update_bindings_file: don't log temp file name mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 17/37] libmultipath: alias.c: factor out read_binding() mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 18/37] libmultipath: keep bindings in memory mwilck
2023-09-12 23:00 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 19/37] multipath-tools tests: fix alias tests mwilck
2023-09-12 22:02 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 20/37] libmultipath: dm_get_uuid(): return emtpy UUID for non-existing maps mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 21/37] libmultipath: adapt to new semantics of dm_get_uuid() mwilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 22/37] libmultipath: sort aliases by length and strcmp mwilck
2023-09-12 23:00 ` Benjamin Marzinski [this message]
2023-09-13 13:53 ` Martin Wilck
2023-09-13 14:38 ` Benjamin Marzinski
2023-09-13 19:07 ` Martin Wilck
2023-09-13 23:15 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 23/37] multipath-tools tests: fix alias test after sort order change mwilck
2023-09-12 23:01 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 24/37] libmultipath: simplify get_free_id() assuming total ordering mwilck
2023-09-12 22:59 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 25/37] multipath-tools tests: adapt alias tests for " mwilck
2023-09-12 23:01 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 26/37] multipath-tools tests: add test for ordering of bindings mwilck
2023-09-12 23:05 ` Benjamin Marzinski
2023-09-12 23:20 ` Benjamin Marzinski
2023-09-13 14:05 ` Martin Wilck
2023-09-11 16:38 ` [dm-devel] [PATCH v2 27/37] multipathd: watch bindings file with inotify + timestamp mwilck
2023-09-13 22:07 ` Benjamin Marzinski
2023-09-14 13:25 ` Martin Wilck
2023-09-14 14:28 ` Martin Wilck
2023-09-14 15:00 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 28/37] multipath-tools tests: mock pthread_mutex_{lock, unlock} mwilck
2023-09-13 22:18 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 29/37] multipath-tools Makefile: sanitize paths for configuration files mwilck
2023-09-13 22:30 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 30/37] multipath-tools: add compile time configuration for "/etc/multipath" mwilck
2023-09-13 22:32 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 31/37] multipath-tools man pages: generate with correct paths mwilck
2023-09-13 22:44 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 32/37] libdmmp/Makefile: fix bug in install section mwilck
2023-09-13 22:46 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 33/37] multipath-tools: README.md: improve documentation for compile-time options mwilck
2023-09-13 22:58 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 34/37] libmultipath: print built-in values for deprecated options mwilck
2023-09-13 23:05 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 35/37] multipath: add a missing newline mwilck
2023-09-13 23:05 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 36/37] multipath-tools: allow prefixes with and w/o trailing slash mwilck
2023-09-13 23:09 ` Benjamin Marzinski
2023-09-11 16:38 ` [dm-devel] [PATCH v2 37/37] libmultipath: deprecate bindings_file, wwids_file, prkeys_file mwilck
2023-09-13 23:13 ` Benjamin Marzinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230912230048.GR7412@octiron.msp.redhat.com \
--to=bmarzins@redhat.com \
--cc=dm-devel@redhat.com \
--cc=mwilck@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox