xref: /openbmc/linux/fs/smb/client/dfs.c (revision 3fea12f3c67de30fd2e6a3f5da2b5354aa22348e)
138c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0
238c8a9a5SSteve French /*
338c8a9a5SSteve French  * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
438c8a9a5SSteve French  */
538c8a9a5SSteve French 
638c8a9a5SSteve French #include <linux/namei.h>
738c8a9a5SSteve French #include "cifsproto.h"
838c8a9a5SSteve French #include "cifs_debug.h"
938c8a9a5SSteve French #include "dns_resolve.h"
1038c8a9a5SSteve French #include "fs_context.h"
1138c8a9a5SSteve French #include "dfs.h"
1238c8a9a5SSteve French 
1338c8a9a5SSteve French /**
1438c8a9a5SSteve French  * dfs_parse_target_referral - set fs context for dfs target referral
1538c8a9a5SSteve French  *
1638c8a9a5SSteve French  * @full_path: full path in UNC format.
1738c8a9a5SSteve French  * @ref: dfs referral pointer.
1838c8a9a5SSteve French  * @ctx: smb3 fs context pointer.
1938c8a9a5SSteve French  *
2038c8a9a5SSteve French  * Return zero if dfs referral was parsed correctly, otherwise non-zero.
2138c8a9a5SSteve French  */
2238c8a9a5SSteve French int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
2338c8a9a5SSteve French 			      struct smb3_fs_context *ctx)
2438c8a9a5SSteve French {
2538c8a9a5SSteve French 	int rc;
2638c8a9a5SSteve French 	const char *prepath = NULL;
2738c8a9a5SSteve French 	char *path;
2838c8a9a5SSteve French 
2938c8a9a5SSteve French 	if (!full_path || !*full_path || !ref || !ctx)
3038c8a9a5SSteve French 		return -EINVAL;
3138c8a9a5SSteve French 
3238c8a9a5SSteve French 	if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
3338c8a9a5SSteve French 		return -EINVAL;
3438c8a9a5SSteve French 
3538c8a9a5SSteve French 	if (strlen(full_path) - ref->path_consumed) {
3638c8a9a5SSteve French 		prepath = full_path + ref->path_consumed;
3738c8a9a5SSteve French 		/* skip initial delimiter */
3838c8a9a5SSteve French 		if (*prepath == '/' || *prepath == '\\')
3938c8a9a5SSteve French 			prepath++;
4038c8a9a5SSteve French 	}
4138c8a9a5SSteve French 
4238c8a9a5SSteve French 	path = cifs_build_devname(ref->node_name, prepath);
4338c8a9a5SSteve French 	if (IS_ERR(path))
4438c8a9a5SSteve French 		return PTR_ERR(path);
4538c8a9a5SSteve French 
4638c8a9a5SSteve French 	rc = smb3_parse_devname(path, ctx);
4738c8a9a5SSteve French 	if (rc)
4838c8a9a5SSteve French 		goto out;
4938c8a9a5SSteve French 
5038c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
5138c8a9a5SSteve French 
5238c8a9a5SSteve French out:
5338c8a9a5SSteve French 	kfree(path);
5438c8a9a5SSteve French 	return rc;
5538c8a9a5SSteve French }
5638c8a9a5SSteve French 
5738c8a9a5SSteve French static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
5838c8a9a5SSteve French {
5938c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
6038c8a9a5SSteve French 	int rc;
6138c8a9a5SSteve French 
6238c8a9a5SSteve French 	ctx->leaf_fullpath = (char *)full_path;
6338c8a9a5SSteve French 	rc = cifs_mount_get_session(mnt_ctx);
6438c8a9a5SSteve French 	ctx->leaf_fullpath = NULL;
6538c8a9a5SSteve French 
6638c8a9a5SSteve French 	return rc;
6738c8a9a5SSteve French }
6838c8a9a5SSteve French 
69bf99f6beSPaulo Alcantara /*
70bf99f6beSPaulo Alcantara  * Track individual DFS referral servers used by new DFS mount.
71bf99f6beSPaulo Alcantara  *
72bf99f6beSPaulo Alcantara  * On success, their lifetime will be shared by final tcon (dfs_ses_list).
73bf99f6beSPaulo Alcantara  * Otherwise, they will be put by dfs_put_root_smb_sessions() in cifs_mount().
74bf99f6beSPaulo Alcantara  */
7538c8a9a5SSteve French static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
7638c8a9a5SSteve French {
7738c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
7838c8a9a5SSteve French 	struct dfs_root_ses *root_ses;
7938c8a9a5SSteve French 	struct cifs_ses *ses = mnt_ctx->ses;
8038c8a9a5SSteve French 
8138c8a9a5SSteve French 	if (ses) {
8238c8a9a5SSteve French 		root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
8338c8a9a5SSteve French 		if (!root_ses)
8438c8a9a5SSteve French 			return -ENOMEM;
8538c8a9a5SSteve French 
8638c8a9a5SSteve French 		INIT_LIST_HEAD(&root_ses->list);
8738c8a9a5SSteve French 
8838c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
89bf99f6beSPaulo Alcantara 		cifs_smb_ses_inc_refcount(ses);
9038c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
9138c8a9a5SSteve French 		root_ses->ses = ses;
9238c8a9a5SSteve French 		list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
9338c8a9a5SSteve French 	}
94bf99f6beSPaulo Alcantara 	/* Select new DFS referral server so that new referrals go through it */
9538c8a9a5SSteve French 	ctx->dfs_root_ses = ses;
9638c8a9a5SSteve French 	return 0;
9738c8a9a5SSteve French }
9838c8a9a5SSteve French 
9938c8a9a5SSteve French static int get_dfs_conn(struct cifs_mount_ctx *mnt_ctx, const char *ref_path, const char *full_path,
10038c8a9a5SSteve French 			const struct dfs_cache_tgt_iterator *tit)
10138c8a9a5SSteve French {
10238c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
10338c8a9a5SSteve French 	struct dfs_info3_param ref = {};
10438c8a9a5SSteve French 	bool is_refsrv;
10538c8a9a5SSteve French 	int rc, rc2;
10638c8a9a5SSteve French 
10738c8a9a5SSteve French 	rc = dfs_cache_get_tgt_referral(ref_path + 1, tit, &ref);
10838c8a9a5SSteve French 	if (rc)
10938c8a9a5SSteve French 		return rc;
11038c8a9a5SSteve French 
11138c8a9a5SSteve French 	rc = dfs_parse_target_referral(full_path + 1, &ref, ctx);
11238c8a9a5SSteve French 	if (rc)
11338c8a9a5SSteve French 		goto out;
11438c8a9a5SSteve French 
11538c8a9a5SSteve French 	cifs_mount_put_conns(mnt_ctx);
11638c8a9a5SSteve French 	rc = get_session(mnt_ctx, ref_path);
11738c8a9a5SSteve French 	if (rc)
11838c8a9a5SSteve French 		goto out;
11938c8a9a5SSteve French 
12038c8a9a5SSteve French 	is_refsrv = !!(ref.flags & DFSREF_REFERRAL_SERVER);
12138c8a9a5SSteve French 
12238c8a9a5SSteve French 	rc = -EREMOTE;
12338c8a9a5SSteve French 	if (ref.flags & DFSREF_STORAGE_SERVER) {
12438c8a9a5SSteve French 		rc = cifs_mount_get_tcon(mnt_ctx);
12538c8a9a5SSteve French 		if (rc)
12638c8a9a5SSteve French 			goto out;
12738c8a9a5SSteve French 
12838c8a9a5SSteve French 		/* some servers may not advertise referral capability under ref.flags */
12938c8a9a5SSteve French 		is_refsrv |= is_tcon_dfs(mnt_ctx->tcon);
13038c8a9a5SSteve French 
13138c8a9a5SSteve French 		rc = cifs_is_path_remote(mnt_ctx);
13238c8a9a5SSteve French 	}
13338c8a9a5SSteve French 
13438c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(ref_path + 1, tit);
13538c8a9a5SSteve French 
13638c8a9a5SSteve French 	if (rc == -EREMOTE && is_refsrv) {
13738c8a9a5SSteve French 		rc2 = add_root_smb_session(mnt_ctx);
13838c8a9a5SSteve French 		if (rc2)
13938c8a9a5SSteve French 			rc = rc2;
14038c8a9a5SSteve French 	}
14138c8a9a5SSteve French 
14238c8a9a5SSteve French out:
14338c8a9a5SSteve French 	free_dfs_info_param(&ref);
14438c8a9a5SSteve French 	return rc;
14538c8a9a5SSteve French }
14638c8a9a5SSteve French 
14738c8a9a5SSteve French static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
14838c8a9a5SSteve French {
14938c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
15038c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
15138c8a9a5SSteve French 	char *ref_path = NULL, *full_path = NULL;
15238c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
15338c8a9a5SSteve French 	struct cifs_tcon *tcon;
15438c8a9a5SSteve French 	char *origin_fullpath = NULL;
15549024ec8SPaulo Alcantara 	char sep = CIFS_DIR_SEP(cifs_sb);
15638c8a9a5SSteve French 	int num_links = 0;
15738c8a9a5SSteve French 	int rc;
15838c8a9a5SSteve French 
15938c8a9a5SSteve French 	ref_path = dfs_get_path(cifs_sb, ctx->UNC);
16038c8a9a5SSteve French 	if (IS_ERR(ref_path))
16138c8a9a5SSteve French 		return PTR_ERR(ref_path);
16238c8a9a5SSteve French 
16349024ec8SPaulo Alcantara 	full_path = smb3_fs_context_fullpath(ctx, sep);
16438c8a9a5SSteve French 	if (IS_ERR(full_path)) {
16538c8a9a5SSteve French 		rc = PTR_ERR(full_path);
16638c8a9a5SSteve French 		full_path = NULL;
16738c8a9a5SSteve French 		goto out;
16838c8a9a5SSteve French 	}
16938c8a9a5SSteve French 
17038c8a9a5SSteve French 	origin_fullpath = kstrdup(full_path, GFP_KERNEL);
17138c8a9a5SSteve French 	if (!origin_fullpath) {
17238c8a9a5SSteve French 		rc = -ENOMEM;
17338c8a9a5SSteve French 		goto out;
17438c8a9a5SSteve French 	}
17538c8a9a5SSteve French 
17638c8a9a5SSteve French 	do {
177*3fea12f3SPaulo Alcantara 		DFS_CACHE_TGT_LIST(tl);
17838c8a9a5SSteve French 
17938c8a9a5SSteve French 		rc = dfs_get_referral(mnt_ctx, ref_path + 1, NULL, &tl);
18011260c3dSPaulo Alcantara 		if (rc) {
18111260c3dSPaulo Alcantara 			rc = cifs_mount_get_tcon(mnt_ctx);
18211260c3dSPaulo Alcantara 			if (!rc)
18311260c3dSPaulo Alcantara 				rc = cifs_is_path_remote(mnt_ctx);
18438c8a9a5SSteve French 			break;
18511260c3dSPaulo Alcantara 		}
18638c8a9a5SSteve French 
18738c8a9a5SSteve French 		tit = dfs_cache_get_tgt_iterator(&tl);
18838c8a9a5SSteve French 		if (!tit) {
18938c8a9a5SSteve French 			cifs_dbg(VFS, "%s: dfs referral (%s) with no targets\n", __func__,
19038c8a9a5SSteve French 				 ref_path + 1);
19138c8a9a5SSteve French 			rc = -ENOENT;
19238c8a9a5SSteve French 			dfs_cache_free_tgts(&tl);
19338c8a9a5SSteve French 			break;
19438c8a9a5SSteve French 		}
19538c8a9a5SSteve French 
19638c8a9a5SSteve French 		do {
19738c8a9a5SSteve French 			rc = get_dfs_conn(mnt_ctx, ref_path, full_path, tit);
19838c8a9a5SSteve French 			if (!rc)
19938c8a9a5SSteve French 				break;
20038c8a9a5SSteve French 			if (rc == -EREMOTE) {
20138c8a9a5SSteve French 				if (++num_links > MAX_NESTED_LINKS) {
20238c8a9a5SSteve French 					rc = -ELOOP;
20338c8a9a5SSteve French 					break;
20438c8a9a5SSteve French 				}
20538c8a9a5SSteve French 				kfree(ref_path);
20638c8a9a5SSteve French 				kfree(full_path);
20738c8a9a5SSteve French 				ref_path = full_path = NULL;
20838c8a9a5SSteve French 
20949024ec8SPaulo Alcantara 				full_path = smb3_fs_context_fullpath(ctx, sep);
21038c8a9a5SSteve French 				if (IS_ERR(full_path)) {
21138c8a9a5SSteve French 					rc = PTR_ERR(full_path);
21238c8a9a5SSteve French 					full_path = NULL;
21338c8a9a5SSteve French 				} else {
21438c8a9a5SSteve French 					ref_path = dfs_get_path(cifs_sb, full_path);
21538c8a9a5SSteve French 					if (IS_ERR(ref_path)) {
21638c8a9a5SSteve French 						rc = PTR_ERR(ref_path);
21738c8a9a5SSteve French 						ref_path = NULL;
21838c8a9a5SSteve French 					}
21938c8a9a5SSteve French 				}
22038c8a9a5SSteve French 				break;
22138c8a9a5SSteve French 			}
22238c8a9a5SSteve French 		} while ((tit = dfs_cache_get_next_tgt(&tl, tit)));
22338c8a9a5SSteve French 		dfs_cache_free_tgts(&tl);
22438c8a9a5SSteve French 	} while (rc == -EREMOTE);
22538c8a9a5SSteve French 
22638c8a9a5SSteve French 	if (!rc) {
22738c8a9a5SSteve French 		tcon = mnt_ctx->tcon;
22838c8a9a5SSteve French 
2293ae872deSPaulo Alcantara 		spin_lock(&tcon->tc_lock);
2303ae872deSPaulo Alcantara 		if (!tcon->origin_fullpath) {
2313ae872deSPaulo Alcantara 			tcon->origin_fullpath = origin_fullpath;
23238c8a9a5SSteve French 			origin_fullpath = NULL;
23338c8a9a5SSteve French 		}
2343ae872deSPaulo Alcantara 		spin_unlock(&tcon->tc_lock);
23538c8a9a5SSteve French 
23638c8a9a5SSteve French 		if (list_empty(&tcon->dfs_ses_list)) {
23738c8a9a5SSteve French 			list_replace_init(&mnt_ctx->dfs_ses_list,
23838c8a9a5SSteve French 					  &tcon->dfs_ses_list);
23938c8a9a5SSteve French 			queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
24038c8a9a5SSteve French 					   dfs_cache_get_ttl() * HZ);
24138c8a9a5SSteve French 		} else {
24238c8a9a5SSteve French 			dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
24338c8a9a5SSteve French 		}
24438c8a9a5SSteve French 	}
24538c8a9a5SSteve French 
24638c8a9a5SSteve French out:
24738c8a9a5SSteve French 	kfree(origin_fullpath);
24838c8a9a5SSteve French 	kfree(ref_path);
24938c8a9a5SSteve French 	kfree(full_path);
25038c8a9a5SSteve French 	return rc;
25138c8a9a5SSteve French }
25238c8a9a5SSteve French 
25338c8a9a5SSteve French int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
25438c8a9a5SSteve French {
25538c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
25638c8a9a5SSteve French 	bool nodfs = ctx->nodfs;
25738c8a9a5SSteve French 	int rc;
25838c8a9a5SSteve French 
25938c8a9a5SSteve French 	*isdfs = false;
26038c8a9a5SSteve French 	rc = get_session(mnt_ctx, NULL);
26138c8a9a5SSteve French 	if (rc)
2623ae872deSPaulo Alcantara 		return rc;
26338c8a9a5SSteve French 
26438c8a9a5SSteve French 	ctx->dfs_root_ses = mnt_ctx->ses;
26538c8a9a5SSteve French 	/*
26638c8a9a5SSteve French 	 * If called with 'nodfs' mount option, then skip DFS resolving.  Otherwise unconditionally
26738c8a9a5SSteve French 	 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
26838c8a9a5SSteve French 	 *
26938c8a9a5SSteve French 	 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
27038c8a9a5SSteve French 	 * to respond with PATH_NOT_COVERED to requests that include the prefix.
27138c8a9a5SSteve French 	 */
27238c8a9a5SSteve French 	if (!nodfs) {
27338c8a9a5SSteve French 		rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
27438c8a9a5SSteve French 		if (rc) {
2755f2a0afaSPaulo Alcantara 			cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
2765f2a0afaSPaulo Alcantara 				 __func__, ctx->UNC + 1, rc);
2775f2a0afaSPaulo Alcantara 			cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
27838c8a9a5SSteve French 			nodfs = true;
27938c8a9a5SSteve French 		}
28038c8a9a5SSteve French 	}
28138c8a9a5SSteve French 	if (nodfs) {
28238c8a9a5SSteve French 		rc = cifs_mount_get_tcon(mnt_ctx);
28338c8a9a5SSteve French 		if (!rc)
28438c8a9a5SSteve French 			rc = cifs_is_path_remote(mnt_ctx);
2853ae872deSPaulo Alcantara 		return rc;
28638c8a9a5SSteve French 	}
28738c8a9a5SSteve French 
28838c8a9a5SSteve French 	*isdfs = true;
289bf99f6beSPaulo Alcantara 	add_root_smb_session(mnt_ctx);
290bf99f6beSPaulo Alcantara 	return __dfs_mount_share(mnt_ctx);
29138c8a9a5SSteve French }
29238c8a9a5SSteve French 
29338c8a9a5SSteve French /* Update dfs referral path of superblock */
29438c8a9a5SSteve French static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
29538c8a9a5SSteve French 				  const char *target)
29638c8a9a5SSteve French {
29738c8a9a5SSteve French 	int rc = 0;
29838c8a9a5SSteve French 	size_t len = strlen(target);
29938c8a9a5SSteve French 	char *refpath, *npath;
30038c8a9a5SSteve French 
30138c8a9a5SSteve French 	if (unlikely(len < 2 || *target != '\\'))
30238c8a9a5SSteve French 		return -EINVAL;
30338c8a9a5SSteve French 
30438c8a9a5SSteve French 	if (target[1] == '\\') {
30538c8a9a5SSteve French 		len += 1;
30638c8a9a5SSteve French 		refpath = kmalloc(len, GFP_KERNEL);
30738c8a9a5SSteve French 		if (!refpath)
30838c8a9a5SSteve French 			return -ENOMEM;
30938c8a9a5SSteve French 
31038c8a9a5SSteve French 		scnprintf(refpath, len, "%s", target);
31138c8a9a5SSteve French 	} else {
31238c8a9a5SSteve French 		len += sizeof("\\");
31338c8a9a5SSteve French 		refpath = kmalloc(len, GFP_KERNEL);
31438c8a9a5SSteve French 		if (!refpath)
31538c8a9a5SSteve French 			return -ENOMEM;
31638c8a9a5SSteve French 
31738c8a9a5SSteve French 		scnprintf(refpath, len, "\\%s", target);
31838c8a9a5SSteve French 	}
31938c8a9a5SSteve French 
32038c8a9a5SSteve French 	npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
32138c8a9a5SSteve French 	kfree(refpath);
32238c8a9a5SSteve French 
32338c8a9a5SSteve French 	if (IS_ERR(npath)) {
32438c8a9a5SSteve French 		rc = PTR_ERR(npath);
32538c8a9a5SSteve French 	} else {
32638c8a9a5SSteve French 		mutex_lock(&server->refpath_lock);
32738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
32838c8a9a5SSteve French 		kfree(server->leaf_fullpath);
32938c8a9a5SSteve French 		server->leaf_fullpath = npath;
33038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
33138c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
33238c8a9a5SSteve French 	}
33338c8a9a5SSteve French 	return rc;
33438c8a9a5SSteve French }
33538c8a9a5SSteve French 
33638c8a9a5SSteve French static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
33738c8a9a5SSteve French 				       bool *target_match)
33838c8a9a5SSteve French {
33938c8a9a5SSteve French 	int rc = 0;
34038c8a9a5SSteve French 	const char *dfs_host;
34138c8a9a5SSteve French 	size_t dfs_host_len;
34238c8a9a5SSteve French 
34338c8a9a5SSteve French 	*target_match = true;
34438c8a9a5SSteve French 	extract_unc_hostname(share, &dfs_host, &dfs_host_len);
34538c8a9a5SSteve French 
34638c8a9a5SSteve French 	/* Check if hostnames or addresses match */
34738c8a9a5SSteve French 	cifs_server_lock(server);
34838c8a9a5SSteve French 	if (dfs_host_len != strlen(server->hostname) ||
34938c8a9a5SSteve French 	    strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
35038c8a9a5SSteve French 		cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
35138c8a9a5SSteve French 			 (int)dfs_host_len, dfs_host, server->hostname);
35238c8a9a5SSteve French 		rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
35338c8a9a5SSteve French 		if (rc)
35438c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
35538c8a9a5SSteve French 	}
35638c8a9a5SSteve French 	cifs_server_unlock(server);
35738c8a9a5SSteve French 	return rc;
35838c8a9a5SSteve French }
35938c8a9a5SSteve French 
36038c8a9a5SSteve French static void __tree_connect_ipc(const unsigned int xid, char *tree,
36138c8a9a5SSteve French 			       struct cifs_sb_info *cifs_sb,
36238c8a9a5SSteve French 			       struct cifs_ses *ses)
36338c8a9a5SSteve French {
36438c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
36538c8a9a5SSteve French 	struct cifs_tcon *tcon = ses->tcon_ipc;
36638c8a9a5SSteve French 	int rc;
36738c8a9a5SSteve French 
36838c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
36938c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
37038c8a9a5SSteve French 	if (cifs_chan_needs_reconnect(ses, server) ||
37138c8a9a5SSteve French 	    ses->ses_status != SES_GOOD) {
37238c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
37338c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
37438c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
37538c8a9a5SSteve French 				__func__);
37638c8a9a5SSteve French 		return;
37738c8a9a5SSteve French 	}
37838c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
37938c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
38038c8a9a5SSteve French 
38138c8a9a5SSteve French 	cifs_server_lock(server);
38238c8a9a5SSteve French 	scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
38338c8a9a5SSteve French 	cifs_server_unlock(server);
38438c8a9a5SSteve French 
38538c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, tree, tcon,
38638c8a9a5SSteve French 				       cifs_sb->local_nls);
38738c8a9a5SSteve French 	cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
38838c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
38938c8a9a5SSteve French 	if (rc) {
39038c8a9a5SSteve French 		tcon->status = TID_NEED_TCON;
39138c8a9a5SSteve French 	} else {
39238c8a9a5SSteve French 		tcon->status = TID_GOOD;
39338c8a9a5SSteve French 		tcon->need_reconnect = false;
39438c8a9a5SSteve French 	}
39538c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
39638c8a9a5SSteve French }
39738c8a9a5SSteve French 
39838c8a9a5SSteve French static void tree_connect_ipc(const unsigned int xid, char *tree,
39938c8a9a5SSteve French 			     struct cifs_sb_info *cifs_sb,
40038c8a9a5SSteve French 			     struct cifs_tcon *tcon)
40138c8a9a5SSteve French {
40238c8a9a5SSteve French 	struct cifs_ses *ses = tcon->ses;
40338c8a9a5SSteve French 
40438c8a9a5SSteve French 	__tree_connect_ipc(xid, tree, cifs_sb, ses);
40538c8a9a5SSteve French 	__tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
40638c8a9a5SSteve French }
40738c8a9a5SSteve French 
40838c8a9a5SSteve French static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
40938c8a9a5SSteve French 				     struct cifs_sb_info *cifs_sb, char *tree, bool islink,
41038c8a9a5SSteve French 				     struct dfs_cache_tgt_list *tl)
41138c8a9a5SSteve French {
41238c8a9a5SSteve French 	int rc;
41338c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
41438c8a9a5SSteve French 	const struct smb_version_operations *ops = server->ops;
41538c8a9a5SSteve French 	struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
41638c8a9a5SSteve French 	char *share = NULL, *prefix = NULL;
41738c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
41838c8a9a5SSteve French 	bool target_match;
41938c8a9a5SSteve French 
42038c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
42138c8a9a5SSteve French 	if (!tit) {
42238c8a9a5SSteve French 		rc = -ENOENT;
42338c8a9a5SSteve French 		goto out;
42438c8a9a5SSteve French 	}
42538c8a9a5SSteve French 
42638c8a9a5SSteve French 	/* Try to tree connect to all dfs targets */
42738c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
42838c8a9a5SSteve French 		const char *target = dfs_cache_get_tgt_name(tit);
429*3fea12f3SPaulo Alcantara 		DFS_CACHE_TGT_LIST(ntl);
43038c8a9a5SSteve French 
43138c8a9a5SSteve French 		kfree(share);
43238c8a9a5SSteve French 		kfree(prefix);
43338c8a9a5SSteve French 		share = prefix = NULL;
43438c8a9a5SSteve French 
43538c8a9a5SSteve French 		/* Check if share matches with tcp ses */
43638c8a9a5SSteve French 		rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
43738c8a9a5SSteve French 		if (rc) {
43838c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
43938c8a9a5SSteve French 			break;
44038c8a9a5SSteve French 		}
44138c8a9a5SSteve French 
44238c8a9a5SSteve French 		rc = target_share_matches_server(server, share, &target_match);
44338c8a9a5SSteve French 		if (rc)
44438c8a9a5SSteve French 			break;
44538c8a9a5SSteve French 		if (!target_match) {
44638c8a9a5SSteve French 			rc = -EHOSTUNREACH;
44738c8a9a5SSteve French 			continue;
44838c8a9a5SSteve French 		}
44938c8a9a5SSteve French 
45038c8a9a5SSteve French 		dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
45138c8a9a5SSteve French 		tree_connect_ipc(xid, tree, cifs_sb, tcon);
45238c8a9a5SSteve French 
45338c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
45438c8a9a5SSteve French 		if (!islink) {
45538c8a9a5SSteve French 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
45638c8a9a5SSteve French 			break;
45738c8a9a5SSteve French 		}
45838c8a9a5SSteve French 
45938c8a9a5SSteve French 		/*
46038c8a9a5SSteve French 		 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
46138c8a9a5SSteve French 		 * to it.  Otherwise, cache the dfs referral and then mark current tcp ses for
46238c8a9a5SSteve French 		 * reconnect so either the demultiplex thread or the echo worker will reconnect to
46338c8a9a5SSteve French 		 * newly resolved target.
46438c8a9a5SSteve French 		 */
46538c8a9a5SSteve French 		if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
46638c8a9a5SSteve French 				   NULL, &ntl)) {
46738c8a9a5SSteve French 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
46838c8a9a5SSteve French 			if (rc)
46938c8a9a5SSteve French 				continue;
47038c8a9a5SSteve French 
47138c8a9a5SSteve French 			rc = cifs_update_super_prepath(cifs_sb, prefix);
47238c8a9a5SSteve French 		} else {
47338c8a9a5SSteve French 			/* Target is another dfs share */
47438c8a9a5SSteve French 			rc = update_server_fullpath(server, cifs_sb, target);
47538c8a9a5SSteve French 			dfs_cache_free_tgts(tl);
47638c8a9a5SSteve French 
47738c8a9a5SSteve French 			if (!rc) {
47838c8a9a5SSteve French 				rc = -EREMOTE;
47938c8a9a5SSteve French 				list_replace_init(&ntl.tl_list, &tl->tl_list);
48038c8a9a5SSteve French 			} else
48138c8a9a5SSteve French 				dfs_cache_free_tgts(&ntl);
48238c8a9a5SSteve French 		}
48338c8a9a5SSteve French 		break;
48438c8a9a5SSteve French 	}
48538c8a9a5SSteve French 
48638c8a9a5SSteve French out:
48738c8a9a5SSteve French 	kfree(share);
48838c8a9a5SSteve French 	kfree(prefix);
48938c8a9a5SSteve French 
49038c8a9a5SSteve French 	return rc;
49138c8a9a5SSteve French }
49238c8a9a5SSteve French 
49338c8a9a5SSteve French static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
49438c8a9a5SSteve French 				   struct cifs_sb_info *cifs_sb, char *tree, bool islink,
49538c8a9a5SSteve French 				   struct dfs_cache_tgt_list *tl)
49638c8a9a5SSteve French {
49738c8a9a5SSteve French 	int rc;
49838c8a9a5SSteve French 	int num_links = 0;
49938c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
50038c8a9a5SSteve French 	char *old_fullpath = server->leaf_fullpath;
50138c8a9a5SSteve French 
50238c8a9a5SSteve French 	do {
50338c8a9a5SSteve French 		rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
50438c8a9a5SSteve French 		if (!rc || rc != -EREMOTE)
50538c8a9a5SSteve French 			break;
50638c8a9a5SSteve French 	} while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
50738c8a9a5SSteve French 	/*
50838c8a9a5SSteve French 	 * If we couldn't tree connect to any targets from last referral path, then
50938c8a9a5SSteve French 	 * retry it from newly resolved dfs referral.
51038c8a9a5SSteve French 	 */
51138c8a9a5SSteve French 	if (rc && server->leaf_fullpath != old_fullpath)
51238c8a9a5SSteve French 		cifs_signal_cifsd_for_reconnect(server, true);
51338c8a9a5SSteve French 
51438c8a9a5SSteve French 	dfs_cache_free_tgts(tl);
51538c8a9a5SSteve French 	return rc;
51638c8a9a5SSteve French }
51738c8a9a5SSteve French 
51838c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
51938c8a9a5SSteve French {
52038c8a9a5SSteve French 	int rc;
52138c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
52238c8a9a5SSteve French 	const struct smb_version_operations *ops = server->ops;
523*3fea12f3SPaulo Alcantara 	DFS_CACHE_TGT_LIST(tl);
5243ae872deSPaulo Alcantara 	struct cifs_sb_info *cifs_sb = NULL;
5253ae872deSPaulo Alcantara 	struct super_block *sb = NULL;
52638c8a9a5SSteve French 	struct dfs_info3_param ref = {0};
5273ae872deSPaulo Alcantara 	char *tree;
52838c8a9a5SSteve French 
52938c8a9a5SSteve French 	/* only send once per connect */
53038c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
53191f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
53291f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
53391f4480cSShyam Prasad N 		return 0;
53491f4480cSShyam Prasad N 	}
53591f4480cSShyam Prasad N 
53638c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
53738c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
53838c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
53938c8a9a5SSteve French 		return -EHOSTDOWN;
54038c8a9a5SSteve French 	}
54138c8a9a5SSteve French 
54238c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
54338c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
54438c8a9a5SSteve French 
54538c8a9a5SSteve French 	tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
54638c8a9a5SSteve French 	if (!tree) {
54738c8a9a5SSteve French 		rc = -ENOMEM;
54838c8a9a5SSteve French 		goto out;
54938c8a9a5SSteve French 	}
55038c8a9a5SSteve French 
55138c8a9a5SSteve French 	if (tcon->ipc) {
55238c8a9a5SSteve French 		cifs_server_lock(server);
55338c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
55438c8a9a5SSteve French 		cifs_server_unlock(server);
55538c8a9a5SSteve French 		rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
55638c8a9a5SSteve French 		goto out;
55738c8a9a5SSteve French 	}
55838c8a9a5SSteve French 
5593ae872deSPaulo Alcantara 	sb = cifs_get_dfs_tcon_super(tcon);
5603ae872deSPaulo Alcantara 	if (!IS_ERR(sb))
56138c8a9a5SSteve French 		cifs_sb = CIFS_SB(sb);
56238c8a9a5SSteve French 
5633ae872deSPaulo Alcantara 	/*
5643ae872deSPaulo Alcantara 	 * Tree connect to last share in @tcon->tree_name whether dfs super or
5653ae872deSPaulo Alcantara 	 * cached dfs referral was not found.
5663ae872deSPaulo Alcantara 	 */
5673ae872deSPaulo Alcantara 	if (!cifs_sb || !server->leaf_fullpath ||
56838c8a9a5SSteve French 	    dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
5693ae872deSPaulo Alcantara 		rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon,
5703ae872deSPaulo Alcantara 				       cifs_sb ? cifs_sb->local_nls : nlsc);
57138c8a9a5SSteve French 		goto out;
57238c8a9a5SSteve French 	}
57338c8a9a5SSteve French 
57438c8a9a5SSteve French 	rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
57538c8a9a5SSteve French 				     &tl);
57638c8a9a5SSteve French 	free_dfs_info_param(&ref);
57738c8a9a5SSteve French 
57838c8a9a5SSteve French out:
57938c8a9a5SSteve French 	kfree(tree);
58038c8a9a5SSteve French 	cifs_put_tcp_super(sb);
58138c8a9a5SSteve French 
58238c8a9a5SSteve French 	if (rc) {
58338c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
58438c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
58538c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
58638c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
58738c8a9a5SSteve French 	} else {
58838c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
58938c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
59038c8a9a5SSteve French 			tcon->status = TID_GOOD;
59138c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
59238c8a9a5SSteve French 		tcon->need_reconnect = false;
59338c8a9a5SSteve French 	}
59438c8a9a5SSteve French 
59538c8a9a5SSteve French 	return rc;
59638c8a9a5SSteve French }
597