diff --git a/fs/namei.c b/fs/namei.c
index d729ef7cbf46ffa366f9b30638bf85f03e3f8966..99374704c17ddf45bdc0fb6e689f28a7b46f8490 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1792,7 +1792,9 @@ static int link_path_walk(const char *name, struct nameidata *nd)
 
 				if (unlikely(IS_ERR(s))) {
 					err = PTR_ERR(s);
-					break;
+					current->link_count--;
+					nd->depth--;
+					return err;
 				}
 				err = 0;
 				if (likely(s)) {