blob: a6c860bcf391b32ca6e1e2e7575e393a126892dc [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))
28 wake_up_atomic_t(&net->servers_outstanding);
29}
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;
62 diff = (u16)a->sin6_port - (u16)b->sin6_port;
63 if (diff == 0)
64 diff = memcmp(&a->sin6_addr,
65 &b->sin6_addr,
66 sizeof(struct in6_addr));
67 if (diff == 0)
68 goto found;
69 if (diff < 0) {
70 // TODO: Sort the list
71 //if (i == alist->nr_ipv4)
72 // goto not_found;
73 break;
74 }
75 }
76 }
77 } else {
78 hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) {
79 alist = rcu_dereference(server->addresses);
80 for (i = 0; i < alist->nr_ipv4; i++) {
81 b = &alist->addrs[i].transport.sin6;
82 diff = (u16)a->sin6_port - (u16)b->sin6_port;
83 if (diff == 0)
84 diff = ((u32)a->sin6_addr.s6_addr32[3] -
85 (u32)b->sin6_addr.s6_addr32[3]);
86 if (diff == 0)
87 goto found;
88 if (diff < 0) {
89 // TODO: Sort the list
90 //if (i == 0)
91 // goto not_found;
92 break;
93 }
94 }
95 }
96 }
97
98 //not_found:
99 server = NULL;
100 found:
101 if (server && !atomic_inc_not_zero(&server->usage))
102 server = NULL;
103
104 } while (need_seqretry(&net->fs_addr_lock, seq));
105
106 done_seqretry(&net->fs_addr_lock, seq);
107
108 rcu_read_unlock();
109 return server;
David Howells59fa1c42017-11-02 15:27:45 +0000110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
David Howellsd2ddc772017-11-02 15:27:50 +0000113 * Look up a server by its UUID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
David Howellsd2ddc772017-11-02 15:27:50 +0000115struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David Howellsd2ddc772017-11-02 15:27:50 +0000117 struct afs_server *server = NULL;
118 struct rb_node *p;
119 int diff, seq = 0;
120
121 _enter("%pU", uuid);
122
123 do {
124 /* Unfortunately, rbtree walking doesn't give reliable results
125 * under just the RCU read lock, so we have to check for
126 * changes.
127 */
128 if (server)
129 afs_put_server(net, server);
130 server = NULL;
131
132 read_seqbegin_or_lock(&net->fs_lock, &seq);
133
134 p = net->fs_servers.rb_node;
135 while (p) {
136 server = rb_entry(p, struct afs_server, uuid_rb);
137
138 diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
139 if (diff < 0) {
140 p = p->rb_left;
141 } else if (diff > 0) {
142 p = p->rb_right;
143 } else {
144 afs_get_server(server);
145 break;
146 }
147
148 server = NULL;
149 }
150 } while (need_seqretry(&net->fs_lock, seq));
151
152 done_seqretry(&net->fs_lock, seq);
153
154 _leave(" = %p", server);
155 return server;
156}
157
158/*
159 * Install a server record in the namespace tree
160 */
161static struct afs_server *afs_install_server(struct afs_net *net,
162 struct afs_server *candidate)
163{
164 const struct afs_addr_list *alist;
165 struct afs_server *server;
David Howells08e0e7c2007-04-26 15:55:03 -0700166 struct rb_node **pp, *p;
David Howellsd2ddc772017-11-02 15:27:50 +0000167 int ret = -EEXIST, diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David Howellsd2ddc772017-11-02 15:27:50 +0000169 _enter("%p", candidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Howellsd2ddc772017-11-02 15:27:50 +0000171 write_seqlock(&net->fs_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700172
David Howellsd2ddc772017-11-02 15:27:50 +0000173 /* Firstly install the server in the UUID lookup tree */
174 pp = &net->fs_servers.rb_node;
David Howells08e0e7c2007-04-26 15:55:03 -0700175 p = NULL;
176 while (*pp) {
177 p = *pp;
178 _debug("- consider %p", p);
David Howellsd2ddc772017-11-02 15:27:50 +0000179 server = rb_entry(p, struct afs_server, uuid_rb);
180 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
David Howells4d9df982017-11-02 15:27:47 +0000181 if (diff < 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700182 pp = &(*pp)->rb_left;
David Howells4d9df982017-11-02 15:27:47 +0000183 else if (diff > 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700184 pp = &(*pp)->rb_right;
185 else
David Howellsd2ddc772017-11-02 15:27:50 +0000186 goto exists;
David Howells08e0e7c2007-04-26 15:55:03 -0700187 }
188
David Howellsd2ddc772017-11-02 15:27:50 +0000189 server = candidate;
190 rb_link_node(&server->uuid_rb, p, pp);
191 rb_insert_color(&server->uuid_rb, &net->fs_servers);
192 hlist_add_head_rcu(&server->proc_link, &net->fs_proc);
193
194 write_seqlock(&net->fs_addr_lock);
195 alist = rcu_dereference_protected(server->addresses,
196 lockdep_is_held(&net->fs_addr_lock.lock));
197
198 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
199 * it in the IPv4 and/or IPv6 reverse-map lists.
200 *
201 * TODO: For speed we want to use something other than a flat list
202 * here; even sorting the list in terms of lowest address would help a
203 * bit, but anything we might want to do gets messy and memory
204 * intensive.
205 */
206 if (alist->nr_ipv4 > 0)
207 hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4);
208 if (alist->nr_addrs > alist->nr_ipv4)
209 hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6);
210
211 write_sequnlock(&net->fs_addr_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700212 ret = 0;
213
David Howellsd2ddc772017-11-02 15:27:50 +0000214exists:
215 afs_get_server(server);
216 write_sequnlock(&net->fs_lock);
217 return server;
David Howells08e0e7c2007-04-26 15:55:03 -0700218}
219
220/*
221 * allocate a new server record
222 */
David Howellsd2ddc772017-11-02 15:27:50 +0000223static struct afs_server *afs_alloc_server(struct afs_net *net,
224 const uuid_t *uuid,
225 struct afs_addr_list *alist)
David Howells08e0e7c2007-04-26 15:55:03 -0700226{
227 struct afs_server *server;
228
229 _enter("");
230
Yan Burmanb593e482006-12-06 20:40:32 -0800231 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
David Howells8b2a4642017-11-02 15:27:50 +0000232 if (!server)
233 goto enomem;
David Howells08e0e7c2007-04-26 15:55:03 -0700234
David Howells8b2a4642017-11-02 15:27:50 +0000235 atomic_set(&server->usage, 1);
David Howellsd2ddc772017-11-02 15:27:50 +0000236 RCU_INIT_POINTER(server->addresses, alist);
237 server->addr_version = alist->version;
238 server->uuid = *uuid;
239 server->flags = (1UL << AFS_SERVER_FL_NEW);
240 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
241 rwlock_init(&server->fs_lock);
David Howells8b2a4642017-11-02 15:27:50 +0000242 INIT_LIST_HEAD(&server->cb_interests);
243 rwlock_init(&server->cb_break_lock);
244
David Howellsd2ddc772017-11-02 15:27:50 +0000245 afs_inc_servers_outstanding(net);
246 _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 Howellsd2ddc772017-11-02 15:27:50 +0000260 struct afs_addr_cursor ac;
261 struct afs_addr_list *alist;
262 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700263
David Howellsd2ddc772017-11-02 15:27:50 +0000264 ret = afs_set_vl_cursor(&ac, cell);
265 if (ret < 0)
266 return ERR_PTR(ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700267
David Howellsd2ddc772017-11-02 15:27:50 +0000268 while (afs_iterate_addresses(&ac)) {
269 alist = afs_vl_get_addrs_u(cell->net, &ac, key, uuid);
270 switch (ac.error) {
271 case 0:
272 afs_end_cursor(&ac);
273 return alist;
274 case -ECONNABORTED:
275 ac.error = afs_abort_to_error(ac.abort_code);
276 goto error;
277 case -ENOMEM:
278 case -ENONET:
279 goto error;
280 case -ENETUNREACH:
281 case -EHOSTUNREACH:
282 case -ECONNREFUSED:
283 break;
284 default:
285 ac.error = -EIO;
286 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700287 }
288 }
289
David Howellsd2ddc772017-11-02 15:27:50 +0000290error:
291 return ERR_PTR(afs_end_cursor(&ac));
292}
293
294/*
295 * Get or create a fileserver record.
296 */
297struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
298 const uuid_t *uuid)
299{
300 struct afs_addr_list *alist;
301 struct afs_server *server, *candidate;
302
303 _enter("%p,%pU", cell->net, uuid);
304
305 server = afs_find_server_by_uuid(cell->net, uuid);
306 if (server)
307 return server;
308
309 alist = afs_vl_lookup_addrs(cell, key, uuid);
310 if (IS_ERR(alist))
311 return ERR_CAST(alist);
312
313 candidate = afs_alloc_server(cell->net, uuid, alist);
314 if (!candidate) {
315 afs_put_addrlist(alist);
316 return ERR_PTR(-ENOMEM);
317 }
318
319 server = afs_install_server(cell->net, candidate);
320 if (server != candidate) {
321 afs_put_addrlist(alist);
322 kfree(candidate);
323 }
324
325 _leave(" = %p{%d}", server, atomic_read(&server->usage));
David Howells08e0e7c2007-04-26 15:55:03 -0700326 return server;
David Howellsec268152007-04-26 15:49:28 -0700327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Howellsd2ddc772017-11-02 15:27:50 +0000329/*
330 * Set the server timer to fire after a given delay, assuming it's not already
331 * set for an earlier time.
332 */
David Howells59fa1c42017-11-02 15:27:45 +0000333static void afs_set_server_timer(struct afs_net *net, time64_t delay)
334{
David Howells59fa1c42017-11-02 15:27:45 +0000335 if (net->live) {
David Howellsd2ddc772017-11-02 15:27:50 +0000336 afs_inc_servers_outstanding(net);
337 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
David Howells59fa1c42017-11-02 15:27:45 +0000338 afs_dec_servers_outstanding(net);
339 }
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*
David Howellsd2ddc772017-11-02 15:27:50 +0000343 * Server management timer. We have an increment on fs_outstanding that we
344 * need to pass along to the work item.
345 */
346void afs_servers_timer(struct timer_list *timer)
347{
348 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
349
350 _enter("");
351 if (!queue_work(afs_wq, &net->fs_manager))
352 afs_dec_servers_outstanding(net);
353}
354
355/*
356 * Release a reference on a server record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
David Howells9ed900b2017-11-02 15:27:46 +0000358void afs_put_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Howellsd2ddc772017-11-02 15:27:50 +0000360 unsigned int usage;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!server)
363 return;
364
David Howellsd2ddc772017-11-02 15:27:50 +0000365 server->put_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howellsd2ddc772017-11-02 15:27:50 +0000367 usage = atomic_dec_return(&server->usage);
David Howells260a9802007-04-26 15:59:35 -0700368
David Howellsd2ddc772017-11-02 15:27:50 +0000369 _enter("{%u}", usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Howellsd2ddc772017-11-02 15:27:50 +0000371 if (likely(usage > 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howellsd2ddc772017-11-02 15:27:50 +0000374 afs_set_server_timer(net, afs_server_gc_delay);
375}
376
377static void afs_server_rcu(struct rcu_head *rcu)
378{
379 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
380
381 afs_put_addrlist(server->addresses);
382 kfree(server);
David Howellsec268152007-04-26 15:49:28 -0700383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
David Howells08e0e7c2007-04-26 15:55:03 -0700386 * destroy a dead server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
David Howells59fa1c42017-11-02 15:27:45 +0000388static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
David Howellsd2ddc772017-11-02 15:27:50 +0000390 struct afs_addr_list *alist = server->addresses;
David Howells8b2a4642017-11-02 15:27:50 +0000391 struct afs_addr_cursor ac = {
392 .alist = alist,
393 .addr = &alist->addrs[0],
394 .start = alist->index,
395 .index = alist->index,
396 .error = 0,
397 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 _enter("%p", server);
399
David Howellsd2ddc772017-11-02 15:27:50 +0000400 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
401 call_rcu(&server->rcu, afs_server_rcu);
David Howells59fa1c42017-11-02 15:27:45 +0000402 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
David Howellsd2ddc772017-11-02 15:27:50 +0000406 * Garbage collect any expired servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
David Howellsd2ddc772017-11-02 15:27:50 +0000408static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
David Howells08e0e7c2007-04-26 15:55:03 -0700410 struct afs_server *server;
David Howellsd2ddc772017-11-02 15:27:50 +0000411 bool deleted;
412 int usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Howellsd2ddc772017-11-02 15:27:50 +0000414 while ((server = gc_list)) {
415 gc_list = server->gc_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howellsd2ddc772017-11-02 15:27:50 +0000417 write_seqlock(&net->fs_lock);
418 usage = 1;
419 deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
420 if (deleted) {
421 rb_erase(&server->uuid_rb, &net->fs_servers);
422 hlist_del_rcu(&server->proc_link);
423 }
424 write_sequnlock(&net->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howellsd2ddc772017-11-02 15:27:50 +0000426 if (deleted)
427 afs_destroy_server(net, server);
428 }
429}
430
431/*
432 * Manage the records of servers known to be within a network namespace. This
433 * includes garbage collecting unused servers.
434 *
435 * Note also that we were given an increment on net->servers_outstanding by
436 * whoever queued us that we need to deal with before returning.
437 */
438void afs_manage_servers(struct work_struct *work)
439{
440 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
441 struct afs_server *gc_list = NULL;
442 struct rb_node *cursor;
443 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
444 bool purging = !net->live;
445
446 _enter("");
447
448 /* Trawl the server list looking for servers that have expired from
449 * lack of use.
450 */
451 read_seqlock_excl(&net->fs_lock);
452
453 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
454 struct afs_server *server =
455 rb_entry(cursor, struct afs_server, uuid_rb);
456 int usage = atomic_read(&server->usage);
457
458 _debug("manage %pU %u", &server->uuid, usage);
459
460 ASSERTCMP(usage, >=, 1);
461 ASSERTIFCMP(purging, usage, ==, 1);
462
463 if (usage == 1) {
464 time64_t expire_at = server->put_time;
465
466 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
467 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
468 expire_at += afs_server_gc_delay;
469 if (purging || expire_at <= now) {
470 server->gc_next = gc_list;
471 gc_list = server;
472 } else if (expire_at < next_manage) {
473 next_manage = expire_at;
David Howellsf044c882017-11-02 15:27:45 +0000474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
David Howellsd2ddc772017-11-02 15:27:50 +0000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David Howellsd2ddc772017-11-02 15:27:50 +0000478 read_sequnlock_excl(&net->fs_lock);
479
480 /* Update the timer on the way out. We have to pass an increment on
481 * servers_outstanding in the namespace that we are in to the timer or
482 * the work scheduler.
483 */
484 if (!purging && next_manage < TIME64_MAX) {
485 now = ktime_get_real_seconds();
486
487 if (next_manage - now <= 0) {
488 if (queue_work(afs_wq, &net->fs_manager))
489 afs_inc_servers_outstanding(net);
David Howells08e0e7c2007-04-26 15:55:03 -0700490 } else {
David Howellsd2ddc772017-11-02 15:27:50 +0000491 afs_set_server_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 }
494
David Howellsd2ddc772017-11-02 15:27:50 +0000495 afs_gc_servers(net, gc_list);
David Howells59fa1c42017-11-02 15:27:45 +0000496
497 afs_dec_servers_outstanding(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000498 _leave(" [%d]", atomic_read(&net->servers_outstanding));
499}
500
501static void afs_queue_server_manager(struct afs_net *net)
502{
503 afs_inc_servers_outstanding(net);
504 if (!queue_work(afs_wq, &net->fs_manager))
505 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
David Howellsd2ddc772017-11-02 15:27:50 +0000509 * Purge list of servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
David Howellsd2ddc772017-11-02 15:27:50 +0000511void afs_purge_servers(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
David Howellsd2ddc772017-11-02 15:27:50 +0000513 _enter("");
514
515 if (del_timer_sync(&net->fs_timer))
David Howells59fa1c42017-11-02 15:27:45 +0000516 atomic_dec(&net->servers_outstanding);
517
David Howellsd2ddc772017-11-02 15:27:50 +0000518 afs_queue_server_manager(net);
David Howells59fa1c42017-11-02 15:27:45 +0000519
David Howellsd2ddc772017-11-02 15:27:50 +0000520 _debug("wait");
David Howells59fa1c42017-11-02 15:27:45 +0000521 wait_on_atomic_t(&net->servers_outstanding, atomic_t_wait,
522 TASK_UNINTERRUPTIBLE);
David Howellsd2ddc772017-11-02 15:27:50 +0000523 _leave("");
524}
525
526/*
527 * Probe a fileserver to find its capabilities.
528 *
529 * TODO: Try service upgrade.
530 */
531static bool afs_do_probe_fileserver(struct afs_fs_cursor *fc)
532{
533 _enter("");
534
535 fc->ac.addr = NULL;
536 fc->ac.start = READ_ONCE(fc->ac.alist->index);
537 fc->ac.index = fc->ac.start;
538 fc->ac.error = 0;
539 fc->ac.begun = false;
540
541 while (afs_iterate_addresses(&fc->ac)) {
542 afs_fs_get_capabilities(afs_v2net(fc->vnode), fc->cbi->server,
543 &fc->ac, fc->key);
544 switch (fc->ac.error) {
545 case 0:
546 afs_end_cursor(&fc->ac);
547 set_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags);
548 return true;
549 case -ECONNABORTED:
550 fc->ac.error = afs_abort_to_error(fc->ac.abort_code);
551 goto error;
552 case -ENOMEM:
553 case -ENONET:
554 goto error;
555 case -ENETUNREACH:
556 case -EHOSTUNREACH:
557 case -ECONNREFUSED:
558 case -ETIMEDOUT:
559 case -ETIME:
560 break;
561 default:
562 fc->ac.error = -EIO;
563 goto error;
564 }
565 }
566
567error:
568 afs_end_cursor(&fc->ac);
569 return false;
570}
571
572/*
573 * If we haven't already, try probing the fileserver to get its capabilities.
574 * We try not to instigate parallel probes, but it's possible that the parallel
575 * probes will fail due to authentication failure when ours would succeed.
576 *
577 * TODO: Try sending an anonymous probe if an authenticated probe fails.
578 */
579bool afs_probe_fileserver(struct afs_fs_cursor *fc)
580{
581 bool success;
582 int ret, retries = 0;
583
584 _enter("");
585
586retry:
587 if (test_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags)) {
588 _leave(" = t");
589 return true;
590 }
591
592 if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags)) {
593 success = afs_do_probe_fileserver(fc);
594 clear_bit_unlock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags);
595 wake_up_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING);
596 _leave(" = t");
597 return success;
598 }
599
600 _debug("wait");
601 ret = wait_on_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING,
602 TASK_INTERRUPTIBLE);
603 if (ret == -ERESTARTSYS) {
604 fc->ac.error = ret;
605 _leave(" = f [%d]", ret);
606 return false;
607 }
608
609 retries++;
610 if (retries == 4) {
611 fc->ac.error = -ESTALE;
612 _leave(" = f [stale]");
613 return false;
614 }
615 _debug("retry");
616 goto retry;
617}
618
619/*
620 * Get an update for a server's address list.
621 */
622static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
623{
624 struct afs_addr_list *alist, *discard;
625
626 _enter("");
627
628 alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
629 &server->uuid);
630 if (IS_ERR(alist)) {
631 fc->ac.error = PTR_ERR(alist);
632 _leave(" = f [%d]", fc->ac.error);
633 return false;
634 }
635
636 discard = alist;
637 if (server->addr_version != alist->version) {
638 write_lock(&server->fs_lock);
639 discard = rcu_dereference_protected(server->addresses,
640 lockdep_is_held(&server->fs_lock));
641 rcu_assign_pointer(server->addresses, alist);
642 server->addr_version = alist->version;
643 write_unlock(&server->fs_lock);
644 }
645
646 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
647 afs_put_addrlist(discard);
648 _leave(" = t");
649 return true;
650}
651
652/*
653 * See if a server's address list needs updating.
654 */
655bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
656{
657 time64_t now = ktime_get_real_seconds();
658 long diff;
659 bool success;
660 int ret, retries = 0;
661
662 _enter("");
663
664 ASSERT(server);
665
666retry:
667 diff = READ_ONCE(server->update_at) - now;
668 if (diff > 0) {
669 _leave(" = t [not now %ld]", diff);
670 return true;
671 }
672
673 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
674 success = afs_update_server_record(fc, server);
675 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
676 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
677 _leave(" = %d", success);
678 return success;
679 }
680
681 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
682 TASK_INTERRUPTIBLE);
683 if (ret == -ERESTARTSYS) {
684 fc->ac.error = ret;
685 _leave(" = f [intr]");
686 return false;
687 }
688
689 retries++;
690 if (retries == 4) {
691 _leave(" = f [stale]");
692 ret = -ESTALE;
693 return false;
694 }
695 goto retry;
David Howellsec268152007-04-26 15:49:28 -0700696}