xref: /openbmc/linux/fs/smb/client/dfs.h (revision e9b7b8b3)
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 #include <linux/namei.h>
13 
14 #define DFS_INTERLINK(v) \
15 	(((v) & DFSREF_REFERRAL_SERVER) && !((v) & DFSREF_STORAGE_SERVER))
16 
17 struct dfs_ref {
18 	char *path;
19 	char *full_path;
20 	struct dfs_cache_tgt_list tl;
21 	struct dfs_cache_tgt_iterator *tit;
22 };
23 
24 struct dfs_ref_walk {
25 	struct dfs_ref *ref;
26 	struct dfs_ref refs[MAX_NESTED_LINKS];
27 };
28 
29 #define ref_walk_start(w)	((w)->refs)
30 #define ref_walk_end(w)	(&(w)->refs[ARRAY_SIZE((w)->refs) - 1])
31 #define ref_walk_cur(w)	((w)->ref)
32 #define ref_walk_descend(w)	(--ref_walk_cur(w) >= ref_walk_start(w))
33 
34 #define ref_walk_tit(w)	(ref_walk_cur(w)->tit)
35 #define ref_walk_empty(w)	(!ref_walk_tit(w))
36 #define ref_walk_path(w)	(ref_walk_cur(w)->path)
37 #define ref_walk_fpath(w)	(ref_walk_cur(w)->full_path)
38 #define ref_walk_tl(w)		(&ref_walk_cur(w)->tl)
39 
40 static inline struct dfs_ref_walk *ref_walk_alloc(void)
41 {
42 	struct dfs_ref_walk *rw;
43 
44 	rw = kmalloc(sizeof(*rw), GFP_KERNEL);
45 	if (!rw)
46 		return ERR_PTR(-ENOMEM);
47 	return rw;
48 }
49 
50 static inline void ref_walk_init(struct dfs_ref_walk *rw)
51 {
52 	memset(rw, 0, sizeof(*rw));
53 	ref_walk_cur(rw) = ref_walk_start(rw);
54 }
55 
56 static inline void __ref_walk_free(struct dfs_ref *ref)
57 {
58 	kfree(ref->path);
59 	kfree(ref->full_path);
60 	dfs_cache_free_tgts(&ref->tl);
61 	memset(ref, 0, sizeof(*ref));
62 }
63 
64 static inline void ref_walk_free(struct dfs_ref_walk *rw)
65 {
66 	struct dfs_ref *ref = ref_walk_start(rw);
67 
68 	for (; ref <= ref_walk_end(rw); ref++)
69 		__ref_walk_free(ref);
70 	kfree(rw);
71 }
72 
73 static inline int ref_walk_advance(struct dfs_ref_walk *rw)
74 {
75 	struct dfs_ref *ref = ref_walk_cur(rw) + 1;
76 
77 	if (ref > ref_walk_end(rw))
78 		return -ELOOP;
79 	__ref_walk_free(ref);
80 	ref_walk_cur(rw) = ref;
81 	return 0;
82 }
83 
84 static inline struct dfs_cache_tgt_iterator *
85 ref_walk_next_tgt(struct dfs_ref_walk *rw)
86 {
87 	struct dfs_cache_tgt_iterator *tit;
88 	struct dfs_ref *ref = ref_walk_cur(rw);
89 
90 	if (!ref->tit)
91 		tit = dfs_cache_get_tgt_iterator(&ref->tl);
92 	else
93 		tit = dfs_cache_get_next_tgt(&ref->tl, ref->tit);
94 	ref->tit = tit;
95 	return tit;
96 }
97 
98 static inline int ref_walk_get_tgt(struct dfs_ref_walk *rw,
99 				   struct dfs_info3_param *tgt)
100 {
101 	zfree_dfs_info_param(tgt);
102 	return dfs_cache_get_tgt_referral(ref_walk_path(rw) + 1,
103 					  ref_walk_tit(rw), tgt);
104 }
105 
106 static inline int ref_walk_num_tgts(struct dfs_ref_walk *rw)
107 {
108 	return dfs_cache_get_nr_tgts(ref_walk_tl(rw));
109 }
110 
111 static inline void ref_walk_set_tgt_hint(struct dfs_ref_walk *rw)
112 {
113 	dfs_cache_noreq_update_tgthint(ref_walk_path(rw) + 1,
114 				       ref_walk_tit(rw));
115 }
116 
117 struct dfs_root_ses {
118 	struct list_head list;
119 	struct cifs_ses *ses;
120 };
121 
122 int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
123 			      struct smb3_fs_context *ctx);
124 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs);
125 
126 static inline char *dfs_get_path(struct cifs_sb_info *cifs_sb, const char *path)
127 {
128 	return dfs_cache_canonical_path(path, cifs_sb->local_nls, cifs_remap(cifs_sb));
129 }
130 
131 static inline int dfs_get_referral(struct cifs_mount_ctx *mnt_ctx, const char *path,
132 				   struct dfs_info3_param *ref, struct dfs_cache_tgt_list *tl)
133 {
134 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
135 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
136 
137 	return dfs_cache_find(mnt_ctx->xid, ctx->dfs_root_ses, cifs_sb->local_nls,
138 			      cifs_remap(cifs_sb), path, ref, tl);
139 }
140 
141 static inline void dfs_put_root_smb_sessions(struct list_head *head)
142 {
143 	struct dfs_root_ses *root, *tmp;
144 
145 	list_for_each_entry_safe(root, tmp, head, list) {
146 		list_del_init(&root->list);
147 		cifs_put_smb_ses(root->ses);
148 		kfree(root);
149 	}
150 }
151 
152 #endif /* _CIFS_DFS_H */
153