blob: 775dbc7b0d7a990823b0651652151e25a02a8b0a [file] [log] [blame]
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * DFS referral cache routines
4 *
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03005 * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
Paulo Alcantara54be1f62018-11-14 16:01:21 -02006 */
7
Paulo Alcantara54be1f62018-11-14 16:01:21 -02008#include <linux/jhash.h>
9#include <linux/ktime.h>
10#include <linux/slab.h>
Alexey Dobriyan97a32532020-02-03 17:37:17 -080011#include <linux/proc_fs.h>
Paulo Alcantara54be1f62018-11-14 16:01:21 -020012#include <linux/nls.h>
13#include <linux/workqueue.h>
Paulo Alcantarac9f71102021-06-04 19:25:29 -030014#include <linux/uuid.h>
Paulo Alcantara54be1f62018-11-14 16:01:21 -020015#include "cifsglob.h"
16#include "smb2pdu.h"
17#include "smb2proto.h"
18#include "cifsproto.h"
19#include "cifs_debug.h"
20#include "cifs_unicode.h"
21#include "smb2glob.h"
22
23#include "dfs_cache.h"
24
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030025#define CACHE_HTABLE_SIZE 32
26#define CACHE_MAX_ENTRIES 64
Paulo Alcantarac950fc72021-06-04 19:25:32 -030027#define CACHE_MIN_TTL 120 /* 2 minutes */
Paulo Alcantara54be1f62018-11-14 16:01:21 -020028
29#define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
30 DFSREF_STORAGE_SERVER))
31
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030032struct cache_dfs_tgt {
33 char *name;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -030034 int path_consumed;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030035 struct list_head list;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020036};
37
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030038struct cache_entry {
39 struct hlist_node hlist;
40 const char *path;
Paulo Alcantara5ff28362021-02-24 20:59:23 -030041 int hdr_flags; /* RESP_GET_DFS_REFERRAL.ReferralHeaderFlags */
42 int ttl; /* DFS_REREFERRAL_V3.TimeToLive */
43 int srvtype; /* DFS_REREFERRAL_V3.ServerType */
44 int ref_flags; /* DFS_REREFERRAL_V3.ReferralEntryFlags */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030045 struct timespec64 etime;
Paulo Alcantara5ff28362021-02-24 20:59:23 -030046 int path_consumed; /* RESP_GET_DFS_REFERRAL.PathConsumed */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030047 int numtgts;
48 struct list_head tlist;
49 struct cache_dfs_tgt *tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020050};
51
Paulo Alcantarac9f71102021-06-04 19:25:29 -030052/* List of referral server sessions per dfs mount */
53struct mount_group {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030054 struct list_head list;
Paulo Alcantarac9f71102021-06-04 19:25:29 -030055 uuid_t id;
56 struct cifs_ses *sessions[CACHE_MAX_ENTRIES];
57 int num_sessions;
58 spinlock_t lock;
59 struct list_head refresh_list;
60 struct kref refcount;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020061};
62
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030063static struct kmem_cache *cache_slab __read_mostly;
64static struct workqueue_struct *dfscache_wq __read_mostly;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020065
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030066static int cache_ttl;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -030067static DEFINE_SPINLOCK(cache_ttl_lock);
68
Paulo Alcantarac870a8e2021-06-04 19:25:30 -030069static struct nls_table *cache_cp;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020070
71/*
72 * Number of entries in the cache
73 */
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -030074static atomic_t cache_count;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020075
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030076static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -030077static DECLARE_RWSEM(htable_rw_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030078
Paulo Alcantarac9f71102021-06-04 19:25:29 -030079static LIST_HEAD(mount_group_list);
80static DEFINE_MUTEX(mount_group_list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -020081
82static void refresh_cache_worker(struct work_struct *work);
83
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030084static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
85
Paulo Alcantarac9f71102021-06-04 19:25:29 -030086static void get_ipc_unc(const char *ref_path, char *ipc, size_t ipclen)
87{
88 const char *host;
89 size_t len;
90
91 extract_unc_hostname(ref_path, &host, &len);
92 scnprintf(ipc, ipclen, "\\\\%.*s\\IPC$", (int)len, host);
93}
94
95static struct cifs_ses *find_ipc_from_server_path(struct cifs_ses **ses, const char *path)
96{
97 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
98
99 get_ipc_unc(path, unc, sizeof(unc));
100 for (; *ses; ses++) {
101 if (!strcasecmp(unc, (*ses)->tcon_ipc->treeName))
102 return *ses;
103 }
104 return ERR_PTR(-ENOENT);
105}
106
107static void __mount_group_release(struct mount_group *mg)
108{
109 int i;
110
111 for (i = 0; i < mg->num_sessions; i++)
112 cifs_put_smb_ses(mg->sessions[i]);
113 kfree(mg);
114}
115
116static void mount_group_release(struct kref *kref)
117{
118 struct mount_group *mg = container_of(kref, struct mount_group, refcount);
119
120 mutex_lock(&mount_group_list_lock);
121 list_del(&mg->list);
122 mutex_unlock(&mount_group_list_lock);
123 __mount_group_release(mg);
124}
125
126static struct mount_group *find_mount_group_locked(const uuid_t *id)
127{
128 struct mount_group *mg;
129
130 list_for_each_entry(mg, &mount_group_list, list) {
131 if (uuid_equal(&mg->id, id))
132 return mg;
133 }
134 return ERR_PTR(-ENOENT);
135}
136
137static struct mount_group *__get_mount_group_locked(const uuid_t *id)
138{
139 struct mount_group *mg;
140
141 mg = find_mount_group_locked(id);
142 if (!IS_ERR(mg))
143 return mg;
144
145 mg = kmalloc(sizeof(*mg), GFP_KERNEL);
146 if (!mg)
147 return ERR_PTR(-ENOMEM);
148 kref_init(&mg->refcount);
149 uuid_copy(&mg->id, id);
150 mg->num_sessions = 0;
151 spin_lock_init(&mg->lock);
152 list_add(&mg->list, &mount_group_list);
153 return mg;
154}
155
156static struct mount_group *get_mount_group(const uuid_t *id)
157{
158 struct mount_group *mg;
159
160 mutex_lock(&mount_group_list_lock);
161 mg = __get_mount_group_locked(id);
162 if (!IS_ERR(mg))
163 kref_get(&mg->refcount);
164 mutex_unlock(&mount_group_list_lock);
165
166 return mg;
167}
168
169static void free_mount_group_list(void)
170{
171 struct mount_group *mg, *tmp_mg;
172
173 list_for_each_entry_safe(mg, tmp_mg, &mount_group_list, list) {
174 list_del_init(&mg->list);
175 __mount_group_release(mg);
176 }
177}
178
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300179/**
180 * dfs_cache_canonical_path - get a canonical DFS path
181 *
182 * @path: DFS path
183 * @cp: codepage
184 * @remap: mapping type
185 *
186 * Return canonical path if success, otherwise error.
187 */
188char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200189{
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300190 char *tmp;
191 int plen = 0;
192 char *npath;
193
Paulo Alcantara (SUSE)ff2f7fc2019-12-04 17:38:01 -0300194 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300195 return ERR_PTR(-EINVAL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200196
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300197 if (unlikely(strcmp(cp->charset, cache_cp->charset))) {
198 tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, cp, remap);
199 if (!tmp) {
200 cifs_dbg(VFS, "%s: failed to convert path to utf16\n", __func__);
201 return ERR_PTR(-EINVAL);
202 }
203
204 npath = cifs_strndup_from_utf16(tmp, plen, true, cache_cp);
205 kfree(tmp);
206
207 if (!npath) {
208 cifs_dbg(VFS, "%s: failed to convert path from utf16\n", __func__);
209 return ERR_PTR(-EINVAL);
210 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200211 } else {
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300212 npath = kstrdup(path, GFP_KERNEL);
213 if (!npath)
214 return ERR_PTR(-ENOMEM);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200215 }
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300216 convert_delimiter(npath, '\\');
217 return npath;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200218}
219
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300220static inline bool cache_entry_expired(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200221{
222 struct timespec64 ts;
223
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100224 ktime_get_coarse_real_ts64(&ts);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300225 return timespec64_compare(&ts, &ce->etime) >= 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200226}
227
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300228static inline void free_tgts(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200229{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300230 struct cache_dfs_tgt *t, *n;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200231
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300232 list_for_each_entry_safe(t, n, &ce->tlist, list) {
233 list_del(&t->list);
234 kfree(t->name);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200235 kfree(t);
236 }
237}
238
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300239static inline void flush_cache_ent(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200240{
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300241 hlist_del_init(&ce->hlist);
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300242 kfree(ce->path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200243 free_tgts(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300244 atomic_dec(&cache_count);
245 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200246}
247
248static void flush_cache_ents(void)
249{
250 int i;
251
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300252 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
253 struct hlist_head *l = &cache_htable[i];
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300254 struct hlist_node *n;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300255 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200256
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300257 hlist_for_each_entry_safe(ce, n, l, hlist) {
258 if (!hlist_unhashed(&ce->hlist))
259 flush_cache_ent(ce);
260 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200261 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200262}
263
264/*
265 * dfs cache /proc file
266 */
267static int dfscache_proc_show(struct seq_file *m, void *v)
268{
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300269 int i;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300270 struct cache_entry *ce;
271 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200272
273 seq_puts(m, "DFS cache\n---------\n");
274
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300275 down_read(&htable_rw_lock);
276 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
277 struct hlist_head *l = &cache_htable[i];
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200278
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300279 hlist_for_each_entry(ce, l, hlist) {
280 if (hlist_unhashed(&ce->hlist))
281 continue;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200282
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300283 seq_printf(m,
Paulo Alcantara5ff28362021-02-24 20:59:23 -0300284 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
285 ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
286 ce->ttl, ce->etime.tv_nsec, ce->ref_flags, ce->hdr_flags,
287 IS_INTERLINK_SET(ce->hdr_flags) ? "yes" : "no",
288 ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300289
290 list_for_each_entry(t, &ce->tlist, list) {
291 seq_printf(m, " %s%s\n",
292 t->name,
293 ce->tgthint == t ? " (target hint)" : "");
294 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200295 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200296 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300297 up_read(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200298
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200299 return 0;
300}
301
302static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
303 size_t count, loff_t *ppos)
304{
305 char c;
306 int rc;
307
308 rc = get_user(c, buffer);
309 if (rc)
310 return rc;
311
312 if (c != '0')
313 return -EINVAL;
314
Joe Perchesa0a30362020-04-14 22:42:53 -0700315 cifs_dbg(FYI, "clearing dfs cache\n");
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300316
317 down_write(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200318 flush_cache_ents();
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300319 up_write(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200320
321 return count;
322}
323
324static int dfscache_proc_open(struct inode *inode, struct file *file)
325{
326 return single_open(file, dfscache_proc_show, NULL);
327}
328
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800329const struct proc_ops dfscache_proc_ops = {
330 .proc_open = dfscache_proc_open,
331 .proc_read = seq_read,
332 .proc_lseek = seq_lseek,
333 .proc_release = single_release,
334 .proc_write = dfscache_proc_write,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200335};
336
337#ifdef CONFIG_CIFS_DEBUG2
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300338static inline void dump_tgts(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200339{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300340 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200341
342 cifs_dbg(FYI, "target list:\n");
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300343 list_for_each_entry(t, &ce->tlist, list) {
344 cifs_dbg(FYI, " %s%s\n", t->name,
345 ce->tgthint == t ? " (target hint)" : "");
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200346 }
347}
348
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300349static inline void dump_ce(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200350{
Paulo Alcantara5ff28362021-02-24 20:59:23 -0300351 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
Joe Perchesa0a30362020-04-14 22:42:53 -0700352 ce->path,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300353 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
354 ce->etime.tv_nsec,
Paulo Alcantara5ff28362021-02-24 20:59:23 -0300355 ce->hdr_flags, ce->ref_flags,
356 IS_INTERLINK_SET(ce->hdr_flags) ? "yes" : "no",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300357 ce->path_consumed,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200358 cache_entry_expired(ce) ? "yes" : "no");
359 dump_tgts(ce);
360}
361
362static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
363{
364 int i;
365
366 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
367 for (i = 0; i < numrefs; i++) {
368 const struct dfs_info3_param *ref = &refs[i];
369
370 cifs_dbg(FYI,
371 "\n"
372 "flags: 0x%x\n"
373 "path_consumed: %d\n"
374 "server_type: 0x%x\n"
375 "ref_flag: 0x%x\n"
376 "path_name: %s\n"
377 "node_name: %s\n"
378 "ttl: %d (%dm)\n",
379 ref->flags, ref->path_consumed, ref->server_type,
380 ref->ref_flag, ref->path_name, ref->node_name,
381 ref->ttl, ref->ttl / 60);
382 }
383}
384#else
385#define dump_tgts(e)
386#define dump_ce(e)
387#define dump_refs(r, n)
388#endif
389
390/**
391 * dfs_cache_init - Initialize DFS referral cache.
392 *
393 * Return zero if initialized successfully, otherwise non-zero.
394 */
395int dfs_cache_init(void)
396{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300397 int rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200398 int i;
399
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300400 dfscache_wq = alloc_workqueue("cifs-dfscache", WQ_FREEZABLE | WQ_UNBOUND, 1);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300401 if (!dfscache_wq)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200402 return -ENOMEM;
403
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300404 cache_slab = kmem_cache_create("cifs_dfs_cache",
405 sizeof(struct cache_entry), 0,
406 SLAB_HWCACHE_ALIGN, NULL);
407 if (!cache_slab) {
408 rc = -ENOMEM;
409 goto out_destroy_wq;
410 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200411
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300412 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
413 INIT_HLIST_HEAD(&cache_htable[i]);
414
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300415 atomic_set(&cache_count, 0);
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300416 cache_cp = load_nls("utf8");
417 if (!cache_cp)
418 cache_cp = load_nls_default();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200419
420 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
421 return 0;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300422
423out_destroy_wq:
424 destroy_workqueue(dfscache_wq);
425 return rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200426}
427
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300428static int cache_entry_hash(const void *data, int size, unsigned int *hash)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200429{
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300430 int i, clen;
431 const unsigned char *s = data;
432 wchar_t c;
433 unsigned int h = 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200434
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300435 for (i = 0; i < size; i += clen) {
436 clen = cache_cp->char2uni(&s[i], size - i, &c);
437 if (unlikely(clen < 0)) {
438 cifs_dbg(VFS, "%s: can't convert char\n", __func__);
439 return clen;
440 }
441 c = cifs_toupper(c);
442 h = jhash(&c, sizeof(c), h);
443 }
444 *hash = h % CACHE_HTABLE_SIZE;
445 return 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200446}
447
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200448/* Return target hint of a DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300449static inline char *get_tgt_name(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200450{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300451 struct cache_dfs_tgt *t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200452
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300453 return t ? t->name : ERR_PTR(-ENOENT);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200454}
455
456/* Return expire time out of a new entry's TTL */
457static inline struct timespec64 get_expire_time(int ttl)
458{
459 struct timespec64 ts = {
460 .tv_sec = ttl,
461 .tv_nsec = 0,
462 };
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100463 struct timespec64 now;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200464
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100465 ktime_get_coarse_real_ts64(&now);
466 return timespec64_add(now, ts);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200467}
468
469/* Allocate a new DFS target */
Paulo Alcantara7548e1d2020-07-21 09:36:42 -0300470static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200471{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300472 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200473
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300474 t = kmalloc(sizeof(*t), GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200475 if (!t)
476 return ERR_PTR(-ENOMEM);
Al Viro8d767222021-03-05 15:02:34 -0500477 t->name = kstrdup(name, GFP_ATOMIC);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300478 if (!t->name) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200479 kfree(t);
480 return ERR_PTR(-ENOMEM);
481 }
Paulo Alcantara7548e1d2020-07-21 09:36:42 -0300482 t->path_consumed = path_consumed;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300483 INIT_LIST_HEAD(&t->list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200484 return t;
485}
486
487/*
488 * Copy DFS referral information to a cache entry and conditionally update
489 * target hint.
490 */
491static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300492 struct cache_entry *ce, const char *tgthint)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200493{
494 int i;
495
Paulo Alcantarac950fc72021-06-04 19:25:32 -0300496 ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300497 ce->etime = get_expire_time(ce->ttl);
498 ce->srvtype = refs[0].server_type;
Paulo Alcantara5ff28362021-02-24 20:59:23 -0300499 ce->hdr_flags = refs[0].flags;
500 ce->ref_flags = refs[0].ref_flag;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300501 ce->path_consumed = refs[0].path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200502
503 for (i = 0; i < numrefs; i++) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300504 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200505
Paulo Alcantara7548e1d2020-07-21 09:36:42 -0300506 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200507 if (IS_ERR(t)) {
508 free_tgts(ce);
509 return PTR_ERR(t);
510 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300511 if (tgthint && !strcasecmp(t->name, tgthint)) {
512 list_add(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200513 tgthint = NULL;
514 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300515 list_add_tail(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200516 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300517 ce->numtgts++;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200518 }
519
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300520 ce->tgthint = list_first_entry_or_null(&ce->tlist,
521 struct cache_dfs_tgt, list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200522
523 return 0;
524}
525
526/* Allocate a new cache entry */
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300527static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200528{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300529 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200530 int rc;
531
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300532 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200533 if (!ce)
534 return ERR_PTR(-ENOMEM);
535
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300536 ce->path = refs[0].path_name;
537 refs[0].path_name = NULL;
538
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300539 INIT_HLIST_NODE(&ce->hlist);
540 INIT_LIST_HEAD(&ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200541
542 rc = copy_ref_data(refs, numrefs, ce, NULL);
543 if (rc) {
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300544 kfree(ce->path);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300545 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200546 ce = ERR_PTR(rc);
547 }
548 return ce;
549}
550
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300551static void remove_oldest_entry_locked(void)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200552{
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300553 int i;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300554 struct cache_entry *ce;
555 struct cache_entry *to_del = NULL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200556
Paulo Alcantara1023e902021-06-08 13:50:06 -0300557 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
558
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300559 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
560 struct hlist_head *l = &cache_htable[i];
561
562 hlist_for_each_entry(ce, l, hlist) {
563 if (hlist_unhashed(&ce->hlist))
564 continue;
565 if (!to_del || timespec64_compare(&ce->etime,
566 &to_del->etime) < 0)
567 to_del = ce;
568 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200569 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300570
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200571 if (!to_del) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700572 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300573 return;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200574 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300575
Joe Perchesa0a30362020-04-14 22:42:53 -0700576 cifs_dbg(FYI, "%s: removing entry\n", __func__);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200577 dump_ce(to_del);
578 flush_cache_ent(to_del);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200579}
580
581/* Add a new DFS cache entry */
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300582static int add_cache_entry_locked(struct dfs_info3_param *refs, int numrefs)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200583{
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300584 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300585 struct cache_entry *ce;
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300586 unsigned int hash;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200587
Paulo Alcantara1023e902021-06-08 13:50:06 -0300588 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
589
590 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
591 cifs_dbg(FYI, "%s: reached max cache size (%d)\n", __func__, CACHE_MAX_ENTRIES);
592 remove_oldest_entry_locked();
593 }
594
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300595 rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
596 if (rc)
597 return rc;
598
599 ce = alloc_cache_entry(refs, numrefs);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200600 if (IS_ERR(ce))
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300601 return PTR_ERR(ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200602
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300603 spin_lock(&cache_ttl_lock);
604 if (!cache_ttl) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300605 cache_ttl = ce->ttl;
606 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200607 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300608 cache_ttl = min_t(int, cache_ttl, ce->ttl);
609 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200610 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300611 spin_unlock(&cache_ttl_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200612
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300613 hlist_add_head(&ce->hlist, &cache_htable[hash]);
614 dump_ce(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300615
Paulo Alcantara1023e902021-06-08 13:50:06 -0300616 atomic_inc(&cache_count);
617
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300618 return 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200619}
620
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300621/* Check if two DFS paths are equal. @s1 and @s2 are expected to be in @cache_cp's charset */
622static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
623{
624 int i, l1, l2;
625 wchar_t c1, c2;
626
627 if (len1 != len2)
628 return false;
629
630 for (i = 0; i < len1; i += l1) {
631 l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
632 l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
633 if (unlikely(l1 < 0 && l2 < 0)) {
634 if (s1[i] != s2[i])
635 return false;
636 l1 = 1;
637 continue;
638 }
639 if (l1 != l2)
640 return false;
641 if (cifs_toupper(c1) != cifs_toupper(c2))
642 return false;
643 }
644 return true;
645}
646
647static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200648{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300649 struct cache_entry *ce;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300650
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300651 hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
652 if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300653 dump_ce(ce);
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300654 return ce;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300655 }
656 }
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300657 return ERR_PTR(-EEXIST);
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300658}
659
660/*
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300661 * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300662 *
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300663 * Use whole path components in the match. Must be called with htable_rw_lock held.
664 *
665 * Return ERR_PTR(-EEXIST) if the entry is not found.
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300666 */
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300667static struct cache_entry *lookup_cache_entry(const char *path)
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300668{
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300669 struct cache_entry *ce;
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300670 int cnt = 0;
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300671 const char *s = path, *e;
672 char sep = *s;
673 unsigned int hash;
674 int rc;
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300675
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300676 while ((s = strchr(s, sep)) && ++cnt < 3)
677 s++;
678
679 if (cnt < 3) {
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300680 rc = cache_entry_hash(path, strlen(path), &hash);
681 if (rc)
682 return ERR_PTR(rc);
683 return __lookup_cache_entry(path, hash, strlen(path));
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300684 }
685 /*
686 * Handle paths that have more than two path components and are a complete prefix of the DFS
687 * referral request path (@path).
688 *
689 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
690 */
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300691 e = path + strlen(path) - 1;
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300692 while (e > s) {
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300693 int len;
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300694
695 /* skip separators */
696 while (e > s && *e == sep)
697 e--;
698 if (e == s)
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300699 break;
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300700
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300701 len = e + 1 - path;
702 rc = cache_entry_hash(path, len, &hash);
703 if (rc)
704 return ERR_PTR(rc);
705 ce = __lookup_cache_entry(path, hash, len);
706 if (!IS_ERR(ce))
707 return ce;
708
Paulo Alcantara2e5de422020-07-21 09:36:39 -0300709 /* backward until separator */
710 while (e > s && *e != sep)
711 e--;
712 }
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300713 return ERR_PTR(-EEXIST);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200714}
715
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200716/**
717 * dfs_cache_destroy - destroy DFS referral cache
718 */
719void dfs_cache_destroy(void)
720{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300721 cancel_delayed_work_sync(&refresh_task);
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300722 unload_nls(cache_cp);
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300723 free_mount_group_list();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200724 flush_cache_ents();
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300725 kmem_cache_destroy(cache_slab);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300726 destroy_workqueue(dfscache_wq);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200727
728 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
729}
730
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300731/* Update a cache entry with the new referral in @refs */
Paulo Alcantara1023e902021-06-08 13:50:06 -0300732static int update_cache_entry_locked(struct cache_entry *ce, const struct dfs_info3_param *refs,
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300733 int numrefs)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200734{
735 int rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200736 char *s, *th = NULL;
737
Paulo Alcantara1023e902021-06-08 13:50:06 -0300738 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200739
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300740 if (ce->tgthint) {
741 s = ce->tgthint->name;
Al Viro8d767222021-03-05 15:02:34 -0500742 th = kstrdup(s, GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200743 if (!th)
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300744 return -ENOMEM;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200745 }
746
747 free_tgts(ce);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300748 ce->numtgts = 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200749
750 rc = copy_ref_data(refs, numrefs, ce, th);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300751
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200752 kfree(th);
753
YueHaibingeecfc572020-01-17 10:21:56 +0800754 return rc;
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300755}
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200756
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300757static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const char *path,
758 struct dfs_info3_param **refs, int *numrefs)
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300759{
Paulo Alcantara1023e902021-06-08 13:50:06 -0300760 int rc;
761 int i;
762
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300763 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
764
Paulo Alcantara1023e902021-06-08 13:50:06 -0300765 *refs = NULL;
766 *numrefs = 0;
767
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300768 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
769 return -EOPNOTSUPP;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300770 if (unlikely(!cache_cp))
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300771 return -EINVAL;
772
Paulo Alcantara1023e902021-06-08 13:50:06 -0300773 rc = ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs, cache_cp,
774 NO_MAP_UNI_RSVD);
775 if (!rc) {
776 struct dfs_info3_param *ref = *refs;
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300777
Paulo Alcantara1023e902021-06-08 13:50:06 -0300778 for (i = 0; i < *numrefs; i++)
779 convert_delimiter(ref[i].path_name, '\\');
780 }
781 return rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200782}
783
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200784/*
785 * Find, create or update a DFS cache entry.
786 *
787 * If the entry wasn't found, it will create a new one. Or if it was found but
788 * expired, then it will update the entry accordingly.
789 *
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300790 * For interlinks, cifs_mount() and expand_dfs_referral() are supposed to
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200791 * handle them properly.
792 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300793static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, const char *path)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200794{
795 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300796 struct cache_entry *ce;
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300797 struct dfs_info3_param *refs = NULL;
798 int numrefs = 0;
799 bool newent = false;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200800
801 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
802
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300803 down_write(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200804
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300805 ce = lookup_cache_entry(path);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300806 if (!IS_ERR(ce)) {
807 if (!cache_entry_expired(ce)) {
808 dump_ce(ce);
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300809 up_write(&htable_rw_lock);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300810 return 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200811 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300812 } else {
813 newent = true;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200814 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300815
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300816 /*
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300817 * Either the entry was not found, or it is expired.
818 * Request a new DFS referral in order to create or update a cache entry.
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300819 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300820 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300821 if (rc)
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300822 goto out_unlock;
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300823
824 dump_refs(refs, numrefs);
825
826 if (!newent) {
Paulo Alcantara1023e902021-06-08 13:50:06 -0300827 rc = update_cache_entry_locked(ce, refs, numrefs);
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300828 goto out_unlock;
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300829 }
830
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300831 rc = add_cache_entry_locked(refs, numrefs);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300832
Paulo Alcantarac9f71102021-06-04 19:25:29 -0300833out_unlock:
834 up_write(&htable_rw_lock);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300835 free_dfs_info_array(refs, numrefs);
836 return rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200837}
838
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300839/*
840 * Set up a DFS referral from a given cache entry.
841 *
842 * Must be called with htable_rw_lock held.
843 */
844static int setup_referral(const char *path, struct cache_entry *ce,
845 struct dfs_info3_param *ref, const char *target)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200846{
847 int rc;
848
849 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
850
851 memset(ref, 0, sizeof(*ref));
852
Al Viro8d767222021-03-05 15:02:34 -0500853 ref->path_name = kstrdup(path, GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200854 if (!ref->path_name)
855 return -ENOMEM;
856
Al Viro8d767222021-03-05 15:02:34 -0500857 ref->node_name = kstrdup(target, GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200858 if (!ref->node_name) {
859 rc = -ENOMEM;
860 goto err_free_path;
861 }
862
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300863 ref->path_consumed = ce->path_consumed;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300864 ref->ttl = ce->ttl;
865 ref->server_type = ce->srvtype;
Paulo Alcantara5ff28362021-02-24 20:59:23 -0300866 ref->ref_flag = ce->ref_flags;
867 ref->flags = ce->hdr_flags;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200868
869 return 0;
870
871err_free_path:
872 kfree(ref->path_name);
873 ref->path_name = NULL;
874 return rc;
875}
876
877/* Return target list of a DFS cache entry */
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300878static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200879{
880 int rc;
881 struct list_head *head = &tl->tl_list;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300882 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200883 struct dfs_cache_tgt_iterator *it, *nit;
884
885 memset(tl, 0, sizeof(*tl));
886 INIT_LIST_HEAD(head);
887
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300888 list_for_each_entry(t, &ce->tlist, list) {
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300889 it = kzalloc(sizeof(*it), GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200890 if (!it) {
891 rc = -ENOMEM;
892 goto err_free_it;
893 }
894
Al Viro8d767222021-03-05 15:02:34 -0500895 it->it_name = kstrdup(t->name, GFP_ATOMIC);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200896 if (!it->it_name) {
Dan Carpenterc715f892019-01-05 21:18:03 +0300897 kfree(it);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200898 rc = -ENOMEM;
899 goto err_free_it;
900 }
Paulo Alcantara7548e1d2020-07-21 09:36:42 -0300901 it->it_path_consumed = t->path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200902
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300903 if (ce->tgthint == t)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200904 list_add(&it->it_list, head);
905 else
906 list_add_tail(&it->it_list, head);
907 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300908
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300909 tl->tl_numtgts = ce->numtgts;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200910
911 return 0;
912
913err_free_it:
914 list_for_each_entry_safe(it, nit, head, it_list) {
915 kfree(it->it_name);
916 kfree(it);
917 }
918 return rc;
919}
920
921/**
922 * dfs_cache_find - find a DFS cache entry
923 *
924 * If it doesn't find the cache entry, then it will get a DFS referral
925 * for @path and create a new entry.
926 *
927 * In case the cache entry exists but expired, it will get a DFS referral
928 * for @path and then update the respective cache entry.
929 *
930 * These parameters are passed down to the get_dfs_refer() call if it
931 * needs to be issued:
932 * @xid: syscall xid
933 * @ses: smb session to issue the request on
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300934 * @cp: codepage
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200935 * @remap: path character remapping type
936 * @path: path to lookup in DFS referral cache.
937 *
938 * @ref: when non-NULL, store single DFS referral result in it.
939 * @tgt_list: when non-NULL, store complete DFS target list in it.
940 *
941 * Return zero if the target was found, otherwise non-zero.
942 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300943int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
944 int remap, const char *path, struct dfs_info3_param *ref,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200945 struct dfs_cache_tgt_list *tgt_list)
946{
947 int rc;
Al Viro9cfdb1c2021-03-18 01:03:34 -0400948 const char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300949 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200950
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300951 npath = dfs_cache_canonical_path(path, cp, remap);
952 if (IS_ERR(npath))
953 return PTR_ERR(npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200954
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300955 rc = cache_refresh_path(xid, ses, npath);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300956 if (rc)
957 goto out_free_path;
958
959 down_read(&htable_rw_lock);
960
Paulo Alcantara42caeba2021-06-04 19:25:31 -0300961 ce = lookup_cache_entry(npath);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300962 if (IS_ERR(ce)) {
963 up_read(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200964 rc = PTR_ERR(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300965 goto out_free_path;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200966 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -0300967
968 if (ref)
969 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
970 else
971 rc = 0;
972 if (!rc && tgt_list)
973 rc = get_targets(ce, tgt_list);
974
975 up_read(&htable_rw_lock);
976
977out_free_path:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300978 kfree(npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200979 return rc;
980}
981
982/**
983 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
984 * the currently connected server.
985 *
986 * NOTE: This function will neither update a cache entry in case it was
987 * expired, nor create a new cache entry if @path hasn't been found. It heavily
988 * relies on an existing cache entry.
989 *
Paulo Alcantarac870a8e2021-06-04 19:25:30 -0300990 * @path: canonical DFS path to lookup in the DFS referral cache.
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200991 * @ref: when non-NULL, store single DFS referral result in it.
992 * @tgt_list: when non-NULL, store complete DFS target list in it.
993 *
994 * Return 0 if successful.
995 * Return -ENOENT if the entry was not found.
996 * Return non-zero for other errors.
997 */
998int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
999 struct dfs_cache_tgt_list *tgt_list)
1000{
1001 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001002 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001003
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001004 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001005
1006 down_read(&htable_rw_lock);
1007
Paulo Alcantara42caeba2021-06-04 19:25:31 -03001008 ce = lookup_cache_entry(path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001009 if (IS_ERR(ce)) {
1010 rc = PTR_ERR(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001011 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001012 }
1013
1014 if (ref)
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001015 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001016 else
1017 rc = 0;
1018 if (!rc && tgt_list)
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001019 rc = get_targets(ce, tgt_list);
1020
1021out_unlock:
1022 up_read(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001023 return rc;
1024}
1025
1026/**
1027 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
1028 *
1029 * If it doesn't find the cache entry, then it will get a DFS referral for @path
1030 * and create a new entry.
1031 *
1032 * In case the cache entry exists but expired, it will get a DFS referral
1033 * for @path and then update the respective cache entry.
1034 *
1035 * @xid: syscall id
1036 * @ses: smb session
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001037 * @cp: codepage
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001038 * @remap: type of character remapping for paths
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001039 * @path: path to lookup in DFS referral cache
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001040 * @it: DFS target iterator
1041 *
1042 * Return zero if the target hint was updated successfully, otherwise non-zero.
1043 */
1044int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001045 const struct nls_table *cp, int remap, const char *path,
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001046 const struct dfs_cache_tgt_iterator *it)
1047{
1048 int rc;
Al Viro9cfdb1c2021-03-18 01:03:34 -04001049 const char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001050 struct cache_entry *ce;
1051 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001052
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001053 npath = dfs_cache_canonical_path(path, cp, remap);
1054 if (IS_ERR(npath))
1055 return PTR_ERR(npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001056
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001057 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001058
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001059 rc = cache_refresh_path(xid, ses, npath);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001060 if (rc)
1061 goto out_free_path;
1062
1063 down_write(&htable_rw_lock);
1064
Paulo Alcantara42caeba2021-06-04 19:25:31 -03001065 ce = lookup_cache_entry(npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001066 if (IS_ERR(ce)) {
1067 rc = PTR_ERR(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001068 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001069 }
1070
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001071 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001072
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001073 if (likely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001074 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001075
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001076 list_for_each_entry(t, &ce->tlist, list) {
1077 if (!strcasecmp(t->name, it->it_name)) {
1078 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001079 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1080 it->it_name);
1081 break;
1082 }
1083 }
1084
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001085out_unlock:
1086 up_write(&htable_rw_lock);
1087out_free_path:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001088 kfree(npath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001089 return rc;
1090}
1091
1092/**
1093 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1094 * without sending any requests to the currently connected server.
1095 *
1096 * NOTE: This function will neither update a cache entry in case it was
1097 * expired, nor create a new cache entry if @path hasn't been found. It heavily
1098 * relies on an existing cache entry.
1099 *
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001100 * @path: canonical DFS path to lookup in DFS referral cache.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001101 * @it: target iterator which contains the target hint to update the cache
1102 * entry with.
1103 *
1104 * Return zero if the target hint was updated successfully, otherwise non-zero.
1105 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001106int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001107{
1108 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001109 struct cache_entry *ce;
1110 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001111
Paulo Alcantara (SUSE)ff2f7fc2019-12-04 17:38:01 -03001112 if (!it)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001113 return -EINVAL;
1114
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001115 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001116
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001117 down_write(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001118
Paulo Alcantara42caeba2021-06-04 19:25:31 -03001119 ce = lookup_cache_entry(path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001120 if (IS_ERR(ce)) {
1121 rc = PTR_ERR(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001122 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001123 }
1124
1125 rc = 0;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001126 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001127
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001128 if (unlikely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001129 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001130
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001131 list_for_each_entry(t, &ce->tlist, list) {
1132 if (!strcasecmp(t->name, it->it_name)) {
1133 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001134 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1135 it->it_name);
1136 break;
1137 }
1138 }
1139
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001140out_unlock:
1141 up_write(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001142 return rc;
1143}
1144
1145/**
1146 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1147 * target iterator (@it).
1148 *
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001149 * @path: canonical DFS path to lookup in DFS referral cache.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001150 * @it: DFS target iterator.
1151 * @ref: DFS referral pointer to set up the gathered information.
1152 *
1153 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1154 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001155int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001156 struct dfs_info3_param *ref)
1157{
1158 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001159 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001160
1161 if (!it || !ref)
1162 return -EINVAL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001163
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001164 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001165
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001166 down_read(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001167
Paulo Alcantara42caeba2021-06-04 19:25:31 -03001168 ce = lookup_cache_entry(path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001169 if (IS_ERR(ce)) {
1170 rc = PTR_ERR(ce);
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001171 goto out_unlock;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001172 }
1173
1174 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1175
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001176 rc = setup_referral(path, ce, ref, it->it_name);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001177
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001178out_unlock:
1179 up_read(&htable_rw_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001180 return rc;
1181}
1182
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001183/**
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001184 * dfs_cache_add_refsrv_session - add SMB session of referral server
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001185 *
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001186 * @mount_id: mount group uuid to lookup.
1187 * @ses: reference counted SMB session of referral server.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001188 */
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001189void dfs_cache_add_refsrv_session(const uuid_t *mount_id, struct cifs_ses *ses)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001190{
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001191 struct mount_group *mg;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001192
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001193 if (WARN_ON_ONCE(!mount_id || uuid_is_null(mount_id) || !ses))
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001194 return;
1195
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001196 mg = get_mount_group(mount_id);
1197 if (WARN_ON_ONCE(IS_ERR(mg)))
1198 return;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001199
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001200 spin_lock(&mg->lock);
1201 if (mg->num_sessions < ARRAY_SIZE(mg->sessions))
1202 mg->sessions[mg->num_sessions++] = ses;
1203 spin_unlock(&mg->lock);
1204 kref_put(&mg->refcount, mount_group_release);
1205}
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001206
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001207/**
1208 * dfs_cache_put_refsrv_sessions - put all referral server sessions
1209 *
1210 * Put all SMB sessions from the given mount group id.
1211 *
1212 * @mount_id: mount group uuid to lookup.
1213 */
1214void dfs_cache_put_refsrv_sessions(const uuid_t *mount_id)
1215{
1216 struct mount_group *mg;
1217
1218 if (!mount_id || uuid_is_null(mount_id))
1219 return;
1220
1221 mutex_lock(&mount_group_list_lock);
1222 mg = find_mount_group_locked(mount_id);
1223 if (IS_ERR(mg)) {
1224 mutex_unlock(&mount_group_list_lock);
1225 return;
1226 }
1227 mutex_unlock(&mount_group_list_lock);
1228 kref_put(&mg->refcount, mount_group_release);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001229}
1230
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001231/**
1232 * dfs_cache_get_tgt_share - parse a DFS target
1233 *
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001234 * @path: DFS full path
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001235 * @it: DFS target iterator.
1236 * @share: tree name.
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001237 * @prefix: prefix path.
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001238 *
1239 * Return zero if target was parsed correctly, otherwise non-zero.
1240 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03001241int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
1242 char **prefix)
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001243{
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001244 char *s, sep, *p;
1245 size_t len;
1246 size_t plen1, plen2;
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001247
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001248 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001249 return -EINVAL;
1250
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001251 *share = NULL;
1252 *prefix = NULL;
1253
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001254 sep = it->it_name[0];
1255 if (sep != '\\' && sep != '/')
1256 return -EINVAL;
1257
1258 s = strchr(it->it_name + 1, sep);
1259 if (!s)
1260 return -EINVAL;
1261
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001262 /* point to prefix in target node */
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001263 s = strchrnul(s + 1, sep);
1264
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001265 /* extract target share */
1266 *share = kstrndup(it->it_name, s - it->it_name, GFP_KERNEL);
1267 if (!*share)
1268 return -ENOMEM;
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001269
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03001270 /* skip separator */
1271 if (*s)
1272 s++;
1273 /* point to prefix in DFS path */
1274 p = path + it->it_path_consumed;
1275 if (*p == sep)
1276 p++;
1277
1278 /* merge prefix paths from DFS path and target node */
1279 plen1 = it->it_name + strlen(it->it_name) - s;
1280 plen2 = path + strlen(path) - p;
1281 if (plen1 || plen2) {
1282 len = plen1 + plen2 + 2;
1283 *prefix = kmalloc(len, GFP_KERNEL);
1284 if (!*prefix) {
1285 kfree(*share);
1286 *share = NULL;
1287 return -ENOMEM;
1288 }
1289 if (plen1)
1290 scnprintf(*prefix, len, "%.*s%c%.*s", (int)plen1, s, sep, (int)plen2, p);
1291 else
1292 strscpy(*prefix, p, len);
1293 }
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03001294 return 0;
1295}
1296
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001297/*
1298 * Refresh all active dfs mounts regardless of whether they are in cache or not.
1299 * (cache can be cleared)
1300 */
1301static void refresh_mounts(struct cifs_ses **sessions)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001302{
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001303 struct TCP_Server_Info *server;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001304 struct cifs_ses *ses;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001305 struct cifs_tcon *tcon, *ntcon;
1306 struct list_head tcons;
1307 unsigned int xid;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001308
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001309 INIT_LIST_HEAD(&tcons);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001310
1311 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001312 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1313 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1314 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1315 if (tcon->dfs_path) {
1316 tcon->tc_count++;
1317 list_add_tail(&tcon->ulist, &tcons);
1318 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001319 }
1320 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001321 }
1322 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001323
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001324 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1325 const char *path = tcon->dfs_path + 1;
Paulo Alcantara1023e902021-06-08 13:50:06 -03001326 struct cache_entry *ce;
1327 struct dfs_info3_param *refs = NULL;
1328 int numrefs = 0;
1329 bool needs_refresh = false;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001330 int rc = 0;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001331
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001332 list_del_init(&tcon->ulist);
Paulo Alcantara1023e902021-06-08 13:50:06 -03001333
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001334 ses = find_ipc_from_server_path(sessions, path);
Paulo Alcantara1023e902021-06-08 13:50:06 -03001335 if (IS_ERR(ses))
1336 goto next_tcon;
1337
1338 down_read(&htable_rw_lock);
1339 ce = lookup_cache_entry(path);
1340 needs_refresh = IS_ERR(ce) || cache_entry_expired(ce);
1341 up_read(&htable_rw_lock);
1342
1343 if (!needs_refresh)
1344 goto next_tcon;
1345
1346 xid = get_xid();
1347 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1348 free_xid(xid);
1349
1350 /* Create or update a cache entry with the new referral */
1351 if (!rc) {
1352 down_write(&htable_rw_lock);
1353 ce = lookup_cache_entry(path);
1354 if (IS_ERR(ce))
1355 add_cache_entry_locked(refs, numrefs);
1356 else if (cache_entry_expired(ce))
1357 update_cache_entry_locked(ce, refs, numrefs);
1358 up_write(&htable_rw_lock);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001359 }
Paulo Alcantara1023e902021-06-08 13:50:06 -03001360
1361next_tcon:
1362 free_dfs_info_array(refs, numrefs);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001363 cifs_put_tcon(tcon);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001364 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001365}
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001366
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001367static void refresh_cache(struct cifs_ses **sessions)
1368{
1369 int i;
1370 struct cifs_ses *ses;
1371 unsigned int xid;
Paulo Alcantara1023e902021-06-08 13:50:06 -03001372 char *ref_paths[CACHE_MAX_ENTRIES];
1373 int count = 0;
1374 struct cache_entry *ce;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001375
1376 /*
Paulo Alcantara1023e902021-06-08 13:50:06 -03001377 * Refresh all cached entries. Get all new referrals outside critical section to avoid
1378 * starvation while performing SMB2 IOCTL on broken or slow connections.
1379
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001380 * The cache entries may cover more paths than the active mounts
1381 * (e.g. domain-based DFS referrals or multi tier DFS setups).
1382 */
Paulo Alcantara1023e902021-06-08 13:50:06 -03001383 down_read(&htable_rw_lock);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001384 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001385 struct hlist_head *l = &cache_htable[i];
1386
1387 hlist_for_each_entry(ce, l, hlist) {
Paulo Alcantara1023e902021-06-08 13:50:06 -03001388 if (count == ARRAY_SIZE(ref_paths))
1389 goto out_unlock;
1390 if (hlist_unhashed(&ce->hlist) || !cache_entry_expired(ce) ||
1391 IS_ERR(find_ipc_from_server_path(sessions, ce->path)))
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001392 continue;
Paulo Alcantara1023e902021-06-08 13:50:06 -03001393 ref_paths[count++] = kstrdup(ce->path, GFP_ATOMIC);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001394 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001395 }
Paulo Alcantara1023e902021-06-08 13:50:06 -03001396
1397out_unlock:
1398 up_read(&htable_rw_lock);
1399
1400 for (i = 0; i < count; i++) {
1401 char *path = ref_paths[i];
1402 struct dfs_info3_param *refs = NULL;
1403 int numrefs = 0;
1404 int rc = 0;
1405
1406 if (!path)
1407 continue;
1408
1409 ses = find_ipc_from_server_path(sessions, path);
1410 if (IS_ERR(ses))
1411 goto next_referral;
1412
1413 xid = get_xid();
1414 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1415 free_xid(xid);
1416
1417 if (!rc) {
1418 down_write(&htable_rw_lock);
1419 ce = lookup_cache_entry(path);
1420 /*
1421 * We need to re-check it because other tasks might have it deleted or
1422 * updated.
1423 */
1424 if (!IS_ERR(ce) && cache_entry_expired(ce))
1425 update_cache_entry_locked(ce, refs, numrefs);
1426 up_write(&htable_rw_lock);
1427 }
1428
1429next_referral:
1430 kfree(path);
1431 free_dfs_info_array(refs, numrefs);
1432 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001433}
1434
1435/*
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001436 * Worker that will refresh DFS cache and active mounts based on lowest TTL value from a DFS
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001437 * referral.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001438 */
1439static void refresh_cache_worker(struct work_struct *work)
1440{
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001441 struct list_head mglist;
1442 struct mount_group *mg, *tmp_mg;
1443 struct cifs_ses *sessions[CACHE_MAX_ENTRIES + 1] = {NULL};
1444 int max_sessions = ARRAY_SIZE(sessions) - 1;
1445 int i = 0, count;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001446
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001447 INIT_LIST_HEAD(&mglist);
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001448
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001449 /* Get refereces of mount groups */
1450 mutex_lock(&mount_group_list_lock);
1451 list_for_each_entry(mg, &mount_group_list, list) {
1452 kref_get(&mg->refcount);
1453 list_add(&mg->refresh_list, &mglist);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001454 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001455 mutex_unlock(&mount_group_list_lock);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001456
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001457 /* Fill in local array with an NULL-terminated list of all referral server sessions */
1458 list_for_each_entry(mg, &mglist, refresh_list) {
1459 if (i >= max_sessions)
1460 break;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001461
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001462 spin_lock(&mg->lock);
1463 if (i + mg->num_sessions > max_sessions)
1464 count = max_sessions - i;
1465 else
1466 count = mg->num_sessions;
1467 memcpy(&sessions[i], mg->sessions, count * sizeof(mg->sessions[0]));
1468 spin_unlock(&mg->lock);
1469 i += count;
1470 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001471
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001472 if (sessions[0]) {
1473 /* Refresh all active mounts and cached entries */
1474 refresh_mounts(sessions);
1475 refresh_cache(sessions);
1476 }
Paulo Alcantara (SUSE)742d8de2019-12-04 17:38:03 -03001477
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001478 list_for_each_entry_safe(mg, tmp_mg, &mglist, refresh_list) {
1479 list_del_init(&mg->refresh_list);
1480 kref_put(&mg->refcount, mount_group_release);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001481 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001482
1483 spin_lock(&cache_ttl_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001484 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001485 spin_unlock(&cache_ttl_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001486}