]>
Commit | Line | Data |
---|---|---|
54be1f6c PA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * DFS referral cache routines | |
4 | * | |
5 | * Copyright (c) 2018 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 DFS_CACHE_HTABLE_SIZE 32 | |
26 | #define DFS_CACHE_MAX_ENTRIES 64 | |
27 | ||
28 | #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \ | |
29 | DFSREF_STORAGE_SERVER)) | |
30 | ||
31 | struct dfs_cache_tgt { | |
32 | char *t_name; | |
33 | struct list_head t_list; | |
34 | }; | |
35 | ||
36 | struct dfs_cache_entry { | |
37 | struct hlist_node ce_hlist; | |
38 | const char *ce_path; | |
39 | int ce_ttl; | |
40 | int ce_srvtype; | |
41 | int ce_flags; | |
42 | struct timespec64 ce_etime; | |
43 | int ce_path_consumed; | |
44 | int ce_numtgts; | |
45 | struct list_head ce_tlist; | |
46 | struct dfs_cache_tgt *ce_tgthint; | |
47 | struct rcu_head ce_rcu; | |
48 | }; | |
49 | ||
50 | static struct kmem_cache *dfs_cache_slab __read_mostly; | |
51 | ||
52 | struct dfs_cache_vol_info { | |
53 | char *vi_fullpath; | |
54 | struct smb_vol vi_vol; | |
55 | struct list_head vi_list; | |
56 | }; | |
57 | ||
58 | struct dfs_cache { | |
59 | struct mutex dc_lock; | |
60 | struct nls_table *dc_nlsc; | |
61 | struct list_head dc_vol_list; | |
62 | int dc_ttl; | |
63 | struct delayed_work dc_refresh; | |
64 | }; | |
65 | ||
66 | static struct dfs_cache dfs_cache; | |
67 | ||
68 | /* | |
69 | * Number of entries in the cache | |
70 | */ | |
71 | static size_t dfs_cache_count; | |
72 | ||
73 | static DEFINE_MUTEX(dfs_cache_list_lock); | |
74 | static struct hlist_head dfs_cache_htable[DFS_CACHE_HTABLE_SIZE]; | |
75 | ||
76 | static void refresh_cache_worker(struct work_struct *work); | |
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 dfs_cache_entry *ce) | |
103 | { | |
104 | struct timespec64 ts; | |
105 | ||
106 | ts = current_kernel_time64(); | |
107 | return timespec64_compare(&ts, &ce->ce_etime) >= 0; | |
108 | } | |
109 | ||
110 | static inline void free_tgts(struct dfs_cache_entry *ce) | |
111 | { | |
112 | struct dfs_cache_tgt *t, *n; | |
113 | ||
114 | list_for_each_entry_safe(t, n, &ce->ce_tlist, t_list) { | |
115 | list_del(&t->t_list); | |
116 | kfree(t->t_name); | |
117 | kfree(t); | |
118 | } | |
119 | } | |
120 | ||
121 | static void free_cache_entry(struct rcu_head *rcu) | |
122 | { | |
123 | struct dfs_cache_entry *ce = container_of(rcu, struct dfs_cache_entry, | |
124 | ce_rcu); | |
125 | kmem_cache_free(dfs_cache_slab, ce); | |
126 | } | |
127 | ||
128 | static inline void flush_cache_ent(struct dfs_cache_entry *ce) | |
129 | { | |
130 | if (hlist_unhashed(&ce->ce_hlist)) | |
131 | return; | |
132 | ||
133 | hlist_del_init_rcu(&ce->ce_hlist); | |
134 | kfree(ce->ce_path); | |
135 | free_tgts(ce); | |
136 | dfs_cache_count--; | |
137 | call_rcu(&ce->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 < DFS_CACHE_HTABLE_SIZE; i++) { | |
146 | struct hlist_head *l = &dfs_cache_htable[i]; | |
147 | struct dfs_cache_entry *ce; | |
148 | ||
149 | hlist_for_each_entry_rcu(ce, l, ce_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 dfs_cache_entry *ce; | |
162 | struct dfs_cache_tgt *t; | |
163 | ||
164 | seq_puts(m, "DFS cache\n---------\n"); | |
165 | ||
166 | mutex_lock(&dfs_cache_list_lock); | |
167 | ||
168 | rcu_read_lock(); | |
169 | hash_for_each_rcu(dfs_cache_htable, bucket, ce, 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->ce_path, | |
174 | ce->ce_srvtype == DFS_TYPE_ROOT ? "root" : "link", | |
175 | ce->ce_ttl, ce->ce_etime.tv_nsec, | |
176 | IS_INTERLINK_SET(ce->ce_flags) ? "yes" : "no", | |
177 | ce->ce_path_consumed, | |
178 | cache_entry_expired(ce) ? "yes" : "no"); | |
179 | ||
180 | list_for_each_entry(t, &ce->ce_tlist, t_list) { | |
181 | seq_printf(m, " %s%s\n", | |
182 | t->t_name, | |
183 | ce->ce_tgthint == t ? " (target hint)" : ""); | |
184 | } | |
185 | ||
186 | } | |
187 | rcu_read_unlock(); | |
188 | ||
189 | mutex_unlock(&dfs_cache_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(&dfs_cache_list_lock); | |
208 | flush_cache_ents(); | |
209 | mutex_unlock(&dfs_cache_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 dfs_cache_entry *ce) | |
229 | { | |
230 | struct dfs_cache_tgt *t; | |
231 | ||
232 | cifs_dbg(FYI, "target list:\n"); | |
233 | list_for_each_entry(t, &ce->ce_tlist, t_list) { | |
234 | cifs_dbg(FYI, " %s%s\n", t->t_name, | |
235 | ce->ce_tgthint == t ? " (target hint)" : ""); | |
236 | } | |
237 | } | |
238 | ||
239 | static inline void dump_ce(const struct dfs_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->ce_path, | |
243 | ce->ce_srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ce_ttl, | |
244 | ce->ce_etime.tv_nsec, | |
245 | IS_INTERLINK_SET(ce->ce_flags) ? "yes" : "no", | |
246 | ce->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 i; | |
287 | ||
288 | dfs_cache_slab = kmem_cache_create("cifs_dfs_cache", | |
289 | sizeof(struct dfs_cache_entry), 0, | |
290 | SLAB_HWCACHE_ALIGN, NULL); | |
291 | if (!dfs_cache_slab) | |
292 | return -ENOMEM; | |
293 | ||
294 | for (i = 0; i < DFS_CACHE_HTABLE_SIZE; i++) | |
295 | INIT_HLIST_HEAD(&dfs_cache_htable[i]); | |
296 | ||
297 | INIT_LIST_HEAD(&dfs_cache.dc_vol_list); | |
298 | mutex_init(&dfs_cache.dc_lock); | |
299 | INIT_DELAYED_WORK(&dfs_cache.dc_refresh, refresh_cache_worker); | |
300 | dfs_cache.dc_ttl = -1; | |
301 | dfs_cache.dc_nlsc = load_nls_default(); | |
302 | ||
303 | cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | static inline unsigned int cache_entry_hash(const void *data, int size) | |
308 | { | |
309 | unsigned int h; | |
310 | ||
311 | h = jhash(data, size, 0); | |
312 | return h & (DFS_CACHE_HTABLE_SIZE - 1); | |
313 | } | |
314 | ||
315 | /* Check whether second path component of @path is SYSVOL or NETLOGON */ | |
316 | static inline bool is_sysvol_or_netlogon(const char *path) | |
317 | { | |
318 | const char *s; | |
319 | char sep = path[0]; | |
320 | ||
321 | s = strchr(path + 1, sep) + 1; | |
322 | return !strncasecmp(s, "sysvol", strlen("sysvol")) || | |
323 | !strncasecmp(s, "netlogon", strlen("netlogon")); | |
324 | } | |
325 | ||
326 | /* Return target hint of a DFS cache entry */ | |
327 | static inline char *get_tgt_name(const struct dfs_cache_entry *ce) | |
328 | { | |
329 | struct dfs_cache_tgt *t = ce->ce_tgthint; | |
330 | ||
331 | return t ? t->t_name : ERR_PTR(-ENOENT); | |
332 | } | |
333 | ||
334 | /* Return expire time out of a new entry's TTL */ | |
335 | static inline struct timespec64 get_expire_time(int ttl) | |
336 | { | |
337 | struct timespec64 ts = { | |
338 | .tv_sec = ttl, | |
339 | .tv_nsec = 0, | |
340 | }; | |
341 | ||
342 | return timespec64_add(current_kernel_time64(), ts); | |
343 | } | |
344 | ||
345 | /* Allocate a new DFS target */ | |
346 | static inline struct dfs_cache_tgt *alloc_tgt(const char *name) | |
347 | { | |
348 | struct dfs_cache_tgt *t; | |
349 | ||
350 | t = kmalloc(sizeof(*t), GFP_KERNEL); | |
351 | if (!t) | |
352 | return ERR_PTR(-ENOMEM); | |
353 | t->t_name = kstrndup(name, strlen(name), GFP_KERNEL); | |
354 | if (!t->t_name) { | |
355 | kfree(t); | |
356 | return ERR_PTR(-ENOMEM); | |
357 | } | |
358 | INIT_LIST_HEAD(&t->t_list); | |
359 | return t; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Copy DFS referral information to a cache entry and conditionally update | |
364 | * target hint. | |
365 | */ | |
366 | static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs, | |
367 | struct dfs_cache_entry *ce, const char *tgthint) | |
368 | { | |
369 | int i; | |
370 | ||
371 | ce->ce_ttl = refs[0].ttl; | |
372 | ce->ce_etime = get_expire_time(ce->ce_ttl); | |
373 | ce->ce_srvtype = refs[0].server_type; | |
374 | ce->ce_flags = refs[0].ref_flag; | |
375 | ce->ce_path_consumed = refs[0].path_consumed; | |
376 | ||
377 | for (i = 0; i < numrefs; i++) { | |
378 | struct dfs_cache_tgt *t; | |
379 | ||
380 | t = alloc_tgt(refs[i].node_name); | |
381 | if (IS_ERR(t)) { | |
382 | free_tgts(ce); | |
383 | return PTR_ERR(t); | |
384 | } | |
385 | if (tgthint && !strcasecmp(t->t_name, tgthint)) { | |
386 | list_add(&t->t_list, &ce->ce_tlist); | |
387 | tgthint = NULL; | |
388 | } else { | |
389 | list_add_tail(&t->t_list, &ce->ce_tlist); | |
390 | } | |
391 | ce->ce_numtgts++; | |
392 | } | |
393 | ||
394 | ce->ce_tgthint = list_first_entry_or_null(&ce->ce_tlist, | |
395 | struct dfs_cache_tgt, t_list); | |
396 | ||
397 | return 0; | |
398 | } | |
399 | ||
400 | /* Allocate a new cache entry */ | |
401 | static struct dfs_cache_entry * | |
402 | alloc_cache_entry(const char *path, const struct dfs_info3_param *refs, | |
403 | int numrefs) | |
404 | { | |
405 | struct dfs_cache_entry *ce; | |
406 | int rc; | |
407 | ||
408 | ce = kmem_cache_zalloc(dfs_cache_slab, GFP_KERNEL); | |
409 | if (!ce) | |
410 | return ERR_PTR(-ENOMEM); | |
411 | ||
412 | ce->ce_path = kstrdup_const(path, GFP_KERNEL); | |
413 | if (!ce->ce_path) { | |
414 | kfree(ce); | |
415 | return ERR_PTR(-ENOMEM); | |
416 | } | |
417 | INIT_HLIST_NODE(&ce->ce_hlist); | |
418 | INIT_LIST_HEAD(&ce->ce_tlist); | |
419 | ||
420 | rc = copy_ref_data(refs, numrefs, ce, NULL); | |
421 | if (rc) { | |
422 | kfree(ce->ce_path); | |
423 | kfree(ce); | |
424 | ce = ERR_PTR(rc); | |
425 | } | |
426 | return ce; | |
427 | } | |
428 | ||
429 | static void remove_oldest_entry(void) | |
430 | { | |
431 | int bucket; | |
432 | struct dfs_cache_entry *ce; | |
433 | struct dfs_cache_entry *to_del = NULL; | |
434 | ||
435 | rcu_read_lock(); | |
436 | hash_for_each_rcu(dfs_cache_htable, bucket, ce, ce_hlist) { | |
437 | if (!to_del || timespec64_compare(&ce->ce_etime, | |
438 | &to_del->ce_etime) < 0) | |
439 | to_del = ce; | |
440 | } | |
441 | if (!to_del) { | |
442 | cifs_dbg(FYI, "%s: no entry to remove", __func__); | |
443 | goto out; | |
444 | } | |
445 | cifs_dbg(FYI, "%s: removing entry", __func__); | |
446 | dump_ce(to_del); | |
447 | flush_cache_ent(to_del); | |
448 | out: | |
449 | rcu_read_unlock(); | |
450 | } | |
451 | ||
452 | /* Add a new DFS cache entry */ | |
453 | static inline struct dfs_cache_entry * | |
454 | add_cache_entry(unsigned int hash, const char *path, | |
455 | const struct dfs_info3_param *refs, int numrefs) | |
456 | { | |
457 | struct dfs_cache_entry *ce; | |
458 | ||
459 | ce = alloc_cache_entry(path, refs, numrefs); | |
460 | if (IS_ERR(ce)) | |
461 | return ce; | |
462 | ||
463 | hlist_add_head_rcu(&ce->ce_hlist, &dfs_cache_htable[hash]); | |
464 | ||
465 | mutex_lock(&dfs_cache.dc_lock); | |
466 | if (dfs_cache.dc_ttl < 0) { | |
467 | dfs_cache.dc_ttl = ce->ce_ttl; | |
468 | queue_delayed_work(cifsiod_wq, &dfs_cache.dc_refresh, | |
469 | dfs_cache.dc_ttl * HZ); | |
470 | } else { | |
471 | dfs_cache.dc_ttl = min_t(int, dfs_cache.dc_ttl, ce->ce_ttl); | |
472 | mod_delayed_work(cifsiod_wq, &dfs_cache.dc_refresh, | |
473 | dfs_cache.dc_ttl * HZ); | |
474 | } | |
475 | mutex_unlock(&dfs_cache.dc_lock); | |
476 | ||
477 | return ce; | |
478 | } | |
479 | ||
480 | static struct dfs_cache_entry *__find_cache_entry(unsigned int hash, | |
481 | const char *path) | |
482 | { | |
483 | struct dfs_cache_entry *ce; | |
484 | bool found = false; | |
485 | ||
486 | rcu_read_lock(); | |
487 | hlist_for_each_entry_rcu(ce, &dfs_cache_htable[hash], ce_hlist) { | |
488 | if (!strcasecmp(path, ce->ce_path)) { | |
489 | #ifdef CONFIG_CIFS_DEBUG2 | |
490 | char *name = get_tgt_name(ce); | |
491 | ||
492 | if (unlikely(IS_ERR(name))) { | |
493 | rcu_read_unlock(); | |
494 | return ERR_CAST(name); | |
495 | } | |
496 | cifs_dbg(FYI, "%s: cache hit\n", __func__); | |
497 | cifs_dbg(FYI, "%s: target hint: %s\n", __func__, name); | |
498 | #endif | |
499 | found = true; | |
500 | break; | |
501 | } | |
502 | } | |
503 | rcu_read_unlock(); | |
504 | return found ? ce : ERR_PTR(-ENOENT); | |
505 | } | |
506 | ||
507 | /* | |
508 | * Find a DFS cache entry in hash table and optionally check prefix path against | |
509 | * @path. | |
510 | * Use whole path components in the match. | |
511 | * Return ERR_PTR(-ENOENT) if the entry is not found. | |
512 | */ | |
513 | static inline struct dfs_cache_entry *find_cache_entry(const char *path, | |
514 | unsigned int *hash) | |
515 | { | |
516 | *hash = cache_entry_hash(path, strlen(path)); | |
517 | return __find_cache_entry(*hash, path); | |
518 | } | |
519 | ||
520 | static inline void destroy_slab_cache(void) | |
521 | { | |
522 | rcu_barrier(); | |
523 | kmem_cache_destroy(dfs_cache_slab); | |
524 | } | |
525 | ||
526 | static inline void free_vol(struct dfs_cache_vol_info *vi) | |
527 | { | |
528 | list_del(&vi->vi_list); | |
529 | kfree(vi->vi_fullpath); | |
530 | cifs_cleanup_volume_info_contents(&vi->vi_vol); | |
531 | kfree(vi); | |
532 | } | |
533 | ||
534 | static inline void free_vol_list(void) | |
535 | { | |
536 | struct dfs_cache_vol_info *vi, *nvi; | |
537 | ||
538 | list_for_each_entry_safe(vi, nvi, &dfs_cache.dc_vol_list, vi_list) | |
539 | free_vol(vi); | |
540 | } | |
541 | ||
542 | /** | |
543 | * dfs_cache_destroy - destroy DFS referral cache | |
544 | */ | |
545 | void dfs_cache_destroy(void) | |
546 | { | |
547 | cancel_delayed_work_sync(&dfs_cache.dc_refresh); | |
548 | unload_nls(dfs_cache.dc_nlsc); | |
549 | free_vol_list(); | |
550 | mutex_destroy(&dfs_cache.dc_lock); | |
551 | ||
552 | flush_cache_ents(); | |
553 | destroy_slab_cache(); | |
554 | mutex_destroy(&dfs_cache_list_lock); | |
555 | ||
556 | cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__); | |
557 | } | |
558 | ||
559 | static inline struct dfs_cache_entry * | |
560 | __update_cache_entry(const char *path, const struct dfs_info3_param *refs, | |
561 | int numrefs) | |
562 | { | |
563 | int rc; | |
564 | unsigned int h; | |
565 | struct dfs_cache_entry *ce; | |
566 | char *s, *th = NULL; | |
567 | ||
568 | ce = find_cache_entry(path, &h); | |
569 | if (IS_ERR(ce)) | |
570 | return ce; | |
571 | ||
572 | if (ce->ce_tgthint) { | |
573 | s = ce->ce_tgthint->t_name; | |
574 | th = kstrndup(s, strlen(s), GFP_KERNEL); | |
575 | if (!th) | |
576 | return ERR_PTR(-ENOMEM); | |
577 | } | |
578 | ||
579 | free_tgts(ce); | |
580 | ce->ce_numtgts = 0; | |
581 | ||
582 | rc = copy_ref_data(refs, numrefs, ce, th); | |
583 | kfree(th); | |
584 | ||
585 | if (rc) | |
586 | ce = ERR_PTR(rc); | |
587 | ||
588 | return ce; | |
589 | } | |
590 | ||
591 | /* Update an expired cache entry by getting a new DFS referral from server */ | |
592 | static struct dfs_cache_entry * | |
593 | update_cache_entry(const unsigned int xid, struct cifs_ses *ses, | |
594 | const struct nls_table *nls_codepage, int remap, | |
595 | const char *path, struct dfs_cache_entry *ce) | |
596 | { | |
597 | int rc; | |
598 | struct dfs_info3_param *refs = NULL; | |
599 | int numrefs = 0; | |
600 | ||
601 | cifs_dbg(FYI, "%s: update expired cache entry\n", __func__); | |
602 | /* | |
603 | * Check if caller provided enough parameters to update an expired | |
604 | * entry. | |
605 | */ | |
606 | if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) | |
607 | return ERR_PTR(-ETIME); | |
608 | if (unlikely(!nls_codepage)) | |
609 | return ERR_PTR(-ETIME); | |
610 | ||
611 | cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, path); | |
612 | ||
613 | rc = ses->server->ops->get_dfs_refer(xid, ses, path, &refs, &numrefs, | |
614 | nls_codepage, remap); | |
615 | if (rc) | |
616 | ce = ERR_PTR(rc); | |
617 | else | |
618 | ce = __update_cache_entry(path, refs, numrefs); | |
619 | ||
620 | dump_refs(refs, numrefs); | |
621 | free_dfs_info_array(refs, numrefs); | |
622 | ||
623 | return ce; | |
624 | } | |
625 | ||
626 | /* | |
627 | * Find, create or update a DFS cache entry. | |
628 | * | |
629 | * If the entry wasn't found, it will create a new one. Or if it was found but | |
630 | * expired, then it will update the entry accordingly. | |
631 | * | |
632 | * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to | |
633 | * handle them properly. | |
634 | */ | |
635 | static struct dfs_cache_entry * | |
636 | do_dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, | |
637 | const struct nls_table *nls_codepage, int remap, | |
638 | const char *path, bool noreq) | |
639 | { | |
640 | int rc; | |
641 | unsigned int h; | |
642 | struct dfs_cache_entry *ce; | |
643 | struct dfs_info3_param *nrefs; | |
644 | int numnrefs; | |
645 | ||
646 | cifs_dbg(FYI, "%s: search path: %s\n", __func__, path); | |
647 | ||
648 | ce = find_cache_entry(path, &h); | |
649 | if (IS_ERR(ce)) { | |
650 | cifs_dbg(FYI, "%s: cache miss\n", __func__); | |
651 | /* | |
652 | * If @noreq is set, no requests will be sent to the server for | |
653 | * either updating or getting a new DFS referral. | |
654 | */ | |
655 | if (noreq) | |
656 | return ce; | |
657 | /* | |
658 | * No cache entry was found, so check for valid parameters that | |
659 | * will be required to get a new DFS referral and then create a | |
660 | * new cache entry. | |
661 | */ | |
662 | if (!ses || !ses->server || !ses->server->ops->get_dfs_refer) { | |
663 | ce = ERR_PTR(-EOPNOTSUPP); | |
664 | return ce; | |
665 | } | |
666 | if (unlikely(!nls_codepage)) { | |
667 | ce = ERR_PTR(-EINVAL); | |
668 | return ce; | |
669 | } | |
670 | ||
671 | nrefs = NULL; | |
672 | numnrefs = 0; | |
673 | ||
674 | cifs_dbg(FYI, "%s: DFS referral request for %s\n", __func__, | |
675 | path); | |
676 | ||
677 | rc = ses->server->ops->get_dfs_refer(xid, ses, path, &nrefs, | |
678 | &numnrefs, nls_codepage, | |
679 | remap); | |
680 | if (rc) { | |
681 | ce = ERR_PTR(rc); | |
682 | return ce; | |
683 | } | |
684 | ||
685 | dump_refs(nrefs, numnrefs); | |
686 | ||
687 | cifs_dbg(FYI, "%s: new cache entry\n", __func__); | |
688 | ||
689 | if (dfs_cache_count >= DFS_CACHE_MAX_ENTRIES) { | |
690 | cifs_dbg(FYI, "%s: reached max cache size (%d)", | |
691 | __func__, DFS_CACHE_MAX_ENTRIES); | |
692 | remove_oldest_entry(); | |
693 | } | |
694 | ce = add_cache_entry(h, path, nrefs, numnrefs); | |
695 | free_dfs_info_array(nrefs, numnrefs); | |
696 | ||
697 | if (IS_ERR(ce)) | |
698 | return ce; | |
699 | ||
700 | dfs_cache_count++; | |
701 | } | |
702 | ||
703 | dump_ce(ce); | |
704 | ||
705 | /* Just return the found cache entry in case @noreq is set */ | |
706 | if (noreq) | |
707 | return ce; | |
708 | ||
709 | if (cache_entry_expired(ce)) { | |
710 | cifs_dbg(FYI, "%s: expired cache entry\n", __func__); | |
711 | ce = update_cache_entry(xid, ses, nls_codepage, remap, path, | |
712 | ce); | |
713 | if (IS_ERR(ce)) { | |
714 | cifs_dbg(FYI, "%s: failed to update expired entry\n", | |
715 | __func__); | |
716 | } | |
717 | } | |
718 | return ce; | |
719 | } | |
720 | ||
721 | /* Set up a new DFS referral from a given cache entry */ | |
722 | static int setup_ref(const char *path, const struct dfs_cache_entry *ce, | |
723 | struct dfs_info3_param *ref, const char *tgt) | |
724 | { | |
725 | int rc; | |
726 | ||
727 | cifs_dbg(FYI, "%s: set up new ref\n", __func__); | |
728 | ||
729 | memset(ref, 0, sizeof(*ref)); | |
730 | ||
731 | ref->path_name = kstrndup(path, strlen(path), GFP_KERNEL); | |
732 | if (!ref->path_name) | |
733 | return -ENOMEM; | |
734 | ||
735 | ref->path_consumed = ce->ce_path_consumed; | |
736 | ||
737 | ref->node_name = kstrndup(tgt, strlen(tgt), GFP_KERNEL); | |
738 | if (!ref->node_name) { | |
739 | rc = -ENOMEM; | |
740 | goto err_free_path; | |
741 | } | |
742 | ||
743 | ref->ttl = ce->ce_ttl; | |
744 | ref->server_type = ce->ce_srvtype; | |
745 | ref->ref_flag = ce->ce_flags; | |
746 | ||
747 | return 0; | |
748 | ||
749 | err_free_path: | |
750 | kfree(ref->path_name); | |
751 | ref->path_name = NULL; | |
752 | return rc; | |
753 | } | |
754 | ||
755 | /* Return target list of a DFS cache entry */ | |
756 | static int get_tgt_list(const struct dfs_cache_entry *ce, | |
757 | struct dfs_cache_tgt_list *tl) | |
758 | { | |
759 | int rc; | |
760 | struct list_head *head = &tl->tl_list; | |
761 | struct dfs_cache_tgt *t; | |
762 | struct dfs_cache_tgt_iterator *it, *nit; | |
763 | ||
764 | memset(tl, 0, sizeof(*tl)); | |
765 | INIT_LIST_HEAD(head); | |
766 | ||
767 | list_for_each_entry(t, &ce->ce_tlist, t_list) { | |
768 | it = kzalloc(sizeof(*it), GFP_KERNEL); | |
769 | if (!it) { | |
770 | rc = -ENOMEM; | |
771 | goto err_free_it; | |
772 | } | |
773 | ||
774 | it->it_name = kstrndup(t->t_name, strlen(t->t_name), | |
775 | GFP_KERNEL); | |
776 | if (!it->it_name) { | |
777 | rc = -ENOMEM; | |
778 | goto err_free_it; | |
779 | } | |
780 | ||
781 | if (ce->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->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 dfs_cache_entry *ce; | |
828 | ||
829 | if (unlikely(!is_path_valid(path))) | |
830 | return -EINVAL; | |
831 | ||
832 | rc = get_normalized_path(path, &npath); | |
833 | if (rc) | |
834 | return rc; | |
835 | ||
836 | mutex_lock(&dfs_cache_list_lock); | |
837 | ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false); | |
838 | if (!IS_ERR(ce)) { | |
839 | if (ref) | |
840 | rc = setup_ref(path, ce, ref, get_tgt_name(ce)); | |
841 | else | |
842 | rc = 0; | |
843 | if (!rc && tgt_list) | |
844 | rc = get_tgt_list(ce, tgt_list); | |
845 | } else { | |
846 | rc = PTR_ERR(ce); | |
847 | } | |
848 | mutex_unlock(&dfs_cache_list_lock); | |
849 | free_normalized_path(path, npath); | |
850 | return rc; | |
851 | } | |
852 | ||
853 | /** | |
854 | * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to | |
855 | * the currently connected server. | |
856 | * | |
857 | * NOTE: This function will neither update a cache entry in case it was | |
858 | * expired, nor create a new cache entry if @path hasn't been found. It heavily | |
859 | * relies on an existing cache entry. | |
860 | * | |
861 | * @path: path to lookup in the DFS referral cache. | |
862 | * @ref: when non-NULL, store single DFS referral result in it. | |
863 | * @tgt_list: when non-NULL, store complete DFS target list in it. | |
864 | * | |
865 | * Return 0 if successful. | |
866 | * Return -ENOENT if the entry was not found. | |
867 | * Return non-zero for other errors. | |
868 | */ | |
869 | int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref, | |
870 | struct dfs_cache_tgt_list *tgt_list) | |
871 | { | |
872 | int rc; | |
873 | char *npath; | |
874 | struct dfs_cache_entry *ce; | |
875 | ||
876 | if (unlikely(!is_path_valid(path))) | |
877 | return -EINVAL; | |
878 | ||
879 | rc = get_normalized_path(path, &npath); | |
880 | if (rc) | |
881 | return rc; | |
882 | ||
883 | mutex_lock(&dfs_cache_list_lock); | |
884 | ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true); | |
885 | if (IS_ERR(ce)) { | |
886 | rc = PTR_ERR(ce); | |
887 | goto out; | |
888 | } | |
889 | ||
890 | if (ref) | |
891 | rc = setup_ref(path, ce, ref, get_tgt_name(ce)); | |
892 | else | |
893 | rc = 0; | |
894 | if (!rc && tgt_list) | |
895 | rc = get_tgt_list(ce, tgt_list); | |
896 | out: | |
897 | mutex_unlock(&dfs_cache_list_lock); | |
898 | free_normalized_path(path, npath); | |
899 | return rc; | |
900 | } | |
901 | ||
902 | /** | |
903 | * dfs_cache_update_tgthint - update target hint of a DFS cache entry | |
904 | * | |
905 | * If it doesn't find the cache entry, then it will get a DFS referral for @path | |
906 | * and create a new entry. | |
907 | * | |
908 | * In case the cache entry exists but expired, it will get a DFS referral | |
909 | * for @path and then update the respective cache entry. | |
910 | * | |
911 | * @xid: syscall id | |
912 | * @ses: smb session | |
913 | * @nls_codepage: charset conversion | |
914 | * @remap: type of character remapping for paths | |
915 | * @path: path to lookup in DFS referral cache. | |
916 | * @it: DFS target iterator | |
917 | * | |
918 | * Return zero if the target hint was updated successfully, otherwise non-zero. | |
919 | */ | |
920 | int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses, | |
921 | const struct nls_table *nls_codepage, int remap, | |
922 | const char *path, | |
923 | const struct dfs_cache_tgt_iterator *it) | |
924 | { | |
925 | int rc; | |
926 | char *npath; | |
927 | struct dfs_cache_entry *ce; | |
928 | struct dfs_cache_tgt *t; | |
929 | ||
930 | if (unlikely(!is_path_valid(path))) | |
931 | return -EINVAL; | |
932 | ||
933 | rc = get_normalized_path(path, &npath); | |
934 | if (rc) | |
935 | return rc; | |
936 | ||
937 | cifs_dbg(FYI, "%s: path: %s\n", __func__, npath); | |
938 | ||
939 | mutex_lock(&dfs_cache_list_lock); | |
940 | ce = do_dfs_cache_find(xid, ses, nls_codepage, remap, npath, false); | |
941 | if (IS_ERR(ce)) { | |
942 | rc = PTR_ERR(ce); | |
943 | goto out; | |
944 | } | |
945 | ||
946 | rc = 0; | |
947 | ||
948 | t = ce->ce_tgthint; | |
949 | ||
950 | if (likely(!strcasecmp(it->it_name, t->t_name))) | |
951 | goto out; | |
952 | ||
953 | list_for_each_entry(t, &ce->ce_tlist, t_list) { | |
954 | if (!strcasecmp(t->t_name, it->it_name)) { | |
955 | ce->ce_tgthint = t; | |
956 | cifs_dbg(FYI, "%s: new target hint: %s\n", __func__, | |
957 | it->it_name); | |
958 | break; | |
959 | } | |
960 | } | |
961 | ||
962 | out: | |
963 | mutex_unlock(&dfs_cache_list_lock); | |
964 | free_normalized_path(path, npath); | |
965 | return rc; | |
966 | } | |
967 | ||
968 | /** | |
969 | * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry | |
970 | * without sending any requests to the currently connected server. | |
971 | * | |
972 | * NOTE: This function will neither update a cache entry in case it was | |
973 | * expired, nor create a new cache entry if @path hasn't been found. It heavily | |
974 | * relies on an existing cache entry. | |
975 | * | |
976 | * @path: path to lookup in DFS referral cache. | |
977 | * @it: target iterator which contains the target hint to update the cache | |
978 | * entry with. | |
979 | * | |
980 | * Return zero if the target hint was updated successfully, otherwise non-zero. | |
981 | */ | |
982 | int dfs_cache_noreq_update_tgthint(const char *path, | |
983 | const struct dfs_cache_tgt_iterator *it) | |
984 | { | |
985 | int rc; | |
986 | char *npath; | |
987 | struct dfs_cache_entry *ce; | |
988 | struct dfs_cache_tgt *t; | |
989 | ||
990 | if (unlikely(!is_path_valid(path)) || !it) | |
991 | return -EINVAL; | |
992 | ||
993 | rc = get_normalized_path(path, &npath); | |
994 | if (rc) | |
995 | return rc; | |
996 | ||
997 | cifs_dbg(FYI, "%s: path: %s\n", __func__, npath); | |
998 | ||
999 | mutex_lock(&dfs_cache_list_lock); | |
1000 | ||
1001 | ce = do_dfs_cache_find(0, NULL, NULL, 0, npath, true); | |
1002 | if (IS_ERR(ce)) { | |
1003 | rc = PTR_ERR(ce); | |
1004 | goto out; | |
1005 | } | |
1006 | ||
1007 | rc = 0; | |
1008 | ||
1009 | t = ce->ce_tgthint; | |
1010 | ||
1011 | if (unlikely(!strcasecmp(it->it_name, t->t_name))) | |
1012 | goto out; | |
1013 | ||
1014 | list_for_each_entry(t, &ce->ce_tlist, t_list) { | |
1015 | if (!strcasecmp(t->t_name, it->it_name)) { | |
1016 | ce->ce_tgthint = t; | |
1017 | cifs_dbg(FYI, "%s: new target hint: %s\n", __func__, | |
1018 | it->it_name); | |
1019 | break; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | out: | |
1024 | mutex_unlock(&dfs_cache_list_lock); | |
1025 | free_normalized_path(path, npath); | |
1026 | return rc; | |
1027 | } | |
1028 | ||
1029 | /** | |
1030 | * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given | |
1031 | * target iterator (@it). | |
1032 | * | |
1033 | * @path: path to lookup in DFS referral cache. | |
1034 | * @it: DFS target iterator. | |
1035 | * @ref: DFS referral pointer to set up the gathered information. | |
1036 | * | |
1037 | * Return zero if the DFS referral was set up correctly, otherwise non-zero. | |
1038 | */ | |
1039 | int dfs_cache_get_tgt_referral(const char *path, | |
1040 | const struct dfs_cache_tgt_iterator *it, | |
1041 | struct dfs_info3_param *ref) | |
1042 | { | |
1043 | int rc; | |
1044 | char *npath; | |
1045 | struct dfs_cache_entry *ce; | |
1046 | unsigned int h; | |
1047 | ||
1048 | if (!it || !ref) | |
1049 | return -EINVAL; | |
1050 | if (unlikely(!is_path_valid(path))) | |
1051 | return -EINVAL; | |
1052 | ||
1053 | rc = get_normalized_path(path, &npath); | |
1054 | if (rc) | |
1055 | return rc; | |
1056 | ||
1057 | cifs_dbg(FYI, "%s: path: %s\n", __func__, npath); | |
1058 | ||
1059 | mutex_lock(&dfs_cache_list_lock); | |
1060 | ||
1061 | ce = find_cache_entry(npath, &h); | |
1062 | if (IS_ERR(ce)) { | |
1063 | rc = PTR_ERR(ce); | |
1064 | goto out; | |
1065 | } | |
1066 | ||
1067 | cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name); | |
1068 | ||
1069 | rc = setup_ref(path, ce, ref, it->it_name); | |
1070 | ||
1071 | out: | |
1072 | mutex_unlock(&dfs_cache_list_lock); | |
1073 | free_normalized_path(path, npath); | |
1074 | return rc; | |
1075 | } | |
1076 | ||
1077 | static int dup_vol(struct smb_vol *vol, struct smb_vol *new) | |
1078 | { | |
1079 | memcpy(new, vol, sizeof(*new)); | |
1080 | ||
1081 | if (vol->username) { | |
1082 | new->username = kstrndup(vol->username, strlen(vol->username), | |
1083 | GFP_KERNEL); | |
1084 | if (!new->username) | |
1085 | return -ENOMEM; | |
1086 | } | |
1087 | if (vol->password) { | |
1088 | new->password = kstrndup(vol->password, strlen(vol->password), | |
1089 | GFP_KERNEL); | |
1090 | if (!new->password) | |
1091 | goto err_free_username; | |
1092 | } | |
1093 | if (vol->UNC) { | |
1094 | cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC); | |
1095 | new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL); | |
1096 | if (!new->UNC) | |
1097 | goto err_free_password; | |
1098 | } | |
1099 | if (vol->domainname) { | |
1100 | new->domainname = kstrndup(vol->domainname, | |
1101 | strlen(vol->domainname), GFP_KERNEL); | |
1102 | if (!new->domainname) | |
1103 | goto err_free_unc; | |
1104 | } | |
1105 | if (vol->iocharset) { | |
1106 | new->iocharset = kstrndup(vol->iocharset, | |
1107 | strlen(vol->iocharset), GFP_KERNEL); | |
1108 | if (!new->iocharset) | |
1109 | goto err_free_domainname; | |
1110 | } | |
1111 | if (vol->prepath) { | |
1112 | cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath); | |
1113 | new->prepath = kstrndup(vol->prepath, strlen(vol->prepath), | |
1114 | GFP_KERNEL); | |
1115 | if (!new->prepath) | |
1116 | goto err_free_iocharset; | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | ||
1121 | err_free_iocharset: | |
1122 | kfree(new->iocharset); | |
1123 | err_free_domainname: | |
1124 | kfree(new->domainname); | |
1125 | err_free_unc: | |
1126 | kfree(new->UNC); | |
1127 | err_free_password: | |
1128 | kfree(new->password); | |
1129 | err_free_username: | |
1130 | kfree(new->username); | |
1131 | kfree(new); | |
1132 | return -ENOMEM; | |
1133 | } | |
1134 | ||
1135 | /** | |
1136 | * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by | |
1137 | * DFS cache refresh worker. | |
1138 | * | |
1139 | * @vol: cifs volume. | |
1140 | * @fullpath: origin full path. | |
1141 | * | |
1142 | * Return zero if volume was set up correctly, otherwise non-zero. | |
1143 | */ | |
1144 | int dfs_cache_add_vol(struct smb_vol *vol, const char *fullpath) | |
1145 | { | |
1146 | int rc; | |
1147 | struct dfs_cache_vol_info *vi; | |
1148 | ||
1149 | if (!vol || !fullpath) | |
1150 | return -EINVAL; | |
1151 | ||
1152 | cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath); | |
1153 | ||
1154 | vi = kzalloc(sizeof(*vi), GFP_KERNEL); | |
1155 | if (!vi) | |
1156 | return -ENOMEM; | |
1157 | ||
1158 | vi->vi_fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL); | |
1159 | if (!vi->vi_fullpath) { | |
1160 | rc = -ENOMEM; | |
1161 | goto err_free_vi; | |
1162 | } | |
1163 | ||
1164 | rc = dup_vol(vol, &vi->vi_vol); | |
1165 | if (rc) | |
1166 | goto err_free_fullpath; | |
1167 | ||
1168 | mutex_lock(&dfs_cache.dc_lock); | |
1169 | list_add_tail(&vi->vi_list, &dfs_cache.dc_vol_list); | |
1170 | mutex_unlock(&dfs_cache.dc_lock); | |
1171 | return 0; | |
1172 | ||
1173 | err_free_fullpath: | |
1174 | kfree(vi->vi_fullpath); | |
1175 | err_free_vi: | |
1176 | kfree(vi); | |
1177 | return rc; | |
1178 | } | |
1179 | ||
1180 | static inline struct dfs_cache_vol_info *find_vol(const char *fullpath) | |
1181 | { | |
1182 | struct dfs_cache_vol_info *vi; | |
1183 | ||
1184 | list_for_each_entry(vi, &dfs_cache.dc_vol_list, vi_list) { | |
1185 | cifs_dbg(FYI, "%s: vi->vi_fullpath: %s\n", __func__, | |
1186 | vi->vi_fullpath); | |
1187 | if (!strcasecmp(vi->vi_fullpath, fullpath)) | |
1188 | return vi; | |
1189 | } | |
1190 | return ERR_PTR(-ENOENT); | |
1191 | } | |
1192 | ||
1193 | /** | |
1194 | * dfs_cache_update_vol - update vol info in DFS cache after failover | |
1195 | * | |
1196 | * @fullpath: fullpath to look up in volume list. | |
1197 | * @server: TCP ses pointer. | |
1198 | * | |
1199 | * Return zero if volume was updated, otherwise non-zero. | |
1200 | */ | |
1201 | int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server) | |
1202 | { | |
1203 | int rc; | |
1204 | struct dfs_cache_vol_info *vi; | |
1205 | ||
1206 | if (!fullpath || !server) | |
1207 | return -EINVAL; | |
1208 | ||
1209 | cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath); | |
1210 | ||
1211 | mutex_lock(&dfs_cache.dc_lock); | |
1212 | ||
1213 | vi = find_vol(fullpath); | |
1214 | if (IS_ERR(vi)) { | |
1215 | rc = PTR_ERR(vi); | |
1216 | goto out; | |
1217 | } | |
1218 | ||
1219 | cifs_dbg(FYI, "%s: updating volume info\n", __func__); | |
1220 | memcpy(&vi->vi_vol.dstaddr, &server->dstaddr, | |
1221 | sizeof(vi->vi_vol.dstaddr)); | |
1222 | rc = 0; | |
1223 | ||
1224 | out: | |
1225 | mutex_unlock(&dfs_cache.dc_lock); | |
1226 | return rc; | |
1227 | } | |
1228 | ||
1229 | /** | |
1230 | * dfs_cache_del_vol - remove volume info in DFS cache during umount() | |
1231 | * | |
1232 | * @fullpath: fullpath to look up in volume list. | |
1233 | */ | |
1234 | void dfs_cache_del_vol(const char *fullpath) | |
1235 | { | |
1236 | struct dfs_cache_vol_info *vi; | |
1237 | ||
1238 | if (!fullpath || !*fullpath) | |
1239 | return; | |
1240 | ||
1241 | cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath); | |
1242 | ||
1243 | mutex_lock(&dfs_cache.dc_lock); | |
1244 | vi = find_vol(fullpath); | |
1245 | if (!IS_ERR(vi)) | |
1246 | free_vol(vi); | |
1247 | mutex_unlock(&dfs_cache.dc_lock); | |
1248 | } | |
1249 | ||
1250 | /* Get all tcons that are within a DFS namespace and can be refreshed */ | |
1251 | static void get_tcons(struct TCP_Server_Info *server, struct list_head *head) | |
1252 | { | |
1253 | struct cifs_ses *ses; | |
1254 | struct cifs_tcon *tcon; | |
1255 | ||
1256 | INIT_LIST_HEAD(head); | |
1257 | ||
1258 | spin_lock(&cifs_tcp_ses_lock); | |
1259 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { | |
1260 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { | |
1261 | if (!tcon->need_reconnect && !tcon->need_reopen_files && | |
1262 | tcon->dfs_path) { | |
1263 | tcon->tc_count++; | |
1264 | list_add_tail(&tcon->ulist, head); | |
1265 | } | |
1266 | } | |
1267 | if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect && | |
1268 | ses->tcon_ipc->dfs_path) { | |
1269 | list_add_tail(&ses->tcon_ipc->ulist, head); | |
1270 | } | |
1271 | } | |
1272 | spin_unlock(&cifs_tcp_ses_lock); | |
1273 | } | |
1274 | ||
1275 | /* Refresh DFS cache entry from a given tcon */ | |
1276 | static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon) | |
1277 | { | |
1278 | int rc = 0; | |
1279 | unsigned int xid; | |
1280 | char *path, *npath; | |
1281 | unsigned int h; | |
1282 | struct dfs_cache_entry *ce; | |
1283 | struct dfs_info3_param *refs = NULL; | |
1284 | int numrefs = 0; | |
1285 | ||
1286 | xid = get_xid(); | |
1287 | ||
1288 | path = tcon->dfs_path + 1; | |
1289 | ||
1290 | rc = get_normalized_path(path, &npath); | |
1291 | if (rc) | |
1292 | goto out; | |
1293 | ||
1294 | mutex_lock(&dfs_cache_list_lock); | |
1295 | ce = find_cache_entry(npath, &h); | |
1296 | mutex_unlock(&dfs_cache_list_lock); | |
1297 | ||
1298 | if (IS_ERR(ce)) { | |
1299 | rc = PTR_ERR(ce); | |
1300 | goto out; | |
1301 | } | |
1302 | ||
1303 | if (!cache_entry_expired(ce)) | |
1304 | goto out; | |
1305 | ||
1306 | if (unlikely(!tcon->ses->server->ops->get_dfs_refer)) { | |
1307 | rc = -EOPNOTSUPP; | |
1308 | } else { | |
1309 | rc = tcon->ses->server->ops->get_dfs_refer(xid, tcon->ses, path, | |
1310 | &refs, &numrefs, | |
1311 | dc->dc_nlsc, | |
1312 | tcon->remap); | |
1313 | if (!rc) { | |
1314 | mutex_lock(&dfs_cache_list_lock); | |
1315 | ce = __update_cache_entry(npath, refs, numrefs); | |
1316 | mutex_unlock(&dfs_cache_list_lock); | |
1317 | dump_refs(refs, numrefs); | |
1318 | free_dfs_info_array(refs, numrefs); | |
1319 | if (IS_ERR(ce)) | |
1320 | rc = PTR_ERR(ce); | |
1321 | } | |
1322 | } | |
1323 | if (rc) | |
1324 | cifs_dbg(FYI, "%s: failed to update expired entry\n", __func__); | |
1325 | out: | |
1326 | free_xid(xid); | |
1327 | free_normalized_path(path, npath); | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * Worker that will refresh DFS cache based on lowest TTL value from a DFS | |
1332 | * referral. | |
1333 | * | |
1334 | * FIXME: ensure that all requests are sent to DFS root for refreshing the | |
1335 | * cache. | |
1336 | */ | |
1337 | static void refresh_cache_worker(struct work_struct *work) | |
1338 | { | |
1339 | struct dfs_cache *dc = container_of(work, struct dfs_cache, | |
1340 | dc_refresh.work); | |
1341 | struct dfs_cache_vol_info *vi; | |
1342 | struct TCP_Server_Info *server; | |
1343 | LIST_HEAD(list); | |
1344 | struct cifs_tcon *tcon, *ntcon; | |
1345 | ||
1346 | mutex_lock(&dc->dc_lock); | |
1347 | ||
1348 | list_for_each_entry(vi, &dc->dc_vol_list, vi_list) { | |
1349 | server = cifs_find_tcp_session(&vi->vi_vol); | |
1350 | if (IS_ERR_OR_NULL(server)) | |
1351 | continue; | |
1352 | if (server->tcpStatus != CifsGood) | |
1353 | goto next; | |
1354 | get_tcons(server, &list); | |
1355 | list_for_each_entry_safe(tcon, ntcon, &list, ulist) { | |
1356 | do_refresh_tcon(dc, tcon); | |
1357 | list_del_init(&tcon->ulist); | |
1358 | cifs_put_tcon(tcon); | |
1359 | } | |
1360 | next: | |
1361 | cifs_put_tcp_session(server, 0); | |
1362 | } | |
1363 | queue_delayed_work(cifsiod_wq, &dc->dc_refresh, dc->dc_ttl * HZ); | |
1364 | mutex_unlock(&dc->dc_lock); | |
1365 | } |