[PATCH 06/18] smb:client: Store the tcon dstaddr in the witness registration

Samuel Cabrero <[email protected]>
Newsgroups org.kernel.vger.linux-cifs
Message-ID <[email protected]>
Prepare to remove the tcon pointer from cifs_swn_reg struct.

Always use the address stored in the swnreg to send the register netlink
message.

To find the swnreg matching a tcon the stored address is compared to the
tcon dstaddr.

Signed-off-by: Samuel Cabrero <[email protected]>
---
 fs/smb/client/cifs_swn.c | 125 ++++++++++++++++++---------------------
 1 file changed, 56 insertions(+), 69 deletions(-)

diff --git a/fs/smb/client/cifs_swn.c b/fs/smb/client/cifs_swn.c
index 36e199eb04c1..a33caaf4aaf0 100644
--- a/fs/smb/client/cifs_swn.c
+++ b/fs/smb/client/cifs_swn.c
@@ -25,6 +25,7 @@ struct cifs_swn_reg {
 
 	const char *net_name;
 	const char *share_name;
+	struct sockaddr_storage addr;
 	bool net_name_notify;
 	bool share_name_notify;
 	bool ip_notify;
@@ -81,7 +82,6 @@ static int cifs_swn_send_register_message(struct cifs_swn_reg *swnreg)
 	struct sk_buff *skb;
 	struct genlmsghdr *hdr;
 	enum securityEnum authtype;
-	struct sockaddr_storage *addr;
 	int ret;
 
 	skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
@@ -106,18 +106,8 @@ static int cifs_swn_send_register_message(struct cifs_swn_reg *swnreg)
 	if (ret < 0)
 		goto nlmsg_fail;
 
-	/*
-	 * If there is an address stored use it instead of the server address, because we are
-	 * in the process of reconnecting to it after a share has been moved or we have been
-	 * told to switch to it (client move message). In these cases we unregister from the
-	 * server address and register to the new address when we receive the notification.
-	 */
-	if (swnreg->tcon->ses->server->use_swn_dstaddr)
-		addr = &swnreg->tcon->ses->server->swn_dstaddr;
-	else
-		addr = &swnreg->tcon->ses->server->dstaddr;
-
-	ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP, sizeof(struct sockaddr_storage), addr);
+	ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP,
+		      sizeof(struct sockaddr_storage), &swnreg->addr);
 	if (ret < 0)
 		goto nlmsg_fail;
 
@@ -207,8 +197,8 @@ static int cifs_swn_send_unregister_message(struct cifs_swn_reg *swnreg)
 	if (ret < 0)
 		goto nlmsg_fail;
 
-	ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP, sizeof(struct sockaddr_storage),
-			&swnreg->tcon->ses->server->dstaddr);
+	ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP,
+		      sizeof(struct sockaddr_storage), &swnreg->addr);
 	if (ret < 0)
 		goto nlmsg_fail;
 
@@ -290,7 +280,6 @@ static void cifs_swn_reg_check(struct work_struct *work)
 	}
 	mutex_unlock(&cifs_swnreg_idr_mutex);
 
-
 	/*
 	 * It is safe to send the registration message multiple times.
 	 * The userspace client library tracks if registered or not
@@ -347,13 +336,23 @@ static struct cifs_swn_reg *cifs_find_swn_reg(struct cifs_tcon *tcon)
 	}
 
 	idr_for_each_entry(&cifs_swnreg_idr, swnreg, id) {
-		if (strcasecmp(swnreg->net_name, net_name) != 0
-		    || strcasecmp(swnreg->share_name, share_name) != 0) {
+		struct sockaddr_storage *tcon_dstaddr;
+
+		if (tcon->ses->server->use_swn_dstaddr)
+			tcon_dstaddr = &tcon->ses->server->swn_dstaddr;
+		else
+			tcon_dstaddr = &tcon->ses->server->dstaddr;
+
+		if (strcasecmp(swnreg->net_name, net_name) != 0 ||
+		    strcasecmp(swnreg->share_name, share_name) != 0 ||
+		    !cifs_match_ipaddr((struct sockaddr *)&swnreg->addr,
+				       (struct sockaddr *)tcon_dstaddr)) {
 			continue;
 		}
 
-		cifs_dbg(FYI, "Existing swn registration for %s:%s found\n", swnreg->net_name,
-				swnreg->share_name);
+		cifs_dbg(FYI,
+			 "Existing swn registration for %pISc:%s:%s found\n",
+			 &swnreg->addr, swnreg->net_name, swnreg->share_name);
 
 		kfree(net_name);
 		kfree(share_name);
@@ -416,6 +415,17 @@ static struct cifs_swn_reg *cifs_get_swn_reg(struct cifs_tcon *tcon)
 		goto fail_net_name;
 	}
 
+	/*
+	 * If there is an address stored use it instead of the server address, because we are
+	 * in the process of reconnecting to it after a share has been moved or we have been
+	 * told to switch to it (client move message). In these cases we unregister from the
+	 * server address and register to the new address when we receive the notification.
+	 */
+	if (tcon->ses->server->use_swn_dstaddr)
+		swnreg->addr = tcon->ses->server->swn_dstaddr;
+	else
+		swnreg->addr = tcon->ses->server->dstaddr;
+
 	swnreg->net_name_notify = true;
 	swnreg->share_name_notify =
 		(tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC);
@@ -461,26 +471,6 @@ static int cifs_swn_resource_state_changed(struct cifs_swn_reg *swnreg, const ch
 	return 0;
 }
 
-static bool cifs_sockaddr_equal(struct sockaddr_storage *addr1, struct sockaddr_storage *addr2)
-{
-	if (addr1->ss_family != addr2->ss_family)
-		return false;
-
-	if (addr1->ss_family == AF_INET) {
-		return (memcmp(&((const struct sockaddr_in *)addr1)->sin_addr,
-				&((const struct sockaddr_in *)addr2)->sin_addr,
-				sizeof(struct in_addr)) == 0);
-	}
-
-	if (addr1->ss_family == AF_INET6) {
-		return (memcmp(&((const struct sockaddr_in6 *)addr1)->sin6_addr,
-				&((const struct sockaddr_in6 *)addr2)->sin6_addr,
-				sizeof(struct in6_addr)) == 0);
-	}
-
-	return false;
-}
-
 static int cifs_swn_store_swn_addr(const struct sockaddr_storage *new,
 				   const struct sockaddr_storage *old,
 				   struct sockaddr_storage *dst)
@@ -516,18 +506,13 @@ static int cifs_swn_reconnect(struct cifs_tcon *tcon, struct sockaddr_storage *a
 {
 	int ret = 0;
 
-	/* Store the reconnect address */
 	cifs_server_lock(tcon->ses->server);
-	if (cifs_sockaddr_equal(&tcon->ses->server->dstaddr, addr))
-		goto unlock;
 
-	ret = cifs_swn_store_swn_addr(addr, &tcon->ses->server->dstaddr,
-				      &tcon->ses->server->swn_dstaddr);
-	if (ret < 0) {
-		cifs_dbg(VFS, "%s: failed to store address: %d\n", __func__, ret);
+	if (cifs_match_ipaddr((struct sockaddr *)&tcon->ses->server->dstaddr,
+			      (struct sockaddr *)addr)) {
+		/* no-op */
 		goto unlock;
 	}
-	tcon->ses->server->use_swn_dstaddr = true;
 
 	/*
 	 * Unregister to stop receiving notifications for the old IP address.
@@ -536,8 +521,23 @@ static int cifs_swn_reconnect(struct cifs_tcon *tcon, struct sockaddr_storage *a
 	if (ret < 0) {
 		cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
 			 __func__, ret);
+		/*
+		 * Do not jump return on error, continue storing and registering for
+		 * notifications for the new address. There will be a stale registration
+		 * around running its periodic check task, which should cancel itself
+		 * if no matching any tcon.
+		 */
+	}
+
+	/* Store the reconnect address */
+	ret = cifs_swn_store_swn_addr(addr, &tcon->ses->server->dstaddr,
+				      &tcon->ses->server->swn_dstaddr);
+	if (ret < 0) {
+		cifs_dbg(VFS, "%s: failed to store address: %d\n", __func__,
+			 ret);
 		goto unlock;
 	}
+	tcon->ses->server->use_swn_dstaddr = true;
 
 	/*
 	 * And register to receive notifications for the new IP address now that we have
@@ -681,33 +681,20 @@ int cifs_swn_unregister(struct cifs_tcon *tcon)
 void cifs_swn_dump(struct seq_file *m)
 {
 	struct cifs_swn_reg *swnreg;
-	struct sockaddr_in *sa;
-	struct sockaddr_in6 *sa6;
 	int id;
 
 	seq_puts(m, "Witness registrations:");
 
 	mutex_lock(&cifs_swnreg_idr_mutex);
 	idr_for_each_entry(&cifs_swnreg_idr, swnreg, id) {
-		seq_printf(m, "\nId: %u Refs: %u Network name: '%s'%s Share name: '%s'%s Ip address: ",
-				id, kref_read(&swnreg->ref_count),
-				swnreg->net_name, swnreg->net_name_notify ? "(y)" : "(n)",
-				swnreg->share_name, swnreg->share_name_notify ? "(y)" : "(n)");
-		switch (swnreg->tcon->ses->server->dstaddr.ss_family) {
-		case AF_INET:
-			sa = (struct sockaddr_in *) &swnreg->tcon->ses->server->dstaddr;
-			seq_printf(m, "%pI4", &sa->sin_addr.s_addr);
-			break;
-		case AF_INET6:
-			sa6 = (struct sockaddr_in6 *) &swnreg->tcon->ses->server->dstaddr;
-			seq_printf(m, "%pI6", &sa6->sin6_addr.s6_addr);
-			if (sa6->sin6_scope_id)
-				seq_printf(m, "%%%u", sa6->sin6_scope_id);
-			break;
-		default:
-			seq_puts(m, "(unknown)");
-		}
-		seq_printf(m, "%s", swnreg->ip_notify ? "(y)" : "(n)");
+		seq_printf(
+			m,
+			"\nId: %u Refs: %u Network name: '%s'%s Share name: '%s'%s Ip address: '%pISc'%s",
+			id, kref_read(&swnreg->ref_count), swnreg->net_name,
+			swnreg->net_name_notify ? "(y)" : "(n)",
+			swnreg->share_name,
+			swnreg->share_name_notify ? "(y)" : "(n)",
+			&swnreg->addr, swnreg->ip_notify ? "(y)" : "(n)");
 	}
 	mutex_unlock(&cifs_swnreg_idr_mutex);
 	seq_puts(m, "\n");
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.