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