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 unsigned afs_server_update_delay
= 30; /* Time till VLDB recheck in secs */
17 static void afs_inc_servers_outstanding(struct afs_net
*net
)
19 atomic_inc(&net
->servers_outstanding
);
22 static void afs_dec_servers_outstanding(struct afs_net
*net
)
24 if (atomic_dec_and_test(&net
->servers_outstanding
))
25 wake_up_var(&net
->servers_outstanding
);
29 * Find a server by one of its addresses.
31 struct afs_server
*afs_find_server(struct afs_net
*net
,
32 const struct sockaddr_rxrpc
*srx
)
34 const struct sockaddr_in6
*a
= &srx
->transport
.sin6
, *b
;
35 const struct afs_addr_list
*alist
;
36 struct afs_server
*server
= NULL
;
41 if (srx
->transport
.sin6
.sin6_addr
.s6_addr32
[0] == 0 ||
42 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[1] == 0 ||
43 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[2] == htonl(0xffff))
50 afs_put_server(net
, server
);
52 read_seqbegin_or_lock(&net
->fs_addr_lock
, &seq
);
55 hlist_for_each_entry_rcu(server
, &net
->fs_addresses6
, addr6_link
) {
56 alist
= rcu_dereference(server
->addresses
);
57 for (i
= alist
->nr_ipv4
; i
< alist
->nr_addrs
; i
++) {
58 b
= &alist
->addrs
[i
].transport
.sin6
;
59 diff
= ((u16 __force
)a
->sin6_port
-
60 (u16 __force
)b
->sin6_port
);
62 diff
= memcmp(&a
->sin6_addr
,
64 sizeof(struct in6_addr
));
70 hlist_for_each_entry_rcu(server
, &net
->fs_addresses4
, addr4_link
) {
71 alist
= rcu_dereference(server
->addresses
);
72 for (i
= 0; i
< alist
->nr_ipv4
; i
++) {
73 b
= &alist
->addrs
[i
].transport
.sin6
;
74 diff
= ((u16 __force
)a
->sin6_port
-
75 (u16 __force
)b
->sin6_port
);
77 diff
= ((u32 __force
)a
->sin6_addr
.s6_addr32
[3] -
78 (u32 __force
)b
->sin6_addr
.s6_addr32
[3]);
87 if (server
&& !atomic_inc_not_zero(&server
->usage
))
90 } while (need_seqretry(&net
->fs_addr_lock
, seq
));
92 done_seqretry(&net
->fs_addr_lock
, seq
);
99 * Look up a server by its UUID
101 struct afs_server
*afs_find_server_by_uuid(struct afs_net
*net
, const uuid_t
*uuid
)
103 struct afs_server
*server
= NULL
;
110 /* Unfortunately, rbtree walking doesn't give reliable results
111 * under just the RCU read lock, so we have to check for
115 afs_put_server(net
, server
);
118 read_seqbegin_or_lock(&net
->fs_lock
, &seq
);
120 p
= net
->fs_servers
.rb_node
;
122 server
= rb_entry(p
, struct afs_server
, uuid_rb
);
124 diff
= memcmp(uuid
, &server
->uuid
, sizeof(*uuid
));
127 } else if (diff
> 0) {
130 afs_get_server(server
);
136 } while (need_seqretry(&net
->fs_lock
, seq
));
138 done_seqretry(&net
->fs_lock
, seq
);
140 _leave(" = %p", server
);
145 * Install a server record in the namespace tree
147 static struct afs_server
*afs_install_server(struct afs_net
*net
,
148 struct afs_server
*candidate
)
150 const struct afs_addr_list
*alist
;
151 struct afs_server
*server
;
152 struct rb_node
**pp
, *p
;
153 int ret
= -EEXIST
, diff
;
155 _enter("%p", candidate
);
157 write_seqlock(&net
->fs_lock
);
159 /* Firstly install the server in the UUID lookup tree */
160 pp
= &net
->fs_servers
.rb_node
;
164 _debug("- consider %p", p
);
165 server
= rb_entry(p
, struct afs_server
, uuid_rb
);
166 diff
= memcmp(&candidate
->uuid
, &server
->uuid
, sizeof(uuid_t
));
168 pp
= &(*pp
)->rb_left
;
170 pp
= &(*pp
)->rb_right
;
176 rb_link_node(&server
->uuid_rb
, p
, pp
);
177 rb_insert_color(&server
->uuid_rb
, &net
->fs_servers
);
178 hlist_add_head_rcu(&server
->proc_link
, &net
->fs_proc
);
180 write_seqlock(&net
->fs_addr_lock
);
181 alist
= rcu_dereference_protected(server
->addresses
,
182 lockdep_is_held(&net
->fs_addr_lock
.lock
));
184 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
185 * it in the IPv4 and/or IPv6 reverse-map lists.
187 * TODO: For speed we want to use something other than a flat list
188 * here; even sorting the list in terms of lowest address would help a
189 * bit, but anything we might want to do gets messy and memory
192 if (alist
->nr_ipv4
> 0)
193 hlist_add_head_rcu(&server
->addr4_link
, &net
->fs_addresses4
);
194 if (alist
->nr_addrs
> alist
->nr_ipv4
)
195 hlist_add_head_rcu(&server
->addr6_link
, &net
->fs_addresses6
);
197 write_sequnlock(&net
->fs_addr_lock
);
201 afs_get_server(server
);
202 write_sequnlock(&net
->fs_lock
);
207 * allocate a new server record
209 static struct afs_server
*afs_alloc_server(struct afs_net
*net
,
211 struct afs_addr_list
*alist
)
213 struct afs_server
*server
;
217 server
= kzalloc(sizeof(struct afs_server
), GFP_KERNEL
);
221 atomic_set(&server
->usage
, 1);
222 RCU_INIT_POINTER(server
->addresses
, alist
);
223 server
->addr_version
= alist
->version
;
224 server
->uuid
= *uuid
;
225 server
->update_at
= ktime_get_real_seconds() + afs_server_update_delay
;
226 rwlock_init(&server
->fs_lock
);
227 INIT_HLIST_HEAD(&server
->cb_volumes
);
228 rwlock_init(&server
->cb_break_lock
);
229 init_waitqueue_head(&server
->probe_wq
);
230 spin_lock_init(&server
->probe_lock
);
232 afs_inc_servers_outstanding(net
);
233 _leave(" = %p", server
);
237 _leave(" = NULL [nomem]");
242 * Look up an address record for a server
244 static struct afs_addr_list
*afs_vl_lookup_addrs(struct afs_cell
*cell
,
245 struct key
*key
, const uuid_t
*uuid
)
247 struct afs_vl_cursor vc
;
248 struct afs_addr_list
*alist
= NULL
;
252 if (afs_begin_vlserver_operation(&vc
, cell
, key
)) {
253 while (afs_select_vlserver(&vc
)) {
254 if (test_bit(AFS_VLSERVER_FL_IS_YFS
, &vc
.server
->flags
))
255 alist
= afs_yfsvl_get_endpoints(&vc
, uuid
);
257 alist
= afs_vl_get_addrs_u(&vc
, uuid
);
260 ret
= afs_end_vlserver_operation(&vc
);
263 return ret
< 0 ? ERR_PTR(ret
) : alist
;
267 * Get or create a fileserver record.
269 struct afs_server
*afs_lookup_server(struct afs_cell
*cell
, struct key
*key
,
272 struct afs_addr_list
*alist
;
273 struct afs_server
*server
, *candidate
;
275 _enter("%p,%pU", cell
->net
, uuid
);
277 server
= afs_find_server_by_uuid(cell
->net
, uuid
);
281 alist
= afs_vl_lookup_addrs(cell
, key
, uuid
);
283 return ERR_CAST(alist
);
285 candidate
= afs_alloc_server(cell
->net
, uuid
, alist
);
287 afs_put_addrlist(alist
);
288 return ERR_PTR(-ENOMEM
);
291 server
= afs_install_server(cell
->net
, candidate
);
292 if (server
!= candidate
) {
293 afs_put_addrlist(alist
);
297 _leave(" = %p{%d}", server
, atomic_read(&server
->usage
));
302 * Set the server timer to fire after a given delay, assuming it's not already
303 * set for an earlier time.
305 static void afs_set_server_timer(struct afs_net
*net
, time64_t delay
)
308 afs_inc_servers_outstanding(net
);
309 if (timer_reduce(&net
->fs_timer
, jiffies
+ delay
* HZ
))
310 afs_dec_servers_outstanding(net
);
315 * Server management timer. We have an increment on fs_outstanding that we
316 * need to pass along to the work item.
318 void afs_servers_timer(struct timer_list
*timer
)
320 struct afs_net
*net
= container_of(timer
, struct afs_net
, fs_timer
);
323 if (!queue_work(afs_wq
, &net
->fs_manager
))
324 afs_dec_servers_outstanding(net
);
328 * Release a reference on a server record.
330 void afs_put_server(struct afs_net
*net
, struct afs_server
*server
)
337 server
->put_time
= ktime_get_real_seconds();
339 usage
= atomic_dec_return(&server
->usage
);
341 _enter("{%u}", usage
);
343 if (likely(usage
> 0))
346 afs_set_server_timer(net
, afs_server_gc_delay
);
349 static void afs_server_rcu(struct rcu_head
*rcu
)
351 struct afs_server
*server
= container_of(rcu
, struct afs_server
, rcu
);
353 afs_put_addrlist(rcu_access_pointer(server
->addresses
));
358 * destroy a dead server
360 static void afs_destroy_server(struct afs_net
*net
, struct afs_server
*server
)
362 struct afs_addr_list
*alist
= rcu_access_pointer(server
->addresses
);
363 struct afs_addr_cursor ac
= {
365 .index
= alist
->preferred
,
368 _enter("%p", server
);
370 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB
, &server
->flags
))
371 afs_fs_give_up_all_callbacks(net
, server
, &ac
, NULL
);
373 wait_var_event(&server
->probe_outstanding
,
374 atomic_read(&server
->probe_outstanding
) == 0);
376 call_rcu(&server
->rcu
, afs_server_rcu
);
377 afs_dec_servers_outstanding(net
);
381 * Garbage collect any expired servers.
383 static void afs_gc_servers(struct afs_net
*net
, struct afs_server
*gc_list
)
385 struct afs_server
*server
;
389 while ((server
= gc_list
)) {
390 gc_list
= server
->gc_next
;
392 write_seqlock(&net
->fs_lock
);
394 deleted
= atomic_try_cmpxchg(&server
->usage
, &usage
, 0);
396 rb_erase(&server
->uuid_rb
, &net
->fs_servers
);
397 hlist_del_rcu(&server
->proc_link
);
399 write_sequnlock(&net
->fs_lock
);
402 write_seqlock(&net
->fs_addr_lock
);
403 if (!hlist_unhashed(&server
->addr4_link
))
404 hlist_del_rcu(&server
->addr4_link
);
405 if (!hlist_unhashed(&server
->addr6_link
))
406 hlist_del_rcu(&server
->addr6_link
);
407 write_sequnlock(&net
->fs_addr_lock
);
408 afs_destroy_server(net
, server
);
414 * Manage the records of servers known to be within a network namespace. This
415 * includes garbage collecting unused servers.
417 * Note also that we were given an increment on net->servers_outstanding by
418 * whoever queued us that we need to deal with before returning.
420 void afs_manage_servers(struct work_struct
*work
)
422 struct afs_net
*net
= container_of(work
, struct afs_net
, fs_manager
);
423 struct afs_server
*gc_list
= NULL
;
424 struct rb_node
*cursor
;
425 time64_t now
= ktime_get_real_seconds(), next_manage
= TIME64_MAX
;
426 bool purging
= !net
->live
;
430 /* Trawl the server list looking for servers that have expired from
433 read_seqlock_excl(&net
->fs_lock
);
435 for (cursor
= rb_first(&net
->fs_servers
); cursor
; cursor
= rb_next(cursor
)) {
436 struct afs_server
*server
=
437 rb_entry(cursor
, struct afs_server
, uuid_rb
);
438 int usage
= atomic_read(&server
->usage
);
440 _debug("manage %pU %u", &server
->uuid
, usage
);
442 ASSERTCMP(usage
, >=, 1);
443 ASSERTIFCMP(purging
, usage
, ==, 1);
446 time64_t expire_at
= server
->put_time
;
448 if (!test_bit(AFS_SERVER_FL_VL_FAIL
, &server
->flags
) &&
449 !test_bit(AFS_SERVER_FL_NOT_FOUND
, &server
->flags
))
450 expire_at
+= afs_server_gc_delay
;
451 if (purging
|| expire_at
<= now
) {
452 server
->gc_next
= gc_list
;
454 } else if (expire_at
< next_manage
) {
455 next_manage
= expire_at
;
460 read_sequnlock_excl(&net
->fs_lock
);
462 /* Update the timer on the way out. We have to pass an increment on
463 * servers_outstanding in the namespace that we are in to the timer or
464 * the work scheduler.
466 if (!purging
&& next_manage
< TIME64_MAX
) {
467 now
= ktime_get_real_seconds();
469 if (next_manage
- now
<= 0) {
470 if (queue_work(afs_wq
, &net
->fs_manager
))
471 afs_inc_servers_outstanding(net
);
473 afs_set_server_timer(net
, next_manage
- now
);
477 afs_gc_servers(net
, gc_list
);
479 afs_dec_servers_outstanding(net
);
480 _leave(" [%d]", atomic_read(&net
->servers_outstanding
));
483 static void afs_queue_server_manager(struct afs_net
*net
)
485 afs_inc_servers_outstanding(net
);
486 if (!queue_work(afs_wq
, &net
->fs_manager
))
487 afs_dec_servers_outstanding(net
);
491 * Purge list of servers.
493 void afs_purge_servers(struct afs_net
*net
)
497 if (del_timer_sync(&net
->fs_timer
))
498 atomic_dec(&net
->servers_outstanding
);
500 afs_queue_server_manager(net
);
503 wait_var_event(&net
->servers_outstanding
,
504 !atomic_read(&net
->servers_outstanding
));
509 * Get an update for a server's address list.
511 static noinline
bool afs_update_server_record(struct afs_fs_cursor
*fc
, struct afs_server
*server
)
513 struct afs_addr_list
*alist
, *discard
;
517 alist
= afs_vl_lookup_addrs(fc
->vnode
->volume
->cell
, fc
->key
,
520 if ((PTR_ERR(alist
) == -ERESTARTSYS
||
521 PTR_ERR(alist
) == -EINTR
) &&
522 !(fc
->flags
& AFS_FS_CURSOR_INTR
) &&
524 _leave(" = t [intr]");
527 fc
->error
= PTR_ERR(alist
);
528 _leave(" = f [%d]", fc
->error
);
533 if (server
->addr_version
!= alist
->version
) {
534 write_lock(&server
->fs_lock
);
535 discard
= rcu_dereference_protected(server
->addresses
,
536 lockdep_is_held(&server
->fs_lock
));
537 rcu_assign_pointer(server
->addresses
, alist
);
538 server
->addr_version
= alist
->version
;
539 write_unlock(&server
->fs_lock
);
542 server
->update_at
= ktime_get_real_seconds() + afs_server_update_delay
;
543 afs_put_addrlist(discard
);
549 * See if a server's address list needs updating.
551 bool afs_check_server_record(struct afs_fs_cursor
*fc
, struct afs_server
*server
)
553 time64_t now
= ktime_get_real_seconds();
556 int ret
, retries
= 0;
563 diff
= READ_ONCE(server
->update_at
) - now
;
565 _leave(" = t [not now %ld]", diff
);
569 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING
, &server
->flags
)) {
570 success
= afs_update_server_record(fc
, server
);
571 clear_bit_unlock(AFS_SERVER_FL_UPDATING
, &server
->flags
);
572 wake_up_bit(&server
->flags
, AFS_SERVER_FL_UPDATING
);
573 _leave(" = %d", success
);
577 ret
= wait_on_bit(&server
->flags
, AFS_SERVER_FL_UPDATING
,
579 if (ret
== -ERESTARTSYS
) {
580 if (!(fc
->flags
& AFS_FS_CURSOR_INTR
) && server
->addresses
) {
581 _leave(" = t [intr]");
585 _leave(" = f [intr]");
591 _leave(" = f [stale]");