blob: 49c5f045270fda4292fb10daa54417dd784efa4a [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;
52 struct smb_vol smb_vol;
53 char *mntdata;
54 struct list_head list;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020055};
56
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030057static struct kmem_cache *cache_slab __read_mostly;
58static struct workqueue_struct *dfscache_wq __read_mostly;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020059
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030060static int cache_ttl;
61static struct nls_table *cache_nlsc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020062
63/*
64 * Number of entries in the cache
65 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030066static size_t cache_count;
Paulo Alcantara54be1f62018-11-14 16:01:21 -020067
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030068static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
69static DEFINE_MUTEX(list_lock);
70
71static LIST_HEAD(vol_list);
72static DEFINE_MUTEX(vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -020073
74static void refresh_cache_worker(struct work_struct *work);
75
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -030076static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
77
Paulo Alcantara54be1f62018-11-14 16:01:21 -020078static inline bool is_path_valid(const char *path)
79{
80 return path && (strchr(path + 1, '\\') || strchr(path + 1, '/'));
81}
82
83static inline int get_normalized_path(const char *path, char **npath)
84{
85 if (*path == '\\') {
86 *npath = (char *)path;
87 } else {
88 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
89 if (!*npath)
90 return -ENOMEM;
91 convert_delimiter(*npath, '\\');
92 }
93 return 0;
94}
95
96static inline void free_normalized_path(const char *path, char *npath)
97{
98 if (path != npath)
99 kfree(npath);
100}
101
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300102static inline bool cache_entry_expired(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200103{
104 struct timespec64 ts;
105
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100106 ktime_get_coarse_real_ts64(&ts);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300107 return timespec64_compare(&ts, &ce->etime) >= 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200108}
109
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300110static inline void free_tgts(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200111{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300112 struct cache_dfs_tgt *t, *n;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200113
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300114 list_for_each_entry_safe(t, n, &ce->tlist, list) {
115 list_del(&t->list);
116 kfree(t->name);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200117 kfree(t);
118 }
119}
120
121static void free_cache_entry(struct rcu_head *rcu)
122{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300123 struct cache_entry *ce = container_of(rcu, struct cache_entry, rcu);
124
125 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200126}
127
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300128static inline void flush_cache_ent(struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200129{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300130 if (hlist_unhashed(&ce->hlist))
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200131 return;
132
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300133 hlist_del_init_rcu(&ce->hlist);
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300134 kfree(ce->path);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200135 free_tgts(ce);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300136 cache_count--;
137 call_rcu(&ce->rcu, free_cache_entry);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200138}
139
140static void flush_cache_ents(void)
141{
142 int i;
143
144 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300145 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
146 struct hlist_head *l = &cache_htable[i];
147 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200148
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300149 hlist_for_each_entry_rcu(ce, l, hlist)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200150 flush_cache_ent(ce);
151 }
152 rcu_read_unlock();
153}
154
155/*
156 * dfs cache /proc file
157 */
158static int dfscache_proc_show(struct seq_file *m, void *v)
159{
160 int bucket;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300161 struct cache_entry *ce;
162 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200163
164 seq_puts(m, "DFS cache\n---------\n");
165
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300166 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200167
168 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300169 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200170 seq_printf(m,
171 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
172 "interlink=%s,path_consumed=%d,expired=%s\n",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300173 ce->path,
174 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
175 ce->ttl, ce->etime.tv_nsec,
176 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
177 ce->path_consumed,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200178 cache_entry_expired(ce) ? "yes" : "no");
179
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300180 list_for_each_entry(t, &ce->tlist, list) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200181 seq_printf(m, " %s%s\n",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300182 t->name,
183 ce->tgthint == t ? " (target hint)" : "");
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200184 }
185
186 }
187 rcu_read_unlock();
188
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300189 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200190 return 0;
191}
192
193static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
194 size_t count, loff_t *ppos)
195{
196 char c;
197 int rc;
198
199 rc = get_user(c, buffer);
200 if (rc)
201 return rc;
202
203 if (c != '0')
204 return -EINVAL;
205
206 cifs_dbg(FYI, "clearing dfs cache");
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300207 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200208 flush_cache_ents();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300209 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200210
211 return count;
212}
213
214static int dfscache_proc_open(struct inode *inode, struct file *file)
215{
216 return single_open(file, dfscache_proc_show, NULL);
217}
218
219const struct file_operations dfscache_proc_fops = {
220 .open = dfscache_proc_open,
221 .read = seq_read,
222 .llseek = seq_lseek,
223 .release = single_release,
224 .write = dfscache_proc_write,
225};
226
227#ifdef CONFIG_CIFS_DEBUG2
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300228static inline void dump_tgts(const 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;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200231
232 cifs_dbg(FYI, "target list:\n");
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300233 list_for_each_entry(t, &ce->tlist, list) {
234 cifs_dbg(FYI, " %s%s\n", t->name,
235 ce->tgthint == t ? " (target hint)" : "");
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200236 }
237}
238
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300239static inline void dump_ce(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200240{
241 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300242 "interlink=%s,path_consumed=%d,expired=%s\n", ce->path,
243 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
244 ce->etime.tv_nsec,
245 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
246 ce->path_consumed,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200247 cache_entry_expired(ce) ? "yes" : "no");
248 dump_tgts(ce);
249}
250
251static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
252{
253 int i;
254
255 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
256 for (i = 0; i < numrefs; i++) {
257 const struct dfs_info3_param *ref = &refs[i];
258
259 cifs_dbg(FYI,
260 "\n"
261 "flags: 0x%x\n"
262 "path_consumed: %d\n"
263 "server_type: 0x%x\n"
264 "ref_flag: 0x%x\n"
265 "path_name: %s\n"
266 "node_name: %s\n"
267 "ttl: %d (%dm)\n",
268 ref->flags, ref->path_consumed, ref->server_type,
269 ref->ref_flag, ref->path_name, ref->node_name,
270 ref->ttl, ref->ttl / 60);
271 }
272}
273#else
274#define dump_tgts(e)
275#define dump_ce(e)
276#define dump_refs(r, n)
277#endif
278
279/**
280 * dfs_cache_init - Initialize DFS referral cache.
281 *
282 * Return zero if initialized successfully, otherwise non-zero.
283 */
284int dfs_cache_init(void)
285{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300286 int rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200287 int i;
288
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300289 dfscache_wq = alloc_workqueue("cifs-dfscache",
290 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
291 if (!dfscache_wq)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200292 return -ENOMEM;
293
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300294 cache_slab = kmem_cache_create("cifs_dfs_cache",
295 sizeof(struct cache_entry), 0,
296 SLAB_HWCACHE_ALIGN, NULL);
297 if (!cache_slab) {
298 rc = -ENOMEM;
299 goto out_destroy_wq;
300 }
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200301
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300302 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
303 INIT_HLIST_HEAD(&cache_htable[i]);
304
305 cache_ttl = -1;
306 cache_nlsc = load_nls_default();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200307
308 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
309 return 0;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300310
311out_destroy_wq:
312 destroy_workqueue(dfscache_wq);
313 return rc;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200314}
315
316static inline unsigned int cache_entry_hash(const void *data, int size)
317{
318 unsigned int h;
319
320 h = jhash(data, size, 0);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300321 return h & (CACHE_HTABLE_SIZE - 1);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200322}
323
324/* Check whether second path component of @path is SYSVOL or NETLOGON */
325static inline bool is_sysvol_or_netlogon(const char *path)
326{
327 const char *s;
328 char sep = path[0];
329
330 s = strchr(path + 1, sep) + 1;
331 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
332 !strncasecmp(s, "netlogon", strlen("netlogon"));
333}
334
335/* Return target hint of a DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300336static inline char *get_tgt_name(const struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200337{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300338 struct cache_dfs_tgt *t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200339
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300340 return t ? t->name : ERR_PTR(-ENOENT);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200341}
342
343/* Return expire time out of a new entry's TTL */
344static inline struct timespec64 get_expire_time(int ttl)
345{
346 struct timespec64 ts = {
347 .tv_sec = ttl,
348 .tv_nsec = 0,
349 };
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100350 struct timespec64 now;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200351
Stephen Rothwell54e4f732018-12-17 20:11:46 +1100352 ktime_get_coarse_real_ts64(&now);
353 return timespec64_add(now, ts);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200354}
355
356/* Allocate a new DFS target */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300357static inline struct cache_dfs_tgt *alloc_tgt(const char *name)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200358{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300359 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200360
361 t = kmalloc(sizeof(*t), GFP_KERNEL);
362 if (!t)
363 return ERR_PTR(-ENOMEM);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300364 t->name = kstrndup(name, strlen(name), GFP_KERNEL);
365 if (!t->name) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200366 kfree(t);
367 return ERR_PTR(-ENOMEM);
368 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300369 INIT_LIST_HEAD(&t->list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200370 return t;
371}
372
373/*
374 * Copy DFS referral information to a cache entry and conditionally update
375 * target hint.
376 */
377static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300378 struct cache_entry *ce, const char *tgthint)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200379{
380 int i;
381
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300382 ce->ttl = refs[0].ttl;
383 ce->etime = get_expire_time(ce->ttl);
384 ce->srvtype = refs[0].server_type;
385 ce->flags = refs[0].ref_flag;
386 ce->path_consumed = refs[0].path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200387
388 for (i = 0; i < numrefs; i++) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300389 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200390
391 t = alloc_tgt(refs[i].node_name);
392 if (IS_ERR(t)) {
393 free_tgts(ce);
394 return PTR_ERR(t);
395 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300396 if (tgthint && !strcasecmp(t->name, tgthint)) {
397 list_add(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200398 tgthint = NULL;
399 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300400 list_add_tail(&t->list, &ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200401 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300402 ce->numtgts++;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200403 }
404
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300405 ce->tgthint = list_first_entry_or_null(&ce->tlist,
406 struct cache_dfs_tgt, list);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200407
408 return 0;
409}
410
411/* Allocate a new cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300412static struct cache_entry *alloc_cache_entry(const char *path,
413 const struct dfs_info3_param *refs,
414 int numrefs)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200415{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300416 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200417 int rc;
418
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300419 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200420 if (!ce)
421 return ERR_PTR(-ENOMEM);
422
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300423 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300424 if (!ce->path) {
425 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200426 return ERR_PTR(-ENOMEM);
427 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300428 INIT_HLIST_NODE(&ce->hlist);
429 INIT_LIST_HEAD(&ce->tlist);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200430
431 rc = copy_ref_data(refs, numrefs, ce, NULL);
432 if (rc) {
Paulo Alcantara (SUSE)199c6bd2019-12-04 17:37:59 -0300433 kfree(ce->path);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300434 kmem_cache_free(cache_slab, ce);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200435 ce = ERR_PTR(rc);
436 }
437 return ce;
438}
439
440static void remove_oldest_entry(void)
441{
442 int bucket;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300443 struct cache_entry *ce;
444 struct cache_entry *to_del = NULL;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200445
446 rcu_read_lock();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300447 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
448 if (!to_del || timespec64_compare(&ce->etime,
449 &to_del->etime) < 0)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200450 to_del = ce;
451 }
452 if (!to_del) {
453 cifs_dbg(FYI, "%s: no entry to remove", __func__);
454 goto out;
455 }
456 cifs_dbg(FYI, "%s: removing entry", __func__);
457 dump_ce(to_del);
458 flush_cache_ent(to_del);
459out:
460 rcu_read_unlock();
461}
462
463/* Add a new DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300464static inline struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200465add_cache_entry(unsigned int hash, const char *path,
466 const struct dfs_info3_param *refs, int numrefs)
467{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300468 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200469
470 ce = alloc_cache_entry(path, refs, numrefs);
471 if (IS_ERR(ce))
472 return ce;
473
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300474 hlist_add_head_rcu(&ce->hlist, &cache_htable[hash]);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200475
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300476 mutex_lock(&vol_lock);
477 if (cache_ttl < 0) {
478 cache_ttl = ce->ttl;
479 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200480 } else {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300481 cache_ttl = min_t(int, cache_ttl, ce->ttl);
482 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200483 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300484 mutex_unlock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200485
486 return ce;
487}
488
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200489/*
490 * Find a DFS cache entry in hash table and optionally check prefix path against
491 * @path.
492 * Use whole path components in the match.
493 * Return ERR_PTR(-ENOENT) if the entry is not found.
494 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300495static struct cache_entry *lookup_cache_entry(const char *path,
496 unsigned int *hash)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200497{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300498 struct cache_entry *ce;
499 unsigned int h;
500 bool found = false;
501
502 h = cache_entry_hash(path, strlen(path));
503
504 rcu_read_lock();
505 hlist_for_each_entry_rcu(ce, &cache_htable[h], hlist) {
506 if (!strcasecmp(path, ce->path)) {
507 found = true;
508 dump_ce(ce);
509 break;
510 }
511 }
512 rcu_read_unlock();
513
514 if (!found)
515 ce = ERR_PTR(-ENOENT);
516 if (hash)
517 *hash = h;
518
519 return ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200520}
521
522static inline void destroy_slab_cache(void)
523{
524 rcu_barrier();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300525 kmem_cache_destroy(cache_slab);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200526}
527
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300528static inline void free_vol(struct vol_info *vi)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200529{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300530 list_del(&vi->list);
531 kfree(vi->fullpath);
532 kfree(vi->mntdata);
533 cifs_cleanup_volume_info_contents(&vi->smb_vol);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200534 kfree(vi);
535}
536
537static inline void free_vol_list(void)
538{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300539 struct vol_info *vi, *nvi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200540
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300541 list_for_each_entry_safe(vi, nvi, &vol_list, list)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200542 free_vol(vi);
543}
544
545/**
546 * dfs_cache_destroy - destroy DFS referral cache
547 */
548void dfs_cache_destroy(void)
549{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300550 cancel_delayed_work_sync(&refresh_task);
551 unload_nls(cache_nlsc);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200552 free_vol_list();
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200553 flush_cache_ents();
554 destroy_slab_cache();
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300555 destroy_workqueue(dfscache_wq);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200556
557 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
558}
559
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300560static inline struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200561__update_cache_entry(const char *path, const struct dfs_info3_param *refs,
562 int numrefs)
563{
564 int rc;
565 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300566 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200567 char *s, *th = NULL;
568
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300569 ce = lookup_cache_entry(path, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200570 if (IS_ERR(ce))
571 return ce;
572
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300573 if (ce->tgthint) {
574 s = ce->tgthint->name;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200575 th = kstrndup(s, strlen(s), GFP_KERNEL);
576 if (!th)
577 return ERR_PTR(-ENOMEM);
578 }
579
580 free_tgts(ce);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300581 ce->numtgts = 0;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200582
583 rc = copy_ref_data(refs, numrefs, ce, th);
584 kfree(th);
585
586 if (rc)
587 ce = ERR_PTR(rc);
588
589 return ce;
590}
591
592/* Update an expired cache entry by getting a new DFS referral from server */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300593static struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200594update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
595 const struct nls_table *nls_codepage, int remap,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300596 const char *path, struct cache_entry *ce)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200597{
598 int rc;
599 struct dfs_info3_param *refs = NULL;
600 int numrefs = 0;
601
602 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
603 /*
604 * Check if caller provided enough parameters to update an expired
605 * entry.
606 */
607 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
608 return ERR_PTR(-ETIME);
609 if (unlikely(!nls_codepage))
610 return ERR_PTR(-ETIME);
611
612 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
613
614 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
615 nls_codepage, remap);
616 if (rc)
617 ce = ERR_PTR(rc);
618 else
619 ce = __update_cache_entry(path, refs, numrefs);
620
621 dump_refs(refs, numrefs);
622 free_dfs_info_array(refs, numrefs);
623
624 return ce;
625}
626
627/*
628 * Find, create or update a DFS cache entry.
629 *
630 * If the entry wasn't found, it will create a new one. Or if it was found but
631 * expired, then it will update the entry accordingly.
632 *
633 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
634 * handle them properly.
635 */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300636static struct cache_entry *
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200637do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
638 const struct nls_table *nls_codepage, int remap,
639 const char *path, bool noreq)
640{
641 int rc;
642 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300643 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200644 struct dfs_info3_param *nrefs;
645 int numnrefs;
646
647 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
648
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300649 ce = lookup_cache_entry(path, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200650 if (IS_ERR(ce)) {
651 cifs_dbg(FYI, "%s: cache miss\n", __func__);
652 /*
653 * If @noreq is set, no requests will be sent to the server for
654 * either updating or getting a new DFS referral.
655 */
656 if (noreq)
657 return ce;
658 /*
659 * No cache entry was found, so check for valid parameters that
660 * will be required to get a new DFS referral and then create a
661 * new cache entry.
662 */
663 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
664 ce = ERR_PTR(-EOPNOTSUPP);
665 return ce;
666 }
667 if (unlikely(!nls_codepage)) {
668 ce = ERR_PTR(-EINVAL);
669 return ce;
670 }
671
672 nrefs = NULL;
673 numnrefs = 0;
674
675 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
676 path);
677
678 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
679 &numnrefs, nls_codepage,
680 remap);
681 if (rc) {
682 ce = ERR_PTR(rc);
683 return ce;
684 }
685
686 dump_refs(nrefs, numnrefs);
687
688 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
689
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300690 if (cache_count >= CACHE_MAX_ENTRIES) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200691 cifs_dbg(FYI, "%s: reached max cache size (%d)",
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300692 __func__, CACHE_MAX_ENTRIES);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200693 remove_oldest_entry();
694 }
695 ce = add_cache_entry(h, path, nrefs, numnrefs);
696 free_dfs_info_array(nrefs, numnrefs);
697
698 if (IS_ERR(ce))
699 return ce;
700
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300701 cache_count++;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200702 }
703
704 dump_ce(ce);
705
706 /* Just return the found cache entry in case @noreq is set */
707 if (noreq)
708 return ce;
709
710 if (cache_entry_expired(ce)) {
711 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
712 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
713 ce);
714 if (IS_ERR(ce)) {
715 cifs_dbg(FYI, "%s: failed to update expired entry\n",
716 __func__);
717 }
718 }
719 return ce;
720}
721
722/* Set up a new DFS referral from a given cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300723static int setup_ref(const char *path, const struct cache_entry *ce,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200724 struct dfs_info3_param *ref, const char *tgt)
725{
726 int rc;
727
728 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
729
730 memset(ref, 0, sizeof(*ref));
731
732 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
733 if (!ref->path_name)
734 return -ENOMEM;
735
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300736 ref->path_consumed = ce->path_consumed;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200737
738 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
739 if (!ref->node_name) {
740 rc = -ENOMEM;
741 goto err_free_path;
742 }
743
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300744 ref->ttl = ce->ttl;
745 ref->server_type = ce->srvtype;
746 ref->ref_flag = ce->flags;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200747
748 return 0;
749
750err_free_path:
751 kfree(ref->path_name);
752 ref->path_name = NULL;
753 return rc;
754}
755
756/* Return target list of a DFS cache entry */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300757static int get_tgt_list(const struct cache_entry *ce,
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200758 struct dfs_cache_tgt_list *tl)
759{
760 int rc;
761 struct list_head *head = &tl->tl_list;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300762 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200763 struct dfs_cache_tgt_iterator *it, *nit;
764
765 memset(tl, 0, sizeof(*tl));
766 INIT_LIST_HEAD(head);
767
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300768 list_for_each_entry(t, &ce->tlist, list) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200769 it = kzalloc(sizeof(*it), GFP_KERNEL);
770 if (!it) {
771 rc = -ENOMEM;
772 goto err_free_it;
773 }
774
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300775 it->it_name = kstrndup(t->name, strlen(t->name),
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200776 GFP_KERNEL);
777 if (!it->it_name) {
Dan Carpenterc715f892019-01-05 21:18:03 +0300778 kfree(it);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200779 rc = -ENOMEM;
780 goto err_free_it;
781 }
782
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300783 if (ce->tgthint == t)
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200784 list_add(&it->it_list, head);
785 else
786 list_add_tail(&it->it_list, head);
787 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300788 tl->tl_numtgts = ce->numtgts;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200789
790 return 0;
791
792err_free_it:
793 list_for_each_entry_safe(it, nit, head, it_list) {
794 kfree(it->it_name);
795 kfree(it);
796 }
797 return rc;
798}
799
800/**
801 * dfs_cache_find - find a DFS cache entry
802 *
803 * If it doesn't find the cache entry, then it will get a DFS referral
804 * for @path and create a new entry.
805 *
806 * In case the cache entry exists but expired, it will get a DFS referral
807 * for @path and then update the respective cache entry.
808 *
809 * These parameters are passed down to the get_dfs_refer() call if it
810 * needs to be issued:
811 * @xid: syscall xid
812 * @ses: smb session to issue the request on
813 * @nls_codepage: charset conversion
814 * @remap: path character remapping type
815 * @path: path to lookup in DFS referral cache.
816 *
817 * @ref: when non-NULL, store single DFS referral result in it.
818 * @tgt_list: when non-NULL, store complete DFS target list in it.
819 *
820 * Return zero if the target was found, otherwise non-zero.
821 */
822int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
823 const struct nls_table *nls_codepage, int remap,
824 const char *path, struct dfs_info3_param *ref,
825 struct dfs_cache_tgt_list *tgt_list)
826{
827 int rc;
828 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300829 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200830
831 if (unlikely(!is_path_valid(path)))
832 return -EINVAL;
833
834 rc = get_normalized_path(path, &npath);
835 if (rc)
836 return rc;
837
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300838 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200839 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
840 if (!IS_ERR(ce)) {
841 if (ref)
842 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
843 else
844 rc = 0;
845 if (!rc && tgt_list)
846 rc = get_tgt_list(ce, tgt_list);
847 } else {
848 rc = PTR_ERR(ce);
849 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300850 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200851 free_normalized_path(path, npath);
852 return rc;
853}
854
855/**
856 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
857 * the currently connected server.
858 *
859 * NOTE: This function will neither update a cache entry in case it was
860 * expired, nor create a new cache entry if @path hasn't been found. It heavily
861 * relies on an existing cache entry.
862 *
863 * @path: path to lookup in the DFS referral cache.
864 * @ref: when non-NULL, store single DFS referral result in it.
865 * @tgt_list: when non-NULL, store complete DFS target list in it.
866 *
867 * Return 0 if successful.
868 * Return -ENOENT if the entry was not found.
869 * Return non-zero for other errors.
870 */
871int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
872 struct dfs_cache_tgt_list *tgt_list)
873{
874 int rc;
875 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300876 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200877
878 if (unlikely(!is_path_valid(path)))
879 return -EINVAL;
880
881 rc = get_normalized_path(path, &npath);
882 if (rc)
883 return rc;
884
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300885 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200886 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
887 if (IS_ERR(ce)) {
888 rc = PTR_ERR(ce);
889 goto out;
890 }
891
892 if (ref)
893 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
894 else
895 rc = 0;
896 if (!rc && tgt_list)
897 rc = get_tgt_list(ce, tgt_list);
898out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300899 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200900 free_normalized_path(path, npath);
901 return rc;
902}
903
904/**
905 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
906 *
907 * If it doesn't find the cache entry, then it will get a DFS referral for @path
908 * and create a new entry.
909 *
910 * In case the cache entry exists but expired, it will get a DFS referral
911 * for @path and then update the respective cache entry.
912 *
913 * @xid: syscall id
914 * @ses: smb session
915 * @nls_codepage: charset conversion
916 * @remap: type of character remapping for paths
917 * @path: path to lookup in DFS referral cache.
918 * @it: DFS target iterator
919 *
920 * Return zero if the target hint was updated successfully, otherwise non-zero.
921 */
922int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
923 const struct nls_table *nls_codepage, int remap,
924 const char *path,
925 const struct dfs_cache_tgt_iterator *it)
926{
927 int rc;
928 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300929 struct cache_entry *ce;
930 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200931
932 if (unlikely(!is_path_valid(path)))
933 return -EINVAL;
934
935 rc = get_normalized_path(path, &npath);
936 if (rc)
937 return rc;
938
939 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
940
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300941 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200942 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
943 if (IS_ERR(ce)) {
944 rc = PTR_ERR(ce);
945 goto out;
946 }
947
948 rc = 0;
949
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300950 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200951
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300952 if (likely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200953 goto out;
954
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300955 list_for_each_entry(t, &ce->tlist, list) {
956 if (!strcasecmp(t->name, it->it_name)) {
957 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200958 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
959 it->it_name);
960 break;
961 }
962 }
963
964out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300965 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200966 free_normalized_path(path, npath);
967 return rc;
968}
969
970/**
971 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
972 * without sending any requests to the currently connected server.
973 *
974 * NOTE: This function will neither update a cache entry in case it was
975 * expired, nor create a new cache entry if @path hasn't been found. It heavily
976 * relies on an existing cache entry.
977 *
978 * @path: path to lookup in DFS referral cache.
979 * @it: target iterator which contains the target hint to update the cache
980 * entry with.
981 *
982 * Return zero if the target hint was updated successfully, otherwise non-zero.
983 */
984int dfs_cache_noreq_update_tgthint(const char *path,
985 const struct dfs_cache_tgt_iterator *it)
986{
987 int rc;
988 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -0300989 struct cache_entry *ce;
990 struct cache_dfs_tgt *t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -0200991
992 if (unlikely(!is_path_valid(path)) || !it)
993 return -EINVAL;
994
995 rc = get_normalized_path(path, &npath);
996 if (rc)
997 return rc;
998
999 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1000
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001001 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001002
1003 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1004 if (IS_ERR(ce)) {
1005 rc = PTR_ERR(ce);
1006 goto out;
1007 }
1008
1009 rc = 0;
1010
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001011 t = ce->tgthint;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001012
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001013 if (unlikely(!strcasecmp(it->it_name, t->name)))
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001014 goto out;
1015
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001016 list_for_each_entry(t, &ce->tlist, list) {
1017 if (!strcasecmp(t->name, it->it_name)) {
1018 ce->tgthint = t;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001019 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1020 it->it_name);
1021 break;
1022 }
1023 }
1024
1025out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001026 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001027 free_normalized_path(path, npath);
1028 return rc;
1029}
1030
1031/**
1032 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1033 * target iterator (@it).
1034 *
1035 * @path: path to lookup in DFS referral cache.
1036 * @it: DFS target iterator.
1037 * @ref: DFS referral pointer to set up the gathered information.
1038 *
1039 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1040 */
1041int dfs_cache_get_tgt_referral(const char *path,
1042 const struct dfs_cache_tgt_iterator *it,
1043 struct dfs_info3_param *ref)
1044{
1045 int rc;
1046 char *npath;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001047 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001048 unsigned int h;
1049
1050 if (!it || !ref)
1051 return -EINVAL;
1052 if (unlikely(!is_path_valid(path)))
1053 return -EINVAL;
1054
1055 rc = get_normalized_path(path, &npath);
1056 if (rc)
1057 return rc;
1058
1059 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1060
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001061 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001062
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001063 ce = lookup_cache_entry(npath, &h);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001064 if (IS_ERR(ce)) {
1065 rc = PTR_ERR(ce);
1066 goto out;
1067 }
1068
1069 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1070
1071 rc = setup_ref(path, ce, ref, it->it_name);
1072
1073out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001074 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001075 free_normalized_path(path, npath);
1076 return rc;
1077}
1078
1079static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1080{
1081 memcpy(new, vol, sizeof(*new));
1082
1083 if (vol->username) {
1084 new->username = kstrndup(vol->username, strlen(vol->username),
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001085 GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001086 if (!new->username)
1087 return -ENOMEM;
1088 }
1089 if (vol->password) {
1090 new->password = kstrndup(vol->password, strlen(vol->password),
1091 GFP_KERNEL);
1092 if (!new->password)
1093 goto err_free_username;
1094 }
1095 if (vol->UNC) {
1096 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1097 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1098 if (!new->UNC)
1099 goto err_free_password;
1100 }
1101 if (vol->domainname) {
1102 new->domainname = kstrndup(vol->domainname,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001103 strlen(vol->domainname), GFP_KERNEL);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001104 if (!new->domainname)
1105 goto err_free_unc;
1106 }
1107 if (vol->iocharset) {
1108 new->iocharset = kstrndup(vol->iocharset,
1109 strlen(vol->iocharset), GFP_KERNEL);
1110 if (!new->iocharset)
1111 goto err_free_domainname;
1112 }
1113 if (vol->prepath) {
1114 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1115 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1116 GFP_KERNEL);
1117 if (!new->prepath)
1118 goto err_free_iocharset;
1119 }
1120
1121 return 0;
1122
1123err_free_iocharset:
1124 kfree(new->iocharset);
1125err_free_domainname:
1126 kfree(new->domainname);
1127err_free_unc:
1128 kfree(new->UNC);
1129err_free_password:
Dan Carpenter34bca9b2018-12-20 14:32:43 +03001130 kzfree(new->password);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001131err_free_username:
1132 kfree(new->username);
1133 kfree(new);
1134 return -ENOMEM;
1135}
1136
1137/**
1138 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1139 * DFS cache refresh worker.
1140 *
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001141 * @mntdata: mount data.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001142 * @vol: cifs volume.
1143 * @fullpath: origin full path.
1144 *
1145 * Return zero if volume was set up correctly, otherwise non-zero.
1146 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001147int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001148{
1149 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001150 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001151
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001152 if (!vol || !fullpath || !mntdata)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001153 return -EINVAL;
1154
1155 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1156
1157 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1158 if (!vi)
1159 return -ENOMEM;
1160
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001161 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1162 if (!vi->fullpath) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001163 rc = -ENOMEM;
1164 goto err_free_vi;
1165 }
1166
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001167 rc = dup_vol(vol, &vi->smb_vol);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001168 if (rc)
1169 goto err_free_fullpath;
1170
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001171 vi->mntdata = mntdata;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001172
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001173 mutex_lock(&vol_lock);
1174 list_add_tail(&vi->list, &vol_list);
1175 mutex_unlock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001176 return 0;
1177
1178err_free_fullpath:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001179 kfree(vi->fullpath);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001180err_free_vi:
1181 kfree(vi);
1182 return rc;
1183}
1184
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001185static inline struct vol_info *find_vol(const char *fullpath)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001186{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001187 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001188
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001189 list_for_each_entry(vi, &vol_list, list) {
1190 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1191 if (!strcasecmp(vi->fullpath, fullpath))
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001192 return vi;
1193 }
1194 return ERR_PTR(-ENOENT);
1195}
1196
1197/**
1198 * dfs_cache_update_vol - update vol info in DFS cache after failover
1199 *
1200 * @fullpath: fullpath to look up in volume list.
1201 * @server: TCP ses pointer.
1202 *
1203 * Return zero if volume was updated, otherwise non-zero.
1204 */
1205int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1206{
1207 int rc;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001208 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001209
1210 if (!fullpath || !server)
1211 return -EINVAL;
1212
1213 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1214
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001215 mutex_lock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001216
1217 vi = find_vol(fullpath);
1218 if (IS_ERR(vi)) {
1219 rc = PTR_ERR(vi);
1220 goto out;
1221 }
1222
1223 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001224 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1225 sizeof(vi->smb_vol.dstaddr));
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001226 rc = 0;
1227
1228out:
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001229 mutex_unlock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001230 return rc;
1231}
1232
1233/**
1234 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1235 *
1236 * @fullpath: fullpath to look up in volume list.
1237 */
1238void dfs_cache_del_vol(const char *fullpath)
1239{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001240 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001241
1242 if (!fullpath || !*fullpath)
1243 return;
1244
1245 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1246
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001247 mutex_lock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001248 vi = find_vol(fullpath);
1249 if (!IS_ERR(vi))
1250 free_vol(vi);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001251 mutex_unlock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001252}
1253
1254/* Get all tcons that are within a DFS namespace and can be refreshed */
1255static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1256{
1257 struct cifs_ses *ses;
1258 struct cifs_tcon *tcon;
1259
1260 INIT_LIST_HEAD(head);
1261
1262 spin_lock(&cifs_tcp_ses_lock);
1263 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1264 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1265 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1266 tcon->dfs_path) {
1267 tcon->tc_count++;
1268 list_add_tail(&tcon->ulist, head);
1269 }
1270 }
1271 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1272 ses->tcon_ipc->dfs_path) {
1273 list_add_tail(&ses->tcon_ipc->ulist, head);
1274 }
1275 }
1276 spin_unlock(&cifs_tcp_ses_lock);
1277}
1278
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001279static bool is_dfs_link(const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001280{
1281 char *s;
1282
1283 s = strchr(path + 1, '\\');
1284 if (!s)
1285 return false;
1286 return !!strchr(s + 1, '\\');
1287}
1288
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001289static char *get_dfs_root(const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001290{
1291 char *s, *npath;
1292
1293 s = strchr(path + 1, '\\');
1294 if (!s)
1295 return ERR_PTR(-EINVAL);
1296
1297 s = strchr(s + 1, '\\');
1298 if (!s)
1299 return ERR_PTR(-EINVAL);
1300
1301 npath = kstrndup(path, s - path, GFP_KERNEL);
1302 if (!npath)
1303 return ERR_PTR(-ENOMEM);
1304
1305 return npath;
1306}
1307
1308/* Find root SMB session out of a DFS link path */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001309static struct cifs_ses *find_root_ses(struct vol_info *vi,
1310 struct cifs_tcon *tcon,
1311 const char *path)
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001312{
1313 char *rpath;
1314 int rc;
1315 struct dfs_info3_param ref = {0};
1316 char *mdata = NULL, *devname = NULL;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001317 struct TCP_Server_Info *server;
1318 struct cifs_ses *ses;
1319 struct smb_vol vol;
1320
1321 rpath = get_dfs_root(path);
1322 if (IS_ERR(rpath))
1323 return ERR_CAST(rpath);
1324
1325 memset(&vol, 0, sizeof(vol));
1326
1327 rc = dfs_cache_noreq_find(rpath, &ref, NULL);
1328 if (rc) {
1329 ses = ERR_PTR(rc);
1330 goto out;
1331 }
1332
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001333 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref, &devname);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001334 free_dfs_info_param(&ref);
1335
1336 if (IS_ERR(mdata)) {
1337 ses = ERR_CAST(mdata);
1338 mdata = NULL;
1339 goto out;
1340 }
1341
Paulo Alcantara (SUSE)df3df922019-11-22 12:30:52 -03001342 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001343 kfree(devname);
1344
1345 if (rc) {
1346 ses = ERR_PTR(rc);
1347 goto out;
1348 }
1349
1350 server = cifs_find_tcp_session(&vol);
1351 if (IS_ERR_OR_NULL(server)) {
1352 ses = ERR_PTR(-EHOSTDOWN);
1353 goto out;
1354 }
1355 if (server->tcpStatus != CifsGood) {
1356 cifs_put_tcp_session(server, 0);
1357 ses = ERR_PTR(-EHOSTDOWN);
1358 goto out;
1359 }
1360
1361 ses = cifs_get_smb_ses(server, &vol);
1362
1363out:
1364 cifs_cleanup_volume_info_contents(&vol);
1365 kfree(mdata);
1366 kfree(rpath);
1367
1368 return ses;
1369}
1370
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001371/* Refresh DFS cache entry from a given tcon */
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001372static void refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001373{
1374 int rc = 0;
1375 unsigned int xid;
1376 char *path, *npath;
1377 unsigned int h;
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001378 struct cache_entry *ce;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001379 struct dfs_info3_param *refs = NULL;
1380 int numrefs = 0;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001381 struct cifs_ses *root_ses = NULL, *ses;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001382
1383 xid = get_xid();
1384
1385 path = tcon->dfs_path + 1;
1386
1387 rc = get_normalized_path(path, &npath);
1388 if (rc)
1389 goto out;
1390
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001391 mutex_lock(&list_lock);
1392 ce = lookup_cache_entry(npath, &h);
1393 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001394
1395 if (IS_ERR(ce)) {
1396 rc = PTR_ERR(ce);
1397 goto out;
1398 }
1399
1400 if (!cache_entry_expired(ce))
1401 goto out;
1402
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001403 /* If it's a DFS Link, then use root SMB session for refreshing it */
1404 if (is_dfs_link(npath)) {
1405 ses = root_ses = find_root_ses(vi, tcon, npath);
1406 if (IS_ERR(ses)) {
1407 rc = PTR_ERR(ses);
1408 root_ses = NULL;
1409 goto out;
1410 }
1411 } else {
1412 ses = tcon->ses;
1413 }
1414
1415 if (unlikely(!ses->server->ops->get_dfs_refer)) {
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001416 rc = -EOPNOTSUPP;
1417 } else {
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001418 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs,
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001419 &numrefs, cache_nlsc,
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001420 tcon->remap);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001421 if (!rc) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001422 mutex_lock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001423 ce = __update_cache_entry(npath, refs, numrefs);
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001424 mutex_unlock(&list_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001425 dump_refs(refs, numrefs);
1426 free_dfs_info_array(refs, numrefs);
1427 if (IS_ERR(ce))
1428 rc = PTR_ERR(ce);
1429 }
1430 }
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001431
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001432out:
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001433 if (root_ses)
1434 cifs_put_smb_ses(root_ses);
1435
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001436 free_xid(xid);
1437 free_normalized_path(path, npath);
1438}
1439
1440/*
1441 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1442 * referral.
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001443 */
1444static void refresh_cache_worker(struct work_struct *work)
1445{
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001446 struct vol_info *vi;
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001447 struct TCP_Server_Info *server;
1448 LIST_HEAD(list);
1449 struct cifs_tcon *tcon, *ntcon;
1450
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001451 mutex_lock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001452
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001453 list_for_each_entry(vi, &vol_list, list) {
1454 server = cifs_find_tcp_session(&vi->smb_vol);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001455 if (IS_ERR_OR_NULL(server))
1456 continue;
1457 if (server->tcpStatus != CifsGood)
1458 goto next;
1459 get_tcons(server, &list);
1460 list_for_each_entry_safe(tcon, ntcon, &list, ulist) {
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001461 refresh_tcon(vi, tcon);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001462 list_del_init(&tcon->ulist);
1463 cifs_put_tcon(tcon);
1464 }
1465next:
1466 cifs_put_tcp_session(server, 0);
1467 }
Paulo Alcantara (SUSE)185352a2019-12-04 17:37:58 -03001468 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1469 mutex_unlock(&vol_lock);
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001470}