1 // SPDX-License-Identifier: GPL-2.0
3 * DFS referral cache routines
5 * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
8 #include <linux/jhash.h>
9 #include <linux/ktime.h>
10 #include <linux/slab.h>
11 #include <linux/proc_fs.h>
12 #include <linux/nls.h>
13 #include <linux/workqueue.h>
16 #include "smb2proto.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
22 #include "dfs_cache.h"
24 #define CACHE_HTABLE_SIZE 32
25 #define CACHE_MAX_ENTRIES 64
27 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
28 DFSREF_STORAGE_SERVER))
30 struct cache_dfs_tgt {
32 struct list_head list;
36 struct hlist_node hlist;
41 struct timespec64 etime;
44 struct list_head tlist;
45 struct cache_dfs_tgt *tgthint;
50 spinlock_t smb_vol_lock;
51 struct smb_vol smb_vol;
53 struct list_head list;
54 struct list_head rlist;
58 static struct kmem_cache *cache_slab __read_mostly;
59 static struct workqueue_struct *dfscache_wq __read_mostly;
62 static DEFINE_SPINLOCK(cache_ttl_lock);
64 static struct nls_table *cache_nlsc;
67 * Number of entries in the cache
69 static atomic_t cache_count;
71 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
72 static DECLARE_RWSEM(htable_rw_lock);
74 static LIST_HEAD(vol_list);
75 static DEFINE_SPINLOCK(vol_list_lock);
77 static void refresh_cache_worker(struct work_struct *work);
79 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
81 static int get_normalized_path(const char *path, char **npath)
83 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
87 *npath = (char *)path;
89 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
92 convert_delimiter(*npath, '\\');
97 static inline void free_normalized_path(const char *path, char *npath)
103 static inline bool cache_entry_expired(const struct cache_entry *ce)
105 struct timespec64 ts;
107 ktime_get_coarse_real_ts64(&ts);
108 return timespec64_compare(&ts, &ce->etime) >= 0;
111 static inline void free_tgts(struct cache_entry *ce)
113 struct cache_dfs_tgt *t, *n;
115 list_for_each_entry_safe(t, n, &ce->tlist, list) {
122 static inline void flush_cache_ent(struct cache_entry *ce)
124 hlist_del_init(&ce->hlist);
127 atomic_dec(&cache_count);
128 kmem_cache_free(cache_slab, ce);
131 static void flush_cache_ents(void)
135 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
136 struct hlist_head *l = &cache_htable[i];
137 struct hlist_node *n;
138 struct cache_entry *ce;
140 hlist_for_each_entry_safe(ce, n, l, hlist) {
141 if (!hlist_unhashed(&ce->hlist))
148 * dfs cache /proc file
150 static int dfscache_proc_show(struct seq_file *m, void *v)
153 struct cache_entry *ce;
154 struct cache_dfs_tgt *t;
156 seq_puts(m, "DFS cache\n---------\n");
158 down_read(&htable_rw_lock);
159 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
160 struct hlist_head *l = &cache_htable[i];
162 hlist_for_each_entry(ce, l, hlist) {
163 if (hlist_unhashed(&ce->hlist))
167 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
168 "interlink=%s,path_consumed=%d,expired=%s\n",
170 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
171 ce->ttl, ce->etime.tv_nsec,
172 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
174 cache_entry_expired(ce) ? "yes" : "no");
176 list_for_each_entry(t, &ce->tlist, list) {
177 seq_printf(m, " %s%s\n",
179 ce->tgthint == t ? " (target hint)" : "");
183 up_read(&htable_rw_lock);
188 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
189 size_t count, loff_t *ppos)
194 rc = get_user(c, buffer);
201 cifs_dbg(FYI, "clearing dfs cache\n");
203 down_write(&htable_rw_lock);
205 up_write(&htable_rw_lock);
210 static int dfscache_proc_open(struct inode *inode, struct file *file)
212 return single_open(file, dfscache_proc_show, NULL);
215 const struct proc_ops dfscache_proc_ops = {
216 .proc_open = dfscache_proc_open,
217 .proc_read = seq_read,
218 .proc_lseek = seq_lseek,
219 .proc_release = single_release,
220 .proc_write = dfscache_proc_write,
223 #ifdef CONFIG_CIFS_DEBUG2
224 static inline void dump_tgts(const struct cache_entry *ce)
226 struct cache_dfs_tgt *t;
228 cifs_dbg(FYI, "target list:\n");
229 list_for_each_entry(t, &ce->tlist, list) {
230 cifs_dbg(FYI, " %s%s\n", t->name,
231 ce->tgthint == t ? " (target hint)" : "");
235 static inline void dump_ce(const struct cache_entry *ce)
237 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,interlink=%s,path_consumed=%d,expired=%s\n",
239 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
241 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
243 cache_entry_expired(ce) ? "yes" : "no");
247 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
251 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
252 for (i = 0; i < numrefs; i++) {
253 const struct dfs_info3_param *ref = &refs[i];
258 "path_consumed: %d\n"
259 "server_type: 0x%x\n"
264 ref->flags, ref->path_consumed, ref->server_type,
265 ref->ref_flag, ref->path_name, ref->node_name,
266 ref->ttl, ref->ttl / 60);
272 #define dump_refs(r, n)
276 * dfs_cache_init - Initialize DFS referral cache.
278 * Return zero if initialized successfully, otherwise non-zero.
280 int dfs_cache_init(void)
285 dfscache_wq = alloc_workqueue("cifs-dfscache",
286 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
290 cache_slab = kmem_cache_create("cifs_dfs_cache",
291 sizeof(struct cache_entry), 0,
292 SLAB_HWCACHE_ALIGN, NULL);
298 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
299 INIT_HLIST_HEAD(&cache_htable[i]);
301 atomic_set(&cache_count, 0);
302 cache_nlsc = load_nls_default();
304 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
308 destroy_workqueue(dfscache_wq);
312 static inline unsigned int cache_entry_hash(const void *data, int size)
316 h = jhash(data, size, 0);
317 return h & (CACHE_HTABLE_SIZE - 1);
320 /* Check whether second path component of @path is SYSVOL or NETLOGON */
321 static inline bool is_sysvol_or_netlogon(const char *path)
326 s = strchr(path + 1, sep) + 1;
327 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
328 !strncasecmp(s, "netlogon", strlen("netlogon"));
331 /* Return target hint of a DFS cache entry */
332 static inline char *get_tgt_name(const struct cache_entry *ce)
334 struct cache_dfs_tgt *t = ce->tgthint;
336 return t ? t->name : ERR_PTR(-ENOENT);
339 /* Return expire time out of a new entry's TTL */
340 static inline struct timespec64 get_expire_time(int ttl)
342 struct timespec64 ts = {
346 struct timespec64 now;
348 ktime_get_coarse_real_ts64(&now);
349 return timespec64_add(now, ts);
352 /* Allocate a new DFS target */
353 static struct cache_dfs_tgt *alloc_target(const char *name)
355 struct cache_dfs_tgt *t;
357 t = kmalloc(sizeof(*t), GFP_ATOMIC);
359 return ERR_PTR(-ENOMEM);
360 t->name = kstrndup(name, strlen(name), GFP_ATOMIC);
363 return ERR_PTR(-ENOMEM);
365 INIT_LIST_HEAD(&t->list);
370 * Copy DFS referral information to a cache entry and conditionally update
373 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
374 struct cache_entry *ce, const char *tgthint)
378 ce->ttl = refs[0].ttl;
379 ce->etime = get_expire_time(ce->ttl);
380 ce->srvtype = refs[0].server_type;
381 ce->flags = refs[0].ref_flag;
382 ce->path_consumed = refs[0].path_consumed;
384 for (i = 0; i < numrefs; i++) {
385 struct cache_dfs_tgt *t;
387 t = alloc_target(refs[i].node_name);
392 if (tgthint && !strcasecmp(t->name, tgthint)) {
393 list_add(&t->list, &ce->tlist);
396 list_add_tail(&t->list, &ce->tlist);
401 ce->tgthint = list_first_entry_or_null(&ce->tlist,
402 struct cache_dfs_tgt, list);
407 /* Allocate a new cache entry */
408 static struct cache_entry *alloc_cache_entry(const char *path,
409 const struct dfs_info3_param *refs,
412 struct cache_entry *ce;
415 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
417 return ERR_PTR(-ENOMEM);
419 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
421 kmem_cache_free(cache_slab, ce);
422 return ERR_PTR(-ENOMEM);
424 INIT_HLIST_NODE(&ce->hlist);
425 INIT_LIST_HEAD(&ce->tlist);
427 rc = copy_ref_data(refs, numrefs, ce, NULL);
430 kmem_cache_free(cache_slab, ce);
436 /* Must be called with htable_rw_lock held */
437 static void remove_oldest_entry(void)
440 struct cache_entry *ce;
441 struct cache_entry *to_del = NULL;
443 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
444 struct hlist_head *l = &cache_htable[i];
446 hlist_for_each_entry(ce, l, hlist) {
447 if (hlist_unhashed(&ce->hlist))
449 if (!to_del || timespec64_compare(&ce->etime,
456 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
460 cifs_dbg(FYI, "%s: removing entry\n", __func__);
462 flush_cache_ent(to_del);
465 /* Add a new DFS cache entry */
466 static int add_cache_entry(const char *path, unsigned int hash,
467 struct dfs_info3_param *refs, int numrefs)
469 struct cache_entry *ce;
471 ce = alloc_cache_entry(path, refs, numrefs);
475 spin_lock(&cache_ttl_lock);
478 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
480 cache_ttl = min_t(int, cache_ttl, ce->ttl);
481 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
483 spin_unlock(&cache_ttl_lock);
485 down_write(&htable_rw_lock);
486 hlist_add_head(&ce->hlist, &cache_htable[hash]);
488 up_write(&htable_rw_lock);
493 static struct cache_entry *__lookup_cache_entry(const char *path)
495 struct cache_entry *ce;
499 h = cache_entry_hash(path, strlen(path));
501 hlist_for_each_entry(ce, &cache_htable[h], hlist) {
502 if (!strcasecmp(path, ce->path)) {
510 ce = ERR_PTR(-ENOENT);
515 * Find a DFS cache entry in hash table and optionally check prefix path against
517 * Use whole path components in the match.
518 * Must be called with htable_rw_lock held.
520 * Return ERR_PTR(-ENOENT) if the entry is not found.
522 static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
524 struct cache_entry *ce = ERR_PTR(-ENOENT);
531 npath = kstrndup(path, strlen(path), GFP_KERNEL);
533 return ERR_PTR(-ENOMEM);
537 while ((s = strchr(s, sep)) && ++cnt < 3)
541 h = cache_entry_hash(path, strlen(path));
542 ce = __lookup_cache_entry(path);
546 * Handle paths that have more than two path components and are a complete prefix of the DFS
547 * referral request path (@path).
549 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
551 h = cache_entry_hash(npath, strlen(npath));
552 e = npath + strlen(npath) - 1;
556 /* skip separators */
557 while (e > s && *e == sep)
565 ce = __lookup_cache_entry(npath);
567 h = cache_entry_hash(npath, strlen(npath));
572 /* backward until separator */
573 while (e > s && *e != sep)
583 static void __vol_release(struct vol_info *vi)
587 cifs_cleanup_volume_info_contents(&vi->smb_vol);
591 static void vol_release(struct kref *kref)
593 struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
595 spin_lock(&vol_list_lock);
597 spin_unlock(&vol_list_lock);
601 static inline void free_vol_list(void)
603 struct vol_info *vi, *nvi;
605 list_for_each_entry_safe(vi, nvi, &vol_list, list) {
606 list_del_init(&vi->list);
612 * dfs_cache_destroy - destroy DFS referral cache
614 void dfs_cache_destroy(void)
616 cancel_delayed_work_sync(&refresh_task);
617 unload_nls(cache_nlsc);
620 kmem_cache_destroy(cache_slab);
621 destroy_workqueue(dfscache_wq);
623 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
626 /* Must be called with htable_rw_lock held */
627 static int __update_cache_entry(const char *path,
628 const struct dfs_info3_param *refs,
632 struct cache_entry *ce;
635 ce = lookup_cache_entry(path, NULL);
640 s = ce->tgthint->name;
641 th = kstrndup(s, strlen(s), GFP_ATOMIC);
649 rc = copy_ref_data(refs, numrefs, ce, th);
656 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
657 const struct nls_table *nls_codepage, int remap,
658 const char *path, struct dfs_info3_param **refs,
661 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
663 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
665 if (unlikely(!nls_codepage))
671 return ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs,
672 nls_codepage, remap);
675 /* Update an expired cache entry by getting a new DFS referral from server */
676 static int update_cache_entry(const char *path,
677 const struct dfs_info3_param *refs,
683 down_write(&htable_rw_lock);
684 rc = __update_cache_entry(path, refs, numrefs);
685 up_write(&htable_rw_lock);
691 * Find, create or update a DFS cache entry.
693 * If the entry wasn't found, it will create a new one. Or if it was found but
694 * expired, then it will update the entry accordingly.
696 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
697 * handle them properly.
699 static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
700 const struct nls_table *nls_codepage, int remap,
701 const char *path, bool noreq)
705 struct cache_entry *ce;
706 struct dfs_info3_param *refs = NULL;
710 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
712 down_read(&htable_rw_lock);
714 ce = lookup_cache_entry(path, &hash);
717 * If @noreq is set, no requests will be sent to the server. Just return
721 up_read(&htable_rw_lock);
722 return PTR_ERR_OR_ZERO(ce);
726 if (!cache_entry_expired(ce)) {
728 up_read(&htable_rw_lock);
735 up_read(&htable_rw_lock);
738 * No entry was found.
740 * Request a new DFS referral in order to create a new cache entry, or
741 * updating an existing one.
743 rc = get_dfs_referral(xid, ses, nls_codepage, remap, path,
748 dump_refs(refs, numrefs);
751 rc = update_cache_entry(path, refs, numrefs);
755 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
756 cifs_dbg(FYI, "%s: reached max cache size (%d)\n",
757 __func__, CACHE_MAX_ENTRIES);
758 down_write(&htable_rw_lock);
759 remove_oldest_entry();
760 up_write(&htable_rw_lock);
763 rc = add_cache_entry(path, hash, refs, numrefs);
765 atomic_inc(&cache_count);
768 free_dfs_info_array(refs, numrefs);
773 * Set up a DFS referral from a given cache entry.
775 * Must be called with htable_rw_lock held.
777 static int setup_referral(const char *path, struct cache_entry *ce,
778 struct dfs_info3_param *ref, const char *target)
782 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
784 memset(ref, 0, sizeof(*ref));
786 ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC);
790 ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC);
791 if (!ref->node_name) {
796 ref->path_consumed = ce->path_consumed;
798 ref->server_type = ce->srvtype;
799 ref->ref_flag = ce->flags;
804 kfree(ref->path_name);
805 ref->path_name = NULL;
809 /* Return target list of a DFS cache entry */
810 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
813 struct list_head *head = &tl->tl_list;
814 struct cache_dfs_tgt *t;
815 struct dfs_cache_tgt_iterator *it, *nit;
817 memset(tl, 0, sizeof(*tl));
818 INIT_LIST_HEAD(head);
820 list_for_each_entry(t, &ce->tlist, list) {
821 it = kzalloc(sizeof(*it), GFP_ATOMIC);
827 it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC);
834 if (ce->tgthint == t)
835 list_add(&it->it_list, head);
837 list_add_tail(&it->it_list, head);
840 tl->tl_numtgts = ce->numtgts;
845 list_for_each_entry_safe(it, nit, head, it_list) {
853 * dfs_cache_find - find a DFS cache entry
855 * If it doesn't find the cache entry, then it will get a DFS referral
856 * for @path and create a new entry.
858 * In case the cache entry exists but expired, it will get a DFS referral
859 * for @path and then update the respective cache entry.
861 * These parameters are passed down to the get_dfs_refer() call if it
862 * needs to be issued:
864 * @ses: smb session to issue the request on
865 * @nls_codepage: charset conversion
866 * @remap: path character remapping type
867 * @path: path to lookup in DFS referral cache.
869 * @ref: when non-NULL, store single DFS referral result in it.
870 * @tgt_list: when non-NULL, store complete DFS target list in it.
872 * Return zero if the target was found, otherwise non-zero.
874 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
875 const struct nls_table *nls_codepage, int remap,
876 const char *path, struct dfs_info3_param *ref,
877 struct dfs_cache_tgt_list *tgt_list)
881 struct cache_entry *ce;
883 rc = get_normalized_path(path, &npath);
887 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
891 down_read(&htable_rw_lock);
893 ce = lookup_cache_entry(npath, NULL);
895 up_read(&htable_rw_lock);
901 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
905 rc = get_targets(ce, tgt_list);
907 up_read(&htable_rw_lock);
910 free_normalized_path(path, npath);
915 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
916 * the currently connected server.
918 * NOTE: This function will neither update a cache entry in case it was
919 * expired, nor create a new cache entry if @path hasn't been found. It heavily
920 * relies on an existing cache entry.
922 * @path: path to lookup in the DFS referral cache.
923 * @ref: when non-NULL, store single DFS referral result in it.
924 * @tgt_list: when non-NULL, store complete DFS target list in it.
926 * Return 0 if successful.
927 * Return -ENOENT if the entry was not found.
928 * Return non-zero for other errors.
930 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
931 struct dfs_cache_tgt_list *tgt_list)
935 struct cache_entry *ce;
937 rc = get_normalized_path(path, &npath);
941 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
943 down_read(&htable_rw_lock);
945 ce = lookup_cache_entry(npath, NULL);
952 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
956 rc = get_targets(ce, tgt_list);
959 up_read(&htable_rw_lock);
960 free_normalized_path(path, npath);
966 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
968 * If it doesn't find the cache entry, then it will get a DFS referral for @path
969 * and create a new entry.
971 * In case the cache entry exists but expired, it will get a DFS referral
972 * for @path and then update the respective cache entry.
976 * @nls_codepage: charset conversion
977 * @remap: type of character remapping for paths
978 * @path: path to lookup in DFS referral cache.
979 * @it: DFS target iterator
981 * Return zero if the target hint was updated successfully, otherwise non-zero.
983 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
984 const struct nls_table *nls_codepage, int remap,
986 const struct dfs_cache_tgt_iterator *it)
990 struct cache_entry *ce;
991 struct cache_dfs_tgt *t;
993 rc = get_normalized_path(path, &npath);
997 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
999 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
1003 down_write(&htable_rw_lock);
1005 ce = lookup_cache_entry(npath, NULL);
1013 if (likely(!strcasecmp(it->it_name, t->name)))
1016 list_for_each_entry(t, &ce->tlist, list) {
1017 if (!strcasecmp(t->name, it->it_name)) {
1019 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1026 up_write(&htable_rw_lock);
1028 free_normalized_path(path, npath);
1034 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1035 * without sending any requests to the currently connected server.
1037 * NOTE: This function will neither update a cache entry in case it was
1038 * expired, nor create a new cache entry if @path hasn't been found. It heavily
1039 * relies on an existing cache entry.
1041 * @path: path to lookup in DFS referral cache.
1042 * @it: target iterator which contains the target hint to update the cache
1045 * Return zero if the target hint was updated successfully, otherwise non-zero.
1047 int dfs_cache_noreq_update_tgthint(const char *path,
1048 const struct dfs_cache_tgt_iterator *it)
1052 struct cache_entry *ce;
1053 struct cache_dfs_tgt *t;
1058 rc = get_normalized_path(path, &npath);
1062 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1064 down_write(&htable_rw_lock);
1066 ce = lookup_cache_entry(npath, NULL);
1075 if (unlikely(!strcasecmp(it->it_name, t->name)))
1078 list_for_each_entry(t, &ce->tlist, list) {
1079 if (!strcasecmp(t->name, it->it_name)) {
1081 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1088 up_write(&htable_rw_lock);
1089 free_normalized_path(path, npath);
1095 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1096 * target iterator (@it).
1098 * @path: path to lookup in DFS referral cache.
1099 * @it: DFS target iterator.
1100 * @ref: DFS referral pointer to set up the gathered information.
1102 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1104 int dfs_cache_get_tgt_referral(const char *path,
1105 const struct dfs_cache_tgt_iterator *it,
1106 struct dfs_info3_param *ref)
1110 struct cache_entry *ce;
1115 rc = get_normalized_path(path, &npath);
1119 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1121 down_read(&htable_rw_lock);
1123 ce = lookup_cache_entry(npath, NULL);
1129 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1131 rc = setup_referral(path, ce, ref, it->it_name);
1134 up_read(&htable_rw_lock);
1135 free_normalized_path(path, npath);
1140 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1142 memcpy(new, vol, sizeof(*new));
1144 if (vol->username) {
1145 new->username = kstrndup(vol->username, strlen(vol->username),
1150 if (vol->password) {
1151 new->password = kstrndup(vol->password, strlen(vol->password),
1154 goto err_free_username;
1157 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1158 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1160 goto err_free_password;
1162 if (vol->domainname) {
1163 new->domainname = kstrndup(vol->domainname,
1164 strlen(vol->domainname), GFP_KERNEL);
1165 if (!new->domainname)
1168 if (vol->iocharset) {
1169 new->iocharset = kstrndup(vol->iocharset,
1170 strlen(vol->iocharset), GFP_KERNEL);
1171 if (!new->iocharset)
1172 goto err_free_domainname;
1175 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1176 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1179 goto err_free_iocharset;
1185 kfree(new->iocharset);
1186 err_free_domainname:
1187 kfree(new->domainname);
1191 kzfree(new->password);
1193 kfree(new->username);
1199 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1200 * DFS cache refresh worker.
1202 * @mntdata: mount data.
1203 * @vol: cifs volume.
1204 * @fullpath: origin full path.
1206 * Return zero if volume was set up correctly, otherwise non-zero.
1208 int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
1211 struct vol_info *vi;
1213 if (!vol || !fullpath || !mntdata)
1216 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1218 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1222 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1223 if (!vi->fullpath) {
1228 rc = dup_vol(vol, &vi->smb_vol);
1230 goto err_free_fullpath;
1232 vi->mntdata = mntdata;
1233 spin_lock_init(&vi->smb_vol_lock);
1234 kref_init(&vi->refcnt);
1236 spin_lock(&vol_list_lock);
1237 list_add_tail(&vi->list, &vol_list);
1238 spin_unlock(&vol_list_lock);
1243 kfree(vi->fullpath);
1249 /* Must be called with vol_list_lock held */
1250 static struct vol_info *find_vol(const char *fullpath)
1252 struct vol_info *vi;
1254 list_for_each_entry(vi, &vol_list, list) {
1255 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1256 if (!strcasecmp(vi->fullpath, fullpath))
1259 return ERR_PTR(-ENOENT);
1263 * dfs_cache_update_vol - update vol info in DFS cache after failover
1265 * @fullpath: fullpath to look up in volume list.
1266 * @server: TCP ses pointer.
1268 * Return zero if volume was updated, otherwise non-zero.
1270 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1272 struct vol_info *vi;
1274 if (!fullpath || !server)
1277 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1279 spin_lock(&vol_list_lock);
1280 vi = find_vol(fullpath);
1282 spin_unlock(&vol_list_lock);
1285 kref_get(&vi->refcnt);
1286 spin_unlock(&vol_list_lock);
1288 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1289 spin_lock(&vi->smb_vol_lock);
1290 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1291 sizeof(vi->smb_vol.dstaddr));
1292 spin_unlock(&vi->smb_vol_lock);
1294 kref_put(&vi->refcnt, vol_release);
1300 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1302 * @fullpath: fullpath to look up in volume list.
1304 void dfs_cache_del_vol(const char *fullpath)
1306 struct vol_info *vi;
1308 if (!fullpath || !*fullpath)
1311 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1313 spin_lock(&vol_list_lock);
1314 vi = find_vol(fullpath);
1315 spin_unlock(&vol_list_lock);
1317 kref_put(&vi->refcnt, vol_release);
1321 * dfs_cache_get_tgt_share - parse a DFS target
1323 * @it: DFS target iterator.
1324 * @share: tree name.
1325 * @share_len: length of tree name.
1326 * @prefix: prefix path.
1327 * @prefix_len: length of prefix path.
1329 * Return zero if target was parsed correctly, otherwise non-zero.
1331 int dfs_cache_get_tgt_share(const struct dfs_cache_tgt_iterator *it,
1332 const char **share, size_t *share_len,
1333 const char **prefix, size_t *prefix_len)
1337 if (!it || !share || !share_len || !prefix || !prefix_len)
1340 sep = it->it_name[0];
1341 if (sep != '\\' && sep != '/')
1344 s = strchr(it->it_name + 1, sep);
1348 s = strchrnul(s + 1, sep);
1350 *share = it->it_name;
1351 *share_len = s - it->it_name;
1352 *prefix = *s ? s + 1 : s;
1353 *prefix_len = &it->it_name[strlen(it->it_name)] - *prefix;
1358 /* Get all tcons that are within a DFS namespace and can be refreshed */
1359 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1361 struct cifs_ses *ses;
1362 struct cifs_tcon *tcon;
1364 INIT_LIST_HEAD(head);
1366 spin_lock(&cifs_tcp_ses_lock);
1367 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1368 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1369 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1372 list_add_tail(&tcon->ulist, head);
1375 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1376 ses->tcon_ipc->dfs_path) {
1377 list_add_tail(&ses->tcon_ipc->ulist, head);
1380 spin_unlock(&cifs_tcp_ses_lock);
1383 static bool is_dfs_link(const char *path)
1387 s = strchr(path + 1, '\\');
1390 return !!strchr(s + 1, '\\');
1393 static char *get_dfs_root(const char *path)
1397 s = strchr(path + 1, '\\');
1399 return ERR_PTR(-EINVAL);
1401 s = strchr(s + 1, '\\');
1403 return ERR_PTR(-EINVAL);
1405 npath = kstrndup(path, s - path, GFP_KERNEL);
1407 return ERR_PTR(-ENOMEM);
1412 static inline void put_tcp_server(struct TCP_Server_Info *server)
1414 cifs_put_tcp_session(server, 0);
1417 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1419 struct TCP_Server_Info *server;
1421 server = cifs_find_tcp_session(vol);
1422 if (IS_ERR_OR_NULL(server))
1425 spin_lock(&GlobalMid_Lock);
1426 if (server->tcpStatus != CifsGood) {
1427 spin_unlock(&GlobalMid_Lock);
1428 put_tcp_server(server);
1431 spin_unlock(&GlobalMid_Lock);
1436 /* Find root SMB session out of a DFS link path */
1437 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1438 struct cifs_tcon *tcon,
1443 struct cache_entry *ce;
1444 struct dfs_info3_param ref = {0};
1445 char *mdata = NULL, *devname = NULL;
1446 struct TCP_Server_Info *server;
1447 struct cifs_ses *ses;
1448 struct smb_vol vol = {NULL};
1450 rpath = get_dfs_root(path);
1452 return ERR_CAST(rpath);
1454 down_read(&htable_rw_lock);
1456 ce = lookup_cache_entry(rpath, NULL);
1458 up_read(&htable_rw_lock);
1463 rc = setup_referral(path, ce, &ref, get_tgt_name(ce));
1465 up_read(&htable_rw_lock);
1470 up_read(&htable_rw_lock);
1472 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref,
1474 free_dfs_info_param(&ref);
1476 if (IS_ERR(mdata)) {
1477 ses = ERR_CAST(mdata);
1482 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1490 server = get_tcp_server(&vol);
1492 ses = ERR_PTR(-EHOSTDOWN);
1496 ses = cifs_get_smb_ses(server, &vol);
1499 cifs_cleanup_volume_info_contents(&vol);
1506 /* Refresh DFS cache entry from a given tcon */
1507 static int refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1512 struct cache_entry *ce;
1513 struct cifs_ses *root_ses = NULL, *ses;
1514 struct dfs_info3_param *refs = NULL;
1519 path = tcon->dfs_path + 1;
1521 rc = get_normalized_path(path, &npath);
1525 down_read(&htable_rw_lock);
1527 ce = lookup_cache_entry(npath, NULL);
1530 up_read(&htable_rw_lock);
1534 if (!cache_entry_expired(ce)) {
1535 up_read(&htable_rw_lock);
1539 up_read(&htable_rw_lock);
1541 /* If it's a DFS Link, then use root SMB session for refreshing it */
1542 if (is_dfs_link(npath)) {
1543 ses = root_ses = find_root_ses(vi, tcon, npath);
1553 rc = get_dfs_referral(xid, ses, cache_nlsc, tcon->remap, npath, &refs,
1556 dump_refs(refs, numrefs);
1557 rc = update_cache_entry(npath, refs, numrefs);
1558 free_dfs_info_array(refs, numrefs);
1562 cifs_put_smb_ses(root_ses);
1565 free_normalized_path(path, npath);
1573 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1576 static void refresh_cache_worker(struct work_struct *work)
1578 struct vol_info *vi, *nvi;
1579 struct TCP_Server_Info *server;
1582 struct cifs_tcon *tcon, *ntcon;
1586 * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1589 spin_lock(&vol_list_lock);
1590 list_for_each_entry(vi, &vol_list, list) {
1591 server = get_tcp_server(&vi->smb_vol);
1595 kref_get(&vi->refcnt);
1596 list_add_tail(&vi->rlist, &vols);
1597 put_tcp_server(server);
1599 spin_unlock(&vol_list_lock);
1601 /* Walk through all TCONs and refresh any expired cache entry */
1602 list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1603 spin_lock(&vi->smb_vol_lock);
1604 server = get_tcp_server(&vi->smb_vol);
1605 spin_unlock(&vi->smb_vol_lock);
1610 get_tcons(server, &tcons);
1613 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1615 * Skip tcp server if any of its tcons failed to refresh
1616 * (possibily due to reconnects).
1619 rc = refresh_tcon(vi, tcon);
1621 list_del_init(&tcon->ulist);
1622 cifs_put_tcon(tcon);
1625 put_tcp_server(server);
1628 list_del_init(&vi->rlist);
1629 kref_put(&vi->refcnt, vol_release);
1632 spin_lock(&cache_ttl_lock);
1633 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1634 spin_unlock(&cache_ttl_lock);