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>
14 #include <linux/uuid.h>
17 #include "smb2proto.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_unicode.h"
22 #include "dns_resolve.h"
24 #include "dfs_cache.h"
26 #define CACHE_HTABLE_SIZE 32
27 #define CACHE_MAX_ENTRIES 64
28 #define CACHE_MIN_TTL 120 /* 2 minutes */
30 #define IS_DFS_INTERLINK(v) (((v) & DFSREF_REFERRAL_SERVER) && !((v) & DFSREF_STORAGE_SERVER))
32 struct cache_dfs_tgt {
35 struct list_head list;
39 struct hlist_node hlist;
41 int hdr_flags; /* RESP_GET_DFS_REFERRAL.ReferralHeaderFlags */
42 int ttl; /* DFS_REREFERRAL_V3.TimeToLive */
43 int srvtype; /* DFS_REREFERRAL_V3.ServerType */
44 int ref_flags; /* DFS_REREFERRAL_V3.ReferralEntryFlags */
45 struct timespec64 etime;
46 int path_consumed; /* RESP_GET_DFS_REFERRAL.PathConsumed */
48 struct list_head tlist;
49 struct cache_dfs_tgt *tgthint;
52 /* List of referral server sessions per dfs mount */
54 struct list_head list;
56 struct cifs_ses *sessions[CACHE_MAX_ENTRIES];
59 struct list_head refresh_list;
63 static struct kmem_cache *cache_slab __read_mostly;
64 static struct workqueue_struct *dfscache_wq __read_mostly;
67 static DEFINE_SPINLOCK(cache_ttl_lock);
69 static struct nls_table *cache_cp;
72 * Number of entries in the cache
74 static atomic_t cache_count;
76 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
77 static DECLARE_RWSEM(htable_rw_lock);
79 static LIST_HEAD(mount_group_list);
80 static DEFINE_MUTEX(mount_group_list_lock);
82 static void refresh_cache_worker(struct work_struct *work);
84 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
86 static void get_ipc_unc(const char *ref_path, char *ipc, size_t ipclen)
91 extract_unc_hostname(ref_path, &host, &len);
92 scnprintf(ipc, ipclen, "\\\\%.*s\\IPC$", (int)len, host);
95 static struct cifs_ses *find_ipc_from_server_path(struct cifs_ses **ses, const char *path)
97 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
99 get_ipc_unc(path, unc, sizeof(unc));
100 for (; *ses; ses++) {
101 if (!strcasecmp(unc, (*ses)->tcon_ipc->tree_name))
104 return ERR_PTR(-ENOENT);
107 static void __mount_group_release(struct mount_group *mg)
111 for (i = 0; i < mg->num_sessions; i++)
112 cifs_put_smb_ses(mg->sessions[i]);
116 static void mount_group_release(struct kref *kref)
118 struct mount_group *mg = container_of(kref, struct mount_group, refcount);
120 mutex_lock(&mount_group_list_lock);
122 mutex_unlock(&mount_group_list_lock);
123 __mount_group_release(mg);
126 static struct mount_group *find_mount_group_locked(const uuid_t *id)
128 struct mount_group *mg;
130 list_for_each_entry(mg, &mount_group_list, list) {
131 if (uuid_equal(&mg->id, id))
134 return ERR_PTR(-ENOENT);
137 static struct mount_group *__get_mount_group_locked(const uuid_t *id)
139 struct mount_group *mg;
141 mg = find_mount_group_locked(id);
145 mg = kmalloc(sizeof(*mg), GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
148 kref_init(&mg->refcount);
149 uuid_copy(&mg->id, id);
150 mg->num_sessions = 0;
151 spin_lock_init(&mg->lock);
152 list_add(&mg->list, &mount_group_list);
156 static struct mount_group *get_mount_group(const uuid_t *id)
158 struct mount_group *mg;
160 mutex_lock(&mount_group_list_lock);
161 mg = __get_mount_group_locked(id);
163 kref_get(&mg->refcount);
164 mutex_unlock(&mount_group_list_lock);
169 static void free_mount_group_list(void)
171 struct mount_group *mg, *tmp_mg;
173 list_for_each_entry_safe(mg, tmp_mg, &mount_group_list, list) {
174 list_del_init(&mg->list);
175 __mount_group_release(mg);
180 * dfs_cache_canonical_path - get a canonical DFS path
184 * @remap: mapping type
186 * Return canonical path if success, otherwise error.
188 char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap)
194 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
195 return ERR_PTR(-EINVAL);
197 if (unlikely(strcmp(cp->charset, cache_cp->charset))) {
198 tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, cp, remap);
200 cifs_dbg(VFS, "%s: failed to convert path to utf16\n", __func__);
201 return ERR_PTR(-EINVAL);
204 npath = cifs_strndup_from_utf16(tmp, plen, true, cache_cp);
208 cifs_dbg(VFS, "%s: failed to convert path from utf16\n", __func__);
209 return ERR_PTR(-EINVAL);
212 npath = kstrdup(path, GFP_KERNEL);
214 return ERR_PTR(-ENOMEM);
216 convert_delimiter(npath, '\\');
220 static inline bool cache_entry_expired(const struct cache_entry *ce)
222 struct timespec64 ts;
224 ktime_get_coarse_real_ts64(&ts);
225 return timespec64_compare(&ts, &ce->etime) >= 0;
228 static inline void free_tgts(struct cache_entry *ce)
230 struct cache_dfs_tgt *t, *n;
232 list_for_each_entry_safe(t, n, &ce->tlist, list) {
239 static inline void flush_cache_ent(struct cache_entry *ce)
241 hlist_del_init(&ce->hlist);
244 atomic_dec(&cache_count);
245 kmem_cache_free(cache_slab, ce);
248 static void flush_cache_ents(void)
252 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
253 struct hlist_head *l = &cache_htable[i];
254 struct hlist_node *n;
255 struct cache_entry *ce;
257 hlist_for_each_entry_safe(ce, n, l, hlist) {
258 if (!hlist_unhashed(&ce->hlist))
265 * dfs cache /proc file
267 static int dfscache_proc_show(struct seq_file *m, void *v)
270 struct cache_entry *ce;
271 struct cache_dfs_tgt *t;
273 seq_puts(m, "DFS cache\n---------\n");
275 down_read(&htable_rw_lock);
276 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
277 struct hlist_head *l = &cache_htable[i];
279 hlist_for_each_entry(ce, l, hlist) {
280 if (hlist_unhashed(&ce->hlist))
284 "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
285 ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
286 ce->ttl, ce->etime.tv_nsec, ce->hdr_flags, ce->ref_flags,
287 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
288 ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
290 list_for_each_entry(t, &ce->tlist, list) {
291 seq_printf(m, " %s%s\n",
293 ce->tgthint == t ? " (target hint)" : "");
297 up_read(&htable_rw_lock);
302 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
303 size_t count, loff_t *ppos)
308 rc = get_user(c, buffer);
315 cifs_dbg(FYI, "clearing dfs cache\n");
317 down_write(&htable_rw_lock);
319 up_write(&htable_rw_lock);
324 static int dfscache_proc_open(struct inode *inode, struct file *file)
326 return single_open(file, dfscache_proc_show, NULL);
329 const struct proc_ops dfscache_proc_ops = {
330 .proc_open = dfscache_proc_open,
331 .proc_read = seq_read,
332 .proc_lseek = seq_lseek,
333 .proc_release = single_release,
334 .proc_write = dfscache_proc_write,
337 #ifdef CONFIG_CIFS_DEBUG2
338 static inline void dump_tgts(const struct cache_entry *ce)
340 struct cache_dfs_tgt *t;
342 cifs_dbg(FYI, "target list:\n");
343 list_for_each_entry(t, &ce->tlist, list) {
344 cifs_dbg(FYI, " %s%s\n", t->name,
345 ce->tgthint == t ? " (target hint)" : "");
349 static inline void dump_ce(const struct cache_entry *ce)
351 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
353 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
355 ce->hdr_flags, ce->ref_flags,
356 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
358 cache_entry_expired(ce) ? "yes" : "no");
362 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
366 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
367 for (i = 0; i < numrefs; i++) {
368 const struct dfs_info3_param *ref = &refs[i];
373 "path_consumed: %d\n"
374 "server_type: 0x%x\n"
379 ref->flags, ref->path_consumed, ref->server_type,
380 ref->ref_flag, ref->path_name, ref->node_name,
381 ref->ttl, ref->ttl / 60);
387 #define dump_refs(r, n)
391 * dfs_cache_init - Initialize DFS referral cache.
393 * Return zero if initialized successfully, otherwise non-zero.
395 int dfs_cache_init(void)
400 dfscache_wq = alloc_workqueue("cifs-dfscache", WQ_FREEZABLE | WQ_UNBOUND, 1);
404 cache_slab = kmem_cache_create("cifs_dfs_cache",
405 sizeof(struct cache_entry), 0,
406 SLAB_HWCACHE_ALIGN, NULL);
412 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
413 INIT_HLIST_HEAD(&cache_htable[i]);
415 atomic_set(&cache_count, 0);
416 cache_cp = load_nls("utf8");
418 cache_cp = load_nls_default();
420 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
424 destroy_workqueue(dfscache_wq);
428 static int cache_entry_hash(const void *data, int size, unsigned int *hash)
431 const unsigned char *s = data;
435 for (i = 0; i < size; i += clen) {
436 clen = cache_cp->char2uni(&s[i], size - i, &c);
437 if (unlikely(clen < 0)) {
438 cifs_dbg(VFS, "%s: can't convert char\n", __func__);
442 h = jhash(&c, sizeof(c), h);
444 *hash = h % CACHE_HTABLE_SIZE;
448 /* Return target hint of a DFS cache entry */
449 static inline char *get_tgt_name(const struct cache_entry *ce)
451 struct cache_dfs_tgt *t = ce->tgthint;
453 return t ? t->name : ERR_PTR(-ENOENT);
456 /* Return expire time out of a new entry's TTL */
457 static inline struct timespec64 get_expire_time(int ttl)
459 struct timespec64 ts = {
463 struct timespec64 now;
465 ktime_get_coarse_real_ts64(&now);
466 return timespec64_add(now, ts);
469 /* Allocate a new DFS target */
470 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
472 struct cache_dfs_tgt *t;
474 t = kmalloc(sizeof(*t), GFP_ATOMIC);
476 return ERR_PTR(-ENOMEM);
477 t->name = kstrdup(name, GFP_ATOMIC);
480 return ERR_PTR(-ENOMEM);
482 t->path_consumed = path_consumed;
483 INIT_LIST_HEAD(&t->list);
488 * Copy DFS referral information to a cache entry and conditionally update
491 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
492 struct cache_entry *ce, const char *tgthint)
496 ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
497 ce->etime = get_expire_time(ce->ttl);
498 ce->srvtype = refs[0].server_type;
499 ce->hdr_flags = refs[0].flags;
500 ce->ref_flags = refs[0].ref_flag;
501 ce->path_consumed = refs[0].path_consumed;
503 for (i = 0; i < numrefs; i++) {
504 struct cache_dfs_tgt *t;
506 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
511 if (tgthint && !strcasecmp(t->name, tgthint)) {
512 list_add(&t->list, &ce->tlist);
515 list_add_tail(&t->list, &ce->tlist);
520 ce->tgthint = list_first_entry_or_null(&ce->tlist,
521 struct cache_dfs_tgt, list);
526 /* Allocate a new cache entry */
527 static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
529 struct cache_entry *ce;
532 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
534 return ERR_PTR(-ENOMEM);
536 ce->path = refs[0].path_name;
537 refs[0].path_name = NULL;
539 INIT_HLIST_NODE(&ce->hlist);
540 INIT_LIST_HEAD(&ce->tlist);
542 rc = copy_ref_data(refs, numrefs, ce, NULL);
545 kmem_cache_free(cache_slab, ce);
551 static void remove_oldest_entry_locked(void)
554 struct cache_entry *ce;
555 struct cache_entry *to_del = NULL;
557 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
559 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
560 struct hlist_head *l = &cache_htable[i];
562 hlist_for_each_entry(ce, l, hlist) {
563 if (hlist_unhashed(&ce->hlist))
565 if (!to_del || timespec64_compare(&ce->etime,
572 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
576 cifs_dbg(FYI, "%s: removing entry\n", __func__);
578 flush_cache_ent(to_del);
581 /* Add a new DFS cache entry */
582 static int add_cache_entry_locked(struct dfs_info3_param *refs, int numrefs)
585 struct cache_entry *ce;
588 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
590 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
591 cifs_dbg(FYI, "%s: reached max cache size (%d)\n", __func__, CACHE_MAX_ENTRIES);
592 remove_oldest_entry_locked();
595 rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
599 ce = alloc_cache_entry(refs, numrefs);
603 spin_lock(&cache_ttl_lock);
606 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
608 cache_ttl = min_t(int, cache_ttl, ce->ttl);
609 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
611 spin_unlock(&cache_ttl_lock);
613 hlist_add_head(&ce->hlist, &cache_htable[hash]);
616 atomic_inc(&cache_count);
621 /* Check if two DFS paths are equal. @s1 and @s2 are expected to be in @cache_cp's charset */
622 static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
630 for (i = 0; i < len1; i += l1) {
631 l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
632 l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
633 if (unlikely(l1 < 0 && l2 < 0)) {
641 if (cifs_toupper(c1) != cifs_toupper(c2))
647 static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
649 struct cache_entry *ce;
651 hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
652 if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
657 return ERR_PTR(-ENOENT);
661 * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
663 * Use whole path components in the match. Must be called with htable_rw_lock held.
665 * Return ERR_PTR(-ENOENT) if the entry is not found.
667 static struct cache_entry *lookup_cache_entry(const char *path)
669 struct cache_entry *ce;
671 const char *s = path, *e;
676 while ((s = strchr(s, sep)) && ++cnt < 3)
680 rc = cache_entry_hash(path, strlen(path), &hash);
683 return __lookup_cache_entry(path, hash, strlen(path));
686 * Handle paths that have more than two path components and are a complete prefix of the DFS
687 * referral request path (@path).
689 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
691 e = path + strlen(path) - 1;
695 /* skip separators */
696 while (e > s && *e == sep)
702 rc = cache_entry_hash(path, len, &hash);
705 ce = __lookup_cache_entry(path, hash, len);
709 /* backward until separator */
710 while (e > s && *e != sep)
713 return ERR_PTR(-ENOENT);
717 * dfs_cache_destroy - destroy DFS referral cache
719 void dfs_cache_destroy(void)
721 cancel_delayed_work_sync(&refresh_task);
722 unload_nls(cache_cp);
723 free_mount_group_list();
725 kmem_cache_destroy(cache_slab);
726 destroy_workqueue(dfscache_wq);
728 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
731 /* Update a cache entry with the new referral in @refs */
732 static int update_cache_entry_locked(struct cache_entry *ce, const struct dfs_info3_param *refs,
738 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
741 s = ce->tgthint->name;
742 th = kstrdup(s, GFP_ATOMIC);
750 rc = copy_ref_data(refs, numrefs, ce, th);
757 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const char *path,
758 struct dfs_info3_param **refs, int *numrefs)
763 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
768 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
770 if (unlikely(!cache_cp))
773 rc = ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs, cache_cp,
776 struct dfs_info3_param *ref = *refs;
778 for (i = 0; i < *numrefs; i++)
779 convert_delimiter(ref[i].path_name, '\\');
785 * Find, create or update a DFS cache entry.
787 * If the entry wasn't found, it will create a new one. Or if it was found but
788 * expired, then it will update the entry accordingly.
790 * For interlinks, cifs_mount() and expand_dfs_referral() are supposed to
791 * handle them properly.
793 static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, const char *path)
795 struct dfs_info3_param *refs = NULL;
796 struct cache_entry *ce;
800 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
802 down_read(&htable_rw_lock);
804 ce = lookup_cache_entry(path);
805 if (!IS_ERR(ce) && !cache_entry_expired(ce)) {
806 up_read(&htable_rw_lock);
810 * Unlock shared access as we don't want to hold any locks while getting
811 * a new referral. The @ses used for performing the I/O could be
812 * reconnecting and it acquires @htable_rw_lock to look up the dfs cache
813 * in order to failover -- if necessary.
815 up_read(&htable_rw_lock);
818 * Either the entry was not found, or it is expired.
819 * Request a new DFS referral in order to create or update a cache entry.
821 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
825 dump_refs(refs, numrefs);
827 down_write(&htable_rw_lock);
828 /* Re-check as another task might have it added or refreshed already */
829 ce = lookup_cache_entry(path);
831 if (cache_entry_expired(ce))
832 rc = update_cache_entry_locked(ce, refs, numrefs);
834 rc = add_cache_entry_locked(refs, numrefs);
837 up_write(&htable_rw_lock);
839 free_dfs_info_array(refs, numrefs);
844 * Set up a DFS referral from a given cache entry.
846 * Must be called with htable_rw_lock held.
848 static int setup_referral(const char *path, struct cache_entry *ce,
849 struct dfs_info3_param *ref, const char *target)
853 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
855 memset(ref, 0, sizeof(*ref));
857 ref->path_name = kstrdup(path, GFP_ATOMIC);
861 ref->node_name = kstrdup(target, GFP_ATOMIC);
862 if (!ref->node_name) {
867 ref->path_consumed = ce->path_consumed;
869 ref->server_type = ce->srvtype;
870 ref->ref_flag = ce->ref_flags;
871 ref->flags = ce->hdr_flags;
876 kfree(ref->path_name);
877 ref->path_name = NULL;
881 /* Return target list of a DFS cache entry */
882 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
885 struct list_head *head = &tl->tl_list;
886 struct cache_dfs_tgt *t;
887 struct dfs_cache_tgt_iterator *it, *nit;
889 memset(tl, 0, sizeof(*tl));
890 INIT_LIST_HEAD(head);
892 list_for_each_entry(t, &ce->tlist, list) {
893 it = kzalloc(sizeof(*it), GFP_ATOMIC);
899 it->it_name = kstrdup(t->name, GFP_ATOMIC);
905 it->it_path_consumed = t->path_consumed;
907 if (ce->tgthint == t)
908 list_add(&it->it_list, head);
910 list_add_tail(&it->it_list, head);
913 tl->tl_numtgts = ce->numtgts;
918 list_for_each_entry_safe(it, nit, head, it_list) {
919 list_del(&it->it_list);
927 * dfs_cache_find - find a DFS cache entry
929 * If it doesn't find the cache entry, then it will get a DFS referral
930 * for @path and create a new entry.
932 * In case the cache entry exists but expired, it will get a DFS referral
933 * for @path and then update the respective cache entry.
935 * These parameters are passed down to the get_dfs_refer() call if it
936 * needs to be issued:
938 * @ses: smb session to issue the request on
940 * @remap: path character remapping type
941 * @path: path to lookup in DFS referral cache.
943 * @ref: when non-NULL, store single DFS referral result in it.
944 * @tgt_list: when non-NULL, store complete DFS target list in it.
946 * Return zero if the target was found, otherwise non-zero.
948 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
949 int remap, const char *path, struct dfs_info3_param *ref,
950 struct dfs_cache_tgt_list *tgt_list)
954 struct cache_entry *ce;
956 npath = dfs_cache_canonical_path(path, cp, remap);
958 return PTR_ERR(npath);
960 rc = cache_refresh_path(xid, ses, npath);
964 down_read(&htable_rw_lock);
966 ce = lookup_cache_entry(npath);
968 up_read(&htable_rw_lock);
974 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
978 rc = get_targets(ce, tgt_list);
980 up_read(&htable_rw_lock);
988 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
989 * the currently connected server.
991 * NOTE: This function will neither update a cache entry in case it was
992 * expired, nor create a new cache entry if @path hasn't been found. It heavily
993 * relies on an existing cache entry.
995 * @path: canonical DFS path to lookup in the DFS referral cache.
996 * @ref: when non-NULL, store single DFS referral result in it.
997 * @tgt_list: when non-NULL, store complete DFS target list in it.
999 * Return 0 if successful.
1000 * Return -ENOENT if the entry was not found.
1001 * Return non-zero for other errors.
1003 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
1004 struct dfs_cache_tgt_list *tgt_list)
1007 struct cache_entry *ce;
1009 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1011 down_read(&htable_rw_lock);
1013 ce = lookup_cache_entry(path);
1020 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
1023 if (!rc && tgt_list)
1024 rc = get_targets(ce, tgt_list);
1027 up_read(&htable_rw_lock);
1032 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
1034 * If it doesn't find the cache entry, then it will get a DFS referral for @path
1035 * and create a new entry.
1037 * In case the cache entry exists but expired, it will get a DFS referral
1038 * for @path and then update the respective cache entry.
1043 * @remap: type of character remapping for paths
1044 * @path: path to lookup in DFS referral cache
1045 * @it: DFS target iterator
1047 * Return zero if the target hint was updated successfully, otherwise non-zero.
1049 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
1050 const struct nls_table *cp, int remap, const char *path,
1051 const struct dfs_cache_tgt_iterator *it)
1053 struct cache_dfs_tgt *t;
1054 struct cache_entry *ce;
1058 npath = dfs_cache_canonical_path(path, cp, remap);
1060 return PTR_ERR(npath);
1062 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
1064 rc = cache_refresh_path(xid, ses, npath);
1068 down_write(&htable_rw_lock);
1070 ce = lookup_cache_entry(npath);
1078 if (likely(!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);
1098 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1099 * without sending any requests to the currently connected server.
1101 * NOTE: This function will neither update a cache entry in case it was
1102 * expired, nor create a new cache entry if @path hasn't been found. It heavily
1103 * relies on an existing cache entry.
1105 * @path: canonical DFS path to lookup in DFS referral cache.
1106 * @it: target iterator which contains the target hint to update the cache
1109 * Return zero if the target hint was updated successfully, otherwise non-zero.
1111 int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it)
1114 struct cache_entry *ce;
1115 struct cache_dfs_tgt *t;
1120 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1122 down_write(&htable_rw_lock);
1124 ce = lookup_cache_entry(path);
1133 if (unlikely(!strcasecmp(it->it_name, t->name)))
1136 list_for_each_entry(t, &ce->tlist, list) {
1137 if (!strcasecmp(t->name, it->it_name)) {
1139 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1146 up_write(&htable_rw_lock);
1151 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1152 * target iterator (@it).
1154 * @path: canonical DFS path to lookup in DFS referral cache.
1155 * @it: DFS target iterator.
1156 * @ref: DFS referral pointer to set up the gathered information.
1158 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1160 int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
1161 struct dfs_info3_param *ref)
1164 struct cache_entry *ce;
1169 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1171 down_read(&htable_rw_lock);
1173 ce = lookup_cache_entry(path);
1179 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1181 rc = setup_referral(path, ce, ref, it->it_name);
1184 up_read(&htable_rw_lock);
1189 * dfs_cache_add_refsrv_session - add SMB session of referral server
1191 * @mount_id: mount group uuid to lookup.
1192 * @ses: reference counted SMB session of referral server.
1194 void dfs_cache_add_refsrv_session(const uuid_t *mount_id, struct cifs_ses *ses)
1196 struct mount_group *mg;
1198 if (WARN_ON_ONCE(!mount_id || uuid_is_null(mount_id) || !ses))
1201 mg = get_mount_group(mount_id);
1202 if (WARN_ON_ONCE(IS_ERR(mg)))
1205 spin_lock(&mg->lock);
1206 if (mg->num_sessions < ARRAY_SIZE(mg->sessions))
1207 mg->sessions[mg->num_sessions++] = ses;
1208 spin_unlock(&mg->lock);
1209 kref_put(&mg->refcount, mount_group_release);
1213 * dfs_cache_put_refsrv_sessions - put all referral server sessions
1215 * Put all SMB sessions from the given mount group id.
1217 * @mount_id: mount group uuid to lookup.
1219 void dfs_cache_put_refsrv_sessions(const uuid_t *mount_id)
1221 struct mount_group *mg;
1223 if (!mount_id || uuid_is_null(mount_id))
1226 mutex_lock(&mount_group_list_lock);
1227 mg = find_mount_group_locked(mount_id);
1229 mutex_unlock(&mount_group_list_lock);
1232 mutex_unlock(&mount_group_list_lock);
1233 kref_put(&mg->refcount, mount_group_release);
1236 /* Extract share from DFS target and return a pointer to prefix path or NULL */
1237 static const char *parse_target_share(const char *target, char **share)
1239 const char *s, *seps = "/\\";
1242 s = strpbrk(target + 1, seps);
1244 return ERR_PTR(-EINVAL);
1246 len = strcspn(s + 1, seps);
1248 return ERR_PTR(-EINVAL);
1251 len = s - target + 1;
1252 *share = kstrndup(target, len, GFP_KERNEL);
1254 return ERR_PTR(-ENOMEM);
1257 return s + strspn(s, seps);
1261 * dfs_cache_get_tgt_share - parse a DFS target
1263 * @path: DFS full path
1264 * @it: DFS target iterator.
1265 * @share: tree name.
1266 * @prefix: prefix path.
1268 * Return zero if target was parsed correctly, otherwise non-zero.
1270 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
1276 const char *target_ppath, *dfsref_ppath;
1277 size_t target_pplen, dfsref_pplen;
1280 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1283 sep = it->it_name[0];
1284 if (sep != '\\' && sep != '/')
1287 target_ppath = parse_target_share(it->it_name, &target_share);
1288 if (IS_ERR(target_ppath))
1289 return PTR_ERR(target_ppath);
1291 /* point to prefix in DFS referral path */
1292 dfsref_ppath = path + it->it_path_consumed;
1293 dfsref_ppath += strspn(dfsref_ppath, "/\\");
1295 target_pplen = strlen(target_ppath);
1296 dfsref_pplen = strlen(dfsref_ppath);
1298 /* merge prefix paths from DFS referral path and target node */
1299 if (target_pplen || dfsref_pplen) {
1300 len = target_pplen + dfsref_pplen + 2;
1301 ppath = kzalloc(len, GFP_KERNEL);
1303 kfree(target_share);
1306 c = strscpy(ppath, target_ppath, len);
1307 if (c && dfsref_pplen)
1309 strlcat(ppath, dfsref_ppath, len);
1311 *share = target_share;
1316 static bool target_share_equal(struct TCP_Server_Info *server, const char *s1, const char *s2)
1318 char unc[sizeof("\\\\") + SERVER_NAME_LENGTH] = {0};
1326 if (strcasecmp(s1, s2))
1330 * Resolve share's hostname and check if server address matches. Otherwise just ignore it
1331 * as we could not have upcall to resolve hostname or failed to convert ip address.
1334 extract_unc_hostname(s1, &host, &hostlen);
1335 scnprintf(unc, sizeof(unc), "\\\\%.*s", (int)hostlen, host);
1337 rc = dns_resolve_server_name_to_ip(unc, &ip, NULL);
1339 cifs_dbg(FYI, "%s: could not resolve %.*s. assuming server address matches.\n",
1340 __func__, (int)hostlen, host);
1344 if (!cifs_convert_address(&sa, ip, strlen(ip))) {
1345 cifs_dbg(VFS, "%s: failed to convert address \'%s\'. skip address matching.\n",
1348 cifs_server_lock(server);
1349 match = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr, &sa);
1350 cifs_server_unlock(server);
1358 * Mark dfs tcon for reconnecting when the currently connected tcon does not match any of the new
1359 * target shares in @refs.
1361 static void mark_for_reconnect_if_needed(struct cifs_tcon *tcon, struct dfs_cache_tgt_list *tl,
1362 const struct dfs_info3_param *refs, int numrefs)
1364 struct dfs_cache_tgt_iterator *it;
1367 for (it = dfs_cache_get_tgt_iterator(tl); it; it = dfs_cache_get_next_tgt(tl, it)) {
1368 for (i = 0; i < numrefs; i++) {
1369 if (target_share_equal(tcon->ses->server, dfs_cache_get_tgt_name(it),
1375 cifs_dbg(FYI, "%s: no cached or matched targets. mark dfs share for reconnect.\n", __func__);
1376 cifs_signal_cifsd_for_reconnect(tcon->ses->server, true);
1379 /* Refresh dfs referral of tcon and mark it for reconnect if needed */
1380 static int __refresh_tcon(const char *path, struct cifs_ses **sessions, struct cifs_tcon *tcon,
1383 struct cifs_ses *ses;
1384 struct cache_entry *ce;
1385 struct dfs_info3_param *refs = NULL;
1387 bool needs_refresh = false;
1388 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
1392 ses = find_ipc_from_server_path(sessions, path);
1394 cifs_dbg(FYI, "%s: could not find ipc session\n", __func__);
1395 return PTR_ERR(ses);
1398 down_read(&htable_rw_lock);
1399 ce = lookup_cache_entry(path);
1400 needs_refresh = force_refresh || IS_ERR(ce) || cache_entry_expired(ce);
1402 rc = get_targets(ce, &tl);
1404 cifs_dbg(FYI, "%s: could not get dfs targets: %d\n", __func__, rc);
1406 up_read(&htable_rw_lock);
1408 if (!needs_refresh) {
1414 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1417 /* Create or update a cache entry with the new referral */
1419 dump_refs(refs, numrefs);
1421 down_write(&htable_rw_lock);
1422 ce = lookup_cache_entry(path);
1424 add_cache_entry_locked(refs, numrefs);
1425 else if (force_refresh || cache_entry_expired(ce))
1426 update_cache_entry_locked(ce, refs, numrefs);
1427 up_write(&htable_rw_lock);
1429 mark_for_reconnect_if_needed(tcon, &tl, refs, numrefs);
1433 dfs_cache_free_tgts(&tl);
1434 free_dfs_info_array(refs, numrefs);
1438 static int refresh_tcon(struct cifs_ses **sessions, struct cifs_tcon *tcon, bool force_refresh)
1440 struct TCP_Server_Info *server = tcon->ses->server;
1442 mutex_lock(&server->refpath_lock);
1443 if (server->origin_fullpath) {
1444 if (server->leaf_fullpath && strcasecmp(server->leaf_fullpath,
1445 server->origin_fullpath))
1446 __refresh_tcon(server->leaf_fullpath + 1, sessions, tcon, force_refresh);
1447 __refresh_tcon(server->origin_fullpath + 1, sessions, tcon, force_refresh);
1449 mutex_unlock(&server->refpath_lock);
1455 * dfs_cache_remount_fs - remount a DFS share
1457 * Reconfigure dfs mount by forcing a new DFS referral and if the currently cached targets do not
1458 * match any of the new targets, mark it for reconnect.
1460 * @cifs_sb: cifs superblock.
1462 * Return zero if remounted, otherwise non-zero.
1464 int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb)
1466 struct cifs_tcon *tcon;
1467 struct TCP_Server_Info *server;
1468 struct mount_group *mg;
1469 struct cifs_ses *sessions[CACHE_MAX_ENTRIES + 1] = {NULL};
1472 if (!cifs_sb || !cifs_sb->master_tlink)
1475 tcon = cifs_sb_master_tcon(cifs_sb);
1476 server = tcon->ses->server;
1478 if (!server->origin_fullpath) {
1479 cifs_dbg(FYI, "%s: not a dfs mount\n", __func__);
1483 if (uuid_is_null(&cifs_sb->dfs_mount_id)) {
1484 cifs_dbg(FYI, "%s: no dfs mount group id\n", __func__);
1488 mutex_lock(&mount_group_list_lock);
1489 mg = find_mount_group_locked(&cifs_sb->dfs_mount_id);
1491 mutex_unlock(&mount_group_list_lock);
1492 cifs_dbg(FYI, "%s: no ipc session for refreshing referral\n", __func__);
1495 kref_get(&mg->refcount);
1496 mutex_unlock(&mount_group_list_lock);
1498 spin_lock(&mg->lock);
1499 memcpy(&sessions, mg->sessions, mg->num_sessions * sizeof(mg->sessions[0]));
1500 spin_unlock(&mg->lock);
1503 * After reconnecting to a different server, unique ids won't match anymore, so we disable
1504 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
1506 cifs_autodisable_serverino(cifs_sb);
1508 * Force the use of prefix path to support failover on DFS paths that resolve to targets
1509 * that have different prefix paths.
1511 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1512 rc = refresh_tcon(sessions, tcon, true);
1514 kref_put(&mg->refcount, mount_group_release);
1519 * Refresh all active dfs mounts regardless of whether they are in cache or not.
1520 * (cache can be cleared)
1522 static void refresh_mounts(struct cifs_ses **sessions)
1524 struct TCP_Server_Info *server;
1525 struct cifs_ses *ses;
1526 struct cifs_tcon *tcon, *ntcon;
1527 struct list_head tcons;
1529 INIT_LIST_HEAD(&tcons);
1531 spin_lock(&cifs_tcp_ses_lock);
1532 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1533 spin_lock(&server->srv_lock);
1534 if (!server->is_dfs_conn) {
1535 spin_unlock(&server->srv_lock);
1538 spin_unlock(&server->srv_lock);
1540 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1541 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1542 spin_lock(&tcon->tc_lock);
1543 if (!tcon->ipc && !tcon->need_reconnect) {
1545 list_add_tail(&tcon->ulist, &tcons);
1547 spin_unlock(&tcon->tc_lock);
1551 spin_unlock(&cifs_tcp_ses_lock);
1553 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1554 struct TCP_Server_Info *server = tcon->ses->server;
1556 list_del_init(&tcon->ulist);
1558 mutex_lock(&server->refpath_lock);
1559 if (server->origin_fullpath) {
1560 if (server->leaf_fullpath && strcasecmp(server->leaf_fullpath,
1561 server->origin_fullpath))
1562 __refresh_tcon(server->leaf_fullpath + 1, sessions, tcon, false);
1563 __refresh_tcon(server->origin_fullpath + 1, sessions, tcon, false);
1565 mutex_unlock(&server->refpath_lock);
1567 cifs_put_tcon(tcon);
1571 static void refresh_cache(struct cifs_ses **sessions)
1574 struct cifs_ses *ses;
1576 char *ref_paths[CACHE_MAX_ENTRIES];
1578 struct cache_entry *ce;
1581 * Refresh all cached entries. Get all new referrals outside critical section to avoid
1582 * starvation while performing SMB2 IOCTL on broken or slow connections.
1584 * The cache entries may cover more paths than the active mounts
1585 * (e.g. domain-based DFS referrals or multi tier DFS setups).
1587 down_read(&htable_rw_lock);
1588 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
1589 struct hlist_head *l = &cache_htable[i];
1591 hlist_for_each_entry(ce, l, hlist) {
1592 if (count == ARRAY_SIZE(ref_paths))
1594 if (hlist_unhashed(&ce->hlist) || !cache_entry_expired(ce) ||
1595 IS_ERR(find_ipc_from_server_path(sessions, ce->path)))
1597 ref_paths[count++] = kstrdup(ce->path, GFP_ATOMIC);
1602 up_read(&htable_rw_lock);
1604 for (i = 0; i < count; i++) {
1605 char *path = ref_paths[i];
1606 struct dfs_info3_param *refs = NULL;
1613 ses = find_ipc_from_server_path(sessions, path);
1618 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1622 down_write(&htable_rw_lock);
1623 ce = lookup_cache_entry(path);
1625 * We need to re-check it because other tasks might have it deleted or
1628 if (!IS_ERR(ce) && cache_entry_expired(ce))
1629 update_cache_entry_locked(ce, refs, numrefs);
1630 up_write(&htable_rw_lock);
1635 free_dfs_info_array(refs, numrefs);
1640 * Worker that will refresh DFS cache and active mounts based on lowest TTL value from a DFS
1643 static void refresh_cache_worker(struct work_struct *work)
1645 struct list_head mglist;
1646 struct mount_group *mg, *tmp_mg;
1647 struct cifs_ses *sessions[CACHE_MAX_ENTRIES + 1] = {NULL};
1648 int max_sessions = ARRAY_SIZE(sessions) - 1;
1651 INIT_LIST_HEAD(&mglist);
1653 /* Get refereces of mount groups */
1654 mutex_lock(&mount_group_list_lock);
1655 list_for_each_entry(mg, &mount_group_list, list) {
1656 kref_get(&mg->refcount);
1657 list_add(&mg->refresh_list, &mglist);
1659 mutex_unlock(&mount_group_list_lock);
1661 /* Fill in local array with an NULL-terminated list of all referral server sessions */
1662 list_for_each_entry(mg, &mglist, refresh_list) {
1663 if (i >= max_sessions)
1666 spin_lock(&mg->lock);
1667 if (i + mg->num_sessions > max_sessions)
1668 count = max_sessions - i;
1670 count = mg->num_sessions;
1671 memcpy(&sessions[i], mg->sessions, count * sizeof(mg->sessions[0]));
1672 spin_unlock(&mg->lock);
1677 /* Refresh all active mounts and cached entries */
1678 refresh_mounts(sessions);
1679 refresh_cache(sessions);
1682 list_for_each_entry_safe(mg, tmp_mg, &mglist, refresh_list) {
1683 list_del_init(&mg->refresh_list);
1684 kref_put(&mg->refcount, mount_group_release);
1687 spin_lock(&cache_ttl_lock);
1688 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1689 spin_unlock(&cache_ttl_lock);