1 // SPDX-License-Identifier: GPL-2.0
3 * DFS referral cache routines
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),
784 if (ce->ce_tgthint == t)
785 list_add(&it->it_list, head);
787 list_add_tail(&it->it_list, head);
789 tl->tl_numtgts = ce->ce_numtgts;
794 list_for_each_entry_safe(it, nit, head, it_list) {
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:
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)
830 struct dfs_cache_entry *ce;
832 if (unlikely(!is_path_valid(path)))
835 rc = get_normalized_path(path, &npath);
839 mutex_lock(&dfs_cache_list_lock);
840 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
843 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
847 rc = get_tgt_list(ce, tgt_list);
851 mutex_unlock(&dfs_cache_list_lock);
852 free_normalized_path(path, npath);
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)
877 struct dfs_cache_entry *ce;
879 if (unlikely(!is_path_valid(path)))
882 rc = get_normalized_path(path, &npath);
886 mutex_lock(&dfs_cache_list_lock);
887 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
894 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
898 rc = get_tgt_list(ce, tgt_list);
900 mutex_unlock(&dfs_cache_list_lock);
901 free_normalized_path(path, npath);
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.
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,
926 const struct dfs_cache_tgt_iterator *it)
930 struct dfs_cache_entry *ce;
931 struct dfs_cache_tgt *t;
933 if (unlikely(!is_path_valid(path)))
936 rc = get_normalized_path(path, &npath);
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);
953 if (likely(!strcasecmp(it->it_name, t->t_name)))
956 list_for_each_entry(t, &ce->ce_tlist, t_list) {
957 if (!strcasecmp(t->t_name, it->it_name)) {
959 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
966 mutex_unlock(&dfs_cache_list_lock);
967 free_normalized_path(path, npath);
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
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)
990 struct dfs_cache_entry *ce;
991 struct dfs_cache_tgt *t;
993 if (unlikely(!is_path_valid(path)) || !it)
996 rc = get_normalized_path(path, &npath);
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);
1014 if (unlikely(!strcasecmp(it->it_name, t->t_name)))
1017 list_for_each_entry(t, &ce->ce_tlist, t_list) {
1018 if (!strcasecmp(t->t_name, it->it_name)) {
1020 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1027 mutex_unlock(&dfs_cache_list_lock);
1028 free_normalized_path(path, npath);
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)
1048 struct dfs_cache_entry *ce;
1053 if (unlikely(!is_path_valid(path)))
1056 rc = get_normalized_path(path, &npath);
1060 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1062 mutex_lock(&dfs_cache_list_lock);
1064 ce = find_cache_entry(npath, &h);
1070 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1072 rc = setup_ref(path, ce, ref, it->it_name);
1075 mutex_unlock(&dfs_cache_list_lock);
1076 free_normalized_path(path, npath);
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),
1090 if (vol->password) {
1091 new->password = kstrndup(vol->password, strlen(vol->password),
1094 goto err_free_username;
1097 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1098 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
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)
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;
1115 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1116 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1119 goto err_free_iocharset;
1125 kfree(new->iocharset);
1126 err_free_domainname:
1127 kfree(new->domainname);
1131 kzfree(new->password);
1133 kfree(new->username);
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)
1150 struct dfs_cache_vol_info *vi;
1152 if (!vol || !fullpath)
1155 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1157 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1161 vi->vi_fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1162 if (!vi->vi_fullpath) {
1167 rc = dup_vol(vol, &vi->vi_vol);
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);
1177 kfree(vi->vi_fullpath);
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__,
1190 if (!strcasecmp(vi->vi_fullpath, fullpath))
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)
1207 struct dfs_cache_vol_info *vi;
1209 if (!fullpath || !server)
1212 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1214 mutex_lock(&dfs_cache.dc_lock);
1216 vi = find_vol(fullpath);
1222 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1223 memcpy(&vi->vi_vol.dstaddr, &server->dstaddr,
1224 sizeof(vi->vi_vol.dstaddr));
1228 mutex_unlock(&dfs_cache.dc_lock);
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)
1244 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1246 mutex_lock(&dfs_cache.dc_lock);
1247 vi = find_vol(fullpath);
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 &&
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)
1285 struct dfs_cache_entry *ce;
1286 struct dfs_info3_param *refs = NULL;
1291 path = tcon->dfs_path + 1;
1293 rc = get_normalized_path(path, &npath);
1297 mutex_lock(&dfs_cache_list_lock);
1298 ce = find_cache_entry(npath, &h);
1299 mutex_unlock(&dfs_cache_list_lock);
1306 if (!cache_entry_expired(ce))
1309 if (unlikely(!tcon->ses->server->ops->get_dfs_refer)) {
1312 rc = tcon->ses->server->ops->get_dfs_refer(xid, tcon->ses, path,
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);
1327 cifs_dbg(FYI, "%s: failed to update expired entry\n", __func__);
1330 free_normalized_path(path, npath);
1334 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1337 * FIXME: ensure that all requests are sent to DFS root for refreshing the
1340 static void refresh_cache_worker(struct work_struct *work)
1342 struct dfs_cache *dc = container_of(work, struct dfs_cache,
1344 struct dfs_cache_vol_info *vi;
1345 struct TCP_Server_Info *server;
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))
1355 if (server->tcpStatus != CifsGood)
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);
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);