1 // SPDX-License-Identifier: GPL-2.0
3 * DFS referral cache routines
5 * Copyright (c) 2018 Paulo Alcantara <palcantara@suse.de>
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>
17 #include "smb2proto.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_unicode.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 {
33 struct list_head t_list;
36 struct dfs_cache_entry {
37 struct hlist_node ce_hlist;
42 struct timespec64 ce_etime;
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 {
54 struct smb_vol vi_vol;
55 struct list_head vi_list;
60 struct nls_table *dc_nlsc;
61 struct list_head dc_vol_list;
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)
86 *npath = (char *)path;
88 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
91 convert_delimiter(*npath, '\\');
96 static inline void free_normalized_path(const char *path, char *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);
121 static void free_cache_entry(struct rcu_head *rcu)
123 struct dfs_cache_entry *ce = container_of(rcu, struct dfs_cache_entry,
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))
133 hlist_del_init_rcu(&ce->ce_hlist);
137 call_rcu(&ce->ce_rcu, free_cache_entry);
140 static void flush_cache_ents(void)
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)
156 * dfs cache /proc file
158 static int dfscache_proc_show(struct seq_file *m, void *v)
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);
169 hash_for_each_rcu(dfs_cache_htable, bucket, ce, ce_hlist) {
171 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
172 "interlink=%s,path_consumed=%d,expired=%s\n",
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",
183 ce->ce_tgthint == t ? " (target hint)" : "");
189 mutex_unlock(&dfs_cache_list_lock);
193 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
194 size_t count, loff_t *ppos)
199 rc = get_user(c, buffer);
206 cifs_dbg(FYI, "clearing dfs cache");
207 mutex_lock(&dfs_cache_list_lock);
209 mutex_unlock(&dfs_cache_list_lock);
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,
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");
251 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
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];
262 "path_consumed: %d\n"
263 "server_type: 0x%x\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);
276 #define dump_refs(r, n)
280 * dfs_cache_init - Initialize DFS referral cache.
282 * Return zero if initialized successfully, otherwise non-zero.
284 int dfs_cache_init(void)
288 dfs_cache_slab = kmem_cache_create("cifs_dfs_cache",
289 sizeof(struct dfs_cache_entry), 0,
290 SLAB_HWCACHE_ALIGN, NULL);
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__);
307 static inline unsigned int cache_entry_hash(const void *data, int size)
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)
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 = {
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);
354 return ERR_PTR(-ENOMEM);
355 t->t_name = kstrndup(name, strlen(name), GFP_KERNEL);
358 return ERR_PTR(-ENOMEM);
360 INIT_LIST_HEAD(&t->t_list);
365 * Copy DFS referral information to a cache entry and conditionally update
368 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
369 struct dfs_cache_entry *ce, const char *tgthint)
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);
387 if (tgthint && !strcasecmp(t->t_name, tgthint)) {
388 list_add(&t->t_list, &ce->ce_tlist);
391 list_add_tail(&t->t_list, &ce->ce_tlist);
396 ce->ce_tgthint = list_first_entry_or_null(&ce->ce_tlist,
397 struct dfs_cache_tgt, t_list);
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,
407 struct dfs_cache_entry *ce;
410 ce = kmem_cache_zalloc(dfs_cache_slab, GFP_KERNEL);
412 return ERR_PTR(-ENOMEM);
414 ce->ce_path = kstrdup_const(path, GFP_KERNEL);
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);
425 kmem_cache_free(dfs_cache_slab, ce);
431 static void remove_oldest_entry(void)
434 struct dfs_cache_entry *ce;
435 struct dfs_cache_entry *to_del = NULL;
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)
444 cifs_dbg(FYI, "%s: no entry to remove", __func__);
447 cifs_dbg(FYI, "%s: removing entry", __func__);
449 flush_cache_ent(to_del);
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);
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);
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);
482 static struct dfs_cache_entry *__find_cache_entry(unsigned int hash,
485 struct dfs_cache_entry *ce;
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))) {
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);
506 return found ? ce : ERR_PTR(-ENOENT);
510 * Find a DFS cache entry in hash table and optionally check prefix path against
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,
518 *hash = cache_entry_hash(path, strlen(path));
519 return __find_cache_entry(*hash, path);
522 static inline void destroy_slab_cache(void)
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);
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)
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);
552 mutex_destroy(&dfs_cache.dc_lock);
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,
567 struct dfs_cache_entry *ce;
570 ce = find_cache_entry(path, &h);
574 if (ce->ce_tgthint) {
575 s = ce->ce_tgthint->t_name;
576 th = kstrndup(s, strlen(s), GFP_KERNEL);
578 return ERR_PTR(-ENOMEM);
584 rc = copy_ref_data(refs, numrefs, ce, th);
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)
600 struct dfs_info3_param *refs = NULL;
603 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
605 * Check if caller provided enough parameters to update an expired
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);
620 ce = __update_cache_entry(path, refs, numrefs);
622 dump_refs(refs, numrefs);
623 free_dfs_info_array(refs, numrefs);
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)
644 struct dfs_cache_entry *ce;
645 struct dfs_info3_param *nrefs;
648 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
650 ce = find_cache_entry(path, &h);
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.
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
664 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
665 ce = ERR_PTR(-EOPNOTSUPP);
668 if (unlikely(!nls_codepage)) {
669 ce = ERR_PTR(-EINVAL);
676 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
679 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
680 &numnrefs, nls_codepage,
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);
707 /* Just return the found cache entry in case @noreq is set */
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,
716 cifs_dbg(FYI, "%s: failed to update expired entry\n",
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)
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);
737 ref->path_consumed = ce->ce_path_consumed;
739 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
740 if (!ref->node_name) {
745 ref->ttl = ce->ce_ttl;
746 ref->server_type = ce->ce_srvtype;
747 ref->ref_flag = ce->ce_flags;
752 kfree(ref->path_name);
753 ref->path_name = NULL;
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)
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);
776 it->it_name = kstrndup(t->t_name, strlen(t->t_name),
783 if (ce->ce_tgthint == t)
784 list_add(&it->it_list, head);
786 list_add_tail(&it->it_list, head);
788 tl->tl_numtgts = ce->ce_numtgts;
793 list_for_each_entry_safe(it, nit, head, it_list) {
801 * dfs_cache_find - find a DFS cache entry
803 * If it doesn't find the cache entry, then it will get a DFS referral
804 * for @path and create a new entry.
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.
809 * These parameters are passed down to the get_dfs_refer() call if it
810 * needs to be issued:
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.
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.
820 * Return zero if the target was found, otherwise non-zero.
822 int 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)
829 struct dfs_cache_entry *ce;
831 if (unlikely(!is_path_valid(path)))
834 rc = get_normalized_path(path, &npath);
838 mutex_lock(&dfs_cache_list_lock);
839 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
842 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
846 rc = get_tgt_list(ce, tgt_list);
850 mutex_unlock(&dfs_cache_list_lock);
851 free_normalized_path(path, npath);
856 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
857 * the currently connected server.
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.
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.
867 * Return 0 if successful.
868 * Return -ENOENT if the entry was not found.
869 * Return non-zero for other errors.
871 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
872 struct dfs_cache_tgt_list *tgt_list)
876 struct dfs_cache_entry *ce;
878 if (unlikely(!is_path_valid(path)))
881 rc = get_normalized_path(path, &npath);
885 mutex_lock(&dfs_cache_list_lock);
886 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
893 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
897 rc = get_tgt_list(ce, tgt_list);
899 mutex_unlock(&dfs_cache_list_lock);
900 free_normalized_path(path, npath);
905 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
907 * If it doesn't find the cache entry, then it will get a DFS referral for @path
908 * and create a new entry.
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.
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
920 * Return zero if the target hint was updated successfully, otherwise non-zero.
922 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
923 const struct nls_table *nls_codepage, int remap,
925 const struct dfs_cache_tgt_iterator *it)
929 struct dfs_cache_entry *ce;
930 struct dfs_cache_tgt *t;
932 if (unlikely(!is_path_valid(path)))
935 rc = get_normalized_path(path, &npath);
939 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
941 mutex_lock(&dfs_cache_list_lock);
942 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
952 if (likely(!strcasecmp(it->it_name, t->t_name)))
955 list_for_each_entry(t, &ce->ce_tlist, t_list) {
956 if (!strcasecmp(t->t_name, it->it_name)) {
958 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
965 mutex_unlock(&dfs_cache_list_lock);
966 free_normalized_path(path, npath);
971 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
972 * without sending any requests to the currently connected server.
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.
978 * @path: path to lookup in DFS referral cache.
979 * @it: target iterator which contains the target hint to update the cache
982 * Return zero if the target hint was updated successfully, otherwise non-zero.
984 int dfs_cache_noreq_update_tgthint(const char *path,
985 const struct dfs_cache_tgt_iterator *it)
989 struct dfs_cache_entry *ce;
990 struct dfs_cache_tgt *t;
992 if (unlikely(!is_path_valid(path)) || !it)
995 rc = get_normalized_path(path, &npath);
999 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1001 mutex_lock(&dfs_cache_list_lock);
1003 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1013 if (unlikely(!strcasecmp(it->it_name, t->t_name)))
1016 list_for_each_entry(t, &ce->ce_tlist, t_list) {
1017 if (!strcasecmp(t->t_name, it->it_name)) {
1019 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1026 mutex_unlock(&dfs_cache_list_lock);
1027 free_normalized_path(path, npath);
1032 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1033 * target iterator (@it).
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.
1039 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1041 int dfs_cache_get_tgt_referral(const char *path,
1042 const struct dfs_cache_tgt_iterator *it,
1043 struct dfs_info3_param *ref)
1047 struct dfs_cache_entry *ce;
1052 if (unlikely(!is_path_valid(path)))
1055 rc = get_normalized_path(path, &npath);
1059 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1061 mutex_lock(&dfs_cache_list_lock);
1063 ce = find_cache_entry(npath, &h);
1069 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1071 rc = setup_ref(path, ce, ref, it->it_name);
1074 mutex_unlock(&dfs_cache_list_lock);
1075 free_normalized_path(path, npath);
1079 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1081 memcpy(new, vol, sizeof(*new));
1083 if (vol->username) {
1084 new->username = kstrndup(vol->username, strlen(vol->username),
1089 if (vol->password) {
1090 new->password = kstrndup(vol->password, strlen(vol->password),
1093 goto err_free_username;
1096 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1097 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1099 goto err_free_password;
1101 if (vol->domainname) {
1102 new->domainname = kstrndup(vol->domainname,
1103 strlen(vol->domainname), GFP_KERNEL);
1104 if (!new->domainname)
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;
1114 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1115 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1118 goto err_free_iocharset;
1124 kfree(new->iocharset);
1125 err_free_domainname:
1126 kfree(new->domainname);
1130 kzfree(new->password);
1132 kfree(new->username);
1138 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1139 * DFS cache refresh worker.
1141 * @vol: cifs volume.
1142 * @fullpath: origin full path.
1144 * Return zero if volume was set up correctly, otherwise non-zero.
1146 int dfs_cache_add_vol(struct smb_vol *vol, const char *fullpath)
1149 struct dfs_cache_vol_info *vi;
1151 if (!vol || !fullpath)
1154 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1156 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1160 vi->vi_fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1161 if (!vi->vi_fullpath) {
1166 rc = dup_vol(vol, &vi->vi_vol);
1168 goto err_free_fullpath;
1170 mutex_lock(&dfs_cache.dc_lock);
1171 list_add_tail(&vi->vi_list, &dfs_cache.dc_vol_list);
1172 mutex_unlock(&dfs_cache.dc_lock);
1176 kfree(vi->vi_fullpath);
1182 static inline struct dfs_cache_vol_info *find_vol(const char *fullpath)
1184 struct dfs_cache_vol_info *vi;
1186 list_for_each_entry(vi, &dfs_cache.dc_vol_list, vi_list) {
1187 cifs_dbg(FYI, "%s: vi->vi_fullpath: %s\n", __func__,
1189 if (!strcasecmp(vi->vi_fullpath, fullpath))
1192 return ERR_PTR(-ENOENT);
1196 * dfs_cache_update_vol - update vol info in DFS cache after failover
1198 * @fullpath: fullpath to look up in volume list.
1199 * @server: TCP ses pointer.
1201 * Return zero if volume was updated, otherwise non-zero.
1203 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1206 struct dfs_cache_vol_info *vi;
1208 if (!fullpath || !server)
1211 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1213 mutex_lock(&dfs_cache.dc_lock);
1215 vi = find_vol(fullpath);
1221 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1222 memcpy(&vi->vi_vol.dstaddr, &server->dstaddr,
1223 sizeof(vi->vi_vol.dstaddr));
1227 mutex_unlock(&dfs_cache.dc_lock);
1232 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1234 * @fullpath: fullpath to look up in volume list.
1236 void dfs_cache_del_vol(const char *fullpath)
1238 struct dfs_cache_vol_info *vi;
1240 if (!fullpath || !*fullpath)
1243 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1245 mutex_lock(&dfs_cache.dc_lock);
1246 vi = find_vol(fullpath);
1249 mutex_unlock(&dfs_cache.dc_lock);
1252 /* Get all tcons that are within a DFS namespace and can be refreshed */
1253 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1255 struct cifs_ses *ses;
1256 struct cifs_tcon *tcon;
1258 INIT_LIST_HEAD(head);
1260 spin_lock(&cifs_tcp_ses_lock);
1261 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1262 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1263 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1266 list_add_tail(&tcon->ulist, head);
1269 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1270 ses->tcon_ipc->dfs_path) {
1271 list_add_tail(&ses->tcon_ipc->ulist, head);
1274 spin_unlock(&cifs_tcp_ses_lock);
1277 /* Refresh DFS cache entry from a given tcon */
1278 static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon)
1284 struct dfs_cache_entry *ce;
1285 struct dfs_info3_param *refs = NULL;
1290 path = tcon->dfs_path + 1;
1292 rc = get_normalized_path(path, &npath);
1296 mutex_lock(&dfs_cache_list_lock);
1297 ce = find_cache_entry(npath, &h);
1298 mutex_unlock(&dfs_cache_list_lock);
1305 if (!cache_entry_expired(ce))
1308 if (unlikely(!tcon->ses->server->ops->get_dfs_refer)) {
1311 rc = tcon->ses->server->ops->get_dfs_refer(xid, tcon->ses, path,
1316 mutex_lock(&dfs_cache_list_lock);
1317 ce = __update_cache_entry(npath, refs, numrefs);
1318 mutex_unlock(&dfs_cache_list_lock);
1319 dump_refs(refs, numrefs);
1320 free_dfs_info_array(refs, numrefs);
1326 cifs_dbg(FYI, "%s: failed to update expired entry\n", __func__);
1329 free_normalized_path(path, npath);
1333 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1336 * FIXME: ensure that all requests are sent to DFS root for refreshing the
1339 static void refresh_cache_worker(struct work_struct *work)
1341 struct dfs_cache *dc = container_of(work, struct dfs_cache,
1343 struct dfs_cache_vol_info *vi;
1344 struct TCP_Server_Info *server;
1346 struct cifs_tcon *tcon, *ntcon;
1348 mutex_lock(&dc->dc_lock);
1350 list_for_each_entry(vi, &dc->dc_vol_list, vi_list) {
1351 server = cifs_find_tcp_session(&vi->vi_vol);
1352 if (IS_ERR_OR_NULL(server))
1354 if (server->tcpStatus != CifsGood)
1356 get_tcons(server, &list);
1357 list_for_each_entry_safe(tcon, ntcon, &list, ulist) {
1358 do_refresh_tcon(dc, tcon);
1359 list_del_init(&tcon->ulist);
1360 cifs_put_tcon(tcon);
1363 cifs_put_tcp_session(server, 0);
1365 queue_delayed_work(cifsiod_wq, &dc->dc_refresh, dc->dc_ttl * HZ);
1366 mutex_unlock(&dc->dc_lock);