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"
23 #include "dfs_cache.h"
25 #define CACHE_HTABLE_SIZE 32
26 #define CACHE_MAX_ENTRIES 64
27 #define CACHE_MIN_TTL 120 /* 2 minutes */
29 #define IS_DFS_INTERLINK(v) (((v) & DFSREF_REFERRAL_SERVER) && !((v) & DFSREF_STORAGE_SERVER))
31 struct cache_dfs_tgt {
34 struct list_head list;
38 struct hlist_node hlist;
40 int hdr_flags; /* RESP_GET_DFS_REFERRAL.ReferralHeaderFlags */
41 int ttl; /* DFS_REREFERRAL_V3.TimeToLive */
42 int srvtype; /* DFS_REREFERRAL_V3.ServerType */
43 int ref_flags; /* DFS_REREFERRAL_V3.ReferralEntryFlags */
44 struct timespec64 etime;
45 int path_consumed; /* RESP_GET_DFS_REFERRAL.PathConsumed */
47 struct list_head tlist;
48 struct cache_dfs_tgt *tgthint;
51 /* List of referral server sessions per dfs mount */
53 struct list_head list;
55 struct cifs_ses *sessions[CACHE_MAX_ENTRIES];
58 struct list_head refresh_list;
62 static struct kmem_cache *cache_slab __read_mostly;
63 static struct workqueue_struct *dfscache_wq __read_mostly;
66 static DEFINE_SPINLOCK(cache_ttl_lock);
68 static struct nls_table *cache_cp;
71 * Number of entries in the cache
73 static atomic_t cache_count;
75 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
76 static DECLARE_RWSEM(htable_rw_lock);
78 static LIST_HEAD(mount_group_list);
79 static DEFINE_MUTEX(mount_group_list_lock);
81 static void refresh_cache_worker(struct work_struct *work);
83 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
85 static void get_ipc_unc(const char *ref_path, char *ipc, size_t ipclen)
90 extract_unc_hostname(ref_path, &host, &len);
91 scnprintf(ipc, ipclen, "\\\\%.*s\\IPC$", (int)len, host);
94 static struct cifs_ses *find_ipc_from_server_path(struct cifs_ses **ses, const char *path)
96 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
98 get_ipc_unc(path, unc, sizeof(unc));
100 if (!strcasecmp(unc, (*ses)->tcon_ipc->treeName))
103 return ERR_PTR(-ENOENT);
106 static void __mount_group_release(struct mount_group *mg)
110 for (i = 0; i < mg->num_sessions; i++)
111 cifs_put_smb_ses(mg->sessions[i]);
115 static void mount_group_release(struct kref *kref)
117 struct mount_group *mg = container_of(kref, struct mount_group, refcount);
119 mutex_lock(&mount_group_list_lock);
121 mutex_unlock(&mount_group_list_lock);
122 __mount_group_release(mg);
125 static struct mount_group *find_mount_group_locked(const uuid_t *id)
127 struct mount_group *mg;
129 list_for_each_entry(mg, &mount_group_list, list) {
130 if (uuid_equal(&mg->id, id))
133 return ERR_PTR(-ENOENT);
136 static struct mount_group *__get_mount_group_locked(const uuid_t *id)
138 struct mount_group *mg;
140 mg = find_mount_group_locked(id);
144 mg = kmalloc(sizeof(*mg), GFP_KERNEL);
146 return ERR_PTR(-ENOMEM);
147 kref_init(&mg->refcount);
148 uuid_copy(&mg->id, id);
149 mg->num_sessions = 0;
150 spin_lock_init(&mg->lock);
151 list_add(&mg->list, &mount_group_list);
155 static struct mount_group *get_mount_group(const uuid_t *id)
157 struct mount_group *mg;
159 mutex_lock(&mount_group_list_lock);
160 mg = __get_mount_group_locked(id);
162 kref_get(&mg->refcount);
163 mutex_unlock(&mount_group_list_lock);
168 static void free_mount_group_list(void)
170 struct mount_group *mg, *tmp_mg;
172 list_for_each_entry_safe(mg, tmp_mg, &mount_group_list, list) {
173 list_del_init(&mg->list);
174 __mount_group_release(mg);
179 * dfs_cache_canonical_path - get a canonical DFS path
183 * @remap: mapping type
185 * Return canonical path if success, otherwise error.
187 char *dfs_cache_canonical_path(const char *path, const struct nls_table *cp, int remap)
193 if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
194 return ERR_PTR(-EINVAL);
196 if (unlikely(strcmp(cp->charset, cache_cp->charset))) {
197 tmp = (char *)cifs_strndup_to_utf16(path, strlen(path), &plen, cp, remap);
199 cifs_dbg(VFS, "%s: failed to convert path to utf16\n", __func__);
200 return ERR_PTR(-EINVAL);
203 npath = cifs_strndup_from_utf16(tmp, plen, true, cache_cp);
207 cifs_dbg(VFS, "%s: failed to convert path from utf16\n", __func__);
208 return ERR_PTR(-EINVAL);
211 npath = kstrdup(path, GFP_KERNEL);
213 return ERR_PTR(-ENOMEM);
215 convert_delimiter(npath, '\\');
219 static inline bool cache_entry_expired(const struct cache_entry *ce)
221 struct timespec64 ts;
223 ktime_get_coarse_real_ts64(&ts);
224 return timespec64_compare(&ts, &ce->etime) >= 0;
227 static inline void free_tgts(struct cache_entry *ce)
229 struct cache_dfs_tgt *t, *n;
231 list_for_each_entry_safe(t, n, &ce->tlist, list) {
238 static inline void flush_cache_ent(struct cache_entry *ce)
240 hlist_del_init(&ce->hlist);
243 atomic_dec(&cache_count);
244 kmem_cache_free(cache_slab, ce);
247 static void flush_cache_ents(void)
251 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
252 struct hlist_head *l = &cache_htable[i];
253 struct hlist_node *n;
254 struct cache_entry *ce;
256 hlist_for_each_entry_safe(ce, n, l, hlist) {
257 if (!hlist_unhashed(&ce->hlist))
264 * dfs cache /proc file
266 static int dfscache_proc_show(struct seq_file *m, void *v)
269 struct cache_entry *ce;
270 struct cache_dfs_tgt *t;
272 seq_puts(m, "DFS cache\n---------\n");
274 down_read(&htable_rw_lock);
275 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
276 struct hlist_head *l = &cache_htable[i];
278 hlist_for_each_entry(ce, l, hlist) {
279 if (hlist_unhashed(&ce->hlist))
283 "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",
284 ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
285 ce->ttl, ce->etime.tv_nsec, ce->ref_flags, ce->hdr_flags,
286 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
287 ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
289 list_for_each_entry(t, &ce->tlist, list) {
290 seq_printf(m, " %s%s\n",
292 ce->tgthint == t ? " (target hint)" : "");
296 up_read(&htable_rw_lock);
301 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
302 size_t count, loff_t *ppos)
307 rc = get_user(c, buffer);
314 cifs_dbg(FYI, "clearing dfs cache\n");
316 down_write(&htable_rw_lock);
318 up_write(&htable_rw_lock);
323 static int dfscache_proc_open(struct inode *inode, struct file *file)
325 return single_open(file, dfscache_proc_show, NULL);
328 const struct proc_ops dfscache_proc_ops = {
329 .proc_open = dfscache_proc_open,
330 .proc_read = seq_read,
331 .proc_lseek = seq_lseek,
332 .proc_release = single_release,
333 .proc_write = dfscache_proc_write,
336 #ifdef CONFIG_CIFS_DEBUG2
337 static inline void dump_tgts(const struct cache_entry *ce)
339 struct cache_dfs_tgt *t;
341 cifs_dbg(FYI, "target list:\n");
342 list_for_each_entry(t, &ce->tlist, list) {
343 cifs_dbg(FYI, " %s%s\n", t->name,
344 ce->tgthint == t ? " (target hint)" : "");
348 static inline void dump_ce(const struct cache_entry *ce)
350 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",
352 ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
354 ce->hdr_flags, ce->ref_flags,
355 IS_DFS_INTERLINK(ce->hdr_flags) ? "yes" : "no",
357 cache_entry_expired(ce) ? "yes" : "no");
361 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
365 cifs_dbg(FYI, "DFS referrals returned by the server:\n");
366 for (i = 0; i < numrefs; i++) {
367 const struct dfs_info3_param *ref = &refs[i];
372 "path_consumed: %d\n"
373 "server_type: 0x%x\n"
378 ref->flags, ref->path_consumed, ref->server_type,
379 ref->ref_flag, ref->path_name, ref->node_name,
380 ref->ttl, ref->ttl / 60);
386 #define dump_refs(r, n)
390 * dfs_cache_init - Initialize DFS referral cache.
392 * Return zero if initialized successfully, otherwise non-zero.
394 int dfs_cache_init(void)
399 dfscache_wq = alloc_workqueue("cifs-dfscache", WQ_FREEZABLE | WQ_UNBOUND, 1);
403 cache_slab = kmem_cache_create("cifs_dfs_cache",
404 sizeof(struct cache_entry), 0,
405 SLAB_HWCACHE_ALIGN, NULL);
411 for (i = 0; i < CACHE_HTABLE_SIZE; i++)
412 INIT_HLIST_HEAD(&cache_htable[i]);
414 atomic_set(&cache_count, 0);
415 cache_cp = load_nls("utf8");
417 cache_cp = load_nls_default();
419 cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
423 destroy_workqueue(dfscache_wq);
427 static int cache_entry_hash(const void *data, int size, unsigned int *hash)
430 const unsigned char *s = data;
434 for (i = 0; i < size; i += clen) {
435 clen = cache_cp->char2uni(&s[i], size - i, &c);
436 if (unlikely(clen < 0)) {
437 cifs_dbg(VFS, "%s: can't convert char\n", __func__);
441 h = jhash(&c, sizeof(c), h);
443 *hash = h % CACHE_HTABLE_SIZE;
447 /* Return target hint of a DFS cache entry */
448 static inline char *get_tgt_name(const struct cache_entry *ce)
450 struct cache_dfs_tgt *t = ce->tgthint;
452 return t ? t->name : ERR_PTR(-ENOENT);
455 /* Return expire time out of a new entry's TTL */
456 static inline struct timespec64 get_expire_time(int ttl)
458 struct timespec64 ts = {
462 struct timespec64 now;
464 ktime_get_coarse_real_ts64(&now);
465 return timespec64_add(now, ts);
468 /* Allocate a new DFS target */
469 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
471 struct cache_dfs_tgt *t;
473 t = kmalloc(sizeof(*t), GFP_ATOMIC);
475 return ERR_PTR(-ENOMEM);
476 t->name = kstrdup(name, GFP_ATOMIC);
479 return ERR_PTR(-ENOMEM);
481 t->path_consumed = path_consumed;
482 INIT_LIST_HEAD(&t->list);
487 * Copy DFS referral information to a cache entry and conditionally update
490 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
491 struct cache_entry *ce, const char *tgthint)
495 ce->ttl = max_t(int, refs[0].ttl, CACHE_MIN_TTL);
496 ce->etime = get_expire_time(ce->ttl);
497 ce->srvtype = refs[0].server_type;
498 ce->hdr_flags = refs[0].flags;
499 ce->ref_flags = refs[0].ref_flag;
500 ce->path_consumed = refs[0].path_consumed;
502 for (i = 0; i < numrefs; i++) {
503 struct cache_dfs_tgt *t;
505 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
510 if (tgthint && !strcasecmp(t->name, tgthint)) {
511 list_add(&t->list, &ce->tlist);
514 list_add_tail(&t->list, &ce->tlist);
519 ce->tgthint = list_first_entry_or_null(&ce->tlist,
520 struct cache_dfs_tgt, list);
525 /* Allocate a new cache entry */
526 static struct cache_entry *alloc_cache_entry(struct dfs_info3_param *refs, int numrefs)
528 struct cache_entry *ce;
531 ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
533 return ERR_PTR(-ENOMEM);
535 ce->path = refs[0].path_name;
536 refs[0].path_name = NULL;
538 INIT_HLIST_NODE(&ce->hlist);
539 INIT_LIST_HEAD(&ce->tlist);
541 rc = copy_ref_data(refs, numrefs, ce, NULL);
544 kmem_cache_free(cache_slab, ce);
550 static void remove_oldest_entry_locked(void)
553 struct cache_entry *ce;
554 struct cache_entry *to_del = NULL;
556 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
558 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
559 struct hlist_head *l = &cache_htable[i];
561 hlist_for_each_entry(ce, l, hlist) {
562 if (hlist_unhashed(&ce->hlist))
564 if (!to_del || timespec64_compare(&ce->etime,
571 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
575 cifs_dbg(FYI, "%s: removing entry\n", __func__);
577 flush_cache_ent(to_del);
580 /* Add a new DFS cache entry */
581 static int add_cache_entry_locked(struct dfs_info3_param *refs, int numrefs)
584 struct cache_entry *ce;
587 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
589 if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
590 cifs_dbg(FYI, "%s: reached max cache size (%d)\n", __func__, CACHE_MAX_ENTRIES);
591 remove_oldest_entry_locked();
594 rc = cache_entry_hash(refs[0].path_name, strlen(refs[0].path_name), &hash);
598 ce = alloc_cache_entry(refs, numrefs);
602 spin_lock(&cache_ttl_lock);
605 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
607 cache_ttl = min_t(int, cache_ttl, ce->ttl);
608 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
610 spin_unlock(&cache_ttl_lock);
612 hlist_add_head(&ce->hlist, &cache_htable[hash]);
615 atomic_inc(&cache_count);
620 /* Check if two DFS paths are equal. @s1 and @s2 are expected to be in @cache_cp's charset */
621 static bool dfs_path_equal(const char *s1, int len1, const char *s2, int len2)
629 for (i = 0; i < len1; i += l1) {
630 l1 = cache_cp->char2uni(&s1[i], len1 - i, &c1);
631 l2 = cache_cp->char2uni(&s2[i], len2 - i, &c2);
632 if (unlikely(l1 < 0 && l2 < 0)) {
640 if (cifs_toupper(c1) != cifs_toupper(c2))
646 static struct cache_entry *__lookup_cache_entry(const char *path, unsigned int hash, int len)
648 struct cache_entry *ce;
650 hlist_for_each_entry(ce, &cache_htable[hash], hlist) {
651 if (dfs_path_equal(ce->path, strlen(ce->path), path, len)) {
656 return ERR_PTR(-EEXIST);
660 * Find a DFS cache entry in hash table and optionally check prefix path against normalized @path.
662 * Use whole path components in the match. Must be called with htable_rw_lock held.
664 * Return ERR_PTR(-EEXIST) if the entry is not found.
666 static struct cache_entry *lookup_cache_entry(const char *path)
668 struct cache_entry *ce;
670 const char *s = path, *e;
675 while ((s = strchr(s, sep)) && ++cnt < 3)
679 rc = cache_entry_hash(path, strlen(path), &hash);
682 return __lookup_cache_entry(path, hash, strlen(path));
685 * Handle paths that have more than two path components and are a complete prefix of the DFS
686 * referral request path (@path).
688 * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
690 e = path + strlen(path) - 1;
694 /* skip separators */
695 while (e > s && *e == sep)
701 rc = cache_entry_hash(path, len, &hash);
704 ce = __lookup_cache_entry(path, hash, len);
708 /* backward until separator */
709 while (e > s && *e != sep)
712 return ERR_PTR(-EEXIST);
716 * dfs_cache_destroy - destroy DFS referral cache
718 void dfs_cache_destroy(void)
720 cancel_delayed_work_sync(&refresh_task);
721 unload_nls(cache_cp);
722 free_mount_group_list();
724 kmem_cache_destroy(cache_slab);
725 destroy_workqueue(dfscache_wq);
727 cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
730 /* Update a cache entry with the new referral in @refs */
731 static int update_cache_entry_locked(struct cache_entry *ce, const struct dfs_info3_param *refs,
737 WARN_ON(!rwsem_is_locked(&htable_rw_lock));
740 s = ce->tgthint->name;
741 th = kstrdup(s, GFP_ATOMIC);
749 rc = copy_ref_data(refs, numrefs, ce, th);
756 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses, const char *path,
757 struct dfs_info3_param **refs, int *numrefs)
762 cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
767 if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
769 if (unlikely(!cache_cp))
772 rc = ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs, cache_cp,
775 struct dfs_info3_param *ref = *refs;
777 for (i = 0; i < *numrefs; i++)
778 convert_delimiter(ref[i].path_name, '\\');
784 * Find, create or update a DFS cache entry.
786 * If the entry wasn't found, it will create a new one. Or if it was found but
787 * expired, then it will update the entry accordingly.
789 * For interlinks, cifs_mount() and expand_dfs_referral() are supposed to
790 * handle them properly.
792 static int cache_refresh_path(const unsigned int xid, struct cifs_ses *ses, const char *path)
795 struct cache_entry *ce;
796 struct dfs_info3_param *refs = NULL;
800 cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
802 down_write(&htable_rw_lock);
804 ce = lookup_cache_entry(path);
806 if (!cache_entry_expired(ce)) {
808 up_write(&htable_rw_lock);
816 * Either the entry was not found, or it is expired.
817 * Request a new DFS referral in order to create or update a cache entry.
819 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
823 dump_refs(refs, numrefs);
826 rc = update_cache_entry_locked(ce, refs, numrefs);
830 rc = add_cache_entry_locked(refs, numrefs);
833 up_write(&htable_rw_lock);
834 free_dfs_info_array(refs, numrefs);
839 * Set up a DFS referral from a given cache entry.
841 * Must be called with htable_rw_lock held.
843 static int setup_referral(const char *path, struct cache_entry *ce,
844 struct dfs_info3_param *ref, const char *target)
848 cifs_dbg(FYI, "%s: set up new ref\n", __func__);
850 memset(ref, 0, sizeof(*ref));
852 ref->path_name = kstrdup(path, GFP_ATOMIC);
856 ref->node_name = kstrdup(target, GFP_ATOMIC);
857 if (!ref->node_name) {
862 ref->path_consumed = ce->path_consumed;
864 ref->server_type = ce->srvtype;
865 ref->ref_flag = ce->ref_flags;
866 ref->flags = ce->hdr_flags;
871 kfree(ref->path_name);
872 ref->path_name = NULL;
876 /* Return target list of a DFS cache entry */
877 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
880 struct list_head *head = &tl->tl_list;
881 struct cache_dfs_tgt *t;
882 struct dfs_cache_tgt_iterator *it, *nit;
884 memset(tl, 0, sizeof(*tl));
885 INIT_LIST_HEAD(head);
887 list_for_each_entry(t, &ce->tlist, list) {
888 it = kzalloc(sizeof(*it), GFP_ATOMIC);
894 it->it_name = kstrdup(t->name, GFP_ATOMIC);
900 it->it_path_consumed = t->path_consumed;
902 if (ce->tgthint == t)
903 list_add(&it->it_list, head);
905 list_add_tail(&it->it_list, head);
908 tl->tl_numtgts = ce->numtgts;
913 list_for_each_entry_safe(it, nit, head, it_list) {
921 * dfs_cache_find - find a DFS cache entry
923 * If it doesn't find the cache entry, then it will get a DFS referral
924 * for @path and create a new entry.
926 * In case the cache entry exists but expired, it will get a DFS referral
927 * for @path and then update the respective cache entry.
929 * These parameters are passed down to the get_dfs_refer() call if it
930 * needs to be issued:
932 * @ses: smb session to issue the request on
934 * @remap: path character remapping type
935 * @path: path to lookup in DFS referral cache.
937 * @ref: when non-NULL, store single DFS referral result in it.
938 * @tgt_list: when non-NULL, store complete DFS target list in it.
940 * Return zero if the target was found, otherwise non-zero.
942 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nls_table *cp,
943 int remap, const char *path, struct dfs_info3_param *ref,
944 struct dfs_cache_tgt_list *tgt_list)
948 struct cache_entry *ce;
950 npath = dfs_cache_canonical_path(path, cp, remap);
952 return PTR_ERR(npath);
954 rc = cache_refresh_path(xid, ses, npath);
958 down_read(&htable_rw_lock);
960 ce = lookup_cache_entry(npath);
962 up_read(&htable_rw_lock);
968 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
972 rc = get_targets(ce, tgt_list);
974 up_read(&htable_rw_lock);
982 * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
983 * the currently connected server.
985 * NOTE: This function will neither update a cache entry in case it was
986 * expired, nor create a new cache entry if @path hasn't been found. It heavily
987 * relies on an existing cache entry.
989 * @path: canonical DFS path to lookup in the DFS referral cache.
990 * @ref: when non-NULL, store single DFS referral result in it.
991 * @tgt_list: when non-NULL, store complete DFS target list in it.
993 * Return 0 if successful.
994 * Return -ENOENT if the entry was not found.
995 * Return non-zero for other errors.
997 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
998 struct dfs_cache_tgt_list *tgt_list)
1001 struct cache_entry *ce;
1003 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1005 down_read(&htable_rw_lock);
1007 ce = lookup_cache_entry(path);
1014 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
1017 if (!rc && tgt_list)
1018 rc = get_targets(ce, tgt_list);
1021 up_read(&htable_rw_lock);
1026 * dfs_cache_update_tgthint - update target hint of a DFS cache entry
1028 * If it doesn't find the cache entry, then it will get a DFS referral for @path
1029 * and create a new entry.
1031 * In case the cache entry exists but expired, it will get a DFS referral
1032 * for @path and then update the respective cache entry.
1037 * @remap: type of character remapping for paths
1038 * @path: path to lookup in DFS referral cache
1039 * @it: DFS target iterator
1041 * Return zero if the target hint was updated successfully, otherwise non-zero.
1043 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
1044 const struct nls_table *cp, int remap, const char *path,
1045 const struct dfs_cache_tgt_iterator *it)
1049 struct cache_entry *ce;
1050 struct cache_dfs_tgt *t;
1052 npath = dfs_cache_canonical_path(path, cp, remap);
1054 return PTR_ERR(npath);
1056 cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
1058 rc = cache_refresh_path(xid, ses, npath);
1062 down_write(&htable_rw_lock);
1064 ce = lookup_cache_entry(npath);
1072 if (likely(!strcasecmp(it->it_name, t->name)))
1075 list_for_each_entry(t, &ce->tlist, list) {
1076 if (!strcasecmp(t->name, it->it_name)) {
1078 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1085 up_write(&htable_rw_lock);
1092 * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1093 * without sending any requests to the currently connected server.
1095 * NOTE: This function will neither update a cache entry in case it was
1096 * expired, nor create a new cache entry if @path hasn't been found. It heavily
1097 * relies on an existing cache entry.
1099 * @path: canonical DFS path to lookup in DFS referral cache.
1100 * @it: target iterator which contains the target hint to update the cache
1103 * Return zero if the target hint was updated successfully, otherwise non-zero.
1105 int dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt_iterator *it)
1108 struct cache_entry *ce;
1109 struct cache_dfs_tgt *t;
1114 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1116 down_write(&htable_rw_lock);
1118 ce = lookup_cache_entry(path);
1127 if (unlikely(!strcasecmp(it->it_name, t->name)))
1130 list_for_each_entry(t, &ce->tlist, list) {
1131 if (!strcasecmp(t->name, it->it_name)) {
1133 cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1140 up_write(&htable_rw_lock);
1145 * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1146 * target iterator (@it).
1148 * @path: canonical DFS path to lookup in DFS referral cache.
1149 * @it: DFS target iterator.
1150 * @ref: DFS referral pointer to set up the gathered information.
1152 * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1154 int dfs_cache_get_tgt_referral(const char *path, const struct dfs_cache_tgt_iterator *it,
1155 struct dfs_info3_param *ref)
1158 struct cache_entry *ce;
1163 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
1165 down_read(&htable_rw_lock);
1167 ce = lookup_cache_entry(path);
1173 cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1175 rc = setup_referral(path, ce, ref, it->it_name);
1178 up_read(&htable_rw_lock);
1183 * dfs_cache_add_refsrv_session - add SMB session of referral server
1185 * @mount_id: mount group uuid to lookup.
1186 * @ses: reference counted SMB session of referral server.
1188 void dfs_cache_add_refsrv_session(const uuid_t *mount_id, struct cifs_ses *ses)
1190 struct mount_group *mg;
1192 if (WARN_ON_ONCE(!mount_id || uuid_is_null(mount_id) || !ses))
1195 mg = get_mount_group(mount_id);
1196 if (WARN_ON_ONCE(IS_ERR(mg)))
1199 spin_lock(&mg->lock);
1200 if (mg->num_sessions < ARRAY_SIZE(mg->sessions))
1201 mg->sessions[mg->num_sessions++] = ses;
1202 spin_unlock(&mg->lock);
1203 kref_put(&mg->refcount, mount_group_release);
1207 * dfs_cache_put_refsrv_sessions - put all referral server sessions
1209 * Put all SMB sessions from the given mount group id.
1211 * @mount_id: mount group uuid to lookup.
1213 void dfs_cache_put_refsrv_sessions(const uuid_t *mount_id)
1215 struct mount_group *mg;
1217 if (!mount_id || uuid_is_null(mount_id))
1220 mutex_lock(&mount_group_list_lock);
1221 mg = find_mount_group_locked(mount_id);
1223 mutex_unlock(&mount_group_list_lock);
1226 mutex_unlock(&mount_group_list_lock);
1227 kref_put(&mg->refcount, mount_group_release);
1231 * dfs_cache_get_tgt_share - parse a DFS target
1233 * @path: DFS full path
1234 * @it: DFS target iterator.
1235 * @share: tree name.
1236 * @prefix: prefix path.
1238 * Return zero if target was parsed correctly, otherwise non-zero.
1240 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it, char **share,
1245 size_t plen1, plen2;
1247 if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1253 sep = it->it_name[0];
1254 if (sep != '\\' && sep != '/')
1257 s = strchr(it->it_name + 1, sep);
1261 /* point to prefix in target node */
1262 s = strchrnul(s + 1, sep);
1264 /* extract target share */
1265 *share = kstrndup(it->it_name, s - it->it_name, GFP_KERNEL);
1269 /* skip separator */
1272 /* point to prefix in DFS path */
1273 p = path + it->it_path_consumed;
1277 /* merge prefix paths from DFS path and target node */
1278 plen1 = it->it_name + strlen(it->it_name) - s;
1279 plen2 = path + strlen(path) - p;
1280 if (plen1 || plen2) {
1281 len = plen1 + plen2 + 2;
1282 *prefix = kmalloc(len, GFP_KERNEL);
1289 scnprintf(*prefix, len, "%.*s%c%.*s", (int)plen1, s, sep, (int)plen2, p);
1291 strscpy(*prefix, p, len);
1297 * Refresh all active dfs mounts regardless of whether they are in cache or not.
1298 * (cache can be cleared)
1300 static void refresh_mounts(struct cifs_ses **sessions)
1302 struct TCP_Server_Info *server;
1303 struct cifs_ses *ses;
1304 struct cifs_tcon *tcon, *ntcon;
1305 struct list_head tcons;
1308 INIT_LIST_HEAD(&tcons);
1310 spin_lock(&cifs_tcp_ses_lock);
1311 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1312 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1313 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1314 if (tcon->dfs_path) {
1316 list_add_tail(&tcon->ulist, &tcons);
1321 spin_unlock(&cifs_tcp_ses_lock);
1323 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1324 const char *path = tcon->dfs_path + 1;
1325 struct cache_entry *ce;
1326 struct dfs_info3_param *refs = NULL;
1328 bool needs_refresh = false;
1331 list_del_init(&tcon->ulist);
1333 ses = find_ipc_from_server_path(sessions, path);
1337 down_read(&htable_rw_lock);
1338 ce = lookup_cache_entry(path);
1339 needs_refresh = IS_ERR(ce) || cache_entry_expired(ce);
1340 up_read(&htable_rw_lock);
1346 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1349 /* Create or update a cache entry with the new referral */
1351 down_write(&htable_rw_lock);
1352 ce = lookup_cache_entry(path);
1354 add_cache_entry_locked(refs, numrefs);
1355 else if (cache_entry_expired(ce))
1356 update_cache_entry_locked(ce, refs, numrefs);
1357 up_write(&htable_rw_lock);
1361 free_dfs_info_array(refs, numrefs);
1362 cifs_put_tcon(tcon);
1366 static void refresh_cache(struct cifs_ses **sessions)
1369 struct cifs_ses *ses;
1371 char *ref_paths[CACHE_MAX_ENTRIES];
1373 struct cache_entry *ce;
1376 * Refresh all cached entries. Get all new referrals outside critical section to avoid
1377 * starvation while performing SMB2 IOCTL on broken or slow connections.
1379 * The cache entries may cover more paths than the active mounts
1380 * (e.g. domain-based DFS referrals or multi tier DFS setups).
1382 down_read(&htable_rw_lock);
1383 for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
1384 struct hlist_head *l = &cache_htable[i];
1386 hlist_for_each_entry(ce, l, hlist) {
1387 if (count == ARRAY_SIZE(ref_paths))
1389 if (hlist_unhashed(&ce->hlist) || !cache_entry_expired(ce) ||
1390 IS_ERR(find_ipc_from_server_path(sessions, ce->path)))
1392 ref_paths[count++] = kstrdup(ce->path, GFP_ATOMIC);
1397 up_read(&htable_rw_lock);
1399 for (i = 0; i < count; i++) {
1400 char *path = ref_paths[i];
1401 struct dfs_info3_param *refs = NULL;
1408 ses = find_ipc_from_server_path(sessions, path);
1413 rc = get_dfs_referral(xid, ses, path, &refs, &numrefs);
1417 down_write(&htable_rw_lock);
1418 ce = lookup_cache_entry(path);
1420 * We need to re-check it because other tasks might have it deleted or
1423 if (!IS_ERR(ce) && cache_entry_expired(ce))
1424 update_cache_entry_locked(ce, refs, numrefs);
1425 up_write(&htable_rw_lock);
1430 free_dfs_info_array(refs, numrefs);
1435 * Worker that will refresh DFS cache and active mounts based on lowest TTL value from a DFS
1438 static void refresh_cache_worker(struct work_struct *work)
1440 struct list_head mglist;
1441 struct mount_group *mg, *tmp_mg;
1442 struct cifs_ses *sessions[CACHE_MAX_ENTRIES + 1] = {NULL};
1443 int max_sessions = ARRAY_SIZE(sessions) - 1;
1446 INIT_LIST_HEAD(&mglist);
1448 /* Get refereces of mount groups */
1449 mutex_lock(&mount_group_list_lock);
1450 list_for_each_entry(mg, &mount_group_list, list) {
1451 kref_get(&mg->refcount);
1452 list_add(&mg->refresh_list, &mglist);
1454 mutex_unlock(&mount_group_list_lock);
1456 /* Fill in local array with an NULL-terminated list of all referral server sessions */
1457 list_for_each_entry(mg, &mglist, refresh_list) {
1458 if (i >= max_sessions)
1461 spin_lock(&mg->lock);
1462 if (i + mg->num_sessions > max_sessions)
1463 count = max_sessions - i;
1465 count = mg->num_sessions;
1466 memcpy(&sessions[i], mg->sessions, count * sizeof(mg->sessions[0]));
1467 spin_unlock(&mg->lock);
1472 /* Refresh all active mounts and cached entries */
1473 refresh_mounts(sessions);
1474 refresh_cache(sessions);
1477 list_for_each_entry_safe(mg, tmp_mg, &mglist, refresh_list) {
1478 list_del_init(&mg->refresh_list);
1479 kref_put(&mg->refcount, mount_group_release);
1482 spin_lock(&cache_ttl_lock);
1483 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1484 spin_unlock(&cache_ttl_lock);