Whamcloud - gitweb
LU-14665 lnet: simplify lnet_ni_add_interface 25/43525/12
authorOlaf Faaland <faaland1@llnl.gov>
Tue, 4 May 2021 02:40:22 +0000 (19:40 -0700)
committerOleg Drokin <green@whamcloud.com>
Wed, 2 Jun 2021 17:50:01 +0000 (17:50 +0000)
Remove an unnecessary counter and move the comment before
the relevant code.  Improve error messages.

Test-parameters: trivial

Signed-off-by: Olaf Faaland <faaland1@llnl.gov>
Change-Id: Iffc7a128b16bc1b2be7a44413a5972c97b12a5fa
Reviewed-on: https://review.whamcloud.com/43525
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Serguei Smirnov <ssmirnov@whamcloud.com>
Reviewed-by: Neil Brown <neilb@suse.de>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lnet/lnet/config.c

index 1c9bd4f..f6ade39 100644 (file)
@@ -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;
 }