David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* AFS server record management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 3 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/slab.h> |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 14 | #include "afs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "internal.h" |
| 16 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 17 | static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */ |
| 18 | static unsigned afs_server_update_delay = 30; /* Time till VLDB recheck in secs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 20 | static void afs_inc_servers_outstanding(struct afs_net *net) |
| 21 | { |
| 22 | atomic_inc(&net->servers_outstanding); |
| 23 | } |
| 24 | |
| 25 | static void afs_dec_servers_outstanding(struct afs_net *net) |
| 26 | { |
| 27 | if (atomic_dec_and_test(&net->servers_outstanding)) |
| 28 | wake_up_atomic_t(&net->servers_outstanding); |
| 29 | } |
| 30 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 31 | /* |
| 32 | * Find a server by one of its addresses. |
| 33 | */ |
| 34 | struct afs_server *afs_find_server(struct afs_net *net, |
| 35 | const struct sockaddr_rxrpc *srx) |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 36 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 37 | const struct sockaddr_in6 *a = &srx->transport.sin6, *b; |
| 38 | const struct afs_addr_list *alist; |
| 39 | struct afs_server *server = NULL; |
| 40 | unsigned int i; |
| 41 | bool ipv6 = true; |
| 42 | int seq = 0, diff; |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 43 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 44 | if (srx->transport.sin6.sin6_addr.s6_addr32[0] == 0 || |
| 45 | srx->transport.sin6.sin6_addr.s6_addr32[1] == 0 || |
| 46 | srx->transport.sin6.sin6_addr.s6_addr32[2] == htonl(0xffff)) |
| 47 | ipv6 = false; |
| 48 | |
| 49 | rcu_read_lock(); |
| 50 | |
| 51 | do { |
| 52 | if (server) |
| 53 | afs_put_server(net, server); |
| 54 | server = NULL; |
| 55 | read_seqbegin_or_lock(&net->fs_addr_lock, &seq); |
| 56 | |
| 57 | if (ipv6) { |
| 58 | hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) { |
| 59 | alist = rcu_dereference(server->addresses); |
| 60 | for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) { |
| 61 | b = &alist->addrs[i].transport.sin6; |
| 62 | diff = (u16)a->sin6_port - (u16)b->sin6_port; |
| 63 | if (diff == 0) |
| 64 | diff = memcmp(&a->sin6_addr, |
| 65 | &b->sin6_addr, |
| 66 | sizeof(struct in6_addr)); |
| 67 | if (diff == 0) |
| 68 | goto found; |
| 69 | if (diff < 0) { |
| 70 | // TODO: Sort the list |
| 71 | //if (i == alist->nr_ipv4) |
| 72 | // goto not_found; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } else { |
| 78 | hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) { |
| 79 | alist = rcu_dereference(server->addresses); |
| 80 | for (i = 0; i < alist->nr_ipv4; i++) { |
| 81 | b = &alist->addrs[i].transport.sin6; |
| 82 | diff = (u16)a->sin6_port - (u16)b->sin6_port; |
| 83 | if (diff == 0) |
| 84 | diff = ((u32)a->sin6_addr.s6_addr32[3] - |
| 85 | (u32)b->sin6_addr.s6_addr32[3]); |
| 86 | if (diff == 0) |
| 87 | goto found; |
| 88 | if (diff < 0) { |
| 89 | // TODO: Sort the list |
| 90 | //if (i == 0) |
| 91 | // goto not_found; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //not_found: |
| 99 | server = NULL; |
| 100 | found: |
| 101 | if (server && !atomic_inc_not_zero(&server->usage)) |
| 102 | server = NULL; |
| 103 | |
| 104 | } while (need_seqretry(&net->fs_addr_lock, seq)); |
| 105 | |
| 106 | done_seqretry(&net->fs_addr_lock, seq); |
| 107 | |
| 108 | rcu_read_unlock(); |
| 109 | return server; |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 113 | * Look up a server by its UUID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 115 | struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 117 | struct afs_server *server = NULL; |
| 118 | struct rb_node *p; |
| 119 | int diff, seq = 0; |
| 120 | |
| 121 | _enter("%pU", uuid); |
| 122 | |
| 123 | do { |
| 124 | /* Unfortunately, rbtree walking doesn't give reliable results |
| 125 | * under just the RCU read lock, so we have to check for |
| 126 | * changes. |
| 127 | */ |
| 128 | if (server) |
| 129 | afs_put_server(net, server); |
| 130 | server = NULL; |
| 131 | |
| 132 | read_seqbegin_or_lock(&net->fs_lock, &seq); |
| 133 | |
| 134 | p = net->fs_servers.rb_node; |
| 135 | while (p) { |
| 136 | server = rb_entry(p, struct afs_server, uuid_rb); |
| 137 | |
| 138 | diff = memcmp(uuid, &server->uuid, sizeof(*uuid)); |
| 139 | if (diff < 0) { |
| 140 | p = p->rb_left; |
| 141 | } else if (diff > 0) { |
| 142 | p = p->rb_right; |
| 143 | } else { |
| 144 | afs_get_server(server); |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | server = NULL; |
| 149 | } |
| 150 | } while (need_seqretry(&net->fs_lock, seq)); |
| 151 | |
| 152 | done_seqretry(&net->fs_lock, seq); |
| 153 | |
| 154 | _leave(" = %p", server); |
| 155 | return server; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Install a server record in the namespace tree |
| 160 | */ |
| 161 | static struct afs_server *afs_install_server(struct afs_net *net, |
| 162 | struct afs_server *candidate) |
| 163 | { |
| 164 | const struct afs_addr_list *alist; |
| 165 | struct afs_server *server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 166 | struct rb_node **pp, *p; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 167 | int ret = -EEXIST, diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 169 | _enter("%p", candidate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 171 | write_seqlock(&net->fs_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 172 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 173 | /* Firstly install the server in the UUID lookup tree */ |
| 174 | pp = &net->fs_servers.rb_node; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 175 | p = NULL; |
| 176 | while (*pp) { |
| 177 | p = *pp; |
| 178 | _debug("- consider %p", p); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 179 | server = rb_entry(p, struct afs_server, uuid_rb); |
| 180 | diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t)); |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 181 | if (diff < 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 182 | pp = &(*pp)->rb_left; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 183 | else if (diff > 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 184 | pp = &(*pp)->rb_right; |
| 185 | else |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 186 | goto exists; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 189 | server = candidate; |
| 190 | rb_link_node(&server->uuid_rb, p, pp); |
| 191 | rb_insert_color(&server->uuid_rb, &net->fs_servers); |
| 192 | hlist_add_head_rcu(&server->proc_link, &net->fs_proc); |
| 193 | |
| 194 | write_seqlock(&net->fs_addr_lock); |
| 195 | alist = rcu_dereference_protected(server->addresses, |
| 196 | lockdep_is_held(&net->fs_addr_lock.lock)); |
| 197 | |
| 198 | /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install |
| 199 | * it in the IPv4 and/or IPv6 reverse-map lists. |
| 200 | * |
| 201 | * TODO: For speed we want to use something other than a flat list |
| 202 | * here; even sorting the list in terms of lowest address would help a |
| 203 | * bit, but anything we might want to do gets messy and memory |
| 204 | * intensive. |
| 205 | */ |
| 206 | if (alist->nr_ipv4 > 0) |
| 207 | hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4); |
| 208 | if (alist->nr_addrs > alist->nr_ipv4) |
| 209 | hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6); |
| 210 | |
| 211 | write_sequnlock(&net->fs_addr_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 212 | ret = 0; |
| 213 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 214 | exists: |
| 215 | afs_get_server(server); |
| 216 | write_sequnlock(&net->fs_lock); |
| 217 | return server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
| 221 | * allocate a new server record |
| 222 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 223 | static struct afs_server *afs_alloc_server(struct afs_net *net, |
| 224 | const uuid_t *uuid, |
| 225 | struct afs_addr_list *alist) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 226 | { |
| 227 | struct afs_server *server; |
| 228 | |
| 229 | _enter(""); |
| 230 | |
Yan Burman | b593e48 | 2006-12-06 20:40:32 -0800 | [diff] [blame] | 231 | server = kzalloc(sizeof(struct afs_server), GFP_KERNEL); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 232 | if (!server) |
| 233 | goto enomem; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 234 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 235 | atomic_set(&server->usage, 1); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 236 | RCU_INIT_POINTER(server->addresses, alist); |
| 237 | server->addr_version = alist->version; |
| 238 | server->uuid = *uuid; |
| 239 | server->flags = (1UL << AFS_SERVER_FL_NEW); |
| 240 | server->update_at = ktime_get_real_seconds() + afs_server_update_delay; |
| 241 | rwlock_init(&server->fs_lock); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 242 | INIT_LIST_HEAD(&server->cb_interests); |
| 243 | rwlock_init(&server->cb_break_lock); |
| 244 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 245 | afs_inc_servers_outstanding(net); |
| 246 | _leave(" = %p", server); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 247 | return server; |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 248 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 249 | enomem: |
| 250 | _leave(" = NULL [nomem]"); |
| 251 | return NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 255 | * Look up an address record for a server |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 256 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 257 | static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell, |
| 258 | struct key *key, const uuid_t *uuid) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 259 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 260 | struct afs_addr_cursor ac; |
| 261 | struct afs_addr_list *alist; |
| 262 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 263 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 264 | ret = afs_set_vl_cursor(&ac, cell); |
| 265 | if (ret < 0) |
| 266 | return ERR_PTR(ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 267 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 268 | while (afs_iterate_addresses(&ac)) { |
| 269 | alist = afs_vl_get_addrs_u(cell->net, &ac, key, uuid); |
| 270 | switch (ac.error) { |
| 271 | case 0: |
| 272 | afs_end_cursor(&ac); |
| 273 | return alist; |
| 274 | case -ECONNABORTED: |
| 275 | ac.error = afs_abort_to_error(ac.abort_code); |
| 276 | goto error; |
| 277 | case -ENOMEM: |
| 278 | case -ENONET: |
| 279 | goto error; |
| 280 | case -ENETUNREACH: |
| 281 | case -EHOSTUNREACH: |
| 282 | case -ECONNREFUSED: |
| 283 | break; |
| 284 | default: |
| 285 | ac.error = -EIO; |
| 286 | goto error; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 290 | error: |
| 291 | return ERR_PTR(afs_end_cursor(&ac)); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Get or create a fileserver record. |
| 296 | */ |
| 297 | struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key, |
| 298 | const uuid_t *uuid) |
| 299 | { |
| 300 | struct afs_addr_list *alist; |
| 301 | struct afs_server *server, *candidate; |
| 302 | |
| 303 | _enter("%p,%pU", cell->net, uuid); |
| 304 | |
| 305 | server = afs_find_server_by_uuid(cell->net, uuid); |
| 306 | if (server) |
| 307 | return server; |
| 308 | |
| 309 | alist = afs_vl_lookup_addrs(cell, key, uuid); |
| 310 | if (IS_ERR(alist)) |
| 311 | return ERR_CAST(alist); |
| 312 | |
| 313 | candidate = afs_alloc_server(cell->net, uuid, alist); |
| 314 | if (!candidate) { |
| 315 | afs_put_addrlist(alist); |
| 316 | return ERR_PTR(-ENOMEM); |
| 317 | } |
| 318 | |
| 319 | server = afs_install_server(cell->net, candidate); |
| 320 | if (server != candidate) { |
| 321 | afs_put_addrlist(alist); |
| 322 | kfree(candidate); |
| 323 | } |
| 324 | |
| 325 | _leave(" = %p{%d}", server, atomic_read(&server->usage)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 326 | return server; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 329 | /* |
| 330 | * Set the server timer to fire after a given delay, assuming it's not already |
| 331 | * set for an earlier time. |
| 332 | */ |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 333 | static void afs_set_server_timer(struct afs_net *net, time64_t delay) |
| 334 | { |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 335 | if (net->live) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 336 | afs_inc_servers_outstanding(net); |
| 337 | if (timer_reduce(&net->fs_timer, jiffies + delay * HZ)) |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 338 | afs_dec_servers_outstanding(net); |
| 339 | } |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 343 | * Server management timer. We have an increment on fs_outstanding that we |
| 344 | * need to pass along to the work item. |
| 345 | */ |
| 346 | void afs_servers_timer(struct timer_list *timer) |
| 347 | { |
| 348 | struct afs_net *net = container_of(timer, struct afs_net, fs_timer); |
| 349 | |
| 350 | _enter(""); |
| 351 | if (!queue_work(afs_wq, &net->fs_manager)) |
| 352 | afs_dec_servers_outstanding(net); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Release a reference on a server record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | */ |
David Howells | 9ed900b | 2017-11-02 15:27:46 +0000 | [diff] [blame] | 358 | void afs_put_server(struct afs_net *net, struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 360 | unsigned int usage; |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (!server) |
| 363 | return; |
| 364 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 365 | server->put_time = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 367 | usage = atomic_dec_return(&server->usage); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 368 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 369 | _enter("{%u}", usage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 371 | if (likely(usage > 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 374 | afs_set_server_timer(net, afs_server_gc_delay); |
| 375 | } |
| 376 | |
| 377 | static void afs_server_rcu(struct rcu_head *rcu) |
| 378 | { |
| 379 | struct afs_server *server = container_of(rcu, struct afs_server, rcu); |
| 380 | |
| 381 | afs_put_addrlist(server->addresses); |
| 382 | kfree(server); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 386 | * destroy a dead server |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | */ |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 388 | static void afs_destroy_server(struct afs_net *net, struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 390 | struct afs_addr_list *alist = server->addresses; |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 391 | struct afs_addr_cursor ac = { |
| 392 | .alist = alist, |
| 393 | .addr = &alist->addrs[0], |
| 394 | .start = alist->index, |
| 395 | .index = alist->index, |
| 396 | .error = 0, |
| 397 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | _enter("%p", server); |
| 399 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 400 | afs_fs_give_up_all_callbacks(net, server, &ac, NULL); |
| 401 | call_rcu(&server->rcu, afs_server_rcu); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 402 | afs_dec_servers_outstanding(net); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 406 | * Garbage collect any expired servers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 408 | static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 410 | struct afs_server *server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 411 | bool deleted; |
| 412 | int usage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 414 | while ((server = gc_list)) { |
| 415 | gc_list = server->gc_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 417 | write_seqlock(&net->fs_lock); |
| 418 | usage = 1; |
| 419 | deleted = atomic_try_cmpxchg(&server->usage, &usage, 0); |
| 420 | if (deleted) { |
| 421 | rb_erase(&server->uuid_rb, &net->fs_servers); |
| 422 | hlist_del_rcu(&server->proc_link); |
| 423 | } |
| 424 | write_sequnlock(&net->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 426 | if (deleted) |
| 427 | afs_destroy_server(net, server); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Manage the records of servers known to be within a network namespace. This |
| 433 | * includes garbage collecting unused servers. |
| 434 | * |
| 435 | * Note also that we were given an increment on net->servers_outstanding by |
| 436 | * whoever queued us that we need to deal with before returning. |
| 437 | */ |
| 438 | void afs_manage_servers(struct work_struct *work) |
| 439 | { |
| 440 | struct afs_net *net = container_of(work, struct afs_net, fs_manager); |
| 441 | struct afs_server *gc_list = NULL; |
| 442 | struct rb_node *cursor; |
| 443 | time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX; |
| 444 | bool purging = !net->live; |
| 445 | |
| 446 | _enter(""); |
| 447 | |
| 448 | /* Trawl the server list looking for servers that have expired from |
| 449 | * lack of use. |
| 450 | */ |
| 451 | read_seqlock_excl(&net->fs_lock); |
| 452 | |
| 453 | for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) { |
| 454 | struct afs_server *server = |
| 455 | rb_entry(cursor, struct afs_server, uuid_rb); |
| 456 | int usage = atomic_read(&server->usage); |
| 457 | |
| 458 | _debug("manage %pU %u", &server->uuid, usage); |
| 459 | |
| 460 | ASSERTCMP(usage, >=, 1); |
| 461 | ASSERTIFCMP(purging, usage, ==, 1); |
| 462 | |
| 463 | if (usage == 1) { |
| 464 | time64_t expire_at = server->put_time; |
| 465 | |
| 466 | if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) && |
| 467 | !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags)) |
| 468 | expire_at += afs_server_gc_delay; |
| 469 | if (purging || expire_at <= now) { |
| 470 | server->gc_next = gc_list; |
| 471 | gc_list = server; |
| 472 | } else if (expire_at < next_manage) { |
| 473 | next_manage = expire_at; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 478 | read_sequnlock_excl(&net->fs_lock); |
| 479 | |
| 480 | /* Update the timer on the way out. We have to pass an increment on |
| 481 | * servers_outstanding in the namespace that we are in to the timer or |
| 482 | * the work scheduler. |
| 483 | */ |
| 484 | if (!purging && next_manage < TIME64_MAX) { |
| 485 | now = ktime_get_real_seconds(); |
| 486 | |
| 487 | if (next_manage - now <= 0) { |
| 488 | if (queue_work(afs_wq, &net->fs_manager)) |
| 489 | afs_inc_servers_outstanding(net); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 490 | } else { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 491 | afs_set_server_timer(net, next_manage - now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 495 | afs_gc_servers(net, gc_list); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 496 | |
| 497 | afs_dec_servers_outstanding(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 498 | _leave(" [%d]", atomic_read(&net->servers_outstanding)); |
| 499 | } |
| 500 | |
| 501 | static void afs_queue_server_manager(struct afs_net *net) |
| 502 | { |
| 503 | afs_inc_servers_outstanding(net); |
| 504 | if (!queue_work(afs_wq, &net->fs_manager)) |
| 505 | afs_dec_servers_outstanding(net); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 509 | * Purge list of servers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 511 | void afs_purge_servers(struct afs_net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 513 | _enter(""); |
| 514 | |
| 515 | if (del_timer_sync(&net->fs_timer)) |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 516 | atomic_dec(&net->servers_outstanding); |
| 517 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 518 | afs_queue_server_manager(net); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 519 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 520 | _debug("wait"); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 521 | wait_on_atomic_t(&net->servers_outstanding, atomic_t_wait, |
| 522 | TASK_UNINTERRUPTIBLE); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 523 | _leave(""); |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Probe a fileserver to find its capabilities. |
| 528 | * |
| 529 | * TODO: Try service upgrade. |
| 530 | */ |
| 531 | static bool afs_do_probe_fileserver(struct afs_fs_cursor *fc) |
| 532 | { |
| 533 | _enter(""); |
| 534 | |
| 535 | fc->ac.addr = NULL; |
| 536 | fc->ac.start = READ_ONCE(fc->ac.alist->index); |
| 537 | fc->ac.index = fc->ac.start; |
| 538 | fc->ac.error = 0; |
| 539 | fc->ac.begun = false; |
| 540 | |
| 541 | while (afs_iterate_addresses(&fc->ac)) { |
| 542 | afs_fs_get_capabilities(afs_v2net(fc->vnode), fc->cbi->server, |
| 543 | &fc->ac, fc->key); |
| 544 | switch (fc->ac.error) { |
| 545 | case 0: |
| 546 | afs_end_cursor(&fc->ac); |
| 547 | set_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags); |
| 548 | return true; |
| 549 | case -ECONNABORTED: |
| 550 | fc->ac.error = afs_abort_to_error(fc->ac.abort_code); |
| 551 | goto error; |
| 552 | case -ENOMEM: |
| 553 | case -ENONET: |
| 554 | goto error; |
| 555 | case -ENETUNREACH: |
| 556 | case -EHOSTUNREACH: |
| 557 | case -ECONNREFUSED: |
| 558 | case -ETIMEDOUT: |
| 559 | case -ETIME: |
| 560 | break; |
| 561 | default: |
| 562 | fc->ac.error = -EIO; |
| 563 | goto error; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | error: |
| 568 | afs_end_cursor(&fc->ac); |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If we haven't already, try probing the fileserver to get its capabilities. |
| 574 | * We try not to instigate parallel probes, but it's possible that the parallel |
| 575 | * probes will fail due to authentication failure when ours would succeed. |
| 576 | * |
| 577 | * TODO: Try sending an anonymous probe if an authenticated probe fails. |
| 578 | */ |
| 579 | bool afs_probe_fileserver(struct afs_fs_cursor *fc) |
| 580 | { |
| 581 | bool success; |
| 582 | int ret, retries = 0; |
| 583 | |
| 584 | _enter(""); |
| 585 | |
| 586 | retry: |
| 587 | if (test_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags)) { |
| 588 | _leave(" = t"); |
| 589 | return true; |
| 590 | } |
| 591 | |
| 592 | if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags)) { |
| 593 | success = afs_do_probe_fileserver(fc); |
| 594 | clear_bit_unlock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags); |
| 595 | wake_up_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING); |
| 596 | _leave(" = t"); |
| 597 | return success; |
| 598 | } |
| 599 | |
| 600 | _debug("wait"); |
| 601 | ret = wait_on_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING, |
| 602 | TASK_INTERRUPTIBLE); |
| 603 | if (ret == -ERESTARTSYS) { |
| 604 | fc->ac.error = ret; |
| 605 | _leave(" = f [%d]", ret); |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | retries++; |
| 610 | if (retries == 4) { |
| 611 | fc->ac.error = -ESTALE; |
| 612 | _leave(" = f [stale]"); |
| 613 | return false; |
| 614 | } |
| 615 | _debug("retry"); |
| 616 | goto retry; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Get an update for a server's address list. |
| 621 | */ |
| 622 | static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server) |
| 623 | { |
| 624 | struct afs_addr_list *alist, *discard; |
| 625 | |
| 626 | _enter(""); |
| 627 | |
| 628 | alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key, |
| 629 | &server->uuid); |
| 630 | if (IS_ERR(alist)) { |
| 631 | fc->ac.error = PTR_ERR(alist); |
| 632 | _leave(" = f [%d]", fc->ac.error); |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | discard = alist; |
| 637 | if (server->addr_version != alist->version) { |
| 638 | write_lock(&server->fs_lock); |
| 639 | discard = rcu_dereference_protected(server->addresses, |
| 640 | lockdep_is_held(&server->fs_lock)); |
| 641 | rcu_assign_pointer(server->addresses, alist); |
| 642 | server->addr_version = alist->version; |
| 643 | write_unlock(&server->fs_lock); |
| 644 | } |
| 645 | |
| 646 | server->update_at = ktime_get_real_seconds() + afs_server_update_delay; |
| 647 | afs_put_addrlist(discard); |
| 648 | _leave(" = t"); |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * See if a server's address list needs updating. |
| 654 | */ |
| 655 | bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server) |
| 656 | { |
| 657 | time64_t now = ktime_get_real_seconds(); |
| 658 | long diff; |
| 659 | bool success; |
| 660 | int ret, retries = 0; |
| 661 | |
| 662 | _enter(""); |
| 663 | |
| 664 | ASSERT(server); |
| 665 | |
| 666 | retry: |
| 667 | diff = READ_ONCE(server->update_at) - now; |
| 668 | if (diff > 0) { |
| 669 | _leave(" = t [not now %ld]", diff); |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) { |
| 674 | success = afs_update_server_record(fc, server); |
| 675 | clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags); |
| 676 | wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING); |
| 677 | _leave(" = %d", success); |
| 678 | return success; |
| 679 | } |
| 680 | |
| 681 | ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING, |
| 682 | TASK_INTERRUPTIBLE); |
| 683 | if (ret == -ERESTARTSYS) { |
| 684 | fc->ac.error = ret; |
| 685 | _leave(" = f [intr]"); |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | retries++; |
| 690 | if (retries == 4) { |
| 691 | _leave(" = f [stale]"); |
| 692 | ret = -ESTALE; |
| 693 | return false; |
| 694 | } |
| 695 | goto retry; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 696 | } |