split dev_queue
[cor.git] / fs / afs / server.c
blob1686bf188ccd056c7453db63557efddceaf08833
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)
6 */
8 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include "afs_fs.h"
11 #include "internal.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;
38 unsigned int i;
39 bool ipv6 = true;
40 int seq = 0, diff;
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))
45 ipv6 = false;
47 rcu_read_lock();
49 do {
50 if (server)
51 afs_put_server(net, server, afs_server_trace_put_find_rsq);
52 server = NULL;
53 read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
55 if (ipv6) {
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);
62 if (diff == 0)
63 diff = memcmp(&a->sin6_addr,
64 &b->sin6_addr,
65 sizeof(struct in6_addr));
66 if (diff == 0)
67 goto found;
70 } else {
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);
77 if (diff == 0)
78 diff = ((u32 __force)a->sin6_addr.s6_addr32[3] -
79 (u32 __force)b->sin6_addr.s6_addr32[3]);
80 if (diff == 0)
81 goto found;
86 server = NULL;
87 found:
88 if (server && !atomic_inc_not_zero(&server->usage))
89 server = NULL;
91 } while (need_seqretry(&net->fs_addr_lock, seq));
93 done_seqretry(&net->fs_addr_lock, seq);
95 rcu_read_unlock();
96 return server;
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;
105 struct rb_node *p;
106 int diff, seq = 0;
108 _enter("%pU", uuid);
110 do {
111 /* Unfortunately, rbtree walking doesn't give reliable results
112 * under just the RCU read lock, so we have to check for
113 * changes.
115 if (server)
116 afs_put_server(net, server, afs_server_trace_put_uuid_rsq);
117 server = NULL;
119 read_seqbegin_or_lock(&net->fs_lock, &seq);
121 p = net->fs_servers.rb_node;
122 while (p) {
123 server = rb_entry(p, struct afs_server, uuid_rb);
125 diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
126 if (diff < 0) {
127 p = p->rb_left;
128 } else if (diff > 0) {
129 p = p->rb_right;
130 } else {
131 afs_get_server(server, afs_server_trace_get_by_uuid);
132 break;
135 server = NULL;
137 } while (need_seqretry(&net->fs_lock, seq));
139 done_seqretry(&net->fs_lock, seq);
141 _leave(" = %p", server);
142 return 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 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;
162 p = NULL;
163 while (*pp) {
164 p = *pp;
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));
168 if (diff < 0)
169 pp = &(*pp)->rb_left;
170 else if (diff > 0)
171 pp = &(*pp)->rb_right;
172 else
173 goto exists;
176 server = candidate;
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
191 * intensive.
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);
200 exists:
201 afs_get_server(server, afs_server_trace_get_install);
202 write_sequnlock(&net->fs_lock);
203 return server;
207 * allocate a new server record
209 static struct afs_server *afs_alloc_server(struct afs_net *net,
210 const uuid_t *uuid,
211 struct afs_addr_list *alist)
213 struct afs_server *server;
215 _enter("");
217 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
218 if (!server)
219 goto enomem;
221 atomic_set(&server->usage, 1);
222 server->debug_id = atomic_inc_return(&afs_server_debug_id);
223 RCU_INIT_POINTER(server->addresses, alist);
224 server->addr_version = alist->version;
225 server->uuid = *uuid;
226 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
227 rwlock_init(&server->fs_lock);
228 INIT_HLIST_HEAD(&server->cb_volumes);
229 rwlock_init(&server->cb_break_lock);
230 init_waitqueue_head(&server->probe_wq);
231 spin_lock_init(&server->probe_lock);
233 afs_inc_servers_outstanding(net);
234 trace_afs_server(server, 1, afs_server_trace_alloc);
235 _leave(" = %p", server);
236 return server;
238 enomem:
239 _leave(" = NULL [nomem]");
240 return NULL;
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_vl_cursor vc;
250 struct afs_addr_list *alist = NULL;
251 int ret;
253 ret = -ERESTARTSYS;
254 if (afs_begin_vlserver_operation(&vc, cell, key)) {
255 while (afs_select_vlserver(&vc)) {
256 if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags))
257 alist = afs_yfsvl_get_endpoints(&vc, uuid);
258 else
259 alist = afs_vl_get_addrs_u(&vc, uuid);
262 ret = afs_end_vlserver_operation(&vc);
265 return ret < 0 ? ERR_PTR(ret) : alist;
269 * Get or create a fileserver record.
271 struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
272 const uuid_t *uuid)
274 struct afs_addr_list *alist;
275 struct afs_server *server, *candidate;
277 _enter("%p,%pU", cell->net, uuid);
279 server = afs_find_server_by_uuid(cell->net, uuid);
280 if (server)
281 return server;
283 alist = afs_vl_lookup_addrs(cell, key, uuid);
284 if (IS_ERR(alist))
285 return ERR_CAST(alist);
287 candidate = afs_alloc_server(cell->net, uuid, alist);
288 if (!candidate) {
289 afs_put_addrlist(alist);
290 return ERR_PTR(-ENOMEM);
293 server = afs_install_server(cell->net, candidate);
294 if (server != candidate) {
295 afs_put_addrlist(alist);
296 kfree(candidate);
299 _leave(" = %p{%d}", server, atomic_read(&server->usage));
300 return server;
304 * Set the server timer to fire after a given delay, assuming it's not already
305 * set for an earlier time.
307 static void afs_set_server_timer(struct afs_net *net, time64_t delay)
309 if (net->live) {
310 afs_inc_servers_outstanding(net);
311 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
312 afs_dec_servers_outstanding(net);
317 * Server management timer. We have an increment on fs_outstanding that we
318 * need to pass along to the work item.
320 void afs_servers_timer(struct timer_list *timer)
322 struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
324 _enter("");
325 if (!queue_work(afs_wq, &net->fs_manager))
326 afs_dec_servers_outstanding(net);
330 * Get a reference on a server object.
332 struct afs_server *afs_get_server(struct afs_server *server,
333 enum afs_server_trace reason)
335 unsigned int u = atomic_inc_return(&server->usage);
337 trace_afs_server(server, u, reason);
338 return server;
342 * Release a reference on a server record.
344 void afs_put_server(struct afs_net *net, struct afs_server *server,
345 enum afs_server_trace reason)
347 unsigned int usage;
349 if (!server)
350 return;
352 server->put_time = ktime_get_real_seconds();
354 usage = atomic_dec_return(&server->usage);
356 trace_afs_server(server, usage, reason);
358 if (likely(usage > 0))
359 return;
361 afs_set_server_timer(net, afs_server_gc_delay);
364 static void afs_server_rcu(struct rcu_head *rcu)
366 struct afs_server *server = container_of(rcu, struct afs_server, rcu);
368 trace_afs_server(server, atomic_read(&server->usage),
369 afs_server_trace_free);
370 afs_put_addrlist(rcu_access_pointer(server->addresses));
371 kfree(server);
375 * destroy a dead server
377 static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
379 struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
380 struct afs_addr_cursor ac = {
381 .alist = alist,
382 .index = alist->preferred,
383 .error = 0,
386 trace_afs_server(server, atomic_read(&server->usage),
387 afs_server_trace_give_up_cb);
389 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
390 afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
392 wait_var_event(&server->probe_outstanding,
393 atomic_read(&server->probe_outstanding) == 0);
395 trace_afs_server(server, atomic_read(&server->usage),
396 afs_server_trace_destroy);
397 call_rcu(&server->rcu, afs_server_rcu);
398 afs_dec_servers_outstanding(net);
402 * Garbage collect any expired servers.
404 static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
406 struct afs_server *server;
407 bool deleted;
408 int usage;
410 while ((server = gc_list)) {
411 gc_list = server->gc_next;
413 write_seqlock(&net->fs_lock);
414 usage = 1;
415 deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
416 trace_afs_server(server, usage, afs_server_trace_gc);
417 if (deleted) {
418 rb_erase(&server->uuid_rb, &net->fs_servers);
419 hlist_del_rcu(&server->proc_link);
421 write_sequnlock(&net->fs_lock);
423 if (deleted) {
424 write_seqlock(&net->fs_addr_lock);
425 if (!hlist_unhashed(&server->addr4_link))
426 hlist_del_rcu(&server->addr4_link);
427 if (!hlist_unhashed(&server->addr6_link))
428 hlist_del_rcu(&server->addr6_link);
429 write_sequnlock(&net->fs_addr_lock);
430 afs_destroy_server(net, server);
436 * Manage the records of servers known to be within a network namespace. This
437 * includes garbage collecting unused servers.
439 * Note also that we were given an increment on net->servers_outstanding by
440 * whoever queued us that we need to deal with before returning.
442 void afs_manage_servers(struct work_struct *work)
444 struct afs_net *net = container_of(work, struct afs_net, fs_manager);
445 struct afs_server *gc_list = NULL;
446 struct rb_node *cursor;
447 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
448 bool purging = !net->live;
450 _enter("");
452 /* Trawl the server list looking for servers that have expired from
453 * lack of use.
455 read_seqlock_excl(&net->fs_lock);
457 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
458 struct afs_server *server =
459 rb_entry(cursor, struct afs_server, uuid_rb);
460 int usage = atomic_read(&server->usage);
462 _debug("manage %pU %u", &server->uuid, usage);
464 ASSERTCMP(usage, >=, 1);
465 ASSERTIFCMP(purging, usage, ==, 1);
467 if (usage == 1) {
468 time64_t expire_at = server->put_time;
470 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
471 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
472 expire_at += afs_server_gc_delay;
473 if (purging || expire_at <= now) {
474 server->gc_next = gc_list;
475 gc_list = server;
476 } else if (expire_at < next_manage) {
477 next_manage = expire_at;
482 read_sequnlock_excl(&net->fs_lock);
484 /* Update the timer on the way out. We have to pass an increment on
485 * servers_outstanding in the namespace that we are in to the timer or
486 * the work scheduler.
488 if (!purging && next_manage < TIME64_MAX) {
489 now = ktime_get_real_seconds();
491 if (next_manage - now <= 0) {
492 if (queue_work(afs_wq, &net->fs_manager))
493 afs_inc_servers_outstanding(net);
494 } else {
495 afs_set_server_timer(net, next_manage - now);
499 afs_gc_servers(net, gc_list);
501 afs_dec_servers_outstanding(net);
502 _leave(" [%d]", atomic_read(&net->servers_outstanding));
505 static void afs_queue_server_manager(struct afs_net *net)
507 afs_inc_servers_outstanding(net);
508 if (!queue_work(afs_wq, &net->fs_manager))
509 afs_dec_servers_outstanding(net);
513 * Purge list of servers.
515 void afs_purge_servers(struct afs_net *net)
517 _enter("");
519 if (del_timer_sync(&net->fs_timer))
520 atomic_dec(&net->servers_outstanding);
522 afs_queue_server_manager(net);
524 _debug("wait");
525 wait_var_event(&net->servers_outstanding,
526 !atomic_read(&net->servers_outstanding));
527 _leave("");
531 * Get an update for a server's address list.
533 static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
535 struct afs_addr_list *alist, *discard;
537 _enter("");
539 trace_afs_server(server, atomic_read(&server->usage), afs_server_trace_update);
541 alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
542 &server->uuid);
543 if (IS_ERR(alist)) {
544 if ((PTR_ERR(alist) == -ERESTARTSYS ||
545 PTR_ERR(alist) == -EINTR) &&
546 !(fc->flags & AFS_FS_CURSOR_INTR) &&
547 server->addresses) {
548 _leave(" = t [intr]");
549 return true;
551 fc->error = PTR_ERR(alist);
552 _leave(" = f [%d]", fc->error);
553 return false;
556 discard = alist;
557 if (server->addr_version != alist->version) {
558 write_lock(&server->fs_lock);
559 discard = rcu_dereference_protected(server->addresses,
560 lockdep_is_held(&server->fs_lock));
561 rcu_assign_pointer(server->addresses, alist);
562 server->addr_version = alist->version;
563 write_unlock(&server->fs_lock);
566 server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
567 afs_put_addrlist(discard);
568 _leave(" = t");
569 return true;
573 * See if a server's address list needs updating.
575 bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
577 time64_t now = ktime_get_real_seconds();
578 long diff;
579 bool success;
580 int ret, retries = 0;
582 _enter("");
584 ASSERT(server);
586 retry:
587 diff = READ_ONCE(server->update_at) - now;
588 if (diff > 0) {
589 _leave(" = t [not now %ld]", diff);
590 return true;
593 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
594 success = afs_update_server_record(fc, server);
595 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
596 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
597 _leave(" = %d", success);
598 return success;
601 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
602 TASK_INTERRUPTIBLE);
603 if (ret == -ERESTARTSYS) {
604 if (!(fc->flags & AFS_FS_CURSOR_INTR) && server->addresses) {
605 _leave(" = t [intr]");
606 return true;
608 fc->error = ret;
609 _leave(" = f [intr]");
610 return false;
613 retries++;
614 if (retries == 4) {
615 _leave(" = f [stale]");
616 ret = -ESTALE;
617 return false;
619 goto retry;