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/nls.h>
12 #include <linux/workqueue.h>
15 #include "smb2proto.h"
16 #include "cifsproto.h"
17 #include "cifs_debug.h"
18 #include "cifs_unicode.h"
21 #include "dfs_cache.h"
23 #define CACHE_HTABLE_SIZE 32
24 #define CACHE_MAX_ENTRIES 64
26 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
27 DFSREF_STORAGE_SERVER))
29 struct cache_dfs_tgt {
31 struct list_head list;
35 struct hlist_node hlist;
40 struct timespec64 etime;
43 struct list_head tlist;
44 struct cache_dfs_tgt *tgthint;
49 spinlock_t smb_vol_lock;
50 struct smb_vol smb_vol;
52 struct list_head list;
53 struct list_head rlist;
57 static struct kmem_cache *cache_slab __read_mostly;
58 static struct workqueue_struct *dfscache_wq __read_mostly;
61 static DEFINE_SPINLOCK(cache_ttl_lock);
63 static struct nls_table *cache_nlsc;
66 * Number of entries in the cache
68 static atomic_t cache_count;
70 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
71 static DECLARE_RWSEM(htable_rw_lock);
73 static LIST_HEAD(vol_list);
74 static DEFINE_SPINLOCK(vol_list_lock);
76 static void refresh_cache_worker(struct work_struct *work);
78 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
80 static int get_normalized_path(const char *path, char **npath)
82 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
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 cache_entry *ce)
104 struct timespec64 ts;
106 ktime_get_coarse_real_ts64(&ts);
107 return timespec64_compare(&ts, &ce->etime) >= 0;
110 static inline void free_tgts(struct cache_entry *ce)
112 struct cache_dfs_tgt *t, *n;
114 list_for_each_entry_safe(t, n, &ce->tlist, list) {
121 static inline void flush_cache_ent(struct cache_entry *ce)
123 hlist_del_init(&ce->hlist);
126 atomic_dec(&cache_count);
127 kmem_cache_free(cache_slab, ce);
130 static void flush_cache_ents(void)
134 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
135 struct hlist_head *l = &cache_htable[i];
136 struct hlist_node *n;
137 struct cache_entry *ce;
139 hlist_for_each_entry_safe(ce, n, l, hlist) {
140 if (!hlist_unhashed(&ce->hlist))
147 * dfs cache /proc file
149 static int dfscache_proc_show(struct seq_file *m, void *v)
152 struct cache_entry *ce;
153 struct cache_dfs_tgt *t;
155 seq_puts(m, "DFS cache\n---------\n");
157 down_read(&htable_rw_lock);
158 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
159 struct hlist_head *l = &cache_htable[i];
161 hlist_for_each_entry(ce, l, hlist) {
162 if (hlist_unhashed(&ce->hlist))
166 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
167 "interlink=%s,path_consumed=%d,expired=%s\n",
169 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
170 ce->ttl, ce->etime.tv_nsec,
171 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
173 cache_entry_expired(ce) ? "yes" : "no");
175 list_for_each_entry(t, &ce->tlist, list) {
176 seq_printf(m, " %s%s\n",
178 ce->tgthint == t ? " (target hint)" : "");
182 up_read(&htable_rw_lock);
187 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
188 size_t count, loff_t *ppos)
193 rc = get_user(c, buffer);
200 cifs_dbg(FYI, "clearing dfs cache");
202 down_write(&htable_rw_lock);
204 up_write(&htable_rw_lock);
209 static int dfscache_proc_open(struct inode *inode, struct file *file)
211 return single_open(file, dfscache_proc_show, NULL);
214 const struct file_operations dfscache_proc_fops = {
215 .open = dfscache_proc_open,
218 .release = single_release,
219 .write = dfscache_proc_write,
222 #ifdef CONFIG_CIFS_DEBUG2
223 static inline void dump_tgts(const struct cache_entry *ce)
225 struct cache_dfs_tgt *t;
227 cifs_dbg(FYI, "target list:\n");
228 list_for_each_entry(t, &ce->tlist, list) {
229 cifs_dbg(FYI, " %s%s\n", t->name,
230 ce->tgthint == t ? " (target hint)" : "");
234 static inline void dump_ce(const struct cache_entry *ce)
236 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
237 "interlink=%s,path_consumed=%d,expired=%s\n", ce->path,
238 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
240 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
242 cache_entry_expired(ce) ? "yes" : "no");
246 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
250 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
251 for (i = 0; i < numrefs; i++) {
252 const struct dfs_info3_param *ref = &refs[i];
257 "path_consumed: %d\n"
258 "server_type: 0x%x\n"
263 ref->flags, ref->path_consumed, ref->server_type,
264 ref->ref_flag, ref->path_name, ref->node_name,
265 ref->ttl, ref->ttl / 60);
271 #define dump_refs(r, n)
275 * dfs_cache_init - Initialize DFS referral cache.
277 * Return zero if initialized successfully, otherwise non-zero.
279 int dfs_cache_init(void)
284 dfscache_wq = alloc_workqueue("cifs-dfscache",
285 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
289 cache_slab = kmem_cache_create("cifs_dfs_cache",
290 sizeof(struct cache_entry), 0,
291 SLAB_HWCACHE_ALIGN, NULL);
297 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
298 INIT_HLIST_HEAD(&cache_htable[i]);
300 atomic_set(&cache_count, 0);
301 cache_nlsc = load_nls_default();
303 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
307 destroy_workqueue(dfscache_wq);
311 static inline unsigned int cache_entry_hash(const void *data, int size)
315 h = jhash(data, size, 0);
316 return h & (CACHE_HTABLE_SIZE - 1);
319 /* Check whether second path component of @path is SYSVOL or NETLOGON */
320 static inline bool is_sysvol_or_netlogon(const char *path)
325 s = strchr(path + 1, sep) + 1;
326 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
327 !strncasecmp(s, "netlogon", strlen("netlogon"));
330 /* Return target hint of a DFS cache entry */
331 static inline char *get_tgt_name(const struct cache_entry *ce)
333 struct cache_dfs_tgt *t = ce->tgthint;
335 return t ? t->name : ERR_PTR(-ENOENT);
338 /* Return expire time out of a new entry's TTL */
339 static inline struct timespec64 get_expire_time(int ttl)
341 struct timespec64 ts = {
345 struct timespec64 now;
347 ktime_get_coarse_real_ts64(&now);
348 return timespec64_add(now, ts);
351 /* Allocate a new DFS target */
352 static struct cache_dfs_tgt *alloc_target(const char *name)
354 struct cache_dfs_tgt *t;
356 t = kmalloc(sizeof(*t), GFP_ATOMIC);
358 return ERR_PTR(-ENOMEM);
359 t->name = kstrndup(name, strlen(name), GFP_ATOMIC);
362 return ERR_PTR(-ENOMEM);
364 INIT_LIST_HEAD(&t->list);
369 * Copy DFS referral information to a cache entry and conditionally update
372 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
373 struct cache_entry *ce, const char *tgthint)
377 ce->ttl = refs[0].ttl;
378 ce->etime = get_expire_time(ce->ttl);
379 ce->srvtype = refs[0].server_type;
380 ce->flags = refs[0].ref_flag;
381 ce->path_consumed = refs[0].path_consumed;
383 for (i = 0; i < numrefs; i++) {
384 struct cache_dfs_tgt *t;
386 t = alloc_target(refs[i].node_name);
391 if (tgthint && !strcasecmp(t->name, tgthint)) {
392 list_add(&t->list, &ce->tlist);
395 list_add_tail(&t->list, &ce->tlist);
400 ce->tgthint = list_first_entry_or_null(&ce->tlist,
401 struct cache_dfs_tgt, list);
406 /* Allocate a new cache entry */
407 static struct cache_entry *alloc_cache_entry(const char *path,
408 const struct dfs_info3_param *refs,
411 struct cache_entry *ce;
414 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
416 return ERR_PTR(-ENOMEM);
418 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
420 kmem_cache_free(cache_slab, ce);
421 return ERR_PTR(-ENOMEM);
423 INIT_HLIST_NODE(&ce->hlist);
424 INIT_LIST_HEAD(&ce->tlist);
426 rc = copy_ref_data(refs, numrefs, ce, NULL);
429 kmem_cache_free(cache_slab, ce);
435 /* Must be called with htable_rw_lock held */
436 static void remove_oldest_entry(void)
439 struct cache_entry *ce;
440 struct cache_entry *to_del = NULL;
442 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
443 struct hlist_head *l = &cache_htable[i];
445 hlist_for_each_entry(ce, l, hlist) {
446 if (hlist_unhashed(&ce->hlist))
448 if (!to_del || timespec64_compare(&ce->etime,
455 cifs_dbg(FYI, "%s: no entry to remove", __func__);
459 cifs_dbg(FYI, "%s: removing entry", __func__);
461 flush_cache_ent(to_del);
464 /* Add a new DFS cache entry */
465 static int add_cache_entry(const char *path, unsigned int hash,
466 struct dfs_info3_param *refs, int numrefs)
468 struct cache_entry *ce;
470 ce = alloc_cache_entry(path, refs, numrefs);
474 spin_lock(&cache_ttl_lock);
477 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
479 cache_ttl = min_t(int, cache_ttl, ce->ttl);
480 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
482 spin_unlock(&cache_ttl_lock);
484 down_write(&htable_rw_lock);
485 hlist_add_head(&ce->hlist, &cache_htable[hash]);
487 up_write(&htable_rw_lock);
493 * Find a DFS cache entry in hash table and optionally check prefix path against
495 * Use whole path components in the match.
496 * Must be called with htable_rw_lock held.
498 * Return ERR_PTR(-ENOENT) if the entry is not found.
500 static struct cache_entry *lookup_cache_entry(const char *path,
503 struct cache_entry *ce;
507 h = cache_entry_hash(path, strlen(path));
509 hlist_for_each_entry(ce, &cache_htable[h], hlist) {
510 if (!strcasecmp(path, ce->path)) {
518 ce = ERR_PTR(-ENOENT);
525 static void __vol_release(struct vol_info *vi)
529 cifs_cleanup_volume_info_contents(&vi->smb_vol);
533 static void vol_release(struct kref *kref)
535 struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
537 spin_lock(&vol_list_lock);
539 spin_unlock(&vol_list_lock);
543 static inline void free_vol_list(void)
545 struct vol_info *vi, *nvi;
547 list_for_each_entry_safe(vi, nvi, &vol_list, list) {
548 list_del_init(&vi->list);
554 * dfs_cache_destroy - destroy DFS referral cache
556 void dfs_cache_destroy(void)
558 cancel_delayed_work_sync(&refresh_task);
559 unload_nls(cache_nlsc);
562 kmem_cache_destroy(cache_slab);
563 destroy_workqueue(dfscache_wq);
565 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
568 /* Must be called with htable_rw_lock held */
569 static int __update_cache_entry(const char *path,
570 const struct dfs_info3_param *refs,
574 struct cache_entry *ce;
577 ce = lookup_cache_entry(path, NULL);
582 s = ce->tgthint->name;
583 th = kstrndup(s, strlen(s), GFP_ATOMIC);
591 rc = copy_ref_data(refs, numrefs, ce, th);
598 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
599 const struct nls_table *nls_codepage, int remap,
600 const char *path, struct dfs_info3_param **refs,
603 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
605 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
607 if (unlikely(!nls_codepage))
613 return ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs,
614 nls_codepage, remap);
617 /* Update an expired cache entry by getting a new DFS referral from server */
618 static int update_cache_entry(const char *path,
619 const struct dfs_info3_param *refs,
625 down_write(&htable_rw_lock);
626 rc = __update_cache_entry(path, refs, numrefs);
627 up_write(&htable_rw_lock);
633 * Find, create or update a DFS cache entry.
635 * If the entry wasn't found, it will create a new one. Or if it was found but
636 * expired, then it will update the entry accordingly.
638 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
639 * handle them properly.
641 static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
642 const struct nls_table *nls_codepage, int remap,
643 const char *path, bool noreq)
647 struct cache_entry *ce;
648 struct dfs_info3_param *refs = NULL;
652 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
654 down_read(&htable_rw_lock);
656 ce = lookup_cache_entry(path, &hash);
659 * If @noreq is set, no requests will be sent to the server. Just return
663 up_read(&htable_rw_lock);
664 return PTR_ERR_OR_ZERO(ce);
668 if (!cache_entry_expired(ce)) {
670 up_read(&htable_rw_lock);
677 up_read(&htable_rw_lock);
680 * No entry was found.
682 * Request a new DFS referral in order to create a new cache entry, or
683 * updating an existing one.
685 rc = get_dfs_referral(xid, ses, nls_codepage, remap, path,
690 dump_refs(refs, numrefs);
693 rc = update_cache_entry(path, refs, numrefs);
697 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
698 cifs_dbg(FYI, "%s: reached max cache size (%d)", __func__,
700 down_write(&htable_rw_lock);
701 remove_oldest_entry();
702 up_write(&htable_rw_lock);
705 rc = add_cache_entry(path, hash, refs, numrefs);
707 atomic_inc(&cache_count);
710 free_dfs_info_array(refs, numrefs);
715 * Set up a DFS referral from a given cache entry.
717 * Must be called with htable_rw_lock held.
719 static int setup_referral(const char *path, struct cache_entry *ce,
720 struct dfs_info3_param *ref, const char *target)
724 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
726 memset(ref, 0, sizeof(*ref));
728 ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC);
732 ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC);
733 if (!ref->node_name) {
738 ref->path_consumed = ce->path_consumed;
740 ref->server_type = ce->srvtype;
741 ref->ref_flag = ce->flags;
746 kfree(ref->path_name);
747 ref->path_name = NULL;
751 /* Return target list of a DFS cache entry */
752 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
755 struct list_head *head = &tl->tl_list;
756 struct cache_dfs_tgt *t;
757 struct dfs_cache_tgt_iterator *it, *nit;
759 memset(tl, 0, sizeof(*tl));
760 INIT_LIST_HEAD(head);
762 list_for_each_entry(t, &ce->tlist, list) {
763 it = kzalloc(sizeof(*it), GFP_ATOMIC);
769 it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC);
776 if (ce->tgthint == t)
777 list_add(&it->it_list, head);
779 list_add_tail(&it->it_list, head);
782 tl->tl_numtgts = ce->numtgts;
787 list_for_each_entry_safe(it, nit, head, it_list) {
795 * dfs_cache_find - find a DFS cache entry
797 * If it doesn't find the cache entry, then it will get a DFS referral
798 * for @path and create a new entry.
800 * In case the cache entry exists but expired, it will get a DFS referral
801 * for @path and then update the respective cache entry.
803 * These parameters are passed down to the get_dfs_refer() call if it
804 * needs to be issued:
806 * @ses: smb session to issue the request on
807 * @nls_codepage: charset conversion
808 * @remap: path character remapping type
809 * @path: path to lookup in DFS referral cache.
811 * @ref: when non-NULL, store single DFS referral result in it.
812 * @tgt_list: when non-NULL, store complete DFS target list in it.
814 * Return zero if the target was found, otherwise non-zero.
816 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
817 const struct nls_table *nls_codepage, int remap,
818 const char *path, struct dfs_info3_param *ref,
819 struct dfs_cache_tgt_list *tgt_list)
823 struct cache_entry *ce;
825 rc = get_normalized_path(path, &npath);
829 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
833 down_read(&htable_rw_lock);
835 ce = lookup_cache_entry(npath, NULL);
837 up_read(&htable_rw_lock);
843 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
847 rc = get_targets(ce, tgt_list);
849 up_read(&htable_rw_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 cache_entry *ce;
879 rc = get_normalized_path(path, &npath);
883 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
885 down_read(&htable_rw_lock);
887 ce = lookup_cache_entry(npath, NULL);
894 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
898 rc = get_targets(ce, tgt_list);
901 up_read(&htable_rw_lock);
902 free_normalized_path(path, npath);
908 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
910 * If it doesn't find the cache entry, then it will get a DFS referral for @path
911 * and create a new entry.
913 * In case the cache entry exists but expired, it will get a DFS referral
914 * for @path and then update the respective cache entry.
918 * @nls_codepage: charset conversion
919 * @remap: type of character remapping for paths
920 * @path: path to lookup in DFS referral cache.
921 * @it: DFS target iterator
923 * Return zero if the target hint was updated successfully, otherwise non-zero.
925 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
926 const struct nls_table *nls_codepage, int remap,
928 const struct dfs_cache_tgt_iterator *it)
932 struct cache_entry *ce;
933 struct cache_dfs_tgt *t;
935 rc = get_normalized_path(path, &npath);
939 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
941 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
945 down_write(&htable_rw_lock);
947 ce = lookup_cache_entry(npath, NULL);
955 if (likely(!strcasecmp(it->it_name, t->name)))
958 list_for_each_entry(t, &ce->tlist, list) {
959 if (!strcasecmp(t->name, it->it_name)) {
961 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
968 up_write(&htable_rw_lock);
970 free_normalized_path(path, npath);
976 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
977 * without sending any requests to the currently connected server.
979 * NOTE: This function will neither update a cache entry in case it was
980 * expired, nor create a new cache entry if @path hasn't been found. It heavily
981 * relies on an existing cache entry.
983 * @path: path to lookup in DFS referral cache.
984 * @it: target iterator which contains the target hint to update the cache
987 * Return zero if the target hint was updated successfully, otherwise non-zero.
989 int dfs_cache_noreq_update_tgthint(const char *path,
990 const struct dfs_cache_tgt_iterator *it)
994 struct cache_entry *ce;
995 struct cache_dfs_tgt *t;
1000 rc = get_normalized_path(path, &npath);
1004 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1006 down_write(&htable_rw_lock);
1008 ce = lookup_cache_entry(npath, NULL);
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 up_write(&htable_rw_lock);
1031 free_normalized_path(path, npath);
1037 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1038 * target iterator (@it).
1040 * @path: path to lookup in DFS referral cache.
1041 * @it: DFS target iterator.
1042 * @ref: DFS referral pointer to set up the gathered information.
1044 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1046 int dfs_cache_get_tgt_referral(const char *path,
1047 const struct dfs_cache_tgt_iterator *it,
1048 struct dfs_info3_param *ref)
1052 struct cache_entry *ce;
1057 rc = get_normalized_path(path, &npath);
1061 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1063 down_read(&htable_rw_lock);
1065 ce = lookup_cache_entry(npath, NULL);
1071 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1073 rc = setup_referral(path, ce, ref, it->it_name);
1076 up_read(&htable_rw_lock);
1077 free_normalized_path(path, npath);
1082 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1084 memcpy(new, vol, sizeof(*new));
1086 if (vol->username) {
1087 new->username = kstrndup(vol->username, strlen(vol->username),
1092 if (vol->password) {
1093 new->password = kstrndup(vol->password, strlen(vol->password),
1096 goto err_free_username;
1099 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1100 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1102 goto err_free_password;
1104 if (vol->domainname) {
1105 new->domainname = kstrndup(vol->domainname,
1106 strlen(vol->domainname), GFP_KERNEL);
1107 if (!new->domainname)
1110 if (vol->iocharset) {
1111 new->iocharset = kstrndup(vol->iocharset,
1112 strlen(vol->iocharset), GFP_KERNEL);
1113 if (!new->iocharset)
1114 goto err_free_domainname;
1117 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1118 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1121 goto err_free_iocharset;
1127 kfree(new->iocharset);
1128 err_free_domainname:
1129 kfree(new->domainname);
1133 kzfree(new->password);
1135 kfree(new->username);
1141 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1142 * DFS cache refresh worker.
1144 * @mntdata: mount data.
1145 * @vol: cifs volume.
1146 * @fullpath: origin full path.
1148 * Return zero if volume was set up correctly, otherwise non-zero.
1150 int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
1153 struct vol_info *vi;
1155 if (!vol || !fullpath || !mntdata)
1158 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1160 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1164 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1165 if (!vi->fullpath) {
1170 rc = dup_vol(vol, &vi->smb_vol);
1172 goto err_free_fullpath;
1174 vi->mntdata = mntdata;
1175 spin_lock_init(&vi->smb_vol_lock);
1176 kref_init(&vi->refcnt);
1178 spin_lock(&vol_list_lock);
1179 list_add_tail(&vi->list, &vol_list);
1180 spin_unlock(&vol_list_lock);
1185 kfree(vi->fullpath);
1191 /* Must be called with vol_list_lock held */
1192 static struct vol_info *find_vol(const char *fullpath)
1194 struct vol_info *vi;
1196 list_for_each_entry(vi, &vol_list, list) {
1197 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1198 if (!strcasecmp(vi->fullpath, fullpath))
1201 return ERR_PTR(-ENOENT);
1205 * dfs_cache_update_vol - update vol info in DFS cache after failover
1207 * @fullpath: fullpath to look up in volume list.
1208 * @server: TCP ses pointer.
1210 * Return zero if volume was updated, otherwise non-zero.
1212 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1214 struct vol_info *vi;
1216 if (!fullpath || !server)
1219 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1221 spin_lock(&vol_list_lock);
1222 vi = find_vol(fullpath);
1224 spin_unlock(&vol_list_lock);
1227 kref_get(&vi->refcnt);
1228 spin_unlock(&vol_list_lock);
1230 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1231 spin_lock(&vi->smb_vol_lock);
1232 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1233 sizeof(vi->smb_vol.dstaddr));
1234 spin_unlock(&vi->smb_vol_lock);
1236 kref_put(&vi->refcnt, vol_release);
1242 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1244 * @fullpath: fullpath to look up in volume list.
1246 void dfs_cache_del_vol(const char *fullpath)
1248 struct vol_info *vi;
1250 if (!fullpath || !*fullpath)
1253 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1255 spin_lock(&vol_list_lock);
1256 vi = find_vol(fullpath);
1257 spin_unlock(&vol_list_lock);
1259 kref_put(&vi->refcnt, vol_release);
1262 /* Get all tcons that are within a DFS namespace and can be refreshed */
1263 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1265 struct cifs_ses *ses;
1266 struct cifs_tcon *tcon;
1268 INIT_LIST_HEAD(head);
1270 spin_lock(&cifs_tcp_ses_lock);
1271 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1272 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1273 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1276 list_add_tail(&tcon->ulist, head);
1279 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1280 ses->tcon_ipc->dfs_path) {
1281 list_add_tail(&ses->tcon_ipc->ulist, head);
1284 spin_unlock(&cifs_tcp_ses_lock);
1287 static bool is_dfs_link(const char *path)
1291 s = strchr(path + 1, '\\');
1294 return !!strchr(s + 1, '\\');
1297 static char *get_dfs_root(const char *path)
1301 s = strchr(path + 1, '\\');
1303 return ERR_PTR(-EINVAL);
1305 s = strchr(s + 1, '\\');
1307 return ERR_PTR(-EINVAL);
1309 npath = kstrndup(path, s - path, GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1316 static inline void put_tcp_server(struct TCP_Server_Info *server)
1318 cifs_put_tcp_session(server, 0);
1321 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1323 struct TCP_Server_Info *server;
1325 server = cifs_find_tcp_session(vol);
1326 if (IS_ERR_OR_NULL(server))
1329 spin_lock(&GlobalMid_Lock);
1330 if (server->tcpStatus != CifsGood) {
1331 spin_unlock(&GlobalMid_Lock);
1332 put_tcp_server(server);
1335 spin_unlock(&GlobalMid_Lock);
1340 /* Find root SMB session out of a DFS link path */
1341 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1342 struct cifs_tcon *tcon,
1347 struct cache_entry *ce;
1348 struct dfs_info3_param ref = {0};
1349 char *mdata = NULL, *devname = NULL;
1350 struct TCP_Server_Info *server;
1351 struct cifs_ses *ses;
1352 struct smb_vol vol = {NULL};
1354 rpath = get_dfs_root(path);
1356 return ERR_CAST(rpath);
1358 down_read(&htable_rw_lock);
1360 ce = lookup_cache_entry(rpath, NULL);
1362 up_read(&htable_rw_lock);
1367 rc = setup_referral(path, ce, &ref, get_tgt_name(ce));
1369 up_read(&htable_rw_lock);
1374 up_read(&htable_rw_lock);
1376 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref,
1378 free_dfs_info_param(&ref);
1380 if (IS_ERR(mdata)) {
1381 ses = ERR_CAST(mdata);
1386 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1394 server = get_tcp_server(&vol);
1396 ses = ERR_PTR(-EHOSTDOWN);
1400 ses = cifs_get_smb_ses(server, &vol);
1403 cifs_cleanup_volume_info_contents(&vol);
1410 /* Refresh DFS cache entry from a given tcon */
1411 static int refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1416 struct cache_entry *ce;
1417 struct cifs_ses *root_ses = NULL, *ses;
1418 struct dfs_info3_param *refs = NULL;
1423 path = tcon->dfs_path + 1;
1425 rc = get_normalized_path(path, &npath);
1429 down_read(&htable_rw_lock);
1431 ce = lookup_cache_entry(npath, NULL);
1434 up_read(&htable_rw_lock);
1438 if (!cache_entry_expired(ce)) {
1439 up_read(&htable_rw_lock);
1443 up_read(&htable_rw_lock);
1445 /* If it's a DFS Link, then use root SMB session for refreshing it */
1446 if (is_dfs_link(npath)) {
1447 ses = root_ses = find_root_ses(vi, tcon, npath);
1457 rc = get_dfs_referral(xid, ses, cache_nlsc, tcon->remap, npath, &refs,
1460 dump_refs(refs, numrefs);
1461 rc = update_cache_entry(npath, refs, numrefs);
1462 free_dfs_info_array(refs, numrefs);
1466 cifs_put_smb_ses(root_ses);
1469 free_normalized_path(path, npath);
1477 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1480 static void refresh_cache_worker(struct work_struct *work)
1482 struct vol_info *vi, *nvi;
1483 struct TCP_Server_Info *server;
1486 struct cifs_tcon *tcon, *ntcon;
1490 * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1493 spin_lock(&vol_list_lock);
1494 list_for_each_entry(vi, &vol_list, list) {
1495 server = get_tcp_server(&vi->smb_vol);
1499 kref_get(&vi->refcnt);
1500 list_add_tail(&vi->rlist, &vols);
1501 put_tcp_server(server);
1503 spin_unlock(&vol_list_lock);
1505 /* Walk through all TCONs and refresh any expired cache entry */
1506 list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1507 spin_lock(&vi->smb_vol_lock);
1508 server = get_tcp_server(&vi->smb_vol);
1509 spin_unlock(&vi->smb_vol_lock);
1514 get_tcons(server, &tcons);
1517 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1519 * Skip tcp server if any of its tcons failed to refresh
1520 * (possibily due to reconnects).
1523 rc = refresh_tcon(vi, tcon);
1525 list_del_init(&tcon->ulist);
1526 cifs_put_tcon(tcon);
1529 put_tcp_server(server);
1532 list_del_init(&vi->rlist);
1533 kref_put(&vi->refcnt, vol_release);
1536 spin_lock(&cache_ttl_lock);
1537 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1538 spin_unlock(&cache_ttl_lock);