xref: /openbmc/linux/fs/smb/client/dfs.h (revision 6aeadf78)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
4  */
5 
6 #ifndef _CIFS_DFS_H
7 #define _CIFS_DFS_H
8 
9 #include "cifsglob.h"
10 #include "fs_context.h"
11 #include "cifs_unicode.h"
12 
13 struct dfs_root_ses {
14 	struct list_head list;
15 	struct cifs_ses *ses;
16 };
17 
18 int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
19 			      struct smb3_fs_context *ctx);
20 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs);
21 
22 static inline char *dfs_get_path(struct cifs_sb_info *cifs_sb, const char *path)
23 {
24 	return dfs_cache_canonical_path(path, cifs_sb->local_nls, cifs_remap(cifs_sb));
25 }
26 
27 static inline int dfs_get_referral(struct cifs_mount_ctx *mnt_ctx, const char *path,
28 				   struct dfs_info3_param *ref, struct dfs_cache_tgt_list *tl)
29 {
30 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
31 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
32 
33 	return dfs_cache_find(mnt_ctx->xid, ctx->dfs_root_ses, cifs_sb->local_nls,
34 			      cifs_remap(cifs_sb), path, ref, tl);
35 }
36 
37 /* Return DFS full path out of a dentry set for automount */
38 static inline char *dfs_get_automount_devname(struct dentry *dentry, void *page)
39 {
40 	struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
41 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
42 	struct TCP_Server_Info *server = tcon->ses->server;
43 	size_t len;
44 	char *s;
45 
46 	spin_lock(&server->srv_lock);
47 	if (unlikely(!server->origin_fullpath)) {
48 		spin_unlock(&server->srv_lock);
49 		return ERR_PTR(-EREMOTE);
50 	}
51 	spin_unlock(&server->srv_lock);
52 
53 	s = dentry_path_raw(dentry, page, PATH_MAX);
54 	if (IS_ERR(s))
55 		return s;
56 	/* for root, we want "" */
57 	if (!s[1])
58 		s++;
59 
60 	spin_lock(&server->srv_lock);
61 	len = strlen(server->origin_fullpath);
62 	if (s < (char *)page + len) {
63 		spin_unlock(&server->srv_lock);
64 		return ERR_PTR(-ENAMETOOLONG);
65 	}
66 
67 	s -= len;
68 	memcpy(s, server->origin_fullpath, len);
69 	spin_unlock(&server->srv_lock);
70 	convert_delimiter(s, '/');
71 
72 	return s;
73 }
74 
75 static inline void dfs_put_root_smb_sessions(struct list_head *head)
76 {
77 	struct dfs_root_ses *root, *tmp;
78 
79 	list_for_each_entry_safe(root, tmp, head, list) {
80 		list_del_init(&root->list);
81 		cifs_put_smb_ses(root->ses);
82 		kfree(root);
83 	}
84 }
85 
86 #endif /* _CIFS_DFS_H */
87