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 */
16 static atomic_t afs_server_debug_id
;
18 static void afs_inc_servers_outstanding(struct afs_net
*net
)
20 atomic_inc(&net
->servers_outstanding
);
23 static void afs_dec_servers_outstanding(struct afs_net
*net
)
25 if (atomic_dec_and_test(&net
->servers_outstanding
))
26 wake_up_var(&net
->servers_outstanding
);
30 * Find a server by one of its addresses.
32 struct afs_server
*afs_find_server(struct afs_net
*net
,
33 const struct sockaddr_rxrpc
*srx
)
35 const struct sockaddr_in6
*a
= &srx
->transport
.sin6
, *b
;
36 const struct afs_addr_list
*alist
;
37 struct afs_server
*server
= NULL
;
42 if (srx
->transport
.sin6
.sin6_addr
.s6_addr32
[0] == 0 ||
43 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[1] == 0 ||
44 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[2] == htonl(0xffff))
51 afs_put_server(net
, server
, afs_server_trace_put_find_rsq
);
53 read_seqbegin_or_lock(&net
->fs_addr_lock
, &seq
);
56 hlist_for_each_entry_rcu(server
, &net
->fs_addresses6
, addr6_link
) {
57 alist
= rcu_dereference(server
->addresses
);
58 for (i
= alist
->nr_ipv4
; i
< alist
->nr_addrs
; i
++) {
59 b
= &alist
->addrs
[i
].transport
.sin6
;
60 diff
= ((u16 __force
)a
->sin6_port
-
61 (u16 __force
)b
->sin6_port
);
63 diff
= memcmp(&a
->sin6_addr
,
65 sizeof(struct in6_addr
));
71 hlist_for_each_entry_rcu(server
, &net
->fs_addresses4
, addr4_link
) {
72 alist
= rcu_dereference(server
->addresses
);
73 for (i
= 0; i
< alist
->nr_ipv4
; i
++) {
74 b
= &alist
->addrs
[i
].transport
.sin6
;
75 diff
= ((u16 __force
)a
->sin6_port
-
76 (u16 __force
)b
->sin6_port
);
78 diff
= ((u32 __force
)a
->sin6_addr
.s6_addr32
[3] -
79 (u32 __force
)b
->sin6_addr
.s6_addr32
[3]);
88 if (server
&& !atomic_inc_not_zero(&server
->usage
))
91 } while (need_seqretry(&net
->fs_addr_lock
, seq
));
93 done_seqretry(&net
->fs_addr_lock
, seq
);
100 * Look up a server by its UUID
102 struct afs_server
*afs_find_server_by_uuid(struct afs_net
*net
, const uuid_t
*uuid
)
104 struct afs_server
*server
= NULL
;
111 /* Unfortunately, rbtree walking doesn't give reliable results
112 * under just the RCU read lock, so we have to check for
116 afs_put_server(net
, server
, afs_server_trace_put_uuid_rsq
);
119 read_seqbegin_or_lock(&net
->fs_lock
, &seq
);
121 p
= net
->fs_servers
.rb_node
;
123 server
= rb_entry(p
, struct afs_server
, uuid_rb
);
125 diff
= memcmp(uuid
, &server
->uuid
, sizeof(*uuid
));
128 } else if (diff
> 0) {
131 afs_get_server(server
, afs_server_trace_get_by_uuid
);
137 } while (need_seqretry(&net
->fs_lock
, seq
));
139 done_seqretry(&net
->fs_lock
, seq
);
141 _leave(" = %p", server
);
146 * Install a server record in the namespace tree
148 static struct afs_server
*afs_install_server(struct afs_net
*net
,
149 struct afs_server
*candidate
)
151 const struct afs_addr_list
*alist
;
152 struct afs_server
*server
;
153 struct rb_node
**pp
, *p
;
154 int ret
= -EEXIST
, diff
;
156 _enter("%p", candidate
);
158 write_seqlock(&net
->fs_lock
);
160 /* Firstly install the server in the UUID lookup tree */
161 pp
= &net
->fs_servers
.rb_node
;
165 _debug("- consider %p", p
);
166 server
= rb_entry(p
, struct afs_server
, uuid_rb
);
167 diff
= memcmp(&candidate
->uuid
, &server
->uuid
, sizeof(uuid_t
));
169 pp
= &(*pp
)->rb_left
;
171 pp
= &(*pp
)->rb_right
;
177 rb_link_node(&server
->uuid_rb
, p
, pp
);
178 rb_insert_color(&server
->uuid_rb
, &net
->fs_servers
);
179 hlist_add_head_rcu(&server
->proc_link
, &net
->fs_proc
);
181 write_seqlock(&net
->fs_addr_lock
);
182 alist
= rcu_dereference_protected(server
->addresses
,
183 lockdep_is_held(&net
->fs_addr_lock
.lock
));
185 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
186 * it in the IPv4 and/or IPv6 reverse-map lists.
188 * TODO: For speed we want to use something other than a flat list
189 * here; even sorting the list in terms of lowest address would help a
190 * bit, but anything we might want to do gets messy and memory
193 if (alist
->nr_ipv4
> 0)
194 hlist_add_head_rcu(&server
->addr4_link
, &net
->fs_addresses4
);
195 if (alist
->nr_addrs
> alist
->nr_ipv4
)
196 hlist_add_head_rcu(&server
->addr6_link
, &net
->fs_addresses6
);
198 write_sequnlock(&net
->fs_addr_lock
);
202 afs_get_server(server
, afs_server_trace_get_install
);
203 write_sequnlock(&net
->fs_lock
);
208 * allocate a new server record
210 static struct afs_server
*afs_alloc_server(struct afs_net
*net
,
212 struct afs_addr_list
*alist
)
214 struct afs_server
*server
;
218 server
= kzalloc(sizeof(struct afs_server
), GFP_KERNEL
);
222 atomic_set(&server
->usage
, 1);
223 server
->debug_id
= atomic_inc_return(&afs_server_debug_id
);
224 RCU_INIT_POINTER(server
->addresses
, alist
);
225 server
->addr_version
= alist
->version
;
226 server
->uuid
= *uuid
;
227 server
->update_at
= ktime_get_real_seconds() + afs_server_update_delay
;
228 rwlock_init(&server
->fs_lock
);
229 INIT_HLIST_HEAD(&server
->cb_volumes
);
230 rwlock_init(&server
->cb_break_lock
);
231 init_waitqueue_head(&server
->probe_wq
);
232 spin_lock_init(&server
->probe_lock
);
234 afs_inc_servers_outstanding(net
);
235 trace_afs_server(server
, 1, afs_server_trace_alloc
);
236 _leave(" = %p", server
);
240 _leave(" = NULL [nomem]");
245 * Look up an address record for a server
247 static struct afs_addr_list
*afs_vl_lookup_addrs(struct afs_cell
*cell
,
248 struct key
*key
, const uuid_t
*uuid
)
250 struct afs_vl_cursor vc
;
251 struct afs_addr_list
*alist
= NULL
;
255 if (afs_begin_vlserver_operation(&vc
, cell
, key
)) {
256 while (afs_select_vlserver(&vc
)) {
257 if (test_bit(AFS_VLSERVER_FL_IS_YFS
, &vc
.server
->flags
))
258 alist
= afs_yfsvl_get_endpoints(&vc
, uuid
);
260 alist
= afs_vl_get_addrs_u(&vc
, uuid
);
263 ret
= afs_end_vlserver_operation(&vc
);
266 return ret
< 0 ? ERR_PTR(ret
) : alist
;
270 * Get or create a fileserver record.
272 struct afs_server
*afs_lookup_server(struct afs_cell
*cell
, struct key
*key
,
275 struct afs_addr_list
*alist
;
276 struct afs_server
*server
, *candidate
;
278 _enter("%p,%pU", cell
->net
, uuid
);
280 server
= afs_find_server_by_uuid(cell
->net
, uuid
);
284 alist
= afs_vl_lookup_addrs(cell
, key
, uuid
);
286 return ERR_CAST(alist
);
288 candidate
= afs_alloc_server(cell
->net
, uuid
, alist
);
290 afs_put_addrlist(alist
);
291 return ERR_PTR(-ENOMEM
);
294 server
= afs_install_server(cell
->net
, candidate
);
295 if (server
!= candidate
) {
296 afs_put_addrlist(alist
);
300 _leave(" = %p{%d}", server
, atomic_read(&server
->usage
));
305 * Set the server timer to fire after a given delay, assuming it's not already
306 * set for an earlier time.
308 static void afs_set_server_timer(struct afs_net
*net
, time64_t delay
)
311 afs_inc_servers_outstanding(net
);
312 if (timer_reduce(&net
->fs_timer
, jiffies
+ delay
* HZ
))
313 afs_dec_servers_outstanding(net
);
318 * Server management timer. We have an increment on fs_outstanding that we
319 * need to pass along to the work item.
321 void afs_servers_timer(struct timer_list
*timer
)
323 struct afs_net
*net
= container_of(timer
, struct afs_net
, fs_timer
);
326 if (!queue_work(afs_wq
, &net
->fs_manager
))
327 afs_dec_servers_outstanding(net
);
331 * Get a reference on a server object.
333 struct afs_server
*afs_get_server(struct afs_server
*server
,
334 enum afs_server_trace reason
)
336 unsigned int u
= atomic_inc_return(&server
->usage
);
338 trace_afs_server(server
, u
, reason
);
343 * Release a reference on a server record.
345 void afs_put_server(struct afs_net
*net
, struct afs_server
*server
,
346 enum afs_server_trace reason
)
353 server
->put_time
= ktime_get_real_seconds();
355 usage
= atomic_dec_return(&server
->usage
);
357 trace_afs_server(server
, usage
, reason
);
359 if (likely(usage
> 0))
362 afs_set_server_timer(net
, afs_server_gc_delay
);
365 static void afs_server_rcu(struct rcu_head
*rcu
)
367 struct afs_server
*server
= container_of(rcu
, struct afs_server
, rcu
);
369 trace_afs_server(server
, atomic_read(&server
->usage
),
370 afs_server_trace_free
);
371 afs_put_addrlist(rcu_access_pointer(server
->addresses
));
376 * destroy a dead server
378 static void afs_destroy_server(struct afs_net
*net
, struct afs_server
*server
)
380 struct afs_addr_list
*alist
= rcu_access_pointer(server
->addresses
);
381 struct afs_addr_cursor ac
= {
383 .index
= alist
->preferred
,
387 trace_afs_server(server
, atomic_read(&server
->usage
),
388 afs_server_trace_give_up_cb
);
390 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB
, &server
->flags
))
391 afs_fs_give_up_all_callbacks(net
, server
, &ac
, NULL
);
393 wait_var_event(&server
->probe_outstanding
,
394 atomic_read(&server
->probe_outstanding
) == 0);
396 trace_afs_server(server
, atomic_read(&server
->usage
),
397 afs_server_trace_destroy
);
398 call_rcu(&server
->rcu
, afs_server_rcu
);
399 afs_dec_servers_outstanding(net
);
403 * Garbage collect any expired servers.
405 static void afs_gc_servers(struct afs_net
*net
, struct afs_server
*gc_list
)
407 struct afs_server
*server
;
411 while ((server
= gc_list
)) {
412 gc_list
= server
->gc_next
;
414 write_seqlock(&net
->fs_lock
);
416 deleted
= atomic_try_cmpxchg(&server
->usage
, &usage
, 0);
417 trace_afs_server(server
, usage
, afs_server_trace_gc
);
419 rb_erase(&server
->uuid_rb
, &net
->fs_servers
);
420 hlist_del_rcu(&server
->proc_link
);
422 write_sequnlock(&net
->fs_lock
);
425 write_seqlock(&net
->fs_addr_lock
);
426 if (!hlist_unhashed(&server
->addr4_link
))
427 hlist_del_rcu(&server
->addr4_link
);
428 if (!hlist_unhashed(&server
->addr6_link
))
429 hlist_del_rcu(&server
->addr6_link
);
430 write_sequnlock(&net
->fs_addr_lock
);
431 afs_destroy_server(net
, server
);
437 * Manage the records of servers known to be within a network namespace. This
438 * includes garbage collecting unused servers.
440 * Note also that we were given an increment on net->servers_outstanding by
441 * whoever queued us that we need to deal with before returning.
443 void afs_manage_servers(struct work_struct
*work
)
445 struct afs_net
*net
= container_of(work
, struct afs_net
, fs_manager
);
446 struct afs_server
*gc_list
= NULL
;
447 struct rb_node
*cursor
;
448 time64_t now
= ktime_get_real_seconds(), next_manage
= TIME64_MAX
;
449 bool purging
= !net
->live
;
453 /* Trawl the server list looking for servers that have expired from
456 read_seqlock_excl(&net
->fs_lock
);
458 for (cursor
= rb_first(&net
->fs_servers
); cursor
; cursor
= rb_next(cursor
)) {
459 struct afs_server
*server
=
460 rb_entry(cursor
, struct afs_server
, uuid_rb
);
461 int usage
= atomic_read(&server
->usage
);
463 _debug("manage %pU %u", &server
->uuid
, usage
);
465 ASSERTCMP(usage
, >=, 1);
466 ASSERTIFCMP(purging
, usage
, ==, 1);
469 time64_t expire_at
= server
->put_time
;
471 if (!test_bit(AFS_SERVER_FL_VL_FAIL
, &server
->flags
) &&
472 !test_bit(AFS_SERVER_FL_NOT_FOUND
, &server
->flags
))
473 expire_at
+= afs_server_gc_delay
;
474 if (purging
|| expire_at
<= now
) {
475 server
->gc_next
= gc_list
;
477 } else if (expire_at
< next_manage
) {
478 next_manage
= expire_at
;
483 read_sequnlock_excl(&net
->fs_lock
);
485 /* Update the timer on the way out. We have to pass an increment on
486 * servers_outstanding in the namespace that we are in to the timer or
487 * the work scheduler.
489 if (!purging
&& next_manage
< TIME64_MAX
) {
490 now
= ktime_get_real_seconds();
492 if (next_manage
- now
<= 0) {
493 if (queue_work(afs_wq
, &net
->fs_manager
))
494 afs_inc_servers_outstanding(net
);
496 afs_set_server_timer(net
, next_manage
- now
);
500 afs_gc_servers(net
, gc_list
);
502 afs_dec_servers_outstanding(net
);
503 _leave(" [%d]", atomic_read(&net
->servers_outstanding
));
506 static void afs_queue_server_manager(struct afs_net
*net
)
508 afs_inc_servers_outstanding(net
);
509 if (!queue_work(afs_wq
, &net
->fs_manager
))
510 afs_dec_servers_outstanding(net
);
514 * Purge list of servers.
516 void afs_purge_servers(struct afs_net
*net
)
520 if (del_timer_sync(&net
->fs_timer
))
521 atomic_dec(&net
->servers_outstanding
);
523 afs_queue_server_manager(net
);
526 wait_var_event(&net
->servers_outstanding
,
527 !atomic_read(&net
->servers_outstanding
));
532 * Get an update for a server's address list.
534 static noinline
bool afs_update_server_record(struct afs_fs_cursor
*fc
, struct afs_server
*server
)
536 struct afs_addr_list
*alist
, *discard
;
540 trace_afs_server(server
, atomic_read(&server
->usage
), afs_server_trace_update
);
542 alist
= afs_vl_lookup_addrs(fc
->vnode
->volume
->cell
, fc
->key
,
545 if ((PTR_ERR(alist
) == -ERESTARTSYS
||
546 PTR_ERR(alist
) == -EINTR
) &&
547 !(fc
->flags
& AFS_FS_CURSOR_INTR
) &&
549 _leave(" = t [intr]");
552 fc
->error
= PTR_ERR(alist
);
553 _leave(" = f [%d]", fc
->error
);
558 if (server
->addr_version
!= alist
->version
) {
559 write_lock(&server
->fs_lock
);
560 discard
= rcu_dereference_protected(server
->addresses
,
561 lockdep_is_held(&server
->fs_lock
));
562 rcu_assign_pointer(server
->addresses
, alist
);
563 server
->addr_version
= alist
->version
;
564 write_unlock(&server
->fs_lock
);
567 server
->update_at
= ktime_get_real_seconds() + afs_server_update_delay
;
568 afs_put_addrlist(discard
);
574 * See if a server's address list needs updating.
576 bool afs_check_server_record(struct afs_fs_cursor
*fc
, struct afs_server
*server
)
578 time64_t now
= ktime_get_real_seconds();
581 int ret
, retries
= 0;
588 diff
= READ_ONCE(server
->update_at
) - now
;
590 _leave(" = t [not now %ld]", diff
);
594 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING
, &server
->flags
)) {
595 success
= afs_update_server_record(fc
, server
);
596 clear_bit_unlock(AFS_SERVER_FL_UPDATING
, &server
->flags
);
597 wake_up_bit(&server
->flags
, AFS_SERVER_FL_UPDATING
);
598 _leave(" = %d", success
);
602 ret
= wait_on_bit(&server
->flags
, AFS_SERVER_FL_UPDATING
,
604 if (ret
== -ERESTARTSYS
) {
605 if (!(fc
->flags
& AFS_FS_CURSOR_INTR
) && server
->addresses
) {
606 _leave(" = t [intr]");
610 _leave(" = f [intr]");
616 _leave(" = f [stale]");