blob: 1880f1b6a9f1e92daeaf3e604d9d392e440ae8a0 [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)) {
David Howellsbf99a532017-11-02 15:27:51 +0000269 if (test_bit(ac.index, &ac.alist->yfs))
270 alist = afs_yfsvl_get_endpoints(cell->net, &ac, key, uuid);
271 else
272 alist = afs_vl_get_addrs_u(cell->net, &ac, key, uuid);
David Howellsd2ddc772017-11-02 15:27:50 +0000273 switch (ac.error) {
274 case 0:
275 afs_end_cursor(&ac);
276 return alist;
277 case -ECONNABORTED:
278 ac.error = afs_abort_to_error(ac.abort_code);
279 goto error;
280 case -ENOMEM:
281 case -ENONET:
282 goto error;
283 case -ENETUNREACH:
284 case -EHOSTUNREACH:
285 case -ECONNREFUSED:
286 break;
287 default:
288 ac.error = -EIO;
289 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700290 }
291 }
292
David Howellsd2ddc772017-11-02 15:27:50 +0000293error:
294 return ERR_PTR(afs_end_cursor(&ac));
295}
296
297/*
298 * Get or create a fileserver record.
299 */
300struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
301 const uuid_t *uuid)
302{
303 struct afs_addr_list *alist;
304 struct afs_server *server, *candidate;
305
306 _enter("%p,%pU", cell->net, uuid);
307
308 server = afs_find_server_by_uuid(cell->net, uuid);
309 if (server)
310 return server;
311
312 alist = afs_vl_lookup_addrs(cell, key, uuid);
313 if (IS_ERR(alist))
314 return ERR_CAST(alist);
315
316 candidate = afs_alloc_server(cell->net, uuid, alist);
317 if (!candidate) {
318 afs_put_addrlist(alist);
319 return ERR_PTR(-ENOMEM);
320 }
321
322 server = afs_install_server(cell->net, candidate);
323 if (server != candidate) {
324 afs_put_addrlist(alist);
325 kfree(candidate);
326 }
327
328 _leave(" = %p{%d}", server, atomic_read(&server->usage));
David Howells08e0e7c2007-04-26 15:55:03 -0700329 return server;
David Howellsec268152007-04-26 15:49:28 -0700330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howellsd2ddc772017-11-02 15:27:50 +0000332/*
333 * Set the server timer to fire after a given delay, assuming it's not already
334 * set for an earlier time.
335 */
David Howells59fa1c42017-11-02 15:27:45 +0000336static void afs_set_server_timer(struct afs_net *net, time64_t delay)
337{
David Howells59fa1c42017-11-02 15:27:45 +0000338 if (net->live) {
David Howellsd2ddc772017-11-02 15:27:50 +0000339 afs_inc_servers_outstanding(net);
340 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
David Howells59fa1c42017-11-02 15:27:45 +0000341 afs_dec_servers_outstanding(net);
342 }
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/*
David Howellsd2ddc772017-11-02 15:27:50 +0000346 * Server management timer. We have an increment on fs_outstanding that we
347 * need to pass along to the work item.
348 */
349void afs_servers_timer(struct timer_list *timer)
350{
351 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
352
353 _enter("");
354 if (!queue_work(afs_wq, &net->fs_manager))
355 afs_dec_servers_outstanding(net);
356}
357
358/*
359 * Release a reference on a server record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
David Howells9ed900b2017-11-02 15:27:46 +0000361void afs_put_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
David Howellsd2ddc772017-11-02 15:27:50 +0000363 unsigned int usage;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!server)
366 return;
367
David Howellsd2ddc772017-11-02 15:27:50 +0000368 server->put_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Howellsd2ddc772017-11-02 15:27:50 +0000370 usage = atomic_dec_return(&server->usage);
David Howells260a9802007-04-26 15:59:35 -0700371
David Howellsd2ddc772017-11-02 15:27:50 +0000372 _enter("{%u}", usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howellsd2ddc772017-11-02 15:27:50 +0000374 if (likely(usage > 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Howellsd2ddc772017-11-02 15:27:50 +0000377 afs_set_server_timer(net, afs_server_gc_delay);
378}
379
380static void afs_server_rcu(struct rcu_head *rcu)
381{
382 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
383
384 afs_put_addrlist(server->addresses);
385 kfree(server);
David Howellsec268152007-04-26 15:49:28 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
David Howells08e0e7c2007-04-26 15:55:03 -0700389 * destroy a dead server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
David Howells59fa1c42017-11-02 15:27:45 +0000391static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
David Howellsd2ddc772017-11-02 15:27:50 +0000393 struct afs_addr_list *alist = server->addresses;
David Howells8b2a4642017-11-02 15:27:50 +0000394 struct afs_addr_cursor ac = {
395 .alist = alist,
396 .addr = &alist->addrs[0],
397 .start = alist->index,
398 .index = alist->index,
399 .error = 0,
400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 _enter("%p", server);
402
David Howellsd2ddc772017-11-02 15:27:50 +0000403 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
404 call_rcu(&server->rcu, afs_server_rcu);
David Howells59fa1c42017-11-02 15:27:45 +0000405 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
David Howellsd2ddc772017-11-02 15:27:50 +0000409 * Garbage collect any expired servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
David Howellsd2ddc772017-11-02 15:27:50 +0000411static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Howells08e0e7c2007-04-26 15:55:03 -0700413 struct afs_server *server;
David Howellsd2ddc772017-11-02 15:27:50 +0000414 bool deleted;
415 int usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howellsd2ddc772017-11-02 15:27:50 +0000417 while ((server = gc_list)) {
418 gc_list = server->gc_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howellsd2ddc772017-11-02 15:27:50 +0000420 write_seqlock(&net->fs_lock);
421 usage = 1;
422 deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
423 if (deleted) {
424 rb_erase(&server->uuid_rb, &net->fs_servers);
425 hlist_del_rcu(&server->proc_link);
426 }
427 write_sequnlock(&net->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Howellsd2ddc772017-11-02 15:27:50 +0000429 if (deleted)
430 afs_destroy_server(net, server);
431 }
432}
433
434/*
435 * Manage the records of servers known to be within a network namespace. This
436 * includes garbage collecting unused servers.
437 *
438 * Note also that we were given an increment on net->servers_outstanding by
439 * whoever queued us that we need to deal with before returning.
440 */
441void afs_manage_servers(struct work_struct *work)
442{
443 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
444 struct afs_server *gc_list = NULL;
445 struct rb_node *cursor;
446 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
447 bool purging = !net->live;
448
449 _enter("");
450
451 /* Trawl the server list looking for servers that have expired from
452 * lack of use.
453 */
454 read_seqlock_excl(&net->fs_lock);
455
456 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
457 struct afs_server *server =
458 rb_entry(cursor, struct afs_server, uuid_rb);
459 int usage = atomic_read(&server->usage);
460
461 _debug("manage %pU %u", &server->uuid, usage);
462
463 ASSERTCMP(usage, >=, 1);
464 ASSERTIFCMP(purging, usage, ==, 1);
465
466 if (usage == 1) {
467 time64_t expire_at = server->put_time;
468
469 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
470 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
471 expire_at += afs_server_gc_delay;
472 if (purging || expire_at <= now) {
473 server->gc_next = gc_list;
474 gc_list = server;
475 } else if (expire_at < next_manage) {
476 next_manage = expire_at;
David Howellsf044c882017-11-02 15:27:45 +0000477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
David Howellsd2ddc772017-11-02 15:27:50 +0000479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
David Howellsd2ddc772017-11-02 15:27:50 +0000481 read_sequnlock_excl(&net->fs_lock);
482
483 /* Update the timer on the way out. We have to pass an increment on
484 * servers_outstanding in the namespace that we are in to the timer or
485 * the work scheduler.
486 */
487 if (!purging && next_manage < TIME64_MAX) {
488 now = ktime_get_real_seconds();
489
490 if (next_manage - now <= 0) {
491 if (queue_work(afs_wq, &net->fs_manager))
492 afs_inc_servers_outstanding(net);
David Howells08e0e7c2007-04-26 15:55:03 -0700493 } else {
David Howellsd2ddc772017-11-02 15:27:50 +0000494 afs_set_server_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497
David Howellsd2ddc772017-11-02 15:27:50 +0000498 afs_gc_servers(net, gc_list);
David Howells59fa1c42017-11-02 15:27:45 +0000499
500 afs_dec_servers_outstanding(net);
David Howellsd2ddc772017-11-02 15:27:50 +0000501 _leave(" [%d]", atomic_read(&net->servers_outstanding));
502}
503
504static void afs_queue_server_manager(struct afs_net *net)
505{
506 afs_inc_servers_outstanding(net);
507 if (!queue_work(afs_wq, &net->fs_manager))
508 afs_dec_servers_outstanding(net);
David Howellsec268152007-04-26 15:49:28 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
David Howellsd2ddc772017-11-02 15:27:50 +0000512 * Purge list of servers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
David Howellsd2ddc772017-11-02 15:27:50 +0000514void afs_purge_servers(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
David Howellsd2ddc772017-11-02 15:27:50 +0000516 _enter("");
517
518 if (del_timer_sync(&net->fs_timer))
David Howells59fa1c42017-11-02 15:27:45 +0000519 atomic_dec(&net->servers_outstanding);
520
David Howellsd2ddc772017-11-02 15:27:50 +0000521 afs_queue_server_manager(net);
David Howells59fa1c42017-11-02 15:27:45 +0000522
David Howellsd2ddc772017-11-02 15:27:50 +0000523 _debug("wait");
David Howells59fa1c42017-11-02 15:27:45 +0000524 wait_on_atomic_t(&net->servers_outstanding, atomic_t_wait,
525 TASK_UNINTERRUPTIBLE);
David Howellsd2ddc772017-11-02 15:27:50 +0000526 _leave("");
527}
528
529/*
530 * Probe a fileserver to find its capabilities.
531 *
532 * TODO: Try service upgrade.
533 */
534static bool afs_do_probe_fileserver(struct afs_fs_cursor *fc)
535{
536 _enter("");
537
538 fc->ac.addr = NULL;
539 fc->ac.start = READ_ONCE(fc->ac.alist->index);
540 fc->ac.index = fc->ac.start;
541 fc->ac.error = 0;
542 fc->ac.begun = false;
543
544 while (afs_iterate_addresses(&fc->ac)) {
545 afs_fs_get_capabilities(afs_v2net(fc->vnode), fc->cbi->server,
546 &fc->ac, fc->key);
547 switch (fc->ac.error) {
548 case 0:
549 afs_end_cursor(&fc->ac);
550 set_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags);
551 return true;
552 case -ECONNABORTED:
553 fc->ac.error = afs_abort_to_error(fc->ac.abort_code);
554 goto error;
555 case -ENOMEM:
556 case -ENONET:
557 goto error;
558 case -ENETUNREACH:
559 case -EHOSTUNREACH:
560 case -ECONNREFUSED:
561 case -ETIMEDOUT:
562 case -ETIME:
563 break;
564 default:
565 fc->ac.error = -EIO;
566 goto error;
567 }
568 }
569
570error:
571 afs_end_cursor(&fc->ac);
572 return false;
573}
574
575/*
576 * If we haven't already, try probing the fileserver to get its capabilities.
577 * We try not to instigate parallel probes, but it's possible that the parallel
578 * probes will fail due to authentication failure when ours would succeed.
579 *
580 * TODO: Try sending an anonymous probe if an authenticated probe fails.
581 */
582bool afs_probe_fileserver(struct afs_fs_cursor *fc)
583{
584 bool success;
585 int ret, retries = 0;
586
587 _enter("");
588
589retry:
590 if (test_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags)) {
591 _leave(" = t");
592 return true;
593 }
594
595 if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags)) {
596 success = afs_do_probe_fileserver(fc);
597 clear_bit_unlock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags);
598 wake_up_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING);
599 _leave(" = t");
600 return success;
601 }
602
603 _debug("wait");
604 ret = wait_on_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING,
605 TASK_INTERRUPTIBLE);
606 if (ret == -ERESTARTSYS) {
607 fc->ac.error = ret;
608 _leave(" = f [%d]", ret);
609 return false;
610 }
611
612 retries++;
613 if (retries == 4) {
614 fc->ac.error = -ESTALE;
615 _leave(" = f [stale]");
616 return false;
617 }
618 _debug("retry");
619 goto retry;
620}
621
622/*
623 * Get an update for a server's address list.
624 */
625static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
626{
627 struct afs_addr_list *alist, *discard;
628
629 _enter("");
630
631 alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
632 &server->uuid);
633 if (IS_ERR(alist)) {
634 fc->ac.error = PTR_ERR(alist);
635 _leave(" = f [%d]", fc->ac.error);
636 return false;
637 }
638
639 discard = alist;
640 if (server->addr_version != alist->version) {
641 write_lock(&server->fs_lock);
642 discard = rcu_dereference_protected(server->addresses,
643 lockdep_is_held(&server->fs_lock));
644 rcu_assign_pointer(server->addresses, alist);
645 server->addr_version = alist->version;
646 write_unlock(&server->fs_lock);
647 }
648
649 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
650 afs_put_addrlist(discard);
651 _leave(" = t");
652 return true;
653}
654
655/*
656 * See if a server's address list needs updating.
657 */
658bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
659{
660 time64_t now = ktime_get_real_seconds();
661 long diff;
662 bool success;
663 int ret, retries = 0;
664
665 _enter("");
666
667 ASSERT(server);
668
669retry:
670 diff = READ_ONCE(server->update_at) - now;
671 if (diff > 0) {
672 _leave(" = t [not now %ld]", diff);
673 return true;
674 }
675
676 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
677 success = afs_update_server_record(fc, server);
678 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
679 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
680 _leave(" = %d", success);
681 return success;
682 }
683
684 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
685 TASK_INTERRUPTIBLE);
686 if (ret == -ERESTARTSYS) {
687 fc->ac.error = ret;
688 _leave(" = f [intr]");
689 return false;
690 }
691
692 retries++;
693 if (retries == 4) {
694 _leave(" = f [stale]");
695 ret = -ESTALE;
696 return false;
697 }
698 goto retry;
David Howellsec268152007-04-26 15:49:28 -0700699}