xref: /openbmc/linux/fs/smb/client/dfs.c (revision 91f4480c41f56f7c723323cf7f581f1d95d9ffbc)
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 /*
5838c8a9a5SSteve French  * cifs_build_path_to_root returns full path to root when we do not have an
5938c8a9a5SSteve French  * existing connection (tcon)
6038c8a9a5SSteve French  */
6138c8a9a5SSteve French static char *build_unc_path_to_root(const struct smb3_fs_context *ctx,
6238c8a9a5SSteve French 				    const struct cifs_sb_info *cifs_sb, bool useppath)
6338c8a9a5SSteve French {
6438c8a9a5SSteve French 	char *full_path, *pos;
6538c8a9a5SSteve French 	unsigned int pplen = useppath && ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
6638c8a9a5SSteve French 	unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
6738c8a9a5SSteve French 
6838c8a9a5SSteve French 	if (unc_len > MAX_TREE_SIZE)
6938c8a9a5SSteve French 		return ERR_PTR(-EINVAL);
7038c8a9a5SSteve French 
7138c8a9a5SSteve French 	full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
7238c8a9a5SSteve French 	if (full_path == NULL)
7338c8a9a5SSteve French 		return ERR_PTR(-ENOMEM);
7438c8a9a5SSteve French 
7538c8a9a5SSteve French 	memcpy(full_path, ctx->UNC, unc_len);
7638c8a9a5SSteve French 	pos = full_path + unc_len;
7738c8a9a5SSteve French 
7838c8a9a5SSteve French 	if (pplen) {
7938c8a9a5SSteve French 		*pos = CIFS_DIR_SEP(cifs_sb);
8038c8a9a5SSteve French 		memcpy(pos + 1, ctx->prepath, pplen);
8138c8a9a5SSteve French 		pos += pplen;
8238c8a9a5SSteve French 	}
8338c8a9a5SSteve French 
8438c8a9a5SSteve French 	*pos = '\0'; /* add trailing null */
8538c8a9a5SSteve French 	convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
8638c8a9a5SSteve French 	cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
8738c8a9a5SSteve French 	return full_path;
8838c8a9a5SSteve French }
8938c8a9a5SSteve French 
9038c8a9a5SSteve French static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
9138c8a9a5SSteve French {
9238c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
9338c8a9a5SSteve French 	int rc;
9438c8a9a5SSteve French 
9538c8a9a5SSteve French 	ctx->leaf_fullpath = (char *)full_path;
9638c8a9a5SSteve French 	rc = cifs_mount_get_session(mnt_ctx);
9738c8a9a5SSteve French 	ctx->leaf_fullpath = NULL;
9838c8a9a5SSteve French 
9938c8a9a5SSteve French 	return rc;
10038c8a9a5SSteve French }
10138c8a9a5SSteve French 
10238c8a9a5SSteve French static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
10338c8a9a5SSteve French {
10438c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
10538c8a9a5SSteve French 	struct dfs_root_ses *root_ses;
10638c8a9a5SSteve French 	struct cifs_ses *ses = mnt_ctx->ses;
10738c8a9a5SSteve French 
10838c8a9a5SSteve French 	if (ses) {
10938c8a9a5SSteve French 		root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
11038c8a9a5SSteve French 		if (!root_ses)
11138c8a9a5SSteve French 			return -ENOMEM;
11238c8a9a5SSteve French 
11338c8a9a5SSteve French 		INIT_LIST_HEAD(&root_ses->list);
11438c8a9a5SSteve French 
11538c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
11638c8a9a5SSteve French 		ses->ses_count++;
11738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
11838c8a9a5SSteve French 		root_ses->ses = ses;
11938c8a9a5SSteve French 		list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
12038c8a9a5SSteve French 	}
12138c8a9a5SSteve French 	ctx->dfs_root_ses = ses;
12238c8a9a5SSteve French 	return 0;
12338c8a9a5SSteve French }
12438c8a9a5SSteve French 
12538c8a9a5SSteve French static int get_dfs_conn(struct cifs_mount_ctx *mnt_ctx, const char *ref_path, const char *full_path,
12638c8a9a5SSteve French 			const struct dfs_cache_tgt_iterator *tit)
12738c8a9a5SSteve French {
12838c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
12938c8a9a5SSteve French 	struct dfs_info3_param ref = {};
13038c8a9a5SSteve French 	bool is_refsrv;
13138c8a9a5SSteve French 	int rc, rc2;
13238c8a9a5SSteve French 
13338c8a9a5SSteve French 	rc = dfs_cache_get_tgt_referral(ref_path + 1, tit, &ref);
13438c8a9a5SSteve French 	if (rc)
13538c8a9a5SSteve French 		return rc;
13638c8a9a5SSteve French 
13738c8a9a5SSteve French 	rc = dfs_parse_target_referral(full_path + 1, &ref, ctx);
13838c8a9a5SSteve French 	if (rc)
13938c8a9a5SSteve French 		goto out;
14038c8a9a5SSteve French 
14138c8a9a5SSteve French 	cifs_mount_put_conns(mnt_ctx);
14238c8a9a5SSteve French 	rc = get_session(mnt_ctx, ref_path);
14338c8a9a5SSteve French 	if (rc)
14438c8a9a5SSteve French 		goto out;
14538c8a9a5SSteve French 
14638c8a9a5SSteve French 	is_refsrv = !!(ref.flags & DFSREF_REFERRAL_SERVER);
14738c8a9a5SSteve French 
14838c8a9a5SSteve French 	rc = -EREMOTE;
14938c8a9a5SSteve French 	if (ref.flags & DFSREF_STORAGE_SERVER) {
15038c8a9a5SSteve French 		rc = cifs_mount_get_tcon(mnt_ctx);
15138c8a9a5SSteve French 		if (rc)
15238c8a9a5SSteve French 			goto out;
15338c8a9a5SSteve French 
15438c8a9a5SSteve French 		/* some servers may not advertise referral capability under ref.flags */
15538c8a9a5SSteve French 		is_refsrv |= is_tcon_dfs(mnt_ctx->tcon);
15638c8a9a5SSteve French 
15738c8a9a5SSteve French 		rc = cifs_is_path_remote(mnt_ctx);
15838c8a9a5SSteve French 	}
15938c8a9a5SSteve French 
16038c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(ref_path + 1, tit);
16138c8a9a5SSteve French 
16238c8a9a5SSteve French 	if (rc == -EREMOTE && is_refsrv) {
16338c8a9a5SSteve French 		rc2 = add_root_smb_session(mnt_ctx);
16438c8a9a5SSteve French 		if (rc2)
16538c8a9a5SSteve French 			rc = rc2;
16638c8a9a5SSteve French 	}
16738c8a9a5SSteve French 
16838c8a9a5SSteve French out:
16938c8a9a5SSteve French 	free_dfs_info_param(&ref);
17038c8a9a5SSteve French 	return rc;
17138c8a9a5SSteve French }
17238c8a9a5SSteve French 
17338c8a9a5SSteve French static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
17438c8a9a5SSteve French {
17538c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
17638c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
17738c8a9a5SSteve French 	char *ref_path = NULL, *full_path = NULL;
17838c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
17938c8a9a5SSteve French 	struct TCP_Server_Info *server;
18038c8a9a5SSteve French 	struct cifs_tcon *tcon;
18138c8a9a5SSteve French 	char *origin_fullpath = NULL;
18238c8a9a5SSteve French 	int num_links = 0;
18338c8a9a5SSteve French 	int rc;
18438c8a9a5SSteve French 
18538c8a9a5SSteve French 	ref_path = dfs_get_path(cifs_sb, ctx->UNC);
18638c8a9a5SSteve French 	if (IS_ERR(ref_path))
18738c8a9a5SSteve French 		return PTR_ERR(ref_path);
18838c8a9a5SSteve French 
18938c8a9a5SSteve French 	full_path = build_unc_path_to_root(ctx, cifs_sb, true);
19038c8a9a5SSteve French 	if (IS_ERR(full_path)) {
19138c8a9a5SSteve French 		rc = PTR_ERR(full_path);
19238c8a9a5SSteve French 		full_path = NULL;
19338c8a9a5SSteve French 		goto out;
19438c8a9a5SSteve French 	}
19538c8a9a5SSteve French 
19638c8a9a5SSteve French 	origin_fullpath = kstrdup(full_path, GFP_KERNEL);
19738c8a9a5SSteve French 	if (!origin_fullpath) {
19838c8a9a5SSteve French 		rc = -ENOMEM;
19938c8a9a5SSteve French 		goto out;
20038c8a9a5SSteve French 	}
20138c8a9a5SSteve French 
20238c8a9a5SSteve French 	do {
20338c8a9a5SSteve French 		struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
20438c8a9a5SSteve French 
20538c8a9a5SSteve French 		rc = dfs_get_referral(mnt_ctx, ref_path + 1, NULL, &tl);
20638c8a9a5SSteve French 		if (rc)
20738c8a9a5SSteve French 			break;
20838c8a9a5SSteve French 
20938c8a9a5SSteve French 		tit = dfs_cache_get_tgt_iterator(&tl);
21038c8a9a5SSteve French 		if (!tit) {
21138c8a9a5SSteve French 			cifs_dbg(VFS, "%s: dfs referral (%s) with no targets\n", __func__,
21238c8a9a5SSteve French 				 ref_path + 1);
21338c8a9a5SSteve French 			rc = -ENOENT;
21438c8a9a5SSteve French 			dfs_cache_free_tgts(&tl);
21538c8a9a5SSteve French 			break;
21638c8a9a5SSteve French 		}
21738c8a9a5SSteve French 
21838c8a9a5SSteve French 		do {
21938c8a9a5SSteve French 			rc = get_dfs_conn(mnt_ctx, ref_path, full_path, tit);
22038c8a9a5SSteve French 			if (!rc)
22138c8a9a5SSteve French 				break;
22238c8a9a5SSteve French 			if (rc == -EREMOTE) {
22338c8a9a5SSteve French 				if (++num_links > MAX_NESTED_LINKS) {
22438c8a9a5SSteve French 					rc = -ELOOP;
22538c8a9a5SSteve French 					break;
22638c8a9a5SSteve French 				}
22738c8a9a5SSteve French 				kfree(ref_path);
22838c8a9a5SSteve French 				kfree(full_path);
22938c8a9a5SSteve French 				ref_path = full_path = NULL;
23038c8a9a5SSteve French 
23138c8a9a5SSteve French 				full_path = build_unc_path_to_root(ctx, cifs_sb, true);
23238c8a9a5SSteve French 				if (IS_ERR(full_path)) {
23338c8a9a5SSteve French 					rc = PTR_ERR(full_path);
23438c8a9a5SSteve French 					full_path = NULL;
23538c8a9a5SSteve French 				} else {
23638c8a9a5SSteve French 					ref_path = dfs_get_path(cifs_sb, full_path);
23738c8a9a5SSteve French 					if (IS_ERR(ref_path)) {
23838c8a9a5SSteve French 						rc = PTR_ERR(ref_path);
23938c8a9a5SSteve French 						ref_path = NULL;
24038c8a9a5SSteve French 					}
24138c8a9a5SSteve French 				}
24238c8a9a5SSteve French 				break;
24338c8a9a5SSteve French 			}
24438c8a9a5SSteve French 		} while ((tit = dfs_cache_get_next_tgt(&tl, tit)));
24538c8a9a5SSteve French 		dfs_cache_free_tgts(&tl);
24638c8a9a5SSteve French 	} while (rc == -EREMOTE);
24738c8a9a5SSteve French 
24838c8a9a5SSteve French 	if (!rc) {
24938c8a9a5SSteve French 		server = mnt_ctx->server;
25038c8a9a5SSteve French 		tcon = mnt_ctx->tcon;
25138c8a9a5SSteve French 
25238c8a9a5SSteve French 		mutex_lock(&server->refpath_lock);
25338c8a9a5SSteve French 		spin_lock(&server->srv_lock);
25438c8a9a5SSteve French 		if (!server->origin_fullpath) {
25538c8a9a5SSteve French 			server->origin_fullpath = origin_fullpath;
25638c8a9a5SSteve French 			origin_fullpath = NULL;
25738c8a9a5SSteve French 		}
25838c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
25938c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
26038c8a9a5SSteve French 
26138c8a9a5SSteve French 		if (list_empty(&tcon->dfs_ses_list)) {
26238c8a9a5SSteve French 			list_replace_init(&mnt_ctx->dfs_ses_list,
26338c8a9a5SSteve French 					  &tcon->dfs_ses_list);
26438c8a9a5SSteve French 			queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
26538c8a9a5SSteve French 					   dfs_cache_get_ttl() * HZ);
26638c8a9a5SSteve French 		} else {
26738c8a9a5SSteve French 			dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
26838c8a9a5SSteve French 		}
26938c8a9a5SSteve French 	}
27038c8a9a5SSteve French 
27138c8a9a5SSteve French out:
27238c8a9a5SSteve French 	kfree(origin_fullpath);
27338c8a9a5SSteve French 	kfree(ref_path);
27438c8a9a5SSteve French 	kfree(full_path);
27538c8a9a5SSteve French 	return rc;
27638c8a9a5SSteve French }
27738c8a9a5SSteve French 
27838c8a9a5SSteve French int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
27938c8a9a5SSteve French {
28038c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
28138c8a9a5SSteve French 	struct cifs_ses *ses;
28238c8a9a5SSteve French 	char *source = ctx->source;
28338c8a9a5SSteve French 	bool nodfs = ctx->nodfs;
28438c8a9a5SSteve French 	int rc;
28538c8a9a5SSteve French 
28638c8a9a5SSteve French 	*isdfs = false;
28738c8a9a5SSteve French 	/* Temporarily set @ctx->source to NULL as we're not matching DFS
28838c8a9a5SSteve French 	 * superblocks yet.  See cifs_match_super() and match_server().
28938c8a9a5SSteve French 	 */
29038c8a9a5SSteve French 	ctx->source = NULL;
29138c8a9a5SSteve French 	rc = get_session(mnt_ctx, NULL);
29238c8a9a5SSteve French 	if (rc)
29338c8a9a5SSteve French 		goto out;
29438c8a9a5SSteve French 
29538c8a9a5SSteve French 	ctx->dfs_root_ses = mnt_ctx->ses;
29638c8a9a5SSteve French 	/*
29738c8a9a5SSteve French 	 * If called with 'nodfs' mount option, then skip DFS resolving.  Otherwise unconditionally
29838c8a9a5SSteve French 	 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
29938c8a9a5SSteve French 	 *
30038c8a9a5SSteve French 	 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
30138c8a9a5SSteve French 	 * to respond with PATH_NOT_COVERED to requests that include the prefix.
30238c8a9a5SSteve French 	 */
30338c8a9a5SSteve French 	if (!nodfs) {
30438c8a9a5SSteve French 		rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
30538c8a9a5SSteve French 		if (rc) {
30638c8a9a5SSteve French 			if (rc != -ENOENT && rc != -EOPNOTSUPP && rc != -EIO)
30738c8a9a5SSteve French 				goto out;
30838c8a9a5SSteve French 			nodfs = true;
30938c8a9a5SSteve French 		}
31038c8a9a5SSteve French 	}
31138c8a9a5SSteve French 	if (nodfs) {
31238c8a9a5SSteve French 		rc = cifs_mount_get_tcon(mnt_ctx);
31338c8a9a5SSteve French 		if (!rc)
31438c8a9a5SSteve French 			rc = cifs_is_path_remote(mnt_ctx);
31538c8a9a5SSteve French 		goto out;
31638c8a9a5SSteve French 	}
31738c8a9a5SSteve French 
31838c8a9a5SSteve French 	*isdfs = true;
31938c8a9a5SSteve French 	/*
32038c8a9a5SSteve French 	 * Prevent DFS root session of being put in the first call to
32138c8a9a5SSteve French 	 * cifs_mount_put_conns().  If another DFS root server was not found
32238c8a9a5SSteve French 	 * while chasing the referrals (@ctx->dfs_root_ses == @ses), then we
32338c8a9a5SSteve French 	 * can safely put extra refcount of @ses.
32438c8a9a5SSteve French 	 */
32538c8a9a5SSteve French 	ses = mnt_ctx->ses;
32638c8a9a5SSteve French 	mnt_ctx->ses = NULL;
32738c8a9a5SSteve French 	mnt_ctx->server = NULL;
32838c8a9a5SSteve French 	rc = __dfs_mount_share(mnt_ctx);
32938c8a9a5SSteve French 	if (ses == ctx->dfs_root_ses)
33038c8a9a5SSteve French 		cifs_put_smb_ses(ses);
33138c8a9a5SSteve French out:
33238c8a9a5SSteve French 	/*
33338c8a9a5SSteve French 	 * Restore previous value of @ctx->source so DFS superblock can be
33438c8a9a5SSteve French 	 * matched in cifs_match_super().
33538c8a9a5SSteve French 	 */
33638c8a9a5SSteve French 	ctx->source = source;
33738c8a9a5SSteve French 	return rc;
33838c8a9a5SSteve French }
33938c8a9a5SSteve French 
34038c8a9a5SSteve French /* Update dfs referral path of superblock */
34138c8a9a5SSteve French static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
34238c8a9a5SSteve French 				  const char *target)
34338c8a9a5SSteve French {
34438c8a9a5SSteve French 	int rc = 0;
34538c8a9a5SSteve French 	size_t len = strlen(target);
34638c8a9a5SSteve French 	char *refpath, *npath;
34738c8a9a5SSteve French 
34838c8a9a5SSteve French 	if (unlikely(len < 2 || *target != '\\'))
34938c8a9a5SSteve French 		return -EINVAL;
35038c8a9a5SSteve French 
35138c8a9a5SSteve French 	if (target[1] == '\\') {
35238c8a9a5SSteve French 		len += 1;
35338c8a9a5SSteve French 		refpath = kmalloc(len, GFP_KERNEL);
35438c8a9a5SSteve French 		if (!refpath)
35538c8a9a5SSteve French 			return -ENOMEM;
35638c8a9a5SSteve French 
35738c8a9a5SSteve French 		scnprintf(refpath, len, "%s", target);
35838c8a9a5SSteve French 	} else {
35938c8a9a5SSteve French 		len += sizeof("\\");
36038c8a9a5SSteve French 		refpath = kmalloc(len, GFP_KERNEL);
36138c8a9a5SSteve French 		if (!refpath)
36238c8a9a5SSteve French 			return -ENOMEM;
36338c8a9a5SSteve French 
36438c8a9a5SSteve French 		scnprintf(refpath, len, "\\%s", target);
36538c8a9a5SSteve French 	}
36638c8a9a5SSteve French 
36738c8a9a5SSteve French 	npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
36838c8a9a5SSteve French 	kfree(refpath);
36938c8a9a5SSteve French 
37038c8a9a5SSteve French 	if (IS_ERR(npath)) {
37138c8a9a5SSteve French 		rc = PTR_ERR(npath);
37238c8a9a5SSteve French 	} else {
37338c8a9a5SSteve French 		mutex_lock(&server->refpath_lock);
37438c8a9a5SSteve French 		spin_lock(&server->srv_lock);
37538c8a9a5SSteve French 		kfree(server->leaf_fullpath);
37638c8a9a5SSteve French 		server->leaf_fullpath = npath;
37738c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
37838c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
37938c8a9a5SSteve French 	}
38038c8a9a5SSteve French 	return rc;
38138c8a9a5SSteve French }
38238c8a9a5SSteve French 
38338c8a9a5SSteve French static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
38438c8a9a5SSteve French 				       bool *target_match)
38538c8a9a5SSteve French {
38638c8a9a5SSteve French 	int rc = 0;
38738c8a9a5SSteve French 	const char *dfs_host;
38838c8a9a5SSteve French 	size_t dfs_host_len;
38938c8a9a5SSteve French 
39038c8a9a5SSteve French 	*target_match = true;
39138c8a9a5SSteve French 	extract_unc_hostname(share, &dfs_host, &dfs_host_len);
39238c8a9a5SSteve French 
39338c8a9a5SSteve French 	/* Check if hostnames or addresses match */
39438c8a9a5SSteve French 	cifs_server_lock(server);
39538c8a9a5SSteve French 	if (dfs_host_len != strlen(server->hostname) ||
39638c8a9a5SSteve French 	    strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
39738c8a9a5SSteve French 		cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
39838c8a9a5SSteve French 			 (int)dfs_host_len, dfs_host, server->hostname);
39938c8a9a5SSteve French 		rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
40038c8a9a5SSteve French 		if (rc)
40138c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
40238c8a9a5SSteve French 	}
40338c8a9a5SSteve French 	cifs_server_unlock(server);
40438c8a9a5SSteve French 	return rc;
40538c8a9a5SSteve French }
40638c8a9a5SSteve French 
40738c8a9a5SSteve French static void __tree_connect_ipc(const unsigned int xid, char *tree,
40838c8a9a5SSteve French 			       struct cifs_sb_info *cifs_sb,
40938c8a9a5SSteve French 			       struct cifs_ses *ses)
41038c8a9a5SSteve French {
41138c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
41238c8a9a5SSteve French 	struct cifs_tcon *tcon = ses->tcon_ipc;
41338c8a9a5SSteve French 	int rc;
41438c8a9a5SSteve French 
41538c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
41638c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
41738c8a9a5SSteve French 	if (cifs_chan_needs_reconnect(ses, server) ||
41838c8a9a5SSteve French 	    ses->ses_status != SES_GOOD) {
41938c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
42038c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
42138c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
42238c8a9a5SSteve French 				__func__);
42338c8a9a5SSteve French 		return;
42438c8a9a5SSteve French 	}
42538c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
42638c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
42738c8a9a5SSteve French 
42838c8a9a5SSteve French 	cifs_server_lock(server);
42938c8a9a5SSteve French 	scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
43038c8a9a5SSteve French 	cifs_server_unlock(server);
43138c8a9a5SSteve French 
43238c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, tree, tcon,
43338c8a9a5SSteve French 				       cifs_sb->local_nls);
43438c8a9a5SSteve French 	cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
43538c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
43638c8a9a5SSteve French 	if (rc) {
43738c8a9a5SSteve French 		tcon->status = TID_NEED_TCON;
43838c8a9a5SSteve French 	} else {
43938c8a9a5SSteve French 		tcon->status = TID_GOOD;
44038c8a9a5SSteve French 		tcon->need_reconnect = false;
44138c8a9a5SSteve French 	}
44238c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
44338c8a9a5SSteve French }
44438c8a9a5SSteve French 
44538c8a9a5SSteve French static void tree_connect_ipc(const unsigned int xid, char *tree,
44638c8a9a5SSteve French 			     struct cifs_sb_info *cifs_sb,
44738c8a9a5SSteve French 			     struct cifs_tcon *tcon)
44838c8a9a5SSteve French {
44938c8a9a5SSteve French 	struct cifs_ses *ses = tcon->ses;
45038c8a9a5SSteve French 
45138c8a9a5SSteve French 	__tree_connect_ipc(xid, tree, cifs_sb, ses);
45238c8a9a5SSteve French 	__tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
45338c8a9a5SSteve French }
45438c8a9a5SSteve French 
45538c8a9a5SSteve French static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
45638c8a9a5SSteve French 				     struct cifs_sb_info *cifs_sb, char *tree, bool islink,
45738c8a9a5SSteve French 				     struct dfs_cache_tgt_list *tl)
45838c8a9a5SSteve French {
45938c8a9a5SSteve French 	int rc;
46038c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
46138c8a9a5SSteve French 	const struct smb_version_operations *ops = server->ops;
46238c8a9a5SSteve French 	struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
46338c8a9a5SSteve French 	char *share = NULL, *prefix = NULL;
46438c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
46538c8a9a5SSteve French 	bool target_match;
46638c8a9a5SSteve French 
46738c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
46838c8a9a5SSteve French 	if (!tit) {
46938c8a9a5SSteve French 		rc = -ENOENT;
47038c8a9a5SSteve French 		goto out;
47138c8a9a5SSteve French 	}
47238c8a9a5SSteve French 
47338c8a9a5SSteve French 	/* Try to tree connect to all dfs targets */
47438c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
47538c8a9a5SSteve French 		const char *target = dfs_cache_get_tgt_name(tit);
47638c8a9a5SSteve French 		struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
47738c8a9a5SSteve French 
47838c8a9a5SSteve French 		kfree(share);
47938c8a9a5SSteve French 		kfree(prefix);
48038c8a9a5SSteve French 		share = prefix = NULL;
48138c8a9a5SSteve French 
48238c8a9a5SSteve French 		/* Check if share matches with tcp ses */
48338c8a9a5SSteve French 		rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
48438c8a9a5SSteve French 		if (rc) {
48538c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
48638c8a9a5SSteve French 			break;
48738c8a9a5SSteve French 		}
48838c8a9a5SSteve French 
48938c8a9a5SSteve French 		rc = target_share_matches_server(server, share, &target_match);
49038c8a9a5SSteve French 		if (rc)
49138c8a9a5SSteve French 			break;
49238c8a9a5SSteve French 		if (!target_match) {
49338c8a9a5SSteve French 			rc = -EHOSTUNREACH;
49438c8a9a5SSteve French 			continue;
49538c8a9a5SSteve French 		}
49638c8a9a5SSteve French 
49738c8a9a5SSteve French 		dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
49838c8a9a5SSteve French 		tree_connect_ipc(xid, tree, cifs_sb, tcon);
49938c8a9a5SSteve French 
50038c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
50138c8a9a5SSteve French 		if (!islink) {
50238c8a9a5SSteve French 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
50338c8a9a5SSteve French 			break;
50438c8a9a5SSteve French 		}
50538c8a9a5SSteve French 
50638c8a9a5SSteve French 		/*
50738c8a9a5SSteve French 		 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
50838c8a9a5SSteve French 		 * to it.  Otherwise, cache the dfs referral and then mark current tcp ses for
50938c8a9a5SSteve French 		 * reconnect so either the demultiplex thread or the echo worker will reconnect to
51038c8a9a5SSteve French 		 * newly resolved target.
51138c8a9a5SSteve French 		 */
51238c8a9a5SSteve French 		if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
51338c8a9a5SSteve French 				   NULL, &ntl)) {
51438c8a9a5SSteve French 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
51538c8a9a5SSteve French 			if (rc)
51638c8a9a5SSteve French 				continue;
51738c8a9a5SSteve French 
51838c8a9a5SSteve French 			rc = cifs_update_super_prepath(cifs_sb, prefix);
51938c8a9a5SSteve French 		} else {
52038c8a9a5SSteve French 			/* Target is another dfs share */
52138c8a9a5SSteve French 			rc = update_server_fullpath(server, cifs_sb, target);
52238c8a9a5SSteve French 			dfs_cache_free_tgts(tl);
52338c8a9a5SSteve French 
52438c8a9a5SSteve French 			if (!rc) {
52538c8a9a5SSteve French 				rc = -EREMOTE;
52638c8a9a5SSteve French 				list_replace_init(&ntl.tl_list, &tl->tl_list);
52738c8a9a5SSteve French 			} else
52838c8a9a5SSteve French 				dfs_cache_free_tgts(&ntl);
52938c8a9a5SSteve French 		}
53038c8a9a5SSteve French 		break;
53138c8a9a5SSteve French 	}
53238c8a9a5SSteve French 
53338c8a9a5SSteve French out:
53438c8a9a5SSteve French 	kfree(share);
53538c8a9a5SSteve French 	kfree(prefix);
53638c8a9a5SSteve French 
53738c8a9a5SSteve French 	return rc;
53838c8a9a5SSteve French }
53938c8a9a5SSteve French 
54038c8a9a5SSteve French static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
54138c8a9a5SSteve French 				   struct cifs_sb_info *cifs_sb, char *tree, bool islink,
54238c8a9a5SSteve French 				   struct dfs_cache_tgt_list *tl)
54338c8a9a5SSteve French {
54438c8a9a5SSteve French 	int rc;
54538c8a9a5SSteve French 	int num_links = 0;
54638c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
54738c8a9a5SSteve French 	char *old_fullpath = server->leaf_fullpath;
54838c8a9a5SSteve French 
54938c8a9a5SSteve French 	do {
55038c8a9a5SSteve French 		rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
55138c8a9a5SSteve French 		if (!rc || rc != -EREMOTE)
55238c8a9a5SSteve French 			break;
55338c8a9a5SSteve French 	} while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
55438c8a9a5SSteve French 	/*
55538c8a9a5SSteve French 	 * If we couldn't tree connect to any targets from last referral path, then
55638c8a9a5SSteve French 	 * retry it from newly resolved dfs referral.
55738c8a9a5SSteve French 	 */
55838c8a9a5SSteve French 	if (rc && server->leaf_fullpath != old_fullpath)
55938c8a9a5SSteve French 		cifs_signal_cifsd_for_reconnect(server, true);
56038c8a9a5SSteve French 
56138c8a9a5SSteve French 	dfs_cache_free_tgts(tl);
56238c8a9a5SSteve French 	return rc;
56338c8a9a5SSteve French }
56438c8a9a5SSteve French 
56538c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
56638c8a9a5SSteve French {
56738c8a9a5SSteve French 	int rc;
56838c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
56938c8a9a5SSteve French 	const struct smb_version_operations *ops = server->ops;
57038c8a9a5SSteve French 	struct super_block *sb = NULL;
57138c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
57238c8a9a5SSteve French 	struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
57338c8a9a5SSteve French 	char *tree;
57438c8a9a5SSteve French 	struct dfs_info3_param ref = {0};
57538c8a9a5SSteve French 
57638c8a9a5SSteve French 	/* only send once per connect */
57738c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
578*91f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
579*91f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
580*91f4480cSShyam Prasad N 		return 0;
581*91f4480cSShyam Prasad N 	}
582*91f4480cSShyam Prasad N 
58338c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
58438c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
58538c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
58638c8a9a5SSteve French 		return -EHOSTDOWN;
58738c8a9a5SSteve French 	}
58838c8a9a5SSteve French 
58938c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
59038c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
59138c8a9a5SSteve French 
59238c8a9a5SSteve French 	tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
59338c8a9a5SSteve French 	if (!tree) {
59438c8a9a5SSteve French 		rc = -ENOMEM;
59538c8a9a5SSteve French 		goto out;
59638c8a9a5SSteve French 	}
59738c8a9a5SSteve French 
59838c8a9a5SSteve French 	if (tcon->ipc) {
59938c8a9a5SSteve French 		cifs_server_lock(server);
60038c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
60138c8a9a5SSteve French 		cifs_server_unlock(server);
60238c8a9a5SSteve French 		rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
60338c8a9a5SSteve French 		goto out;
60438c8a9a5SSteve French 	}
60538c8a9a5SSteve French 
60638c8a9a5SSteve French 	sb = cifs_get_tcp_super(server);
60738c8a9a5SSteve French 	if (IS_ERR(sb)) {
60838c8a9a5SSteve French 		rc = PTR_ERR(sb);
60938c8a9a5SSteve French 		cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
61038c8a9a5SSteve French 		goto out;
61138c8a9a5SSteve French 	}
61238c8a9a5SSteve French 
61338c8a9a5SSteve French 	cifs_sb = CIFS_SB(sb);
61438c8a9a5SSteve French 
61538c8a9a5SSteve French 	/* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
61638c8a9a5SSteve French 	if (!server->leaf_fullpath ||
61738c8a9a5SSteve French 	    dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
61838c8a9a5SSteve French 		rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, cifs_sb->local_nls);
61938c8a9a5SSteve French 		goto out;
62038c8a9a5SSteve French 	}
62138c8a9a5SSteve French 
62238c8a9a5SSteve French 	rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
62338c8a9a5SSteve French 				     &tl);
62438c8a9a5SSteve French 	free_dfs_info_param(&ref);
62538c8a9a5SSteve French 
62638c8a9a5SSteve French out:
62738c8a9a5SSteve French 	kfree(tree);
62838c8a9a5SSteve French 	cifs_put_tcp_super(sb);
62938c8a9a5SSteve French 
63038c8a9a5SSteve French 	if (rc) {
63138c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
63238c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
63338c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
63438c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
63538c8a9a5SSteve French 	} else {
63638c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
63738c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
63838c8a9a5SSteve French 			tcon->status = TID_GOOD;
63938c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
64038c8a9a5SSteve French 		tcon->need_reconnect = false;
64138c8a9a5SSteve French 	}
64238c8a9a5SSteve French 
64338c8a9a5SSteve French 	return rc;
64438c8a9a5SSteve French }
645