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/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 CACHE_HTABLE_SIZE 32
26 #define CACHE_MAX_ENTRIES 64
28 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
29 DFSREF_STORAGE_SERVER))
31 struct cache_dfs_tgt {
33 struct list_head list;
37 struct hlist_node hlist;
42 struct timespec64 etime;
45 struct list_head tlist;
46 struct cache_dfs_tgt *tgthint;
52 spinlock_t smb_vol_lock;
53 struct smb_vol smb_vol;
55 struct list_head list;
56 struct list_head rlist;
60 static struct kmem_cache *cache_slab __read_mostly;
61 static struct workqueue_struct *dfscache_wq __read_mostly;
64 static DEFINE_SPINLOCK(cache_ttl_lock);
66 static struct nls_table *cache_nlsc;
69 * Number of entries in the cache
71 static size_t cache_count;
73 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
74 static DEFINE_MUTEX(list_lock);
76 static LIST_HEAD(vol_list);
77 static DEFINE_SPINLOCK(vol_list_lock);
79 static void refresh_cache_worker(struct work_struct *work);
81 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
83 static int get_normalized_path(const char *path, char **npath)
85 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
89 *npath = (char *)path;
91 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
94 convert_delimiter(*npath, '\\');
99 static inline void free_normalized_path(const char *path, char *npath)
105 static inline bool cache_entry_expired(const struct cache_entry *ce)
107 struct timespec64 ts;
109 ktime_get_coarse_real_ts64(&ts);
110 return timespec64_compare(&ts, &ce->etime) >= 0;
113 static inline void free_tgts(struct cache_entry *ce)
115 struct cache_dfs_tgt *t, *n;
117 list_for_each_entry_safe(t, n, &ce->tlist, list) {
124 static void free_cache_entry(struct rcu_head *rcu)
126 struct cache_entry *ce = container_of(rcu, struct cache_entry, rcu);
128 kmem_cache_free(cache_slab, ce);
131 static inline void flush_cache_ent(struct cache_entry *ce)
133 if (hlist_unhashed(&ce->hlist))
136 hlist_del_init_rcu(&ce->hlist);
140 call_rcu(&ce->rcu, free_cache_entry);
143 static void flush_cache_ents(void)
148 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
149 struct hlist_head *l = &cache_htable[i];
150 struct cache_entry *ce;
152 hlist_for_each_entry_rcu(ce, l, hlist)
159 * dfs cache /proc file
161 static int dfscache_proc_show(struct seq_file *m, void *v)
164 struct cache_entry *ce;
165 struct cache_dfs_tgt *t;
167 seq_puts(m, "DFS cache\n---------\n");
169 mutex_lock(&list_lock);
172 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
174 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
175 "interlink=%s,path_consumed=%d,expired=%s\n",
177 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
178 ce->ttl, ce->etime.tv_nsec,
179 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
181 cache_entry_expired(ce) ? "yes" : "no");
183 list_for_each_entry(t, &ce->tlist, list) {
184 seq_printf(m, " %s%s\n",
186 ce->tgthint == t ? " (target hint)" : "");
192 mutex_unlock(&list_lock);
196 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
197 size_t count, loff_t *ppos)
202 rc = get_user(c, buffer);
209 cifs_dbg(FYI, "clearing dfs cache");
210 mutex_lock(&list_lock);
212 mutex_unlock(&list_lock);
217 static int dfscache_proc_open(struct inode *inode, struct file *file)
219 return single_open(file, dfscache_proc_show, NULL);
222 const struct file_operations dfscache_proc_fops = {
223 .open = dfscache_proc_open,
226 .release = single_release,
227 .write = dfscache_proc_write,
230 #ifdef CONFIG_CIFS_DEBUG2
231 static inline void dump_tgts(const struct cache_entry *ce)
233 struct cache_dfs_tgt *t;
235 cifs_dbg(FYI, "target list:\n");
236 list_for_each_entry(t, &ce->tlist, list) {
237 cifs_dbg(FYI, " %s%s\n", t->name,
238 ce->tgthint == t ? " (target hint)" : "");
242 static inline void dump_ce(const struct cache_entry *ce)
244 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
245 "interlink=%s,path_consumed=%d,expired=%s\n", ce->path,
246 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
248 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
250 cache_entry_expired(ce) ? "yes" : "no");
254 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
258 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
259 for (i = 0; i < numrefs; i++) {
260 const struct dfs_info3_param *ref = &refs[i];
265 "path_consumed: %d\n"
266 "server_type: 0x%x\n"
271 ref->flags, ref->path_consumed, ref->server_type,
272 ref->ref_flag, ref->path_name, ref->node_name,
273 ref->ttl, ref->ttl / 60);
279 #define dump_refs(r, n)
283 * dfs_cache_init - Initialize DFS referral cache.
285 * Return zero if initialized successfully, otherwise non-zero.
287 int dfs_cache_init(void)
292 dfscache_wq = alloc_workqueue("cifs-dfscache",
293 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
297 cache_slab = kmem_cache_create("cifs_dfs_cache",
298 sizeof(struct cache_entry), 0,
299 SLAB_HWCACHE_ALIGN, NULL);
305 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
306 INIT_HLIST_HEAD(&cache_htable[i]);
308 cache_nlsc = load_nls_default();
310 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
314 destroy_workqueue(dfscache_wq);
318 static inline unsigned int cache_entry_hash(const void *data, int size)
322 h = jhash(data, size, 0);
323 return h & (CACHE_HTABLE_SIZE - 1);
326 /* Check whether second path component of @path is SYSVOL or NETLOGON */
327 static inline bool is_sysvol_or_netlogon(const char *path)
332 s = strchr(path + 1, sep) + 1;
333 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
334 !strncasecmp(s, "netlogon", strlen("netlogon"));
337 /* Return target hint of a DFS cache entry */
338 static inline char *get_tgt_name(const struct cache_entry *ce)
340 struct cache_dfs_tgt *t = ce->tgthint;
342 return t ? t->name : ERR_PTR(-ENOENT);
345 /* Return expire time out of a new entry's TTL */
346 static inline struct timespec64 get_expire_time(int ttl)
348 struct timespec64 ts = {
352 struct timespec64 now;
354 ktime_get_coarse_real_ts64(&now);
355 return timespec64_add(now, ts);
358 /* Allocate a new DFS target */
359 static inline struct cache_dfs_tgt *alloc_tgt(const char *name)
361 struct cache_dfs_tgt *t;
363 t = kmalloc(sizeof(*t), GFP_KERNEL);
365 return ERR_PTR(-ENOMEM);
366 t->name = kstrndup(name, strlen(name), GFP_KERNEL);
369 return ERR_PTR(-ENOMEM);
371 INIT_LIST_HEAD(&t->list);
376 * Copy DFS referral information to a cache entry and conditionally update
379 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
380 struct cache_entry *ce, const char *tgthint)
384 ce->ttl = refs[0].ttl;
385 ce->etime = get_expire_time(ce->ttl);
386 ce->srvtype = refs[0].server_type;
387 ce->flags = refs[0].ref_flag;
388 ce->path_consumed = refs[0].path_consumed;
390 for (i = 0; i < numrefs; i++) {
391 struct cache_dfs_tgt *t;
393 t = alloc_tgt(refs[i].node_name);
398 if (tgthint && !strcasecmp(t->name, tgthint)) {
399 list_add(&t->list, &ce->tlist);
402 list_add_tail(&t->list, &ce->tlist);
407 ce->tgthint = list_first_entry_or_null(&ce->tlist,
408 struct cache_dfs_tgt, list);
413 /* Allocate a new cache entry */
414 static struct cache_entry *alloc_cache_entry(const char *path,
415 const struct dfs_info3_param *refs,
418 struct cache_entry *ce;
421 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
423 return ERR_PTR(-ENOMEM);
425 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
427 kmem_cache_free(cache_slab, ce);
428 return ERR_PTR(-ENOMEM);
430 INIT_HLIST_NODE(&ce->hlist);
431 INIT_LIST_HEAD(&ce->tlist);
433 rc = copy_ref_data(refs, numrefs, ce, NULL);
436 kmem_cache_free(cache_slab, ce);
442 static void remove_oldest_entry(void)
445 struct cache_entry *ce;
446 struct cache_entry *to_del = NULL;
449 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
450 if (!to_del || timespec64_compare(&ce->etime,
455 cifs_dbg(FYI, "%s: no entry to remove", __func__);
458 cifs_dbg(FYI, "%s: removing entry", __func__);
460 flush_cache_ent(to_del);
465 /* Add a new DFS cache entry */
466 static inline struct cache_entry *
467 add_cache_entry(unsigned int hash, const char *path,
468 const struct dfs_info3_param *refs, int numrefs)
470 struct cache_entry *ce;
472 ce = alloc_cache_entry(path, refs, numrefs);
476 hlist_add_head_rcu(&ce->hlist, &cache_htable[hash]);
478 spin_lock(&cache_ttl_lock);
481 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
483 cache_ttl = min_t(int, cache_ttl, ce->ttl);
484 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
486 spin_unlock(&cache_ttl_lock);
492 * Find a DFS cache entry in hash table and optionally check prefix path against
494 * Use whole path components in the match.
495 * Return ERR_PTR(-ENOENT) if the entry is not found.
497 static struct cache_entry *lookup_cache_entry(const char *path,
500 struct cache_entry *ce;
504 h = cache_entry_hash(path, strlen(path));
507 hlist_for_each_entry_rcu(ce, &cache_htable[h], hlist) {
508 if (!strcasecmp(path, ce->path)) {
517 ce = ERR_PTR(-ENOENT);
524 static inline void destroy_slab_cache(void)
527 kmem_cache_destroy(cache_slab);
530 static void __vol_release(struct vol_info *vi)
534 cifs_cleanup_volume_info_contents(&vi->smb_vol);
538 static void vol_release(struct kref *kref)
540 struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
542 spin_lock(&vol_list_lock);
544 spin_unlock(&vol_list_lock);
548 static inline void free_vol_list(void)
550 struct vol_info *vi, *nvi;
552 list_for_each_entry_safe(vi, nvi, &vol_list, list) {
553 list_del_init(&vi->list);
559 * dfs_cache_destroy - destroy DFS referral cache
561 void dfs_cache_destroy(void)
563 cancel_delayed_work_sync(&refresh_task);
564 unload_nls(cache_nlsc);
567 destroy_slab_cache();
568 destroy_workqueue(dfscache_wq);
570 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
573 static inline struct cache_entry *
574 __update_cache_entry(const char *path, const struct dfs_info3_param *refs,
579 struct cache_entry *ce;
582 ce = lookup_cache_entry(path, &h);
587 s = ce->tgthint->name;
588 th = kstrndup(s, strlen(s), GFP_KERNEL);
590 return ERR_PTR(-ENOMEM);
596 rc = copy_ref_data(refs, numrefs, ce, th);
605 /* Update an expired cache entry by getting a new DFS referral from server */
606 static struct cache_entry *
607 update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
608 const struct nls_table *nls_codepage, int remap,
609 const char *path, struct cache_entry *ce)
612 struct dfs_info3_param *refs = NULL;
615 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
617 * Check if caller provided enough parameters to update an expired
620 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
621 return ERR_PTR(-ETIME);
622 if (unlikely(!nls_codepage))
623 return ERR_PTR(-ETIME);
625 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
627 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
628 nls_codepage, remap);
632 ce = __update_cache_entry(path, refs, numrefs);
634 dump_refs(refs, numrefs);
635 free_dfs_info_array(refs, numrefs);
641 * Find, create or update a DFS cache entry.
643 * If the entry wasn't found, it will create a new one. Or if it was found but
644 * expired, then it will update the entry accordingly.
646 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
647 * handle them properly.
649 static struct cache_entry *
650 do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
651 const struct nls_table *nls_codepage, int remap,
652 const char *path, bool noreq)
656 struct cache_entry *ce;
657 struct dfs_info3_param *nrefs;
660 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
662 ce = lookup_cache_entry(path, &h);
664 cifs_dbg(FYI, "%s: cache miss\n", __func__);
666 * If @noreq is set, no requests will be sent to the server for
667 * either updating or getting a new DFS referral.
672 * No cache entry was found, so check for valid parameters that
673 * will be required to get a new DFS referral and then create a
676 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
677 ce = ERR_PTR(-EOPNOTSUPP);
680 if (unlikely(!nls_codepage)) {
681 ce = ERR_PTR(-EINVAL);
688 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
691 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
692 &numnrefs, nls_codepage,
699 dump_refs(nrefs, numnrefs);
701 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
703 if (cache_count >= CACHE_MAX_ENTRIES) {
704 cifs_dbg(FYI, "%s: reached max cache size (%d)",
705 __func__, CACHE_MAX_ENTRIES);
706 remove_oldest_entry();
708 ce = add_cache_entry(h, path, nrefs, numnrefs);
709 free_dfs_info_array(nrefs, numnrefs);
719 /* Just return the found cache entry in case @noreq is set */
723 if (cache_entry_expired(ce)) {
724 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
725 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
728 cifs_dbg(FYI, "%s: failed to update expired entry\n",
735 /* Set up a new DFS referral from a given cache entry */
736 static int setup_ref(const char *path, const struct cache_entry *ce,
737 struct dfs_info3_param *ref, const char *tgt)
741 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
743 memset(ref, 0, sizeof(*ref));
745 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
749 ref->path_consumed = ce->path_consumed;
751 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
752 if (!ref->node_name) {
758 ref->server_type = ce->srvtype;
759 ref->ref_flag = ce->flags;
764 kfree(ref->path_name);
765 ref->path_name = NULL;
769 /* Return target list of a DFS cache entry */
770 static int get_tgt_list(const struct cache_entry *ce,
771 struct dfs_cache_tgt_list *tl)
774 struct list_head *head = &tl->tl_list;
775 struct cache_dfs_tgt *t;
776 struct dfs_cache_tgt_iterator *it, *nit;
778 memset(tl, 0, sizeof(*tl));
779 INIT_LIST_HEAD(head);
781 list_for_each_entry(t, &ce->tlist, list) {
782 it = kzalloc(sizeof(*it), GFP_KERNEL);
788 it->it_name = kstrndup(t->name, strlen(t->name),
796 if (ce->tgthint == t)
797 list_add(&it->it_list, head);
799 list_add_tail(&it->it_list, head);
801 tl->tl_numtgts = ce->numtgts;
806 list_for_each_entry_safe(it, nit, head, it_list) {
814 * dfs_cache_find - find a DFS cache entry
816 * If it doesn't find the cache entry, then it will get a DFS referral
817 * for @path and create a new entry.
819 * In case the cache entry exists but expired, it will get a DFS referral
820 * for @path and then update the respective cache entry.
822 * These parameters are passed down to the get_dfs_refer() call if it
823 * needs to be issued:
825 * @ses: smb session to issue the request on
826 * @nls_codepage: charset conversion
827 * @remap: path character remapping type
828 * @path: path to lookup in DFS referral cache.
830 * @ref: when non-NULL, store single DFS referral result in it.
831 * @tgt_list: when non-NULL, store complete DFS target list in it.
833 * Return zero if the target was found, otherwise non-zero.
835 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
836 const struct nls_table *nls_codepage, int remap,
837 const char *path, struct dfs_info3_param *ref,
838 struct dfs_cache_tgt_list *tgt_list)
842 struct cache_entry *ce;
844 rc = get_normalized_path(path, &npath);
848 mutex_lock(&list_lock);
849 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
852 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
856 rc = get_tgt_list(ce, tgt_list);
860 mutex_unlock(&list_lock);
861 free_normalized_path(path, npath);
866 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
867 * the currently connected server.
869 * NOTE: This function will neither update a cache entry in case it was
870 * expired, nor create a new cache entry if @path hasn't been found. It heavily
871 * relies on an existing cache entry.
873 * @path: path to lookup in the DFS referral cache.
874 * @ref: when non-NULL, store single DFS referral result in it.
875 * @tgt_list: when non-NULL, store complete DFS target list in it.
877 * Return 0 if successful.
878 * Return -ENOENT if the entry was not found.
879 * Return non-zero for other errors.
881 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
882 struct dfs_cache_tgt_list *tgt_list)
886 struct cache_entry *ce;
888 rc = get_normalized_path(path, &npath);
892 mutex_lock(&list_lock);
893 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
900 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
904 rc = get_tgt_list(ce, tgt_list);
906 mutex_unlock(&list_lock);
907 free_normalized_path(path, npath);
912 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
914 * If it doesn't find the cache entry, then it will get a DFS referral for @path
915 * and create a new entry.
917 * In case the cache entry exists but expired, it will get a DFS referral
918 * for @path and then update the respective cache entry.
922 * @nls_codepage: charset conversion
923 * @remap: type of character remapping for paths
924 * @path: path to lookup in DFS referral cache.
925 * @it: DFS target iterator
927 * Return zero if the target hint was updated successfully, otherwise non-zero.
929 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
930 const struct nls_table *nls_codepage, int remap,
932 const struct dfs_cache_tgt_iterator *it)
936 struct cache_entry *ce;
937 struct cache_dfs_tgt *t;
939 rc = get_normalized_path(path, &npath);
943 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
945 mutex_lock(&list_lock);
946 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
956 if (likely(!strcasecmp(it->it_name, t->name)))
959 list_for_each_entry(t, &ce->tlist, list) {
960 if (!strcasecmp(t->name, it->it_name)) {
962 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
969 mutex_unlock(&list_lock);
970 free_normalized_path(path, npath);
975 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
976 * without sending any requests to the currently connected server.
978 * NOTE: This function will neither update a cache entry in case it was
979 * expired, nor create a new cache entry if @path hasn't been found. It heavily
980 * relies on an existing cache entry.
982 * @path: path to lookup in DFS referral cache.
983 * @it: target iterator which contains the target hint to update the cache
986 * Return zero if the target hint was updated successfully, otherwise non-zero.
988 int dfs_cache_noreq_update_tgthint(const char *path,
989 const struct dfs_cache_tgt_iterator *it)
993 struct cache_entry *ce;
994 struct cache_dfs_tgt *t;
999 rc = get_normalized_path(path, &npath);
1003 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1005 mutex_lock(&list_lock);
1007 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1017 if (unlikely(!strcasecmp(it->it_name, t->name)))
1020 list_for_each_entry(t, &ce->tlist, list) {
1021 if (!strcasecmp(t->name, it->it_name)) {
1023 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1030 mutex_unlock(&list_lock);
1031 free_normalized_path(path, npath);
1036 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1037 * target iterator (@it).
1039 * @path: path to lookup in DFS referral cache.
1040 * @it: DFS target iterator.
1041 * @ref: DFS referral pointer to set up the gathered information.
1043 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1045 int dfs_cache_get_tgt_referral(const char *path,
1046 const struct dfs_cache_tgt_iterator *it,
1047 struct dfs_info3_param *ref)
1051 struct cache_entry *ce;
1057 rc = get_normalized_path(path, &npath);
1061 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1063 mutex_lock(&list_lock);
1065 ce = lookup_cache_entry(npath, &h);
1071 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1073 rc = setup_ref(path, ce, ref, it->it_name);
1076 mutex_unlock(&list_lock);
1077 free_normalized_path(path, npath);
1081 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1083 memcpy(new, vol, sizeof(*new));
1085 if (vol->username) {
1086 new->username = kstrndup(vol->username, strlen(vol->username),
1091 if (vol->password) {
1092 new->password = kstrndup(vol->password, strlen(vol->password),
1095 goto err_free_username;
1098 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1099 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1101 goto err_free_password;
1103 if (vol->domainname) {
1104 new->domainname = kstrndup(vol->domainname,
1105 strlen(vol->domainname), GFP_KERNEL);
1106 if (!new->domainname)
1109 if (vol->iocharset) {
1110 new->iocharset = kstrndup(vol->iocharset,
1111 strlen(vol->iocharset), GFP_KERNEL);
1112 if (!new->iocharset)
1113 goto err_free_domainname;
1116 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1117 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1120 goto err_free_iocharset;
1126 kfree(new->iocharset);
1127 err_free_domainname:
1128 kfree(new->domainname);
1132 kzfree(new->password);
1134 kfree(new->username);
1140 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1141 * DFS cache refresh worker.
1143 * @mntdata: mount data.
1144 * @vol: cifs volume.
1145 * @fullpath: origin full path.
1147 * Return zero if volume was set up correctly, otherwise non-zero.
1149 int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
1152 struct vol_info *vi;
1154 if (!vol || !fullpath || !mntdata)
1157 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1159 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1163 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1164 if (!vi->fullpath) {
1169 rc = dup_vol(vol, &vi->smb_vol);
1171 goto err_free_fullpath;
1173 vi->mntdata = mntdata;
1174 spin_lock_init(&vi->smb_vol_lock);
1175 kref_init(&vi->refcnt);
1177 spin_lock(&vol_list_lock);
1178 list_add_tail(&vi->list, &vol_list);
1179 spin_unlock(&vol_list_lock);
1184 kfree(vi->fullpath);
1190 /* Must be called with vol_list_lock held */
1191 static struct vol_info *find_vol(const char *fullpath)
1193 struct vol_info *vi;
1195 list_for_each_entry(vi, &vol_list, list) {
1196 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1197 if (!strcasecmp(vi->fullpath, fullpath))
1200 return ERR_PTR(-ENOENT);
1204 * dfs_cache_update_vol - update vol info in DFS cache after failover
1206 * @fullpath: fullpath to look up in volume list.
1207 * @server: TCP ses pointer.
1209 * Return zero if volume was updated, otherwise non-zero.
1211 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1213 struct vol_info *vi;
1215 if (!fullpath || !server)
1218 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1220 spin_lock(&vol_list_lock);
1221 vi = find_vol(fullpath);
1223 spin_unlock(&vol_list_lock);
1226 kref_get(&vi->refcnt);
1227 spin_unlock(&vol_list_lock);
1229 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1230 spin_lock(&vi->smb_vol_lock);
1231 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1232 sizeof(vi->smb_vol.dstaddr));
1233 spin_unlock(&vi->smb_vol_lock);
1235 kref_put(&vi->refcnt, vol_release);
1241 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1243 * @fullpath: fullpath to look up in volume list.
1245 void dfs_cache_del_vol(const char *fullpath)
1247 struct vol_info *vi;
1249 if (!fullpath || !*fullpath)
1252 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1254 spin_lock(&vol_list_lock);
1255 vi = find_vol(fullpath);
1256 spin_unlock(&vol_list_lock);
1258 kref_put(&vi->refcnt, vol_release);
1261 /* Get all tcons that are within a DFS namespace and can be refreshed */
1262 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1264 struct cifs_ses *ses;
1265 struct cifs_tcon *tcon;
1267 INIT_LIST_HEAD(head);
1269 spin_lock(&cifs_tcp_ses_lock);
1270 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1271 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1272 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1275 list_add_tail(&tcon->ulist, head);
1278 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1279 ses->tcon_ipc->dfs_path) {
1280 list_add_tail(&ses->tcon_ipc->ulist, head);
1283 spin_unlock(&cifs_tcp_ses_lock);
1286 static bool is_dfs_link(const char *path)
1290 s = strchr(path + 1, '\\');
1293 return !!strchr(s + 1, '\\');
1296 static char *get_dfs_root(const char *path)
1300 s = strchr(path + 1, '\\');
1302 return ERR_PTR(-EINVAL);
1304 s = strchr(s + 1, '\\');
1306 return ERR_PTR(-EINVAL);
1308 npath = kstrndup(path, s - path, GFP_KERNEL);
1310 return ERR_PTR(-ENOMEM);
1315 static inline void put_tcp_server(struct TCP_Server_Info *server)
1317 cifs_put_tcp_session(server, 0);
1320 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1322 struct TCP_Server_Info *server;
1324 server = cifs_find_tcp_session(vol);
1325 if (IS_ERR_OR_NULL(server))
1328 spin_lock(&GlobalMid_Lock);
1329 if (server->tcpStatus != CifsGood) {
1330 spin_unlock(&GlobalMid_Lock);
1331 put_tcp_server(server);
1334 spin_unlock(&GlobalMid_Lock);
1339 /* Find root SMB session out of a DFS link path */
1340 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1341 struct cifs_tcon *tcon,
1346 struct dfs_info3_param ref = {0};
1347 char *mdata = NULL, *devname = NULL;
1348 struct TCP_Server_Info *server;
1349 struct cifs_ses *ses;
1350 struct smb_vol vol = {NULL};
1352 rpath = get_dfs_root(path);
1354 return ERR_CAST(rpath);
1356 memset(&vol, 0, sizeof(vol));
1358 rc = dfs_cache_noreq_find(rpath, &ref, NULL);
1364 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref, &devname);
1365 free_dfs_info_param(&ref);
1367 if (IS_ERR(mdata)) {
1368 ses = ERR_CAST(mdata);
1373 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1381 server = get_tcp_server(&vol);
1383 ses = ERR_PTR(-EHOSTDOWN);
1387 ses = cifs_get_smb_ses(server, &vol);
1390 cifs_cleanup_volume_info_contents(&vol);
1397 /* Refresh DFS cache entry from a given tcon */
1398 static void refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1404 struct cache_entry *ce;
1405 struct dfs_info3_param *refs = NULL;
1407 struct cifs_ses *root_ses = NULL, *ses;
1411 path = tcon->dfs_path + 1;
1413 rc = get_normalized_path(path, &npath);
1417 mutex_lock(&list_lock);
1418 ce = lookup_cache_entry(npath, &h);
1419 mutex_unlock(&list_lock);
1426 if (!cache_entry_expired(ce))
1429 /* If it's a DFS Link, then use root SMB session for refreshing it */
1430 if (is_dfs_link(npath)) {
1431 ses = root_ses = find_root_ses(vi, tcon, npath);
1441 if (unlikely(!ses->server->ops->get_dfs_refer)) {
1444 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs,
1445 &numrefs, cache_nlsc,
1448 mutex_lock(&list_lock);
1449 ce = __update_cache_entry(npath, refs, numrefs);
1450 mutex_unlock(&list_lock);
1451 dump_refs(refs, numrefs);
1452 free_dfs_info_array(refs, numrefs);
1460 cifs_put_smb_ses(root_ses);
1463 free_normalized_path(path, npath);
1467 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1470 static void refresh_cache_worker(struct work_struct *work)
1472 struct vol_info *vi, *nvi;
1473 struct TCP_Server_Info *server;
1476 struct cifs_tcon *tcon, *ntcon;
1479 * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1482 spin_lock(&vol_list_lock);
1483 list_for_each_entry(vi, &vol_list, list) {
1484 server = get_tcp_server(&vi->smb_vol);
1488 kref_get(&vi->refcnt);
1489 list_add_tail(&vi->rlist, &vols);
1490 put_tcp_server(server);
1492 spin_unlock(&vol_list_lock);
1494 /* Walk through all TCONs and refresh any expired cache entry */
1495 list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1496 spin_lock(&vi->smb_vol_lock);
1497 server = get_tcp_server(&vi->smb_vol);
1498 spin_unlock(&vi->smb_vol_lock);
1503 get_tcons(server, &tcons);
1504 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1505 refresh_tcon(vi, tcon);
1506 list_del_init(&tcon->ulist);
1507 cifs_put_tcon(tcon);
1510 put_tcp_server(server);
1513 list_del_init(&vi->rlist);
1514 kref_put(&vi->refcnt, vol_release);
1517 spin_lock(&cache_ttl_lock);
1518 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1519 spin_unlock(&cache_ttl_lock);