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