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 struct smb_vol smb_vol;
54 struct list_head list;
57 static struct kmem_cache *cache_slab __read_mostly;
58 static struct workqueue_struct *dfscache_wq __read_mostly;
61 static struct nls_table *cache_nlsc;
64 * Number of entries in the cache
66 static size_t cache_count;
68 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
69 static DEFINE_MUTEX(list_lock);
71 static LIST_HEAD(vol_list);
72 static DEFINE_MUTEX(vol_lock);
74 static void refresh_cache_worker(struct work_struct *work);
76 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
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 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 void free_cache_entry(struct rcu_head *rcu)
123 struct cache_entry *ce = container_of(rcu, struct cache_entry, rcu);
125 kmem_cache_free(cache_slab, ce);
128 static inline void flush_cache_ent(struct cache_entry *ce)
130 if (hlist_unhashed(&ce->hlist))
133 hlist_del_init_rcu(&ce->hlist);
137 call_rcu(&ce->rcu, free_cache_entry);
140 static void flush_cache_ents(void)
145 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
146 struct hlist_head *l = &cache_htable[i];
147 struct cache_entry *ce;
149 hlist_for_each_entry_rcu(ce, l, hlist)
156 * dfs cache /proc file
158 static int dfscache_proc_show(struct seq_file *m, void *v)
161 struct cache_entry *ce;
162 struct cache_dfs_tgt *t;
164 seq_puts(m, "DFS cache\n---------\n");
166 mutex_lock(&list_lock);
169 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
171 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
172 "interlink=%s,path_consumed=%d,expired=%s\n",
174 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
175 ce->ttl, ce->etime.tv_nsec,
176 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
178 cache_entry_expired(ce) ? "yes" : "no");
180 list_for_each_entry(t, &ce->tlist, list) {
181 seq_printf(m, " %s%s\n",
183 ce->tgthint == t ? " (target hint)" : "");
189 mutex_unlock(&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(&list_lock);
209 mutex_unlock(&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 cache_entry *ce)
230 struct cache_dfs_tgt *t;
232 cifs_dbg(FYI, "target list:\n");
233 list_for_each_entry(t, &ce->tlist, list) {
234 cifs_dbg(FYI, " %s%s\n", t->name,
235 ce->tgthint == t ? " (target hint)" : "");
239 static inline void dump_ce(const struct 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->path,
243 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
245 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
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)
289 dfscache_wq = alloc_workqueue("cifs-dfscache",
290 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
294 cache_slab = kmem_cache_create("cifs_dfs_cache",
295 sizeof(struct cache_entry), 0,
296 SLAB_HWCACHE_ALIGN, NULL);
302 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
303 INIT_HLIST_HEAD(&cache_htable[i]);
306 cache_nlsc = load_nls_default();
308 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
312 destroy_workqueue(dfscache_wq);
316 static inline unsigned int cache_entry_hash(const void *data, int size)
320 h = jhash(data, size, 0);
321 return h & (CACHE_HTABLE_SIZE - 1);
324 /* Check whether second path component of @path is SYSVOL or NETLOGON */
325 static inline bool is_sysvol_or_netlogon(const char *path)
330 s = strchr(path + 1, sep) + 1;
331 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
332 !strncasecmp(s, "netlogon", strlen("netlogon"));
335 /* Return target hint of a DFS cache entry */
336 static inline char *get_tgt_name(const struct cache_entry *ce)
338 struct cache_dfs_tgt *t = ce->tgthint;
340 return t ? t->name : ERR_PTR(-ENOENT);
343 /* Return expire time out of a new entry's TTL */
344 static inline struct timespec64 get_expire_time(int ttl)
346 struct timespec64 ts = {
350 struct timespec64 now;
352 ktime_get_coarse_real_ts64(&now);
353 return timespec64_add(now, ts);
356 /* Allocate a new DFS target */
357 static inline struct cache_dfs_tgt *alloc_tgt(const char *name)
359 struct cache_dfs_tgt *t;
361 t = kmalloc(sizeof(*t), GFP_KERNEL);
363 return ERR_PTR(-ENOMEM);
364 t->name = kstrndup(name, strlen(name), GFP_KERNEL);
367 return ERR_PTR(-ENOMEM);
369 INIT_LIST_HEAD(&t->list);
374 * Copy DFS referral information to a cache entry and conditionally update
377 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
378 struct cache_entry *ce, const char *tgthint)
382 ce->ttl = refs[0].ttl;
383 ce->etime = get_expire_time(ce->ttl);
384 ce->srvtype = refs[0].server_type;
385 ce->flags = refs[0].ref_flag;
386 ce->path_consumed = refs[0].path_consumed;
388 for (i = 0; i < numrefs; i++) {
389 struct cache_dfs_tgt *t;
391 t = alloc_tgt(refs[i].node_name);
396 if (tgthint && !strcasecmp(t->name, tgthint)) {
397 list_add(&t->list, &ce->tlist);
400 list_add_tail(&t->list, &ce->tlist);
405 ce->tgthint = list_first_entry_or_null(&ce->tlist,
406 struct cache_dfs_tgt, list);
411 /* Allocate a new cache entry */
412 static struct cache_entry *alloc_cache_entry(const char *path,
413 const struct dfs_info3_param *refs,
416 struct cache_entry *ce;
419 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
421 return ERR_PTR(-ENOMEM);
423 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
425 kmem_cache_free(cache_slab, ce);
426 return ERR_PTR(-ENOMEM);
428 INIT_HLIST_NODE(&ce->hlist);
429 INIT_LIST_HEAD(&ce->tlist);
431 rc = copy_ref_data(refs, numrefs, ce, NULL);
434 kmem_cache_free(cache_slab, ce);
440 static void remove_oldest_entry(void)
443 struct cache_entry *ce;
444 struct cache_entry *to_del = NULL;
447 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
448 if (!to_del || timespec64_compare(&ce->etime,
453 cifs_dbg(FYI, "%s: no entry to remove", __func__);
456 cifs_dbg(FYI, "%s: removing entry", __func__);
458 flush_cache_ent(to_del);
463 /* Add a new DFS cache entry */
464 static inline struct cache_entry *
465 add_cache_entry(unsigned int hash, const char *path,
466 const struct dfs_info3_param *refs, int numrefs)
468 struct cache_entry *ce;
470 ce = alloc_cache_entry(path, refs, numrefs);
474 hlist_add_head_rcu(&ce->hlist, &cache_htable[hash]);
476 mutex_lock(&vol_lock);
479 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
481 cache_ttl = min_t(int, cache_ttl, ce->ttl);
482 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
484 mutex_unlock(&vol_lock);
490 * Find a DFS cache entry in hash table and optionally check prefix path against
492 * Use whole path components in the match.
493 * Return ERR_PTR(-ENOENT) if the entry is not found.
495 static struct cache_entry *lookup_cache_entry(const char *path,
498 struct cache_entry *ce;
502 h = cache_entry_hash(path, strlen(path));
505 hlist_for_each_entry_rcu(ce, &cache_htable[h], hlist) {
506 if (!strcasecmp(path, ce->path)) {
515 ce = ERR_PTR(-ENOENT);
522 static inline void destroy_slab_cache(void)
525 kmem_cache_destroy(cache_slab);
528 static inline void free_vol(struct vol_info *vi)
533 cifs_cleanup_volume_info_contents(&vi->smb_vol);
537 static inline void free_vol_list(void)
539 struct vol_info *vi, *nvi;
541 list_for_each_entry_safe(vi, nvi, &vol_list, list)
546 * dfs_cache_destroy - destroy DFS referral cache
548 void dfs_cache_destroy(void)
550 cancel_delayed_work_sync(&refresh_task);
551 unload_nls(cache_nlsc);
554 destroy_slab_cache();
555 destroy_workqueue(dfscache_wq);
557 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
560 static inline struct cache_entry *
561 __update_cache_entry(const char *path, const struct dfs_info3_param *refs,
566 struct cache_entry *ce;
569 ce = lookup_cache_entry(path, &h);
574 s = ce->tgthint->name;
575 th = kstrndup(s, strlen(s), GFP_KERNEL);
577 return ERR_PTR(-ENOMEM);
583 rc = copy_ref_data(refs, numrefs, ce, th);
592 /* Update an expired cache entry by getting a new DFS referral from server */
593 static struct cache_entry *
594 update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
595 const struct nls_table *nls_codepage, int remap,
596 const char *path, struct cache_entry *ce)
599 struct dfs_info3_param *refs = NULL;
602 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
604 * Check if caller provided enough parameters to update an expired
607 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
608 return ERR_PTR(-ETIME);
609 if (unlikely(!nls_codepage))
610 return ERR_PTR(-ETIME);
612 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
614 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
615 nls_codepage, remap);
619 ce = __update_cache_entry(path, refs, numrefs);
621 dump_refs(refs, numrefs);
622 free_dfs_info_array(refs, numrefs);
628 * Find, create or update a DFS cache entry.
630 * If the entry wasn't found, it will create a new one. Or if it was found but
631 * expired, then it will update the entry accordingly.
633 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
634 * handle them properly.
636 static struct cache_entry *
637 do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
638 const struct nls_table *nls_codepage, int remap,
639 const char *path, bool noreq)
643 struct cache_entry *ce;
644 struct dfs_info3_param *nrefs;
647 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
649 ce = lookup_cache_entry(path, &h);
651 cifs_dbg(FYI, "%s: cache miss\n", __func__);
653 * If @noreq is set, no requests will be sent to the server for
654 * either updating or getting a new DFS referral.
659 * No cache entry was found, so check for valid parameters that
660 * will be required to get a new DFS referral and then create a
663 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
664 ce = ERR_PTR(-EOPNOTSUPP);
667 if (unlikely(!nls_codepage)) {
668 ce = ERR_PTR(-EINVAL);
675 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
678 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
679 &numnrefs, nls_codepage,
686 dump_refs(nrefs, numnrefs);
688 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
690 if (cache_count >= CACHE_MAX_ENTRIES) {
691 cifs_dbg(FYI, "%s: reached max cache size (%d)",
692 __func__, CACHE_MAX_ENTRIES);
693 remove_oldest_entry();
695 ce = add_cache_entry(h, path, nrefs, numnrefs);
696 free_dfs_info_array(nrefs, numnrefs);
706 /* Just return the found cache entry in case @noreq is set */
710 if (cache_entry_expired(ce)) {
711 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
712 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
715 cifs_dbg(FYI, "%s: failed to update expired entry\n",
722 /* Set up a new DFS referral from a given cache entry */
723 static int setup_ref(const char *path, const struct cache_entry *ce,
724 struct dfs_info3_param *ref, const char *tgt)
728 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
730 memset(ref, 0, sizeof(*ref));
732 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
736 ref->path_consumed = ce->path_consumed;
738 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
739 if (!ref->node_name) {
745 ref->server_type = ce->srvtype;
746 ref->ref_flag = ce->flags;
751 kfree(ref->path_name);
752 ref->path_name = NULL;
756 /* Return target list of a DFS cache entry */
757 static int get_tgt_list(const struct cache_entry *ce,
758 struct dfs_cache_tgt_list *tl)
761 struct list_head *head = &tl->tl_list;
762 struct cache_dfs_tgt *t;
763 struct dfs_cache_tgt_iterator *it, *nit;
765 memset(tl, 0, sizeof(*tl));
766 INIT_LIST_HEAD(head);
768 list_for_each_entry(t, &ce->tlist, list) {
769 it = kzalloc(sizeof(*it), GFP_KERNEL);
775 it->it_name = kstrndup(t->name, strlen(t->name),
783 if (ce->tgthint == t)
784 list_add(&it->it_list, head);
786 list_add_tail(&it->it_list, head);
788 tl->tl_numtgts = ce->numtgts;
793 list_for_each_entry_safe(it, nit, head, it_list) {
801 * dfs_cache_find - find a DFS cache entry
803 * If it doesn't find the cache entry, then it will get a DFS referral
804 * for @path and create a new entry.
806 * In case the cache entry exists but expired, it will get a DFS referral
807 * for @path and then update the respective cache entry.
809 * These parameters are passed down to the get_dfs_refer() call if it
810 * needs to be issued:
812 * @ses: smb session to issue the request on
813 * @nls_codepage: charset conversion
814 * @remap: path character remapping type
815 * @path: path to lookup in DFS referral cache.
817 * @ref: when non-NULL, store single DFS referral result in it.
818 * @tgt_list: when non-NULL, store complete DFS target list in it.
820 * Return zero if the target was found, otherwise non-zero.
822 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
823 const struct nls_table *nls_codepage, int remap,
824 const char *path, struct dfs_info3_param *ref,
825 struct dfs_cache_tgt_list *tgt_list)
829 struct cache_entry *ce;
831 if (unlikely(!is_path_valid(path)))
834 rc = get_normalized_path(path, &npath);
838 mutex_lock(&list_lock);
839 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
842 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
846 rc = get_tgt_list(ce, tgt_list);
850 mutex_unlock(&list_lock);
851 free_normalized_path(path, npath);
856 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
857 * the currently connected server.
859 * NOTE: This function will neither update a cache entry in case it was
860 * expired, nor create a new cache entry if @path hasn't been found. It heavily
861 * relies on an existing cache entry.
863 * @path: path to lookup in the DFS referral cache.
864 * @ref: when non-NULL, store single DFS referral result in it.
865 * @tgt_list: when non-NULL, store complete DFS target list in it.
867 * Return 0 if successful.
868 * Return -ENOENT if the entry was not found.
869 * Return non-zero for other errors.
871 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
872 struct dfs_cache_tgt_list *tgt_list)
876 struct cache_entry *ce;
878 if (unlikely(!is_path_valid(path)))
881 rc = get_normalized_path(path, &npath);
885 mutex_lock(&list_lock);
886 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
893 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
897 rc = get_tgt_list(ce, tgt_list);
899 mutex_unlock(&list_lock);
900 free_normalized_path(path, npath);
905 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
907 * If it doesn't find the cache entry, then it will get a DFS referral for @path
908 * and create a new entry.
910 * In case the cache entry exists but expired, it will get a DFS referral
911 * for @path and then update the respective cache entry.
915 * @nls_codepage: charset conversion
916 * @remap: type of character remapping for paths
917 * @path: path to lookup in DFS referral cache.
918 * @it: DFS target iterator
920 * Return zero if the target hint was updated successfully, otherwise non-zero.
922 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
923 const struct nls_table *nls_codepage, int remap,
925 const struct dfs_cache_tgt_iterator *it)
929 struct cache_entry *ce;
930 struct cache_dfs_tgt *t;
932 if (unlikely(!is_path_valid(path)))
935 rc = get_normalized_path(path, &npath);
939 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
941 mutex_lock(&list_lock);
942 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
952 if (likely(!strcasecmp(it->it_name, t->name)))
955 list_for_each_entry(t, &ce->tlist, list) {
956 if (!strcasecmp(t->name, it->it_name)) {
958 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
965 mutex_unlock(&list_lock);
966 free_normalized_path(path, npath);
971 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
972 * without sending any requests to the currently connected server.
974 * NOTE: This function will neither update a cache entry in case it was
975 * expired, nor create a new cache entry if @path hasn't been found. It heavily
976 * relies on an existing cache entry.
978 * @path: path to lookup in DFS referral cache.
979 * @it: target iterator which contains the target hint to update the cache
982 * Return zero if the target hint was updated successfully, otherwise non-zero.
984 int dfs_cache_noreq_update_tgthint(const char *path,
985 const struct dfs_cache_tgt_iterator *it)
989 struct cache_entry *ce;
990 struct cache_dfs_tgt *t;
992 if (unlikely(!is_path_valid(path)) || !it)
995 rc = get_normalized_path(path, &npath);
999 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1001 mutex_lock(&list_lock);
1003 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1013 if (unlikely(!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 mutex_unlock(&list_lock);
1027 free_normalized_path(path, npath);
1032 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1033 * target iterator (@it).
1035 * @path: path to lookup in DFS referral cache.
1036 * @it: DFS target iterator.
1037 * @ref: DFS referral pointer to set up the gathered information.
1039 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1041 int dfs_cache_get_tgt_referral(const char *path,
1042 const struct dfs_cache_tgt_iterator *it,
1043 struct dfs_info3_param *ref)
1047 struct cache_entry *ce;
1052 if (unlikely(!is_path_valid(path)))
1055 rc = get_normalized_path(path, &npath);
1059 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1061 mutex_lock(&list_lock);
1063 ce = lookup_cache_entry(npath, &h);
1069 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1071 rc = setup_ref(path, ce, ref, it->it_name);
1074 mutex_unlock(&list_lock);
1075 free_normalized_path(path, npath);
1079 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1081 memcpy(new, vol, sizeof(*new));
1083 if (vol->username) {
1084 new->username = kstrndup(vol->username, strlen(vol->username),
1089 if (vol->password) {
1090 new->password = kstrndup(vol->password, strlen(vol->password),
1093 goto err_free_username;
1096 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1097 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1099 goto err_free_password;
1101 if (vol->domainname) {
1102 new->domainname = kstrndup(vol->domainname,
1103 strlen(vol->domainname), GFP_KERNEL);
1104 if (!new->domainname)
1107 if (vol->iocharset) {
1108 new->iocharset = kstrndup(vol->iocharset,
1109 strlen(vol->iocharset), GFP_KERNEL);
1110 if (!new->iocharset)
1111 goto err_free_domainname;
1114 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1115 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1118 goto err_free_iocharset;
1124 kfree(new->iocharset);
1125 err_free_domainname:
1126 kfree(new->domainname);
1130 kzfree(new->password);
1132 kfree(new->username);
1138 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1139 * DFS cache refresh worker.
1141 * @mntdata: mount data.
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(char *mntdata, struct smb_vol *vol, const char *fullpath)
1150 struct vol_info *vi;
1152 if (!vol || !fullpath || !mntdata)
1155 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1157 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1161 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1162 if (!vi->fullpath) {
1167 rc = dup_vol(vol, &vi->smb_vol);
1169 goto err_free_fullpath;
1171 vi->mntdata = mntdata;
1173 mutex_lock(&vol_lock);
1174 list_add_tail(&vi->list, &vol_list);
1175 mutex_unlock(&vol_lock);
1179 kfree(vi->fullpath);
1185 static inline struct vol_info *find_vol(const char *fullpath)
1187 struct vol_info *vi;
1189 list_for_each_entry(vi, &vol_list, list) {
1190 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1191 if (!strcasecmp(vi->fullpath, fullpath))
1194 return ERR_PTR(-ENOENT);
1198 * dfs_cache_update_vol - update vol info in DFS cache after failover
1200 * @fullpath: fullpath to look up in volume list.
1201 * @server: TCP ses pointer.
1203 * Return zero if volume was updated, otherwise non-zero.
1205 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1208 struct vol_info *vi;
1210 if (!fullpath || !server)
1213 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1215 mutex_lock(&vol_lock);
1217 vi = find_vol(fullpath);
1223 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1224 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1225 sizeof(vi->smb_vol.dstaddr));
1229 mutex_unlock(&vol_lock);
1234 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1236 * @fullpath: fullpath to look up in volume list.
1238 void dfs_cache_del_vol(const char *fullpath)
1240 struct vol_info *vi;
1242 if (!fullpath || !*fullpath)
1245 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1247 mutex_lock(&vol_lock);
1248 vi = find_vol(fullpath);
1251 mutex_unlock(&vol_lock);
1254 /* Get all tcons that are within a DFS namespace and can be refreshed */
1255 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1257 struct cifs_ses *ses;
1258 struct cifs_tcon *tcon;
1260 INIT_LIST_HEAD(head);
1262 spin_lock(&cifs_tcp_ses_lock);
1263 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1264 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1265 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1268 list_add_tail(&tcon->ulist, head);
1271 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1272 ses->tcon_ipc->dfs_path) {
1273 list_add_tail(&ses->tcon_ipc->ulist, head);
1276 spin_unlock(&cifs_tcp_ses_lock);
1279 static bool is_dfs_link(const char *path)
1283 s = strchr(path + 1, '\\');
1286 return !!strchr(s + 1, '\\');
1289 static char *get_dfs_root(const char *path)
1293 s = strchr(path + 1, '\\');
1295 return ERR_PTR(-EINVAL);
1297 s = strchr(s + 1, '\\');
1299 return ERR_PTR(-EINVAL);
1301 npath = kstrndup(path, s - path, GFP_KERNEL);
1303 return ERR_PTR(-ENOMEM);
1308 static inline void put_tcp_server(struct TCP_Server_Info *server)
1310 cifs_put_tcp_session(server, 0);
1313 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1315 struct TCP_Server_Info *server;
1317 server = cifs_find_tcp_session(vol);
1318 if (IS_ERR_OR_NULL(server))
1321 spin_lock(&GlobalMid_Lock);
1322 if (server->tcpStatus != CifsGood) {
1323 spin_unlock(&GlobalMid_Lock);
1324 put_tcp_server(server);
1327 spin_unlock(&GlobalMid_Lock);
1332 /* Find root SMB session out of a DFS link path */
1333 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1334 struct cifs_tcon *tcon,
1339 struct dfs_info3_param ref = {0};
1340 char *mdata = NULL, *devname = NULL;
1341 struct TCP_Server_Info *server;
1342 struct cifs_ses *ses;
1345 rpath = get_dfs_root(path);
1347 return ERR_CAST(rpath);
1349 memset(&vol, 0, sizeof(vol));
1351 rc = dfs_cache_noreq_find(rpath, &ref, NULL);
1357 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref, &devname);
1358 free_dfs_info_param(&ref);
1360 if (IS_ERR(mdata)) {
1361 ses = ERR_CAST(mdata);
1366 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1374 server = get_tcp_server(&vol);
1376 ses = ERR_PTR(-EHOSTDOWN);
1380 ses = cifs_get_smb_ses(server, &vol);
1383 cifs_cleanup_volume_info_contents(&vol);
1390 /* Refresh DFS cache entry from a given tcon */
1391 static void refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1397 struct cache_entry *ce;
1398 struct dfs_info3_param *refs = NULL;
1400 struct cifs_ses *root_ses = NULL, *ses;
1404 path = tcon->dfs_path + 1;
1406 rc = get_normalized_path(path, &npath);
1410 mutex_lock(&list_lock);
1411 ce = lookup_cache_entry(npath, &h);
1412 mutex_unlock(&list_lock);
1419 if (!cache_entry_expired(ce))
1422 /* If it's a DFS Link, then use root SMB session for refreshing it */
1423 if (is_dfs_link(npath)) {
1424 ses = root_ses = find_root_ses(vi, tcon, npath);
1434 if (unlikely(!ses->server->ops->get_dfs_refer)) {
1437 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs,
1438 &numrefs, cache_nlsc,
1441 mutex_lock(&list_lock);
1442 ce = __update_cache_entry(npath, refs, numrefs);
1443 mutex_unlock(&list_lock);
1444 dump_refs(refs, numrefs);
1445 free_dfs_info_array(refs, numrefs);
1453 cifs_put_smb_ses(root_ses);
1456 free_normalized_path(path, npath);
1460 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1463 static void refresh_cache_worker(struct work_struct *work)
1465 struct vol_info *vi;
1466 struct TCP_Server_Info *server;
1468 struct cifs_tcon *tcon, *ntcon;
1470 mutex_lock(&vol_lock);
1472 list_for_each_entry(vi, &vol_list, list) {
1473 server = get_tcp_server(&vi->smb_vol);
1477 get_tcons(server, &list);
1478 list_for_each_entry_safe(tcon, ntcon, &list, ulist) {
1479 refresh_tcon(vi, tcon);
1480 list_del_init(&tcon->ulist);
1481 cifs_put_tcon(tcon);
1484 put_tcp_server(server);
1486 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1487 mutex_unlock(&vol_lock);