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"
25 #include "dfs_cache.h"
27 #define CACHE_HTABLE_SIZE 32
28 #define CACHE_MAX_ENTRIES 64
29 #define CACHE_MIN_TTL 120 /* 2 minutes */
30 #define CACHE_DEFAULT_TTL 300 /* 5 minutes */
32 #define IS_DFS_INTERLINK(v) (((v) & DFSREF_REFERRAL_SERVER) && !((v) & DFSREF_STORAGE_SERVER))
34 struct cache_dfs_tgt {
37 struct list_head list;
41 struct hlist_node hlist;
43 int hdr_flags; /* RESP_GET_DFS_REFERRAL.ReferralHeaderFlags */
44 int ttl; /* DFS_REREFERRAL_V3.TimeToLive */
45 int srvtype; /* DFS_REREFERRAL_V3.ServerType */
46 int ref_flags; /* DFS_REREFERRAL_V3.ReferralEntryFlags */
47 struct timespec64 etime;
48 int path_consumed; /* RESP_GET_DFS_REFERRAL.PathConsumed */
50 struct list_head tlist;
51 struct cache_dfs_tgt *tgthint;
54 static struct kmem_cache *cache_slab __read_mostly;
55 struct workqueue_struct *dfscache_wq;
57 atomic_t dfs_cache_ttl;
59 static struct nls_table *cache_cp;
62 * Number of entries in the cache
64 static atomic_t cache_count;
66 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
67 static DECLARE_RWSEM(htable_rw_lock);
70 * dfs_cache_canonical_path - get a canonical DFS path
74 * @remap: mapping type
76 * Return canonical path if success, otherwise error.
78 char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap)
84 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
85 return ERR_PTR(-EINVAL);
87 if (unlikely(strcmp(cp->charset, cache_cp->charset))) {
88 tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, cp, remap);
90 cifs_dbg(VFS, "%s: failed to convert path to utf16\n", __func__);
91 return ERR_PTR(-EINVAL);
94 npath = cifs_strndup_from_utf16(tmp, plen, true, cache_cp);
98 cifs_dbg(VFS, "%s: failed to convert path from utf16\n", __func__);
99 return ERR_PTR(-EINVAL);
102 npath = kstrdup(path, GFP_KERNEL);
104 return ERR_PTR(-ENOMEM);
106 convert_delimiter(npath, '\\');
110 static inline bool cache_entry_expired(const struct cache_entry *ce)
112 struct timespec64 ts;
114 ktime_get_coarse_real_ts64(&ts);
115 return timespec64_compare(&ts, &ce->etime) >= 0;
118 static inline void free_tgts(struct cache_entry *ce)
120 struct cache_dfs_tgt *t, *n;
122 list_for_each_entry_safe(t, n, &ce->tlist, list) {
129 static inline void flush_cache_ent(struct cache_entry *ce)
131 hlist_del_init(&ce->hlist);
134 atomic_dec(&cache_count);
135 kmem_cache_free(cache_slab, ce);
138 static void flush_cache_ents(void)
142 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
143 struct hlist_head *l = &cache_htable[i];
144 struct hlist_node *n;
145 struct cache_entry *ce;
147 hlist_for_each_entry_safe(ce, n, l, hlist) {
148 if (!hlist_unhashed(&ce->hlist))
155 * dfs cache /proc file
157 static int dfscache_proc_show(struct seq_file *m, void *v)
160 struct cache_entry *ce;
161 struct cache_dfs_tgt *t;
163 seq_puts(m, "DFS cache\n---------\n");
165 down_read(&htable_rw_lock);
166 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
167 struct hlist_head *l = &cache_htable[i];
169 hlist_for_each_entry(ce, l, hlist) {
170 if (hlist_unhashed(&ce->hlist))
174 "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",
175 ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
176 ce->ttl, ce->etime.tv_nsec, ce->hdr_flags, ce->ref_flags,
177 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
178 ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
180 list_for_each_entry(t, &ce->tlist, list) {
181 seq_printf(m, " %s%s\n",
183 READ_ONCE(ce->tgthint) == t ? " (target hint)" : "");
187 up_read(&htable_rw_lock);
192 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
193 size_t count, loff_t *ppos)
198 rc = get_user(c, buffer);
205 cifs_dbg(FYI, "clearing dfs cache\n");
207 down_write(&htable_rw_lock);
209 up_write(&htable_rw_lock);
214 static int dfscache_proc_open(struct inode *inode, struct file *file)
216 return single_open(file, dfscache_proc_show, NULL);
219 const struct proc_ops dfscache_proc_ops = {
220 .proc_open = dfscache_proc_open,
221 .proc_read = seq_read,
222 .proc_lseek = seq_lseek,
223 .proc_release = single_release,
224 .proc_write = dfscache_proc_write,
227 #ifdef CONFIG_CIFS_DEBUG2
228 static inline void dump_tgts(const struct cache_entry *ce)
230 struct cache_dfs_tgt *t;
232 cifs_dbg(FYI, "target list:\n");
233 list_for_each_entry(t, &ce->tlist, list) {
234 cifs_dbg(FYI, " %s%s\n", t->name,
235 READ_ONCE(ce->tgthint) == t ? " (target hint)" : "");
239 static inline void dump_ce(const struct cache_entry *ce)
241 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
243 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
245 ce->hdr_flags, ce->ref_flags,
246 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
248 cache_entry_expired(ce) ? "yes" : "no");
252 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
256 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
257 for (i = 0; i < numrefs; i++) {
258 const struct dfs_info3_param *ref = &refs[i];
263 "path_consumed: %d\n"
264 "server_type: 0x%x\n"
269 ref->flags, ref->path_consumed, ref->server_type,
270 ref->ref_flag, ref->path_name, ref->node_name,
271 ref->ttl, ref->ttl / 60);
277 #define dump_refs(r, n)
281 * dfs_cache_init - Initialize DFS referral cache.
283 * Return zero if initialized successfully, otherwise non-zero.
285 int dfs_cache_init(void)
290 dfscache_wq = alloc_workqueue("cifs-dfscache",
291 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM,
296 cache_slab = kmem_cache_create("cifs_dfs_cache",
297 sizeof(struct cache_entry), 0,
298 SLAB_HWCACHE_ALIGN, NULL);
304 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
305 INIT_HLIST_HEAD(&cache_htable[i]);
307 atomic_set(&cache_count, 0);
308 atomic_set(&dfs_cache_ttl, CACHE_DEFAULT_TTL);
309 cache_cp = load_nls("utf8");
311 cache_cp = load_nls_default();
313 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
317 destroy_workqueue(dfscache_wq);
321 static int cache_entry_hash(const void *data, int size, unsigned int *hash)
324 const unsigned char *s = data;
328 for (i = 0; i < size; i += clen) {
329 clen = cache_cp->char2uni(&s[i], size - i, &c);
330 if (unlikely(clen < 0)) {
331 cifs_dbg(VFS, "%s: can't convert char\n", __func__);
335 h = jhash(&c, sizeof(c), h);
337 *hash = h % CACHE_HTABLE_SIZE;
341 /* Return target hint of a DFS cache entry */
342 static inline char *get_tgt_name(const struct cache_entry *ce)
344 struct cache_dfs_tgt *t = READ_ONCE(ce->tgthint);
346 return t ? t->name : ERR_PTR(-ENOENT);
349 /* Return expire time out of a new entry's TTL */
350 static inline struct timespec64 get_expire_time(int ttl)
352 struct timespec64 ts = {
356 struct timespec64 now;
358 ktime_get_coarse_real_ts64(&now);
359 return timespec64_add(now, ts);
362 /* Allocate a new DFS target */
363 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
365 struct cache_dfs_tgt *t;
367 t = kmalloc(sizeof(*t), GFP_ATOMIC);
369 return ERR_PTR(-ENOMEM);
370 t->name = kstrdup(name, GFP_ATOMIC);
373 return ERR_PTR(-ENOMEM);
375 t->path_consumed = path_consumed;
376 INIT_LIST_HEAD(&t->list);
381 * Copy DFS referral information to a cache entry and conditionally update
384 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
385 struct cache_entry *ce, const char *tgthint)
387 struct cache_dfs_tgt *target;
390 ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
391 ce->etime = get_expire_time(ce->ttl);
392 ce->srvtype = refs[0].server_type;
393 ce->hdr_flags = refs[0].flags;
394 ce->ref_flags = refs[0].ref_flag;
395 ce->path_consumed = refs[0].path_consumed;
397 for (i = 0; i < numrefs; i++) {
398 struct cache_dfs_tgt *t;
400 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
405 if (tgthint && !strcasecmp(t->name, tgthint)) {
406 list_add(&t->list, &ce->tlist);
409 list_add_tail(&t->list, &ce->tlist);
414 target = list_first_entry_or_null(&ce->tlist, struct cache_dfs_tgt,
416 WRITE_ONCE(ce->tgthint, target);
421 /* Allocate a new cache entry */
422 static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
424 struct cache_entry *ce;
427 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
429 return ERR_PTR(-ENOMEM);
431 ce->path = refs[0].path_name;
432 refs[0].path_name = NULL;
434 INIT_HLIST_NODE(&ce->hlist);
435 INIT_LIST_HEAD(&ce->tlist);
437 rc = copy_ref_data(refs, numrefs, ce, NULL);
440 kmem_cache_free(cache_slab, ce);
446 static void remove_oldest_entry_locked(void)
449 struct cache_entry *ce;
450 struct cache_entry *to_del = NULL;
452 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
454 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
455 struct hlist_head *l = &cache_htable[i];
457 hlist_for_each_entry(ce, l, hlist) {
458 if (hlist_unhashed(&ce->hlist))
460 if (!to_del || timespec64_compare(&ce->etime,
467 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
471 cifs_dbg(FYI, "%s: removing entry\n", __func__);
473 flush_cache_ent(to_del);
476 /* Add a new DFS cache entry */
477 static struct cache_entry *add_cache_entry_locked(struct dfs_info3_param *refs,
481 struct cache_entry *ce;
485 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
487 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
488 cifs_dbg(FYI, "%s: reached max cache size (%d)\n", __func__, CACHE_MAX_ENTRIES);
489 remove_oldest_entry_locked();
492 rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
496 ce = alloc_cache_entry(refs, numrefs);
500 ttl = min_t(int, atomic_read(&dfs_cache_ttl), ce->ttl);
501 atomic_set(&dfs_cache_ttl, ttl);
503 hlist_add_head(&ce->hlist, &cache_htable[hash]);
506 atomic_inc(&cache_count);
511 /* Check if two DFS paths are equal. @s1 and @s2 are expected to be in @cache_cp's charset */
512 static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
520 for (i = 0; i < len1; i += l1) {
521 l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
522 l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
523 if (unlikely(l1 < 0 && l2 < 0)) {
531 if (cifs_toupper(c1) != cifs_toupper(c2))
537 static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
539 struct cache_entry *ce;
541 hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
542 if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
547 return ERR_PTR(-ENOENT);
551 * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
553 * Use whole path components in the match. Must be called with htable_rw_lock held.
555 * Return cached entry if successful.
556 * Return ERR_PTR(-ENOENT) if the entry is not found.
557 * Return error ptr otherwise.
559 static struct cache_entry *lookup_cache_entry(const char *path)
561 struct cache_entry *ce;
563 const char *s = path, *e;
568 while ((s = strchr(s, sep)) && ++cnt < 3)
572 rc = cache_entry_hash(path, strlen(path), &hash);
575 return __lookup_cache_entry(path, hash, strlen(path));
578 * Handle paths that have more than two path components and are a complete prefix of the DFS
579 * referral request path (@path).
581 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
583 e = path + strlen(path) - 1;
587 /* skip separators */
588 while (e > s && *e == sep)
594 rc = cache_entry_hash(path, len, &hash);
597 ce = __lookup_cache_entry(path, hash, len);
601 /* backward until separator */
602 while (e > s && *e != sep)
605 return ERR_PTR(-ENOENT);
609 * dfs_cache_destroy - destroy DFS referral cache
611 void dfs_cache_destroy(void)
613 unload_nls(cache_cp);
615 kmem_cache_destroy(cache_slab);
616 destroy_workqueue(dfscache_wq);
618 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
621 /* Update a cache entry with the new referral in @refs */
622 static int update_cache_entry_locked(struct cache_entry *ce, const struct dfs_info3_param *refs,
625 struct cache_dfs_tgt *target;
629 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
631 target = READ_ONCE(ce->tgthint);
633 th = kstrdup(target->name, GFP_ATOMIC);
641 rc = copy_ref_data(refs, numrefs, ce, th);
648 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const char *path,
649 struct dfs_info3_param **refs, int *numrefs)
657 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
659 if (unlikely(!cache_cp))
662 cifs_dbg(FYI, "%s: ipc=%s referral=%s\n", __func__, ses->tcon_ipc->tree_name, path);
663 rc = ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs, cache_cp,
666 struct dfs_info3_param *ref = *refs;
668 for (i = 0; i < *numrefs; i++)
669 convert_delimiter(ref[i].path_name, '\\');
675 * Find, create or update a DFS cache entry.
677 * If the entry wasn't found, it will create a new one. Or if it was found but
678 * expired, then it will update the entry accordingly.
680 * For interlinks, cifs_mount() and expand_dfs_referral() are supposed to
681 * handle them properly.
683 * On success, return entry with acquired lock for reading, otherwise error ptr.
685 static struct cache_entry *cache_refresh_path(const unsigned int xid,
686 struct cifs_ses *ses,
690 struct dfs_info3_param *refs = NULL;
691 struct cache_entry *ce;
695 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
697 down_read(&htable_rw_lock);
699 ce = lookup_cache_entry(path);
701 if (!force_refresh && !cache_entry_expired(ce))
703 } else if (PTR_ERR(ce) != -ENOENT) {
704 up_read(&htable_rw_lock);
709 * Unlock shared access as we don't want to hold any locks while getting
710 * a new referral. The @ses used for performing the I/O could be
711 * reconnecting and it acquires @htable_rw_lock to look up the dfs cache
712 * in order to failover -- if necessary.
714 up_read(&htable_rw_lock);
717 * Either the entry was not found, or it is expired, or it is a forced
719 * Request a new DFS referral in order to create or update a cache entry.
721 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
727 dump_refs(refs, numrefs);
729 down_write(&htable_rw_lock);
730 /* Re-check as another task might have it added or refreshed already */
731 ce = lookup_cache_entry(path);
733 if (force_refresh || cache_entry_expired(ce)) {
734 rc = update_cache_entry_locked(ce, refs, numrefs);
738 } else if (PTR_ERR(ce) == -ENOENT) {
739 ce = add_cache_entry_locked(refs, numrefs);
743 up_write(&htable_rw_lock);
747 downgrade_write(&htable_rw_lock);
749 free_dfs_info_array(refs, numrefs);
754 * Set up a DFS referral from a given cache entry.
756 * Must be called with htable_rw_lock held.
758 static int setup_referral(const char *path, struct cache_entry *ce,
759 struct dfs_info3_param *ref, const char *target)
763 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
765 memset(ref, 0, sizeof(*ref));
767 ref->path_name = kstrdup(path, GFP_ATOMIC);
771 ref->node_name = kstrdup(target, GFP_ATOMIC);
772 if (!ref->node_name) {
777 ref->path_consumed = ce->path_consumed;
779 ref->server_type = ce->srvtype;
780 ref->ref_flag = ce->ref_flags;
781 ref->flags = ce->hdr_flags;
786 kfree(ref->path_name);
787 ref->path_name = NULL;
791 /* Return target list of a DFS cache entry */
792 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
795 struct list_head *head = &tl->tl_list;
796 struct cache_dfs_tgt *t;
797 struct dfs_cache_tgt_iterator *it, *nit;
799 memset(tl, 0, sizeof(*tl));
800 INIT_LIST_HEAD(head);
802 list_for_each_entry(t, &ce->tlist, list) {
803 it = kzalloc(sizeof(*it), GFP_ATOMIC);
809 it->it_name = kstrdup(t->name, GFP_ATOMIC);
815 it->it_path_consumed = t->path_consumed;
817 if (READ_ONCE(ce->tgthint) == t)
818 list_add(&it->it_list, head);
820 list_add_tail(&it->it_list, head);
823 tl->tl_numtgts = ce->numtgts;
828 list_for_each_entry_safe(it, nit, head, it_list) {
829 list_del(&it->it_list);
837 * dfs_cache_find - find a DFS cache entry
839 * If it doesn't find the cache entry, then it will get a DFS referral
840 * for @path and create a new entry.
842 * In case the cache entry exists but expired, it will get a DFS referral
843 * for @path and then update the respective cache entry.
845 * These parameters are passed down to the get_dfs_refer() call if it
846 * needs to be issued:
848 * @ses: smb session to issue the request on
850 * @remap: path character remapping type
851 * @path: path to lookup in DFS referral cache.
853 * @ref: when non-NULL, store single DFS referral result in it.
854 * @tgt_list: when non-NULL, store complete DFS target list in it.
856 * Return zero if the target was found, otherwise non-zero.
858 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
859 int remap, const char *path, struct dfs_info3_param *ref,
860 struct dfs_cache_tgt_list *tgt_list)
864 struct cache_entry *ce;
866 npath = dfs_cache_canonical_path(path, cp, remap);
868 return PTR_ERR(npath);
870 ce = cache_refresh_path(xid, ses, npath, false);
877 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
881 rc = get_targets(ce, tgt_list);
883 up_read(&htable_rw_lock);
891 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
892 * the currently connected server.
894 * NOTE: This function will neither update a cache entry in case it was
895 * expired, nor create a new cache entry if @path hasn't been found. It heavily
896 * relies on an existing cache entry.
898 * @path: canonical DFS path to lookup in the DFS referral cache.
899 * @ref: when non-NULL, store single DFS referral result in it.
900 * @tgt_list: when non-NULL, store complete DFS target list in it.
902 * Return 0 if successful.
903 * Return -ENOENT if the entry was not found.
904 * Return non-zero for other errors.
906 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
907 struct dfs_cache_tgt_list *tgt_list)
910 struct cache_entry *ce;
912 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
914 down_read(&htable_rw_lock);
916 ce = lookup_cache_entry(path);
923 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
927 rc = get_targets(ce, tgt_list);
930 up_read(&htable_rw_lock);
935 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
936 * without sending any requests to the currently connected server.
938 * NOTE: This function will neither update a cache entry in case it was
939 * expired, nor create a new cache entry if @path hasn't been found. It heavily
940 * relies on an existing cache entry.
942 * @path: canonical DFS path to lookup in DFS referral cache.
943 * @it: target iterator which contains the target hint to update the cache
946 * Return zero if the target hint was updated successfully, otherwise non-zero.
948 void dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it)
950 struct cache_dfs_tgt *t;
951 struct cache_entry *ce;
956 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
958 down_read(&htable_rw_lock);
960 ce = lookup_cache_entry(path);
964 t = READ_ONCE(ce->tgthint);
966 if (unlikely(!strcasecmp(it->it_name, t->name)))
969 list_for_each_entry(t, &ce->tlist, list) {
970 if (!strcasecmp(t->name, it->it_name)) {
971 WRITE_ONCE(ce->tgthint, t);
972 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
979 up_read(&htable_rw_lock);
983 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
984 * target iterator (@it).
986 * @path: canonical DFS path to lookup in DFS referral cache.
987 * @it: DFS target iterator.
988 * @ref: DFS referral pointer to set up the gathered information.
990 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
992 int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
993 struct dfs_info3_param *ref)
996 struct cache_entry *ce;
1001 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1003 down_read(&htable_rw_lock);
1005 ce = lookup_cache_entry(path);
1011 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1013 rc = setup_referral(path, ce, ref, it->it_name);
1016 up_read(&htable_rw_lock);
1020 /* Extract share from DFS target and return a pointer to prefix path or NULL */
1021 static const char *parse_target_share(const char *target, char **share)
1023 const char *s, *seps = "/\\";
1026 s = strpbrk(target + 1, seps);
1028 return ERR_PTR(-EINVAL);
1030 len = strcspn(s + 1, seps);
1032 return ERR_PTR(-EINVAL);
1035 len = s - target + 1;
1036 *share = kstrndup(target, len, GFP_KERNEL);
1038 return ERR_PTR(-ENOMEM);
1041 return s + strspn(s, seps);
1045 * dfs_cache_get_tgt_share - parse a DFS target
1047 * @path: DFS full path
1048 * @it: DFS target iterator.
1049 * @share: tree name.
1050 * @prefix: prefix path.
1052 * Return zero if target was parsed correctly, otherwise non-zero.
1054 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
1060 const char *target_ppath, *dfsref_ppath;
1061 size_t target_pplen, dfsref_pplen;
1064 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1067 sep = it->it_name[0];
1068 if (sep != '\\' && sep != '/')
1071 target_ppath = parse_target_share(it->it_name, &target_share);
1072 if (IS_ERR(target_ppath))
1073 return PTR_ERR(target_ppath);
1075 /* point to prefix in DFS referral path */
1076 dfsref_ppath = path + it->it_path_consumed;
1077 dfsref_ppath += strspn(dfsref_ppath, "/\\");
1079 target_pplen = strlen(target_ppath);
1080 dfsref_pplen = strlen(dfsref_ppath);
1082 /* merge prefix paths from DFS referral path and target node */
1083 if (target_pplen || dfsref_pplen) {
1084 len = target_pplen + dfsref_pplen + 2;
1085 ppath = kzalloc(len, GFP_KERNEL);
1087 kfree(target_share);
1090 c = strscpy(ppath, target_ppath, len);
1091 if (c && dfsref_pplen)
1093 strlcat(ppath, dfsref_ppath, len);
1095 *share = target_share;
1100 static bool target_share_equal(struct TCP_Server_Info *server, const char *s1, const char *s2)
1102 char unc[sizeof("\\\\") + SERVER_NAME_LENGTH] = {0};
1105 struct sockaddr_storage ss;
1109 if (strcasecmp(s1, s2))
1113 * Resolve share's hostname and check if server address matches. Otherwise just ignore it
1114 * as we could not have upcall to resolve hostname or failed to convert ip address.
1116 extract_unc_hostname(s1, &host, &hostlen);
1117 scnprintf(unc, sizeof(unc), "\\\\%.*s", (int)hostlen, host);
1119 rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
1121 cifs_dbg(FYI, "%s: could not resolve %.*s. assuming server address matches.\n",
1122 __func__, (int)hostlen, host);
1126 cifs_server_lock(server);
1127 match = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr, (struct sockaddr *)&ss);
1128 cifs_server_unlock(server);
1134 * Mark dfs tcon for reconnecting when the currently connected tcon does not match any of the new
1135 * target shares in @refs.
1137 static void mark_for_reconnect_if_needed(struct TCP_Server_Info *server,
1139 struct dfs_cache_tgt_list *old_tl,
1140 struct dfs_cache_tgt_list *new_tl)
1142 struct dfs_cache_tgt_iterator *oit, *nit;
1144 for (oit = dfs_cache_get_tgt_iterator(old_tl); oit;
1145 oit = dfs_cache_get_next_tgt(old_tl, oit)) {
1146 for (nit = dfs_cache_get_tgt_iterator(new_tl); nit;
1147 nit = dfs_cache_get_next_tgt(new_tl, nit)) {
1148 if (target_share_equal(server,
1149 dfs_cache_get_tgt_name(oit),
1150 dfs_cache_get_tgt_name(nit))) {
1151 dfs_cache_noreq_update_tgthint(path, nit);
1157 cifs_dbg(FYI, "%s: no cached or matched targets. mark dfs share for reconnect.\n", __func__);
1158 cifs_signal_cifsd_for_reconnect(server, true);
1161 static bool is_ses_good(struct cifs_ses *ses)
1163 struct TCP_Server_Info *server = ses->server;
1164 struct cifs_tcon *tcon = ses->tcon_ipc;
1167 spin_lock(&ses->ses_lock);
1168 spin_lock(&ses->chan_lock);
1169 ret = !cifs_chan_needs_reconnect(ses, server) &&
1170 ses->ses_status == SES_GOOD &&
1171 !tcon->need_reconnect;
1172 spin_unlock(&ses->chan_lock);
1173 spin_unlock(&ses->ses_lock);
1177 /* Refresh dfs referral of tcon and mark it for reconnect if needed */
1178 static int __refresh_tcon(const char *path, struct cifs_ses *ses, bool force_refresh)
1180 struct dfs_cache_tgt_list old_tl = DFS_CACHE_TGT_LIST_INIT(old_tl);
1181 struct dfs_cache_tgt_list new_tl = DFS_CACHE_TGT_LIST_INIT(new_tl);
1182 struct TCP_Server_Info *server = ses->server;
1183 bool needs_refresh = false;
1184 struct cache_entry *ce;
1190 down_read(&htable_rw_lock);
1191 ce = lookup_cache_entry(path);
1192 needs_refresh = force_refresh || IS_ERR(ce) || cache_entry_expired(ce);
1194 rc = get_targets(ce, &old_tl);
1195 cifs_dbg(FYI, "%s: get_targets: %d\n", __func__, rc);
1197 up_read(&htable_rw_lock);
1199 if (!needs_refresh) {
1204 ses = CIFS_DFS_ROOT_SES(ses);
1205 if (!is_ses_good(ses)) {
1206 cifs_dbg(FYI, "%s: skip cache refresh due to disconnected ipc\n",
1211 ce = cache_refresh_path(xid, ses, path, true);
1213 rc = get_targets(ce, &new_tl);
1214 up_read(&htable_rw_lock);
1215 cifs_dbg(FYI, "%s: get_targets: %d\n", __func__, rc);
1216 mark_for_reconnect_if_needed(server, path, &old_tl, &new_tl);
1221 dfs_cache_free_tgts(&old_tl);
1222 dfs_cache_free_tgts(&new_tl);
1226 static int refresh_tcon(struct cifs_tcon *tcon, bool force_refresh)
1228 struct TCP_Server_Info *server = tcon->ses->server;
1229 struct cifs_ses *ses = tcon->ses;
1231 mutex_lock(&server->refpath_lock);
1232 if (server->leaf_fullpath)
1233 __refresh_tcon(server->leaf_fullpath + 1, ses, force_refresh);
1234 mutex_unlock(&server->refpath_lock);
1239 * dfs_cache_remount_fs - remount a DFS share
1241 * Reconfigure dfs mount by forcing a new DFS referral and if the currently cached targets do not
1242 * match any of the new targets, mark it for reconnect.
1244 * @cifs_sb: cifs superblock.
1246 * Return zero if remounted, otherwise non-zero.
1248 int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb)
1250 struct cifs_tcon *tcon;
1251 struct TCP_Server_Info *server;
1253 if (!cifs_sb || !cifs_sb->master_tlink)
1256 tcon = cifs_sb_master_tcon(cifs_sb);
1257 server = tcon->ses->server;
1259 if (!server->origin_fullpath) {
1260 cifs_dbg(FYI, "%s: not a dfs mount\n", __func__);
1264 * After reconnecting to a different server, unique ids won't match anymore, so we disable
1265 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
1267 cifs_autodisable_serverino(cifs_sb);
1269 * Force the use of prefix path to support failover on DFS paths that resolve to targets
1270 * that have different prefix paths.
1272 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1274 return refresh_tcon(tcon, true);
1277 /* Refresh all DFS referrals related to DFS tcon */
1278 void dfs_cache_refresh(struct work_struct *work)
1280 struct TCP_Server_Info *server;
1281 struct dfs_root_ses *rses;
1282 struct cifs_tcon *tcon;
1283 struct cifs_ses *ses;
1285 tcon = container_of(work, struct cifs_tcon, dfs_cache_work.work);
1287 server = ses->server;
1289 mutex_lock(&server->refpath_lock);
1290 if (server->leaf_fullpath)
1291 __refresh_tcon(server->leaf_fullpath + 1, ses, false);
1292 mutex_unlock(&server->refpath_lock);
1294 list_for_each_entry(rses, &tcon->dfs_ses_list, list) {
1296 server = ses->server;
1297 mutex_lock(&server->refpath_lock);
1298 if (server->leaf_fullpath)
1299 __refresh_tcon(server->leaf_fullpath + 1, ses, false);
1300 mutex_unlock(&server->refpath_lock);
1303 queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
1304 atomic_read(&dfs_cache_ttl) * HZ);