1 /* AFS server record management
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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>
17 static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */
18 static unsigned afs_server_update_delay = 30; /* Time till VLDB recheck in secs */
20 static void afs_inc_servers_outstanding(struct afs_net *net)
22 atomic_inc(&net->servers_outstanding);
25 static void afs_dec_servers_outstanding(struct afs_net *net)
27 if (atomic_dec_and_test(&net->servers_outstanding))
28 wake_up_var(&net->servers_outstanding);
32 * Find a server by one of its addresses.
34 struct afs_server *afs_find_server(struct afs_net *net,
35 const struct sockaddr_rxrpc *srx)
37 const struct sockaddr_in6 *a = &srx->transport.sin6, *b;
38 const struct afs_addr_list *alist;
39 struct afs_server *server = NULL;
44 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))
53 afs_put_server(net, server);
55 read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
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 __force)a->sin6_port -
63 (u16 __force)b->sin6_port);
65 diff = memcmp(&a->sin6_addr,
67 sizeof(struct in6_addr));
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;
77 diff = ((u16 __force)a->sin6_port -
78 (u16 __force)b->sin6_port);
80 diff = ((u32 __force)a->sin6_addr.s6_addr32[3] -
81 (u32 __force)b->sin6_addr.s6_addr32[3]);
90 if (server && !atomic_inc_not_zero(&server->usage))
93 } while (need_seqretry(&net->fs_addr_lock, seq));
95 done_seqretry(&net->fs_addr_lock, seq);
102 * Look up a server by its UUID
104 struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
106 struct afs_server *server = NULL;
113 /* Unfortunately, rbtree walking doesn't give reliable results
114 * under just the RCU read lock, so we have to check for
118 afs_put_server(net, server);
121 read_seqbegin_or_lock(&net->fs_lock, &seq);
123 p = net->fs_servers.rb_node;
125 server = rb_entry(p, struct afs_server, uuid_rb);
127 diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
130 } else if (diff > 0) {
133 afs_get_server(server);
139 } while (need_seqretry(&net->fs_lock, seq));
141 done_seqretry(&net->fs_lock, seq);
143 _leave(" = %p", server);
148 * Install a server record in the namespace tree
150 static struct afs_server *afs_install_server(struct afs_net *net,
151 struct afs_server *candidate)
153 const struct afs_addr_list *alist;
154 struct afs_server *server;
155 struct rb_node **pp, *p;
156 int ret = -EEXIST, diff;
158 _enter("%p", candidate);
160 write_seqlock(&net->fs_lock);
162 /* Firstly install the server in the UUID lookup tree */
163 pp = &net->fs_servers.rb_node;
167 _debug("- consider %p", p);
168 server = rb_entry(p, struct afs_server, uuid_rb);
169 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
171 pp = &(*pp)->rb_left;
173 pp = &(*pp)->rb_right;
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);
183 write_seqlock(&net->fs_addr_lock);
184 alist = rcu_dereference_protected(server->addresses,
185 lockdep_is_held(&net->fs_addr_lock.lock));
187 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
188 * it in the IPv4 and/or IPv6 reverse-map lists.
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
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);
200 write_sequnlock(&net->fs_addr_lock);
204 afs_get_server(server);
205 write_sequnlock(&net->fs_lock);
210 * allocate a new server record
212 static struct afs_server *afs_alloc_server(struct afs_net *net,
214 struct afs_addr_list *alist)
216 struct afs_server *server;
220 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
224 atomic_set(&server->usage, 1);
225 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);
231 INIT_LIST_HEAD(&server->cb_interests);
232 rwlock_init(&server->cb_break_lock);
234 afs_inc_servers_outstanding(net);
235 _leave(" = %p", server);
239 _leave(" = NULL [nomem]");
244 * Look up an address record for a server
246 static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
247 struct key *key, const uuid_t *uuid)
249 struct afs_addr_cursor ac;
250 struct afs_addr_list *alist;
253 ret = afs_set_vl_cursor(&ac, cell);
257 while (afs_iterate_addresses(&ac)) {
258 if (test_bit(ac.index, &ac.alist->yfs))
259 alist = afs_yfsvl_get_endpoints(cell->net, &ac, key, uuid);
261 alist = afs_vl_get_addrs_u(cell->net, &ac, key, uuid);
267 ac.error = afs_abort_to_error(ac.abort_code);
283 return ERR_PTR(afs_end_cursor(&ac));
287 * Get or create a fileserver record.
289 struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
292 struct afs_addr_list *alist;
293 struct afs_server *server, *candidate;
295 _enter("%p,%pU", cell->net, uuid);
297 server = afs_find_server_by_uuid(cell->net, uuid);
301 alist = afs_vl_lookup_addrs(cell, key, uuid);
303 return ERR_CAST(alist);
305 candidate = afs_alloc_server(cell->net, uuid, alist);
307 afs_put_addrlist(alist);
308 return ERR_PTR(-ENOMEM);
311 server = afs_install_server(cell->net, candidate);
312 if (server != candidate) {
313 afs_put_addrlist(alist);
317 _leave(" = %p{%d}", server, atomic_read(&server->usage));
322 * Set the server timer to fire after a given delay, assuming it's not already
323 * set for an earlier time.
325 static void afs_set_server_timer(struct afs_net *net, time64_t delay)
328 afs_inc_servers_outstanding(net);
329 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
330 afs_dec_servers_outstanding(net);
335 * Server management timer. We have an increment on fs_outstanding that we
336 * need to pass along to the work item.
338 void afs_servers_timer(struct timer_list *timer)
340 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
343 if (!queue_work(afs_wq, &net->fs_manager))
344 afs_dec_servers_outstanding(net);
348 * Release a reference on a server record.
350 void afs_put_server(struct afs_net *net, struct afs_server *server)
357 server->put_time = ktime_get_real_seconds();
359 usage = atomic_dec_return(&server->usage);
361 _enter("{%u}", usage);
363 if (likely(usage > 0))
366 afs_set_server_timer(net, afs_server_gc_delay);
369 static void afs_server_rcu(struct rcu_head *rcu)
371 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
373 afs_put_addrlist(rcu_access_pointer(server->addresses));
378 * destroy a dead server
380 static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
382 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
383 struct afs_addr_cursor ac = {
385 .start = alist->index,
387 .addr = &alist->addrs[alist->index],
390 _enter("%p", server);
392 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
393 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
395 call_rcu(&server->rcu, afs_server_rcu);
396 afs_dec_servers_outstanding(net);
400 * Garbage collect any expired servers.
402 static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
404 struct afs_server *server;
408 while ((server = gc_list)) {
409 gc_list = server->gc_next;
411 write_seqlock(&net->fs_lock);
413 deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
415 rb_erase(&server->uuid_rb, &net->fs_servers);
416 hlist_del_rcu(&server->proc_link);
418 write_sequnlock(&net->fs_lock);
421 write_seqlock(&net->fs_addr_lock);
422 if (!hlist_unhashed(&server->addr4_link))
423 hlist_del_rcu(&server->addr4_link);
424 if (!hlist_unhashed(&server->addr6_link))
425 hlist_del_rcu(&server->addr6_link);
426 write_sequnlock(&net->fs_addr_lock);
427 afs_destroy_server(net, server);
433 * Manage the records of servers known to be within a network namespace. This
434 * includes garbage collecting unused servers.
436 * Note also that we were given an increment on net->servers_outstanding by
437 * whoever queued us that we need to deal with before returning.
439 void afs_manage_servers(struct work_struct *work)
441 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
442 struct afs_server *gc_list = NULL;
443 struct rb_node *cursor;
444 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
445 bool purging = !net->live;
449 /* Trawl the server list looking for servers that have expired from
452 read_seqlock_excl(&net->fs_lock);
454 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
455 struct afs_server *server =
456 rb_entry(cursor, struct afs_server, uuid_rb);
457 int usage = atomic_read(&server->usage);
459 _debug("manage %pU %u", &server->uuid, usage);
461 ASSERTCMP(usage, >=, 1);
462 ASSERTIFCMP(purging, usage, ==, 1);
465 time64_t expire_at = server->put_time;
467 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
468 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
469 expire_at += afs_server_gc_delay;
470 if (purging || expire_at <= now) {
471 server->gc_next = gc_list;
473 } else if (expire_at < next_manage) {
474 next_manage = expire_at;
479 read_sequnlock_excl(&net->fs_lock);
481 /* Update the timer on the way out. We have to pass an increment on
482 * servers_outstanding in the namespace that we are in to the timer or
483 * the work scheduler.
485 if (!purging && next_manage < TIME64_MAX) {
486 now = ktime_get_real_seconds();
488 if (next_manage - now <= 0) {
489 if (queue_work(afs_wq, &net->fs_manager))
490 afs_inc_servers_outstanding(net);
492 afs_set_server_timer(net, next_manage - now);
496 afs_gc_servers(net, gc_list);
498 afs_dec_servers_outstanding(net);
499 _leave(" [%d]", atomic_read(&net->servers_outstanding));
502 static void afs_queue_server_manager(struct afs_net *net)
504 afs_inc_servers_outstanding(net);
505 if (!queue_work(afs_wq, &net->fs_manager))
506 afs_dec_servers_outstanding(net);
510 * Purge list of servers.
512 void afs_purge_servers(struct afs_net *net)
516 if (del_timer_sync(&net->fs_timer))
517 atomic_dec(&net->servers_outstanding);
519 afs_queue_server_manager(net);
522 wait_var_event(&net->servers_outstanding,
523 !atomic_read(&net->servers_outstanding));
528 * Probe a fileserver to find its capabilities.
530 * TODO: Try service upgrade.
532 static bool afs_do_probe_fileserver(struct afs_fs_cursor *fc)
537 fc->ac.start = READ_ONCE(fc->ac.alist->index);
538 fc->ac.index = fc->ac.start;
540 fc->ac.begun = false;
542 while (afs_iterate_addresses(&fc->ac)) {
543 afs_fs_get_capabilities(afs_v2net(fc->vnode), fc->cbi->server,
545 switch (fc->ac.error) {
547 afs_end_cursor(&fc->ac);
548 set_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags);
551 fc->ac.error = afs_abort_to_error(fc->ac.abort_code);
569 afs_end_cursor(&fc->ac);
574 * If we haven't already, try probing the fileserver to get its capabilities.
575 * We try not to instigate parallel probes, but it's possible that the parallel
576 * probes will fail due to authentication failure when ours would succeed.
578 * TODO: Try sending an anonymous probe if an authenticated probe fails.
580 bool afs_probe_fileserver(struct afs_fs_cursor *fc)
583 int ret, retries = 0;
588 if (test_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags)) {
593 if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags)) {
594 success = afs_do_probe_fileserver(fc);
595 clear_bit_unlock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags);
596 wake_up_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING);
602 ret = wait_on_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING,
604 if (ret == -ERESTARTSYS) {
606 _leave(" = f [%d]", ret);
612 fc->ac.error = -ESTALE;
613 _leave(" = f [stale]");
621 * Get an update for a server's address list.
623 static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
625 struct afs_addr_list *alist, *discard;
629 alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
632 fc->ac.error = PTR_ERR(alist);
633 _leave(" = f [%d]", fc->ac.error);
638 if (server->addr_version != alist->version) {
639 write_lock(&server->fs_lock);
640 discard = rcu_dereference_protected(server->addresses,
641 lockdep_is_held(&server->fs_lock));
642 rcu_assign_pointer(server->addresses, alist);
643 server->addr_version = alist->version;
644 write_unlock(&server->fs_lock);
647 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
648 afs_put_addrlist(discard);
654 * See if a server's address list needs updating.
656 bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
658 time64_t now = ktime_get_real_seconds();
661 int ret, retries = 0;
668 diff = READ_ONCE(server->update_at) - now;
670 _leave(" = t [not now %ld]", diff);
674 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
675 success = afs_update_server_record(fc, server);
676 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
677 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
678 _leave(" = %d", success);
682 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
684 if (ret == -ERESTARTSYS) {
686 _leave(" = f [intr]");
692 _leave(" = f [stale]");