xref: /openbmc/linux/fs/smb/client/dfs.h (revision 11930010)
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 	size_t len;
43 	char *s;
44 
45 	spin_lock(&tcon->tc_lock);
46 	if (unlikely(!tcon->origin_fullpath)) {
47 		spin_unlock(&tcon->tc_lock);
48 		return ERR_PTR(-EREMOTE);
49 	}
50 	spin_unlock(&tcon->tc_lock);
51 
52 	s = dentry_path_raw(dentry, page, PATH_MAX);
53 	if (IS_ERR(s))
54 		return s;
55 	/* for root, we want "" */
56 	if (!s[1])
57 		s++;
58 
59 	spin_lock(&tcon->tc_lock);
60 	len = strlen(tcon->origin_fullpath);
61 	if (s < (char *)page + len) {
62 		spin_unlock(&tcon->tc_lock);
63 		return ERR_PTR(-ENAMETOOLONG);
64 	}
65 
66 	s -= len;
67 	memcpy(s, tcon->origin_fullpath, len);
68 	spin_unlock(&tcon->tc_lock);
69 	convert_delimiter(s, '/');
70 
71 	return s;
72 }
73 
74 static inline void dfs_put_root_smb_sessions(struct list_head *head)
75 {
76 	struct dfs_root_ses *root, *tmp;
77 
78 	list_for_each_entry_safe(root, tmp, head, list) {
79 		list_del_init(&root->list);
80 		cifs_put_smb_ses(root->ses);
81 		kfree(root);
82 	}
83 }
84 
85 #endif /* _CIFS_DFS_H */
86