1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS server record management
4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/sched.h>
9 #include <linux/slab.h>
12 #include "protocol_yfs.h"
14 static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */
15 static atomic_t afs_server_debug_id;
17 static struct afs_server *afs_maybe_use_server(struct afs_server *,
18 enum afs_server_trace);
19 static void __afs_put_server(struct afs_net *, struct afs_server *);
22 * Find a server by one of its addresses.
24 struct afs_server *afs_find_server(struct afs_net *net,
25 const struct sockaddr_rxrpc *srx)
27 const struct afs_addr_list *alist;
28 struct afs_server *server = NULL;
36 afs_unuse_server_notime(net, server, afs_server_trace_put_find_rsq);
38 read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
40 if (srx->transport.family == AF_INET6) {
41 const struct sockaddr_in6 *a = &srx->transport.sin6, *b;
42 hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) {
43 alist = rcu_dereference(server->addresses);
44 for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) {
45 b = &alist->addrs[i].transport.sin6;
46 diff = ((u16 __force)a->sin6_port -
47 (u16 __force)b->sin6_port);
49 diff = memcmp(&a->sin6_addr,
51 sizeof(struct in6_addr));
57 const struct sockaddr_in *a = &srx->transport.sin, *b;
58 hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) {
59 alist = rcu_dereference(server->addresses);
60 for (i = 0; i < alist->nr_ipv4; i++) {
61 b = &alist->addrs[i].transport.sin;
62 diff = ((u16 __force)a->sin_port -
63 (u16 __force)b->sin_port);
65 diff = ((u32 __force)a->sin_addr.s_addr -
66 (u32 __force)b->sin_addr.s_addr);
76 server = afs_maybe_use_server(server, afs_server_trace_get_by_addr);
78 } while (need_seqretry(&net->fs_addr_lock, seq));
80 done_seqretry(&net->fs_addr_lock, seq);
87 * Look up a server by its UUID and mark it active.
89 struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
91 struct afs_server *server = NULL;
98 /* Unfortunately, rbtree walking doesn't give reliable results
99 * under just the RCU read lock, so we have to check for
103 afs_unuse_server(net, server, afs_server_trace_put_uuid_rsq);
106 read_seqbegin_or_lock(&net->fs_lock, &seq);
108 p = net->fs_servers.rb_node;
110 server = rb_entry(p, struct afs_server, uuid_rb);
112 diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
115 } else if (diff > 0) {
118 afs_use_server(server, afs_server_trace_get_by_uuid);
124 } while (need_seqretry(&net->fs_lock, seq));
126 done_seqretry(&net->fs_lock, seq);
128 _leave(" = %p", server);
133 * Install a server record in the namespace tree. If there's a clash, we stick
134 * it into a list anchored on whichever afs_server struct is actually in the
137 static struct afs_server *afs_install_server(struct afs_cell *cell,
138 struct afs_server *candidate)
140 const struct afs_addr_list *alist;
141 struct afs_server *server, *next;
142 struct afs_net *net = cell->net;
143 struct rb_node **pp, *p;
146 _enter("%p", candidate);
148 write_seqlock(&net->fs_lock);
150 /* Firstly install the server in the UUID lookup tree */
151 pp = &net->fs_servers.rb_node;
155 _debug("- consider %p", p);
156 server = rb_entry(p, struct afs_server, uuid_rb);
157 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
159 pp = &(*pp)->rb_left;
160 } else if (diff > 0) {
161 pp = &(*pp)->rb_right;
163 if (server->cell == cell)
166 /* We have the same UUID representing servers in
167 * different cells. Append the new server to the list.
170 next = rcu_dereference_protected(
172 lockdep_is_held(&net->fs_lock.lock));
177 rcu_assign_pointer(server->uuid_next, candidate);
178 candidate->uuid_prev = server;
185 rb_link_node(&server->uuid_rb, p, pp);
186 rb_insert_color(&server->uuid_rb, &net->fs_servers);
187 hlist_add_head_rcu(&server->proc_link, &net->fs_proc);
190 write_seqlock(&net->fs_addr_lock);
191 alist = rcu_dereference_protected(server->addresses,
192 lockdep_is_held(&net->fs_addr_lock.lock));
194 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
195 * it in the IPv4 and/or IPv6 reverse-map lists.
197 * TODO: For speed we want to use something other than a flat list
198 * here; even sorting the list in terms of lowest address would help a
199 * bit, but anything we might want to do gets messy and memory
202 if (alist->nr_ipv4 > 0)
203 hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4);
204 if (alist->nr_addrs > alist->nr_ipv4)
205 hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6);
207 write_sequnlock(&net->fs_addr_lock);
210 afs_get_server(server, afs_server_trace_get_install);
211 write_sequnlock(&net->fs_lock);
216 * Allocate a new server record and mark it active.
218 static struct afs_server *afs_alloc_server(struct afs_cell *cell,
220 struct afs_addr_list *alist)
222 struct afs_server *server;
223 struct afs_net *net = cell->net;
227 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
231 refcount_set(&server->ref, 1);
232 atomic_set(&server->active, 1);
233 server->debug_id = atomic_inc_return(&afs_server_debug_id);
234 RCU_INIT_POINTER(server->addresses, alist);
235 server->addr_version = alist->version;
236 server->uuid = *uuid;
237 rwlock_init(&server->fs_lock);
238 INIT_WORK(&server->initcb_work, afs_server_init_callback_work);
239 init_waitqueue_head(&server->probe_wq);
240 INIT_LIST_HEAD(&server->probe_link);
241 spin_lock_init(&server->probe_lock);
243 server->rtt = UINT_MAX;
245 afs_inc_servers_outstanding(net);
246 trace_afs_server(server->debug_id, 1, 1, afs_server_trace_alloc);
247 _leave(" = %p", server);
251 _leave(" = NULL [nomem]");
256 * Look up an address record for a server
258 static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
259 struct key *key, const uuid_t *uuid)
261 struct afs_vl_cursor vc;
262 struct afs_addr_list *alist = NULL;
266 if (afs_begin_vlserver_operation(&vc, cell, key)) {
267 while (afs_select_vlserver(&vc)) {
268 if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags))
269 alist = afs_yfsvl_get_endpoints(&vc, uuid);
271 alist = afs_vl_get_addrs_u(&vc, uuid);
274 ret = afs_end_vlserver_operation(&vc);
277 return ret < 0 ? ERR_PTR(ret) : alist;
281 * Get or create a fileserver record.
283 struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
284 const uuid_t *uuid, u32 addr_version)
286 struct afs_addr_list *alist;
287 struct afs_server *server, *candidate;
289 _enter("%p,%pU", cell->net, uuid);
291 server = afs_find_server_by_uuid(cell->net, uuid);
293 if (server->addr_version != addr_version)
294 set_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags);
298 alist = afs_vl_lookup_addrs(cell, key, uuid);
300 return ERR_CAST(alist);
302 candidate = afs_alloc_server(cell, uuid, alist);
304 afs_put_addrlist(alist);
305 return ERR_PTR(-ENOMEM);
308 server = afs_install_server(cell, candidate);
309 if (server != candidate) {
310 afs_put_addrlist(alist);
313 /* Immediately dispatch an asynchronous probe to each interface
314 * on the fileserver. This will make sure the repeat-probing
315 * service is started.
317 afs_fs_probe_fileserver(cell->net, server, key, true);
324 * Set the server timer to fire after a given delay, assuming it's not already
325 * set for an earlier time.
327 static void afs_set_server_timer(struct afs_net *net, time64_t delay)
330 afs_inc_servers_outstanding(net);
331 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
332 afs_dec_servers_outstanding(net);
337 * Server management timer. We have an increment on fs_outstanding that we
338 * need to pass along to the work item.
340 void afs_servers_timer(struct timer_list *timer)
342 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
345 if (!queue_work(afs_wq, &net->fs_manager))
346 afs_dec_servers_outstanding(net);
350 * Get a reference on a server object.
352 struct afs_server *afs_get_server(struct afs_server *server,
353 enum afs_server_trace reason)
358 __refcount_inc(&server->ref, &r);
359 a = atomic_read(&server->active);
360 trace_afs_server(server->debug_id, r + 1, a, reason);
365 * Try to get a reference on a server object.
367 static struct afs_server *afs_maybe_use_server(struct afs_server *server,
368 enum afs_server_trace reason)
373 if (!__refcount_inc_not_zero(&server->ref, &r))
376 a = atomic_inc_return(&server->active);
377 trace_afs_server(server->debug_id, r + 1, a, reason);
382 * Get an active count on a server object.
384 struct afs_server *afs_use_server(struct afs_server *server, enum afs_server_trace reason)
389 __refcount_inc(&server->ref, &r);
390 a = atomic_inc_return(&server->active);
392 trace_afs_server(server->debug_id, r + 1, a, reason);
397 * Release a reference on a server record.
399 void afs_put_server(struct afs_net *net, struct afs_server *server,
400 enum afs_server_trace reason)
402 unsigned int a, debug_id = server->debug_id;
409 a = atomic_read(&server->active);
410 zero = __refcount_dec_and_test(&server->ref, &r);
411 trace_afs_server(debug_id, r - 1, a, reason);
413 __afs_put_server(net, server);
417 * Drop an active count on a server object without updating the last-unused
420 void afs_unuse_server_notime(struct afs_net *net, struct afs_server *server,
421 enum afs_server_trace reason)
424 unsigned int active = atomic_dec_return(&server->active);
427 afs_set_server_timer(net, afs_server_gc_delay);
428 afs_put_server(net, server, reason);
433 * Drop an active count on a server object.
435 void afs_unuse_server(struct afs_net *net, struct afs_server *server,
436 enum afs_server_trace reason)
439 server->unuse_time = ktime_get_real_seconds();
440 afs_unuse_server_notime(net, server, reason);
444 static void afs_server_rcu(struct rcu_head *rcu)
446 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
448 trace_afs_server(server->debug_id, refcount_read(&server->ref),
449 atomic_read(&server->active), afs_server_trace_free);
450 afs_put_addrlist(rcu_access_pointer(server->addresses));
454 static void __afs_put_server(struct afs_net *net, struct afs_server *server)
456 call_rcu(&server->rcu, afs_server_rcu);
457 afs_dec_servers_outstanding(net);
460 static void afs_give_up_callbacks(struct afs_net *net, struct afs_server *server)
462 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
463 struct afs_addr_cursor ac = {
465 .index = alist->preferred,
469 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
473 * destroy a dead server
475 static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
477 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
478 afs_give_up_callbacks(net, server);
480 flush_work(&server->initcb_work);
481 afs_put_server(net, server, afs_server_trace_destroy);
485 * Garbage collect any expired servers.
487 static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
489 struct afs_server *server, *next, *prev;
492 while ((server = gc_list)) {
493 gc_list = server->gc_next;
495 write_seqlock(&net->fs_lock);
497 active = atomic_read(&server->active);
499 trace_afs_server(server->debug_id, refcount_read(&server->ref),
500 active, afs_server_trace_gc);
501 next = rcu_dereference_protected(
502 server->uuid_next, lockdep_is_held(&net->fs_lock.lock));
503 prev = server->uuid_prev;
505 /* The one at the front is in the tree */
507 rb_erase(&server->uuid_rb, &net->fs_servers);
509 rb_replace_node_rcu(&server->uuid_rb,
512 next->uuid_prev = NULL;
515 /* This server is not at the front */
516 rcu_assign_pointer(prev->uuid_next, next);
518 next->uuid_prev = prev;
521 list_del(&server->probe_link);
522 hlist_del_rcu(&server->proc_link);
523 if (!hlist_unhashed(&server->addr4_link))
524 hlist_del_rcu(&server->addr4_link);
525 if (!hlist_unhashed(&server->addr6_link))
526 hlist_del_rcu(&server->addr6_link);
528 write_sequnlock(&net->fs_lock);
531 afs_destroy_server(net, server);
536 * Manage the records of servers known to be within a network namespace. This
537 * includes garbage collecting unused servers.
539 * Note also that we were given an increment on net->servers_outstanding by
540 * whoever queued us that we need to deal with before returning.
542 void afs_manage_servers(struct work_struct *work)
544 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
545 struct afs_server *gc_list = NULL;
546 struct rb_node *cursor;
547 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
548 bool purging = !net->live;
552 /* Trawl the server list looking for servers that have expired from
555 read_seqlock_excl(&net->fs_lock);
557 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
558 struct afs_server *server =
559 rb_entry(cursor, struct afs_server, uuid_rb);
560 int active = atomic_read(&server->active);
562 _debug("manage %pU %u", &server->uuid, active);
565 trace_afs_server(server->debug_id, refcount_read(&server->ref),
566 active, afs_server_trace_purging);
568 pr_notice("Can't purge s=%08x\n", server->debug_id);
572 time64_t expire_at = server->unuse_time;
574 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
575 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
576 expire_at += afs_server_gc_delay;
577 if (purging || expire_at <= now) {
578 server->gc_next = gc_list;
580 } else if (expire_at < next_manage) {
581 next_manage = expire_at;
586 read_sequnlock_excl(&net->fs_lock);
588 /* Update the timer on the way out. We have to pass an increment on
589 * servers_outstanding in the namespace that we are in to the timer or
590 * the work scheduler.
592 if (!purging && next_manage < TIME64_MAX) {
593 now = ktime_get_real_seconds();
595 if (next_manage - now <= 0) {
596 if (queue_work(afs_wq, &net->fs_manager))
597 afs_inc_servers_outstanding(net);
599 afs_set_server_timer(net, next_manage - now);
603 afs_gc_servers(net, gc_list);
605 afs_dec_servers_outstanding(net);
606 _leave(" [%d]", atomic_read(&net->servers_outstanding));
609 static void afs_queue_server_manager(struct afs_net *net)
611 afs_inc_servers_outstanding(net);
612 if (!queue_work(afs_wq, &net->fs_manager))
613 afs_dec_servers_outstanding(net);
617 * Purge list of servers.
619 void afs_purge_servers(struct afs_net *net)
623 if (del_timer_sync(&net->fs_timer))
624 afs_dec_servers_outstanding(net);
626 afs_queue_server_manager(net);
629 atomic_dec(&net->servers_outstanding);
630 wait_var_event(&net->servers_outstanding,
631 !atomic_read(&net->servers_outstanding));
636 * Get an update for a server's address list.
638 static noinline bool afs_update_server_record(struct afs_operation *op,
639 struct afs_server *server)
641 struct afs_addr_list *alist, *discard;
645 trace_afs_server(server->debug_id, refcount_read(&server->ref),
646 atomic_read(&server->active),
647 afs_server_trace_update);
649 alist = afs_vl_lookup_addrs(op->volume->cell, op->key, &server->uuid);
651 if ((PTR_ERR(alist) == -ERESTARTSYS ||
652 PTR_ERR(alist) == -EINTR) &&
653 (op->flags & AFS_OPERATION_UNINTR) &&
655 _leave(" = t [intr]");
658 op->error = PTR_ERR(alist);
659 _leave(" = f [%d]", op->error);
664 if (server->addr_version != alist->version) {
665 write_lock(&server->fs_lock);
666 discard = rcu_dereference_protected(server->addresses,
667 lockdep_is_held(&server->fs_lock));
668 rcu_assign_pointer(server->addresses, alist);
669 server->addr_version = alist->version;
670 write_unlock(&server->fs_lock);
673 afs_put_addrlist(discard);
679 * See if a server's address list needs updating.
681 bool afs_check_server_record(struct afs_operation *op, struct afs_server *server)
684 int ret, retries = 0;
691 if (test_bit(AFS_SERVER_FL_UPDATING, &server->flags))
693 if (test_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags))
695 _leave(" = t [good]");
699 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
700 clear_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags);
701 success = afs_update_server_record(op, server);
702 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
703 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
704 _leave(" = %d", success);
709 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
710 (op->flags & AFS_OPERATION_UNINTR) ?
711 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
712 if (ret == -ERESTARTSYS) {
714 _leave(" = f [intr]");
720 _leave(" = f [stale]");