Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 2 | /* AFS server record management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 4 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/slab.h> |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 10 | #include "afs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "internal.h" |
David Howells | 30062bd | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 12 | #include "protocol_yfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 14 | static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */ |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 15 | static atomic_t afs_server_debug_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 17 | static struct afs_server *afs_maybe_use_server(struct afs_server *, |
| 18 | enum afs_server_trace); |
| 19 | static void __afs_put_server(struct afs_net *, struct afs_server *); |
| 20 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Find a server by one of its addresses. |
| 23 | */ |
| 24 | struct afs_server *afs_find_server(struct afs_net *net, |
| 25 | const struct sockaddr_rxrpc *srx) |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 26 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 27 | const struct afs_addr_list *alist; |
| 28 | struct afs_server *server = NULL; |
| 29 | unsigned int i; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 30 | int seq = 0, diff; |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 31 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 32 | rcu_read_lock(); |
| 33 | |
| 34 | do { |
| 35 | if (server) |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 36 | afs_unuse_server_notime(net, server, afs_server_trace_put_find_rsq); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 37 | server = NULL; |
| 38 | read_seqbegin_or_lock(&net->fs_addr_lock, &seq); |
| 39 | |
Marc Dionne | 9bd0160 | 2019-12-09 15:04:43 +0000 | [diff] [blame] | 40 | if (srx->transport.family == AF_INET6) { |
| 41 | const struct sockaddr_in6 *a = &srx->transport.sin6, *b; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 42 | hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) { |
| 43 | alist = rcu_dereference(server->addresses); |
| 44 | for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) { |
| 45 | b = &alist->addrs[i].transport.sin6; |
David Howells | fe342cf | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 46 | diff = ((u16 __force)a->sin6_port - |
| 47 | (u16 __force)b->sin6_port); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 48 | if (diff == 0) |
| 49 | diff = memcmp(&a->sin6_addr, |
| 50 | &b->sin6_addr, |
| 51 | sizeof(struct in6_addr)); |
| 52 | if (diff == 0) |
| 53 | goto found; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } else { |
Marc Dionne | 9bd0160 | 2019-12-09 15:04:43 +0000 | [diff] [blame] | 57 | const struct sockaddr_in *a = &srx->transport.sin, *b; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 58 | hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) { |
| 59 | alist = rcu_dereference(server->addresses); |
| 60 | for (i = 0; i < alist->nr_ipv4; i++) { |
Marc Dionne | 9bd0160 | 2019-12-09 15:04:43 +0000 | [diff] [blame] | 61 | b = &alist->addrs[i].transport.sin; |
| 62 | diff = ((u16 __force)a->sin_port - |
| 63 | (u16 __force)b->sin_port); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 64 | if (diff == 0) |
Marc Dionne | 9bd0160 | 2019-12-09 15:04:43 +0000 | [diff] [blame] | 65 | diff = ((u32 __force)a->sin_addr.s_addr - |
| 66 | (u32 __force)b->sin_addr.s_addr); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 67 | if (diff == 0) |
| 68 | goto found; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 73 | server = NULL; |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 74 | continue; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 75 | found: |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 76 | server = afs_maybe_use_server(server, afs_server_trace_get_by_addr); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 77 | |
| 78 | } while (need_seqretry(&net->fs_addr_lock, seq)); |
| 79 | |
| 80 | done_seqretry(&net->fs_addr_lock, seq); |
| 81 | |
| 82 | rcu_read_unlock(); |
| 83 | return server; |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 87 | * Look up a server by its UUID and mark it active. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 89 | 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] | 90 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 91 | struct afs_server *server = NULL; |
| 92 | struct rb_node *p; |
| 93 | int diff, seq = 0; |
| 94 | |
| 95 | _enter("%pU", uuid); |
| 96 | |
| 97 | do { |
| 98 | /* Unfortunately, rbtree walking doesn't give reliable results |
| 99 | * under just the RCU read lock, so we have to check for |
| 100 | * changes. |
| 101 | */ |
| 102 | if (server) |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 103 | afs_unuse_server(net, server, afs_server_trace_put_uuid_rsq); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 104 | server = NULL; |
| 105 | |
| 106 | read_seqbegin_or_lock(&net->fs_lock, &seq); |
| 107 | |
| 108 | p = net->fs_servers.rb_node; |
| 109 | while (p) { |
| 110 | server = rb_entry(p, struct afs_server, uuid_rb); |
| 111 | |
| 112 | diff = memcmp(uuid, &server->uuid, sizeof(*uuid)); |
| 113 | if (diff < 0) { |
| 114 | p = p->rb_left; |
| 115 | } else if (diff > 0) { |
| 116 | p = p->rb_right; |
| 117 | } else { |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 118 | afs_use_server(server, afs_server_trace_get_by_uuid); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | |
| 122 | server = NULL; |
| 123 | } |
| 124 | } while (need_seqretry(&net->fs_lock, seq)); |
| 125 | |
| 126 | done_seqretry(&net->fs_lock, seq); |
| 127 | |
| 128 | _leave(" = %p", server); |
| 129 | return server; |
| 130 | } |
| 131 | |
| 132 | /* |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 133 | * Install a server record in the namespace tree. If there's a clash, we stick |
| 134 | * it into a list anchored on whichever afs_server struct is actually in the |
| 135 | * tree. |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 136 | */ |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 137 | static struct afs_server *afs_install_server(struct afs_cell *cell, |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 138 | struct afs_server *candidate) |
| 139 | { |
| 140 | const struct afs_addr_list *alist; |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 141 | struct afs_server *server, *next; |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 142 | struct afs_net *net = cell->net; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 143 | struct rb_node **pp, *p; |
zhengbin | 4fe171b | 2019-11-21 09:12:18 +0000 | [diff] [blame] | 144 | int diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 146 | _enter("%p", candidate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 148 | write_seqlock(&net->fs_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 149 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 150 | /* Firstly install the server in the UUID lookup tree */ |
| 151 | pp = &net->fs_servers.rb_node; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 152 | p = NULL; |
| 153 | while (*pp) { |
| 154 | p = *pp; |
| 155 | _debug("- consider %p", p); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 156 | server = rb_entry(p, struct afs_server, uuid_rb); |
| 157 | diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t)); |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 158 | if (diff < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 159 | pp = &(*pp)->rb_left; |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 160 | } else if (diff > 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 161 | pp = &(*pp)->rb_right; |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 162 | } else { |
| 163 | if (server->cell == cell) |
| 164 | goto exists; |
| 165 | |
| 166 | /* We have the same UUID representing servers in |
| 167 | * different cells. Append the new server to the list. |
| 168 | */ |
| 169 | for (;;) { |
| 170 | next = rcu_dereference_protected( |
| 171 | server->uuid_next, |
| 172 | lockdep_is_held(&net->fs_lock.lock)); |
| 173 | if (!next) |
| 174 | break; |
| 175 | server = next; |
| 176 | } |
| 177 | rcu_assign_pointer(server->uuid_next, candidate); |
| 178 | candidate->uuid_prev = server; |
| 179 | server = candidate; |
| 180 | goto added_dup; |
| 181 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 182 | } |
| 183 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 184 | server = candidate; |
| 185 | rb_link_node(&server->uuid_rb, p, pp); |
| 186 | rb_insert_color(&server->uuid_rb, &net->fs_servers); |
| 187 | hlist_add_head_rcu(&server->proc_link, &net->fs_proc); |
| 188 | |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 189 | added_dup: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 190 | write_seqlock(&net->fs_addr_lock); |
| 191 | alist = rcu_dereference_protected(server->addresses, |
| 192 | lockdep_is_held(&net->fs_addr_lock.lock)); |
| 193 | |
| 194 | /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install |
| 195 | * it in the IPv4 and/or IPv6 reverse-map lists. |
| 196 | * |
| 197 | * TODO: For speed we want to use something other than a flat list |
| 198 | * here; even sorting the list in terms of lowest address would help a |
| 199 | * bit, but anything we might want to do gets messy and memory |
| 200 | * intensive. |
| 201 | */ |
| 202 | if (alist->nr_ipv4 > 0) |
| 203 | hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4); |
| 204 | if (alist->nr_addrs > alist->nr_ipv4) |
| 205 | hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6); |
| 206 | |
| 207 | write_sequnlock(&net->fs_addr_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 208 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 209 | exists: |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 210 | afs_get_server(server, afs_server_trace_get_install); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 211 | write_sequnlock(&net->fs_lock); |
| 212 | return server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 216 | * Allocate a new server record and mark it active. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 217 | */ |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 218 | static struct afs_server *afs_alloc_server(struct afs_cell *cell, |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 219 | const uuid_t *uuid, |
| 220 | struct afs_addr_list *alist) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 221 | { |
| 222 | struct afs_server *server; |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 223 | struct afs_net *net = cell->net; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 224 | |
| 225 | _enter(""); |
| 226 | |
Yan Burman | b593e48 | 2006-12-06 20:40:32 -0800 | [diff] [blame] | 227 | server = kzalloc(sizeof(struct afs_server), GFP_KERNEL); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 228 | if (!server) |
| 229 | goto enomem; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 230 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 231 | atomic_set(&server->ref, 1); |
| 232 | atomic_set(&server->active, 1); |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 233 | server->debug_id = atomic_inc_return(&afs_server_debug_id); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 234 | RCU_INIT_POINTER(server->addresses, alist); |
| 235 | server->addr_version = alist->version; |
| 236 | server->uuid = *uuid; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 237 | rwlock_init(&server->fs_lock); |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 238 | INIT_WORK(&server->initcb_work, afs_server_init_callback_work); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 239 | init_waitqueue_head(&server->probe_wq); |
David Howells | f6cbb36 | 2020-04-24 15:10:00 +0100 | [diff] [blame] | 240 | INIT_LIST_HEAD(&server->probe_link); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 241 | spin_lock_init(&server->probe_lock); |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 242 | server->cell = cell; |
David Howells | f3c130e | 2020-05-02 13:39:57 +0100 | [diff] [blame] | 243 | server->rtt = UINT_MAX; |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 244 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 245 | afs_inc_servers_outstanding(net); |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 246 | trace_afs_server(server, 1, 1, afs_server_trace_alloc); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 247 | _leave(" = %p", server); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 248 | return server; |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 249 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 250 | enomem: |
| 251 | _leave(" = NULL [nomem]"); |
| 252 | return NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 256 | * Look up an address record for a server |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 257 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 258 | static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell, |
| 259 | struct key *key, const uuid_t *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 | struct afs_vl_cursor vc; |
| 262 | struct afs_addr_list *alist = NULL; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 263 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 264 | |
David Howells | 0a5143f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 265 | ret = -ERESTARTSYS; |
| 266 | if (afs_begin_vlserver_operation(&vc, cell, key)) { |
| 267 | while (afs_select_vlserver(&vc)) { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 268 | if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags)) |
David Howells | 0a5143f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 269 | alist = afs_yfsvl_get_endpoints(&vc, uuid); |
| 270 | else |
| 271 | alist = afs_vl_get_addrs_u(&vc, uuid); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 272 | } |
David Howells | 0a5143f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 273 | |
| 274 | ret = afs_end_vlserver_operation(&vc); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 275 | } |
| 276 | |
David Howells | 0a5143f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 277 | return ret < 0 ? ERR_PTR(ret) : alist; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Get or create a fileserver record. |
| 282 | */ |
| 283 | struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key, |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 284 | const uuid_t *uuid, u32 addr_version) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 285 | { |
| 286 | struct afs_addr_list *alist; |
| 287 | struct afs_server *server, *candidate; |
| 288 | |
| 289 | _enter("%p,%pU", cell->net, uuid); |
| 290 | |
| 291 | server = afs_find_server_by_uuid(cell->net, uuid); |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 292 | if (server) { |
| 293 | if (server->addr_version != addr_version) |
| 294 | set_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 295 | return server; |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 296 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 297 | |
| 298 | alist = afs_vl_lookup_addrs(cell, key, uuid); |
| 299 | if (IS_ERR(alist)) |
| 300 | return ERR_CAST(alist); |
| 301 | |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 302 | candidate = afs_alloc_server(cell, uuid, alist); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 303 | if (!candidate) { |
| 304 | afs_put_addrlist(alist); |
| 305 | return ERR_PTR(-ENOMEM); |
| 306 | } |
| 307 | |
David Howells | 2032596 | 2020-04-30 01:03:49 +0100 | [diff] [blame] | 308 | server = afs_install_server(cell, candidate); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 309 | if (server != candidate) { |
| 310 | afs_put_addrlist(alist); |
| 311 | kfree(candidate); |
David Howells | f6cbb36 | 2020-04-24 15:10:00 +0100 | [diff] [blame] | 312 | } else { |
| 313 | /* Immediately dispatch an asynchronous probe to each interface |
| 314 | * on the fileserver. This will make sure the repeat-probing |
| 315 | * service is started. |
| 316 | */ |
| 317 | afs_fs_probe_fileserver(cell->net, server, key, true); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 318 | } |
| 319 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 320 | return server; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 323 | /* |
| 324 | * Set the server timer to fire after a given delay, assuming it's not already |
| 325 | * set for an earlier time. |
| 326 | */ |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 327 | static void afs_set_server_timer(struct afs_net *net, time64_t delay) |
| 328 | { |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 329 | if (net->live) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 330 | afs_inc_servers_outstanding(net); |
| 331 | if (timer_reduce(&net->fs_timer, jiffies + delay * HZ)) |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 332 | afs_dec_servers_outstanding(net); |
| 333 | } |
| 334 | } |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 337 | * Server management timer. We have an increment on fs_outstanding that we |
| 338 | * need to pass along to the work item. |
| 339 | */ |
| 340 | void afs_servers_timer(struct timer_list *timer) |
| 341 | { |
| 342 | struct afs_net *net = container_of(timer, struct afs_net, fs_timer); |
| 343 | |
| 344 | _enter(""); |
| 345 | if (!queue_work(afs_wq, &net->fs_manager)) |
| 346 | afs_dec_servers_outstanding(net); |
| 347 | } |
| 348 | |
| 349 | /* |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 350 | * Get a reference on a server object. |
| 351 | */ |
| 352 | struct afs_server *afs_get_server(struct afs_server *server, |
| 353 | enum afs_server_trace reason) |
| 354 | { |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 355 | unsigned int u = atomic_inc_return(&server->ref); |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 356 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 357 | trace_afs_server(server, u, atomic_read(&server->active), reason); |
| 358 | return server; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Try to get a reference on a server object. |
| 363 | */ |
| 364 | static struct afs_server *afs_maybe_use_server(struct afs_server *server, |
| 365 | enum afs_server_trace reason) |
| 366 | { |
| 367 | unsigned int r = atomic_fetch_add_unless(&server->ref, 1, 0); |
| 368 | unsigned int a; |
| 369 | |
| 370 | if (r == 0) |
| 371 | return NULL; |
| 372 | |
| 373 | a = atomic_inc_return(&server->active); |
| 374 | trace_afs_server(server, r, a, reason); |
| 375 | return server; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Get an active count on a server object. |
| 380 | */ |
| 381 | struct afs_server *afs_use_server(struct afs_server *server, enum afs_server_trace reason) |
| 382 | { |
| 383 | unsigned int r = atomic_inc_return(&server->ref); |
| 384 | unsigned int a = atomic_inc_return(&server->active); |
| 385 | |
| 386 | trace_afs_server(server, r, a, reason); |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 387 | return server; |
| 388 | } |
| 389 | |
| 390 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 391 | * Release a reference on a server record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | */ |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 393 | void afs_put_server(struct afs_net *net, struct afs_server *server, |
| 394 | enum afs_server_trace reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 396 | unsigned int usage; |
| 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!server) |
| 399 | return; |
| 400 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 401 | usage = atomic_dec_return(&server->ref); |
| 402 | trace_afs_server(server, usage, atomic_read(&server->active), reason); |
| 403 | if (unlikely(usage == 0)) |
| 404 | __afs_put_server(net, server); |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 407 | /* |
| 408 | * Drop an active count on a server object without updating the last-unused |
| 409 | * time. |
| 410 | */ |
| 411 | void afs_unuse_server_notime(struct afs_net *net, struct afs_server *server, |
| 412 | enum afs_server_trace reason) |
| 413 | { |
| 414 | if (server) { |
| 415 | unsigned int active = atomic_dec_return(&server->active); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 416 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 417 | if (active == 0) |
| 418 | afs_set_server_timer(net, afs_server_gc_delay); |
| 419 | afs_put_server(net, server, reason); |
| 420 | } |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 423 | /* |
| 424 | * Drop an active count on a server object. |
| 425 | */ |
| 426 | void afs_unuse_server(struct afs_net *net, struct afs_server *server, |
| 427 | enum afs_server_trace reason) |
| 428 | { |
| 429 | if (server) { |
| 430 | server->unuse_time = ktime_get_real_seconds(); |
| 431 | afs_unuse_server_notime(net, server, reason); |
| 432 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static void afs_server_rcu(struct rcu_head *rcu) |
| 436 | { |
| 437 | struct afs_server *server = container_of(rcu, struct afs_server, rcu); |
| 438 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 439 | trace_afs_server(server, atomic_read(&server->ref), |
| 440 | atomic_read(&server->active), afs_server_trace_free); |
David Howells | fe342cf | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 441 | afs_put_addrlist(rcu_access_pointer(server->addresses)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 442 | kfree(server); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 445 | static void __afs_put_server(struct afs_net *net, struct afs_server *server) |
| 446 | { |
| 447 | call_rcu(&server->rcu, afs_server_rcu); |
| 448 | afs_dec_servers_outstanding(net); |
| 449 | } |
| 450 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 451 | static void afs_give_up_callbacks(struct afs_net *net, struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
David Howells | fe342cf | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 453 | struct afs_addr_list *alist = rcu_access_pointer(server->addresses); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 454 | struct afs_addr_cursor ac = { |
| 455 | .alist = alist, |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 456 | .index = alist->preferred, |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 457 | .error = 0, |
| 458 | }; |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 459 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 460 | afs_fs_give_up_all_callbacks(net, server, &ac, NULL); |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * destroy a dead server |
| 465 | */ |
| 466 | static void afs_destroy_server(struct afs_net *net, struct afs_server *server) |
| 467 | { |
David Howells | f2686b0 | 2018-05-10 14:12:50 +0100 | [diff] [blame] | 468 | if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags)) |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 469 | afs_give_up_callbacks(net, server); |
David Howells | f2686b0 | 2018-05-10 14:12:50 +0100 | [diff] [blame] | 470 | |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 471 | flush_work(&server->initcb_work); |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 472 | afs_put_server(net, server, afs_server_trace_destroy); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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 | * Garbage collect any expired servers. |
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 | 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] | 479 | { |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 480 | struct afs_server *server, *next, *prev; |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 481 | int active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 483 | while ((server = gc_list)) { |
| 484 | gc_list = server->gc_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 486 | write_seqlock(&net->fs_lock); |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 487 | |
| 488 | active = atomic_read(&server->active); |
| 489 | if (active == 0) { |
| 490 | trace_afs_server(server, atomic_read(&server->ref), |
| 491 | active, afs_server_trace_gc); |
David Howells | 3c4c407 | 2020-05-27 15:51:30 +0100 | [diff] [blame] | 492 | next = rcu_dereference_protected( |
| 493 | server->uuid_next, lockdep_is_held(&net->fs_lock.lock)); |
| 494 | prev = server->uuid_prev; |
| 495 | if (!prev) { |
| 496 | /* The one at the front is in the tree */ |
| 497 | if (!next) { |
| 498 | rb_erase(&server->uuid_rb, &net->fs_servers); |
| 499 | } else { |
| 500 | rb_replace_node_rcu(&server->uuid_rb, |
| 501 | &next->uuid_rb, |
| 502 | &net->fs_servers); |
| 503 | next->uuid_prev = NULL; |
| 504 | } |
| 505 | } else { |
| 506 | /* This server is not at the front */ |
| 507 | rcu_assign_pointer(prev->uuid_next, next); |
| 508 | if (next) |
| 509 | next->uuid_prev = prev; |
| 510 | } |
| 511 | |
David Howells | f6cbb36 | 2020-04-24 15:10:00 +0100 | [diff] [blame] | 512 | list_del(&server->probe_link); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 513 | hlist_del_rcu(&server->proc_link); |
David Howells | 6606259 | 2018-04-18 09:38:34 +0100 | [diff] [blame] | 514 | if (!hlist_unhashed(&server->addr4_link)) |
| 515 | hlist_del_rcu(&server->addr4_link); |
| 516 | if (!hlist_unhashed(&server->addr6_link)) |
| 517 | hlist_del_rcu(&server->addr6_link); |
David Howells | 6606259 | 2018-04-18 09:38:34 +0100 | [diff] [blame] | 518 | } |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 519 | write_sequnlock(&net->fs_lock); |
| 520 | |
| 521 | if (active == 0) |
| 522 | afs_destroy_server(net, server); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Manage the records of servers known to be within a network namespace. This |
| 528 | * includes garbage collecting unused servers. |
| 529 | * |
| 530 | * Note also that we were given an increment on net->servers_outstanding by |
| 531 | * whoever queued us that we need to deal with before returning. |
| 532 | */ |
| 533 | void afs_manage_servers(struct work_struct *work) |
| 534 | { |
| 535 | struct afs_net *net = container_of(work, struct afs_net, fs_manager); |
| 536 | struct afs_server *gc_list = NULL; |
| 537 | struct rb_node *cursor; |
| 538 | time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX; |
| 539 | bool purging = !net->live; |
| 540 | |
| 541 | _enter(""); |
| 542 | |
| 543 | /* Trawl the server list looking for servers that have expired from |
| 544 | * lack of use. |
| 545 | */ |
| 546 | read_seqlock_excl(&net->fs_lock); |
| 547 | |
| 548 | for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) { |
| 549 | struct afs_server *server = |
| 550 | rb_entry(cursor, struct afs_server, uuid_rb); |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 551 | int active = atomic_read(&server->active); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 552 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 553 | _debug("manage %pU %u", &server->uuid, active); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 554 | |
David Howells | 7530d3e | 2020-10-15 09:02:25 +0100 | [diff] [blame] | 555 | if (purging) { |
| 556 | trace_afs_server(server, atomic_read(&server->ref), |
| 557 | active, afs_server_trace_purging); |
| 558 | if (active != 0) |
| 559 | pr_notice("Can't purge s=%08x\n", server->debug_id); |
| 560 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 561 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 562 | if (active == 0) { |
| 563 | time64_t expire_at = server->unuse_time; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 564 | |
| 565 | if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) && |
| 566 | !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags)) |
| 567 | expire_at += afs_server_gc_delay; |
| 568 | if (purging || expire_at <= now) { |
| 569 | server->gc_next = gc_list; |
| 570 | gc_list = server; |
| 571 | } else if (expire_at < next_manage) { |
| 572 | next_manage = expire_at; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 577 | read_sequnlock_excl(&net->fs_lock); |
| 578 | |
| 579 | /* Update the timer on the way out. We have to pass an increment on |
| 580 | * servers_outstanding in the namespace that we are in to the timer or |
| 581 | * the work scheduler. |
| 582 | */ |
| 583 | if (!purging && next_manage < TIME64_MAX) { |
| 584 | now = ktime_get_real_seconds(); |
| 585 | |
| 586 | if (next_manage - now <= 0) { |
| 587 | if (queue_work(afs_wq, &net->fs_manager)) |
| 588 | afs_inc_servers_outstanding(net); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 589 | } else { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 590 | afs_set_server_timer(net, next_manage - now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 594 | afs_gc_servers(net, gc_list); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 595 | |
| 596 | afs_dec_servers_outstanding(net); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 597 | _leave(" [%d]", atomic_read(&net->servers_outstanding)); |
| 598 | } |
| 599 | |
| 600 | static void afs_queue_server_manager(struct afs_net *net) |
| 601 | { |
| 602 | afs_inc_servers_outstanding(net); |
| 603 | if (!queue_work(afs_wq, &net->fs_manager)) |
| 604 | afs_dec_servers_outstanding(net); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 608 | * Purge list of servers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 610 | void afs_purge_servers(struct afs_net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 612 | _enter(""); |
| 613 | |
| 614 | if (del_timer_sync(&net->fs_timer)) |
David Howells | 5481fc6 | 2020-06-19 23:39:36 +0100 | [diff] [blame] | 615 | afs_dec_servers_outstanding(net); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 616 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 617 | afs_queue_server_manager(net); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 618 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 619 | _debug("wait"); |
David Howells | 5481fc6 | 2020-06-19 23:39:36 +0100 | [diff] [blame] | 620 | atomic_dec(&net->servers_outstanding); |
Peter Zijlstra | ab1fbe3 | 2018-03-15 11:42:28 +0100 | [diff] [blame] | 621 | wait_var_event(&net->servers_outstanding, |
| 622 | !atomic_read(&net->servers_outstanding)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 623 | _leave(""); |
| 624 | } |
| 625 | |
| 626 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 627 | * Get an update for a server's address list. |
| 628 | */ |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 629 | static noinline bool afs_update_server_record(struct afs_operation *op, |
| 630 | struct afs_server *server) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 631 | { |
| 632 | struct afs_addr_list *alist, *discard; |
| 633 | |
| 634 | _enter(""); |
| 635 | |
David Howells | 977e5f8 | 2020-04-17 17:31:26 +0100 | [diff] [blame] | 636 | trace_afs_server(server, atomic_read(&server->ref), atomic_read(&server->active), |
| 637 | afs_server_trace_update); |
David Howells | 4521819 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 638 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 639 | alist = afs_vl_lookup_addrs(op->volume->cell, op->key, &server->uuid); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 640 | if (IS_ERR(alist)) { |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 641 | if ((PTR_ERR(alist) == -ERESTARTSYS || |
| 642 | PTR_ERR(alist) == -EINTR) && |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 643 | (op->flags & AFS_OPERATION_UNINTR) && |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 644 | server->addresses) { |
| 645 | _leave(" = t [intr]"); |
| 646 | return true; |
| 647 | } |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 648 | op->error = PTR_ERR(alist); |
| 649 | _leave(" = f [%d]", op->error); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
| 653 | discard = alist; |
| 654 | if (server->addr_version != alist->version) { |
| 655 | write_lock(&server->fs_lock); |
| 656 | discard = rcu_dereference_protected(server->addresses, |
| 657 | lockdep_is_held(&server->fs_lock)); |
| 658 | rcu_assign_pointer(server->addresses, alist); |
| 659 | server->addr_version = alist->version; |
| 660 | write_unlock(&server->fs_lock); |
| 661 | } |
| 662 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 663 | afs_put_addrlist(discard); |
| 664 | _leave(" = t"); |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * See if a server's address list needs updating. |
| 670 | */ |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 671 | bool afs_check_server_record(struct afs_operation *op, struct afs_server *server) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 672 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 673 | bool success; |
| 674 | int ret, retries = 0; |
| 675 | |
| 676 | _enter(""); |
| 677 | |
| 678 | ASSERT(server); |
| 679 | |
| 680 | retry: |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 681 | if (test_bit(AFS_SERVER_FL_UPDATING, &server->flags)) |
| 682 | goto wait; |
| 683 | if (test_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags)) |
| 684 | goto update; |
| 685 | _leave(" = t [good]"); |
| 686 | return true; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 687 | |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 688 | update: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 689 | if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) { |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 690 | clear_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 691 | success = afs_update_server_record(op, server); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 692 | clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags); |
| 693 | wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING); |
| 694 | _leave(" = %d", success); |
| 695 | return success; |
| 696 | } |
| 697 | |
David Howells | 8100680 | 2020-04-16 17:05:28 +0100 | [diff] [blame] | 698 | wait: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 699 | ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING, |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 700 | (op->flags & AFS_OPERATION_UNINTR) ? |
| 701 | TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 702 | if (ret == -ERESTARTSYS) { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 703 | op->error = ret; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 704 | _leave(" = f [intr]"); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | retries++; |
| 709 | if (retries == 4) { |
| 710 | _leave(" = f [stale]"); |
| 711 | ret = -ESTALE; |
| 712 | return false; |
| 713 | } |
| 714 | goto retry; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 715 | } |