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