blob: 6102ea9ee3fbf4a61df9f37b0e38f05e5f721b4d [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Howells4d9df982017-11-02 15:27:47 +000014#include "afs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "internal.h"
16
David Howellsd2ddc772017-11-02 15:27:50 +000017static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */
18static unsigned afs_server_update_delay = 30; /* Time till VLDB recheck in secs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
David Howells59fa1c42017-11-02 15:27:45 +000020static void afs_inc_servers_outstanding(struct afs_net *net)
21{
22 atomic_inc(&net->servers_outstanding);
23}
24
25static void afs_dec_servers_outstanding(struct afs_net *net)
26{
27 if (atomic_dec_and_test(&net->servers_outstanding))
Peter Zijlstraab1fbe32018-03-15 11:42:28 +010028 wake_up_var(&net->servers_outstanding);
David Howells59fa1c42017-11-02 15:27:45 +000029}
30
David Howellsd2ddc772017-11-02 15:27:50 +000031/*
32 * Find a server by one of its addresses.
33 */
34struct afs_server *afs_find_server(struct afs_net *net,
35 const struct sockaddr_rxrpc *srx)
David Howells59fa1c42017-11-02 15:27:45 +000036{
David Howellsd2ddc772017-11-02 15:27:50 +000037 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 Howells59fa1c42017-11-02 15:27:45 +000043
David Howellsd2ddc772017-11-02 15:27:50 +000044 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 Howellsfe342cf2018-04-09 21:12:31 +010062 diff = ((u16 __force)a->sin6_port -
63 (u16 __force)b->sin6_port);
David Howellsd2ddc772017-11-02 15:27:50 +000064 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 Howellsd2ddc772017-11-02 15:27:50 +000070 }
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 Howellsfe342cf2018-04-09 21:12:31 +010077 diff = ((u16 __force)a->sin6_port -
78 (u16 __force)b->sin6_port);
David Howellsd2ddc772017-11-02 15:27:50 +000079 if (diff == 0)
David Howellsfe342cf2018-04-09 21:12:31 +010080 diff = ((u32 __force)a->sin6_addr.s6_addr32[3] -
81 (u32 __force)b->sin6_addr.s6_addr32[3]);
David Howellsd2ddc772017-11-02 15:27:50 +000082 if (diff == 0)
83 goto found;
David Howellsd2ddc772017-11-02 15:27:50 +000084 }
85 }
86 }
87
David Howellsd2ddc772017-11-02 15:27:50 +000088 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 Howells59fa1c42017-11-02 15:27:45 +000099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
David Howellsd2ddc772017-11-02 15:27:50 +0000102 * Look up a server by its UUID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
David Howellsd2ddc772017-11-02 15:27:50 +0000104struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
David Howellsd2ddc772017-11-02 15:27:50 +0000106 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 */
150static 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 Howells08e0e7c2007-04-26 15:55:03 -0700155 struct rb_node **pp, *p;
David Howellsd2ddc772017-11-02 15:27:50 +0000156 int ret = -EEXIST, diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David Howellsd2ddc772017-11-02 15:27:50 +0000158 _enter("%p", candidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Howellsd2ddc772017-11-02 15:27:50 +0000160 write_seqlock(&net->fs_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700161
David Howellsd2ddc772017-11-02 15:27:50 +0000162 /* Firstly install the server in the UUID lookup tree */
163 pp = &net->fs_servers.rb_node;
David Howells08e0e7c2007-04-26 15:55:03 -0700164 p = NULL;
165 while (*pp) {
166 p = *pp;
167 _debug("- consider %p", p);
David Howellsd2ddc772017-11-02 15:27:50 +0000168 server = rb_entry(p, struct afs_server, uuid_rb);
169 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
David Howells4d9df982017-11-02 15:27:47 +0000170 if (diff < 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700171 pp = &(*pp)->rb_left;
David Howells4d9df982017-11-02 15:27:47 +0000172 else if (diff > 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700173 pp = &(*pp)->rb_right;
174 else
David Howellsd2ddc772017-11-02 15:27:50 +0000175 goto exists;
David Howells08e0e7c2007-04-26 15:55:03 -0700176 }
177
David Howellsd2ddc772017-11-02 15:27:50 +0000178 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 Howells08e0e7c2007-04-26 15:55:03 -0700201 ret = 0;
202
David Howellsd2ddc772017-11-02 15:27:50 +0000203exists:
204 afs_get_server(server);
205 write_sequnlock(&net->fs_lock);
206 return server;
David Howells08e0e7c2007-04-26 15:55:03 -0700207}
208
209/*
210 * allocate a new server record
211 */
David Howellsd2ddc772017-11-02 15:27:50 +0000212static struct afs_server *afs_alloc_server(struct afs_net *net,
213 const uuid_t *uuid,
214 struct afs_addr_list *alist)
David Howells08e0e7c2007-04-26 15:55:03 -0700215{
216 struct afs_server *server;
217
218 _enter("");
219
Yan Burmanb593e482006-12-06 20:40:32 -0800220 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
David Howells8b2a4642017-11-02 15:27:50 +0000221 if (!server)
222 goto enomem;
David Howells08e0e7c2007-04-26 15:55:03 -0700223
David Howells8b2a4642017-11-02 15:27:50 +0000224 atomic_set(&server->usage, 1);
David Howellsd2ddc772017-11-02 15:27:50 +0000225 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 Howells47ea0f22018-06-15 15:24:50 +0100231 INIT_HLIST_HEAD(&server->cb_volumes);
David Howells8b2a4642017-11-02 15:27:50 +0000232 rwlock_init(&server->cb_break_lock);
233
David Howellsd2ddc772017-11-02 15:27:50 +0000234 afs_inc_servers_outstanding(net);
235 _leave(" = %p", server);
David Howells08e0e7c2007-04-26 15:55:03 -0700236 return server;
David Howells8b2a4642017-11-02 15:27:50 +0000237
David Howells8b2a4642017-11-02 15:27:50 +0000238enomem:
239 _leave(" = NULL [nomem]");
240 return NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700241}
242
243/*
David Howellsd2ddc772017-11-02 15:27:50 +0000244 * Look up an address record for a server
David Howells08e0e7c2007-04-26 15:55:03 -0700245 */
David Howellsd2ddc772017-11-02 15:27:50 +0000246static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
247 struct key *key, const uuid_t *uuid)
David Howells08e0e7c2007-04-26 15:55:03 -0700248{
David Howells0a5143f2018-10-20 00:57:57 +0100249 struct afs_vl_cursor vc;
250 struct afs_addr_list *alist = NULL;
David Howellsd2ddc772017-11-02 15:27:50 +0000251 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700252
David Howells0a5143f2018-10-20 00:57:57 +0100253 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 Howells08e0e7c2007-04-26 15:55:03 -0700260 }
David Howells0a5143f2018-10-20 00:57:57 +0100261
262 ret = afs_end_vlserver_operation(&vc);
David Howells08e0e7c2007-04-26 15:55:03 -0700263 }
264
David Howells0a5143f2018-10-20 00:57:57 +0100265 return ret < 0 ? ERR_PTR(ret) : alist;
David Howellsd2ddc772017-11-02 15:27:50 +0000266}
267
268/*
269 * Get or create a fileserver record.
270 */
271struct 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 Howells08e0e7c2007-04-26 15:55:03 -0700300 return server;
David Howellsec268152007-04-26 15:49:28 -0700301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David Howellsd2ddc772017-11-02 15:27:50 +0000303/*
304 * Set the server timer to fire after a given delay, assuming it's not already
305 * set for an earlier time.
306 */
David Howells59fa1c42017-11-02 15:27:45 +0000307static void afs_set_server_timer(struct afs_net *net, time64_t delay)
308{
David Howells59fa1c42017-11-02 15:27:45 +0000309 if (net->live) {
David Howellsd2ddc772017-11-02 15:27:50 +0000310 afs_inc_servers_outstanding(net);
311 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
David Howells59fa1c42017-11-02 15:27:45 +0000312 afs_dec_servers_outstanding(net);
313 }
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
David Howellsd2ddc772017-11-02 15:27:50 +0000317 * Server management timer. We have an increment on fs_outstanding that we
318 * need to pass along to the work item.
319 */
320void 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 Torvalds1da177e2005-04-16 15:20:36 -0700331 */
David Howells9ed900b2017-11-02 15:27:46 +0000332void afs_put_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
David Howellsd2ddc772017-11-02 15:27:50 +0000334 unsigned int usage;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!server)
337 return;
338
David Howellsd2ddc772017-11-02 15:27:50 +0000339 server->put_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
David Howellsd2ddc772017-11-02 15:27:50 +0000341 usage = atomic_dec_return(&server->usage);
David Howells260a9802007-04-26 15:59:35 -0700342
David Howellsd2ddc772017-11-02 15:27:50 +0000343 _enter("{%u}", usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David Howellsd2ddc772017-11-02 15:27:50 +0000345 if (likely(usage > 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Howellsd2ddc772017-11-02 15:27:50 +0000348 afs_set_server_timer(net, afs_server_gc_delay);
349}
350
351static void afs_server_rcu(struct rcu_head *rcu)
352{
353 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
354
David Howellsfe342cf2018-04-09 21:12:31 +0100355 afs_put_addrlist(rcu_access_pointer(server->addresses));
David Howellsd2ddc772017-11-02 15:27:50 +0000356 kfree(server);
David Howellsec268152007-04-26 15:49:28 -0700357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*
David Howells08e0e7c2007-04-26 15:55:03 -0700360 * destroy a dead server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
David Howells59fa1c42017-11-02 15:27:45 +0000362static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
David Howellsfe342cf2018-04-09 21:12:31 +0100364 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
David Howells8b2a4642017-11-02 15:27:50 +0000365 struct afs_addr_cursor ac = {
366 .alist = alist,
David Howells8b2a4642017-11-02 15:27:50 +0000367 .start = alist->index,
David Howellsf2686b02018-05-10 14:12:50 +0100368 .index = 0,
369 .addr = &alist->addrs[alist->index],
David Howells8b2a4642017-11-02 15:27:50 +0000370 .error = 0,
371 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 _enter("%p", server);
373
David Howellsf2686b02018-05-10 14:12:50 +0100374 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
375 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
376
David Howellsd2ddc772017-11-02 15:27:50 +0000377 call_rcu(&server->rcu, afs_server_rcu);
David Howells59fa1c42017-11-02 15:27:45 +0000378 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
David Howellsd2ddc772017-11-02 15:27:50 +0000382 * Garbage collect any expired servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
David Howellsd2ddc772017-11-02 15:27:50 +0000384static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Howells08e0e7c2007-04-26 15:55:03 -0700386 struct afs_server *server;
David Howellsd2ddc772017-11-02 15:27:50 +0000387 bool deleted;
388 int usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
David Howellsd2ddc772017-11-02 15:27:50 +0000390 while ((server = gc_list)) {
391 gc_list = server->gc_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howellsd2ddc772017-11-02 15:27:50 +0000393 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 Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells66062592018-04-18 09:38:34 +0100402 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 Howellsd2ddc772017-11-02 15:27:50 +0000409 afs_destroy_server(net, server);
David Howells66062592018-04-18 09:38:34 +0100410 }
David Howellsd2ddc772017-11-02 15:27:50 +0000411 }
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 */
421void 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 Howellsf044c882017-11-02 15:27:45 +0000457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
David Howellsd2ddc772017-11-02 15:27:50 +0000459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
David Howellsd2ddc772017-11-02 15:27:50 +0000461 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 Howells08e0e7c2007-04-26 15:55:03 -0700473 } else {
David Howellsd2ddc772017-11-02 15:27:50 +0000474 afs_set_server_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
477
David Howellsd2ddc772017-11-02 15:27:50 +0000478 afs_gc_servers(net, gc_list);
David Howells59fa1c42017-11-02 15:27:45 +0000479
480 afs_dec_servers_outstanding(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000481 _leave(" [%d]", atomic_read(&net->servers_outstanding));
482}
483
484static 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 Howellsec268152007-04-26 15:49:28 -0700489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
David Howellsd2ddc772017-11-02 15:27:50 +0000492 * Purge list of servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
David Howellsd2ddc772017-11-02 15:27:50 +0000494void afs_purge_servers(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
David Howellsd2ddc772017-11-02 15:27:50 +0000496 _enter("");
497
498 if (del_timer_sync(&net->fs_timer))
David Howells59fa1c42017-11-02 15:27:45 +0000499 atomic_dec(&net->servers_outstanding);
500
David Howellsd2ddc772017-11-02 15:27:50 +0000501 afs_queue_server_manager(net);
David Howells59fa1c42017-11-02 15:27:45 +0000502
David Howellsd2ddc772017-11-02 15:27:50 +0000503 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100504 wait_var_event(&net->servers_outstanding,
505 !atomic_read(&net->servers_outstanding));
David Howellsd2ddc772017-11-02 15:27:50 +0000506 _leave("");
507}
508
509/*
510 * Probe a fileserver to find its capabilities.
511 *
512 * TODO: Try service upgrade.
513 */
514static 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
550error:
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 */
562bool afs_probe_fileserver(struct afs_fs_cursor *fc)
563{
564 bool success;
565 int ret, retries = 0;
566
567 _enter("");
568
569retry:
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 */
605static 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 */
638bool 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
649retry:
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 Howellsec268152007-04-26 15:49:28 -0700679}