From b77a6d86936c32bb5f36e9806323ba00a18b0f4b Mon Sep 17 00:00:00 2001 From: Olaf Faaland Date: Mon, 3 May 2021 19:40:22 -0700 Subject: [PATCH] LU-14665 lnet: simplify lnet_ni_add_interface Remove an unnecessary counter and move the comment before the relevant code. Improve error messages. Test-parameters: trivial Signed-off-by: Olaf Faaland Change-Id: Iffc7a128b16bc1b2be7a44413a5972c97b12a5fa Reviewed-on: https://review.whamcloud.com/43525 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Serguei Smirnov Reviewed-by: Neil Brown Reviewed-by: Oleg Drokin --- lnet/lnet/config.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/lnet/lnet/config.c b/lnet/lnet/config.c index 1c9bd4f..f6ade39 100644 --- a/lnet/lnet/config.c +++ b/lnet/lnet/config.c @@ -376,37 +376,32 @@ lnet_net_alloc(__u32 net_id, struct list_head *net_list) static int lnet_ni_add_interface(struct lnet_ni *ni, char *iface) { - int niface = 0; + size_t iface_len = strlen(iface) + 1; if (ni == NULL) return -ENOMEM; - /* Allocate a separate piece of memory and copy - * into it the string, so we don't have - * a depencency on the tokens string. This way we - * can free the tokens at the end of the function. - * The newly allocated ni_interface can be - * freed when freeing the NI */ - if (ni->ni_interface != NULL) - niface++; - - if (niface >= 1) { - LCONSOLE_ERROR_MSG(0x115, "Too many interfaces " - "for net %s\n", - libcfs_net2str(LNET_NIDNET(ni->ni_nid))); + if (ni->ni_interface != NULL) { + LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n", + iface, ni->ni_interface, + libcfs_net2str(LNET_NIDNET(ni->ni_nid)), + -EINVAL); return -EINVAL; } - LIBCFS_ALLOC(ni->ni_interface, - strlen(iface) + 1); + /* Allocate memory for the interface, so the code parsing input into + * tokens and adding interfaces can free the input safely. + * ni->ni_interface is freed in lnet_ni_free(). + */ + LIBCFS_ALLOC(ni->ni_interface, iface_len); if (ni->ni_interface == NULL) { - CERROR("Can't allocate net interface name\n"); + CERROR("%s: cannot allocate net interface name: rc = %d\n", + iface, -ENOMEM); return -ENOMEM; } - strncpy(ni->ni_interface, iface, - strlen(iface) + 1); + strscpy(ni->ni_interface, iface, iface_len); return 0; } -- 1.8.3.1