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 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 static struct kmem_cache *cache_slab __read_mostly;
53 struct workqueue_struct *dfscache_wq;
55 atomic_t dfs_cache_ttl;
57 static struct nls_table *cache_cp;
60 * Number of entries in the cache
62 static atomic_t cache_count;
64 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
65 static DECLARE_RWSEM(htable_rw_lock);
68 * dfs_cache_canonical_path - get a canonical DFS path
72 * @remap: mapping type
74 * Return canonical path if success, otherwise error.
76 char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap)
82 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
83 return ERR_PTR(-EINVAL);
85 if (unlikely(strcmp(cp->charset, cache_cp->charset))) {
86 tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, cp, remap);
88 cifs_dbg(VFS, "%s: failed to convert path to utf16\n", __func__);
89 return ERR_PTR(-EINVAL);
92 npath = cifs_strndup_from_utf16(tmp, plen, true, cache_cp);
96 cifs_dbg(VFS, "%s: failed to convert path from utf16\n", __func__);
97 return ERR_PTR(-EINVAL);
100 npath = kstrdup(path, GFP_KERNEL);
102 return ERR_PTR(-ENOMEM);
104 convert_delimiter(npath, '\\');
108 static inline bool cache_entry_expired(const struct cache_entry *ce)
110 struct timespec64 ts;
112 ktime_get_coarse_real_ts64(&ts);
113 return timespec64_compare(&ts, &ce->etime) >= 0;
116 static inline void free_tgts(struct cache_entry *ce)
118 struct cache_dfs_tgt *t, *n;
120 list_for_each_entry_safe(t, n, &ce->tlist, list) {
127 static inline void flush_cache_ent(struct cache_entry *ce)
129 hlist_del_init(&ce->hlist);
132 atomic_dec(&cache_count);
133 kmem_cache_free(cache_slab, ce);
136 static void flush_cache_ents(void)
140 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
141 struct hlist_head *l = &cache_htable[i];
142 struct hlist_node *n;
143 struct cache_entry *ce;
145 hlist_for_each_entry_safe(ce, n, l, hlist) {
146 if (!hlist_unhashed(&ce->hlist))
153 * dfs cache /proc file
155 static int dfscache_proc_show(struct seq_file *m, void *v)
158 struct cache_entry *ce;
159 struct cache_dfs_tgt *t;
161 seq_puts(m, "DFS cache\n---------\n");
163 down_read(&htable_rw_lock);
164 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
165 struct hlist_head *l = &cache_htable[i];
167 hlist_for_each_entry(ce, l, hlist) {
168 if (hlist_unhashed(&ce->hlist))
172 "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",
173 ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
174 ce->ttl, ce->etime.tv_nsec, ce->hdr_flags, ce->ref_flags,
175 DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
176 ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
178 list_for_each_entry(t, &ce->tlist, list) {
179 seq_printf(m, " %s%s\n",
181 READ_ONCE(ce->tgthint) == t ? " (target hint)" : "");
185 up_read(&htable_rw_lock);
190 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
191 size_t count, loff_t *ppos)
196 rc = get_user(c, buffer);
203 cifs_dbg(FYI, "clearing dfs cache\n");
205 down_write(&htable_rw_lock);
207 up_write(&htable_rw_lock);
212 static int dfscache_proc_open(struct inode *inode, struct file *file)
214 return single_open(file, dfscache_proc_show, NULL);
217 const struct proc_ops dfscache_proc_ops = {
218 .proc_open = dfscache_proc_open,
219 .proc_read = seq_read,
220 .proc_lseek = seq_lseek,
221 .proc_release = single_release,
222 .proc_write = dfscache_proc_write,
225 #ifdef CONFIG_CIFS_DEBUG2
226 static inline void dump_tgts(const struct cache_entry *ce)
228 struct cache_dfs_tgt *t;
230 cifs_dbg(FYI, "target list:\n");
231 list_for_each_entry(t, &ce->tlist, list) {
232 cifs_dbg(FYI, " %s%s\n", t->name,
233 READ_ONCE(ce->tgthint) == t ? " (target hint)" : "");
237 static inline void dump_ce(const struct cache_entry *ce)
239 cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,hdr_flags=0x%x,ref_flags=0x%x,interlink=%s,path_consumed=%d,expired=%s\n",
241 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
243 ce->hdr_flags, ce->ref_flags,
244 DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
246 cache_entry_expired(ce) ? "yes" : "no");
250 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
254 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
255 for (i = 0; i < numrefs; i++) {
256 const struct dfs_info3_param *ref = &refs[i];
261 "path_consumed: %d\n"
262 "server_type: 0x%x\n"
267 ref->flags, ref->path_consumed, ref->server_type,
268 ref->ref_flag, ref->path_name, ref->node_name,
269 ref->ttl, ref->ttl / 60);
275 #define dump_refs(r, n)
279 * dfs_cache_init - Initialize DFS referral cache.
281 * Return zero if initialized successfully, otherwise non-zero.
283 int dfs_cache_init(void)
288 dfscache_wq = alloc_workqueue("cifs-dfscache",
289 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM,
294 cache_slab = kmem_cache_create("cifs_dfs_cache",
295 sizeof(struct cache_entry), 0,
296 SLAB_HWCACHE_ALIGN, NULL);
302 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
303 INIT_HLIST_HEAD(&cache_htable[i]);
305 atomic_set(&cache_count, 0);
306 atomic_set(&dfs_cache_ttl, CACHE_DEFAULT_TTL);
307 cache_cp = load_nls("utf8");
309 cache_cp = load_nls_default();
311 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
315 destroy_workqueue(dfscache_wq);
319 static int cache_entry_hash(const void *data, int size, unsigned int *hash)
322 const unsigned char *s = data;
326 for (i = 0; i < size; i += clen) {
327 clen = cache_cp->char2uni(&s[i], size - i, &c);
328 if (unlikely(clen < 0)) {
329 cifs_dbg(VFS, "%s: can't convert char\n", __func__);
333 h = jhash(&c, sizeof(c), h);
335 *hash = h % CACHE_HTABLE_SIZE;
339 /* Return target hint of a DFS cache entry */
340 static inline char *get_tgt_name(const struct cache_entry *ce)
342 struct cache_dfs_tgt *t = READ_ONCE(ce->tgthint);
344 return t ? t->name : ERR_PTR(-ENOENT);
347 /* Return expire time out of a new entry's TTL */
348 static inline struct timespec64 get_expire_time(int ttl)
350 struct timespec64 ts = {
354 struct timespec64 now;
356 ktime_get_coarse_real_ts64(&now);
357 return timespec64_add(now, ts);
360 /* Allocate a new DFS target */
361 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
363 struct cache_dfs_tgt *t;
365 t = kmalloc(sizeof(*t), GFP_ATOMIC);
367 return ERR_PTR(-ENOMEM);
368 t->name = kstrdup(name, GFP_ATOMIC);
371 return ERR_PTR(-ENOMEM);
373 t->path_consumed = path_consumed;
374 INIT_LIST_HEAD(&t->list);
379 * Copy DFS referral information to a cache entry and conditionally update
382 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
383 struct cache_entry *ce, const char *tgthint)
385 struct cache_dfs_tgt *target;
388 ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
389 ce->etime = get_expire_time(ce->ttl);
390 ce->srvtype = refs[0].server_type;
391 ce->hdr_flags = refs[0].flags;
392 ce->ref_flags = refs[0].ref_flag;
393 ce->path_consumed = refs[0].path_consumed;
395 for (i = 0; i < numrefs; i++) {
396 struct cache_dfs_tgt *t;
398 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
403 if (tgthint && !strcasecmp(t->name, tgthint)) {
404 list_add(&t->list, &ce->tlist);
407 list_add_tail(&t->list, &ce->tlist);
412 target = list_first_entry_or_null(&ce->tlist, struct cache_dfs_tgt,
414 WRITE_ONCE(ce->tgthint, target);
419 /* Allocate a new cache entry */
420 static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
422 struct cache_entry *ce;
425 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
427 return ERR_PTR(-ENOMEM);
429 ce->path = refs[0].path_name;
430 refs[0].path_name = NULL;
432 INIT_HLIST_NODE(&ce->hlist);
433 INIT_LIST_HEAD(&ce->tlist);
435 rc = copy_ref_data(refs, numrefs, ce, NULL);
438 kmem_cache_free(cache_slab, ce);
444 static void remove_oldest_entry_locked(void)
447 struct cache_entry *ce;
448 struct cache_entry *to_del = NULL;
450 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
452 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
453 struct hlist_head *l = &cache_htable[i];
455 hlist_for_each_entry(ce, l, hlist) {
456 if (hlist_unhashed(&ce->hlist))
458 if (!to_del || timespec64_compare(&ce->etime,
465 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
469 cifs_dbg(FYI, "%s: removing entry\n", __func__);
471 flush_cache_ent(to_del);
474 /* Add a new DFS cache entry */
475 static struct cache_entry *add_cache_entry_locked(struct dfs_info3_param *refs,
479 struct cache_entry *ce;
483 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
485 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
486 cifs_dbg(FYI, "%s: reached max cache size (%d)\n", __func__, CACHE_MAX_ENTRIES);
487 remove_oldest_entry_locked();
490 rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
494 ce = alloc_cache_entry(refs, numrefs);
498 ttl = min_t(int, atomic_read(&dfs_cache_ttl), ce->ttl);
499 atomic_set(&dfs_cache_ttl, ttl);
501 hlist_add_head(&ce->hlist, &cache_htable[hash]);
504 atomic_inc(&cache_count);
509 /* Check if two DFS paths are equal. @s1 and @s2 are expected to be in @cache_cp's charset */
510 static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
518 for (i = 0; i < len1; i += l1) {
519 l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
520 l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
521 if (unlikely(l1 < 0 && l2 < 0)) {
529 if (cifs_toupper(c1) != cifs_toupper(c2))
535 static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
537 struct cache_entry *ce;
539 hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
540 if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
545 return ERR_PTR(-ENOENT);
549 * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
551 * Use whole path components in the match. Must be called with htable_rw_lock held.
553 * Return cached entry if successful.
554 * Return ERR_PTR(-ENOENT) if the entry is not found.
555 * Return error ptr otherwise.
557 static struct cache_entry *lookup_cache_entry(const char *path)
559 struct cache_entry *ce;
561 const char *s = path, *e;
566 while ((s = strchr(s, sep)) && ++cnt < 3)
570 rc = cache_entry_hash(path, strlen(path), &hash);
573 return __lookup_cache_entry(path, hash, strlen(path));
576 * Handle paths that have more than two path components and are a complete prefix of the DFS
577 * referral request path (@path).
579 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
581 e = path + strlen(path) - 1;
585 /* skip separators */
586 while (e > s && *e == sep)
592 rc = cache_entry_hash(path, len, &hash);
595 ce = __lookup_cache_entry(path, hash, len);
599 /* backward until separator */
600 while (e > s && *e != sep)
603 return ERR_PTR(-ENOENT);
607 * dfs_cache_destroy - destroy DFS referral cache
609 void dfs_cache_destroy(void)
611 unload_nls(cache_cp);
613 kmem_cache_destroy(cache_slab);
614 destroy_workqueue(dfscache_wq);
616 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
619 /* Update a cache entry with the new referral in @refs */
620 static int update_cache_entry_locked(struct cache_entry *ce, const struct dfs_info3_param *refs,
623 struct cache_dfs_tgt *target;
627 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
629 target = READ_ONCE(ce->tgthint);
631 th = kstrdup(target->name, GFP_ATOMIC);
639 rc = copy_ref_data(refs, numrefs, ce, th);
646 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const char *path,
647 struct dfs_info3_param **refs, int *numrefs)
655 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
657 if (unlikely(!cache_cp))
660 cifs_dbg(FYI, "%s: ipc=%s referral=%s\n", __func__, ses->tcon_ipc->tree_name, path);
661 rc = ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs, cache_cp,
664 struct dfs_info3_param *ref = *refs;
666 for (i = 0; i < *numrefs; i++)
667 convert_delimiter(ref[i].path_name, '\\');
673 * Find, create or update a DFS cache entry.
675 * If the entry wasn't found, it will create a new one. Or if it was found but
676 * expired, then it will update the entry accordingly.
678 * For interlinks, cifs_mount() and expand_dfs_referral() are supposed to
679 * handle them properly.
681 * On success, return entry with acquired lock for reading, otherwise error ptr.
683 static struct cache_entry *cache_refresh_path(const unsigned int xid,
684 struct cifs_ses *ses,
688 struct dfs_info3_param *refs = NULL;
689 struct cache_entry *ce;
693 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
695 down_read(&htable_rw_lock);
697 ce = lookup_cache_entry(path);
699 if (!force_refresh && !cache_entry_expired(ce))
701 } else if (PTR_ERR(ce) != -ENOENT) {
702 up_read(&htable_rw_lock);
707 * Unlock shared access as we don't want to hold any locks while getting
708 * a new referral. The @ses used for performing the I/O could be
709 * reconnecting and it acquires @htable_rw_lock to look up the dfs cache
710 * in order to failover -- if necessary.
712 up_read(&htable_rw_lock);
715 * Either the entry was not found, or it is expired, or it is a forced
717 * Request a new DFS referral in order to create or update a cache entry.
719 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
725 dump_refs(refs, numrefs);
727 down_write(&htable_rw_lock);
728 /* Re-check as another task might have it added or refreshed already */
729 ce = lookup_cache_entry(path);
731 if (force_refresh || cache_entry_expired(ce)) {
732 rc = update_cache_entry_locked(ce, refs, numrefs);
736 } else if (PTR_ERR(ce) == -ENOENT) {
737 ce = add_cache_entry_locked(refs, numrefs);
741 up_write(&htable_rw_lock);
745 downgrade_write(&htable_rw_lock);
747 free_dfs_info_array(refs, numrefs);
752 * Set up a DFS referral from a given cache entry.
754 * Must be called with htable_rw_lock held.
756 static int setup_referral(const char *path, struct cache_entry *ce,
757 struct dfs_info3_param *ref, const char *target)
761 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
763 memset(ref, 0, sizeof(*ref));
765 ref->path_name = kstrdup(path, GFP_ATOMIC);
769 ref->node_name = kstrdup(target, GFP_ATOMIC);
770 if (!ref->node_name) {
775 ref->path_consumed = ce->path_consumed;
777 ref->server_type = ce->srvtype;
778 ref->ref_flag = ce->ref_flags;
779 ref->flags = ce->hdr_flags;
784 kfree(ref->path_name);
785 ref->path_name = NULL;
789 /* Return target list of a DFS cache entry */
790 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
793 struct list_head *head = &tl->tl_list;
794 struct cache_dfs_tgt *t;
795 struct dfs_cache_tgt_iterator *it, *nit;
797 memset(tl, 0, sizeof(*tl));
798 INIT_LIST_HEAD(head);
800 list_for_each_entry(t, &ce->tlist, list) {
801 it = kzalloc(sizeof(*it), GFP_ATOMIC);
807 it->it_name = kstrdup(t->name, GFP_ATOMIC);
813 it->it_path_consumed = t->path_consumed;
815 if (READ_ONCE(ce->tgthint) == t)
816 list_add(&it->it_list, head);
818 list_add_tail(&it->it_list, head);
821 tl->tl_numtgts = ce->numtgts;
826 list_for_each_entry_safe(it, nit, head, it_list) {
827 list_del(&it->it_list);
835 * dfs_cache_find - find a DFS cache entry
837 * If it doesn't find the cache entry, then it will get a DFS referral
838 * for @path and create a new entry.
840 * In case the cache entry exists but expired, it will get a DFS referral
841 * for @path and then update the respective cache entry.
843 * These parameters are passed down to the get_dfs_refer() call if it
844 * needs to be issued:
846 * @ses: smb session to issue the request on
848 * @remap: path character remapping type
849 * @path: path to lookup in DFS referral cache.
851 * @ref: when non-NULL, store single DFS referral result in it.
852 * @tgt_list: when non-NULL, store complete DFS target list in it.
854 * Return zero if the target was found, otherwise non-zero.
856 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
857 int remap, const char *path, struct dfs_info3_param *ref,
858 struct dfs_cache_tgt_list *tgt_list)
862 struct cache_entry *ce;
864 npath = dfs_cache_canonical_path(path, cp, remap);
866 return PTR_ERR(npath);
868 ce = cache_refresh_path(xid, ses, npath, false);
875 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
879 rc = get_targets(ce, tgt_list);
881 up_read(&htable_rw_lock);
889 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
890 * the currently connected server.
892 * NOTE: This function will neither update a cache entry in case it was
893 * expired, nor create a new cache entry if @path hasn't been found. It heavily
894 * relies on an existing cache entry.
896 * @path: canonical DFS path to lookup in the DFS referral cache.
897 * @ref: when non-NULL, store single DFS referral result in it.
898 * @tgt_list: when non-NULL, store complete DFS target list in it.
900 * Return 0 if successful.
901 * Return -ENOENT if the entry was not found.
902 * Return non-zero for other errors.
904 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
905 struct dfs_cache_tgt_list *tgt_list)
908 struct cache_entry *ce;
910 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
912 down_read(&htable_rw_lock);
914 ce = lookup_cache_entry(path);
921 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
925 rc = get_targets(ce, tgt_list);
928 up_read(&htable_rw_lock);
933 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
934 * without sending any requests to the currently connected server.
936 * NOTE: This function will neither update a cache entry in case it was
937 * expired, nor create a new cache entry if @path hasn't been found. It heavily
938 * relies on an existing cache entry.
940 * @path: canonical DFS path to lookup in DFS referral cache.
941 * @it: target iterator which contains the target hint to update the cache
944 * Return zero if the target hint was updated successfully, otherwise non-zero.
946 void dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it)
948 struct cache_dfs_tgt *t;
949 struct cache_entry *ce;
954 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
956 down_read(&htable_rw_lock);
958 ce = lookup_cache_entry(path);
962 t = READ_ONCE(ce->tgthint);
964 if (unlikely(!strcasecmp(it->it_name, t->name)))
967 list_for_each_entry(t, &ce->tlist, list) {
968 if (!strcasecmp(t->name, it->it_name)) {
969 WRITE_ONCE(ce->tgthint, t);
970 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
977 up_read(&htable_rw_lock);
981 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
982 * target iterator (@it).
984 * @path: canonical DFS path to lookup in DFS referral cache.
985 * @it: DFS target iterator.
986 * @ref: DFS referral pointer to set up the gathered information.
988 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
990 int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
991 struct dfs_info3_param *ref)
994 struct cache_entry *ce;
999 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1001 down_read(&htable_rw_lock);
1003 ce = lookup_cache_entry(path);
1009 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1011 rc = setup_referral(path, ce, ref, it->it_name);
1014 up_read(&htable_rw_lock);
1018 /* Extract share from DFS target and return a pointer to prefix path or NULL */
1019 static const char *parse_target_share(const char *target, char **share)
1021 const char *s, *seps = "/\\";
1024 s = strpbrk(target + 1, seps);
1026 return ERR_PTR(-EINVAL);
1028 len = strcspn(s + 1, seps);
1030 return ERR_PTR(-EINVAL);
1033 len = s - target + 1;
1034 *share = kstrndup(target, len, GFP_KERNEL);
1036 return ERR_PTR(-ENOMEM);
1039 return s + strspn(s, seps);
1043 * dfs_cache_get_tgt_share - parse a DFS target
1045 * @path: DFS full path
1046 * @it: DFS target iterator.
1047 * @share: tree name.
1048 * @prefix: prefix path.
1050 * Return zero if target was parsed correctly, otherwise non-zero.
1052 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
1058 const char *target_ppath, *dfsref_ppath;
1059 size_t target_pplen, dfsref_pplen;
1062 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1065 sep = it->it_name[0];
1066 if (sep != '\\' && sep != '/')
1069 target_ppath = parse_target_share(it->it_name, &target_share);
1070 if (IS_ERR(target_ppath))
1071 return PTR_ERR(target_ppath);
1073 /* point to prefix in DFS referral path */
1074 dfsref_ppath = path + it->it_path_consumed;
1075 dfsref_ppath += strspn(dfsref_ppath, "/\\");
1077 target_pplen = strlen(target_ppath);
1078 dfsref_pplen = strlen(dfsref_ppath);
1080 /* merge prefix paths from DFS referral path and target node */
1081 if (target_pplen || dfsref_pplen) {
1082 len = target_pplen + dfsref_pplen + 2;
1083 ppath = kzalloc(len, GFP_KERNEL);
1085 kfree(target_share);
1088 c = strscpy(ppath, target_ppath, len);
1089 if (c && dfsref_pplen)
1091 strlcat(ppath, dfsref_ppath, len);
1093 *share = target_share;
1098 static bool target_share_equal(struct TCP_Server_Info *server, const char *s1, const char *s2)
1100 char unc[sizeof("\\\\") + SERVER_NAME_LENGTH] = {0};
1103 struct sockaddr_storage ss;
1107 if (strcasecmp(s1, s2))
1111 * Resolve share's hostname and check if server address matches. Otherwise just ignore it
1112 * as we could not have upcall to resolve hostname or failed to convert ip address.
1114 extract_unc_hostname(s1, &host, &hostlen);
1115 scnprintf(unc, sizeof(unc), "\\\\%.*s", (int)hostlen, host);
1117 rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
1119 cifs_dbg(FYI, "%s: could not resolve %.*s. assuming server address matches.\n",
1120 __func__, (int)hostlen, host);
1124 cifs_server_lock(server);
1125 match = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr, (struct sockaddr *)&ss);
1126 cifs_server_unlock(server);
1132 * Mark dfs tcon for reconnecting when the currently connected tcon does not match any of the new
1133 * target shares in @refs.
1135 static void mark_for_reconnect_if_needed(struct TCP_Server_Info *server,
1137 struct dfs_cache_tgt_list *old_tl,
1138 struct dfs_cache_tgt_list *new_tl)
1140 struct dfs_cache_tgt_iterator *oit, *nit;
1142 for (oit = dfs_cache_get_tgt_iterator(old_tl); oit;
1143 oit = dfs_cache_get_next_tgt(old_tl, oit)) {
1144 for (nit = dfs_cache_get_tgt_iterator(new_tl); nit;
1145 nit = dfs_cache_get_next_tgt(new_tl, nit)) {
1146 if (target_share_equal(server,
1147 dfs_cache_get_tgt_name(oit),
1148 dfs_cache_get_tgt_name(nit))) {
1149 dfs_cache_noreq_update_tgthint(path, nit);
1155 cifs_dbg(FYI, "%s: no cached or matched targets. mark dfs share for reconnect.\n", __func__);
1156 cifs_signal_cifsd_for_reconnect(server, true);
1159 static bool is_ses_good(struct cifs_ses *ses)
1161 struct TCP_Server_Info *server = ses->server;
1162 struct cifs_tcon *tcon = ses->tcon_ipc;
1165 spin_lock(&ses->ses_lock);
1166 spin_lock(&ses->chan_lock);
1167 ret = !cifs_chan_needs_reconnect(ses, server) &&
1168 ses->ses_status == SES_GOOD &&
1169 !tcon->need_reconnect;
1170 spin_unlock(&ses->chan_lock);
1171 spin_unlock(&ses->ses_lock);
1175 /* Refresh dfs referral of tcon and mark it for reconnect if needed */
1176 static int __refresh_tcon(const char *path, struct cifs_ses *ses, bool force_refresh)
1178 struct TCP_Server_Info *server = ses->server;
1179 DFS_CACHE_TGT_LIST(old_tl);
1180 DFS_CACHE_TGT_LIST(new_tl);
1181 bool needs_refresh = false;
1182 struct cache_entry *ce;
1188 down_read(&htable_rw_lock);
1189 ce = lookup_cache_entry(path);
1190 needs_refresh = force_refresh || IS_ERR(ce) || cache_entry_expired(ce);
1192 rc = get_targets(ce, &old_tl);
1193 cifs_dbg(FYI, "%s: get_targets: %d\n", __func__, rc);
1195 up_read(&htable_rw_lock);
1197 if (!needs_refresh) {
1202 ses = CIFS_DFS_ROOT_SES(ses);
1203 if (!is_ses_good(ses)) {
1204 cifs_dbg(FYI, "%s: skip cache refresh due to disconnected ipc\n",
1209 ce = cache_refresh_path(xid, ses, path, true);
1211 rc = get_targets(ce, &new_tl);
1212 up_read(&htable_rw_lock);
1213 cifs_dbg(FYI, "%s: get_targets: %d\n", __func__, rc);
1214 mark_for_reconnect_if_needed(server, path, &old_tl, &new_tl);
1219 dfs_cache_free_tgts(&old_tl);
1220 dfs_cache_free_tgts(&new_tl);
1224 static int refresh_tcon(struct cifs_tcon *tcon, bool force_refresh)
1226 struct TCP_Server_Info *server = tcon->ses->server;
1227 struct cifs_ses *ses = tcon->ses;
1229 mutex_lock(&server->refpath_lock);
1230 if (server->leaf_fullpath)
1231 __refresh_tcon(server->leaf_fullpath + 1, ses, force_refresh);
1232 mutex_unlock(&server->refpath_lock);
1237 * dfs_cache_remount_fs - remount a DFS share
1239 * Reconfigure dfs mount by forcing a new DFS referral and if the currently cached targets do not
1240 * match any of the new targets, mark it for reconnect.
1242 * @cifs_sb: cifs superblock.
1244 * Return zero if remounted, otherwise non-zero.
1246 int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb)
1248 struct cifs_tcon *tcon;
1250 if (!cifs_sb || !cifs_sb->master_tlink)
1253 tcon = cifs_sb_master_tcon(cifs_sb);
1255 spin_lock(&tcon->tc_lock);
1256 if (!tcon->origin_fullpath) {
1257 spin_unlock(&tcon->tc_lock);
1258 cifs_dbg(FYI, "%s: not a dfs mount\n", __func__);
1261 spin_unlock(&tcon->tc_lock);
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);