blob: e82e452e26124e2908892eb12716657f3abb6baf [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* AFS server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells08e0e7c2007-04-26 15:55:03 -07004 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/sched.h>
9#include <linux/slab.h>
David Howells4d9df982017-11-02 15:27:47 +000010#include "afs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "internal.h"
David Howells30062bd2018-10-20 00:57:58 +010012#include "protocol_yfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
David Howellsd2ddc772017-11-02 15:27:50 +000014static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */
David Howells45218192019-06-20 18:12:17 +010015static atomic_t afs_server_debug_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
David Howells977e5f82020-04-17 17:31:26 +010017static struct afs_server *afs_maybe_use_server(struct afs_server *,
18 enum afs_server_trace);
19static void __afs_put_server(struct afs_net *, struct afs_server *);
20
David Howellsd2ddc772017-11-02 15:27:50 +000021/*
22 * Find a server by one of its addresses.
23 */
24struct afs_server *afs_find_server(struct afs_net *net,
25 const struct sockaddr_rxrpc *srx)
David Howells59fa1c42017-11-02 15:27:45 +000026{
David Howellsd2ddc772017-11-02 15:27:50 +000027 const struct afs_addr_list *alist;
28 struct afs_server *server = NULL;
29 unsigned int i;
David Howellsd2ddc772017-11-02 15:27:50 +000030 int seq = 0, diff;
David Howells59fa1c42017-11-02 15:27:45 +000031
David Howellsd2ddc772017-11-02 15:27:50 +000032 rcu_read_lock();
33
34 do {
35 if (server)
David Howells977e5f82020-04-17 17:31:26 +010036 afs_unuse_server_notime(net, server, afs_server_trace_put_find_rsq);
David Howellsd2ddc772017-11-02 15:27:50 +000037 server = NULL;
38 read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
39
Marc Dionne9bd01602019-12-09 15:04:43 +000040 if (srx->transport.family == AF_INET6) {
41 const struct sockaddr_in6 *a = &srx->transport.sin6, *b;
David Howellsd2ddc772017-11-02 15:27:50 +000042 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 Howellsfe342cf2018-04-09 21:12:31 +010046 diff = ((u16 __force)a->sin6_port -
47 (u16 __force)b->sin6_port);
David Howellsd2ddc772017-11-02 15:27:50 +000048 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 Howellsd2ddc772017-11-02 15:27:50 +000054 }
55 }
56 } else {
Marc Dionne9bd01602019-12-09 15:04:43 +000057 const struct sockaddr_in *a = &srx->transport.sin, *b;
David Howellsd2ddc772017-11-02 15:27:50 +000058 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 Dionne9bd01602019-12-09 15:04:43 +000061 b = &alist->addrs[i].transport.sin;
62 diff = ((u16 __force)a->sin_port -
63 (u16 __force)b->sin_port);
David Howellsd2ddc772017-11-02 15:27:50 +000064 if (diff == 0)
Marc Dionne9bd01602019-12-09 15:04:43 +000065 diff = ((u32 __force)a->sin_addr.s_addr -
66 (u32 __force)b->sin_addr.s_addr);
David Howellsd2ddc772017-11-02 15:27:50 +000067 if (diff == 0)
68 goto found;
David Howellsd2ddc772017-11-02 15:27:50 +000069 }
70 }
71 }
72
David Howellsd2ddc772017-11-02 15:27:50 +000073 server = NULL;
David Howells977e5f82020-04-17 17:31:26 +010074 continue;
David Howellsd2ddc772017-11-02 15:27:50 +000075 found:
David Howells977e5f82020-04-17 17:31:26 +010076 server = afs_maybe_use_server(server, afs_server_trace_get_by_addr);
David Howellsd2ddc772017-11-02 15:27:50 +000077
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 Howells59fa1c42017-11-02 15:27:45 +000084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
David Howells977e5f82020-04-17 17:31:26 +010087 * Look up a server by its UUID and mark it active.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
David Howellsd2ddc772017-11-02 15:27:50 +000089struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
David Howellsd2ddc772017-11-02 15:27:50 +000091 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 Howells977e5f82020-04-17 17:31:26 +0100103 afs_unuse_server(net, server, afs_server_trace_put_uuid_rsq);
David Howellsd2ddc772017-11-02 15:27:50 +0000104 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 Howells977e5f82020-04-17 17:31:26 +0100118 afs_use_server(server, afs_server_trace_get_by_uuid);
David Howellsd2ddc772017-11-02 15:27:50 +0000119 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 Howells3c4c4072020-05-27 15:51:30 +0100133 * 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 Howellsd2ddc772017-11-02 15:27:50 +0000136 */
David Howells20325962020-04-30 01:03:49 +0100137static struct afs_server *afs_install_server(struct afs_cell *cell,
David Howellsd2ddc772017-11-02 15:27:50 +0000138 struct afs_server *candidate)
139{
140 const struct afs_addr_list *alist;
David Howells3c4c4072020-05-27 15:51:30 +0100141 struct afs_server *server, *next;
David Howells20325962020-04-30 01:03:49 +0100142 struct afs_net *net = cell->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700143 struct rb_node **pp, *p;
zhengbin4fe171b2019-11-21 09:12:18 +0000144 int diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David Howellsd2ddc772017-11-02 15:27:50 +0000146 _enter("%p", candidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David Howellsd2ddc772017-11-02 15:27:50 +0000148 write_seqlock(&net->fs_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700149
David Howellsd2ddc772017-11-02 15:27:50 +0000150 /* Firstly install the server in the UUID lookup tree */
151 pp = &net->fs_servers.rb_node;
David Howells08e0e7c2007-04-26 15:55:03 -0700152 p = NULL;
153 while (*pp) {
154 p = *pp;
155 _debug("- consider %p", p);
David Howellsd2ddc772017-11-02 15:27:50 +0000156 server = rb_entry(p, struct afs_server, uuid_rb);
157 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
David Howells3c4c4072020-05-27 15:51:30 +0100158 if (diff < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700159 pp = &(*pp)->rb_left;
David Howells3c4c4072020-05-27 15:51:30 +0100160 } else if (diff > 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700161 pp = &(*pp)->rb_right;
David Howells3c4c4072020-05-27 15:51:30 +0100162 } 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 Howells08e0e7c2007-04-26 15:55:03 -0700182 }
183
David Howellsd2ddc772017-11-02 15:27:50 +0000184 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 Howells3c4c4072020-05-27 15:51:30 +0100189added_dup:
David Howellsd2ddc772017-11-02 15:27:50 +0000190 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 Howells08e0e7c2007-04-26 15:55:03 -0700208
David Howellsd2ddc772017-11-02 15:27:50 +0000209exists:
David Howells45218192019-06-20 18:12:17 +0100210 afs_get_server(server, afs_server_trace_get_install);
David Howellsd2ddc772017-11-02 15:27:50 +0000211 write_sequnlock(&net->fs_lock);
212 return server;
David Howells08e0e7c2007-04-26 15:55:03 -0700213}
214
215/*
David Howells977e5f82020-04-17 17:31:26 +0100216 * Allocate a new server record and mark it active.
David Howells08e0e7c2007-04-26 15:55:03 -0700217 */
David Howells20325962020-04-30 01:03:49 +0100218static struct afs_server *afs_alloc_server(struct afs_cell *cell,
David Howellsd2ddc772017-11-02 15:27:50 +0000219 const uuid_t *uuid,
220 struct afs_addr_list *alist)
David Howells08e0e7c2007-04-26 15:55:03 -0700221{
222 struct afs_server *server;
David Howells20325962020-04-30 01:03:49 +0100223 struct afs_net *net = cell->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700224
225 _enter("");
226
Yan Burmanb593e482006-12-06 20:40:32 -0800227 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
David Howells8b2a4642017-11-02 15:27:50 +0000228 if (!server)
229 goto enomem;
David Howells08e0e7c2007-04-26 15:55:03 -0700230
David Howells977e5f82020-04-17 17:31:26 +0100231 atomic_set(&server->ref, 1);
232 atomic_set(&server->active, 1);
David Howells45218192019-06-20 18:12:17 +0100233 server->debug_id = atomic_inc_return(&afs_server_debug_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000234 RCU_INIT_POINTER(server->addresses, alist);
235 server->addr_version = alist->version;
236 server->uuid = *uuid;
David Howellsd2ddc772017-11-02 15:27:50 +0000237 rwlock_init(&server->fs_lock);
David Howells3bf0fb62018-10-20 00:57:59 +0100238 init_waitqueue_head(&server->probe_wq);
David Howellsf6cbb362020-04-24 15:10:00 +0100239 INIT_LIST_HEAD(&server->probe_link);
David Howells3bf0fb62018-10-20 00:57:59 +0100240 spin_lock_init(&server->probe_lock);
David Howells20325962020-04-30 01:03:49 +0100241 server->cell = cell;
David Howellsf3c130e2020-05-02 13:39:57 +0100242 server->rtt = UINT_MAX;
David Howells8b2a4642017-11-02 15:27:50 +0000243
David Howellsd2ddc772017-11-02 15:27:50 +0000244 afs_inc_servers_outstanding(net);
David Howells977e5f82020-04-17 17:31:26 +0100245 trace_afs_server(server, 1, 1, afs_server_trace_alloc);
David Howellsd2ddc772017-11-02 15:27:50 +0000246 _leave(" = %p", server);
David Howells08e0e7c2007-04-26 15:55:03 -0700247 return server;
David Howells8b2a4642017-11-02 15:27:50 +0000248
David Howells8b2a4642017-11-02 15:27:50 +0000249enomem:
250 _leave(" = NULL [nomem]");
251 return NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700252}
253
254/*
David Howellsd2ddc772017-11-02 15:27:50 +0000255 * Look up an address record for a server
David Howells08e0e7c2007-04-26 15:55:03 -0700256 */
David Howellsd2ddc772017-11-02 15:27:50 +0000257static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
258 struct key *key, const uuid_t *uuid)
David Howells08e0e7c2007-04-26 15:55:03 -0700259{
David Howells0a5143f2018-10-20 00:57:57 +0100260 struct afs_vl_cursor vc;
261 struct afs_addr_list *alist = NULL;
David Howellsd2ddc772017-11-02 15:27:50 +0000262 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700263
David Howells0a5143f2018-10-20 00:57:57 +0100264 ret = -ERESTARTSYS;
265 if (afs_begin_vlserver_operation(&vc, cell, key)) {
266 while (afs_select_vlserver(&vc)) {
David Howells3bf0fb62018-10-20 00:57:59 +0100267 if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags))
David Howells0a5143f2018-10-20 00:57:57 +0100268 alist = afs_yfsvl_get_endpoints(&vc, uuid);
269 else
270 alist = afs_vl_get_addrs_u(&vc, uuid);
David Howells08e0e7c2007-04-26 15:55:03 -0700271 }
David Howells0a5143f2018-10-20 00:57:57 +0100272
273 ret = afs_end_vlserver_operation(&vc);
David Howells08e0e7c2007-04-26 15:55:03 -0700274 }
275
David Howells0a5143f2018-10-20 00:57:57 +0100276 return ret < 0 ? ERR_PTR(ret) : alist;
David Howellsd2ddc772017-11-02 15:27:50 +0000277}
278
279/*
280 * Get or create a fileserver record.
281 */
282struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
David Howells81006802020-04-16 17:05:28 +0100283 const uuid_t *uuid, u32 addr_version)
David Howellsd2ddc772017-11-02 15:27:50 +0000284{
285 struct afs_addr_list *alist;
286 struct afs_server *server, *candidate;
287
288 _enter("%p,%pU", cell->net, uuid);
289
290 server = afs_find_server_by_uuid(cell->net, uuid);
David Howells81006802020-04-16 17:05:28 +0100291 if (server) {
292 if (server->addr_version != addr_version)
293 set_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags);
David Howellsd2ddc772017-11-02 15:27:50 +0000294 return server;
David Howells81006802020-04-16 17:05:28 +0100295 }
David Howellsd2ddc772017-11-02 15:27:50 +0000296
297 alist = afs_vl_lookup_addrs(cell, key, uuid);
298 if (IS_ERR(alist))
299 return ERR_CAST(alist);
300
David Howells20325962020-04-30 01:03:49 +0100301 candidate = afs_alloc_server(cell, uuid, alist);
David Howellsd2ddc772017-11-02 15:27:50 +0000302 if (!candidate) {
303 afs_put_addrlist(alist);
304 return ERR_PTR(-ENOMEM);
305 }
306
David Howells20325962020-04-30 01:03:49 +0100307 server = afs_install_server(cell, candidate);
David Howellsd2ddc772017-11-02 15:27:50 +0000308 if (server != candidate) {
309 afs_put_addrlist(alist);
310 kfree(candidate);
David Howellsf6cbb362020-04-24 15:10:00 +0100311 } else {
312 /* Immediately dispatch an asynchronous probe to each interface
313 * on the fileserver. This will make sure the repeat-probing
314 * service is started.
315 */
316 afs_fs_probe_fileserver(cell->net, server, key, true);
David Howellsd2ddc772017-11-02 15:27:50 +0000317 }
318
David Howells08e0e7c2007-04-26 15:55:03 -0700319 return server;
David Howellsec268152007-04-26 15:49:28 -0700320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David Howellsd2ddc772017-11-02 15:27:50 +0000322/*
323 * Set the server timer to fire after a given delay, assuming it's not already
324 * set for an earlier time.
325 */
David Howells59fa1c42017-11-02 15:27:45 +0000326static void afs_set_server_timer(struct afs_net *net, time64_t delay)
327{
David Howells59fa1c42017-11-02 15:27:45 +0000328 if (net->live) {
David Howellsd2ddc772017-11-02 15:27:50 +0000329 afs_inc_servers_outstanding(net);
330 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
David Howells59fa1c42017-11-02 15:27:45 +0000331 afs_dec_servers_outstanding(net);
332 }
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
David Howellsd2ddc772017-11-02 15:27:50 +0000336 * Server management timer. We have an increment on fs_outstanding that we
337 * need to pass along to the work item.
338 */
339void afs_servers_timer(struct timer_list *timer)
340{
341 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
342
343 _enter("");
344 if (!queue_work(afs_wq, &net->fs_manager))
345 afs_dec_servers_outstanding(net);
346}
347
348/*
David Howells45218192019-06-20 18:12:17 +0100349 * Get a reference on a server object.
350 */
351struct afs_server *afs_get_server(struct afs_server *server,
352 enum afs_server_trace reason)
353{
David Howells977e5f82020-04-17 17:31:26 +0100354 unsigned int u = atomic_inc_return(&server->ref);
David Howells45218192019-06-20 18:12:17 +0100355
David Howells977e5f82020-04-17 17:31:26 +0100356 trace_afs_server(server, u, atomic_read(&server->active), reason);
357 return server;
358}
359
360/*
361 * Try to get a reference on a server object.
362 */
363static struct afs_server *afs_maybe_use_server(struct afs_server *server,
364 enum afs_server_trace reason)
365{
366 unsigned int r = atomic_fetch_add_unless(&server->ref, 1, 0);
367 unsigned int a;
368
369 if (r == 0)
370 return NULL;
371
372 a = atomic_inc_return(&server->active);
373 trace_afs_server(server, r, a, reason);
374 return server;
375}
376
377/*
378 * Get an active count on a server object.
379 */
380struct afs_server *afs_use_server(struct afs_server *server, enum afs_server_trace reason)
381{
382 unsigned int r = atomic_inc_return(&server->ref);
383 unsigned int a = atomic_inc_return(&server->active);
384
385 trace_afs_server(server, r, a, reason);
David Howells45218192019-06-20 18:12:17 +0100386 return server;
387}
388
389/*
David Howellsd2ddc772017-11-02 15:27:50 +0000390 * Release a reference on a server record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
David Howells45218192019-06-20 18:12:17 +0100392void afs_put_server(struct afs_net *net, struct afs_server *server,
393 enum afs_server_trace reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
David Howellsd2ddc772017-11-02 15:27:50 +0000395 unsigned int usage;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!server)
398 return;
399
David Howells977e5f82020-04-17 17:31:26 +0100400 usage = atomic_dec_return(&server->ref);
401 trace_afs_server(server, usage, atomic_read(&server->active), reason);
402 if (unlikely(usage == 0))
403 __afs_put_server(net, server);
404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Howells977e5f82020-04-17 17:31:26 +0100406/*
407 * Drop an active count on a server object without updating the last-unused
408 * time.
409 */
410void afs_unuse_server_notime(struct afs_net *net, struct afs_server *server,
411 enum afs_server_trace reason)
412{
413 if (server) {
414 unsigned int active = atomic_dec_return(&server->active);
David Howells260a9802007-04-26 15:59:35 -0700415
David Howells977e5f82020-04-17 17:31:26 +0100416 if (active == 0)
417 afs_set_server_timer(net, afs_server_gc_delay);
418 afs_put_server(net, server, reason);
419 }
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Howells977e5f82020-04-17 17:31:26 +0100422/*
423 * Drop an active count on a server object.
424 */
425void afs_unuse_server(struct afs_net *net, struct afs_server *server,
426 enum afs_server_trace reason)
427{
428 if (server) {
429 server->unuse_time = ktime_get_real_seconds();
430 afs_unuse_server_notime(net, server, reason);
431 }
David Howellsd2ddc772017-11-02 15:27:50 +0000432}
433
434static void afs_server_rcu(struct rcu_head *rcu)
435{
436 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
437
David Howells977e5f82020-04-17 17:31:26 +0100438 trace_afs_server(server, atomic_read(&server->ref),
439 atomic_read(&server->active), afs_server_trace_free);
David Howellsfe342cf2018-04-09 21:12:31 +0100440 afs_put_addrlist(rcu_access_pointer(server->addresses));
David Howellsd2ddc772017-11-02 15:27:50 +0000441 kfree(server);
David Howellsec268152007-04-26 15:49:28 -0700442}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Howells977e5f82020-04-17 17:31:26 +0100444static void __afs_put_server(struct afs_net *net, struct afs_server *server)
445{
446 call_rcu(&server->rcu, afs_server_rcu);
447 afs_dec_servers_outstanding(net);
448}
449
David Howellse49c7b22020-04-10 20:51:51 +0100450static void afs_give_up_callbacks(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
David Howellsfe342cf2018-04-09 21:12:31 +0100452 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
David Howells8b2a4642017-11-02 15:27:50 +0000453 struct afs_addr_cursor ac = {
454 .alist = alist,
David Howells3bf0fb62018-10-20 00:57:59 +0100455 .index = alist->preferred,
David Howells8b2a4642017-11-02 15:27:50 +0000456 .error = 0,
457 };
David Howells45218192019-06-20 18:12:17 +0100458
David Howellse49c7b22020-04-10 20:51:51 +0100459 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
460}
461
462/*
463 * destroy a dead server
464 */
465static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
466{
David Howellsf2686b02018-05-10 14:12:50 +0100467 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
David Howellse49c7b22020-04-10 20:51:51 +0100468 afs_give_up_callbacks(net, server);
David Howellsf2686b02018-05-10 14:12:50 +0100469
David Howells977e5f82020-04-17 17:31:26 +0100470 afs_put_server(net, server, afs_server_trace_destroy);
David Howellsec268152007-04-26 15:49:28 -0700471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
David Howellsd2ddc772017-11-02 15:27:50 +0000474 * Garbage collect any expired servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
David Howellsd2ddc772017-11-02 15:27:50 +0000476static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David Howells3c4c4072020-05-27 15:51:30 +0100478 struct afs_server *server, *next, *prev;
David Howells977e5f82020-04-17 17:31:26 +0100479 int active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
David Howellsd2ddc772017-11-02 15:27:50 +0000481 while ((server = gc_list)) {
482 gc_list = server->gc_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
David Howellsd2ddc772017-11-02 15:27:50 +0000484 write_seqlock(&net->fs_lock);
David Howells977e5f82020-04-17 17:31:26 +0100485
486 active = atomic_read(&server->active);
487 if (active == 0) {
488 trace_afs_server(server, atomic_read(&server->ref),
489 active, afs_server_trace_gc);
David Howells3c4c4072020-05-27 15:51:30 +0100490 next = rcu_dereference_protected(
491 server->uuid_next, lockdep_is_held(&net->fs_lock.lock));
492 prev = server->uuid_prev;
493 if (!prev) {
494 /* The one at the front is in the tree */
495 if (!next) {
496 rb_erase(&server->uuid_rb, &net->fs_servers);
497 } else {
498 rb_replace_node_rcu(&server->uuid_rb,
499 &next->uuid_rb,
500 &net->fs_servers);
501 next->uuid_prev = NULL;
502 }
503 } else {
504 /* This server is not at the front */
505 rcu_assign_pointer(prev->uuid_next, next);
506 if (next)
507 next->uuid_prev = prev;
508 }
509
David Howellsf6cbb362020-04-24 15:10:00 +0100510 list_del(&server->probe_link);
David Howellsd2ddc772017-11-02 15:27:50 +0000511 hlist_del_rcu(&server->proc_link);
David Howells66062592018-04-18 09:38:34 +0100512 if (!hlist_unhashed(&server->addr4_link))
513 hlist_del_rcu(&server->addr4_link);
514 if (!hlist_unhashed(&server->addr6_link))
515 hlist_del_rcu(&server->addr6_link);
David Howells66062592018-04-18 09:38:34 +0100516 }
David Howells977e5f82020-04-17 17:31:26 +0100517 write_sequnlock(&net->fs_lock);
518
519 if (active == 0)
520 afs_destroy_server(net, server);
David Howellsd2ddc772017-11-02 15:27:50 +0000521 }
522}
523
524/*
525 * Manage the records of servers known to be within a network namespace. This
526 * includes garbage collecting unused servers.
527 *
528 * Note also that we were given an increment on net->servers_outstanding by
529 * whoever queued us that we need to deal with before returning.
530 */
531void afs_manage_servers(struct work_struct *work)
532{
533 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
534 struct afs_server *gc_list = NULL;
535 struct rb_node *cursor;
536 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
537 bool purging = !net->live;
538
539 _enter("");
540
541 /* Trawl the server list looking for servers that have expired from
542 * lack of use.
543 */
544 read_seqlock_excl(&net->fs_lock);
545
546 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
547 struct afs_server *server =
548 rb_entry(cursor, struct afs_server, uuid_rb);
David Howells977e5f82020-04-17 17:31:26 +0100549 int active = atomic_read(&server->active);
David Howellsd2ddc772017-11-02 15:27:50 +0000550
David Howells977e5f82020-04-17 17:31:26 +0100551 _debug("manage %pU %u", &server->uuid, active);
David Howellsd2ddc772017-11-02 15:27:50 +0000552
David Howells977e5f82020-04-17 17:31:26 +0100553 ASSERTIFCMP(purging, active, ==, 0);
David Howellsd2ddc772017-11-02 15:27:50 +0000554
David Howells977e5f82020-04-17 17:31:26 +0100555 if (active == 0) {
556 time64_t expire_at = server->unuse_time;
David Howellsd2ddc772017-11-02 15:27:50 +0000557
558 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
559 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
560 expire_at += afs_server_gc_delay;
561 if (purging || expire_at <= now) {
562 server->gc_next = gc_list;
563 gc_list = server;
564 } else if (expire_at < next_manage) {
565 next_manage = expire_at;
David Howellsf044c882017-11-02 15:27:45 +0000566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
David Howellsd2ddc772017-11-02 15:27:50 +0000568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Howellsd2ddc772017-11-02 15:27:50 +0000570 read_sequnlock_excl(&net->fs_lock);
571
572 /* Update the timer on the way out. We have to pass an increment on
573 * servers_outstanding in the namespace that we are in to the timer or
574 * the work scheduler.
575 */
576 if (!purging && next_manage < TIME64_MAX) {
577 now = ktime_get_real_seconds();
578
579 if (next_manage - now <= 0) {
580 if (queue_work(afs_wq, &net->fs_manager))
581 afs_inc_servers_outstanding(net);
David Howells08e0e7c2007-04-26 15:55:03 -0700582 } else {
David Howellsd2ddc772017-11-02 15:27:50 +0000583 afs_set_server_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 }
586
David Howellsd2ddc772017-11-02 15:27:50 +0000587 afs_gc_servers(net, gc_list);
David Howells59fa1c42017-11-02 15:27:45 +0000588
589 afs_dec_servers_outstanding(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000590 _leave(" [%d]", atomic_read(&net->servers_outstanding));
591}
592
593static void afs_queue_server_manager(struct afs_net *net)
594{
595 afs_inc_servers_outstanding(net);
596 if (!queue_work(afs_wq, &net->fs_manager))
597 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/*
David Howellsd2ddc772017-11-02 15:27:50 +0000601 * Purge list of servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
David Howellsd2ddc772017-11-02 15:27:50 +0000603void afs_purge_servers(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
David Howellsd2ddc772017-11-02 15:27:50 +0000605 _enter("");
606
607 if (del_timer_sync(&net->fs_timer))
David Howells5481fc62020-06-19 23:39:36 +0100608 afs_dec_servers_outstanding(net);
David Howells59fa1c42017-11-02 15:27:45 +0000609
David Howellsd2ddc772017-11-02 15:27:50 +0000610 afs_queue_server_manager(net);
David Howells59fa1c42017-11-02 15:27:45 +0000611
David Howellsd2ddc772017-11-02 15:27:50 +0000612 _debug("wait");
David Howells5481fc62020-06-19 23:39:36 +0100613 atomic_dec(&net->servers_outstanding);
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100614 wait_var_event(&net->servers_outstanding,
615 !atomic_read(&net->servers_outstanding));
David Howellsd2ddc772017-11-02 15:27:50 +0000616 _leave("");
617}
618
619/*
David Howellsd2ddc772017-11-02 15:27:50 +0000620 * Get an update for a server's address list.
621 */
David Howellse49c7b22020-04-10 20:51:51 +0100622static noinline bool afs_update_server_record(struct afs_operation *op,
623 struct afs_server *server)
David Howellsd2ddc772017-11-02 15:27:50 +0000624{
625 struct afs_addr_list *alist, *discard;
626
627 _enter("");
628
David Howells977e5f82020-04-17 17:31:26 +0100629 trace_afs_server(server, atomic_read(&server->ref), atomic_read(&server->active),
630 afs_server_trace_update);
David Howells45218192019-06-20 18:12:17 +0100631
David Howellse49c7b22020-04-10 20:51:51 +0100632 alist = afs_vl_lookup_addrs(op->volume->cell, op->key, &server->uuid);
David Howellsd2ddc772017-11-02 15:27:50 +0000633 if (IS_ERR(alist)) {
David Howells20b83912019-05-08 16:16:31 +0100634 if ((PTR_ERR(alist) == -ERESTARTSYS ||
635 PTR_ERR(alist) == -EINTR) &&
David Howellse49c7b22020-04-10 20:51:51 +0100636 (op->flags & AFS_OPERATION_UNINTR) &&
David Howells20b83912019-05-08 16:16:31 +0100637 server->addresses) {
638 _leave(" = t [intr]");
639 return true;
640 }
David Howellse49c7b22020-04-10 20:51:51 +0100641 op->error = PTR_ERR(alist);
642 _leave(" = f [%d]", op->error);
David Howellsd2ddc772017-11-02 15:27:50 +0000643 return false;
644 }
645
646 discard = alist;
647 if (server->addr_version != alist->version) {
648 write_lock(&server->fs_lock);
649 discard = rcu_dereference_protected(server->addresses,
650 lockdep_is_held(&server->fs_lock));
651 rcu_assign_pointer(server->addresses, alist);
652 server->addr_version = alist->version;
653 write_unlock(&server->fs_lock);
654 }
655
David Howellsd2ddc772017-11-02 15:27:50 +0000656 afs_put_addrlist(discard);
657 _leave(" = t");
658 return true;
659}
660
661/*
662 * See if a server's address list needs updating.
663 */
David Howellse49c7b22020-04-10 20:51:51 +0100664bool afs_check_server_record(struct afs_operation *op, struct afs_server *server)
David Howellsd2ddc772017-11-02 15:27:50 +0000665{
David Howellsd2ddc772017-11-02 15:27:50 +0000666 bool success;
667 int ret, retries = 0;
668
669 _enter("");
670
671 ASSERT(server);
672
673retry:
David Howells81006802020-04-16 17:05:28 +0100674 if (test_bit(AFS_SERVER_FL_UPDATING, &server->flags))
675 goto wait;
676 if (test_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags))
677 goto update;
678 _leave(" = t [good]");
679 return true;
David Howellsd2ddc772017-11-02 15:27:50 +0000680
David Howells81006802020-04-16 17:05:28 +0100681update:
David Howellsd2ddc772017-11-02 15:27:50 +0000682 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
David Howells81006802020-04-16 17:05:28 +0100683 clear_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags);
David Howellse49c7b22020-04-10 20:51:51 +0100684 success = afs_update_server_record(op, server);
David Howellsd2ddc772017-11-02 15:27:50 +0000685 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
686 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
687 _leave(" = %d", success);
688 return success;
689 }
690
David Howells81006802020-04-16 17:05:28 +0100691wait:
David Howellsd2ddc772017-11-02 15:27:50 +0000692 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
David Howellse49c7b22020-04-10 20:51:51 +0100693 (op->flags & AFS_OPERATION_UNINTR) ?
694 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
David Howellsd2ddc772017-11-02 15:27:50 +0000695 if (ret == -ERESTARTSYS) {
David Howellse49c7b22020-04-10 20:51:51 +0100696 op->error = ret;
David Howellsd2ddc772017-11-02 15:27:50 +0000697 _leave(" = f [intr]");
698 return false;
699 }
700
701 retries++;
702 if (retries == 4) {
703 _leave(" = f [stale]");
704 ret = -ESTALE;
705 return false;
706 }
707 goto retry;
David Howellsec268152007-04-26 15:49:28 -0700708}