USB: serial: f81232: fix interrupt worker not stop
[linux/fpc-iii.git] / fs / cifs / dfs_cache.c
blob09b7d0d4f6e4fe5f6708d6387f6507ee20f6482f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * DFS referral cache routines
5 * Copyright (c) 2018 Paulo Alcantara <palcantara@suse.de>
6 */
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"
23 #include "dfs_cache.h"
25 #define DFS_CACHE_HTABLE_SIZE 32
26 #define DFS_CACHE_MAX_ENTRIES 64
28 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
29 DFSREF_STORAGE_SERVER))
31 struct dfs_cache_tgt {
32 char *t_name;
33 struct list_head t_list;
36 struct dfs_cache_entry {
37 struct hlist_node ce_hlist;
38 const char *ce_path;
39 int ce_ttl;
40 int ce_srvtype;
41 int ce_flags;
42 struct timespec64 ce_etime;
43 int ce_path_consumed;
44 int ce_numtgts;
45 struct list_head ce_tlist;
46 struct dfs_cache_tgt *ce_tgthint;
47 struct rcu_head ce_rcu;
50 static struct kmem_cache *dfs_cache_slab __read_mostly;
52 struct dfs_cache_vol_info {
53 char *vi_fullpath;
54 struct smb_vol vi_vol;
55 struct list_head vi_list;
58 struct dfs_cache {
59 struct mutex dc_lock;
60 struct nls_table *dc_nlsc;
61 struct list_head dc_vol_list;
62 int dc_ttl;
63 struct delayed_work dc_refresh;
66 static struct dfs_cache dfs_cache;
69 * Number of entries in the cache
71 static size_t dfs_cache_count;
73 static DEFINE_MUTEX(dfs_cache_list_lock);
74 static struct hlist_head dfs_cache_htable[DFS_CACHE_HTABLE_SIZE];
76 static void refresh_cache_worker(struct work_struct *work);
78 static inline bool is_path_valid(const char *path)
80 return path && (strchr(path + 1, '\\') || strchr(path + 1, '/'));
83 static inline int get_normalized_path(const char *path, char **npath)
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, '\\');
93 return 0;
96 static inline void free_normalized_path(const char *path, char *npath)
98 if (path != npath)
99 kfree(npath);
102 static inline bool cache_entry_expired(const struct dfs_cache_entry *ce)
104 struct timespec64 ts;
106 ktime_get_coarse_real_ts64(&ts);
107 return timespec64_compare(&ts, &ce->ce_etime) >= 0;
110 static inline void free_tgts(struct dfs_cache_entry *ce)
112 struct dfs_cache_tgt *t, *n;
114 list_for_each_entry_safe(t, n, &ce->ce_tlist, t_list) {
115 list_del(&t->t_list);
116 kfree(t->t_name);
117 kfree(t);
121 static void free_cache_entry(struct rcu_head *rcu)
123 struct dfs_cache_entry *ce = container_of(rcu, struct dfs_cache_entry,
124 ce_rcu);
125 kmem_cache_free(dfs_cache_slab, ce);
128 static inline void flush_cache_ent(struct dfs_cache_entry *ce)
130 if (hlist_unhashed(&ce->ce_hlist))
131 return;
133 hlist_del_init_rcu(&ce->ce_hlist);
134 kfree(ce->ce_path);
135 free_tgts(ce);
136 dfs_cache_count--;
137 call_rcu(&ce->ce_rcu, free_cache_entry);
140 static void flush_cache_ents(void)
142 int i;
144 rcu_read_lock();
145 for (i = 0; i < DFS_CACHE_HTABLE_SIZE; i++) {
146 struct hlist_head *l = &dfs_cache_htable[i];
147 struct dfs_cache_entry *ce;
149 hlist_for_each_entry_rcu(ce, l, ce_hlist)
150 flush_cache_ent(ce);
152 rcu_read_unlock();
156 * dfs cache /proc file
158 static int dfscache_proc_show(struct seq_file *m, void *v)
160 int bucket;
161 struct dfs_cache_entry *ce;
162 struct dfs_cache_tgt *t;
164 seq_puts(m, "DFS cache\n---------\n");
166 mutex_lock(&dfs_cache_list_lock);
168 rcu_read_lock();
169 hash_for_each_rcu(dfs_cache_htable, bucket, ce, ce_hlist) {
170 seq_printf(m,
171 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
172 "interlink=%s,path_consumed=%d,expired=%s\n",
173 ce->ce_path,
174 ce->ce_srvtype == DFS_TYPE_ROOT ? "root" : "link",
175 ce->ce_ttl, ce->ce_etime.tv_nsec,
176 IS_INTERLINK_SET(ce->ce_flags) ? "yes" : "no",
177 ce->ce_path_consumed,
178 cache_entry_expired(ce) ? "yes" : "no");
180 list_for_each_entry(t, &ce->ce_tlist, t_list) {
181 seq_printf(m, " %s%s\n",
182 t->t_name,
183 ce->ce_tgthint == t ? " (target hint)" : "");
187 rcu_read_unlock();
189 mutex_unlock(&dfs_cache_list_lock);
190 return 0;
193 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
194 size_t count, loff_t *ppos)
196 char c;
197 int rc;
199 rc = get_user(c, buffer);
200 if (rc)
201 return rc;
203 if (c != '0')
204 return -EINVAL;
206 cifs_dbg(FYI, "clearing dfs cache");
207 mutex_lock(&dfs_cache_list_lock);
208 flush_cache_ents();
209 mutex_unlock(&dfs_cache_list_lock);
211 return count;
214 static int dfscache_proc_open(struct inode *inode, struct file *file)
216 return single_open(file, dfscache_proc_show, NULL);
219 const 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,
227 #ifdef CONFIG_CIFS_DEBUG2
228 static inline void dump_tgts(const struct dfs_cache_entry *ce)
230 struct dfs_cache_tgt *t;
232 cifs_dbg(FYI, "target list:\n");
233 list_for_each_entry(t, &ce->ce_tlist, t_list) {
234 cifs_dbg(FYI, " %s%s\n", t->t_name,
235 ce->ce_tgthint == t ? " (target hint)" : "");
239 static inline void dump_ce(const struct dfs_cache_entry *ce)
241 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
242 "interlink=%s,path_consumed=%d,expired=%s\n", ce->ce_path,
243 ce->ce_srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ce_ttl,
244 ce->ce_etime.tv_nsec,
245 IS_INTERLINK_SET(ce->ce_flags) ? "yes" : "no",
246 ce->ce_path_consumed,
247 cache_entry_expired(ce) ? "yes" : "no");
248 dump_tgts(ce);
251 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
253 int i;
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];
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);
273 #else
274 #define dump_tgts(e)
275 #define dump_ce(e)
276 #define dump_refs(r, n)
277 #endif
280 * dfs_cache_init - Initialize DFS referral cache.
282 * Return zero if initialized successfully, otherwise non-zero.
284 int dfs_cache_init(void)
286 int i;
288 dfs_cache_slab = kmem_cache_create("cifs_dfs_cache",
289 sizeof(struct dfs_cache_entry), 0,
290 SLAB_HWCACHE_ALIGN, NULL);
291 if (!dfs_cache_slab)
292 return -ENOMEM;
294 for (i = 0; i < DFS_CACHE_HTABLE_SIZE; i++)
295 INIT_HLIST_HEAD(&dfs_cache_htable[i]);
297 INIT_LIST_HEAD(&dfs_cache.dc_vol_list);
298 mutex_init(&dfs_cache.dc_lock);
299 INIT_DELAYED_WORK(&dfs_cache.dc_refresh, refresh_cache_worker);
300 dfs_cache.dc_ttl = -1;
301 dfs_cache.dc_nlsc = load_nls_default();
303 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
304 return 0;
307 static inline unsigned int cache_entry_hash(const void *data, int size)
309 unsigned int h;
311 h = jhash(data, size, 0);
312 return h & (DFS_CACHE_HTABLE_SIZE - 1);
315 /* Check whether second path component of @path is SYSVOL or NETLOGON */
316 static inline bool is_sysvol_or_netlogon(const char *path)
318 const char *s;
319 char sep = path[0];
321 s = strchr(path + 1, sep) + 1;
322 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
323 !strncasecmp(s, "netlogon", strlen("netlogon"));
326 /* Return target hint of a DFS cache entry */
327 static inline char *get_tgt_name(const struct dfs_cache_entry *ce)
329 struct dfs_cache_tgt *t = ce->ce_tgthint;
331 return t ? t->t_name : ERR_PTR(-ENOENT);
334 /* Return expire time out of a new entry's TTL */
335 static inline struct timespec64 get_expire_time(int ttl)
337 struct timespec64 ts = {
338 .tv_sec = ttl,
339 .tv_nsec = 0,
341 struct timespec64 now;
343 ktime_get_coarse_real_ts64(&now);
344 return timespec64_add(now, ts);
347 /* Allocate a new DFS target */
348 static inline struct dfs_cache_tgt *alloc_tgt(const char *name)
350 struct dfs_cache_tgt *t;
352 t = kmalloc(sizeof(*t), GFP_KERNEL);
353 if (!t)
354 return ERR_PTR(-ENOMEM);
355 t->t_name = kstrndup(name, strlen(name), GFP_KERNEL);
356 if (!t->t_name) {
357 kfree(t);
358 return ERR_PTR(-ENOMEM);
360 INIT_LIST_HEAD(&t->t_list);
361 return t;
365 * Copy DFS referral information to a cache entry and conditionally update
366 * target hint.
368 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
369 struct dfs_cache_entry *ce, const char *tgthint)
371 int i;
373 ce->ce_ttl = refs[0].ttl;
374 ce->ce_etime = get_expire_time(ce->ce_ttl);
375 ce->ce_srvtype = refs[0].server_type;
376 ce->ce_flags = refs[0].ref_flag;
377 ce->ce_path_consumed = refs[0].path_consumed;
379 for (i = 0; i < numrefs; i++) {
380 struct dfs_cache_tgt *t;
382 t = alloc_tgt(refs[i].node_name);
383 if (IS_ERR(t)) {
384 free_tgts(ce);
385 return PTR_ERR(t);
387 if (tgthint && !strcasecmp(t->t_name, tgthint)) {
388 list_add(&t->t_list, &ce->ce_tlist);
389 tgthint = NULL;
390 } else {
391 list_add_tail(&t->t_list, &ce->ce_tlist);
393 ce->ce_numtgts++;
396 ce->ce_tgthint = list_first_entry_or_null(&ce->ce_tlist,
397 struct dfs_cache_tgt, t_list);
399 return 0;
402 /* Allocate a new cache entry */
403 static struct dfs_cache_entry *
404 alloc_cache_entry(const char *path, const struct dfs_info3_param *refs,
405 int numrefs)
407 struct dfs_cache_entry *ce;
408 int rc;
410 ce = kmem_cache_zalloc(dfs_cache_slab, GFP_KERNEL);
411 if (!ce)
412 return ERR_PTR(-ENOMEM);
414 ce->ce_path = kstrdup_const(path, GFP_KERNEL);
415 if (!ce->ce_path) {
416 kmem_cache_free(dfs_cache_slab, ce);
417 return ERR_PTR(-ENOMEM);
419 INIT_HLIST_NODE(&ce->ce_hlist);
420 INIT_LIST_HEAD(&ce->ce_tlist);
422 rc = copy_ref_data(refs, numrefs, ce, NULL);
423 if (rc) {
424 kfree(ce->ce_path);
425 kmem_cache_free(dfs_cache_slab, ce);
426 ce = ERR_PTR(rc);
428 return ce;
431 static void remove_oldest_entry(void)
433 int bucket;
434 struct dfs_cache_entry *ce;
435 struct dfs_cache_entry *to_del = NULL;
437 rcu_read_lock();
438 hash_for_each_rcu(dfs_cache_htable, bucket, ce, ce_hlist) {
439 if (!to_del || timespec64_compare(&ce->ce_etime,
440 &to_del->ce_etime) < 0)
441 to_del = ce;
443 if (!to_del) {
444 cifs_dbg(FYI, "%s: no entry to remove", __func__);
445 goto out;
447 cifs_dbg(FYI, "%s: removing entry", __func__);
448 dump_ce(to_del);
449 flush_cache_ent(to_del);
450 out:
451 rcu_read_unlock();
454 /* Add a new DFS cache entry */
455 static inline struct dfs_cache_entry *
456 add_cache_entry(unsigned int hash, const char *path,
457 const struct dfs_info3_param *refs, int numrefs)
459 struct dfs_cache_entry *ce;
461 ce = alloc_cache_entry(path, refs, numrefs);
462 if (IS_ERR(ce))
463 return ce;
465 hlist_add_head_rcu(&ce->ce_hlist, &dfs_cache_htable[hash]);
467 mutex_lock(&dfs_cache.dc_lock);
468 if (dfs_cache.dc_ttl < 0) {
469 dfs_cache.dc_ttl = ce->ce_ttl;
470 queue_delayed_work(cifsiod_wq, &dfs_cache.dc_refresh,
471 dfs_cache.dc_ttl * HZ);
472 } else {
473 dfs_cache.dc_ttl = min_t(int, dfs_cache.dc_ttl, ce->ce_ttl);
474 mod_delayed_work(cifsiod_wq, &dfs_cache.dc_refresh,
475 dfs_cache.dc_ttl * HZ);
477 mutex_unlock(&dfs_cache.dc_lock);
479 return ce;
482 static struct dfs_cache_entry *__find_cache_entry(unsigned int hash,
483 const char *path)
485 struct dfs_cache_entry *ce;
486 bool found = false;
488 rcu_read_lock();
489 hlist_for_each_entry_rcu(ce, &dfs_cache_htable[hash], ce_hlist) {
490 if (!strcasecmp(path, ce->ce_path)) {
491 #ifdef CONFIG_CIFS_DEBUG2
492 char *name = get_tgt_name(ce);
494 if (unlikely(IS_ERR(name))) {
495 rcu_read_unlock();
496 return ERR_CAST(name);
498 cifs_dbg(FYI, "%s: cache hit\n", __func__);
499 cifs_dbg(FYI, "%s: target hint: %s\n", __func__, name);
500 #endif
501 found = true;
502 break;
505 rcu_read_unlock();
506 return found ? ce : ERR_PTR(-ENOENT);
510 * Find a DFS cache entry in hash table and optionally check prefix path against
511 * @path.
512 * Use whole path components in the match.
513 * Return ERR_PTR(-ENOENT) if the entry is not found.
515 static inline struct dfs_cache_entry *find_cache_entry(const char *path,
516 unsigned int *hash)
518 *hash = cache_entry_hash(path, strlen(path));
519 return __find_cache_entry(*hash, path);
522 static inline void destroy_slab_cache(void)
524 rcu_barrier();
525 kmem_cache_destroy(dfs_cache_slab);
528 static inline void free_vol(struct dfs_cache_vol_info *vi)
530 list_del(&vi->vi_list);
531 kfree(vi->vi_fullpath);
532 cifs_cleanup_volume_info_contents(&vi->vi_vol);
533 kfree(vi);
536 static inline void free_vol_list(void)
538 struct dfs_cache_vol_info *vi, *nvi;
540 list_for_each_entry_safe(vi, nvi, &dfs_cache.dc_vol_list, vi_list)
541 free_vol(vi);
545 * dfs_cache_destroy - destroy DFS referral cache
547 void dfs_cache_destroy(void)
549 cancel_delayed_work_sync(&dfs_cache.dc_refresh);
550 unload_nls(dfs_cache.dc_nlsc);
551 free_vol_list();
552 mutex_destroy(&dfs_cache.dc_lock);
554 flush_cache_ents();
555 destroy_slab_cache();
556 mutex_destroy(&dfs_cache_list_lock);
558 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
561 static inline struct dfs_cache_entry *
562 __update_cache_entry(const char *path, const struct dfs_info3_param *refs,
563 int numrefs)
565 int rc;
566 unsigned int h;
567 struct dfs_cache_entry *ce;
568 char *s, *th = NULL;
570 ce = find_cache_entry(path, &h);
571 if (IS_ERR(ce))
572 return ce;
574 if (ce->ce_tgthint) {
575 s = ce->ce_tgthint->t_name;
576 th = kstrndup(s, strlen(s), GFP_KERNEL);
577 if (!th)
578 return ERR_PTR(-ENOMEM);
581 free_tgts(ce);
582 ce->ce_numtgts = 0;
584 rc = copy_ref_data(refs, numrefs, ce, th);
585 kfree(th);
587 if (rc)
588 ce = ERR_PTR(rc);
590 return ce;
593 /* Update an expired cache entry by getting a new DFS referral from server */
594 static struct dfs_cache_entry *
595 update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
596 const struct nls_table *nls_codepage, int remap,
597 const char *path, struct dfs_cache_entry *ce)
599 int rc;
600 struct dfs_info3_param *refs = NULL;
601 int numrefs = 0;
603 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
605 * Check if caller provided enough parameters to update an expired
606 * entry.
608 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
609 return ERR_PTR(-ETIME);
610 if (unlikely(!nls_codepage))
611 return ERR_PTR(-ETIME);
613 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
615 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
616 nls_codepage, remap);
617 if (rc)
618 ce = ERR_PTR(rc);
619 else
620 ce = __update_cache_entry(path, refs, numrefs);
622 dump_refs(refs, numrefs);
623 free_dfs_info_array(refs, numrefs);
625 return ce;
629 * Find, create or update a DFS cache entry.
631 * If the entry wasn't found, it will create a new one. Or if it was found but
632 * expired, then it will update the entry accordingly.
634 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
635 * handle them properly.
637 static struct dfs_cache_entry *
638 do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
639 const struct nls_table *nls_codepage, int remap,
640 const char *path, bool noreq)
642 int rc;
643 unsigned int h;
644 struct dfs_cache_entry *ce;
645 struct dfs_info3_param *nrefs;
646 int numnrefs;
648 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
650 ce = find_cache_entry(path, &h);
651 if (IS_ERR(ce)) {
652 cifs_dbg(FYI, "%s: cache miss\n", __func__);
654 * If @noreq is set, no requests will be sent to the server for
655 * either updating or getting a new DFS referral.
657 if (noreq)
658 return ce;
660 * No cache entry was found, so check for valid parameters that
661 * will be required to get a new DFS referral and then create a
662 * new cache entry.
664 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
665 ce = ERR_PTR(-EOPNOTSUPP);
666 return ce;
668 if (unlikely(!nls_codepage)) {
669 ce = ERR_PTR(-EINVAL);
670 return ce;
673 nrefs = NULL;
674 numnrefs = 0;
676 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
677 path);
679 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
680 &numnrefs, nls_codepage,
681 remap);
682 if (rc) {
683 ce = ERR_PTR(rc);
684 return ce;
687 dump_refs(nrefs, numnrefs);
689 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
691 if (dfs_cache_count >= DFS_CACHE_MAX_ENTRIES) {
692 cifs_dbg(FYI, "%s: reached max cache size (%d)",
693 __func__, DFS_CACHE_MAX_ENTRIES);
694 remove_oldest_entry();
696 ce = add_cache_entry(h, path, nrefs, numnrefs);
697 free_dfs_info_array(nrefs, numnrefs);
699 if (IS_ERR(ce))
700 return ce;
702 dfs_cache_count++;
705 dump_ce(ce);
707 /* Just return the found cache entry in case @noreq is set */
708 if (noreq)
709 return ce;
711 if (cache_entry_expired(ce)) {
712 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
713 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
714 ce);
715 if (IS_ERR(ce)) {
716 cifs_dbg(FYI, "%s: failed to update expired entry\n",
717 __func__);
720 return ce;
723 /* Set up a new DFS referral from a given cache entry */
724 static int setup_ref(const char *path, const struct dfs_cache_entry *ce,
725 struct dfs_info3_param *ref, const char *tgt)
727 int rc;
729 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
731 memset(ref, 0, sizeof(*ref));
733 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
734 if (!ref->path_name)
735 return -ENOMEM;
737 ref->path_consumed = ce->ce_path_consumed;
739 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
740 if (!ref->node_name) {
741 rc = -ENOMEM;
742 goto err_free_path;
745 ref->ttl = ce->ce_ttl;
746 ref->server_type = ce->ce_srvtype;
747 ref->ref_flag = ce->ce_flags;
749 return 0;
751 err_free_path:
752 kfree(ref->path_name);
753 ref->path_name = NULL;
754 return rc;
757 /* Return target list of a DFS cache entry */
758 static int get_tgt_list(const struct dfs_cache_entry *ce,
759 struct dfs_cache_tgt_list *tl)
761 int rc;
762 struct list_head *head = &tl->tl_list;
763 struct dfs_cache_tgt *t;
764 struct dfs_cache_tgt_iterator *it, *nit;
766 memset(tl, 0, sizeof(*tl));
767 INIT_LIST_HEAD(head);
769 list_for_each_entry(t, &ce->ce_tlist, t_list) {
770 it = kzalloc(sizeof(*it), GFP_KERNEL);
771 if (!it) {
772 rc = -ENOMEM;
773 goto err_free_it;
776 it->it_name = kstrndup(t->t_name, strlen(t->t_name),
777 GFP_KERNEL);
778 if (!it->it_name) {
779 kfree(it);
780 rc = -ENOMEM;
781 goto err_free_it;
784 if (ce->ce_tgthint == t)
785 list_add(&it->it_list, head);
786 else
787 list_add_tail(&it->it_list, head);
789 tl->tl_numtgts = ce->ce_numtgts;
791 return 0;
793 err_free_it:
794 list_for_each_entry_safe(it, nit, head, it_list) {
795 kfree(it->it_name);
796 kfree(it);
798 return rc;
802 * dfs_cache_find - find a DFS cache entry
804 * If it doesn't find the cache entry, then it will get a DFS referral
805 * for @path and create a new entry.
807 * In case the cache entry exists but expired, it will get a DFS referral
808 * for @path and then update the respective cache entry.
810 * These parameters are passed down to the get_dfs_refer() call if it
811 * needs to be issued:
812 * @xid: syscall xid
813 * @ses: smb session to issue the request on
814 * @nls_codepage: charset conversion
815 * @remap: path character remapping type
816 * @path: path to lookup in DFS referral cache.
818 * @ref: when non-NULL, store single DFS referral result in it.
819 * @tgt_list: when non-NULL, store complete DFS target list in it.
821 * Return zero if the target was found, otherwise non-zero.
823 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
824 const struct nls_table *nls_codepage, int remap,
825 const char *path, struct dfs_info3_param *ref,
826 struct dfs_cache_tgt_list *tgt_list)
828 int rc;
829 char *npath;
830 struct dfs_cache_entry *ce;
832 if (unlikely(!is_path_valid(path)))
833 return -EINVAL;
835 rc = get_normalized_path(path, &npath);
836 if (rc)
837 return rc;
839 mutex_lock(&dfs_cache_list_lock);
840 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
841 if (!IS_ERR(ce)) {
842 if (ref)
843 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
844 else
845 rc = 0;
846 if (!rc && tgt_list)
847 rc = get_tgt_list(ce, tgt_list);
848 } else {
849 rc = PTR_ERR(ce);
851 mutex_unlock(&dfs_cache_list_lock);
852 free_normalized_path(path, npath);
853 return rc;
857 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
858 * the currently connected server.
860 * NOTE: This function will neither update a cache entry in case it was
861 * expired, nor create a new cache entry if @path hasn't been found. It heavily
862 * relies on an existing cache entry.
864 * @path: path to lookup in the DFS referral cache.
865 * @ref: when non-NULL, store single DFS referral result in it.
866 * @tgt_list: when non-NULL, store complete DFS target list in it.
868 * Return 0 if successful.
869 * Return -ENOENT if the entry was not found.
870 * Return non-zero for other errors.
872 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
873 struct dfs_cache_tgt_list *tgt_list)
875 int rc;
876 char *npath;
877 struct dfs_cache_entry *ce;
879 if (unlikely(!is_path_valid(path)))
880 return -EINVAL;
882 rc = get_normalized_path(path, &npath);
883 if (rc)
884 return rc;
886 mutex_lock(&dfs_cache_list_lock);
887 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
888 if (IS_ERR(ce)) {
889 rc = PTR_ERR(ce);
890 goto out;
893 if (ref)
894 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
895 else
896 rc = 0;
897 if (!rc && tgt_list)
898 rc = get_tgt_list(ce, tgt_list);
899 out:
900 mutex_unlock(&dfs_cache_list_lock);
901 free_normalized_path(path, npath);
902 return rc;
906 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
908 * If it doesn't find the cache entry, then it will get a DFS referral for @path
909 * and create a new entry.
911 * In case the cache entry exists but expired, it will get a DFS referral
912 * for @path and then update the respective cache entry.
914 * @xid: syscall id
915 * @ses: smb session
916 * @nls_codepage: charset conversion
917 * @remap: type of character remapping for paths
918 * @path: path to lookup in DFS referral cache.
919 * @it: DFS target iterator
921 * Return zero if the target hint was updated successfully, otherwise non-zero.
923 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
924 const struct nls_table *nls_codepage, int remap,
925 const char *path,
926 const struct dfs_cache_tgt_iterator *it)
928 int rc;
929 char *npath;
930 struct dfs_cache_entry *ce;
931 struct dfs_cache_tgt *t;
933 if (unlikely(!is_path_valid(path)))
934 return -EINVAL;
936 rc = get_normalized_path(path, &npath);
937 if (rc)
938 return rc;
940 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
942 mutex_lock(&dfs_cache_list_lock);
943 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
944 if (IS_ERR(ce)) {
945 rc = PTR_ERR(ce);
946 goto out;
949 rc = 0;
951 t = ce->ce_tgthint;
953 if (likely(!strcasecmp(it->it_name, t->t_name)))
954 goto out;
956 list_for_each_entry(t, &ce->ce_tlist, t_list) {
957 if (!strcasecmp(t->t_name, it->it_name)) {
958 ce->ce_tgthint = t;
959 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
960 it->it_name);
961 break;
965 out:
966 mutex_unlock(&dfs_cache_list_lock);
967 free_normalized_path(path, npath);
968 return rc;
972 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
973 * without sending any requests to the currently connected server.
975 * NOTE: This function will neither update a cache entry in case it was
976 * expired, nor create a new cache entry if @path hasn't been found. It heavily
977 * relies on an existing cache entry.
979 * @path: path to lookup in DFS referral cache.
980 * @it: target iterator which contains the target hint to update the cache
981 * entry with.
983 * Return zero if the target hint was updated successfully, otherwise non-zero.
985 int dfs_cache_noreq_update_tgthint(const char *path,
986 const struct dfs_cache_tgt_iterator *it)
988 int rc;
989 char *npath;
990 struct dfs_cache_entry *ce;
991 struct dfs_cache_tgt *t;
993 if (unlikely(!is_path_valid(path)) || !it)
994 return -EINVAL;
996 rc = get_normalized_path(path, &npath);
997 if (rc)
998 return rc;
1000 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1002 mutex_lock(&dfs_cache_list_lock);
1004 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1005 if (IS_ERR(ce)) {
1006 rc = PTR_ERR(ce);
1007 goto out;
1010 rc = 0;
1012 t = ce->ce_tgthint;
1014 if (unlikely(!strcasecmp(it->it_name, t->t_name)))
1015 goto out;
1017 list_for_each_entry(t, &ce->ce_tlist, t_list) {
1018 if (!strcasecmp(t->t_name, it->it_name)) {
1019 ce->ce_tgthint = t;
1020 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1021 it->it_name);
1022 break;
1026 out:
1027 mutex_unlock(&dfs_cache_list_lock);
1028 free_normalized_path(path, npath);
1029 return rc;
1033 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1034 * target iterator (@it).
1036 * @path: path to lookup in DFS referral cache.
1037 * @it: DFS target iterator.
1038 * @ref: DFS referral pointer to set up the gathered information.
1040 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1042 int dfs_cache_get_tgt_referral(const char *path,
1043 const struct dfs_cache_tgt_iterator *it,
1044 struct dfs_info3_param *ref)
1046 int rc;
1047 char *npath;
1048 struct dfs_cache_entry *ce;
1049 unsigned int h;
1051 if (!it || !ref)
1052 return -EINVAL;
1053 if (unlikely(!is_path_valid(path)))
1054 return -EINVAL;
1056 rc = get_normalized_path(path, &npath);
1057 if (rc)
1058 return rc;
1060 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1062 mutex_lock(&dfs_cache_list_lock);
1064 ce = find_cache_entry(npath, &h);
1065 if (IS_ERR(ce)) {
1066 rc = PTR_ERR(ce);
1067 goto out;
1070 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1072 rc = setup_ref(path, ce, ref, it->it_name);
1074 out:
1075 mutex_unlock(&dfs_cache_list_lock);
1076 free_normalized_path(path, npath);
1077 return rc;
1080 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1082 memcpy(new, vol, sizeof(*new));
1084 if (vol->username) {
1085 new->username = kstrndup(vol->username, strlen(vol->username),
1086 GFP_KERNEL);
1087 if (!new->username)
1088 return -ENOMEM;
1090 if (vol->password) {
1091 new->password = kstrndup(vol->password, strlen(vol->password),
1092 GFP_KERNEL);
1093 if (!new->password)
1094 goto err_free_username;
1096 if (vol->UNC) {
1097 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1098 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1099 if (!new->UNC)
1100 goto err_free_password;
1102 if (vol->domainname) {
1103 new->domainname = kstrndup(vol->domainname,
1104 strlen(vol->domainname), GFP_KERNEL);
1105 if (!new->domainname)
1106 goto err_free_unc;
1108 if (vol->iocharset) {
1109 new->iocharset = kstrndup(vol->iocharset,
1110 strlen(vol->iocharset), GFP_KERNEL);
1111 if (!new->iocharset)
1112 goto err_free_domainname;
1114 if (vol->prepath) {
1115 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1116 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1117 GFP_KERNEL);
1118 if (!new->prepath)
1119 goto err_free_iocharset;
1122 return 0;
1124 err_free_iocharset:
1125 kfree(new->iocharset);
1126 err_free_domainname:
1127 kfree(new->domainname);
1128 err_free_unc:
1129 kfree(new->UNC);
1130 err_free_password:
1131 kzfree(new->password);
1132 err_free_username:
1133 kfree(new->username);
1134 kfree(new);
1135 return -ENOMEM;
1139 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1140 * DFS cache refresh worker.
1142 * @vol: cifs volume.
1143 * @fullpath: origin full path.
1145 * Return zero if volume was set up correctly, otherwise non-zero.
1147 int dfs_cache_add_vol(struct smb_vol *vol, const char *fullpath)
1149 int rc;
1150 struct dfs_cache_vol_info *vi;
1152 if (!vol || !fullpath)
1153 return -EINVAL;
1155 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1157 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1158 if (!vi)
1159 return -ENOMEM;
1161 vi->vi_fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1162 if (!vi->vi_fullpath) {
1163 rc = -ENOMEM;
1164 goto err_free_vi;
1167 rc = dup_vol(vol, &vi->vi_vol);
1168 if (rc)
1169 goto err_free_fullpath;
1171 mutex_lock(&dfs_cache.dc_lock);
1172 list_add_tail(&vi->vi_list, &dfs_cache.dc_vol_list);
1173 mutex_unlock(&dfs_cache.dc_lock);
1174 return 0;
1176 err_free_fullpath:
1177 kfree(vi->vi_fullpath);
1178 err_free_vi:
1179 kfree(vi);
1180 return rc;
1183 static inline struct dfs_cache_vol_info *find_vol(const char *fullpath)
1185 struct dfs_cache_vol_info *vi;
1187 list_for_each_entry(vi, &dfs_cache.dc_vol_list, vi_list) {
1188 cifs_dbg(FYI, "%s: vi->vi_fullpath: %s\n", __func__,
1189 vi->vi_fullpath);
1190 if (!strcasecmp(vi->vi_fullpath, fullpath))
1191 return vi;
1193 return ERR_PTR(-ENOENT);
1197 * dfs_cache_update_vol - update vol info in DFS cache after failover
1199 * @fullpath: fullpath to look up in volume list.
1200 * @server: TCP ses pointer.
1202 * Return zero if volume was updated, otherwise non-zero.
1204 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1206 int rc;
1207 struct dfs_cache_vol_info *vi;
1209 if (!fullpath || !server)
1210 return -EINVAL;
1212 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1214 mutex_lock(&dfs_cache.dc_lock);
1216 vi = find_vol(fullpath);
1217 if (IS_ERR(vi)) {
1218 rc = PTR_ERR(vi);
1219 goto out;
1222 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1223 memcpy(&vi->vi_vol.dstaddr, &server->dstaddr,
1224 sizeof(vi->vi_vol.dstaddr));
1225 rc = 0;
1227 out:
1228 mutex_unlock(&dfs_cache.dc_lock);
1229 return rc;
1233 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1235 * @fullpath: fullpath to look up in volume list.
1237 void dfs_cache_del_vol(const char *fullpath)
1239 struct dfs_cache_vol_info *vi;
1241 if (!fullpath || !*fullpath)
1242 return;
1244 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1246 mutex_lock(&dfs_cache.dc_lock);
1247 vi = find_vol(fullpath);
1248 if (!IS_ERR(vi))
1249 free_vol(vi);
1250 mutex_unlock(&dfs_cache.dc_lock);
1253 /* Get all tcons that are within a DFS namespace and can be refreshed */
1254 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1256 struct cifs_ses *ses;
1257 struct cifs_tcon *tcon;
1259 INIT_LIST_HEAD(head);
1261 spin_lock(&cifs_tcp_ses_lock);
1262 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1263 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1264 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1265 tcon->dfs_path) {
1266 tcon->tc_count++;
1267 list_add_tail(&tcon->ulist, head);
1270 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1271 ses->tcon_ipc->dfs_path) {
1272 list_add_tail(&ses->tcon_ipc->ulist, head);
1275 spin_unlock(&cifs_tcp_ses_lock);
1278 /* Refresh DFS cache entry from a given tcon */
1279 static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon)
1281 int rc = 0;
1282 unsigned int xid;
1283 char *path, *npath;
1284 unsigned int h;
1285 struct dfs_cache_entry *ce;
1286 struct dfs_info3_param *refs = NULL;
1287 int numrefs = 0;
1289 xid = get_xid();
1291 path = tcon->dfs_path + 1;
1293 rc = get_normalized_path(path, &npath);
1294 if (rc)
1295 goto out;
1297 mutex_lock(&dfs_cache_list_lock);
1298 ce = find_cache_entry(npath, &h);
1299 mutex_unlock(&dfs_cache_list_lock);
1301 if (IS_ERR(ce)) {
1302 rc = PTR_ERR(ce);
1303 goto out;
1306 if (!cache_entry_expired(ce))
1307 goto out;
1309 if (unlikely(!tcon->ses->server->ops->get_dfs_refer)) {
1310 rc = -EOPNOTSUPP;
1311 } else {
1312 rc = tcon->ses->server->ops->get_dfs_refer(xid, tcon->ses, path,
1313 &refs, &numrefs,
1314 dc->dc_nlsc,
1315 tcon->remap);
1316 if (!rc) {
1317 mutex_lock(&dfs_cache_list_lock);
1318 ce = __update_cache_entry(npath, refs, numrefs);
1319 mutex_unlock(&dfs_cache_list_lock);
1320 dump_refs(refs, numrefs);
1321 free_dfs_info_array(refs, numrefs);
1322 if (IS_ERR(ce))
1323 rc = PTR_ERR(ce);
1326 if (rc)
1327 cifs_dbg(FYI, "%s: failed to update expired entry\n", __func__);
1328 out:
1329 free_xid(xid);
1330 free_normalized_path(path, npath);
1334 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1335 * referral.
1337 * FIXME: ensure that all requests are sent to DFS root for refreshing the
1338 * cache.
1340 static void refresh_cache_worker(struct work_struct *work)
1342 struct dfs_cache *dc = container_of(work, struct dfs_cache,
1343 dc_refresh.work);
1344 struct dfs_cache_vol_info *vi;
1345 struct TCP_Server_Info *server;
1346 LIST_HEAD(list);
1347 struct cifs_tcon *tcon, *ntcon;
1349 mutex_lock(&dc->dc_lock);
1351 list_for_each_entry(vi, &dc->dc_vol_list, vi_list) {
1352 server = cifs_find_tcp_session(&vi->vi_vol);
1353 if (IS_ERR_OR_NULL(server))
1354 continue;
1355 if (server->tcpStatus != CifsGood)
1356 goto next;
1357 get_tcons(server, &list);
1358 list_for_each_entry_safe(tcon, ntcon, &list, ulist) {
1359 do_refresh_tcon(dc, tcon);
1360 list_del_init(&tcon->ulist);
1361 cifs_put_tcon(tcon);
1363 next:
1364 cifs_put_tcp_session(server, 0);
1366 queue_delayed_work(cifsiod_wq, &dc->dc_refresh, dc->dc_ttl * HZ);
1367 mutex_unlock(&dc->dc_lock);