1 /* AFS server record management
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
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.
12 #include <linux/sched.h>
13 #include <linux/slab.h>
16 #include "protocol_yfs.h"
18 static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */
19 static unsigned afs_server_update_delay = 30; /* Time till VLDB recheck in secs */
21 static void afs_inc_servers_outstanding(struct afs_net *net)
23 atomic_inc(&net->servers_outstanding);
26 static void afs_dec_servers_outstanding(struct afs_net *net)
28 if (atomic_dec_and_test(&net->servers_outstanding))
29 wake_up_var(&net->servers_outstanding);
33 * Find a server by one of its addresses.
35 struct afs_server *afs_find_server(struct afs_net *net,
36 const struct sockaddr_rxrpc *srx)
38 const struct sockaddr_in6 *a = &srx->transport.sin6, *b;
39 const struct afs_addr_list *alist;
40 struct afs_server *server = NULL;
45 if (srx->transport.sin6.sin6_addr.s6_addr32[0] == 0 ||
46 srx->transport.sin6.sin6_addr.s6_addr32[1] == 0 ||
47 srx->transport.sin6.sin6_addr.s6_addr32[2] == htonl(0xffff))
54 afs_put_server(net, server);
56 read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
59 hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) {
60 alist = rcu_dereference(server->addresses);
61 for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) {
62 b = &alist->addrs[i].transport.sin6;
63 diff = ((u16 __force)a->sin6_port -
64 (u16 __force)b->sin6_port);
66 diff = memcmp(&a->sin6_addr,
68 sizeof(struct in6_addr));
74 hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) {
75 alist = rcu_dereference(server->addresses);
76 for (i = 0; i < alist->nr_ipv4; i++) {
77 b = &alist->addrs[i].transport.sin6;
78 diff = ((u16 __force)a->sin6_port -
79 (u16 __force)b->sin6_port);
81 diff = ((u32 __force)a->sin6_addr.s6_addr32[3] -
82 (u32 __force)b->sin6_addr.s6_addr32[3]);
91 if (server && !atomic_inc_not_zero(&server->usage))
94 } while (need_seqretry(&net->fs_addr_lock, seq));
96 done_seqretry(&net->fs_addr_lock, seq);
103 * Look up a server by its UUID
105 struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
107 struct afs_server *server = NULL;
114 /* Unfortunately, rbtree walking doesn't give reliable results
115 * under just the RCU read lock, so we have to check for
119 afs_put_server(net, server);
122 read_seqbegin_or_lock(&net->fs_lock, &seq);
124 p = net->fs_servers.rb_node;
126 server = rb_entry(p, struct afs_server, uuid_rb);
128 diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
131 } else if (diff > 0) {
134 afs_get_server(server);
140 } while (need_seqretry(&net->fs_lock, seq));
142 done_seqretry(&net->fs_lock, seq);
144 _leave(" = %p", server);
149 * Install a server record in the namespace tree
151 static struct afs_server *afs_install_server(struct afs_net *net,
152 struct afs_server *candidate)
154 const struct afs_addr_list *alist;
155 struct afs_server *server;
156 struct rb_node **pp, *p;
157 int ret = -EEXIST, diff;
159 _enter("%p", candidate);
161 write_seqlock(&net->fs_lock);
163 /* Firstly install the server in the UUID lookup tree */
164 pp = &net->fs_servers.rb_node;
168 _debug("- consider %p", p);
169 server = rb_entry(p, struct afs_server, uuid_rb);
170 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
172 pp = &(*pp)->rb_left;
174 pp = &(*pp)->rb_right;
180 rb_link_node(&server->uuid_rb, p, pp);
181 rb_insert_color(&server->uuid_rb, &net->fs_servers);
182 hlist_add_head_rcu(&server->proc_link, &net->fs_proc);
184 write_seqlock(&net->fs_addr_lock);
185 alist = rcu_dereference_protected(server->addresses,
186 lockdep_is_held(&net->fs_addr_lock.lock));
188 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
189 * it in the IPv4 and/or IPv6 reverse-map lists.
191 * TODO: For speed we want to use something other than a flat list
192 * here; even sorting the list in terms of lowest address would help a
193 * bit, but anything we might want to do gets messy and memory
196 if (alist->nr_ipv4 > 0)
197 hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4);
198 if (alist->nr_addrs > alist->nr_ipv4)
199 hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6);
201 write_sequnlock(&net->fs_addr_lock);
205 afs_get_server(server);
206 write_sequnlock(&net->fs_lock);
211 * allocate a new server record
213 static struct afs_server *afs_alloc_server(struct afs_net *net,
215 struct afs_addr_list *alist)
217 struct afs_server *server;
221 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
225 atomic_set(&server->usage, 1);
226 RCU_INIT_POINTER(server->addresses, alist);
227 server->addr_version = alist->version;
228 server->uuid = *uuid;
229 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
230 rwlock_init(&server->fs_lock);
231 INIT_HLIST_HEAD(&server->cb_volumes);
232 rwlock_init(&server->cb_break_lock);
233 init_waitqueue_head(&server->probe_wq);
234 spin_lock_init(&server->probe_lock);
236 afs_inc_servers_outstanding(net);
237 _leave(" = %p", server);
241 _leave(" = NULL [nomem]");
246 * Look up an address record for a server
248 static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
249 struct key *key, const uuid_t *uuid)
251 struct afs_vl_cursor vc;
252 struct afs_addr_list *alist = NULL;
256 if (afs_begin_vlserver_operation(&vc, cell, key)) {
257 while (afs_select_vlserver(&vc)) {
258 if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags))
259 alist = afs_yfsvl_get_endpoints(&vc, uuid);
261 alist = afs_vl_get_addrs_u(&vc, uuid);
264 ret = afs_end_vlserver_operation(&vc);
267 return ret < 0 ? ERR_PTR(ret) : alist;
271 * Get or create a fileserver record.
273 struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
276 struct afs_addr_list *alist;
277 struct afs_server *server, *candidate;
279 _enter("%p,%pU", cell->net, uuid);
281 server = afs_find_server_by_uuid(cell->net, uuid);
285 alist = afs_vl_lookup_addrs(cell, key, uuid);
287 return ERR_CAST(alist);
289 candidate = afs_alloc_server(cell->net, uuid, alist);
291 afs_put_addrlist(alist);
292 return ERR_PTR(-ENOMEM);
295 server = afs_install_server(cell->net, candidate);
296 if (server != candidate) {
297 afs_put_addrlist(alist);
301 _leave(" = %p{%d}", server, atomic_read(&server->usage));
306 * Set the server timer to fire after a given delay, assuming it's not already
307 * set for an earlier time.
309 static void afs_set_server_timer(struct afs_net *net, time64_t delay)
312 afs_inc_servers_outstanding(net);
313 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
314 afs_dec_servers_outstanding(net);
319 * Server management timer. We have an increment on fs_outstanding that we
320 * need to pass along to the work item.
322 void afs_servers_timer(struct timer_list *timer)
324 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
327 if (!queue_work(afs_wq, &net->fs_manager))
328 afs_dec_servers_outstanding(net);
332 * Release a reference on a server record.
334 void afs_put_server(struct afs_net *net, struct afs_server *server)
341 server->put_time = ktime_get_real_seconds();
343 usage = atomic_dec_return(&server->usage);
345 _enter("{%u}", usage);
347 if (likely(usage > 0))
350 afs_set_server_timer(net, afs_server_gc_delay);
353 static void afs_server_rcu(struct rcu_head *rcu)
355 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
357 afs_put_addrlist(rcu_access_pointer(server->addresses));
362 * destroy a dead server
364 static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
366 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
367 struct afs_addr_cursor ac = {
369 .index = alist->preferred,
372 _enter("%p", server);
374 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
375 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
377 wait_var_event(&server->probe_outstanding,
378 atomic_read(&server->probe_outstanding) == 0);
380 call_rcu(&server->rcu, afs_server_rcu);
381 afs_dec_servers_outstanding(net);
385 * Garbage collect any expired servers.
387 static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
389 struct afs_server *server;
393 while ((server = gc_list)) {
394 gc_list = server->gc_next;
396 write_seqlock(&net->fs_lock);
398 deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
400 rb_erase(&server->uuid_rb, &net->fs_servers);
401 hlist_del_rcu(&server->proc_link);
403 write_sequnlock(&net->fs_lock);
406 write_seqlock(&net->fs_addr_lock);
407 if (!hlist_unhashed(&server->addr4_link))
408 hlist_del_rcu(&server->addr4_link);
409 if (!hlist_unhashed(&server->addr6_link))
410 hlist_del_rcu(&server->addr6_link);
411 write_sequnlock(&net->fs_addr_lock);
412 afs_destroy_server(net, server);
418 * Manage the records of servers known to be within a network namespace. This
419 * includes garbage collecting unused servers.
421 * Note also that we were given an increment on net->servers_outstanding by
422 * whoever queued us that we need to deal with before returning.
424 void afs_manage_servers(struct work_struct *work)
426 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
427 struct afs_server *gc_list = NULL;
428 struct rb_node *cursor;
429 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
430 bool purging = !net->live;
434 /* Trawl the server list looking for servers that have expired from
437 read_seqlock_excl(&net->fs_lock);
439 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
440 struct afs_server *server =
441 rb_entry(cursor, struct afs_server, uuid_rb);
442 int usage = atomic_read(&server->usage);
444 _debug("manage %pU %u", &server->uuid, usage);
446 ASSERTCMP(usage, >=, 1);
447 ASSERTIFCMP(purging, usage, ==, 1);
450 time64_t expire_at = server->put_time;
452 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
453 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
454 expire_at += afs_server_gc_delay;
455 if (purging || expire_at <= now) {
456 server->gc_next = gc_list;
458 } else if (expire_at < next_manage) {
459 next_manage = expire_at;
464 read_sequnlock_excl(&net->fs_lock);
466 /* Update the timer on the way out. We have to pass an increment on
467 * servers_outstanding in the namespace that we are in to the timer or
468 * the work scheduler.
470 if (!purging && next_manage < TIME64_MAX) {
471 now = ktime_get_real_seconds();
473 if (next_manage - now <= 0) {
474 if (queue_work(afs_wq, &net->fs_manager))
475 afs_inc_servers_outstanding(net);
477 afs_set_server_timer(net, next_manage - now);
481 afs_gc_servers(net, gc_list);
483 afs_dec_servers_outstanding(net);
484 _leave(" [%d]", atomic_read(&net->servers_outstanding));
487 static void afs_queue_server_manager(struct afs_net *net)
489 afs_inc_servers_outstanding(net);
490 if (!queue_work(afs_wq, &net->fs_manager))
491 afs_dec_servers_outstanding(net);
495 * Purge list of servers.
497 void afs_purge_servers(struct afs_net *net)
501 if (del_timer_sync(&net->fs_timer))
502 atomic_dec(&net->servers_outstanding);
504 afs_queue_server_manager(net);
507 wait_var_event(&net->servers_outstanding,
508 !atomic_read(&net->servers_outstanding));
513 * Get an update for a server's address list.
515 static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
517 struct afs_addr_list *alist, *discard;
521 alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
524 if ((PTR_ERR(alist) == -ERESTARTSYS ||
525 PTR_ERR(alist) == -EINTR) &&
526 !(fc->flags & AFS_FS_CURSOR_INTR) &&
528 _leave(" = t [intr]");
531 fc->error = PTR_ERR(alist);
532 _leave(" = f [%d]", fc->error);
537 if (server->addr_version != alist->version) {
538 write_lock(&server->fs_lock);
539 discard = rcu_dereference_protected(server->addresses,
540 lockdep_is_held(&server->fs_lock));
541 rcu_assign_pointer(server->addresses, alist);
542 server->addr_version = alist->version;
543 write_unlock(&server->fs_lock);
546 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
547 afs_put_addrlist(discard);
553 * See if a server's address list needs updating.
555 bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
557 time64_t now = ktime_get_real_seconds();
560 int ret, retries = 0;
567 diff = READ_ONCE(server->update_at) - now;
569 _leave(" = t [not now %ld]", diff);
573 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
574 success = afs_update_server_record(fc, server);
575 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
576 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
577 _leave(" = %d", success);
581 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
583 if (ret == -ERESTARTSYS) {
584 if (!(fc->flags & AFS_FS_CURSOR_INTR) && server->addresses) {
585 _leave(" = t [intr]");
589 _leave(" = f [intr]");
595 _leave(" = f [stale]");