OSDN Git Service

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