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