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