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