From: Benjamin Marzinski <bmarzins@redhat.com>
To: mwilck@suse.com
Cc: dm-devel@redhat.com
Subject: Re: [dm-devel] [PATCH 05/21] libmultipath: lookup_binding: add comment about the algorithm
Date: Wed, 6 Sep 2023 17:43:06 -0500 [thread overview]
Message-ID: <20230906224306.GP7412@octiron.msp.redhat.com> (raw)
In-Reply-To: <20230901180235.23980-6-mwilck@suse.com>
On Fri, Sep 01, 2023 at 08:02:18PM +0200, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
>
> When I read this code, I always get confused. Adding comments to
> explain the algorithm.
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
> libmultipath/alias.c | 35 +++++++++++++++++++++++++++++++++++
> 1 file changed, 35 insertions(+)
>
> diff --git a/libmultipath/alias.c b/libmultipath/alias.c
> index f7834d1..e61eb91 100644
> --- a/libmultipath/alias.c
> +++ b/libmultipath/alias.c
> @@ -172,6 +172,41 @@ lookup_binding(FILE *f, const char *map_wwid, char **map_alias,
> alias = strtok_r(buf, " \t", &saveptr);
> if (!alias) /* blank line */
> continue;
> +
> + /*
> + * Find an unused index - explanation of the algorithm
> + *
> + * ID: 1 = mpatha, 2 = mpathb, ...
> + *
> + * We assume the bindings are unsorted. The only constraint
> + * is that no ID occurs more than once. IDs that occur in the
> + * bindings are called "used".
> + *
> + * We call the list 1,2,3,..., exactly in this order, the list
> + * of "expected" IDs. The variable "id" always holds the next
> + * "expected" ID, IOW the last "expected" ID encountered plus 1.
> + * Thus all IDs below "id" are known to be used. However, at the
> + * end of the loop, the value of "id" isn't necessarily unused.
> + *
> + * "smallest_bigger_id" is the smallest used ID that was
> + * encountered while it was larger than the next "expected" ID
> + * at that iteration. Let X be some used ID. If all IDs below X
> + * are used and encountered in the right sequence before X, "id"
> + * will be > X when the loop ends. Otherwise, X was encountered
> + * "out of order", the condition (X > id) holds when X is
> + * encountered, and "smallest_bigger_id" will be set to X; i.e.
> + * it will be less or equal than X when the loop ends.
> + *
> + * At the end of the loop, (id < smallest_bigger_id) means that
> + * the value of "id" had been encountered neither in order nor
> + * out of order, and is thus unused. (id >= smallest_bigger_id)
I know the check is (id >= smallest_bigger_id), but as long as no ID
occurs more than once, id can never actually be bigger than
smallest_bigger_id since id only gets incremented when (curr_id == id)
and if smallest_bigger_id is not INT_MAX, then smallest_bigger_id
already occured once in the file before id was incremented to equal it.
This means it can't occur again, so id can never get incremented past
it. Not this this really matters, so
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> + * means that "id"'s value is in use. In this case, we play safe
> + * and use "biggest_id + 1" as the next value to try.
> + *
> + * biggest_id is always > smallest_bigger_id, except in the
> + * "perfectly ordered" case.
> + */
> +
> curr_id = scan_devname(alias, prefix);
> if (curr_id == id) {
> if (id < INT_MAX)
> --
> 2.41.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-06 22:43 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-01 18:02 [dm-devel] [PATCH 00/21] multipath-tools: user-friendly names rework mwilck
2023-09-01 18:02 ` [dm-devel] [PATCH 01/21] libmultipath: sysfs_set_scsi_tmo: do nothing for ACT_DRY_RUN mwilck
2023-09-06 22:42 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 02/21] libmultipath: add alias_already_taken() mwilck
2023-09-06 22:42 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 03/21] libmultipath: unify use_existing_alias() and get_user_friendly_alias() mwilck
2023-09-06 22:42 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 04/21] libmultipath: never allocate an alias that's already taken mwilck
2023-09-06 22:42 ` Benjamin Marzinski
2023-09-07 7:24 ` Martin Wilck
2023-09-07 13:33 ` Martin Wilck
2023-09-07 14:22 ` Martin Wilck
2023-09-01 18:02 ` [dm-devel] [PATCH 05/21] libmultipath: lookup_binding: add comment about the algorithm mwilck
2023-09-06 22:43 ` Benjamin Marzinski [this message]
2023-09-07 8:22 ` Martin Wilck
2023-09-01 18:02 ` [dm-devel] [PATCH 06/21] multipath-tools test: simplify debugging for condlog mismatch mwilck
2023-09-06 22:43 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 07/21] multipath-tools tests: add tests for get_user_friendly_alias() mwilck
2023-09-06 22:43 ` Benjamin Marzinski
2023-09-07 7:55 ` Martin Wilck
2023-09-01 18:02 ` [dm-devel] [PATCH 08/21] multipath-tools test: consistent use of macros in alias test mwilck
2023-09-06 22:43 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 09/21] multipath-tools tests: convert mock_{failed, used}_alias to macros mwilck
2023-09-06 22:44 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 10/21] multipath-tools test: use mock_bindings_file() consistently mwilck
2023-09-06 22:43 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 11/21] libmultipath: add global variable for current bindings mwilck
2023-09-06 22:44 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 12/21] libmultipath: rename fix_bindings_file() to update_bindings_file() mwilck
2023-09-06 22:44 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 13/21] libmultipath: alias.c: move bindings related code up mwilck
2023-09-06 22:44 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 14/21] libmultipath: update_bindings_file: take filename argument mwilck
2023-09-06 22:45 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 15/21] libmultipath: update_bindings_file: use a single write() mwilck
2023-09-06 22:45 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 16/21] libmultipath: update_bindings_file: don't log temp file name mwilck
2023-09-06 22:45 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 17/21] libmultipath: alias.c: factor out read_binding() mwilck
2023-09-06 22:45 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 18/21] libmultipath: keep bindings in memory mwilck
2023-09-06 22:47 ` Benjamin Marzinski
2023-09-07 10:30 ` Martin Wilck
2023-09-07 19:14 ` Benjamin Marzinski
2023-09-07 20:02 ` Benjamin Marzinski
2023-09-07 20:43 ` Martin Wilck
2023-09-08 17:22 ` Benjamin Marzinski
2023-09-11 6:25 ` Martin Wilck
2023-09-11 14:47 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 19/21] multipath-tools tests: fix alias tests mwilck
2023-09-06 22:47 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 20/21] libmultipath: dm_get_uuid(): return emtpy UUID for non-existing maps mwilck
2023-09-06 22:47 ` Benjamin Marzinski
2023-09-01 18:02 ` [dm-devel] [PATCH 21/21] libmultipath: adapt to new semantics of dm_get_uuid() mwilck
2023-09-06 22:47 ` 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=20230906224306.GP7412@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