On soft mounts cifs_wait_for_server_reconnect() waited up to 10 seconds (per target) for the reconnect, even if the reconnect attempts failed right away, e.g. because the local address used for the connection was removed. This delays e.g. the statfs() umount(8) does before calling umount(2) by 10 seconds. Count the reconnect attempts that failed with an error indicating a local problem, -ENODEV if the network device used for the connection is gone, -ENETDOWN if the local network is down, -EAFNOSUPPORT if the address family isn't available, e.g. IPv6 is disabled and the server's name resolves to an IPv6 address after a DNS change, and -EPERM if a cgroup BPF program for connect or bind denied it (ICMP errors never map to -EPERM), in server->reconnect_local_failures and wake up server->response_q after each of them, so that requests on soft mounts give up once the next such reconnect attempt failed. It's an atomic_t, as SMB1 also reconnects in the context of a request, not only in cifsd. For SMB Direct the route check is only an approximation, as it uses the inputs of the TCP connect, while iWARP connects to port 5445, and an RDMA device removal is reported as -ENETDOWN. The source address is the same since the previous patch. -EADDRNOTAVAIL, -ENETUNREACH, -EHOSTUNREACH and -EACCES are only counted if there is no usable route to the server, e.g. the local address and its route were removed, or there is an unreachable or prohibit route for the server. They also come from ICMP errors of a router on the way, which abort the connect, as -EHOSTUNREACH if the server doesn't answer ARP or neighbour discovery, e.g. while it restarts, or as -EADDRNOTAVAIL if no local port is free, and then a route lookup to the server still succeeds. The lookup uses the same inputs as the TCP connect: the protocol and port for policy routing, IPv4 routing for an IPv4-mapped address and the scope id only for a link-local address, with a snapshot of the addresses taken under srv_lock. As the IPv6 lookup doesn't check whether the source address is still a local one, like the IPv4 lookup does, a removed IPv6 srcaddr is checked explicitly. A local firewall that rejects the connection with an ICMP error looks the same and keeps waiting, which only costs the waiting time as before. Note that a short local outage, e.g. while the address or route is removed or the interface is set down for a few seconds, now also makes requests on soft mounts fail after about 3 seconds instead of 10. That's what soft mounts are for, hard mounts hide it as before. Just losing the carrier, e.g. a cable replug, keeps the address and route, so the requests keep waiting as before. For a DFS mount the reconnect tries all targets in one attempt, so it only counts as failed locally if every target failed locally, as the error of the last target alone says nothing about the others, e.g. one target may be restarting while another one is unreachable from here. IPC tcons, e.g. the one of a session used for DFS referrals, have no retry setting of their own, so they keep waiting as before, also on soft mounts. For other errors the requests wait up to 10 seconds as before, e.g. while the server restarts the first attempt may fail with -ECONNREFUSED and the next one succeeds, so the 10 second wait still hides such a short outage from the applications. Hard mounts still keep waiting. For stable: before v7.1 CONFIG_IPV6 can be a module, so a backport has to use IS_REACHABLE(CONFIG_IPV6) for the IPv6 route lookup or ipv6_stub, and the route lookup helpers changed their arguments over time. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Fixes: aa24d1e96924 ("CIFS: Process reconnects for SMB2 shares") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cifsglob.h | 5 + fs/smb/client/connect.c | 193 ++++++++++++++++++++++++++++++++++++++- fs/smb/client/misc.c | 31 +++++-- 3 files changed, 218 insertions(+), 11 deletions(-) diff --git a/fs/smb/client/cifsglob.h b/fs/smb/client/cifsglob.h index 51c987806fd7..58ad71d20044 100644 --- a/fs/smb/client/cifsglob.h +++ b/fs/smb/client/cifsglob.h @@ -743,6 +743,11 @@ struct TCP_Server_Info { char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* for signing, protected by srv_mutex */ __u32 reconnect_instance; /* incremented on each reconnect */ + /* + * incremented on each reconnect attempt that failed because + * of a local problem + */ + atomic_t reconnect_local_failures; __le32 session_key_id; /* retrieved from negotiate response and send in session setup request */ struct session_key session_key; unsigned long lstrp; /* when we got last response from this server */ diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index c5fa675e6a11..865051e579db 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include #include "cifsglob.h" @@ -409,6 +411,167 @@ static void cifs_reconnect_wait_before_retry(struct TCP_Server_Info *server) 3 * HZ); } +static bool cifs_route4_missing(struct net *net, __be32 daddr, __be32 saddr, + __be16 dport) +{ + struct flowi4 fl4; + struct rtable *rt; + + rt = ip_route_output_ports(net, &fl4, NULL, daddr, saddr, dport, 0, + IPPROTO_TCP, 0, 0); + if (IS_ERR(rt)) + return true; + ip_rt_put(rt); + return false; +} + +/* + * Returns true if there is no usable route to the server, e.g. as the + * local address and its route were removed, or there is an unreachable + * or prohibit route for the server. The lookup uses the same inputs as + * the TCP connect: protocol and port for policy routing, IPv4 routing + * for an IPv4-mapped address and the scope id only for a link-local + * address. + */ +static bool cifs_server_route_missing(struct TCP_Server_Info *server) +{ + struct net *net = cifs_net_ns(server); + struct sockaddr_storage dst, src; + struct sockaddr_in *dst4 = (struct sockaddr_in *)&dst; + struct sockaddr_in *src4 = (struct sockaddr_in *)&src; + + /* the interface query of a channel can change them */ + spin_lock(&server->srv_lock); + memcpy(&dst, &server->dstaddr, sizeof(dst)); + memcpy(&src, &server->srcaddr, sizeof(src)); + spin_unlock(&server->srv_lock); + + switch (dst.ss_family) { + case AF_INET: + return cifs_route4_missing(net, dst4->sin_addr.s_addr, + src.ss_family == AF_INET ? + src4->sin_addr.s_addr : 0, + dst4->sin_port); +#if IS_ENABLED(CONFIG_IPV6) + case AF_INET6: { + struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)&dst; + struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)&src; + struct flowi6 fl6 = { + .flowi6_proto = IPPROTO_TCP, + .daddr = dst6->sin6_addr, + .fl6_dport = dst6->sin6_port, + }; + struct dst_entry *dst_entry; + + if (ipv6_addr_v4mapped(&dst6->sin6_addr)) { + __be32 saddr = 0; + + if (src.ss_family == AF_INET6 && + ipv6_addr_v4mapped(&src6->sin6_addr)) + saddr = src6->sin6_addr.s6_addr32[3]; + else if (src.ss_family == AF_INET) + saddr = src4->sin_addr.s_addr; + return cifs_route4_missing(net, + dst6->sin6_addr.s6_addr32[3], + saddr, dst6->sin6_port); + } + + /* connect failed with -EAFNOSUPPORT anyway */ + if (!ipv6_mod_enabled()) + return false; + + if (ipv6_addr_type(&dst6->sin6_addr) & IPV6_ADDR_LINKLOCAL) + fl6.flowi6_oif = dst6->sin6_scope_id; + if (src.ss_family == AF_INET6) { + /* + * Unlike the IPv4 lookup, the IPv6 one doesn't + * check whether the source address is still a + * local one, the bind() fails if it's gone. + */ + if (!ipv6_addr_any(&src6->sin6_addr) && + !ipv6_chk_addr(net, &src6->sin6_addr, NULL, 0)) + return true; + fl6.saddr = src6->sin6_addr; + } + + dst_entry = ip6_dst_lookup_flow(net, NULL, &fl6, NULL); + if (IS_ERR(dst_entry)) + return true; + dst_release(dst_entry); + return false; + } +#endif + } + + return false; +} + +/* + * Errors that indicate a local problem, which is not expected to go + * away within a few seconds, e.g. the local address or the network + * device used for the connection is gone. Other errors, e.g. + * -ECONNREFUSED while the server restarts, may be transient. + * + * The route check uses the inputs of the TCP connect, iWARP connects + * to port 5445 instead, and an RDMA device removal, e.g. during a + * device reset, is reported as -ENETDOWN, so for SMB Direct this is + * only an approximation. + */ +static bool cifs_reconnect_error_is_local(struct TCP_Server_Info *server, + int rc) +{ + switch (rc) { + case -ENETDOWN: + case -ENODEV: + return true; + case -EAFNOSUPPORT: + /* + * E.g. IPv6 is disabled and the server's name resolves to + * an IPv6 address after a DNS change. + */ + return true; + case -EPERM: + /* + * A cgroup BPF program for connect or bind denied it, ICMP + * errors never map to -EPERM. + */ + return true; + case -EADDRNOTAVAIL: + case -ENETUNREACH: + case -EHOSTUNREACH: + case -EACCES: + /* + * These also come from ICMP errors of a router on the + * way, or -EHOSTUNREACH if the server doesn't answer + * ARP or neighbour discovery, e.g. while it restarts, + * and -EADDRNOTAVAIL if no local port is free. They are + * only local if there is no usable route to the server + * here, e.g. as the source address is gone. + */ + return cifs_server_route_missing(server); + } + + return false; +} + +/* + * Let the requests waiting in cifs_wait_for_server_reconnect() + * on soft mounts know that a reconnect attempt failed because + * of a local problem. + */ +static void cifs_reconnect_failed(struct TCP_Server_Info *server, bool local) +{ + if (!local) + return; + + /* + * Not only cifsd, SMB1 also reconnects from cifs_check_receive() + * in the context of a request. + */ + atomic_inc(&server->reconnect_local_failures); + wake_up_all(&server->response_q); +} + /* * cifs tcp session reconnection * @@ -459,6 +622,8 @@ static int __cifs_reconnect(struct TCP_Server_Info *server, if (rc) { cifs_server_unlock(server); cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); + cifs_reconnect_failed(server, + cifs_reconnect_error_is_local(server, rc)); /* If was asked to reconnect only once, do not try it more times */ if (once) break; @@ -520,35 +685,51 @@ static int __reconnect_target_locked(struct TCP_Server_Info *server, return rc; } +/* + * *all_local tells whether every target failed because of a local + * problem, see cifs_reconnect_error_is_local(), it has to be checked + * after each target, as server->dstaddr changes with the target. + */ static int reconnect_target_locked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl, - struct dfs_cache_tgt_iterator **target_hint) + struct dfs_cache_tgt_iterator **target_hint, + bool *all_local) { struct dfs_cache_tgt_iterator *tit; int rc; *target_hint = NULL; + *all_local = false; /* If dfs target list is empty, then reconnect to last server */ tit = dfs_cache_get_tgt_iterator(tl); - if (!tit) - return __reconnect_target_locked(server, server->hostname); + if (!tit) { + rc = __reconnect_target_locked(server, server->hostname); + if (rc) + *all_local = cifs_reconnect_error_is_local(server, rc); + return rc; + } /* Otherwise, try every dfs target in @tl */ + *all_local = true; do { const char *target = dfs_cache_get_tgt_name(tit); spin_lock(&server->srv_lock); if (server->tcpStatus != CifsNeedReconnect) { spin_unlock(&server->srv_lock); + *all_local = false; return -ECONNRESET; } spin_unlock(&server->srv_lock); rc = __reconnect_target_locked(server, target); if (!rc) { *target_hint = tit; + *all_local = false; break; } + if (!cifs_reconnect_error_is_local(server, rc)) + *all_local = false; } while ((tit = dfs_cache_get_next_tgt(tl, tit))); return rc; } @@ -559,6 +740,7 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server) const char *ref_path = server->leaf_fullpath + 1; DFS_CACHE_TGT_LIST(tl); int num_targets = 0; + bool all_local; int rc = 0; /* @@ -590,11 +772,14 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server) try_to_freeze(); cifs_server_lock(server); - rc = reconnect_target_locked(server, &tl, &target_hint); + rc = reconnect_target_locked(server, &tl, &target_hint, + &all_local); if (rc) { /* Failed to reconnect socket */ cifs_server_unlock(server); cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); + /* only if all targets failed locally */ + cifs_reconnect_failed(server, all_local); cifs_reconnect_wait_before_retry(server); continue; } diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c index 197f39c23119..5ee6414e2aae 100644 --- a/fs/smb/client/misc.c +++ b/fs/smb/client/misc.c @@ -1096,17 +1096,29 @@ bool cifs_server_wait_for_exit_ms(struct TCP_Server_Info *server, } static bool reco_wait_done(struct TCP_Server_Info *server, - struct cifs_tcon *tcon, int aborts) + struct cifs_tcon *tcon, int aborts, + int failures) { - return server->tcpStatus != CifsNeedReconnect || - atomic_read(&tcon->umount_aborts) != aborts || - cifs_tcon_dying(tcon); + if (server->tcpStatus != CifsNeedReconnect || + atomic_read(&tcon->umount_aborts) != aborts || + cifs_tcon_dying(tcon)) + return true; + + /* + * soft mounts only wait until the next reconnect attempt failed + * because of a local problem, not IPC tcons, e.g. the one of a + * session used for DFS referrals, they have no retry setting of + * their own and wait as before + */ + return !tcon->retry && !tcon->ipc && + atomic_read(&server->reconnect_local_failures) != failures; } int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, struct cifs_tcon *tcon, int aborts, bool nowait) { + int failures; int timeout = 10; int rc; @@ -1120,6 +1132,7 @@ int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, return -EAGAIN; } timeout *= server->nr_targets; + failures = atomic_read(&server->reconnect_local_failures); spin_unlock(&server->srv_lock); /* @@ -1127,14 +1140,18 @@ int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, * reconnect -- should be greater than cifs socket timeout which is 7 * seconds. * - * On "soft" mounts we wait once. Hard mounts keep retrying until - * process is killed or server comes back on-line. + * On "soft" mounts we wait once, but only until the next reconnect + * attempt failed because of a local problem, e.g. it can't succeed + * if the local address is gone. + * Hard mounts keep retrying until process is killed or server comes + * back on-line. */ do { rc = wait_event_interruptible_timeout(server->response_q, reco_wait_done(server, tcon, - aborts), + aborts, + failures), timeout * HZ); if (rc < 0) { cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n", -- 2.43.0