From: mwilck@suse.com
To: Christophe Varoqui <christophe.varoqui@opensvc.com>,
Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@redhat.com, Martin Wilck <mwilck@suse.com>
Subject: [dm-devel] [PATCH 13/21] libmultipath: alias.c: move bindings related code up
Date: Fri, 1 Sep 2023 20:02:26 +0200 [thread overview]
Message-ID: <20230901180235.23980-14-mwilck@suse.com> (raw)
In-Reply-To: <20230901180235.23980-1-mwilck@suse.com>
From: Martin Wilck <mwilck@suse.com>
No code changes, just moving code.
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
libmultipath/alias.c | 239 ++++++++++++++++++++++---------------------
1 file changed, 120 insertions(+), 119 deletions(-)
diff --git a/libmultipath/alias.c b/libmultipath/alias.c
index af2f647..5a6cdee 100644
--- a/libmultipath/alias.c
+++ b/libmultipath/alias.c
@@ -9,6 +9,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdbool.h>
+#include <assert.h>
#include "debug.h"
#include "util.h"
@@ -51,6 +52,125 @@
static const char bindings_file_header[] = BINDINGS_FILE_HEADER;
+struct binding {
+ char *alias;
+ char *wwid;
+};
+
+/*
+ * Perhaps one day we'll implement this more efficiently, thus use
+ * an abstract type.
+ */
+typedef struct _vector Bindings;
+static Bindings global_bindings = { .allocated = 0 };
+
+enum {
+ BINDING_EXISTS,
+ BINDING_CONFLICT,
+ BINDING_ADDED,
+ BINDING_DELETED,
+ BINDING_NOTFOUND,
+ BINDING_ERROR,
+};
+
+static void _free_binding(struct binding *bdg)
+{
+ free(bdg->wwid);
+ free(bdg->alias);
+ free(bdg);
+}
+
+static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
+{
+ struct binding *bdg;
+ int i, cmp = 0;
+
+ /*
+ * Keep the bindings array sorted by alias.
+ * Optimization: Search backwards, assuming that the bindings file is
+ * sorted already.
+ */
+ vector_foreach_slot_backwards(bindings, bdg, i) {
+ if ((cmp = strcmp(bdg->alias, alias)) <= 0)
+ break;
+ }
+
+ /* Check for exact match */
+ if (i >= 0 && cmp == 0)
+ return strcmp(bdg->wwid, wwid) ?
+ BINDING_CONFLICT : BINDING_EXISTS;
+
+ i++;
+ bdg = calloc(1, sizeof(*bdg));
+ if (bdg) {
+ bdg->wwid = strdup(wwid);
+ bdg->alias = strdup(alias);
+ if (bdg->wwid && bdg->alias &&
+ vector_insert_slot(bindings, i, bdg))
+ return BINDING_ADDED;
+ else
+ _free_binding(bdg);
+ }
+
+ return BINDING_ERROR;
+}
+
+static int write_bindings_file(const Bindings *bindings, int fd)
+{
+ struct binding *bnd;
+ STRBUF_ON_STACK(line);
+ int i;
+
+ if (write(fd, BINDINGS_FILE_HEADER, sizeof(BINDINGS_FILE_HEADER) - 1)
+ != sizeof(BINDINGS_FILE_HEADER) - 1)
+ return -1;
+
+ vector_foreach_slot(bindings, bnd, i) {
+ int len;
+
+ if ((len = print_strbuf(&line, "%s %s\n",
+ bnd->alias, bnd->wwid)) < 0)
+ return -1;
+ if (write(fd, get_strbuf_str(&line), len) != len)
+ return -1;
+ truncate_strbuf(&line, 0);
+ }
+ return 0;
+}
+
+static int update_bindings_file(const struct config *conf,
+ const Bindings *bindings)
+{
+ int rc;
+ int fd = -1;
+ char tempname[PATH_MAX];
+ mode_t old_umask;
+
+ if (safe_sprintf(tempname, "%s.XXXXXX", conf->bindings_file))
+ return -1;
+ /* coverity: SECURE_TEMP */
+ old_umask = umask(0077);
+ if ((fd = mkstemp(tempname)) == -1) {
+ condlog(1, "%s: mkstemp: %m", __func__);
+ return -1;
+ }
+ umask(old_umask);
+ pthread_cleanup_push(cleanup_fd_ptr, &fd);
+ rc = write_bindings_file(bindings, fd);
+ pthread_cleanup_pop(1);
+ if (rc == -1) {
+ condlog(1, "failed to write new bindings file %s",
+ tempname);
+ unlink(tempname);
+ return rc;
+ }
+ if ((rc = rename(tempname, conf->bindings_file)) == -1)
+ condlog(0, "%s: rename: %m", __func__);
+ else
+ condlog(1, "updated bindings file %s", conf->bindings_file);
+ return rc;
+}
+
int
valid_alias(const char *alias)
{
@@ -505,25 +625,6 @@ get_user_friendly_wwid(const char *alias, char *buff, const char *file)
return 0;
}
-struct binding {
- char *alias;
- char *wwid;
-};
-
-static void _free_binding(struct binding *bdg)
-{
- free(bdg->wwid);
- free(bdg->alias);
- free(bdg);
-}
-
-/*
- * Perhaps one day we'll implement this more efficiently, thus use
- * an abstract type.
- */
-typedef struct _vector Bindings;
-static Bindings global_bindings = { .allocated = 0 };
-
static void free_bindings(Bindings *bindings)
{
struct binding *bdg;
@@ -539,106 +640,6 @@ void cleanup_bindings(void)
free_bindings(&global_bindings);
}
-enum {
- BINDING_EXISTS,
- BINDING_CONFLICT,
- BINDING_ADDED,
- BINDING_DELETED,
- BINDING_NOTFOUND,
- BINDING_ERROR,
-};
-
-static int add_binding(Bindings *bindings, const char *alias, const char *wwid)
-{
- struct binding *bdg;
- int i, cmp = 0;
-
- /*
- * Keep the bindings array sorted by alias.
- * Optimization: Search backwards, assuming that the bindings file is
- * sorted already.
- */
- vector_foreach_slot_backwards(bindings, bdg, i) {
- if ((cmp = strcmp(bdg->alias, alias)) <= 0)
- break;
- }
-
- /* Check for exact match */
- if (i >= 0 && cmp == 0)
- return strcmp(bdg->wwid, wwid) ?
- BINDING_CONFLICT : BINDING_EXISTS;
-
- i++;
- bdg = calloc(1, sizeof(*bdg));
- if (bdg) {
- bdg->wwid = strdup(wwid);
- bdg->alias = strdup(alias);
- if (bdg->wwid && bdg->alias &&
- vector_insert_slot(bindings, i, bdg))
- return BINDING_ADDED;
- else
- _free_binding(bdg);
- }
-
- return BINDING_ERROR;
-}
-
-static int write_bindings_file(const Bindings *bindings, int fd)
-{
- struct binding *bnd;
- STRBUF_ON_STACK(line);
- int i;
-
- if (write(fd, BINDINGS_FILE_HEADER, sizeof(BINDINGS_FILE_HEADER) - 1)
- != sizeof(BINDINGS_FILE_HEADER) - 1)
- return -1;
-
- vector_foreach_slot(bindings, bnd, i) {
- int len;
-
- if ((len = print_strbuf(&line, "%s %s\n",
- bnd->alias, bnd->wwid)) < 0)
- return -1;
- if (write(fd, get_strbuf_str(&line), len) != len)
- return -1;
- truncate_strbuf(&line, 0);
- }
- return 0;
-}
-
-static int update_bindings_file(const struct config *conf,
- const Bindings *bindings)
-{
- int rc;
- int fd = -1;
- char tempname[PATH_MAX];
- mode_t old_umask;
-
- if (safe_sprintf(tempname, "%s.XXXXXX", conf->bindings_file))
- return -1;
- /* coverity: SECURE_TEMP */
- old_umask = umask(0077);
- if ((fd = mkstemp(tempname)) == -1) {
- condlog(1, "%s: mkstemp: %m", __func__);
- return -1;
- }
- umask(old_umask);
- pthread_cleanup_push(cleanup_fd_ptr, &fd);
- rc = write_bindings_file(bindings, fd);
- pthread_cleanup_pop(1);
- if (rc == -1) {
- condlog(1, "failed to write new bindings file %s",
- tempname);
- unlink(tempname);
- return rc;
- }
- if ((rc = rename(tempname, conf->bindings_file)) == -1)
- condlog(0, "%s: rename: %m", __func__);
- else
- condlog(1, "updated bindings file %s", conf->bindings_file);
- return rc;
-}
-
static int _check_bindings_file(const struct config *conf, FILE *file,
Bindings *bindings)
{
--
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-01 18:03 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
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 ` mwilck [this message]
2023-09-06 22:44 ` [dm-devel] [PATCH 13/21] libmultipath: alias.c: move bindings related code up 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=20230901180235.23980-14-mwilck@suse.com \
--to=mwilck@suse.com \
--cc=bmarzins@redhat.com \
--cc=christophe.varoqui@opensvc.com \
--cc=dm-devel@redhat.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