cifs: get rid of @noreq param in __dfs_cache_find()
[platform/kernel/linux-starfive.git] / fs / cifs / dfs_cache.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DFS referral cache routines
4  *
5  * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
6  */
7
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 "cifsglob.h"
15 #include "smb2pdu.h"
16 #include "smb2proto.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_unicode.h"
20 #include "smb2glob.h"
21 #include "fs_context.h"
22
23 #include "dfs_cache.h"
24
25 #define CACHE_HTABLE_SIZE 32
26 #define CACHE_MAX_ENTRIES 64
27
28 #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
29                                     DFSREF_STORAGE_SERVER))
30
31 struct cache_dfs_tgt {
32         char *name;
33         int path_consumed;
34         struct list_head list;
35 };
36
37 struct cache_entry {
38         struct hlist_node hlist;
39         const char *path;
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 */
46         int numtgts;
47         struct list_head tlist;
48         struct cache_dfs_tgt *tgthint;
49 };
50
51 struct vol_info {
52         char *fullpath;
53         spinlock_t ctx_lock;
54         struct smb3_fs_context ctx;
55         char *mntdata;
56         struct list_head list;
57         struct list_head rlist;
58         struct kref refcnt;
59 };
60
61 static struct kmem_cache *cache_slab __read_mostly;
62 static struct workqueue_struct *dfscache_wq __read_mostly;
63
64 static int cache_ttl;
65 static DEFINE_SPINLOCK(cache_ttl_lock);
66
67 static struct nls_table *cache_nlsc;
68
69 /*
70  * Number of entries in the cache
71  */
72 static atomic_t cache_count;
73
74 static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
75 static DECLARE_RWSEM(htable_rw_lock);
76
77 static LIST_HEAD(vol_list);
78 static DEFINE_SPINLOCK(vol_list_lock);
79
80 static void refresh_cache_worker(struct work_struct *work);
81
82 static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
83
84 static int get_normalized_path(const char *path, const char **npath)
85 {
86         if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
87                 return -EINVAL;
88
89         if (*path == '\\') {
90                 *npath = path;
91         } else {
92                 char *s = kstrdup(path, GFP_KERNEL);
93                 if (!s)
94                         return -ENOMEM;
95                 convert_delimiter(s, '\\');
96                 *npath = s;
97         }
98         return 0;
99 }
100
101 static inline void free_normalized_path(const char *path, const char *npath)
102 {
103         if (path != npath)
104                 kfree(npath);
105 }
106
107 static inline bool cache_entry_expired(const struct cache_entry *ce)
108 {
109         struct timespec64 ts;
110
111         ktime_get_coarse_real_ts64(&ts);
112         return timespec64_compare(&ts, &ce->etime) >= 0;
113 }
114
115 static inline void free_tgts(struct cache_entry *ce)
116 {
117         struct cache_dfs_tgt *t, *n;
118
119         list_for_each_entry_safe(t, n, &ce->tlist, list) {
120                 list_del(&t->list);
121                 kfree(t->name);
122                 kfree(t);
123         }
124 }
125
126 static inline void flush_cache_ent(struct cache_entry *ce)
127 {
128         hlist_del_init(&ce->hlist);
129         kfree(ce->path);
130         free_tgts(ce);
131         atomic_dec(&cache_count);
132         kmem_cache_free(cache_slab, ce);
133 }
134
135 static void flush_cache_ents(void)
136 {
137         int i;
138
139         for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
140                 struct hlist_head *l = &cache_htable[i];
141                 struct hlist_node *n;
142                 struct cache_entry *ce;
143
144                 hlist_for_each_entry_safe(ce, n, l, hlist) {
145                         if (!hlist_unhashed(&ce->hlist))
146                                 flush_cache_ent(ce);
147                 }
148         }
149 }
150
151 /*
152  * dfs cache /proc file
153  */
154 static int dfscache_proc_show(struct seq_file *m, void *v)
155 {
156         int i;
157         struct cache_entry *ce;
158         struct cache_dfs_tgt *t;
159
160         seq_puts(m, "DFS cache\n---------\n");
161
162         down_read(&htable_rw_lock);
163         for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
164                 struct hlist_head *l = &cache_htable[i];
165
166                 hlist_for_each_entry(ce, l, hlist) {
167                         if (hlist_unhashed(&ce->hlist))
168                                 continue;
169
170                         seq_printf(m,
171                                    "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",
172                                    ce->path, ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
173                                    ce->ttl, ce->etime.tv_nsec, ce->ref_flags, ce->hdr_flags,
174                                    IS_INTERLINK_SET(ce->hdr_flags) ? "yes" : "no",
175                                    ce->path_consumed, cache_entry_expired(ce) ? "yes" : "no");
176
177                         list_for_each_entry(t, &ce->tlist, list) {
178                                 seq_printf(m, "  %s%s\n",
179                                            t->name,
180                                            ce->tgthint == t ? " (target hint)" : "");
181                         }
182                 }
183         }
184         up_read(&htable_rw_lock);
185
186         return 0;
187 }
188
189 static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
190                                    size_t count, loff_t *ppos)
191 {
192         char c;
193         int rc;
194
195         rc = get_user(c, buffer);
196         if (rc)
197                 return rc;
198
199         if (c != '0')
200                 return -EINVAL;
201
202         cifs_dbg(FYI, "clearing dfs cache\n");
203
204         down_write(&htable_rw_lock);
205         flush_cache_ents();
206         up_write(&htable_rw_lock);
207
208         return count;
209 }
210
211 static int dfscache_proc_open(struct inode *inode, struct file *file)
212 {
213         return single_open(file, dfscache_proc_show, NULL);
214 }
215
216 const struct proc_ops dfscache_proc_ops = {
217         .proc_open      = dfscache_proc_open,
218         .proc_read      = seq_read,
219         .proc_lseek     = seq_lseek,
220         .proc_release   = single_release,
221         .proc_write     = dfscache_proc_write,
222 };
223
224 #ifdef CONFIG_CIFS_DEBUG2
225 static inline void dump_tgts(const struct cache_entry *ce)
226 {
227         struct cache_dfs_tgt *t;
228
229         cifs_dbg(FYI, "target list:\n");
230         list_for_each_entry(t, &ce->tlist, list) {
231                 cifs_dbg(FYI, "  %s%s\n", t->name,
232                          ce->tgthint == t ? " (target hint)" : "");
233         }
234 }
235
236 static inline void dump_ce(const struct cache_entry *ce)
237 {
238         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",
239                  ce->path,
240                  ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
241                  ce->etime.tv_nsec,
242                  ce->hdr_flags, ce->ref_flags,
243                  IS_INTERLINK_SET(ce->hdr_flags) ? "yes" : "no",
244                  ce->path_consumed,
245                  cache_entry_expired(ce) ? "yes" : "no");
246         dump_tgts(ce);
247 }
248
249 static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
250 {
251         int i;
252
253         cifs_dbg(FYI, "DFS referrals returned by the server:\n");
254         for (i = 0; i < numrefs; i++) {
255                 const struct dfs_info3_param *ref = &refs[i];
256
257                 cifs_dbg(FYI,
258                          "\n"
259                          "flags:         0x%x\n"
260                          "path_consumed: %d\n"
261                          "server_type:   0x%x\n"
262                          "ref_flag:      0x%x\n"
263                          "path_name:     %s\n"
264                          "node_name:     %s\n"
265                          "ttl:           %d (%dm)\n",
266                          ref->flags, ref->path_consumed, ref->server_type,
267                          ref->ref_flag, ref->path_name, ref->node_name,
268                          ref->ttl, ref->ttl / 60);
269         }
270 }
271 #else
272 #define dump_tgts(e)
273 #define dump_ce(e)
274 #define dump_refs(r, n)
275 #endif
276
277 /**
278  * dfs_cache_init - Initialize DFS referral cache.
279  *
280  * Return zero if initialized successfully, otherwise non-zero.
281  */
282 int dfs_cache_init(void)
283 {
284         int rc;
285         int i;
286
287         dfscache_wq = alloc_workqueue("cifs-dfscache",
288                                       WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
289         if (!dfscache_wq)
290                 return -ENOMEM;
291
292         cache_slab = kmem_cache_create("cifs_dfs_cache",
293                                        sizeof(struct cache_entry), 0,
294                                        SLAB_HWCACHE_ALIGN, NULL);
295         if (!cache_slab) {
296                 rc = -ENOMEM;
297                 goto out_destroy_wq;
298         }
299
300         for (i = 0; i < CACHE_HTABLE_SIZE; i++)
301                 INIT_HLIST_HEAD(&cache_htable[i]);
302
303         atomic_set(&cache_count, 0);
304         cache_nlsc = load_nls_default();
305
306         cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
307         return 0;
308
309 out_destroy_wq:
310         destroy_workqueue(dfscache_wq);
311         return rc;
312 }
313
314 static inline unsigned int cache_entry_hash(const void *data, int size)
315 {
316         unsigned int h;
317
318         h = jhash(data, size, 0);
319         return h & (CACHE_HTABLE_SIZE - 1);
320 }
321
322 /* Return target hint of a DFS cache entry */
323 static inline char *get_tgt_name(const struct cache_entry *ce)
324 {
325         struct cache_dfs_tgt *t = ce->tgthint;
326
327         return t ? t->name : ERR_PTR(-ENOENT);
328 }
329
330 /* Return expire time out of a new entry's TTL */
331 static inline struct timespec64 get_expire_time(int ttl)
332 {
333         struct timespec64 ts = {
334                 .tv_sec = ttl,
335                 .tv_nsec = 0,
336         };
337         struct timespec64 now;
338
339         ktime_get_coarse_real_ts64(&now);
340         return timespec64_add(now, ts);
341 }
342
343 /* Allocate a new DFS target */
344 static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
345 {
346         struct cache_dfs_tgt *t;
347
348         t = kmalloc(sizeof(*t), GFP_ATOMIC);
349         if (!t)
350                 return ERR_PTR(-ENOMEM);
351         t->name = kstrdup(name, GFP_ATOMIC);
352         if (!t->name) {
353                 kfree(t);
354                 return ERR_PTR(-ENOMEM);
355         }
356         t->path_consumed = path_consumed;
357         INIT_LIST_HEAD(&t->list);
358         return t;
359 }
360
361 /*
362  * Copy DFS referral information to a cache entry and conditionally update
363  * target hint.
364  */
365 static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
366                          struct cache_entry *ce, const char *tgthint)
367 {
368         int i;
369
370         ce->ttl = refs[0].ttl;
371         ce->etime = get_expire_time(ce->ttl);
372         ce->srvtype = refs[0].server_type;
373         ce->hdr_flags = refs[0].flags;
374         ce->ref_flags = refs[0].ref_flag;
375         ce->path_consumed = refs[0].path_consumed;
376
377         for (i = 0; i < numrefs; i++) {
378                 struct cache_dfs_tgt *t;
379
380                 t = alloc_target(refs[i].node_name, refs[i].path_consumed);
381                 if (IS_ERR(t)) {
382                         free_tgts(ce);
383                         return PTR_ERR(t);
384                 }
385                 if (tgthint && !strcasecmp(t->name, tgthint)) {
386                         list_add(&t->list, &ce->tlist);
387                         tgthint = NULL;
388                 } else {
389                         list_add_tail(&t->list, &ce->tlist);
390                 }
391                 ce->numtgts++;
392         }
393
394         ce->tgthint = list_first_entry_or_null(&ce->tlist,
395                                                struct cache_dfs_tgt, list);
396
397         return 0;
398 }
399
400 /* Allocate a new cache entry */
401 static struct cache_entry *alloc_cache_entry(const char *path,
402                                              const struct dfs_info3_param *refs,
403                                              int numrefs)
404 {
405         struct cache_entry *ce;
406         int rc;
407
408         ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
409         if (!ce)
410                 return ERR_PTR(-ENOMEM);
411
412         ce->path = kstrdup(path, GFP_KERNEL);
413         if (!ce->path) {
414                 kmem_cache_free(cache_slab, ce);
415                 return ERR_PTR(-ENOMEM);
416         }
417         INIT_HLIST_NODE(&ce->hlist);
418         INIT_LIST_HEAD(&ce->tlist);
419
420         rc = copy_ref_data(refs, numrefs, ce, NULL);
421         if (rc) {
422                 kfree(ce->path);
423                 kmem_cache_free(cache_slab, ce);
424                 ce = ERR_PTR(rc);
425         }
426         return ce;
427 }
428
429 /* Must be called with htable_rw_lock held */
430 static void remove_oldest_entry(void)
431 {
432         int i;
433         struct cache_entry *ce;
434         struct cache_entry *to_del = NULL;
435
436         for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
437                 struct hlist_head *l = &cache_htable[i];
438
439                 hlist_for_each_entry(ce, l, hlist) {
440                         if (hlist_unhashed(&ce->hlist))
441                                 continue;
442                         if (!to_del || timespec64_compare(&ce->etime,
443                                                           &to_del->etime) < 0)
444                                 to_del = ce;
445                 }
446         }
447
448         if (!to_del) {
449                 cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
450                 return;
451         }
452
453         cifs_dbg(FYI, "%s: removing entry\n", __func__);
454         dump_ce(to_del);
455         flush_cache_ent(to_del);
456 }
457
458 /* Add a new DFS cache entry */
459 static int add_cache_entry(const char *path, unsigned int hash,
460                            struct dfs_info3_param *refs, int numrefs)
461 {
462         struct cache_entry *ce;
463
464         ce = alloc_cache_entry(path, refs, numrefs);
465         if (IS_ERR(ce))
466                 return PTR_ERR(ce);
467
468         spin_lock(&cache_ttl_lock);
469         if (!cache_ttl) {
470                 cache_ttl = ce->ttl;
471                 queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
472         } else {
473                 cache_ttl = min_t(int, cache_ttl, ce->ttl);
474                 mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
475         }
476         spin_unlock(&cache_ttl_lock);
477
478         down_write(&htable_rw_lock);
479         hlist_add_head(&ce->hlist, &cache_htable[hash]);
480         dump_ce(ce);
481         up_write(&htable_rw_lock);
482
483         return 0;
484 }
485
486 static struct cache_entry *__lookup_cache_entry(const char *path)
487 {
488         struct cache_entry *ce;
489         unsigned int h;
490         bool found = false;
491
492         h = cache_entry_hash(path, strlen(path));
493
494         hlist_for_each_entry(ce, &cache_htable[h], hlist) {
495                 if (!strcasecmp(path, ce->path)) {
496                         found = true;
497                         dump_ce(ce);
498                         break;
499                 }
500         }
501
502         if (!found)
503                 ce = ERR_PTR(-ENOENT);
504         return ce;
505 }
506
507 /*
508  * Find a DFS cache entry in hash table and optionally check prefix path against
509  * @path.
510  * Use whole path components in the match.
511  * Must be called with htable_rw_lock held.
512  *
513  * Return ERR_PTR(-ENOENT) if the entry is not found.
514  */
515 static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
516 {
517         struct cache_entry *ce = ERR_PTR(-ENOENT);
518         unsigned int h;
519         int cnt = 0;
520         char *npath;
521         char *s, *e;
522         char sep;
523
524         npath = kstrdup(path, GFP_KERNEL);
525         if (!npath)
526                 return ERR_PTR(-ENOMEM);
527
528         s = npath;
529         sep = *npath;
530         while ((s = strchr(s, sep)) && ++cnt < 3)
531                 s++;
532
533         if (cnt < 3) {
534                 h = cache_entry_hash(path, strlen(path));
535                 ce = __lookup_cache_entry(path);
536                 goto out;
537         }
538         /*
539          * Handle paths that have more than two path components and are a complete prefix of the DFS
540          * referral request path (@path).
541          *
542          * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
543          */
544         h = cache_entry_hash(npath, strlen(npath));
545         e = npath + strlen(npath) - 1;
546         while (e > s) {
547                 char tmp;
548
549                 /* skip separators */
550                 while (e > s && *e == sep)
551                         e--;
552                 if (e == s)
553                         goto out;
554
555                 tmp = *(e+1);
556                 *(e+1) = 0;
557
558                 ce = __lookup_cache_entry(npath);
559                 if (!IS_ERR(ce)) {
560                         h = cache_entry_hash(npath, strlen(npath));
561                         break;
562                 }
563
564                 *(e+1) = tmp;
565                 /* backward until separator */
566                 while (e > s && *e != sep)
567                         e--;
568         }
569 out:
570         if (hash)
571                 *hash = h;
572         kfree(npath);
573         return ce;
574 }
575
576 static void __vol_release(struct vol_info *vi)
577 {
578         kfree(vi->fullpath);
579         kfree(vi->mntdata);
580         smb3_cleanup_fs_context_contents(&vi->ctx);
581         kfree(vi);
582 }
583
584 static void vol_release(struct kref *kref)
585 {
586         struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
587
588         spin_lock(&vol_list_lock);
589         list_del(&vi->list);
590         spin_unlock(&vol_list_lock);
591         __vol_release(vi);
592 }
593
594 static inline void free_vol_list(void)
595 {
596         struct vol_info *vi, *nvi;
597
598         list_for_each_entry_safe(vi, nvi, &vol_list, list) {
599                 list_del_init(&vi->list);
600                 __vol_release(vi);
601         }
602 }
603
604 /**
605  * dfs_cache_destroy - destroy DFS referral cache
606  */
607 void dfs_cache_destroy(void)
608 {
609         cancel_delayed_work_sync(&refresh_task);
610         unload_nls(cache_nlsc);
611         free_vol_list();
612         flush_cache_ents();
613         kmem_cache_destroy(cache_slab);
614         destroy_workqueue(dfscache_wq);
615
616         cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
617 }
618
619 /* Must be called with htable_rw_lock held */
620 static int __update_cache_entry(const char *path,
621                                 const struct dfs_info3_param *refs,
622                                 int numrefs)
623 {
624         int rc;
625         struct cache_entry *ce;
626         char *s, *th = NULL;
627
628         ce = lookup_cache_entry(path, NULL);
629         if (IS_ERR(ce))
630                 return PTR_ERR(ce);
631
632         if (ce->tgthint) {
633                 s = ce->tgthint->name;
634                 th = kstrdup(s, GFP_ATOMIC);
635                 if (!th)
636                         return -ENOMEM;
637         }
638
639         free_tgts(ce);
640         ce->numtgts = 0;
641
642         rc = copy_ref_data(refs, numrefs, ce, th);
643
644         kfree(th);
645
646         return rc;
647 }
648
649 static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
650                             const struct nls_table *nls_codepage, int remap,
651                             const char *path,  struct dfs_info3_param **refs,
652                             int *numrefs)
653 {
654         cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
655
656         if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
657                 return -EOPNOTSUPP;
658         if (unlikely(!nls_codepage))
659                 return -EINVAL;
660
661         *refs = NULL;
662         *numrefs = 0;
663
664         return ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs,
665                                                nls_codepage, remap);
666 }
667
668 /* Update an expired cache entry by getting a new DFS referral from server */
669 static int update_cache_entry(const char *path,
670                               const struct dfs_info3_param *refs,
671                               int numrefs)
672 {
673
674         int rc;
675
676         down_write(&htable_rw_lock);
677         rc = __update_cache_entry(path, refs, numrefs);
678         up_write(&htable_rw_lock);
679
680         return rc;
681 }
682
683 /*
684  * Find, create or update a DFS cache entry.
685  *
686  * If the entry wasn't found, it will create a new one. Or if it was found but
687  * expired, then it will update the entry accordingly.
688  *
689  * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
690  * handle them properly.
691  */
692 static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
693                             const struct nls_table *nls_codepage, int remap, const char *path)
694 {
695         int rc;
696         unsigned int hash;
697         struct cache_entry *ce;
698         struct dfs_info3_param *refs = NULL;
699         int numrefs = 0;
700         bool newent = false;
701
702         cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
703
704         down_read(&htable_rw_lock);
705
706         ce = lookup_cache_entry(path, &hash);
707         if (!IS_ERR(ce)) {
708                 if (!cache_entry_expired(ce)) {
709                         dump_ce(ce);
710                         up_read(&htable_rw_lock);
711                         return 0;
712                 }
713         } else {
714                 newent = true;
715         }
716
717         up_read(&htable_rw_lock);
718
719         /*
720          * No entry was found.
721          *
722          * Request a new DFS referral in order to create a new cache entry, or
723          * updating an existing one.
724          */
725         rc = get_dfs_referral(xid, ses, nls_codepage, remap, path,
726                               &refs, &numrefs);
727         if (rc)
728                 return rc;
729
730         dump_refs(refs, numrefs);
731
732         if (!newent) {
733                 rc = update_cache_entry(path, refs, numrefs);
734                 goto out_free_refs;
735         }
736
737         if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
738                 cifs_dbg(FYI, "%s: reached max cache size (%d)\n",
739                          __func__, CACHE_MAX_ENTRIES);
740                 down_write(&htable_rw_lock);
741                 remove_oldest_entry();
742                 up_write(&htable_rw_lock);
743         }
744
745         rc = add_cache_entry(path, hash, refs, numrefs);
746         if (!rc)
747                 atomic_inc(&cache_count);
748
749 out_free_refs:
750         free_dfs_info_array(refs, numrefs);
751         return rc;
752 }
753
754 /*
755  * Set up a DFS referral from a given cache entry.
756  *
757  * Must be called with htable_rw_lock held.
758  */
759 static int setup_referral(const char *path, struct cache_entry *ce,
760                           struct dfs_info3_param *ref, const char *target)
761 {
762         int rc;
763
764         cifs_dbg(FYI, "%s: set up new ref\n", __func__);
765
766         memset(ref, 0, sizeof(*ref));
767
768         ref->path_name = kstrdup(path, GFP_ATOMIC);
769         if (!ref->path_name)
770                 return -ENOMEM;
771
772         ref->node_name = kstrdup(target, GFP_ATOMIC);
773         if (!ref->node_name) {
774                 rc = -ENOMEM;
775                 goto err_free_path;
776         }
777
778         ref->path_consumed = ce->path_consumed;
779         ref->ttl = ce->ttl;
780         ref->server_type = ce->srvtype;
781         ref->ref_flag = ce->ref_flags;
782         ref->flags = ce->hdr_flags;
783
784         return 0;
785
786 err_free_path:
787         kfree(ref->path_name);
788         ref->path_name = NULL;
789         return rc;
790 }
791
792 /* Return target list of a DFS cache entry */
793 static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
794 {
795         int rc;
796         struct list_head *head = &tl->tl_list;
797         struct cache_dfs_tgt *t;
798         struct dfs_cache_tgt_iterator *it, *nit;
799
800         memset(tl, 0, sizeof(*tl));
801         INIT_LIST_HEAD(head);
802
803         list_for_each_entry(t, &ce->tlist, list) {
804                 it = kzalloc(sizeof(*it), GFP_ATOMIC);
805                 if (!it) {
806                         rc = -ENOMEM;
807                         goto err_free_it;
808                 }
809
810                 it->it_name = kstrdup(t->name, GFP_ATOMIC);
811                 if (!it->it_name) {
812                         kfree(it);
813                         rc = -ENOMEM;
814                         goto err_free_it;
815                 }
816                 it->it_path_consumed = t->path_consumed;
817
818                 if (ce->tgthint == t)
819                         list_add(&it->it_list, head);
820                 else
821                         list_add_tail(&it->it_list, head);
822         }
823
824         tl->tl_numtgts = ce->numtgts;
825
826         return 0;
827
828 err_free_it:
829         list_for_each_entry_safe(it, nit, head, it_list) {
830                 kfree(it->it_name);
831                 kfree(it);
832         }
833         return rc;
834 }
835
836 /**
837  * dfs_cache_find - find a DFS cache entry
838  *
839  * If it doesn't find the cache entry, then it will get a DFS referral
840  * for @path and create a new entry.
841  *
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.
844  *
845  * These parameters are passed down to the get_dfs_refer() call if it
846  * needs to be issued:
847  * @xid: syscall xid
848  * @ses: smb session to issue the request on
849  * @nls_codepage: charset conversion
850  * @remap: path character remapping type
851  * @path: path to lookup in DFS referral cache.
852  *
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.
855  *
856  * Return zero if the target was found, otherwise non-zero.
857  */
858 int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
859                    const struct nls_table *nls_codepage, int remap,
860                    const char *path, struct dfs_info3_param *ref,
861                    struct dfs_cache_tgt_list *tgt_list)
862 {
863         int rc;
864         const char *npath;
865         struct cache_entry *ce;
866
867         rc = get_normalized_path(path, &npath);
868         if (rc)
869                 return rc;
870
871         rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath);
872         if (rc)
873                 goto out_free_path;
874
875         down_read(&htable_rw_lock);
876
877         ce = lookup_cache_entry(npath, NULL);
878         if (IS_ERR(ce)) {
879                 up_read(&htable_rw_lock);
880                 rc = PTR_ERR(ce);
881                 goto out_free_path;
882         }
883
884         if (ref)
885                 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
886         else
887                 rc = 0;
888         if (!rc && tgt_list)
889                 rc = get_targets(ce, tgt_list);
890
891         up_read(&htable_rw_lock);
892
893 out_free_path:
894         free_normalized_path(path, npath);
895         return rc;
896 }
897
898 /**
899  * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
900  * the currently connected server.
901  *
902  * NOTE: This function will neither update a cache entry in case it was
903  * expired, nor create a new cache entry if @path hasn't been found. It heavily
904  * relies on an existing cache entry.
905  *
906  * @path: path to lookup in the DFS referral cache.
907  * @ref: when non-NULL, store single DFS referral result in it.
908  * @tgt_list: when non-NULL, store complete DFS target list in it.
909  *
910  * Return 0 if successful.
911  * Return -ENOENT if the entry was not found.
912  * Return non-zero for other errors.
913  */
914 int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
915                          struct dfs_cache_tgt_list *tgt_list)
916 {
917         int rc;
918         const char *npath;
919         struct cache_entry *ce;
920
921         rc = get_normalized_path(path, &npath);
922         if (rc)
923                 return rc;
924
925         cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
926
927         down_read(&htable_rw_lock);
928
929         ce = lookup_cache_entry(npath, NULL);
930         if (IS_ERR(ce)) {
931                 rc = PTR_ERR(ce);
932                 goto out_unlock;
933         }
934
935         if (ref)
936                 rc = setup_referral(path, ce, ref, get_tgt_name(ce));
937         else
938                 rc = 0;
939         if (!rc && tgt_list)
940                 rc = get_targets(ce, tgt_list);
941
942 out_unlock:
943         up_read(&htable_rw_lock);
944         free_normalized_path(path, npath);
945
946         return rc;
947 }
948
949 /**
950  * dfs_cache_update_tgthint - update target hint of a DFS cache entry
951  *
952  * If it doesn't find the cache entry, then it will get a DFS referral for @path
953  * and create a new entry.
954  *
955  * In case the cache entry exists but expired, it will get a DFS referral
956  * for @path and then update the respective cache entry.
957  *
958  * @xid: syscall id
959  * @ses: smb session
960  * @nls_codepage: charset conversion
961  * @remap: type of character remapping for paths
962  * @path: path to lookup in DFS referral cache.
963  * @it: DFS target iterator
964  *
965  * Return zero if the target hint was updated successfully, otherwise non-zero.
966  */
967 int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
968                              const struct nls_table *nls_codepage, int remap,
969                              const char *path,
970                              const struct dfs_cache_tgt_iterator *it)
971 {
972         int rc;
973         const char *npath;
974         struct cache_entry *ce;
975         struct cache_dfs_tgt *t;
976
977         rc = get_normalized_path(path, &npath);
978         if (rc)
979                 return rc;
980
981         cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
982
983         rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath);
984         if (rc)
985                 goto out_free_path;
986
987         down_write(&htable_rw_lock);
988
989         ce = lookup_cache_entry(npath, NULL);
990         if (IS_ERR(ce)) {
991                 rc = PTR_ERR(ce);
992                 goto out_unlock;
993         }
994
995         t = ce->tgthint;
996
997         if (likely(!strcasecmp(it->it_name, t->name)))
998                 goto out_unlock;
999
1000         list_for_each_entry(t, &ce->tlist, list) {
1001                 if (!strcasecmp(t->name, it->it_name)) {
1002                         ce->tgthint = t;
1003                         cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1004                                  it->it_name);
1005                         break;
1006                 }
1007         }
1008
1009 out_unlock:
1010         up_write(&htable_rw_lock);
1011 out_free_path:
1012         free_normalized_path(path, npath);
1013
1014         return rc;
1015 }
1016
1017 /**
1018  * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
1019  * without sending any requests to the currently connected server.
1020  *
1021  * NOTE: This function will neither update a cache entry in case it was
1022  * expired, nor create a new cache entry if @path hasn't been found. It heavily
1023  * relies on an existing cache entry.
1024  *
1025  * @path: path to lookup in DFS referral cache.
1026  * @it: target iterator which contains the target hint to update the cache
1027  * entry with.
1028  *
1029  * Return zero if the target hint was updated successfully, otherwise non-zero.
1030  */
1031 int dfs_cache_noreq_update_tgthint(const char *path,
1032                                    const struct dfs_cache_tgt_iterator *it)
1033 {
1034         int rc;
1035         const char *npath;
1036         struct cache_entry *ce;
1037         struct cache_dfs_tgt *t;
1038
1039         if (!it)
1040                 return -EINVAL;
1041
1042         rc = get_normalized_path(path, &npath);
1043         if (rc)
1044                 return rc;
1045
1046         cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1047
1048         down_write(&htable_rw_lock);
1049
1050         ce = lookup_cache_entry(npath, NULL);
1051         if (IS_ERR(ce)) {
1052                 rc = PTR_ERR(ce);
1053                 goto out_unlock;
1054         }
1055
1056         rc = 0;
1057         t = ce->tgthint;
1058
1059         if (unlikely(!strcasecmp(it->it_name, t->name)))
1060                 goto out_unlock;
1061
1062         list_for_each_entry(t, &ce->tlist, list) {
1063                 if (!strcasecmp(t->name, it->it_name)) {
1064                         ce->tgthint = t;
1065                         cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
1066                                  it->it_name);
1067                         break;
1068                 }
1069         }
1070
1071 out_unlock:
1072         up_write(&htable_rw_lock);
1073         free_normalized_path(path, npath);
1074
1075         return rc;
1076 }
1077
1078 /**
1079  * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
1080  * target iterator (@it).
1081  *
1082  * @path: path to lookup in DFS referral cache.
1083  * @it: DFS target iterator.
1084  * @ref: DFS referral pointer to set up the gathered information.
1085  *
1086  * Return zero if the DFS referral was set up correctly, otherwise non-zero.
1087  */
1088 int dfs_cache_get_tgt_referral(const char *path,
1089                                const struct dfs_cache_tgt_iterator *it,
1090                                struct dfs_info3_param *ref)
1091 {
1092         int rc;
1093         const char *npath;
1094         struct cache_entry *ce;
1095
1096         if (!it || !ref)
1097                 return -EINVAL;
1098
1099         rc = get_normalized_path(path, &npath);
1100         if (rc)
1101                 return rc;
1102
1103         cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
1104
1105         down_read(&htable_rw_lock);
1106
1107         ce = lookup_cache_entry(npath, NULL);
1108         if (IS_ERR(ce)) {
1109                 rc = PTR_ERR(ce);
1110                 goto out_unlock;
1111         }
1112
1113         cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
1114
1115         rc = setup_referral(path, ce, ref, it->it_name);
1116
1117 out_unlock:
1118         up_read(&htable_rw_lock);
1119         free_normalized_path(path, npath);
1120
1121         return rc;
1122 }
1123
1124 /**
1125  * dfs_cache_add_vol - add a cifs context during mount() that will be handled by
1126  * DFS cache refresh worker.
1127  *
1128  * @mntdata: mount data.
1129  * @ctx: cifs context.
1130  * @fullpath: origin full path.
1131  *
1132  * Return zero if context was set up correctly, otherwise non-zero.
1133  */
1134 int dfs_cache_add_vol(char *mntdata, struct smb3_fs_context *ctx, const char *fullpath)
1135 {
1136         int rc;
1137         struct vol_info *vi;
1138
1139         if (!ctx || !fullpath || !mntdata)
1140                 return -EINVAL;
1141
1142         cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1143
1144         vi = kzalloc(sizeof(*vi), GFP_KERNEL);
1145         if (!vi)
1146                 return -ENOMEM;
1147
1148         vi->fullpath = kstrdup(fullpath, GFP_KERNEL);
1149         if (!vi->fullpath) {
1150                 rc = -ENOMEM;
1151                 goto err_free_vi;
1152         }
1153
1154         rc = smb3_fs_context_dup(&vi->ctx, ctx);
1155         if (rc)
1156                 goto err_free_fullpath;
1157
1158         vi->mntdata = mntdata;
1159         spin_lock_init(&vi->ctx_lock);
1160         kref_init(&vi->refcnt);
1161
1162         spin_lock(&vol_list_lock);
1163         list_add_tail(&vi->list, &vol_list);
1164         spin_unlock(&vol_list_lock);
1165
1166         return 0;
1167
1168 err_free_fullpath:
1169         kfree(vi->fullpath);
1170 err_free_vi:
1171         kfree(vi);
1172         return rc;
1173 }
1174
1175 /* Must be called with vol_list_lock held */
1176 static struct vol_info *find_vol(const char *fullpath)
1177 {
1178         struct vol_info *vi;
1179
1180         list_for_each_entry(vi, &vol_list, list) {
1181                 cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
1182                 if (!strcasecmp(vi->fullpath, fullpath))
1183                         return vi;
1184         }
1185         return ERR_PTR(-ENOENT);
1186 }
1187
1188 /**
1189  * dfs_cache_update_vol - update vol info in DFS cache after failover
1190  *
1191  * @fullpath: fullpath to look up in volume list.
1192  * @server: TCP ses pointer.
1193  *
1194  * Return zero if volume was updated, otherwise non-zero.
1195  */
1196 int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
1197 {
1198         struct vol_info *vi;
1199
1200         if (!fullpath || !server)
1201                 return -EINVAL;
1202
1203         cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1204
1205         spin_lock(&vol_list_lock);
1206         vi = find_vol(fullpath);
1207         if (IS_ERR(vi)) {
1208                 spin_unlock(&vol_list_lock);
1209                 return PTR_ERR(vi);
1210         }
1211         kref_get(&vi->refcnt);
1212         spin_unlock(&vol_list_lock);
1213
1214         cifs_dbg(FYI, "%s: updating volume info\n", __func__);
1215         spin_lock(&vi->ctx_lock);
1216         memcpy(&vi->ctx.dstaddr, &server->dstaddr,
1217                sizeof(vi->ctx.dstaddr));
1218         spin_unlock(&vi->ctx_lock);
1219
1220         kref_put(&vi->refcnt, vol_release);
1221
1222         return 0;
1223 }
1224
1225 /**
1226  * dfs_cache_del_vol - remove volume info in DFS cache during umount()
1227  *
1228  * @fullpath: fullpath to look up in volume list.
1229  */
1230 void dfs_cache_del_vol(const char *fullpath)
1231 {
1232         struct vol_info *vi;
1233
1234         if (!fullpath || !*fullpath)
1235                 return;
1236
1237         cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
1238
1239         spin_lock(&vol_list_lock);
1240         vi = find_vol(fullpath);
1241         spin_unlock(&vol_list_lock);
1242
1243         if (!IS_ERR(vi))
1244                 kref_put(&vi->refcnt, vol_release);
1245 }
1246
1247 /**
1248  * dfs_cache_get_tgt_share - parse a DFS target
1249  *
1250  * @path: DFS full path
1251  * @it: DFS target iterator.
1252  * @share: tree name.
1253  * @prefix: prefix path.
1254  *
1255  * Return zero if target was parsed correctly, otherwise non-zero.
1256  */
1257 int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it,
1258                             char **share, char **prefix)
1259 {
1260         char *s, sep, *p;
1261         size_t len;
1262         size_t plen1, plen2;
1263
1264         if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
1265                 return -EINVAL;
1266
1267         *share = NULL;
1268         *prefix = NULL;
1269
1270         sep = it->it_name[0];
1271         if (sep != '\\' && sep != '/')
1272                 return -EINVAL;
1273
1274         s = strchr(it->it_name + 1, sep);
1275         if (!s)
1276                 return -EINVAL;
1277
1278         /* point to prefix in target node */
1279         s = strchrnul(s + 1, sep);
1280
1281         /* extract target share */
1282         *share = kstrndup(it->it_name, s - it->it_name, GFP_KERNEL);
1283         if (!*share)
1284                 return -ENOMEM;
1285
1286         /* skip separator */
1287         if (*s)
1288                 s++;
1289         /* point to prefix in DFS path */
1290         p = path + it->it_path_consumed;
1291         if (*p == sep)
1292                 p++;
1293
1294         /* merge prefix paths from DFS path and target node */
1295         plen1 = it->it_name + strlen(it->it_name) - s;
1296         plen2 = path + strlen(path) - p;
1297         if (plen1 || plen2) {
1298                 len = plen1 + plen2 + 2;
1299                 *prefix = kmalloc(len, GFP_KERNEL);
1300                 if (!*prefix) {
1301                         kfree(*share);
1302                         *share = NULL;
1303                         return -ENOMEM;
1304                 }
1305                 if (plen1)
1306                         scnprintf(*prefix, len, "%.*s%c%.*s", (int)plen1, s, sep, (int)plen2, p);
1307                 else
1308                         strscpy(*prefix, p, len);
1309         }
1310         return 0;
1311 }
1312
1313 /* Get all tcons that are within a DFS namespace and can be refreshed */
1314 static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
1315 {
1316         struct cifs_ses *ses;
1317         struct cifs_tcon *tcon;
1318
1319         INIT_LIST_HEAD(head);
1320
1321         spin_lock(&cifs_tcp_ses_lock);
1322         list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1323                 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1324                         if (!tcon->need_reconnect && !tcon->need_reopen_files &&
1325                             tcon->dfs_path) {
1326                                 tcon->tc_count++;
1327                                 list_add_tail(&tcon->ulist, head);
1328                         }
1329                 }
1330                 if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
1331                     ses->tcon_ipc->dfs_path) {
1332                         list_add_tail(&ses->tcon_ipc->ulist, head);
1333                 }
1334         }
1335         spin_unlock(&cifs_tcp_ses_lock);
1336 }
1337
1338 static bool is_dfs_link(const char *path)
1339 {
1340         char *s;
1341
1342         s = strchr(path + 1, '\\');
1343         if (!s)
1344                 return false;
1345         return !!strchr(s + 1, '\\');
1346 }
1347
1348 static char *get_dfs_root(const char *path)
1349 {
1350         char *s, *npath;
1351
1352         s = strchr(path + 1, '\\');
1353         if (!s)
1354                 return ERR_PTR(-EINVAL);
1355
1356         s = strchr(s + 1, '\\');
1357         if (!s)
1358                 return ERR_PTR(-EINVAL);
1359
1360         npath = kstrndup(path, s - path, GFP_KERNEL);
1361         if (!npath)
1362                 return ERR_PTR(-ENOMEM);
1363
1364         return npath;
1365 }
1366
1367 static inline void put_tcp_server(struct TCP_Server_Info *server)
1368 {
1369         cifs_put_tcp_session(server, 0);
1370 }
1371
1372 static struct TCP_Server_Info *get_tcp_server(struct smb3_fs_context *ctx)
1373 {
1374         struct TCP_Server_Info *server;
1375
1376         server = cifs_find_tcp_session(ctx);
1377         if (IS_ERR_OR_NULL(server))
1378                 return NULL;
1379
1380         spin_lock(&GlobalMid_Lock);
1381         if (server->tcpStatus != CifsGood) {
1382                 spin_unlock(&GlobalMid_Lock);
1383                 put_tcp_server(server);
1384                 return NULL;
1385         }
1386         spin_unlock(&GlobalMid_Lock);
1387
1388         return server;
1389 }
1390
1391 /* Find root SMB session out of a DFS link path */
1392 static struct cifs_ses *find_root_ses(struct vol_info *vi,
1393                                       struct cifs_tcon *tcon,
1394                                       const char *path)
1395 {
1396         char *rpath;
1397         int rc;
1398         struct cache_entry *ce;
1399         struct dfs_info3_param ref = {0};
1400         char *mdata = NULL, *devname = NULL;
1401         struct TCP_Server_Info *server;
1402         struct cifs_ses *ses;
1403         struct smb3_fs_context ctx = {NULL};
1404
1405         rpath = get_dfs_root(path);
1406         if (IS_ERR(rpath))
1407                 return ERR_CAST(rpath);
1408
1409         down_read(&htable_rw_lock);
1410
1411         ce = lookup_cache_entry(rpath, NULL);
1412         if (IS_ERR(ce)) {
1413                 up_read(&htable_rw_lock);
1414                 ses = ERR_CAST(ce);
1415                 goto out;
1416         }
1417
1418         rc = setup_referral(path, ce, &ref, get_tgt_name(ce));
1419         if (rc) {
1420                 up_read(&htable_rw_lock);
1421                 ses = ERR_PTR(rc);
1422                 goto out;
1423         }
1424
1425         up_read(&htable_rw_lock);
1426
1427         mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref,
1428                                            &devname);
1429         free_dfs_info_param(&ref);
1430
1431         if (IS_ERR(mdata)) {
1432                 ses = ERR_CAST(mdata);
1433                 mdata = NULL;
1434                 goto out;
1435         }
1436
1437         rc = cifs_setup_volume_info(&ctx, NULL, devname);
1438
1439         if (rc) {
1440                 ses = ERR_PTR(rc);
1441                 goto out;
1442         }
1443
1444         server = get_tcp_server(&ctx);
1445         if (!server) {
1446                 ses = ERR_PTR(-EHOSTDOWN);
1447                 goto out;
1448         }
1449
1450         ses = cifs_get_smb_ses(server, &ctx);
1451
1452 out:
1453         smb3_cleanup_fs_context_contents(&ctx);
1454         kfree(mdata);
1455         kfree(rpath);
1456         kfree(devname);
1457
1458         return ses;
1459 }
1460
1461 /* Refresh DFS cache entry from a given tcon */
1462 static int refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
1463 {
1464         int rc = 0;
1465         unsigned int xid;
1466         const char *path, *npath;
1467         struct cache_entry *ce;
1468         struct cifs_ses *root_ses = NULL, *ses;
1469         struct dfs_info3_param *refs = NULL;
1470         int numrefs = 0;
1471
1472         xid = get_xid();
1473
1474         path = tcon->dfs_path + 1;
1475
1476         rc = get_normalized_path(path, &npath);
1477         if (rc)
1478                 goto out_free_xid;
1479
1480         down_read(&htable_rw_lock);
1481
1482         ce = lookup_cache_entry(npath, NULL);
1483         if (IS_ERR(ce)) {
1484                 rc = PTR_ERR(ce);
1485                 up_read(&htable_rw_lock);
1486                 goto out_free_path;
1487         }
1488
1489         if (!cache_entry_expired(ce)) {
1490                 up_read(&htable_rw_lock);
1491                 goto out_free_path;
1492         }
1493
1494         up_read(&htable_rw_lock);
1495
1496         /* If it's a DFS Link, then use root SMB session for refreshing it */
1497         if (is_dfs_link(npath)) {
1498                 ses = root_ses = find_root_ses(vi, tcon, npath);
1499                 if (IS_ERR(ses)) {
1500                         rc = PTR_ERR(ses);
1501                         root_ses = NULL;
1502                         goto out_free_path;
1503                 }
1504         } else {
1505                 ses = tcon->ses;
1506         }
1507
1508         rc = get_dfs_referral(xid, ses, cache_nlsc, tcon->remap, npath, &refs,
1509                               &numrefs);
1510         if (!rc) {
1511                 dump_refs(refs, numrefs);
1512                 rc = update_cache_entry(npath, refs, numrefs);
1513                 free_dfs_info_array(refs, numrefs);
1514         }
1515
1516         if (root_ses)
1517                 cifs_put_smb_ses(root_ses);
1518
1519 out_free_path:
1520         free_normalized_path(path, npath);
1521
1522 out_free_xid:
1523         free_xid(xid);
1524         return rc;
1525 }
1526
1527 /*
1528  * Worker that will refresh DFS cache based on lowest TTL value from a DFS
1529  * referral.
1530  */
1531 static void refresh_cache_worker(struct work_struct *work)
1532 {
1533         struct vol_info *vi, *nvi;
1534         struct TCP_Server_Info *server;
1535         LIST_HEAD(vols);
1536         LIST_HEAD(tcons);
1537         struct cifs_tcon *tcon, *ntcon;
1538         int rc;
1539
1540         /*
1541          * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
1542          * for refreshing.
1543          */
1544         spin_lock(&vol_list_lock);
1545         list_for_each_entry(vi, &vol_list, list) {
1546                 server = get_tcp_server(&vi->ctx);
1547                 if (!server)
1548                         continue;
1549
1550                 kref_get(&vi->refcnt);
1551                 list_add_tail(&vi->rlist, &vols);
1552                 put_tcp_server(server);
1553         }
1554         spin_unlock(&vol_list_lock);
1555
1556         /* Walk through all TCONs and refresh any expired cache entry */
1557         list_for_each_entry_safe(vi, nvi, &vols, rlist) {
1558                 spin_lock(&vi->ctx_lock);
1559                 server = get_tcp_server(&vi->ctx);
1560                 spin_unlock(&vi->ctx_lock);
1561
1562                 if (!server)
1563                         goto next_vol;
1564
1565                 get_tcons(server, &tcons);
1566                 rc = 0;
1567
1568                 list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
1569                         /*
1570                          * Skip tcp server if any of its tcons failed to refresh
1571                          * (possibily due to reconnects).
1572                          */
1573                         if (!rc)
1574                                 rc = refresh_tcon(vi, tcon);
1575
1576                         list_del_init(&tcon->ulist);
1577                         cifs_put_tcon(tcon);
1578                 }
1579
1580                 put_tcp_server(server);
1581
1582 next_vol:
1583                 list_del_init(&vi->rlist);
1584                 kref_put(&vi->refcnt, vol_release);
1585         }
1586
1587         spin_lock(&cache_ttl_lock);
1588         queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
1589         spin_unlock(&cache_ttl_lock);
1590 }