blob: 82c5d9a31f9e47f6ac9ec55d12a8d443a7124440 [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
8#include <linux/rcupdate.h>
9#include <linux/rculist.h>
10#include <linux/jhash.h>
11#include <linux/ktime.h>
12#include <linux/slab.h>
13#include <linux/nls.h>
14#include <linux/workqueue.h>
15#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 Alcantara54be1f62018-11-14 16:01:21 -020027
28#define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
29 DFSREF_STORAGE_SERVER))
30
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030031struct cache_dfs_tgt {
32 char *name;
33 struct list_head list;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020034};
35
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030036struct cache_entry {
37 struct hlist_node hlist;
38 const char *path;
39 int ttl;
40 int srvtype;
41 int flags;
42 struct timespec64 etime;
43 int path_consumed;
44 int numtgts;
45 struct list_head tlist;
46 struct cache_dfs_tgt *tgthint;
47 struct rcu_head rcu;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020048};
49
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030050struct vol_info {
51 char *fullpath;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -030052 spinlock_t smb_vol_lock;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030053 struct smb_vol smb_vol;
54 char *mntdata;
55 struct list_head list;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -030056 struct list_head rlist;
57 struct kref refcnt;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020058};
59
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030060static struct kmem_cache *cache_slab __read_mostly;
61static struct workqueue_struct *dfscache_wq __read_mostly;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020062
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030063static int cache_ttl;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -030064static DEFINE_SPINLOCK(cache_ttl_lock);
65
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030066static struct nls_table *cache_nlsc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020067
68/*
69 * Number of entries in the cache
70 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030071static size_t cache_count;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020072
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030073static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
74static DEFINE_MUTEX(list_lock);
75
76static LIST_HEAD(vol_list);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -030077static DEFINE_SPINLOCK(vol_list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -020078
79static void refresh_cache_worker(struct work_struct *work);
80
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030081static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
82
Paulo Alcantara (SUSE)ff2f7fc2019-12-04 17:38:01 -030083static int get_normalized_path(const char *path, char **npath)
Paulo Alcantara54be1f62018-11-14 16:01:21 -020084{
Paulo Alcantara (SUSE)ff2f7fc2019-12-04 17:38:01 -030085 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
86 return -EINVAL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020087
Paulo Alcantara54be1f62018-11-14 16:01:21 -020088 if (*path == '\\') {
89 *npath = (char *)path;
90 } else {
91 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
92 if (!*npath)
93 return -ENOMEM;
94 convert_delimiter(*npath, '\\');
95 }
96 return 0;
97}
98
99static inline void free_normalized_path(const char *path, char *npath)
100{
101 if (path != npath)
102 kfree(npath);
103}
104
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300105static inline bool cache_entry_expired(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200106{
107 struct timespec64 ts;
108
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100109 ktime_get_coarse_real_ts64(&ts);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300110 return timespec64_compare(&ts, &ce->etime) >= 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200111}
112
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300113static inline void free_tgts(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200114{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300115 struct cache_dfs_tgt *t, *n;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200116
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300117 list_for_each_entry_safe(t, n, &ce->tlist, list) {
118 list_del(&t->list);
119 kfree(t->name);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200120 kfree(t);
121 }
122}
123
124static void free_cache_entry(struct rcu_head *rcu)
125{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300126 struct cache_entry *ce = container_of(rcu, struct cache_entry, rcu);
127
128 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200129}
130
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300131static inline void flush_cache_ent(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200132{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300133 if (hlist_unhashed(&ce->hlist))
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200134 return;
135
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300136 hlist_del_init_rcu(&ce->hlist);
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300137 kfree(ce->path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200138 free_tgts(ce);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300139 cache_count--;
140 call_rcu(&ce->rcu, free_cache_entry);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200141}
142
143static void flush_cache_ents(void)
144{
145 int i;
146
147 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300148 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
149 struct hlist_head *l = &cache_htable[i];
150 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200151
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300152 hlist_for_each_entry_rcu(ce, l, hlist)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200153 flush_cache_ent(ce);
154 }
155 rcu_read_unlock();
156}
157
158/*
159 * dfs cache /proc file
160 */
161static int dfscache_proc_show(struct seq_file *m, void *v)
162{
163 int bucket;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300164 struct cache_entry *ce;
165 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200166
167 seq_puts(m, "DFS cache\n---------\n");
168
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300169 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200170
171 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300172 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200173 seq_printf(m,
174 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
175 "interlink=%s,path_consumed=%d,expired=%s\n",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300176 ce->path,
177 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
178 ce->ttl, ce->etime.tv_nsec,
179 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
180 ce->path_consumed,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200181 cache_entry_expired(ce) ? "yes" : "no");
182
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300183 list_for_each_entry(t, &ce->tlist, list) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200184 seq_printf(m, " %s%s\n",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300185 t->name,
186 ce->tgthint == t ? " (target hint)" : "");
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200187 }
188
189 }
190 rcu_read_unlock();
191
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300192 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200193 return 0;
194}
195
196static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
197 size_t count, loff_t *ppos)
198{
199 char c;
200 int rc;
201
202 rc = get_user(c, buffer);
203 if (rc)
204 return rc;
205
206 if (c != '0')
207 return -EINVAL;
208
209 cifs_dbg(FYI, "clearing dfs cache");
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300210 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200211 flush_cache_ents();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300212 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200213
214 return count;
215}
216
217static int dfscache_proc_open(struct inode *inode, struct file *file)
218{
219 return single_open(file, dfscache_proc_show, NULL);
220}
221
222const struct file_operations dfscache_proc_fops = {
223 .open = dfscache_proc_open,
224 .read = seq_read,
225 .llseek = seq_lseek,
226 .release = single_release,
227 .write = dfscache_proc_write,
228};
229
230#ifdef CONFIG_CIFS_DEBUG2
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300231static inline void dump_tgts(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200232{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300233 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200234
235 cifs_dbg(FYI, "target list:\n");
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300236 list_for_each_entry(t, &ce->tlist, list) {
237 cifs_dbg(FYI, " %s%s\n", t->name,
238 ce->tgthint == t ? " (target hint)" : "");
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200239 }
240}
241
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300242static inline void dump_ce(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200243{
244 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300245 "interlink=%s,path_consumed=%d,expired=%s\n", ce->path,
246 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
247 ce->etime.tv_nsec,
248 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
249 ce->path_consumed,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200250 cache_entry_expired(ce) ? "yes" : "no");
251 dump_tgts(ce);
252}
253
254static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
255{
256 int i;
257
258 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
259 for (i = 0; i < numrefs; i++) {
260 const struct dfs_info3_param *ref = &refs[i];
261
262 cifs_dbg(FYI,
263 "\n"
264 "flags: 0x%x\n"
265 "path_consumed: %d\n"
266 "server_type: 0x%x\n"
267 "ref_flag: 0x%x\n"
268 "path_name: %s\n"
269 "node_name: %s\n"
270 "ttl: %d (%dm)\n",
271 ref->flags, ref->path_consumed, ref->server_type,
272 ref->ref_flag, ref->path_name, ref->node_name,
273 ref->ttl, ref->ttl / 60);
274 }
275}
276#else
277#define dump_tgts(e)
278#define dump_ce(e)
279#define dump_refs(r, n)
280#endif
281
282/**
283 * dfs_cache_init - Initialize DFS referral cache.
284 *
285 * Return zero if initialized successfully, otherwise non-zero.
286 */
287int dfs_cache_init(void)
288{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300289 int rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200290 int i;
291
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300292 dfscache_wq = alloc_workqueue("cifs-dfscache",
293 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
294 if (!dfscache_wq)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200295 return -ENOMEM;
296
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300297 cache_slab = kmem_cache_create("cifs_dfs_cache",
298 sizeof(struct cache_entry), 0,
299 SLAB_HWCACHE_ALIGN, NULL);
300 if (!cache_slab) {
301 rc = -ENOMEM;
302 goto out_destroy_wq;
303 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200304
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300305 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
306 INIT_HLIST_HEAD(&cache_htable[i]);
307
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300308 cache_nlsc = load_nls_default();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200309
310 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
311 return 0;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300312
313out_destroy_wq:
314 destroy_workqueue(dfscache_wq);
315 return rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200316}
317
318static inline unsigned int cache_entry_hash(const void *data, int size)
319{
320 unsigned int h;
321
322 h = jhash(data, size, 0);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300323 return h & (CACHE_HTABLE_SIZE - 1);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200324}
325
326/* Check whether second path component of @path is SYSVOL or NETLOGON */
327static inline bool is_sysvol_or_netlogon(const char *path)
328{
329 const char *s;
330 char sep = path[0];
331
332 s = strchr(path + 1, sep) + 1;
333 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
334 !strncasecmp(s, "netlogon", strlen("netlogon"));
335}
336
337/* Return target hint of a DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300338static inline char *get_tgt_name(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 = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200341
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300342 return t ? t->name : ERR_PTR(-ENOENT);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200343}
344
345/* Return expire time out of a new entry's TTL */
346static inline struct timespec64 get_expire_time(int ttl)
347{
348 struct timespec64 ts = {
349 .tv_sec = ttl,
350 .tv_nsec = 0,
351 };
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100352 struct timespec64 now;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200353
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100354 ktime_get_coarse_real_ts64(&now);
355 return timespec64_add(now, ts);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200356}
357
358/* Allocate a new DFS target */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300359static inline struct cache_dfs_tgt *alloc_tgt(const char *name)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200360{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300361 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200362
363 t = kmalloc(sizeof(*t), GFP_KERNEL);
364 if (!t)
365 return ERR_PTR(-ENOMEM);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300366 t->name = kstrndup(name, strlen(name), GFP_KERNEL);
367 if (!t->name) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200368 kfree(t);
369 return ERR_PTR(-ENOMEM);
370 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300371 INIT_LIST_HEAD(&t->list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200372 return t;
373}
374
375/*
376 * Copy DFS referral information to a cache entry and conditionally update
377 * target hint.
378 */
379static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300380 struct cache_entry *ce, const char *tgthint)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200381{
382 int i;
383
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300384 ce->ttl = refs[0].ttl;
385 ce->etime = get_expire_time(ce->ttl);
386 ce->srvtype = refs[0].server_type;
387 ce->flags = refs[0].ref_flag;
388 ce->path_consumed = refs[0].path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200389
390 for (i = 0; i < numrefs; i++) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300391 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200392
393 t = alloc_tgt(refs[i].node_name);
394 if (IS_ERR(t)) {
395 free_tgts(ce);
396 return PTR_ERR(t);
397 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300398 if (tgthint && !strcasecmp(t->name, tgthint)) {
399 list_add(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200400 tgthint = NULL;
401 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300402 list_add_tail(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200403 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300404 ce->numtgts++;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200405 }
406
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300407 ce->tgthint = list_first_entry_or_null(&ce->tlist,
408 struct cache_dfs_tgt, list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200409
410 return 0;
411}
412
413/* Allocate a new cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300414static struct cache_entry *alloc_cache_entry(const char *path,
415 const struct dfs_info3_param *refs,
416 int numrefs)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200417{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300418 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200419 int rc;
420
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300421 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200422 if (!ce)
423 return ERR_PTR(-ENOMEM);
424
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300425 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300426 if (!ce->path) {
427 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200428 return ERR_PTR(-ENOMEM);
429 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300430 INIT_HLIST_NODE(&ce->hlist);
431 INIT_LIST_HEAD(&ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200432
433 rc = copy_ref_data(refs, numrefs, ce, NULL);
434 if (rc) {
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300435 kfree(ce->path);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300436 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200437 ce = ERR_PTR(rc);
438 }
439 return ce;
440}
441
442static void remove_oldest_entry(void)
443{
444 int bucket;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300445 struct cache_entry *ce;
446 struct cache_entry *to_del = NULL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200447
448 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300449 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
450 if (!to_del || timespec64_compare(&ce->etime,
451 &to_del->etime) < 0)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200452 to_del = ce;
453 }
454 if (!to_del) {
455 cifs_dbg(FYI, "%s: no entry to remove", __func__);
456 goto out;
457 }
458 cifs_dbg(FYI, "%s: removing entry", __func__);
459 dump_ce(to_del);
460 flush_cache_ent(to_del);
461out:
462 rcu_read_unlock();
463}
464
465/* Add a new DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300466static inline struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200467add_cache_entry(unsigned int hash, const char *path,
468 const struct dfs_info3_param *refs, int numrefs)
469{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300470 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200471
472 ce = alloc_cache_entry(path, refs, numrefs);
473 if (IS_ERR(ce))
474 return ce;
475
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300476 hlist_add_head_rcu(&ce->hlist, &cache_htable[hash]);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200477
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300478 spin_lock(&cache_ttl_lock);
479 if (!cache_ttl) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300480 cache_ttl = ce->ttl;
481 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200482 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300483 cache_ttl = min_t(int, cache_ttl, ce->ttl);
484 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200485 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300486 spin_unlock(&cache_ttl_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200487
488 return ce;
489}
490
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200491/*
492 * Find a DFS cache entry in hash table and optionally check prefix path against
493 * @path.
494 * Use whole path components in the match.
495 * Return ERR_PTR(-ENOENT) if the entry is not found.
496 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300497static struct cache_entry *lookup_cache_entry(const char *path,
498 unsigned int *hash)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200499{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300500 struct cache_entry *ce;
501 unsigned int h;
502 bool found = false;
503
504 h = cache_entry_hash(path, strlen(path));
505
506 rcu_read_lock();
507 hlist_for_each_entry_rcu(ce, &cache_htable[h], hlist) {
508 if (!strcasecmp(path, ce->path)) {
509 found = true;
510 dump_ce(ce);
511 break;
512 }
513 }
514 rcu_read_unlock();
515
516 if (!found)
517 ce = ERR_PTR(-ENOENT);
518 if (hash)
519 *hash = h;
520
521 return ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200522}
523
524static inline void destroy_slab_cache(void)
525{
526 rcu_barrier();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300527 kmem_cache_destroy(cache_slab);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200528}
529
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300530static void __vol_release(struct vol_info *vi)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200531{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300532 kfree(vi->fullpath);
533 kfree(vi->mntdata);
534 cifs_cleanup_volume_info_contents(&vi->smb_vol);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200535 kfree(vi);
536}
537
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300538static void vol_release(struct kref *kref)
539{
540 struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
541
542 spin_lock(&vol_list_lock);
543 list_del(&vi->list);
544 spin_unlock(&vol_list_lock);
545 __vol_release(vi);
546}
547
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200548static inline void free_vol_list(void)
549{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300550 struct vol_info *vi, *nvi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200551
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -0300552 list_for_each_entry_safe(vi, nvi, &vol_list, list) {
553 list_del_init(&vi->list);
554 __vol_release(vi);
555 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200556}
557
558/**
559 * dfs_cache_destroy - destroy DFS referral cache
560 */
561void dfs_cache_destroy(void)
562{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300563 cancel_delayed_work_sync(&refresh_task);
564 unload_nls(cache_nlsc);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200565 free_vol_list();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200566 flush_cache_ents();
567 destroy_slab_cache();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300568 destroy_workqueue(dfscache_wq);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200569
570 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
571}
572
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300573static inline struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200574__update_cache_entry(const char *path, const struct dfs_info3_param *refs,
575 int numrefs)
576{
577 int rc;
578 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300579 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200580 char *s, *th = NULL;
581
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300582 ce = lookup_cache_entry(path, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200583 if (IS_ERR(ce))
584 return ce;
585
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300586 if (ce->tgthint) {
587 s = ce->tgthint->name;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200588 th = kstrndup(s, strlen(s), GFP_KERNEL);
589 if (!th)
590 return ERR_PTR(-ENOMEM);
591 }
592
593 free_tgts(ce);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300594 ce->numtgts = 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200595
596 rc = copy_ref_data(refs, numrefs, ce, th);
597 kfree(th);
598
599 if (rc)
600 ce = ERR_PTR(rc);
601
602 return ce;
603}
604
605/* Update an expired cache entry by getting a new DFS referral from server */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300606static struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200607update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
608 const struct nls_table *nls_codepage, int remap,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300609 const char *path, struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200610{
611 int rc;
612 struct dfs_info3_param *refs = NULL;
613 int numrefs = 0;
614
615 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
616 /*
617 * Check if caller provided enough parameters to update an expired
618 * entry.
619 */
620 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
621 return ERR_PTR(-ETIME);
622 if (unlikely(!nls_codepage))
623 return ERR_PTR(-ETIME);
624
625 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
626
627 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
628 nls_codepage, remap);
629 if (rc)
630 ce = ERR_PTR(rc);
631 else
632 ce = __update_cache_entry(path, refs, numrefs);
633
634 dump_refs(refs, numrefs);
635 free_dfs_info_array(refs, numrefs);
636
637 return ce;
638}
639
640/*
641 * Find, create or update a DFS cache entry.
642 *
643 * If the entry wasn't found, it will create a new one. Or if it was found but
644 * expired, then it will update the entry accordingly.
645 *
646 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
647 * handle them properly.
648 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300649static struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200650do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
651 const struct nls_table *nls_codepage, int remap,
652 const char *path, bool noreq)
653{
654 int rc;
655 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300656 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200657 struct dfs_info3_param *nrefs;
658 int numnrefs;
659
660 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
661
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300662 ce = lookup_cache_entry(path, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200663 if (IS_ERR(ce)) {
664 cifs_dbg(FYI, "%s: cache miss\n", __func__);
665 /*
666 * If @noreq is set, no requests will be sent to the server for
667 * either updating or getting a new DFS referral.
668 */
669 if (noreq)
670 return ce;
671 /*
672 * No cache entry was found, so check for valid parameters that
673 * will be required to get a new DFS referral and then create a
674 * new cache entry.
675 */
676 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
677 ce = ERR_PTR(-EOPNOTSUPP);
678 return ce;
679 }
680 if (unlikely(!nls_codepage)) {
681 ce = ERR_PTR(-EINVAL);
682 return ce;
683 }
684
685 nrefs = NULL;
686 numnrefs = 0;
687
688 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
689 path);
690
691 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
692 &numnrefs, nls_codepage,
693 remap);
694 if (rc) {
695 ce = ERR_PTR(rc);
696 return ce;
697 }
698
699 dump_refs(nrefs, numnrefs);
700
701 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
702
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300703 if (cache_count >= CACHE_MAX_ENTRIES) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200704 cifs_dbg(FYI, "%s: reached max cache size (%d)",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300705 __func__, CACHE_MAX_ENTRIES);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200706 remove_oldest_entry();
707 }
708 ce = add_cache_entry(h, path, nrefs, numnrefs);
709 free_dfs_info_array(nrefs, numnrefs);
710
711 if (IS_ERR(ce))
712 return ce;
713
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300714 cache_count++;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200715 }
716
717 dump_ce(ce);
718
719 /* Just return the found cache entry in case @noreq is set */
720 if (noreq)
721 return ce;
722
723 if (cache_entry_expired(ce)) {
724 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
725 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
726 ce);
727 if (IS_ERR(ce)) {
728 cifs_dbg(FYI, "%s: failed to update expired entry\n",
729 __func__);
730 }
731 }
732 return ce;
733}
734
735/* Set up a new DFS referral from a given cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300736static int setup_ref(const char *path, const struct cache_entry *ce,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200737 struct dfs_info3_param *ref, const char *tgt)
738{
739 int rc;
740
741 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
742
743 memset(ref, 0, sizeof(*ref));
744
745 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
746 if (!ref->path_name)
747 return -ENOMEM;
748
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300749 ref->path_consumed = ce->path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200750
751 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
752 if (!ref->node_name) {
753 rc = -ENOMEM;
754 goto err_free_path;
755 }
756
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300757 ref->ttl = ce->ttl;
758 ref->server_type = ce->srvtype;
759 ref->ref_flag = ce->flags;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200760
761 return 0;
762
763err_free_path:
764 kfree(ref->path_name);
765 ref->path_name = NULL;
766 return rc;
767}
768
769/* Return target list of a DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300770static int get_tgt_list(const struct cache_entry *ce,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200771 struct dfs_cache_tgt_list *tl)
772{
773 int rc;
774 struct list_head *head = &tl->tl_list;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300775 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200776 struct dfs_cache_tgt_iterator *it, *nit;
777
778 memset(tl, 0, sizeof(*tl));
779 INIT_LIST_HEAD(head);
780
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300781 list_for_each_entry(t, &ce->tlist, list) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200782 it = kzalloc(sizeof(*it), GFP_KERNEL);
783 if (!it) {
784 rc = -ENOMEM;
785 goto err_free_it;
786 }
787
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300788 it->it_name = kstrndup(t->name, strlen(t->name),
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200789 GFP_KERNEL);
790 if (!it->it_name) {
Dan Carpenterc715f892019-01-05 21:18:03 +0300791 kfree(it);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200792 rc = -ENOMEM;
793 goto err_free_it;
794 }
795
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300796 if (ce->tgthint == t)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200797 list_add(&it->it_list, head);
798 else
799 list_add_tail(&it->it_list, head);
800 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300801 tl->tl_numtgts = ce->numtgts;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200802
803 return 0;
804
805err_free_it:
806 list_for_each_entry_safe(it, nit, head, it_list) {
807 kfree(it->it_name);
808 kfree(it);
809 }
810 return rc;
811}
812
813/**
814 * dfs_cache_find - find a DFS cache entry
815 *
816 * If it doesn't find the cache entry, then it will get a DFS referral
817 * for @path and create a new entry.
818 *
819 * In case the cache entry exists but expired, it will get a DFS referral
820 * for @path and then update the respective cache entry.
821 *
822 * These parameters are passed down to the get_dfs_refer() call if it
823 * needs to be issued:
824 * @xid: syscall xid
825 * @ses: smb session to issue the request on
826 * @nls_codepage: charset conversion
827 * @remap: path character remapping type
828 * @path: path to lookup in DFS referral cache.
829 *
830 * @ref: when non-NULL, store single DFS referral result in it.
831 * @tgt_list: when non-NULL, store complete DFS target list in it.
832 *
833 * Return zero if the target was found, otherwise non-zero.
834 */
835int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
836 const struct nls_table *nls_codepage, int remap,
837 const char *path, struct dfs_info3_param *ref,
838 struct dfs_cache_tgt_list *tgt_list)
839{
840 int rc;
841 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300842 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200843
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200844 rc = get_normalized_path(path, &npath);
845 if (rc)
846 return rc;
847
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300848 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200849 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
850 if (!IS_ERR(ce)) {
851 if (ref)
852 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
853 else
854 rc = 0;
855 if (!rc && tgt_list)
856 rc = get_tgt_list(ce, tgt_list);
857 } else {
858 rc = PTR_ERR(ce);
859 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300860 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200861 free_normalized_path(path, npath);
862 return rc;
863}
864
865/**
866 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
867 * the currently connected server.
868 *
869 * NOTE: This function will neither update a cache entry in case it was
870 * expired, nor create a new cache entry if @path hasn't been found. It heavily
871 * relies on an existing cache entry.
872 *
873 * @path: path to lookup in the DFS referral cache.
874 * @ref: when non-NULL, store single DFS referral result in it.
875 * @tgt_list: when non-NULL, store complete DFS target list in it.
876 *
877 * Return 0 if successful.
878 * Return -ENOENT if the entry was not found.
879 * Return non-zero for other errors.
880 */
881int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
882 struct dfs_cache_tgt_list *tgt_list)
883{
884 int rc;
885 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300886 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200887
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200888 rc = get_normalized_path(path, &npath);
889 if (rc)
890 return rc;
891
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300892 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200893 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
894 if (IS_ERR(ce)) {
895 rc = PTR_ERR(ce);
896 goto out;
897 }
898
899 if (ref)
900 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
901 else
902 rc = 0;
903 if (!rc && tgt_list)
904 rc = get_tgt_list(ce, tgt_list);
905out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300906 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200907 free_normalized_path(path, npath);
908 return rc;
909}
910
911/**
912 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
913 *
914 * If it doesn't find the cache entry, then it will get a DFS referral for @path
915 * and create a new entry.
916 *
917 * In case the cache entry exists but expired, it will get a DFS referral
918 * for @path and then update the respective cache entry.
919 *
920 * @xid: syscall id
921 * @ses: smb session
922 * @nls_codepage: charset conversion
923 * @remap: type of character remapping for paths
924 * @path: path to lookup in DFS referral cache.
925 * @it: DFS target iterator
926 *
927 * Return zero if the target hint was updated successfully, otherwise non-zero.
928 */
929int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
930 const struct nls_table *nls_codepage, int remap,
931 const char *path,
932 const struct dfs_cache_tgt_iterator *it)
933{
934 int rc;
935 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300936 struct cache_entry *ce;
937 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200938
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200939 rc = get_normalized_path(path, &npath);
940 if (rc)
941 return rc;
942
943 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
944
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300945 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200946 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
947 if (IS_ERR(ce)) {
948 rc = PTR_ERR(ce);
949 goto out;
950 }
951
952 rc = 0;
953
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300954 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200955
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300956 if (likely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200957 goto out;
958
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300959 list_for_each_entry(t, &ce->tlist, list) {
960 if (!strcasecmp(t->name, it->it_name)) {
961 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200962 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
963 it->it_name);
964 break;
965 }
966 }
967
968out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300969 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200970 free_normalized_path(path, npath);
971 return rc;
972}
973
974/**
975 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
976 * without sending any requests to the currently connected server.
977 *
978 * NOTE: This function will neither update a cache entry in case it was
979 * expired, nor create a new cache entry if @path hasn't been found. It heavily
980 * relies on an existing cache entry.
981 *
982 * @path: path to lookup in DFS referral cache.
983 * @it: target iterator which contains the target hint to update the cache
984 * entry with.
985 *
986 * Return zero if the target hint was updated successfully, otherwise non-zero.
987 */
988int dfs_cache_noreq_update_tgthint(const char *path,
989 const struct dfs_cache_tgt_iterator *it)
990{
991 int rc;
992 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300993 struct cache_entry *ce;
994 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200995
Paulo Alcantara (SUSE)ff2f7fc2019-12-04 17:38:01 -0300996 if (!it)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200997 return -EINVAL;
998
999 rc = get_normalized_path(path, &npath);
1000 if (rc)
1001 return rc;
1002
1003 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1004
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001005 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001006
1007 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1008 if (IS_ERR(ce)) {
1009 rc = PTR_ERR(ce);
1010 goto out;
1011 }
1012
1013 rc = 0;
1014
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001015 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001016
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001017 if (unlikely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001018 goto out;
1019
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001020 list_for_each_entry(t, &ce->tlist, list) {
1021 if (!strcasecmp(t->name, it->it_name)) {
1022 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001023 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1024 it->it_name);
1025 break;
1026 }
1027 }
1028
1029out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001030 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001031 free_normalized_path(path, npath);
1032 return rc;
1033}
1034
1035/**
1036 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1037 * target iterator (@it).
1038 *
1039 * @path: path to lookup in DFS referral cache.
1040 * @it: DFS target iterator.
1041 * @ref: DFS referral pointer to set up the gathered information.
1042 *
1043 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1044 */
1045int dfs_cache_get_tgt_referral(const char *path,
1046 const struct dfs_cache_tgt_iterator *it,
1047 struct dfs_info3_param *ref)
1048{
1049 int rc;
1050 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001051 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001052 unsigned int h;
1053
1054 if (!it || !ref)
1055 return -EINVAL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001056
1057 rc = get_normalized_path(path, &npath);
1058 if (rc)
1059 return rc;
1060
1061 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1062
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001063 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001064
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001065 ce = lookup_cache_entry(npath, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001066 if (IS_ERR(ce)) {
1067 rc = PTR_ERR(ce);
1068 goto out;
1069 }
1070
1071 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1072
1073 rc = setup_ref(path, ce, ref, it->it_name);
1074
1075out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001076 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001077 free_normalized_path(path, npath);
1078 return rc;
1079}
1080
1081static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1082{
1083 memcpy(new, vol, sizeof(*new));
1084
1085 if (vol->username) {
1086 new->username = kstrndup(vol->username, strlen(vol->username),
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001087 GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001088 if (!new->username)
1089 return -ENOMEM;
1090 }
1091 if (vol->password) {
1092 new->password = kstrndup(vol->password, strlen(vol->password),
1093 GFP_KERNEL);
1094 if (!new->password)
1095 goto err_free_username;
1096 }
1097 if (vol->UNC) {
1098 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1099 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1100 if (!new->UNC)
1101 goto err_free_password;
1102 }
1103 if (vol->domainname) {
1104 new->domainname = kstrndup(vol->domainname,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001105 strlen(vol->domainname), GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001106 if (!new->domainname)
1107 goto err_free_unc;
1108 }
1109 if (vol->iocharset) {
1110 new->iocharset = kstrndup(vol->iocharset,
1111 strlen(vol->iocharset), GFP_KERNEL);
1112 if (!new->iocharset)
1113 goto err_free_domainname;
1114 }
1115 if (vol->prepath) {
1116 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1117 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1118 GFP_KERNEL);
1119 if (!new->prepath)
1120 goto err_free_iocharset;
1121 }
1122
1123 return 0;
1124
1125err_free_iocharset:
1126 kfree(new->iocharset);
1127err_free_domainname:
1128 kfree(new->domainname);
1129err_free_unc:
1130 kfree(new->UNC);
1131err_free_password:
Dan Carpenter34bca9b2018-12-20 14:32:43 +03001132 kzfree(new->password);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001133err_free_username:
1134 kfree(new->username);
1135 kfree(new);
1136 return -ENOMEM;
1137}
1138
1139/**
1140 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1141 * DFS cache refresh worker.
1142 *
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001143 * @mntdata: mount data.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001144 * @vol: cifs volume.
1145 * @fullpath: origin full path.
1146 *
1147 * Return zero if volume was set up correctly, otherwise non-zero.
1148 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001149int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001150{
1151 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001152 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001153
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001154 if (!vol || !fullpath || !mntdata)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001155 return -EINVAL;
1156
1157 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1158
1159 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1160 if (!vi)
1161 return -ENOMEM;
1162
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001163 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1164 if (!vi->fullpath) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001165 rc = -ENOMEM;
1166 goto err_free_vi;
1167 }
1168
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001169 rc = dup_vol(vol, &vi->smb_vol);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001170 if (rc)
1171 goto err_free_fullpath;
1172
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001173 vi->mntdata = mntdata;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001174 spin_lock_init(&vi->smb_vol_lock);
1175 kref_init(&vi->refcnt);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001176
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001177 spin_lock(&vol_list_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001178 list_add_tail(&vi->list, &vol_list);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001179 spin_unlock(&vol_list_lock);
1180
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001181 return 0;
1182
1183err_free_fullpath:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001184 kfree(vi->fullpath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001185err_free_vi:
1186 kfree(vi);
1187 return rc;
1188}
1189
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001190/* Must be called with vol_list_lock held */
1191static struct vol_info *find_vol(const char *fullpath)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001192{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001193 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001194
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001195 list_for_each_entry(vi, &vol_list, list) {
1196 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1197 if (!strcasecmp(vi->fullpath, fullpath))
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001198 return vi;
1199 }
1200 return ERR_PTR(-ENOENT);
1201}
1202
1203/**
1204 * dfs_cache_update_vol - update vol info in DFS cache after failover
1205 *
1206 * @fullpath: fullpath to look up in volume list.
1207 * @server: TCP ses pointer.
1208 *
1209 * Return zero if volume was updated, otherwise non-zero.
1210 */
1211int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1212{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001213 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001214
1215 if (!fullpath || !server)
1216 return -EINVAL;
1217
1218 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1219
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001220 spin_lock(&vol_list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001221 vi = find_vol(fullpath);
1222 if (IS_ERR(vi)) {
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001223 spin_unlock(&vol_list_lock);
1224 return PTR_ERR(vi);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001225 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001226 kref_get(&vi->refcnt);
1227 spin_unlock(&vol_list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001228
1229 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001230 spin_lock(&vi->smb_vol_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001231 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1232 sizeof(vi->smb_vol.dstaddr));
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001233 spin_unlock(&vi->smb_vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001234
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001235 kref_put(&vi->refcnt, vol_release);
1236
1237 return 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001238}
1239
1240/**
1241 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1242 *
1243 * @fullpath: fullpath to look up in volume list.
1244 */
1245void dfs_cache_del_vol(const char *fullpath)
1246{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001247 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001248
1249 if (!fullpath || !*fullpath)
1250 return;
1251
1252 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1253
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001254 spin_lock(&vol_list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001255 vi = find_vol(fullpath);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001256 spin_unlock(&vol_list_lock);
1257
1258 kref_put(&vi->refcnt, vol_release);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001259}
1260
1261/* Get all tcons that are within a DFS namespace and can be refreshed */
1262static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1263{
1264 struct cifs_ses *ses;
1265 struct cifs_tcon *tcon;
1266
1267 INIT_LIST_HEAD(head);
1268
1269 spin_lock(&cifs_tcp_ses_lock);
1270 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1271 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1272 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1273 tcon->dfs_path) {
1274 tcon->tc_count++;
1275 list_add_tail(&tcon->ulist, head);
1276 }
1277 }
1278 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1279 ses->tcon_ipc->dfs_path) {
1280 list_add_tail(&ses->tcon_ipc->ulist, head);
1281 }
1282 }
1283 spin_unlock(&cifs_tcp_ses_lock);
1284}
1285
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001286static bool is_dfs_link(const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001287{
1288 char *s;
1289
1290 s = strchr(path + 1, '\\');
1291 if (!s)
1292 return false;
1293 return !!strchr(s + 1, '\\');
1294}
1295
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001296static char *get_dfs_root(const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001297{
1298 char *s, *npath;
1299
1300 s = strchr(path + 1, '\\');
1301 if (!s)
1302 return ERR_PTR(-EINVAL);
1303
1304 s = strchr(s + 1, '\\');
1305 if (!s)
1306 return ERR_PTR(-EINVAL);
1307
1308 npath = kstrndup(path, s - path, GFP_KERNEL);
1309 if (!npath)
1310 return ERR_PTR(-ENOMEM);
1311
1312 return npath;
1313}
1314
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001315static inline void put_tcp_server(struct TCP_Server_Info *server)
1316{
1317 cifs_put_tcp_session(server, 0);
1318}
1319
1320static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1321{
1322 struct TCP_Server_Info *server;
1323
1324 server = cifs_find_tcp_session(vol);
1325 if (IS_ERR_OR_NULL(server))
1326 return NULL;
1327
1328 spin_lock(&GlobalMid_Lock);
1329 if (server->tcpStatus != CifsGood) {
1330 spin_unlock(&GlobalMid_Lock);
1331 put_tcp_server(server);
1332 return NULL;
1333 }
1334 spin_unlock(&GlobalMid_Lock);
1335
1336 return server;
1337}
1338
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001339/* Find root SMB session out of a DFS link path */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001340static struct cifs_ses *find_root_ses(struct vol_info *vi,
1341 struct cifs_tcon *tcon,
1342 const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001343{
1344 char *rpath;
1345 int rc;
1346 struct dfs_info3_param ref = {0};
1347 char *mdata = NULL, *devname = NULL;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001348 struct TCP_Server_Info *server;
1349 struct cifs_ses *ses;
1350 struct smb_vol vol;
1351
1352 rpath = get_dfs_root(path);
1353 if (IS_ERR(rpath))
1354 return ERR_CAST(rpath);
1355
1356 memset(&vol, 0, sizeof(vol));
1357
1358 rc = dfs_cache_noreq_find(rpath, &ref, NULL);
1359 if (rc) {
1360 ses = ERR_PTR(rc);
1361 goto out;
1362 }
1363
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001364 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref, &devname);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001365 free_dfs_info_param(&ref);
1366
1367 if (IS_ERR(mdata)) {
1368 ses = ERR_CAST(mdata);
1369 mdata = NULL;
1370 goto out;
1371 }
1372
Paulo Alcantara (SUSE)df3df922019-11-22 12:30:52 -03001373 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001374 kfree(devname);
1375
1376 if (rc) {
1377 ses = ERR_PTR(rc);
1378 goto out;
1379 }
1380
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001381 server = get_tcp_server(&vol);
1382 if (!server) {
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001383 ses = ERR_PTR(-EHOSTDOWN);
1384 goto out;
1385 }
1386
1387 ses = cifs_get_smb_ses(server, &vol);
1388
1389out:
1390 cifs_cleanup_volume_info_contents(&vol);
1391 kfree(mdata);
1392 kfree(rpath);
1393
1394 return ses;
1395}
1396
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001397/* Refresh DFS cache entry from a given tcon */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001398static void refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001399{
1400 int rc = 0;
1401 unsigned int xid;
1402 char *path, *npath;
1403 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001404 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001405 struct dfs_info3_param *refs = NULL;
1406 int numrefs = 0;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001407 struct cifs_ses *root_ses = NULL, *ses;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001408
1409 xid = get_xid();
1410
1411 path = tcon->dfs_path + 1;
1412
1413 rc = get_normalized_path(path, &npath);
1414 if (rc)
1415 goto out;
1416
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001417 mutex_lock(&list_lock);
1418 ce = lookup_cache_entry(npath, &h);
1419 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001420
1421 if (IS_ERR(ce)) {
1422 rc = PTR_ERR(ce);
1423 goto out;
1424 }
1425
1426 if (!cache_entry_expired(ce))
1427 goto out;
1428
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001429 /* If it's a DFS Link, then use root SMB session for refreshing it */
1430 if (is_dfs_link(npath)) {
1431 ses = root_ses = find_root_ses(vi, tcon, npath);
1432 if (IS_ERR(ses)) {
1433 rc = PTR_ERR(ses);
1434 root_ses = NULL;
1435 goto out;
1436 }
1437 } else {
1438 ses = tcon->ses;
1439 }
1440
1441 if (unlikely(!ses->server->ops->get_dfs_refer)) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001442 rc = -EOPNOTSUPP;
1443 } else {
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001444 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001445 &numrefs, cache_nlsc,
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001446 tcon->remap);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001447 if (!rc) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001448 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001449 ce = __update_cache_entry(npath, refs, numrefs);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001450 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001451 dump_refs(refs, numrefs);
1452 free_dfs_info_array(refs, numrefs);
1453 if (IS_ERR(ce))
1454 rc = PTR_ERR(ce);
1455 }
1456 }
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001457
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001458out:
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001459 if (root_ses)
1460 cifs_put_smb_ses(root_ses);
1461
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001462 free_xid(xid);
1463 free_normalized_path(path, npath);
1464}
1465
1466/*
1467 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1468 * referral.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001469 */
1470static void refresh_cache_worker(struct work_struct *work)
1471{
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001472 struct vol_info *vi, *nvi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001473 struct TCP_Server_Info *server;
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001474 LIST_HEAD(vols);
1475 LIST_HEAD(tcons);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001476 struct cifs_tcon *tcon, *ntcon;
1477
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001478 /*
1479 * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1480 * for refreshing.
1481 */
1482 spin_lock(&vol_list_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001483 list_for_each_entry(vi, &vol_list, list) {
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001484 server = get_tcp_server(&vi->smb_vol);
1485 if (!server)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001486 continue;
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001487
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001488 kref_get(&vi->refcnt);
1489 list_add_tail(&vi->rlist, &vols);
1490 put_tcp_server(server);
1491 }
1492 spin_unlock(&vol_list_lock);
1493
1494 /* Walk through all TCONs and refresh any expired cache entry */
1495 list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1496 spin_lock(&vi->smb_vol_lock);
1497 server = get_tcp_server(&vi->smb_vol);
1498 spin_unlock(&vi->smb_vol_lock);
1499
1500 if (!server)
1501 goto next_vol;
1502
1503 get_tcons(server, &tcons);
1504 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001505 refresh_tcon(vi, tcon);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001506 list_del_init(&tcon->ulist);
1507 cifs_put_tcon(tcon);
1508 }
Paulo Alcantara (SUSE)345c1a42019-12-04 17:38:00 -03001509
1510 put_tcp_server(server);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001511
1512next_vol:
1513 list_del_init(&vi->rlist);
1514 kref_put(&vi->refcnt, vol_release);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001515 }
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001516
1517 spin_lock(&cache_ttl_lock);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001518 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara (SUSE)06d57372019-12-04 17:38:02 -03001519 spin_unlock(&cache_ttl_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001520}