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 int get_normalized_path(const char *path, char **npath)
80 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
84 *npath = (char *)path;
86 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
89 convert_delimiter(*npath, '\\');
94 static inline void free_normalized_path(const char *path, char *npath)
100 static inline bool cache_entry_expired(const struct cache_entry *ce)
102 struct timespec64 ts;
104 ktime_get_coarse_real_ts64(&ts);
105 return timespec64_compare(&ts, &ce->etime) >= 0;
108 static inline void free_tgts(struct cache_entry *ce)
110 struct cache_dfs_tgt *t, *n;
112 list_for_each_entry_safe(t, n, &ce->tlist, list) {
119 static void free_cache_entry(struct rcu_head *rcu)
121 struct cache_entry *ce = container_of(rcu, struct cache_entry, rcu);
123 kmem_cache_free(cache_slab, ce);
126 static inline void flush_cache_ent(struct cache_entry *ce)
128 if (hlist_unhashed(&ce->hlist))
131 hlist_del_init_rcu(&ce->hlist);
135 call_rcu(&ce->rcu, free_cache_entry);
138 static void flush_cache_ents(void)
143 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
144 struct hlist_head *l = &cache_htable[i];
145 struct cache_entry *ce;
147 hlist_for_each_entry_rcu(ce, l, hlist)
154 * dfs cache /proc file
156 static int dfscache_proc_show(struct seq_file *m, void *v)
159 struct cache_entry *ce;
160 struct cache_dfs_tgt *t;
162 seq_puts(m, "DFS cache\n---------\n");
164 mutex_lock(&list_lock);
167 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
169 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
170 "interlink=%s,path_consumed=%d,expired=%s\n",
172 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
173 ce->ttl, ce->etime.tv_nsec,
174 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
176 cache_entry_expired(ce) ? "yes" : "no");
178 list_for_each_entry(t, &ce->tlist, list) {
179 seq_printf(m, " %s%s\n",
181 ce->tgthint == t ? " (target hint)" : "");
187 mutex_unlock(&list_lock);
191 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t *ppos)
197 rc = get_user(c, buffer);
204 cifs_dbg(FYI, "clearing dfs cache");
205 mutex_lock(&list_lock);
207 mutex_unlock(&list_lock);
212 static int dfscache_proc_open(struct inode *inode, struct file *file)
214 return single_open(file, dfscache_proc_show, NULL);
217 const struct file_operations dfscache_proc_fops = {
218 .open = dfscache_proc_open,
221 .release = single_release,
222 .write = dfscache_proc_write,
225 #ifdef CONFIG_CIFS_DEBUG2
226 static inline void dump_tgts(const struct cache_entry *ce)
228 struct cache_dfs_tgt *t;
230 cifs_dbg(FYI, "target list:\n");
231 list_for_each_entry(t, &ce->tlist, list) {
232 cifs_dbg(FYI, " %s%s\n", t->name,
233 ce->tgthint == t ? " (target hint)" : "");
237 static inline void dump_ce(const struct cache_entry *ce)
239 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
240 "interlink=%s,path_consumed=%d,expired=%s\n", ce->path,
241 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
243 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
245 cache_entry_expired(ce) ? "yes" : "no");
249 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
253 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
254 for (i = 0; i < numrefs; i++) {
255 const struct dfs_info3_param *ref = &refs[i];
260 "path_consumed: %d\n"
261 "server_type: 0x%x\n"
266 ref->flags, ref->path_consumed, ref->server_type,
267 ref->ref_flag, ref->path_name, ref->node_name,
268 ref->ttl, ref->ttl / 60);
274 #define dump_refs(r, n)
278 * dfs_cache_init - Initialize DFS referral cache.
280 * Return zero if initialized successfully, otherwise non-zero.
282 int dfs_cache_init(void)
287 dfscache_wq = alloc_workqueue("cifs-dfscache",
288 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
292 cache_slab = kmem_cache_create("cifs_dfs_cache",
293 sizeof(struct cache_entry), 0,
294 SLAB_HWCACHE_ALIGN, NULL);
300 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
301 INIT_HLIST_HEAD(&cache_htable[i]);
304 cache_nlsc = load_nls_default();
306 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
310 destroy_workqueue(dfscache_wq);
314 static inline unsigned int cache_entry_hash(const void *data, int size)
318 h = jhash(data, size, 0);
319 return h & (CACHE_HTABLE_SIZE - 1);
322 /* Check whether second path component of @path is SYSVOL or NETLOGON */
323 static inline bool is_sysvol_or_netlogon(const char *path)
328 s = strchr(path + 1, sep) + 1;
329 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
330 !strncasecmp(s, "netlogon", strlen("netlogon"));
333 /* Return target hint of a DFS cache entry */
334 static inline char *get_tgt_name(const struct cache_entry *ce)
336 struct cache_dfs_tgt *t = ce->tgthint;
338 return t ? t->name : ERR_PTR(-ENOENT);
341 /* Return expire time out of a new entry's TTL */
342 static inline struct timespec64 get_expire_time(int ttl)
344 struct timespec64 ts = {
348 struct timespec64 now;
350 ktime_get_coarse_real_ts64(&now);
351 return timespec64_add(now, ts);
354 /* Allocate a new DFS target */
355 static inline struct cache_dfs_tgt *alloc_tgt(const char *name)
357 struct cache_dfs_tgt *t;
359 t = kmalloc(sizeof(*t), GFP_KERNEL);
361 return ERR_PTR(-ENOMEM);
362 t->name = kstrndup(name, strlen(name), GFP_KERNEL);
365 return ERR_PTR(-ENOMEM);
367 INIT_LIST_HEAD(&t->list);
372 * Copy DFS referral information to a cache entry and conditionally update
375 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
376 struct cache_entry *ce, const char *tgthint)
380 ce->ttl = refs[0].ttl;
381 ce->etime = get_expire_time(ce->ttl);
382 ce->srvtype = refs[0].server_type;
383 ce->flags = refs[0].ref_flag;
384 ce->path_consumed = refs[0].path_consumed;
386 for (i = 0; i < numrefs; i++) {
387 struct cache_dfs_tgt *t;
389 t = alloc_tgt(refs[i].node_name);
394 if (tgthint && !strcasecmp(t->name, tgthint)) {
395 list_add(&t->list, &ce->tlist);
398 list_add_tail(&t->list, &ce->tlist);
403 ce->tgthint = list_first_entry_or_null(&ce->tlist,
404 struct cache_dfs_tgt, list);
409 /* Allocate a new cache entry */
410 static struct cache_entry *alloc_cache_entry(const char *path,
411 const struct dfs_info3_param *refs,
414 struct cache_entry *ce;
417 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
419 return ERR_PTR(-ENOMEM);
421 ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
423 kmem_cache_free(cache_slab, ce);
424 return ERR_PTR(-ENOMEM);
426 INIT_HLIST_NODE(&ce->hlist);
427 INIT_LIST_HEAD(&ce->tlist);
429 rc = copy_ref_data(refs, numrefs, ce, NULL);
432 kmem_cache_free(cache_slab, ce);
438 static void remove_oldest_entry(void)
441 struct cache_entry *ce;
442 struct cache_entry *to_del = NULL;
445 hash_for_each_rcu(cache_htable, bucket, ce, hlist) {
446 if (!to_del || timespec64_compare(&ce->etime,
451 cifs_dbg(FYI, "%s: no entry to remove", __func__);
454 cifs_dbg(FYI, "%s: removing entry", __func__);
456 flush_cache_ent(to_del);
461 /* Add a new DFS cache entry */
462 static inline struct cache_entry *
463 add_cache_entry(unsigned int hash, const char *path,
464 const struct dfs_info3_param *refs, int numrefs)
466 struct cache_entry *ce;
468 ce = alloc_cache_entry(path, refs, numrefs);
472 hlist_add_head_rcu(&ce->hlist, &cache_htable[hash]);
474 mutex_lock(&vol_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 mutex_unlock(&vol_lock);
488 * Find a DFS cache entry in hash table and optionally check prefix path against
490 * Use whole path components in the match.
491 * Return ERR_PTR(-ENOENT) if the entry is not found.
493 static struct cache_entry *lookup_cache_entry(const char *path,
496 struct cache_entry *ce;
500 h = cache_entry_hash(path, strlen(path));
503 hlist_for_each_entry_rcu(ce, &cache_htable[h], hlist) {
504 if (!strcasecmp(path, ce->path)) {
513 ce = ERR_PTR(-ENOENT);
520 static inline void destroy_slab_cache(void)
523 kmem_cache_destroy(cache_slab);
526 static inline void free_vol(struct vol_info *vi)
531 cifs_cleanup_volume_info_contents(&vi->smb_vol);
535 static inline void free_vol_list(void)
537 struct vol_info *vi, *nvi;
539 list_for_each_entry_safe(vi, nvi, &vol_list, list)
544 * dfs_cache_destroy - destroy DFS referral cache
546 void dfs_cache_destroy(void)
548 cancel_delayed_work_sync(&refresh_task);
549 unload_nls(cache_nlsc);
552 destroy_slab_cache();
553 destroy_workqueue(dfscache_wq);
555 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
558 static inline struct cache_entry *
559 __update_cache_entry(const char *path, const struct dfs_info3_param *refs,
564 struct cache_entry *ce;
567 ce = lookup_cache_entry(path, &h);
572 s = ce->tgthint->name;
573 th = kstrndup(s, strlen(s), GFP_KERNEL);
575 return ERR_PTR(-ENOMEM);
581 rc = copy_ref_data(refs, numrefs, ce, th);
590 /* Update an expired cache entry by getting a new DFS referral from server */
591 static struct cache_entry *
592 update_cache_entry(const unsigned int xid, struct cifs_ses *ses,
593 const struct nls_table *nls_codepage, int remap,
594 const char *path, struct cache_entry *ce)
597 struct dfs_info3_param *refs = NULL;
600 cifs_dbg(FYI, "%s: update expired cache entry\n", __func__);
602 * Check if caller provided enough parameters to update an expired
605 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
606 return ERR_PTR(-ETIME);
607 if (unlikely(!nls_codepage))
608 return ERR_PTR(-ETIME);
610 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path);
612 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs,
613 nls_codepage, remap);
617 ce = __update_cache_entry(path, refs, numrefs);
619 dump_refs(refs, numrefs);
620 free_dfs_info_array(refs, numrefs);
626 * Find, create or update a DFS cache entry.
628 * If the entry wasn't found, it will create a new one. Or if it was found but
629 * expired, then it will update the entry accordingly.
631 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
632 * handle them properly.
634 static struct cache_entry *
635 do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
636 const struct nls_table *nls_codepage, int remap,
637 const char *path, bool noreq)
641 struct cache_entry *ce;
642 struct dfs_info3_param *nrefs;
645 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
647 ce = lookup_cache_entry(path, &h);
649 cifs_dbg(FYI, "%s: cache miss\n", __func__);
651 * If @noreq is set, no requests will be sent to the server for
652 * either updating or getting a new DFS referral.
657 * No cache entry was found, so check for valid parameters that
658 * will be required to get a new DFS referral and then create a
661 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) {
662 ce = ERR_PTR(-EOPNOTSUPP);
665 if (unlikely(!nls_codepage)) {
666 ce = ERR_PTR(-EINVAL);
673 cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__,
676 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs,
677 &numnrefs, nls_codepage,
684 dump_refs(nrefs, numnrefs);
686 cifs_dbg(FYI, "%s: new cache entry\n", __func__);
688 if (cache_count >= CACHE_MAX_ENTRIES) {
689 cifs_dbg(FYI, "%s: reached max cache size (%d)",
690 __func__, CACHE_MAX_ENTRIES);
691 remove_oldest_entry();
693 ce = add_cache_entry(h, path, nrefs, numnrefs);
694 free_dfs_info_array(nrefs, numnrefs);
704 /* Just return the found cache entry in case @noreq is set */
708 if (cache_entry_expired(ce)) {
709 cifs_dbg(FYI, "%s: expired cache entry\n", __func__);
710 ce = update_cache_entry(xid, ses, nls_codepage, remap, path,
713 cifs_dbg(FYI, "%s: failed to update expired entry\n",
720 /* Set up a new DFS referral from a given cache entry */
721 static int setup_ref(const char *path, const struct cache_entry *ce,
722 struct dfs_info3_param *ref, const char *tgt)
726 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
728 memset(ref, 0, sizeof(*ref));
730 ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL);
734 ref->path_consumed = ce->path_consumed;
736 ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL);
737 if (!ref->node_name) {
743 ref->server_type = ce->srvtype;
744 ref->ref_flag = ce->flags;
749 kfree(ref->path_name);
750 ref->path_name = NULL;
754 /* Return target list of a DFS cache entry */
755 static int get_tgt_list(const struct cache_entry *ce,
756 struct dfs_cache_tgt_list *tl)
759 struct list_head *head = &tl->tl_list;
760 struct cache_dfs_tgt *t;
761 struct dfs_cache_tgt_iterator *it, *nit;
763 memset(tl, 0, sizeof(*tl));
764 INIT_LIST_HEAD(head);
766 list_for_each_entry(t, &ce->tlist, list) {
767 it = kzalloc(sizeof(*it), GFP_KERNEL);
773 it->it_name = kstrndup(t->name, strlen(t->name),
781 if (ce->tgthint == t)
782 list_add(&it->it_list, head);
784 list_add_tail(&it->it_list, head);
786 tl->tl_numtgts = ce->numtgts;
791 list_for_each_entry_safe(it, nit, head, it_list) {
799 * dfs_cache_find - find a DFS cache entry
801 * If it doesn't find the cache entry, then it will get a DFS referral
802 * for @path and create a new entry.
804 * In case the cache entry exists but expired, it will get a DFS referral
805 * for @path and then update the respective cache entry.
807 * These parameters are passed down to the get_dfs_refer() call if it
808 * needs to be issued:
810 * @ses: smb session to issue the request on
811 * @nls_codepage: charset conversion
812 * @remap: path character remapping type
813 * @path: path to lookup in DFS referral cache.
815 * @ref: when non-NULL, store single DFS referral result in it.
816 * @tgt_list: when non-NULL, store complete DFS target list in it.
818 * Return zero if the target was found, otherwise non-zero.
820 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
821 const struct nls_table *nls_codepage, int remap,
822 const char *path, struct dfs_info3_param *ref,
823 struct dfs_cache_tgt_list *tgt_list)
827 struct cache_entry *ce;
829 rc = get_normalized_path(path, &npath);
833 mutex_lock(&list_lock);
834 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
837 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
841 rc = get_tgt_list(ce, tgt_list);
845 mutex_unlock(&list_lock);
846 free_normalized_path(path, npath);
851 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
852 * the currently connected server.
854 * NOTE: This function will neither update a cache entry in case it was
855 * expired, nor create a new cache entry if @path hasn't been found. It heavily
856 * relies on an existing cache entry.
858 * @path: path to lookup in the DFS referral cache.
859 * @ref: when non-NULL, store single DFS referral result in it.
860 * @tgt_list: when non-NULL, store complete DFS target list in it.
862 * Return 0 if successful.
863 * Return -ENOENT if the entry was not found.
864 * Return non-zero for other errors.
866 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
867 struct dfs_cache_tgt_list *tgt_list)
871 struct cache_entry *ce;
873 rc = get_normalized_path(path, &npath);
877 mutex_lock(&list_lock);
878 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
885 rc = setup_ref(path, ce, ref, get_tgt_name(ce));
889 rc = get_tgt_list(ce, tgt_list);
891 mutex_unlock(&list_lock);
892 free_normalized_path(path, npath);
897 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
899 * If it doesn't find the cache entry, then it will get a DFS referral for @path
900 * and create a new entry.
902 * In case the cache entry exists but expired, it will get a DFS referral
903 * for @path and then update the respective cache entry.
907 * @nls_codepage: charset conversion
908 * @remap: type of character remapping for paths
909 * @path: path to lookup in DFS referral cache.
910 * @it: DFS target iterator
912 * Return zero if the target hint was updated successfully, otherwise non-zero.
914 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
915 const struct nls_table *nls_codepage, int remap,
917 const struct dfs_cache_tgt_iterator *it)
921 struct cache_entry *ce;
922 struct cache_dfs_tgt *t;
924 rc = get_normalized_path(path, &npath);
928 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
930 mutex_lock(&list_lock);
931 ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
941 if (likely(!strcasecmp(it->it_name, t->name)))
944 list_for_each_entry(t, &ce->tlist, list) {
945 if (!strcasecmp(t->name, it->it_name)) {
947 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
954 mutex_unlock(&list_lock);
955 free_normalized_path(path, npath);
960 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
961 * without sending any requests to the currently connected server.
963 * NOTE: This function will neither update a cache entry in case it was
964 * expired, nor create a new cache entry if @path hasn't been found. It heavily
965 * relies on an existing cache entry.
967 * @path: path to lookup in DFS referral cache.
968 * @it: target iterator which contains the target hint to update the cache
971 * Return zero if the target hint was updated successfully, otherwise non-zero.
973 int dfs_cache_noreq_update_tgthint(const char *path,
974 const struct dfs_cache_tgt_iterator *it)
978 struct cache_entry *ce;
979 struct cache_dfs_tgt *t;
984 rc = get_normalized_path(path, &npath);
988 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
990 mutex_lock(&list_lock);
992 ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true);
1002 if (unlikely(!strcasecmp(it->it_name, t->name)))
1005 list_for_each_entry(t, &ce->tlist, list) {
1006 if (!strcasecmp(t->name, it->it_name)) {
1008 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1015 mutex_unlock(&list_lock);
1016 free_normalized_path(path, npath);
1021 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1022 * target iterator (@it).
1024 * @path: path to lookup in DFS referral cache.
1025 * @it: DFS target iterator.
1026 * @ref: DFS referral pointer to set up the gathered information.
1028 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1030 int dfs_cache_get_tgt_referral(const char *path,
1031 const struct dfs_cache_tgt_iterator *it,
1032 struct dfs_info3_param *ref)
1036 struct cache_entry *ce;
1042 rc = get_normalized_path(path, &npath);
1046 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1048 mutex_lock(&list_lock);
1050 ce = lookup_cache_entry(npath, &h);
1056 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1058 rc = setup_ref(path, ce, ref, it->it_name);
1061 mutex_unlock(&list_lock);
1062 free_normalized_path(path, npath);
1066 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1068 memcpy(new, vol, sizeof(*new));
1070 if (vol->username) {
1071 new->username = kstrndup(vol->username, strlen(vol->username),
1076 if (vol->password) {
1077 new->password = kstrndup(vol->password, strlen(vol->password),
1080 goto err_free_username;
1083 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1084 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1086 goto err_free_password;
1088 if (vol->domainname) {
1089 new->domainname = kstrndup(vol->domainname,
1090 strlen(vol->domainname), GFP_KERNEL);
1091 if (!new->domainname)
1094 if (vol->iocharset) {
1095 new->iocharset = kstrndup(vol->iocharset,
1096 strlen(vol->iocharset), GFP_KERNEL);
1097 if (!new->iocharset)
1098 goto err_free_domainname;
1101 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1102 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1105 goto err_free_iocharset;
1111 kfree(new->iocharset);
1112 err_free_domainname:
1113 kfree(new->domainname);
1117 kzfree(new->password);
1119 kfree(new->username);
1125 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1126 * DFS cache refresh worker.
1128 * @mntdata: mount data.
1129 * @vol: cifs volume.
1130 * @fullpath: origin full path.
1132 * Return zero if volume was set up correctly, otherwise non-zero.
1134 int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
1137 struct vol_info *vi;
1139 if (!vol || !fullpath || !mntdata)
1142 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1144 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1148 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1149 if (!vi->fullpath) {
1154 rc = dup_vol(vol, &vi->smb_vol);
1156 goto err_free_fullpath;
1158 vi->mntdata = mntdata;
1160 mutex_lock(&vol_lock);
1161 list_add_tail(&vi->list, &vol_list);
1162 mutex_unlock(&vol_lock);
1166 kfree(vi->fullpath);
1172 static inline struct vol_info *find_vol(const char *fullpath)
1174 struct vol_info *vi;
1176 list_for_each_entry(vi, &vol_list, list) {
1177 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1178 if (!strcasecmp(vi->fullpath, fullpath))
1181 return ERR_PTR(-ENOENT);
1185 * dfs_cache_update_vol - update vol info in DFS cache after failover
1187 * @fullpath: fullpath to look up in volume list.
1188 * @server: TCP ses pointer.
1190 * Return zero if volume was updated, otherwise non-zero.
1192 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1195 struct vol_info *vi;
1197 if (!fullpath || !server)
1200 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1202 mutex_lock(&vol_lock);
1204 vi = find_vol(fullpath);
1210 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1211 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1212 sizeof(vi->smb_vol.dstaddr));
1216 mutex_unlock(&vol_lock);
1221 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1223 * @fullpath: fullpath to look up in volume list.
1225 void dfs_cache_del_vol(const char *fullpath)
1227 struct vol_info *vi;
1229 if (!fullpath || !*fullpath)
1232 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1234 mutex_lock(&vol_lock);
1235 vi = find_vol(fullpath);
1238 mutex_unlock(&vol_lock);
1241 /* Get all tcons that are within a DFS namespace and can be refreshed */
1242 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1244 struct cifs_ses *ses;
1245 struct cifs_tcon *tcon;
1247 INIT_LIST_HEAD(head);
1249 spin_lock(&cifs_tcp_ses_lock);
1250 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1251 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1252 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1255 list_add_tail(&tcon->ulist, head);
1258 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1259 ses->tcon_ipc->dfs_path) {
1260 list_add_tail(&ses->tcon_ipc->ulist, head);
1263 spin_unlock(&cifs_tcp_ses_lock);
1266 static bool is_dfs_link(const char *path)
1270 s = strchr(path + 1, '\\');
1273 return !!strchr(s + 1, '\\');
1276 static char *get_dfs_root(const char *path)
1280 s = strchr(path + 1, '\\');
1282 return ERR_PTR(-EINVAL);
1284 s = strchr(s + 1, '\\');
1286 return ERR_PTR(-EINVAL);
1288 npath = kstrndup(path, s - path, GFP_KERNEL);
1290 return ERR_PTR(-ENOMEM);
1295 static inline void put_tcp_server(struct TCP_Server_Info *server)
1297 cifs_put_tcp_session(server, 0);
1300 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1302 struct TCP_Server_Info *server;
1304 server = cifs_find_tcp_session(vol);
1305 if (IS_ERR_OR_NULL(server))
1308 spin_lock(&GlobalMid_Lock);
1309 if (server->tcpStatus != CifsGood) {
1310 spin_unlock(&GlobalMid_Lock);
1311 put_tcp_server(server);
1314 spin_unlock(&GlobalMid_Lock);
1319 /* Find root SMB session out of a DFS link path */
1320 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1321 struct cifs_tcon *tcon,
1326 struct dfs_info3_param ref = {0};
1327 char *mdata = NULL, *devname = NULL;
1328 struct TCP_Server_Info *server;
1329 struct cifs_ses *ses;
1332 rpath = get_dfs_root(path);
1334 return ERR_CAST(rpath);
1336 memset(&vol, 0, sizeof(vol));
1338 rc = dfs_cache_noreq_find(rpath, &ref, NULL);
1344 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref, &devname);
1345 free_dfs_info_param(&ref);
1347 if (IS_ERR(mdata)) {
1348 ses = ERR_CAST(mdata);
1353 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1361 server = get_tcp_server(&vol);
1363 ses = ERR_PTR(-EHOSTDOWN);
1367 ses = cifs_get_smb_ses(server, &vol);
1370 cifs_cleanup_volume_info_contents(&vol);
1377 /* Refresh DFS cache entry from a given tcon */
1378 static void refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1384 struct cache_entry *ce;
1385 struct dfs_info3_param *refs = NULL;
1387 struct cifs_ses *root_ses = NULL, *ses;
1391 path = tcon->dfs_path + 1;
1393 rc = get_normalized_path(path, &npath);
1397 mutex_lock(&list_lock);
1398 ce = lookup_cache_entry(npath, &h);
1399 mutex_unlock(&list_lock);
1406 if (!cache_entry_expired(ce))
1409 /* If it's a DFS Link, then use root SMB session for refreshing it */
1410 if (is_dfs_link(npath)) {
1411 ses = root_ses = find_root_ses(vi, tcon, npath);
1421 if (unlikely(!ses->server->ops->get_dfs_refer)) {
1424 rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs,
1425 &numrefs, cache_nlsc,
1428 mutex_lock(&list_lock);
1429 ce = __update_cache_entry(npath, refs, numrefs);
1430 mutex_unlock(&list_lock);
1431 dump_refs(refs, numrefs);
1432 free_dfs_info_array(refs, numrefs);
1440 cifs_put_smb_ses(root_ses);
1443 free_normalized_path(path, npath);
1447 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1450 static void refresh_cache_worker(struct work_struct *work)
1452 struct vol_info *vi;
1453 struct TCP_Server_Info *server;
1455 struct cifs_tcon *tcon, *ntcon;
1457 mutex_lock(&vol_lock);
1459 list_for_each_entry(vi, &vol_list, list) {
1460 server = get_tcp_server(&vi->smb_vol);
1464 get_tcons(server, &list);
1465 list_for_each_entry_safe(tcon, ntcon, &list, ulist) {
1466 refresh_tcon(vi, tcon);
1467 list_del_init(&tcon->ulist);
1468 cifs_put_tcon(tcon);
1471 put_tcp_server(server);
1473 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1474 mutex_unlock(&vol_lock);