1 // SPDX-License-Identifier: GPL-2.0
3 * DFS referral cache routines
5 * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
8 #include <linux/jhash.h>
9 #include <linux/ktime.h>
10 #include <linux/slab.h>
11 #include <linux/proc_fs.h>
12 #include <linux/nls.h>
13 #include <linux/workqueue.h>
16 #include "smb2proto.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
22 #include "dfs_cache.h"
24 #define CACHE_HTABLE_SIZE 32
25 #define CACHE_MAX_ENTRIES 64
27 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
28 DFSREF_STORAGE_SERVER))
30 struct cache_dfs_tgt {
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;
51 spinlock_t smb_vol_lock;
52 struct smb_vol smb_vol;
54 struct list_head list;
55 struct list_head rlist;
59 static struct kmem_cache *cache_slab __read_mostly;
60 static struct workqueue_struct *dfscache_wq __read_mostly;
63 static DEFINE_SPINLOCK(cache_ttl_lock);
65 static struct nls_table *cache_nlsc;
68 * Number of entries in the cache
70 static atomic_t cache_count;
72 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
73 static DECLARE_RWSEM(htable_rw_lock);
75 static LIST_HEAD(vol_list);
76 static DEFINE_SPINLOCK(vol_list_lock);
78 static void refresh_cache_worker(struct work_struct *work);
80 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
82 static int get_normalized_path(const char *path, char **npath)
84 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
88 *npath = (char *)path;
90 *npath = kstrndup(path, strlen(path), GFP_KERNEL);
93 convert_delimiter(*npath, '\\');
98 static inline void free_normalized_path(const char *path, char *npath)
104 static inline bool cache_entry_expired(const struct cache_entry *ce)
106 struct timespec64 ts;
108 ktime_get_coarse_real_ts64(&ts);
109 return timespec64_compare(&ts, &ce->etime) >= 0;
112 static inline void free_tgts(struct cache_entry *ce)
114 struct cache_dfs_tgt *t, *n;
116 list_for_each_entry_safe(t, n, &ce->tlist, list) {
123 static inline void flush_cache_ent(struct cache_entry *ce)
125 hlist_del_init(&ce->hlist);
128 atomic_dec(&cache_count);
129 kmem_cache_free(cache_slab, ce);
132 static void flush_cache_ents(void)
136 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
137 struct hlist_head *l = &cache_htable[i];
138 struct hlist_node *n;
139 struct cache_entry *ce;
141 hlist_for_each_entry_safe(ce, n, l, hlist) {
142 if (!hlist_unhashed(&ce->hlist))
149 * dfs cache /proc file
151 static int dfscache_proc_show(struct seq_file *m, void *v)
154 struct cache_entry *ce;
155 struct cache_dfs_tgt *t;
157 seq_puts(m, "DFS cache\n---------\n");
159 down_read(&htable_rw_lock);
160 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
161 struct hlist_head *l = &cache_htable[i];
163 hlist_for_each_entry(ce, l, hlist) {
164 if (hlist_unhashed(&ce->hlist))
168 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
169 "interlink=%s,path_consumed=%d,expired=%s\n",
171 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
172 ce->ttl, ce->etime.tv_nsec,
173 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
175 cache_entry_expired(ce) ? "yes" : "no");
177 list_for_each_entry(t, &ce->tlist, list) {
178 seq_printf(m, " %s%s\n",
180 ce->tgthint == t ? " (target hint)" : "");
184 up_read(&htable_rw_lock);
189 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
190 size_t count, loff_t *ppos)
195 rc = get_user(c, buffer);
202 cifs_dbg(FYI, "clearing dfs cache\n");
204 down_write(&htable_rw_lock);
206 up_write(&htable_rw_lock);
211 static int dfscache_proc_open(struct inode *inode, struct file *file)
213 return single_open(file, dfscache_proc_show, NULL);
216 const struct proc_ops dfscache_proc_ops = {
217 .proc_open = dfscache_proc_open,
218 .proc_read = seq_read,
219 .proc_lseek = seq_lseek,
220 .proc_release = single_release,
221 .proc_write = dfscache_proc_write,
224 #ifdef CONFIG_CIFS_DEBUG2
225 static inline void dump_tgts(const struct cache_entry *ce)
227 struct cache_dfs_tgt *t;
229 cifs_dbg(FYI, "target list:\n");
230 list_for_each_entry(t, &ce->tlist, list) {
231 cifs_dbg(FYI, " %s%s\n", t->name,
232 ce->tgthint == t ? " (target hint)" : "");
236 static inline void dump_ce(const struct cache_entry *ce)
238 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,interlink=%s,path_consumed=%d,expired=%s\n",
240 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
242 IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
244 cache_entry_expired(ce) ? "yes" : "no");
248 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
252 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
253 for (i = 0; i < numrefs; i++) {
254 const struct dfs_info3_param *ref = &refs[i];
259 "path_consumed: %d\n"
260 "server_type: 0x%x\n"
265 ref->flags, ref->path_consumed, ref->server_type,
266 ref->ref_flag, ref->path_name, ref->node_name,
267 ref->ttl, ref->ttl / 60);
273 #define dump_refs(r, n)
277 * dfs_cache_init - Initialize DFS referral cache.
279 * Return zero if initialized successfully, otherwise non-zero.
281 int dfs_cache_init(void)
286 dfscache_wq = alloc_workqueue("cifs-dfscache",
287 WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
291 cache_slab = kmem_cache_create("cifs_dfs_cache",
292 sizeof(struct cache_entry), 0,
293 SLAB_HWCACHE_ALIGN, NULL);
299 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
300 INIT_HLIST_HEAD(&cache_htable[i]);
302 atomic_set(&cache_count, 0);
303 cache_nlsc = load_nls_default();
305 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
309 destroy_workqueue(dfscache_wq);
313 static inline unsigned int cache_entry_hash(const void *data, int size)
317 h = jhash(data, size, 0);
318 return h & (CACHE_HTABLE_SIZE - 1);
321 /* Check whether second path component of @path is SYSVOL or NETLOGON */
322 static inline bool is_sysvol_or_netlogon(const char *path)
327 s = strchr(path + 1, sep) + 1;
328 return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
329 !strncasecmp(s, "netlogon", strlen("netlogon"));
332 /* Return target hint of a DFS cache entry */
333 static inline char *get_tgt_name(const struct cache_entry *ce)
335 struct cache_dfs_tgt *t = ce->tgthint;
337 return t ? t->name : ERR_PTR(-ENOENT);
340 /* Return expire time out of a new entry's TTL */
341 static inline struct timespec64 get_expire_time(int ttl)
343 struct timespec64 ts = {
347 struct timespec64 now;
349 ktime_get_coarse_real_ts64(&now);
350 return timespec64_add(now, ts);
353 /* Allocate a new DFS target */
354 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
356 struct cache_dfs_tgt *t;
358 t = kmalloc(sizeof(*t), GFP_ATOMIC);
360 return ERR_PTR(-ENOMEM);
361 t->name = kstrndup(name, strlen(name), GFP_ATOMIC);
364 return ERR_PTR(-ENOMEM);
366 t->path_consumed = path_consumed;
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_target(refs[i].node_name, refs[i].path_consumed);
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 /* Must be called with htable_rw_lock held */
439 static void remove_oldest_entry(void)
442 struct cache_entry *ce;
443 struct cache_entry *to_del = NULL;
445 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
446 struct hlist_head *l = &cache_htable[i];
448 hlist_for_each_entry(ce, l, hlist) {
449 if (hlist_unhashed(&ce->hlist))
451 if (!to_del || timespec64_compare(&ce->etime,
458 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
462 cifs_dbg(FYI, "%s: removing entry\n", __func__);
464 flush_cache_ent(to_del);
467 /* Add a new DFS cache entry */
468 static int add_cache_entry(const char *path, unsigned int hash,
469 struct dfs_info3_param *refs, int numrefs)
471 struct cache_entry *ce;
473 ce = alloc_cache_entry(path, refs, numrefs);
477 spin_lock(&cache_ttl_lock);
480 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
482 cache_ttl = min_t(int, cache_ttl, ce->ttl);
483 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
485 spin_unlock(&cache_ttl_lock);
487 down_write(&htable_rw_lock);
488 hlist_add_head(&ce->hlist, &cache_htable[hash]);
490 up_write(&htable_rw_lock);
495 static struct cache_entry *__lookup_cache_entry(const char *path)
497 struct cache_entry *ce;
501 h = cache_entry_hash(path, strlen(path));
503 hlist_for_each_entry(ce, &cache_htable[h], hlist) {
504 if (!strcasecmp(path, ce->path)) {
512 ce = ERR_PTR(-ENOENT);
517 * Find a DFS cache entry in hash table and optionally check prefix path against
519 * Use whole path components in the match.
520 * Must be called with htable_rw_lock held.
522 * Return ERR_PTR(-ENOENT) if the entry is not found.
524 static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
526 struct cache_entry *ce = ERR_PTR(-ENOENT);
533 npath = kstrndup(path, strlen(path), GFP_KERNEL);
535 return ERR_PTR(-ENOMEM);
539 while ((s = strchr(s, sep)) && ++cnt < 3)
543 h = cache_entry_hash(path, strlen(path));
544 ce = __lookup_cache_entry(path);
548 * Handle paths that have more than two path components and are a complete prefix of the DFS
549 * referral request path (@path).
551 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
553 h = cache_entry_hash(npath, strlen(npath));
554 e = npath + strlen(npath) - 1;
558 /* skip separators */
559 while (e > s && *e == sep)
567 ce = __lookup_cache_entry(npath);
569 h = cache_entry_hash(npath, strlen(npath));
574 /* backward until separator */
575 while (e > s && *e != sep)
585 static void __vol_release(struct vol_info *vi)
589 cifs_cleanup_volume_info_contents(&vi->smb_vol);
593 static void vol_release(struct kref *kref)
595 struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
597 spin_lock(&vol_list_lock);
599 spin_unlock(&vol_list_lock);
603 static inline void free_vol_list(void)
605 struct vol_info *vi, *nvi;
607 list_for_each_entry_safe(vi, nvi, &vol_list, list) {
608 list_del_init(&vi->list);
614 * dfs_cache_destroy - destroy DFS referral cache
616 void dfs_cache_destroy(void)
618 cancel_delayed_work_sync(&refresh_task);
619 unload_nls(cache_nlsc);
622 kmem_cache_destroy(cache_slab);
623 destroy_workqueue(dfscache_wq);
625 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
628 /* Must be called with htable_rw_lock held */
629 static int __update_cache_entry(const char *path,
630 const struct dfs_info3_param *refs,
634 struct cache_entry *ce;
637 ce = lookup_cache_entry(path, NULL);
642 s = ce->tgthint->name;
643 th = kstrndup(s, strlen(s), GFP_ATOMIC);
651 rc = copy_ref_data(refs, numrefs, ce, th);
658 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
659 const struct nls_table *nls_codepage, int remap,
660 const char *path, struct dfs_info3_param **refs,
663 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
665 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
667 if (unlikely(!nls_codepage))
673 return ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs,
674 nls_codepage, remap);
677 /* Update an expired cache entry by getting a new DFS referral from server */
678 static int update_cache_entry(const char *path,
679 const struct dfs_info3_param *refs,
685 down_write(&htable_rw_lock);
686 rc = __update_cache_entry(path, refs, numrefs);
687 up_write(&htable_rw_lock);
693 * Find, create or update a DFS cache entry.
695 * If the entry wasn't found, it will create a new one. Or if it was found but
696 * expired, then it will update the entry accordingly.
698 * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
699 * handle them properly.
701 static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
702 const struct nls_table *nls_codepage, int remap,
703 const char *path, bool noreq)
707 struct cache_entry *ce;
708 struct dfs_info3_param *refs = NULL;
712 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
714 down_read(&htable_rw_lock);
716 ce = lookup_cache_entry(path, &hash);
719 * If @noreq is set, no requests will be sent to the server. Just return
723 up_read(&htable_rw_lock);
724 return PTR_ERR_OR_ZERO(ce);
728 if (!cache_entry_expired(ce)) {
730 up_read(&htable_rw_lock);
737 up_read(&htable_rw_lock);
740 * No entry was found.
742 * Request a new DFS referral in order to create a new cache entry, or
743 * updating an existing one.
745 rc = get_dfs_referral(xid, ses, nls_codepage, remap, path,
750 dump_refs(refs, numrefs);
753 rc = update_cache_entry(path, refs, numrefs);
757 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
758 cifs_dbg(FYI, "%s: reached max cache size (%d)\n",
759 __func__, CACHE_MAX_ENTRIES);
760 down_write(&htable_rw_lock);
761 remove_oldest_entry();
762 up_write(&htable_rw_lock);
765 rc = add_cache_entry(path, hash, refs, numrefs);
767 atomic_inc(&cache_count);
770 free_dfs_info_array(refs, numrefs);
775 * Set up a DFS referral from a given cache entry.
777 * Must be called with htable_rw_lock held.
779 static int setup_referral(const char *path, struct cache_entry *ce,
780 struct dfs_info3_param *ref, const char *target)
784 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
786 memset(ref, 0, sizeof(*ref));
788 ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC);
792 ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC);
793 if (!ref->node_name) {
798 ref->path_consumed = ce->path_consumed;
800 ref->server_type = ce->srvtype;
801 ref->ref_flag = ce->flags;
806 kfree(ref->path_name);
807 ref->path_name = NULL;
811 /* Return target list of a DFS cache entry */
812 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
815 struct list_head *head = &tl->tl_list;
816 struct cache_dfs_tgt *t;
817 struct dfs_cache_tgt_iterator *it, *nit;
819 memset(tl, 0, sizeof(*tl));
820 INIT_LIST_HEAD(head);
822 list_for_each_entry(t, &ce->tlist, list) {
823 it = kzalloc(sizeof(*it), GFP_ATOMIC);
829 it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC);
835 it->it_path_consumed = t->path_consumed;
837 if (ce->tgthint == t)
838 list_add(&it->it_list, head);
840 list_add_tail(&it->it_list, head);
843 tl->tl_numtgts = ce->numtgts;
848 list_for_each_entry_safe(it, nit, head, it_list) {
856 * dfs_cache_find - find a DFS cache entry
858 * If it doesn't find the cache entry, then it will get a DFS referral
859 * for @path and create a new entry.
861 * In case the cache entry exists but expired, it will get a DFS referral
862 * for @path and then update the respective cache entry.
864 * These parameters are passed down to the get_dfs_refer() call if it
865 * needs to be issued:
867 * @ses: smb session to issue the request on
868 * @nls_codepage: charset conversion
869 * @remap: path character remapping type
870 * @path: path to lookup in DFS referral cache.
872 * @ref: when non-NULL, store single DFS referral result in it.
873 * @tgt_list: when non-NULL, store complete DFS target list in it.
875 * Return zero if the target was found, otherwise non-zero.
877 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
878 const struct nls_table *nls_codepage, int remap,
879 const char *path, struct dfs_info3_param *ref,
880 struct dfs_cache_tgt_list *tgt_list)
884 struct cache_entry *ce;
886 rc = get_normalized_path(path, &npath);
890 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
894 down_read(&htable_rw_lock);
896 ce = lookup_cache_entry(npath, NULL);
898 up_read(&htable_rw_lock);
904 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
908 rc = get_targets(ce, tgt_list);
910 up_read(&htable_rw_lock);
913 free_normalized_path(path, npath);
918 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
919 * the currently connected server.
921 * NOTE: This function will neither update a cache entry in case it was
922 * expired, nor create a new cache entry if @path hasn't been found. It heavily
923 * relies on an existing cache entry.
925 * @path: path to lookup in the DFS referral cache.
926 * @ref: when non-NULL, store single DFS referral result in it.
927 * @tgt_list: when non-NULL, store complete DFS target list in it.
929 * Return 0 if successful.
930 * Return -ENOENT if the entry was not found.
931 * Return non-zero for other errors.
933 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
934 struct dfs_cache_tgt_list *tgt_list)
938 struct cache_entry *ce;
940 rc = get_normalized_path(path, &npath);
944 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
946 down_read(&htable_rw_lock);
948 ce = lookup_cache_entry(npath, NULL);
955 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
959 rc = get_targets(ce, tgt_list);
962 up_read(&htable_rw_lock);
963 free_normalized_path(path, npath);
969 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
971 * If it doesn't find the cache entry, then it will get a DFS referral for @path
972 * and create a new entry.
974 * In case the cache entry exists but expired, it will get a DFS referral
975 * for @path and then update the respective cache entry.
979 * @nls_codepage: charset conversion
980 * @remap: type of character remapping for paths
981 * @path: path to lookup in DFS referral cache.
982 * @it: DFS target iterator
984 * Return zero if the target hint was updated successfully, otherwise non-zero.
986 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
987 const struct nls_table *nls_codepage, int remap,
989 const struct dfs_cache_tgt_iterator *it)
993 struct cache_entry *ce;
994 struct cache_dfs_tgt *t;
996 rc = get_normalized_path(path, &npath);
1000 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
1002 rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
1006 down_write(&htable_rw_lock);
1008 ce = lookup_cache_entry(npath, NULL);
1016 if (likely(!strcasecmp(it->it_name, t->name)))
1019 list_for_each_entry(t, &ce->tlist, list) {
1020 if (!strcasecmp(t->name, it->it_name)) {
1022 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1029 up_write(&htable_rw_lock);
1031 free_normalized_path(path, npath);
1037 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1038 * without sending any requests to the currently connected server.
1040 * NOTE: This function will neither update a cache entry in case it was
1041 * expired, nor create a new cache entry if @path hasn't been found. It heavily
1042 * relies on an existing cache entry.
1044 * @path: path to lookup in DFS referral cache.
1045 * @it: target iterator which contains the target hint to update the cache
1048 * Return zero if the target hint was updated successfully, otherwise non-zero.
1050 int dfs_cache_noreq_update_tgthint(const char *path,
1051 const struct dfs_cache_tgt_iterator *it)
1055 struct cache_entry *ce;
1056 struct cache_dfs_tgt *t;
1061 rc = get_normalized_path(path, &npath);
1065 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1067 down_write(&htable_rw_lock);
1069 ce = lookup_cache_entry(npath, NULL);
1078 if (unlikely(!strcasecmp(it->it_name, t->name)))
1081 list_for_each_entry(t, &ce->tlist, list) {
1082 if (!strcasecmp(t->name, it->it_name)) {
1084 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1091 up_write(&htable_rw_lock);
1092 free_normalized_path(path, npath);
1098 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1099 * target iterator (@it).
1101 * @path: path to lookup in DFS referral cache.
1102 * @it: DFS target iterator.
1103 * @ref: DFS referral pointer to set up the gathered information.
1105 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1107 int dfs_cache_get_tgt_referral(const char *path,
1108 const struct dfs_cache_tgt_iterator *it,
1109 struct dfs_info3_param *ref)
1113 struct cache_entry *ce;
1118 rc = get_normalized_path(path, &npath);
1122 cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1124 down_read(&htable_rw_lock);
1126 ce = lookup_cache_entry(npath, NULL);
1132 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1134 rc = setup_referral(path, ce, ref, it->it_name);
1137 up_read(&htable_rw_lock);
1138 free_normalized_path(path, npath);
1143 static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
1145 memcpy(new, vol, sizeof(*new));
1147 if (vol->username) {
1148 new->username = kstrndup(vol->username, strlen(vol->username),
1153 if (vol->password) {
1154 new->password = kstrndup(vol->password, strlen(vol->password),
1157 goto err_free_username;
1160 cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
1161 new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
1163 goto err_free_password;
1165 if (vol->domainname) {
1166 new->domainname = kstrndup(vol->domainname,
1167 strlen(vol->domainname), GFP_KERNEL);
1168 if (!new->domainname)
1171 if (vol->iocharset) {
1172 new->iocharset = kstrndup(vol->iocharset,
1173 strlen(vol->iocharset), GFP_KERNEL);
1174 if (!new->iocharset)
1175 goto err_free_domainname;
1178 cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
1179 new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
1182 goto err_free_iocharset;
1188 kfree(new->iocharset);
1189 err_free_domainname:
1190 kfree(new->domainname);
1194 kzfree(new->password);
1196 kfree(new->username);
1202 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
1203 * DFS cache refresh worker.
1205 * @mntdata: mount data.
1206 * @vol: cifs volume.
1207 * @fullpath: origin full path.
1209 * Return zero if volume was set up correctly, otherwise non-zero.
1211 int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
1214 struct vol_info *vi;
1216 if (!vol || !fullpath || !mntdata)
1219 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1221 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1225 vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
1226 if (!vi->fullpath) {
1231 rc = dup_vol(vol, &vi->smb_vol);
1233 goto err_free_fullpath;
1235 vi->mntdata = mntdata;
1236 spin_lock_init(&vi->smb_vol_lock);
1237 kref_init(&vi->refcnt);
1239 spin_lock(&vol_list_lock);
1240 list_add_tail(&vi->list, &vol_list);
1241 spin_unlock(&vol_list_lock);
1246 kfree(vi->fullpath);
1252 /* Must be called with vol_list_lock held */
1253 static struct vol_info *find_vol(const char *fullpath)
1255 struct vol_info *vi;
1257 list_for_each_entry(vi, &vol_list, list) {
1258 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1259 if (!strcasecmp(vi->fullpath, fullpath))
1262 return ERR_PTR(-ENOENT);
1266 * dfs_cache_update_vol - update vol info in DFS cache after failover
1268 * @fullpath: fullpath to look up in volume list.
1269 * @server: TCP ses pointer.
1271 * Return zero if volume was updated, otherwise non-zero.
1273 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1275 struct vol_info *vi;
1277 if (!fullpath || !server)
1280 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1282 spin_lock(&vol_list_lock);
1283 vi = find_vol(fullpath);
1285 spin_unlock(&vol_list_lock);
1288 kref_get(&vi->refcnt);
1289 spin_unlock(&vol_list_lock);
1291 cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1292 spin_lock(&vi->smb_vol_lock);
1293 memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
1294 sizeof(vi->smb_vol.dstaddr));
1295 spin_unlock(&vi->smb_vol_lock);
1297 kref_put(&vi->refcnt, vol_release);
1303 * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1305 * @fullpath: fullpath to look up in volume list.
1307 void dfs_cache_del_vol(const char *fullpath)
1309 struct vol_info *vi;
1311 if (!fullpath || !*fullpath)
1314 cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1316 spin_lock(&vol_list_lock);
1317 vi = find_vol(fullpath);
1318 spin_unlock(&vol_list_lock);
1320 kref_put(&vi->refcnt, vol_release);
1324 * dfs_cache_get_tgt_share - parse a DFS target
1326 * @path: DFS full path
1327 * @it: DFS target iterator.
1328 * @share: tree name.
1329 * @prefix: prefix path.
1331 * Return zero if target was parsed correctly, otherwise non-zero.
1333 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it,
1334 char **share, char **prefix)
1338 size_t plen1, plen2;
1340 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1346 sep = it->it_name[0];
1347 if (sep != '\\' && sep != '/')
1350 s = strchr(it->it_name + 1, sep);
1354 /* point to prefix in target node */
1355 s = strchrnul(s + 1, sep);
1357 /* extract target share */
1358 *share = kstrndup(it->it_name, s - it->it_name, GFP_KERNEL);
1362 /* skip separator */
1365 /* point to prefix in DFS path */
1366 p = path + it->it_path_consumed;
1370 /* merge prefix paths from DFS path and target node */
1371 plen1 = it->it_name + strlen(it->it_name) - s;
1372 plen2 = path + strlen(path) - p;
1373 if (plen1 || plen2) {
1374 len = plen1 + plen2 + 2;
1375 *prefix = kmalloc(len, GFP_KERNEL);
1382 scnprintf(*prefix, len, "%.*s%c%.*s", (int)plen1, s, sep, (int)plen2, p);
1384 strscpy(*prefix, p, len);
1389 /* Get all tcons that are within a DFS namespace and can be refreshed */
1390 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1392 struct cifs_ses *ses;
1393 struct cifs_tcon *tcon;
1395 INIT_LIST_HEAD(head);
1397 spin_lock(&cifs_tcp_ses_lock);
1398 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1399 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1400 if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1403 list_add_tail(&tcon->ulist, head);
1406 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1407 ses->tcon_ipc->dfs_path) {
1408 list_add_tail(&ses->tcon_ipc->ulist, head);
1411 spin_unlock(&cifs_tcp_ses_lock);
1414 static bool is_dfs_link(const char *path)
1418 s = strchr(path + 1, '\\');
1421 return !!strchr(s + 1, '\\');
1424 static char *get_dfs_root(const char *path)
1428 s = strchr(path + 1, '\\');
1430 return ERR_PTR(-EINVAL);
1432 s = strchr(s + 1, '\\');
1434 return ERR_PTR(-EINVAL);
1436 npath = kstrndup(path, s - path, GFP_KERNEL);
1438 return ERR_PTR(-ENOMEM);
1443 static inline void put_tcp_server(struct TCP_Server_Info *server)
1445 cifs_put_tcp_session(server, 0);
1448 static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
1450 struct TCP_Server_Info *server;
1452 server = cifs_find_tcp_session(vol);
1453 if (IS_ERR_OR_NULL(server))
1456 spin_lock(&GlobalMid_Lock);
1457 if (server->tcpStatus != CifsGood) {
1458 spin_unlock(&GlobalMid_Lock);
1459 put_tcp_server(server);
1462 spin_unlock(&GlobalMid_Lock);
1467 /* Find root SMB session out of a DFS link path */
1468 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1469 struct cifs_tcon *tcon,
1474 struct cache_entry *ce;
1475 struct dfs_info3_param ref = {0};
1476 char *mdata = NULL, *devname = NULL;
1477 struct TCP_Server_Info *server;
1478 struct cifs_ses *ses;
1479 struct smb_vol vol = {NULL};
1481 rpath = get_dfs_root(path);
1483 return ERR_CAST(rpath);
1485 down_read(&htable_rw_lock);
1487 ce = lookup_cache_entry(rpath, NULL);
1489 up_read(&htable_rw_lock);
1494 rc = setup_referral(path, ce, &ref, get_tgt_name(ce));
1496 up_read(&htable_rw_lock);
1501 up_read(&htable_rw_lock);
1503 mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref,
1505 free_dfs_info_param(&ref);
1507 if (IS_ERR(mdata)) {
1508 ses = ERR_CAST(mdata);
1513 rc = cifs_setup_volume_info(&vol, mdata, devname, false);
1521 server = get_tcp_server(&vol);
1523 ses = ERR_PTR(-EHOSTDOWN);
1527 ses = cifs_get_smb_ses(server, &vol);
1530 cifs_cleanup_volume_info_contents(&vol);
1537 /* Refresh DFS cache entry from a given tcon */
1538 static int refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1543 struct cache_entry *ce;
1544 struct cifs_ses *root_ses = NULL, *ses;
1545 struct dfs_info3_param *refs = NULL;
1550 path = tcon->dfs_path + 1;
1552 rc = get_normalized_path(path, &npath);
1556 down_read(&htable_rw_lock);
1558 ce = lookup_cache_entry(npath, NULL);
1561 up_read(&htable_rw_lock);
1565 if (!cache_entry_expired(ce)) {
1566 up_read(&htable_rw_lock);
1570 up_read(&htable_rw_lock);
1572 /* If it's a DFS Link, then use root SMB session for refreshing it */
1573 if (is_dfs_link(npath)) {
1574 ses = root_ses = find_root_ses(vi, tcon, npath);
1584 rc = get_dfs_referral(xid, ses, cache_nlsc, tcon->remap, npath, &refs,
1587 dump_refs(refs, numrefs);
1588 rc = update_cache_entry(npath, refs, numrefs);
1589 free_dfs_info_array(refs, numrefs);
1593 cifs_put_smb_ses(root_ses);
1596 free_normalized_path(path, npath);
1604 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1607 static void refresh_cache_worker(struct work_struct *work)
1609 struct vol_info *vi, *nvi;
1610 struct TCP_Server_Info *server;
1613 struct cifs_tcon *tcon, *ntcon;
1617 * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1620 spin_lock(&vol_list_lock);
1621 list_for_each_entry(vi, &vol_list, list) {
1622 server = get_tcp_server(&vi->smb_vol);
1626 kref_get(&vi->refcnt);
1627 list_add_tail(&vi->rlist, &vols);
1628 put_tcp_server(server);
1630 spin_unlock(&vol_list_lock);
1632 /* Walk through all TCONs and refresh any expired cache entry */
1633 list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1634 spin_lock(&vi->smb_vol_lock);
1635 server = get_tcp_server(&vi->smb_vol);
1636 spin_unlock(&vi->smb_vol_lock);
1641 get_tcons(server, &tcons);
1644 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1646 * Skip tcp server if any of its tcons failed to refresh
1647 * (possibily due to reconnects).
1650 rc = refresh_tcon(vi, tcon);
1652 list_del_init(&tcon->ulist);
1653 cifs_put_tcon(tcon);
1656 put_tcp_server(server);
1659 list_del_init(&vi->rlist);
1660 kref_put(&vi->refcnt, vol_release);
1663 spin_lock(&cache_ttl_lock);
1664 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1665 spin_unlock(&cache_ttl_lock);