1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* AFS server record management 3 * 4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/sched.h> 9 #include <linux/slab.h> 10 #include "afs_fs.h" 11 #include "internal.h" 12 #include "protocol_yfs.h" 13 14 static unsigned afs_server_gc_delay = 10; /* Server record timeout in seconds */ 15 static atomic_t afs_server_debug_id; 16 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 *); 20 21 /* 22 * Find a server by one of its addresses. 23 */ 24 struct afs_server *afs_find_server(struct afs_net *net, const struct rxrpc_peer *peer) 25 { 26 const struct afs_endpoint_state *estate; 27 const struct afs_addr_list *alist; 28 struct afs_server *server = NULL; 29 unsigned int i; 30 int seq = 1; 31 32 rcu_read_lock(); 33 34 do { 35 if (server) 36 afs_unuse_server_notime(net, server, afs_server_trace_put_find_rsq); 37 server = NULL; 38 seq++; /* 2 on the 1st/lockless path, otherwise odd */ 39 read_seqbegin_or_lock(&net->fs_addr_lock, &seq); 40 41 hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) { 42 estate = rcu_dereference(server->endpoint_state); 43 alist = estate->addresses; 44 for (i = 0; i < alist->nr_addrs; i++) 45 if (alist->addrs[i].peer == peer) 46 goto found; 47 } 48 49 server = NULL; 50 continue; 51 found: 52 server = afs_maybe_use_server(server, afs_server_trace_get_by_addr); 53 54 } while (need_seqretry(&net->fs_addr_lock, seq)); 55 56 done_seqretry(&net->fs_addr_lock, seq); 57 58 rcu_read_unlock(); 59 return server; 60 } 61 62 /* 63 * Look up a server by its UUID and mark it active. 64 */ 65 struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid) 66 { 67 struct afs_server *server = NULL; 68 struct rb_node *p; 69 int diff, seq = 1; 70 71 _enter("%pU", uuid); 72 73 do { 74 /* Unfortunately, rbtree walking doesn't give reliable results 75 * under just the RCU read lock, so we have to check for 76 * changes. 77 */ 78 if (server) 79 afs_unuse_server(net, server, afs_server_trace_put_uuid_rsq); 80 server = NULL; 81 seq++; /* 2 on the 1st/lockless path, otherwise odd */ 82 read_seqbegin_or_lock(&net->fs_lock, &seq); 83 84 p = net->fs_servers.rb_node; 85 while (p) { 86 server = rb_entry(p, struct afs_server, uuid_rb); 87 88 diff = memcmp(uuid, &server->uuid, sizeof(*uuid)); 89 if (diff < 0) { 90 p = p->rb_left; 91 } else if (diff > 0) { 92 p = p->rb_right; 93 } else { 94 afs_use_server(server, afs_server_trace_get_by_uuid); 95 break; 96 } 97 98 server = NULL; 99 } 100 } while (need_seqretry(&net->fs_lock, seq)); 101 102 done_seqretry(&net->fs_lock, seq); 103 104 _leave(" = %p", server); 105 return server; 106 } 107 108 /* 109 * Install a server record in the namespace tree. If there's a clash, we stick 110 * it into a list anchored on whichever afs_server struct is actually in the 111 * tree. 112 */ 113 static struct afs_server *afs_install_server(struct afs_cell *cell, 114 struct afs_server *candidate) 115 { 116 const struct afs_endpoint_state *estate; 117 const struct afs_addr_list *alist; 118 struct afs_server *server, *next; 119 struct afs_net *net = cell->net; 120 struct rb_node **pp, *p; 121 int diff; 122 123 _enter("%p", candidate); 124 125 write_seqlock(&net->fs_lock); 126 127 /* Firstly install the server in the UUID lookup tree */ 128 pp = &net->fs_servers.rb_node; 129 p = NULL; 130 while (*pp) { 131 p = *pp; 132 _debug("- consider %p", p); 133 server = rb_entry(p, struct afs_server, uuid_rb); 134 diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t)); 135 if (diff < 0) { 136 pp = &(*pp)->rb_left; 137 } else if (diff > 0) { 138 pp = &(*pp)->rb_right; 139 } else { 140 if (server->cell == cell) 141 goto exists; 142 143 /* We have the same UUID representing servers in 144 * different cells. Append the new server to the list. 145 */ 146 for (;;) { 147 next = rcu_dereference_protected( 148 server->uuid_next, 149 lockdep_is_held(&net->fs_lock.lock)); 150 if (!next) 151 break; 152 server = next; 153 } 154 rcu_assign_pointer(server->uuid_next, candidate); 155 candidate->uuid_prev = server; 156 server = candidate; 157 goto added_dup; 158 } 159 } 160 161 server = candidate; 162 rb_link_node(&server->uuid_rb, p, pp); 163 rb_insert_color(&server->uuid_rb, &net->fs_servers); 164 hlist_add_head_rcu(&server->proc_link, &net->fs_proc); 165 166 added_dup: 167 write_seqlock(&net->fs_addr_lock); 168 estate = rcu_dereference_protected(server->endpoint_state, 169 lockdep_is_held(&net->fs_addr_lock.lock)); 170 alist = estate->addresses; 171 172 /* Secondly, if the server has any IPv4 and/or IPv6 addresses, install 173 * it in the IPv4 and/or IPv6 reverse-map lists. 174 * 175 * TODO: For speed we want to use something other than a flat list 176 * here; even sorting the list in terms of lowest address would help a 177 * bit, but anything we might want to do gets messy and memory 178 * intensive. 179 */ 180 if (alist->nr_ipv4 > 0) 181 hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4); 182 if (alist->nr_addrs > alist->nr_ipv4) 183 hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6); 184 185 write_sequnlock(&net->fs_addr_lock); 186 187 exists: 188 afs_get_server(server, afs_server_trace_get_install); 189 write_sequnlock(&net->fs_lock); 190 return server; 191 } 192 193 /* 194 * Allocate a new server record and mark it active. 195 */ 196 static struct afs_server *afs_alloc_server(struct afs_cell *cell, 197 const uuid_t *uuid, 198 struct afs_addr_list *alist) 199 { 200 struct afs_endpoint_state *estate; 201 struct afs_server *server; 202 struct afs_net *net = cell->net; 203 204 _enter(""); 205 206 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL); 207 if (!server) 208 goto enomem; 209 210 estate = kzalloc(sizeof(struct afs_endpoint_state), GFP_KERNEL); 211 if (!estate) 212 goto enomem_server; 213 214 refcount_set(&server->ref, 1); 215 atomic_set(&server->active, 1); 216 server->debug_id = atomic_inc_return(&afs_server_debug_id); 217 server->addr_version = alist->version; 218 server->uuid = *uuid; 219 rwlock_init(&server->fs_lock); 220 INIT_LIST_HEAD(&server->volumes); 221 init_waitqueue_head(&server->probe_wq); 222 INIT_LIST_HEAD(&server->probe_link); 223 spin_lock_init(&server->probe_lock); 224 server->cell = cell; 225 server->rtt = UINT_MAX; 226 server->service_id = FS_SERVICE; 227 228 server->probe_counter = 1; 229 server->probed_at = jiffies - LONG_MAX / 2; 230 refcount_set(&estate->ref, 1); 231 estate->addresses = alist; 232 estate->server_id = server->debug_id; 233 estate->probe_seq = 1; 234 rcu_assign_pointer(server->endpoint_state, estate); 235 236 afs_inc_servers_outstanding(net); 237 trace_afs_server(server->debug_id, 1, 1, afs_server_trace_alloc); 238 trace_afs_estate(estate->server_id, estate->probe_seq, refcount_read(&estate->ref), 239 afs_estate_trace_alloc_server); 240 _leave(" = %p", server); 241 return server; 242 243 enomem_server: 244 kfree(server); 245 enomem: 246 _leave(" = NULL [nomem]"); 247 return NULL; 248 } 249 250 /* 251 * Look up an address record for a server 252 */ 253 static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell, 254 struct key *key, const uuid_t *uuid) 255 { 256 struct afs_vl_cursor vc; 257 struct afs_addr_list *alist = NULL; 258 int ret; 259 260 ret = -ERESTARTSYS; 261 if (afs_begin_vlserver_operation(&vc, cell, key)) { 262 while (afs_select_vlserver(&vc)) { 263 if (test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags)) 264 alist = afs_yfsvl_get_endpoints(&vc, uuid); 265 else 266 alist = afs_vl_get_addrs_u(&vc, uuid); 267 } 268 269 ret = afs_end_vlserver_operation(&vc); 270 } 271 272 return ret < 0 ? ERR_PTR(ret) : alist; 273 } 274 275 /* 276 * Get or create a fileserver record. 277 */ 278 struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key, 279 const uuid_t *uuid, u32 addr_version) 280 { 281 struct afs_addr_list *alist; 282 struct afs_server *server, *candidate; 283 284 _enter("%p,%pU", cell->net, uuid); 285 286 server = afs_find_server_by_uuid(cell->net, uuid); 287 if (server) { 288 if (server->addr_version != addr_version) 289 set_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags); 290 return server; 291 } 292 293 alist = afs_vl_lookup_addrs(cell, key, uuid); 294 if (IS_ERR(alist)) 295 return ERR_CAST(alist); 296 297 candidate = afs_alloc_server(cell, uuid, alist); 298 if (!candidate) { 299 afs_put_addrlist(alist, afs_alist_trace_put_server_oom); 300 return ERR_PTR(-ENOMEM); 301 } 302 303 server = afs_install_server(cell, candidate); 304 if (server != candidate) { 305 afs_put_addrlist(alist, afs_alist_trace_put_server_dup); 306 kfree(candidate); 307 } else { 308 /* Immediately dispatch an asynchronous probe to each interface 309 * on the fileserver. This will make sure the repeat-probing 310 * service is started. 311 */ 312 afs_fs_probe_fileserver(cell->net, server, alist, key); 313 } 314 315 return server; 316 } 317 318 /* 319 * Set the server timer to fire after a given delay, assuming it's not already 320 * set for an earlier time. 321 */ 322 static void afs_set_server_timer(struct afs_net *net, time64_t delay) 323 { 324 if (net->live) { 325 afs_inc_servers_outstanding(net); 326 if (timer_reduce(&net->fs_timer, jiffies + delay * HZ)) 327 afs_dec_servers_outstanding(net); 328 } 329 } 330 331 /* 332 * Server management timer. We have an increment on fs_outstanding that we 333 * need to pass along to the work item. 334 */ 335 void afs_servers_timer(struct timer_list *timer) 336 { 337 struct afs_net *net = container_of(timer, struct afs_net, fs_timer); 338 339 _enter(""); 340 if (!queue_work(afs_wq, &net->fs_manager)) 341 afs_dec_servers_outstanding(net); 342 } 343 344 /* 345 * Get a reference on a server object. 346 */ 347 struct afs_server *afs_get_server(struct afs_server *server, 348 enum afs_server_trace reason) 349 { 350 unsigned int a; 351 int r; 352 353 __refcount_inc(&server->ref, &r); 354 a = atomic_read(&server->active); 355 trace_afs_server(server->debug_id, r + 1, a, reason); 356 return server; 357 } 358 359 /* 360 * Try to get a reference on a server object. 361 */ 362 static struct afs_server *afs_maybe_use_server(struct afs_server *server, 363 enum afs_server_trace reason) 364 { 365 unsigned int a; 366 int r; 367 368 if (!__refcount_inc_not_zero(&server->ref, &r)) 369 return NULL; 370 371 a = atomic_inc_return(&server->active); 372 trace_afs_server(server->debug_id, r + 1, a, reason); 373 return server; 374 } 375 376 /* 377 * Get an active count on a server object. 378 */ 379 struct afs_server *afs_use_server(struct afs_server *server, enum afs_server_trace reason) 380 { 381 unsigned int a; 382 int r; 383 384 __refcount_inc(&server->ref, &r); 385 a = atomic_inc_return(&server->active); 386 387 trace_afs_server(server->debug_id, r + 1, a, reason); 388 return server; 389 } 390 391 /* 392 * Release a reference on a server record. 393 */ 394 void afs_put_server(struct afs_net *net, struct afs_server *server, 395 enum afs_server_trace reason) 396 { 397 unsigned int a, debug_id = server->debug_id; 398 bool zero; 399 int r; 400 401 if (!server) 402 return; 403 404 a = atomic_read(&server->active); 405 zero = __refcount_dec_and_test(&server->ref, &r); 406 trace_afs_server(debug_id, r - 1, a, reason); 407 if (unlikely(zero)) 408 __afs_put_server(net, server); 409 } 410 411 /* 412 * Drop an active count on a server object without updating the last-unused 413 * time. 414 */ 415 void afs_unuse_server_notime(struct afs_net *net, struct afs_server *server, 416 enum afs_server_trace reason) 417 { 418 if (server) { 419 unsigned int active = atomic_dec_return(&server->active); 420 421 if (active == 0) 422 afs_set_server_timer(net, afs_server_gc_delay); 423 afs_put_server(net, server, reason); 424 } 425 } 426 427 /* 428 * Drop an active count on a server object. 429 */ 430 void afs_unuse_server(struct afs_net *net, struct afs_server *server, 431 enum afs_server_trace reason) 432 { 433 if (server) { 434 server->unuse_time = ktime_get_real_seconds(); 435 afs_unuse_server_notime(net, server, reason); 436 } 437 } 438 439 static void afs_server_rcu(struct rcu_head *rcu) 440 { 441 struct afs_server *server = container_of(rcu, struct afs_server, rcu); 442 443 trace_afs_server(server->debug_id, refcount_read(&server->ref), 444 atomic_read(&server->active), afs_server_trace_free); 445 afs_put_endpoint_state(rcu_access_pointer(server->endpoint_state), 446 afs_estate_trace_put_server); 447 kfree(server); 448 } 449 450 static void __afs_put_server(struct afs_net *net, struct afs_server *server) 451 { 452 call_rcu(&server->rcu, afs_server_rcu); 453 afs_dec_servers_outstanding(net); 454 } 455 456 static void afs_give_up_callbacks(struct afs_net *net, struct afs_server *server) 457 { 458 struct afs_endpoint_state *estate = rcu_access_pointer(server->endpoint_state); 459 struct afs_addr_list *alist = estate->addresses; 460 461 afs_fs_give_up_all_callbacks(net, server, &alist->addrs[alist->preferred], NULL); 462 } 463 464 /* 465 * destroy a dead server 466 */ 467 static void afs_destroy_server(struct afs_net *net, struct afs_server *server) 468 { 469 if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags)) 470 afs_give_up_callbacks(net, server); 471 472 afs_put_server(net, server, afs_server_trace_destroy); 473 } 474 475 /* 476 * Garbage collect any expired servers. 477 */ 478 static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list) 479 { 480 struct afs_server *server, *next, *prev; 481 int active; 482 483 while ((server = gc_list)) { 484 gc_list = server->gc_next; 485 486 write_seqlock(&net->fs_lock); 487 488 active = atomic_read(&server->active); 489 if (active == 0) { 490 trace_afs_server(server->debug_id, refcount_read(&server->ref), 491 active, afs_server_trace_gc); 492 next = rcu_dereference_protected( 493 server->uuid_next, lockdep_is_held(&net->fs_lock.lock)); 494 prev = server->uuid_prev; 495 if (!prev) { 496 /* The one at the front is in the tree */ 497 if (!next) { 498 rb_erase(&server->uuid_rb, &net->fs_servers); 499 } else { 500 rb_replace_node_rcu(&server->uuid_rb, 501 &next->uuid_rb, 502 &net->fs_servers); 503 next->uuid_prev = NULL; 504 } 505 } else { 506 /* This server is not at the front */ 507 rcu_assign_pointer(prev->uuid_next, next); 508 if (next) 509 next->uuid_prev = prev; 510 } 511 512 list_del(&server->probe_link); 513 hlist_del_rcu(&server->proc_link); 514 if (!hlist_unhashed(&server->addr4_link)) 515 hlist_del_rcu(&server->addr4_link); 516 if (!hlist_unhashed(&server->addr6_link)) 517 hlist_del_rcu(&server->addr6_link); 518 } 519 write_sequnlock(&net->fs_lock); 520 521 if (active == 0) 522 afs_destroy_server(net, server); 523 } 524 } 525 526 /* 527 * Manage the records of servers known to be within a network namespace. This 528 * includes garbage collecting unused servers. 529 * 530 * Note also that we were given an increment on net->servers_outstanding by 531 * whoever queued us that we need to deal with before returning. 532 */ 533 void afs_manage_servers(struct work_struct *work) 534 { 535 struct afs_net *net = container_of(work, struct afs_net, fs_manager); 536 struct afs_server *gc_list = NULL; 537 struct rb_node *cursor; 538 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX; 539 bool purging = !net->live; 540 541 _enter(""); 542 543 /* Trawl the server list looking for servers that have expired from 544 * lack of use. 545 */ 546 read_seqlock_excl(&net->fs_lock); 547 548 for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) { 549 struct afs_server *server = 550 rb_entry(cursor, struct afs_server, uuid_rb); 551 int active = atomic_read(&server->active); 552 553 _debug("manage %pU %u", &server->uuid, active); 554 555 if (purging) { 556 trace_afs_server(server->debug_id, refcount_read(&server->ref), 557 active, afs_server_trace_purging); 558 if (active != 0) 559 pr_notice("Can't purge s=%08x\n", server->debug_id); 560 } 561 562 if (active == 0) { 563 time64_t expire_at = server->unuse_time; 564 565 if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) && 566 !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags)) 567 expire_at += afs_server_gc_delay; 568 if (purging || expire_at <= now) { 569 server->gc_next = gc_list; 570 gc_list = server; 571 } else if (expire_at < next_manage) { 572 next_manage = expire_at; 573 } 574 } 575 } 576 577 read_sequnlock_excl(&net->fs_lock); 578 579 /* Update the timer on the way out. We have to pass an increment on 580 * servers_outstanding in the namespace that we are in to the timer or 581 * the work scheduler. 582 */ 583 if (!purging && next_manage < TIME64_MAX) { 584 now = ktime_get_real_seconds(); 585 586 if (next_manage - now <= 0) { 587 if (queue_work(afs_wq, &net->fs_manager)) 588 afs_inc_servers_outstanding(net); 589 } else { 590 afs_set_server_timer(net, next_manage - now); 591 } 592 } 593 594 afs_gc_servers(net, gc_list); 595 596 afs_dec_servers_outstanding(net); 597 _leave(" [%d]", atomic_read(&net->servers_outstanding)); 598 } 599 600 static void afs_queue_server_manager(struct afs_net *net) 601 { 602 afs_inc_servers_outstanding(net); 603 if (!queue_work(afs_wq, &net->fs_manager)) 604 afs_dec_servers_outstanding(net); 605 } 606 607 /* 608 * Purge list of servers. 609 */ 610 void afs_purge_servers(struct afs_net *net) 611 { 612 _enter(""); 613 614 if (del_timer_sync(&net->fs_timer)) 615 afs_dec_servers_outstanding(net); 616 617 afs_queue_server_manager(net); 618 619 _debug("wait"); 620 atomic_dec(&net->servers_outstanding); 621 wait_var_event(&net->servers_outstanding, 622 !atomic_read(&net->servers_outstanding)); 623 _leave(""); 624 } 625 626 /* 627 * Get an update for a server's address list. 628 */ 629 static noinline bool afs_update_server_record(struct afs_operation *op, 630 struct afs_server *server, 631 struct key *key) 632 { 633 struct afs_endpoint_state *estate; 634 struct afs_addr_list *alist; 635 bool has_addrs; 636 637 _enter(""); 638 639 trace_afs_server(server->debug_id, refcount_read(&server->ref), 640 atomic_read(&server->active), 641 afs_server_trace_update); 642 643 alist = afs_vl_lookup_addrs(op->volume->cell, op->key, &server->uuid); 644 if (IS_ERR(alist)) { 645 rcu_read_lock(); 646 estate = rcu_dereference(server->endpoint_state); 647 has_addrs = estate->addresses; 648 rcu_read_unlock(); 649 650 if ((PTR_ERR(alist) == -ERESTARTSYS || 651 PTR_ERR(alist) == -EINTR) && 652 (op->flags & AFS_OPERATION_UNINTR) && 653 has_addrs) { 654 _leave(" = t [intr]"); 655 return true; 656 } 657 afs_op_set_error(op, PTR_ERR(alist)); 658 _leave(" = f [%d]", afs_op_error(op)); 659 return false; 660 } 661 662 if (server->addr_version != alist->version) 663 afs_fs_probe_fileserver(op->net, server, alist, key); 664 665 afs_put_addrlist(alist, afs_alist_trace_put_server_update); 666 _leave(" = t"); 667 return true; 668 } 669 670 /* 671 * See if a server's address list needs updating. 672 */ 673 bool afs_check_server_record(struct afs_operation *op, struct afs_server *server, 674 struct key *key) 675 { 676 bool success; 677 int ret, retries = 0; 678 679 _enter(""); 680 681 ASSERT(server); 682 683 retry: 684 if (test_bit(AFS_SERVER_FL_UPDATING, &server->flags)) 685 goto wait; 686 if (test_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags)) 687 goto update; 688 _leave(" = t [good]"); 689 return true; 690 691 update: 692 if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) { 693 clear_bit(AFS_SERVER_FL_NEEDS_UPDATE, &server->flags); 694 success = afs_update_server_record(op, server, key); 695 clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags); 696 wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING); 697 _leave(" = %d", success); 698 return success; 699 } 700 701 wait: 702 ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING, 703 (op->flags & AFS_OPERATION_UNINTR) ? 704 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE); 705 if (ret == -ERESTARTSYS) { 706 afs_op_set_error(op, ret); 707 _leave(" = f [intr]"); 708 return false; 709 } 710 711 retries++; 712 if (retries == 4) { 713 _leave(" = f [stale]"); 714 ret = -ESTALE; 715 return false; 716 } 717 goto retry; 718 } 719