at v5.2-rc2 1898 lines 47 kB view raw
1/* 2 * net/sunrpc/cache.c 3 * 4 * Generic code for various authentication-related caches 5 * used by sunrpc clients and servers. 6 * 7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au> 8 * 9 * Released under terms in GPL version 2. See COPYING. 10 * 11 */ 12 13#include <linux/types.h> 14#include <linux/fs.h> 15#include <linux/file.h> 16#include <linux/slab.h> 17#include <linux/signal.h> 18#include <linux/sched.h> 19#include <linux/kmod.h> 20#include <linux/list.h> 21#include <linux/module.h> 22#include <linux/ctype.h> 23#include <linux/string_helpers.h> 24#include <linux/uaccess.h> 25#include <linux/poll.h> 26#include <linux/seq_file.h> 27#include <linux/proc_fs.h> 28#include <linux/net.h> 29#include <linux/workqueue.h> 30#include <linux/mutex.h> 31#include <linux/pagemap.h> 32#include <asm/ioctls.h> 33#include <linux/sunrpc/types.h> 34#include <linux/sunrpc/cache.h> 35#include <linux/sunrpc/stats.h> 36#include <linux/sunrpc/rpc_pipe_fs.h> 37#include "netns.h" 38 39#define RPCDBG_FACILITY RPCDBG_CACHE 40 41static bool cache_defer_req(struct cache_req *req, struct cache_head *item); 42static void cache_revisit_request(struct cache_head *item); 43static bool cache_listeners_exist(struct cache_detail *detail); 44 45static void cache_init(struct cache_head *h, struct cache_detail *detail) 46{ 47 time_t now = seconds_since_boot(); 48 INIT_HLIST_NODE(&h->cache_list); 49 h->flags = 0; 50 kref_init(&h->ref); 51 h->expiry_time = now + CACHE_NEW_EXPIRY; 52 if (now <= detail->flush_time) 53 /* ensure it isn't already expired */ 54 now = detail->flush_time + 1; 55 h->last_refresh = now; 56} 57 58static inline int cache_is_valid(struct cache_head *h); 59static void cache_fresh_locked(struct cache_head *head, time_t expiry, 60 struct cache_detail *detail); 61static void cache_fresh_unlocked(struct cache_head *head, 62 struct cache_detail *detail); 63 64static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail, 65 struct cache_head *key, 66 int hash) 67{ 68 struct hlist_head *head = &detail->hash_table[hash]; 69 struct cache_head *tmp; 70 71 rcu_read_lock(); 72 hlist_for_each_entry_rcu(tmp, head, cache_list) { 73 if (detail->match(tmp, key)) { 74 if (cache_is_expired(detail, tmp)) 75 continue; 76 tmp = cache_get_rcu(tmp); 77 rcu_read_unlock(); 78 return tmp; 79 } 80 } 81 rcu_read_unlock(); 82 return NULL; 83} 84 85static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail, 86 struct cache_head *key, 87 int hash) 88{ 89 struct cache_head *new, *tmp, *freeme = NULL; 90 struct hlist_head *head = &detail->hash_table[hash]; 91 92 new = detail->alloc(); 93 if (!new) 94 return NULL; 95 /* must fully initialise 'new', else 96 * we might get lose if we need to 97 * cache_put it soon. 98 */ 99 cache_init(new, detail); 100 detail->init(new, key); 101 102 spin_lock(&detail->hash_lock); 103 104 /* check if entry appeared while we slept */ 105 hlist_for_each_entry_rcu(tmp, head, cache_list) { 106 if (detail->match(tmp, key)) { 107 if (cache_is_expired(detail, tmp)) { 108 hlist_del_init_rcu(&tmp->cache_list); 109 detail->entries --; 110 if (cache_is_valid(tmp) == -EAGAIN) 111 set_bit(CACHE_NEGATIVE, &tmp->flags); 112 cache_fresh_locked(tmp, 0, detail); 113 freeme = tmp; 114 break; 115 } 116 cache_get(tmp); 117 spin_unlock(&detail->hash_lock); 118 cache_put(new, detail); 119 return tmp; 120 } 121 } 122 123 hlist_add_head_rcu(&new->cache_list, head); 124 detail->entries++; 125 cache_get(new); 126 spin_unlock(&detail->hash_lock); 127 128 if (freeme) { 129 cache_fresh_unlocked(freeme, detail); 130 cache_put(freeme, detail); 131 } 132 return new; 133} 134 135struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail, 136 struct cache_head *key, int hash) 137{ 138 struct cache_head *ret; 139 140 ret = sunrpc_cache_find_rcu(detail, key, hash); 141 if (ret) 142 return ret; 143 /* Didn't find anything, insert an empty entry */ 144 return sunrpc_cache_add_entry(detail, key, hash); 145} 146EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu); 147 148static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch); 149 150static void cache_fresh_locked(struct cache_head *head, time_t expiry, 151 struct cache_detail *detail) 152{ 153 time_t now = seconds_since_boot(); 154 if (now <= detail->flush_time) 155 /* ensure it isn't immediately treated as expired */ 156 now = detail->flush_time + 1; 157 head->expiry_time = expiry; 158 head->last_refresh = now; 159 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */ 160 set_bit(CACHE_VALID, &head->flags); 161} 162 163static void cache_fresh_unlocked(struct cache_head *head, 164 struct cache_detail *detail) 165{ 166 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) { 167 cache_revisit_request(head); 168 cache_dequeue(detail, head); 169 } 170} 171 172struct cache_head *sunrpc_cache_update(struct cache_detail *detail, 173 struct cache_head *new, struct cache_head *old, int hash) 174{ 175 /* The 'old' entry is to be replaced by 'new'. 176 * If 'old' is not VALID, we update it directly, 177 * otherwise we need to replace it 178 */ 179 struct cache_head *tmp; 180 181 if (!test_bit(CACHE_VALID, &old->flags)) { 182 spin_lock(&detail->hash_lock); 183 if (!test_bit(CACHE_VALID, &old->flags)) { 184 if (test_bit(CACHE_NEGATIVE, &new->flags)) 185 set_bit(CACHE_NEGATIVE, &old->flags); 186 else 187 detail->update(old, new); 188 cache_fresh_locked(old, new->expiry_time, detail); 189 spin_unlock(&detail->hash_lock); 190 cache_fresh_unlocked(old, detail); 191 return old; 192 } 193 spin_unlock(&detail->hash_lock); 194 } 195 /* We need to insert a new entry */ 196 tmp = detail->alloc(); 197 if (!tmp) { 198 cache_put(old, detail); 199 return NULL; 200 } 201 cache_init(tmp, detail); 202 detail->init(tmp, old); 203 204 spin_lock(&detail->hash_lock); 205 if (test_bit(CACHE_NEGATIVE, &new->flags)) 206 set_bit(CACHE_NEGATIVE, &tmp->flags); 207 else 208 detail->update(tmp, new); 209 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]); 210 detail->entries++; 211 cache_get(tmp); 212 cache_fresh_locked(tmp, new->expiry_time, detail); 213 cache_fresh_locked(old, 0, detail); 214 spin_unlock(&detail->hash_lock); 215 cache_fresh_unlocked(tmp, detail); 216 cache_fresh_unlocked(old, detail); 217 cache_put(old, detail); 218 return tmp; 219} 220EXPORT_SYMBOL_GPL(sunrpc_cache_update); 221 222static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h) 223{ 224 if (cd->cache_upcall) 225 return cd->cache_upcall(cd, h); 226 return sunrpc_cache_pipe_upcall(cd, h); 227} 228 229static inline int cache_is_valid(struct cache_head *h) 230{ 231 if (!test_bit(CACHE_VALID, &h->flags)) 232 return -EAGAIN; 233 else { 234 /* entry is valid */ 235 if (test_bit(CACHE_NEGATIVE, &h->flags)) 236 return -ENOENT; 237 else { 238 /* 239 * In combination with write barrier in 240 * sunrpc_cache_update, ensures that anyone 241 * using the cache entry after this sees the 242 * updated contents: 243 */ 244 smp_rmb(); 245 return 0; 246 } 247 } 248} 249 250static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h) 251{ 252 int rv; 253 254 spin_lock(&detail->hash_lock); 255 rv = cache_is_valid(h); 256 if (rv == -EAGAIN) { 257 set_bit(CACHE_NEGATIVE, &h->flags); 258 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY, 259 detail); 260 rv = -ENOENT; 261 } 262 spin_unlock(&detail->hash_lock); 263 cache_fresh_unlocked(h, detail); 264 return rv; 265} 266 267/* 268 * This is the generic cache management routine for all 269 * the authentication caches. 270 * It checks the currency of a cache item and will (later) 271 * initiate an upcall to fill it if needed. 272 * 273 * 274 * Returns 0 if the cache_head can be used, or cache_puts it and returns 275 * -EAGAIN if upcall is pending and request has been queued 276 * -ETIMEDOUT if upcall failed or request could not be queue or 277 * upcall completed but item is still invalid (implying that 278 * the cache item has been replaced with a newer one). 279 * -ENOENT if cache entry was negative 280 */ 281int cache_check(struct cache_detail *detail, 282 struct cache_head *h, struct cache_req *rqstp) 283{ 284 int rv; 285 long refresh_age, age; 286 287 /* First decide return status as best we can */ 288 rv = cache_is_valid(h); 289 290 /* now see if we want to start an upcall */ 291 refresh_age = (h->expiry_time - h->last_refresh); 292 age = seconds_since_boot() - h->last_refresh; 293 294 if (rqstp == NULL) { 295 if (rv == -EAGAIN) 296 rv = -ENOENT; 297 } else if (rv == -EAGAIN || 298 (h->expiry_time != 0 && age > refresh_age/2)) { 299 dprintk("RPC: Want update, refage=%ld, age=%ld\n", 300 refresh_age, age); 301 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) { 302 switch (cache_make_upcall(detail, h)) { 303 case -EINVAL: 304 rv = try_to_negate_entry(detail, h); 305 break; 306 case -EAGAIN: 307 cache_fresh_unlocked(h, detail); 308 break; 309 } 310 } else if (!cache_listeners_exist(detail)) 311 rv = try_to_negate_entry(detail, h); 312 } 313 314 if (rv == -EAGAIN) { 315 if (!cache_defer_req(rqstp, h)) { 316 /* 317 * Request was not deferred; handle it as best 318 * we can ourselves: 319 */ 320 rv = cache_is_valid(h); 321 if (rv == -EAGAIN) 322 rv = -ETIMEDOUT; 323 } 324 } 325 if (rv) 326 cache_put(h, detail); 327 return rv; 328} 329EXPORT_SYMBOL_GPL(cache_check); 330 331/* 332 * caches need to be periodically cleaned. 333 * For this we maintain a list of cache_detail and 334 * a current pointer into that list and into the table 335 * for that entry. 336 * 337 * Each time cache_clean is called it finds the next non-empty entry 338 * in the current table and walks the list in that entry 339 * looking for entries that can be removed. 340 * 341 * An entry gets removed if: 342 * - The expiry is before current time 343 * - The last_refresh time is before the flush_time for that cache 344 * 345 * later we might drop old entries with non-NEVER expiry if that table 346 * is getting 'full' for some definition of 'full' 347 * 348 * The question of "how often to scan a table" is an interesting one 349 * and is answered in part by the use of the "nextcheck" field in the 350 * cache_detail. 351 * When a scan of a table begins, the nextcheck field is set to a time 352 * that is well into the future. 353 * While scanning, if an expiry time is found that is earlier than the 354 * current nextcheck time, nextcheck is set to that expiry time. 355 * If the flush_time is ever set to a time earlier than the nextcheck 356 * time, the nextcheck time is then set to that flush_time. 357 * 358 * A table is then only scanned if the current time is at least 359 * the nextcheck time. 360 * 361 */ 362 363static LIST_HEAD(cache_list); 364static DEFINE_SPINLOCK(cache_list_lock); 365static struct cache_detail *current_detail; 366static int current_index; 367 368static void do_cache_clean(struct work_struct *work); 369static struct delayed_work cache_cleaner; 370 371void sunrpc_init_cache_detail(struct cache_detail *cd) 372{ 373 spin_lock_init(&cd->hash_lock); 374 INIT_LIST_HEAD(&cd->queue); 375 spin_lock(&cache_list_lock); 376 cd->nextcheck = 0; 377 cd->entries = 0; 378 atomic_set(&cd->readers, 0); 379 cd->last_close = 0; 380 cd->last_warn = -1; 381 list_add(&cd->others, &cache_list); 382 spin_unlock(&cache_list_lock); 383 384 /* start the cleaning process */ 385 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0); 386} 387EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail); 388 389void sunrpc_destroy_cache_detail(struct cache_detail *cd) 390{ 391 cache_purge(cd); 392 spin_lock(&cache_list_lock); 393 spin_lock(&cd->hash_lock); 394 if (current_detail == cd) 395 current_detail = NULL; 396 list_del_init(&cd->others); 397 spin_unlock(&cd->hash_lock); 398 spin_unlock(&cache_list_lock); 399 if (list_empty(&cache_list)) { 400 /* module must be being unloaded so its safe to kill the worker */ 401 cancel_delayed_work_sync(&cache_cleaner); 402 } 403} 404EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail); 405 406/* clean cache tries to find something to clean 407 * and cleans it. 408 * It returns 1 if it cleaned something, 409 * 0 if it didn't find anything this time 410 * -1 if it fell off the end of the list. 411 */ 412static int cache_clean(void) 413{ 414 int rv = 0; 415 struct list_head *next; 416 417 spin_lock(&cache_list_lock); 418 419 /* find a suitable table if we don't already have one */ 420 while (current_detail == NULL || 421 current_index >= current_detail->hash_size) { 422 if (current_detail) 423 next = current_detail->others.next; 424 else 425 next = cache_list.next; 426 if (next == &cache_list) { 427 current_detail = NULL; 428 spin_unlock(&cache_list_lock); 429 return -1; 430 } 431 current_detail = list_entry(next, struct cache_detail, others); 432 if (current_detail->nextcheck > seconds_since_boot()) 433 current_index = current_detail->hash_size; 434 else { 435 current_index = 0; 436 current_detail->nextcheck = seconds_since_boot()+30*60; 437 } 438 } 439 440 /* find a non-empty bucket in the table */ 441 while (current_detail && 442 current_index < current_detail->hash_size && 443 hlist_empty(&current_detail->hash_table[current_index])) 444 current_index++; 445 446 /* find a cleanable entry in the bucket and clean it, or set to next bucket */ 447 448 if (current_detail && current_index < current_detail->hash_size) { 449 struct cache_head *ch = NULL; 450 struct cache_detail *d; 451 struct hlist_head *head; 452 struct hlist_node *tmp; 453 454 spin_lock(&current_detail->hash_lock); 455 456 /* Ok, now to clean this strand */ 457 458 head = &current_detail->hash_table[current_index]; 459 hlist_for_each_entry_safe(ch, tmp, head, cache_list) { 460 if (current_detail->nextcheck > ch->expiry_time) 461 current_detail->nextcheck = ch->expiry_time+1; 462 if (!cache_is_expired(current_detail, ch)) 463 continue; 464 465 hlist_del_init_rcu(&ch->cache_list); 466 current_detail->entries--; 467 rv = 1; 468 break; 469 } 470 471 spin_unlock(&current_detail->hash_lock); 472 d = current_detail; 473 if (!ch) 474 current_index ++; 475 spin_unlock(&cache_list_lock); 476 if (ch) { 477 set_bit(CACHE_CLEANED, &ch->flags); 478 cache_fresh_unlocked(ch, d); 479 cache_put(ch, d); 480 } 481 } else 482 spin_unlock(&cache_list_lock); 483 484 return rv; 485} 486 487/* 488 * We want to regularly clean the cache, so we need to schedule some work ... 489 */ 490static void do_cache_clean(struct work_struct *work) 491{ 492 int delay = 5; 493 if (cache_clean() == -1) 494 delay = round_jiffies_relative(30*HZ); 495 496 if (list_empty(&cache_list)) 497 delay = 0; 498 499 if (delay) 500 queue_delayed_work(system_power_efficient_wq, 501 &cache_cleaner, delay); 502} 503 504 505/* 506 * Clean all caches promptly. This just calls cache_clean 507 * repeatedly until we are sure that every cache has had a chance to 508 * be fully cleaned 509 */ 510void cache_flush(void) 511{ 512 while (cache_clean() != -1) 513 cond_resched(); 514 while (cache_clean() != -1) 515 cond_resched(); 516} 517EXPORT_SYMBOL_GPL(cache_flush); 518 519void cache_purge(struct cache_detail *detail) 520{ 521 struct cache_head *ch = NULL; 522 struct hlist_head *head = NULL; 523 struct hlist_node *tmp = NULL; 524 int i = 0; 525 526 spin_lock(&detail->hash_lock); 527 if (!detail->entries) { 528 spin_unlock(&detail->hash_lock); 529 return; 530 } 531 532 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name); 533 for (i = 0; i < detail->hash_size; i++) { 534 head = &detail->hash_table[i]; 535 hlist_for_each_entry_safe(ch, tmp, head, cache_list) { 536 hlist_del_init_rcu(&ch->cache_list); 537 detail->entries--; 538 539 set_bit(CACHE_CLEANED, &ch->flags); 540 spin_unlock(&detail->hash_lock); 541 cache_fresh_unlocked(ch, detail); 542 cache_put(ch, detail); 543 spin_lock(&detail->hash_lock); 544 } 545 } 546 spin_unlock(&detail->hash_lock); 547} 548EXPORT_SYMBOL_GPL(cache_purge); 549 550 551/* 552 * Deferral and Revisiting of Requests. 553 * 554 * If a cache lookup finds a pending entry, we 555 * need to defer the request and revisit it later. 556 * All deferred requests are stored in a hash table, 557 * indexed by "struct cache_head *". 558 * As it may be wasteful to store a whole request 559 * structure, we allow the request to provide a 560 * deferred form, which must contain a 561 * 'struct cache_deferred_req' 562 * This cache_deferred_req contains a method to allow 563 * it to be revisited when cache info is available 564 */ 565 566#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head)) 567#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE) 568 569#define DFR_MAX 300 /* ??? */ 570 571static DEFINE_SPINLOCK(cache_defer_lock); 572static LIST_HEAD(cache_defer_list); 573static struct hlist_head cache_defer_hash[DFR_HASHSIZE]; 574static int cache_defer_cnt; 575 576static void __unhash_deferred_req(struct cache_deferred_req *dreq) 577{ 578 hlist_del_init(&dreq->hash); 579 if (!list_empty(&dreq->recent)) { 580 list_del_init(&dreq->recent); 581 cache_defer_cnt--; 582 } 583} 584 585static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item) 586{ 587 int hash = DFR_HASH(item); 588 589 INIT_LIST_HEAD(&dreq->recent); 590 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]); 591} 592 593static void setup_deferral(struct cache_deferred_req *dreq, 594 struct cache_head *item, 595 int count_me) 596{ 597 598 dreq->item = item; 599 600 spin_lock(&cache_defer_lock); 601 602 __hash_deferred_req(dreq, item); 603 604 if (count_me) { 605 cache_defer_cnt++; 606 list_add(&dreq->recent, &cache_defer_list); 607 } 608 609 spin_unlock(&cache_defer_lock); 610 611} 612 613struct thread_deferred_req { 614 struct cache_deferred_req handle; 615 struct completion completion; 616}; 617 618static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many) 619{ 620 struct thread_deferred_req *dr = 621 container_of(dreq, struct thread_deferred_req, handle); 622 complete(&dr->completion); 623} 624 625static void cache_wait_req(struct cache_req *req, struct cache_head *item) 626{ 627 struct thread_deferred_req sleeper; 628 struct cache_deferred_req *dreq = &sleeper.handle; 629 630 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion); 631 dreq->revisit = cache_restart_thread; 632 633 setup_deferral(dreq, item, 0); 634 635 if (!test_bit(CACHE_PENDING, &item->flags) || 636 wait_for_completion_interruptible_timeout( 637 &sleeper.completion, req->thread_wait) <= 0) { 638 /* The completion wasn't completed, so we need 639 * to clean up 640 */ 641 spin_lock(&cache_defer_lock); 642 if (!hlist_unhashed(&sleeper.handle.hash)) { 643 __unhash_deferred_req(&sleeper.handle); 644 spin_unlock(&cache_defer_lock); 645 } else { 646 /* cache_revisit_request already removed 647 * this from the hash table, but hasn't 648 * called ->revisit yet. It will very soon 649 * and we need to wait for it. 650 */ 651 spin_unlock(&cache_defer_lock); 652 wait_for_completion(&sleeper.completion); 653 } 654 } 655} 656 657static void cache_limit_defers(void) 658{ 659 /* Make sure we haven't exceed the limit of allowed deferred 660 * requests. 661 */ 662 struct cache_deferred_req *discard = NULL; 663 664 if (cache_defer_cnt <= DFR_MAX) 665 return; 666 667 spin_lock(&cache_defer_lock); 668 669 /* Consider removing either the first or the last */ 670 if (cache_defer_cnt > DFR_MAX) { 671 if (prandom_u32() & 1) 672 discard = list_entry(cache_defer_list.next, 673 struct cache_deferred_req, recent); 674 else 675 discard = list_entry(cache_defer_list.prev, 676 struct cache_deferred_req, recent); 677 __unhash_deferred_req(discard); 678 } 679 spin_unlock(&cache_defer_lock); 680 if (discard) 681 discard->revisit(discard, 1); 682} 683 684/* Return true if and only if a deferred request is queued. */ 685static bool cache_defer_req(struct cache_req *req, struct cache_head *item) 686{ 687 struct cache_deferred_req *dreq; 688 689 if (req->thread_wait) { 690 cache_wait_req(req, item); 691 if (!test_bit(CACHE_PENDING, &item->flags)) 692 return false; 693 } 694 dreq = req->defer(req); 695 if (dreq == NULL) 696 return false; 697 setup_deferral(dreq, item, 1); 698 if (!test_bit(CACHE_PENDING, &item->flags)) 699 /* Bit could have been cleared before we managed to 700 * set up the deferral, so need to revisit just in case 701 */ 702 cache_revisit_request(item); 703 704 cache_limit_defers(); 705 return true; 706} 707 708static void cache_revisit_request(struct cache_head *item) 709{ 710 struct cache_deferred_req *dreq; 711 struct list_head pending; 712 struct hlist_node *tmp; 713 int hash = DFR_HASH(item); 714 715 INIT_LIST_HEAD(&pending); 716 spin_lock(&cache_defer_lock); 717 718 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash) 719 if (dreq->item == item) { 720 __unhash_deferred_req(dreq); 721 list_add(&dreq->recent, &pending); 722 } 723 724 spin_unlock(&cache_defer_lock); 725 726 while (!list_empty(&pending)) { 727 dreq = list_entry(pending.next, struct cache_deferred_req, recent); 728 list_del_init(&dreq->recent); 729 dreq->revisit(dreq, 0); 730 } 731} 732 733void cache_clean_deferred(void *owner) 734{ 735 struct cache_deferred_req *dreq, *tmp; 736 struct list_head pending; 737 738 739 INIT_LIST_HEAD(&pending); 740 spin_lock(&cache_defer_lock); 741 742 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) { 743 if (dreq->owner == owner) { 744 __unhash_deferred_req(dreq); 745 list_add(&dreq->recent, &pending); 746 } 747 } 748 spin_unlock(&cache_defer_lock); 749 750 while (!list_empty(&pending)) { 751 dreq = list_entry(pending.next, struct cache_deferred_req, recent); 752 list_del_init(&dreq->recent); 753 dreq->revisit(dreq, 1); 754 } 755} 756 757/* 758 * communicate with user-space 759 * 760 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel. 761 * On read, you get a full request, or block. 762 * On write, an update request is processed. 763 * Poll works if anything to read, and always allows write. 764 * 765 * Implemented by linked list of requests. Each open file has 766 * a ->private that also exists in this list. New requests are added 767 * to the end and may wakeup and preceding readers. 768 * New readers are added to the head. If, on read, an item is found with 769 * CACHE_UPCALLING clear, we free it from the list. 770 * 771 */ 772 773static DEFINE_SPINLOCK(queue_lock); 774static DEFINE_MUTEX(queue_io_mutex); 775 776struct cache_queue { 777 struct list_head list; 778 int reader; /* if 0, then request */ 779}; 780struct cache_request { 781 struct cache_queue q; 782 struct cache_head *item; 783 char * buf; 784 int len; 785 int readers; 786}; 787struct cache_reader { 788 struct cache_queue q; 789 int offset; /* if non-0, we have a refcnt on next request */ 790}; 791 792static int cache_request(struct cache_detail *detail, 793 struct cache_request *crq) 794{ 795 char *bp = crq->buf; 796 int len = PAGE_SIZE; 797 798 detail->cache_request(detail, crq->item, &bp, &len); 799 if (len < 0) 800 return -EAGAIN; 801 return PAGE_SIZE - len; 802} 803 804static ssize_t cache_read(struct file *filp, char __user *buf, size_t count, 805 loff_t *ppos, struct cache_detail *cd) 806{ 807 struct cache_reader *rp = filp->private_data; 808 struct cache_request *rq; 809 struct inode *inode = file_inode(filp); 810 int err; 811 812 if (count == 0) 813 return 0; 814 815 inode_lock(inode); /* protect against multiple concurrent 816 * readers on this file */ 817 again: 818 spin_lock(&queue_lock); 819 /* need to find next request */ 820 while (rp->q.list.next != &cd->queue && 821 list_entry(rp->q.list.next, struct cache_queue, list) 822 ->reader) { 823 struct list_head *next = rp->q.list.next; 824 list_move(&rp->q.list, next); 825 } 826 if (rp->q.list.next == &cd->queue) { 827 spin_unlock(&queue_lock); 828 inode_unlock(inode); 829 WARN_ON_ONCE(rp->offset); 830 return 0; 831 } 832 rq = container_of(rp->q.list.next, struct cache_request, q.list); 833 WARN_ON_ONCE(rq->q.reader); 834 if (rp->offset == 0) 835 rq->readers++; 836 spin_unlock(&queue_lock); 837 838 if (rq->len == 0) { 839 err = cache_request(cd, rq); 840 if (err < 0) 841 goto out; 842 rq->len = err; 843 } 844 845 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { 846 err = -EAGAIN; 847 spin_lock(&queue_lock); 848 list_move(&rp->q.list, &rq->q.list); 849 spin_unlock(&queue_lock); 850 } else { 851 if (rp->offset + count > rq->len) 852 count = rq->len - rp->offset; 853 err = -EFAULT; 854 if (copy_to_user(buf, rq->buf + rp->offset, count)) 855 goto out; 856 rp->offset += count; 857 if (rp->offset >= rq->len) { 858 rp->offset = 0; 859 spin_lock(&queue_lock); 860 list_move(&rp->q.list, &rq->q.list); 861 spin_unlock(&queue_lock); 862 } 863 err = 0; 864 } 865 out: 866 if (rp->offset == 0) { 867 /* need to release rq */ 868 spin_lock(&queue_lock); 869 rq->readers--; 870 if (rq->readers == 0 && 871 !test_bit(CACHE_PENDING, &rq->item->flags)) { 872 list_del(&rq->q.list); 873 spin_unlock(&queue_lock); 874 cache_put(rq->item, cd); 875 kfree(rq->buf); 876 kfree(rq); 877 } else 878 spin_unlock(&queue_lock); 879 } 880 if (err == -EAGAIN) 881 goto again; 882 inode_unlock(inode); 883 return err ? err : count; 884} 885 886static ssize_t cache_do_downcall(char *kaddr, const char __user *buf, 887 size_t count, struct cache_detail *cd) 888{ 889 ssize_t ret; 890 891 if (count == 0) 892 return -EINVAL; 893 if (copy_from_user(kaddr, buf, count)) 894 return -EFAULT; 895 kaddr[count] = '\0'; 896 ret = cd->cache_parse(cd, kaddr, count); 897 if (!ret) 898 ret = count; 899 return ret; 900} 901 902static ssize_t cache_slow_downcall(const char __user *buf, 903 size_t count, struct cache_detail *cd) 904{ 905 static char write_buf[8192]; /* protected by queue_io_mutex */ 906 ssize_t ret = -EINVAL; 907 908 if (count >= sizeof(write_buf)) 909 goto out; 910 mutex_lock(&queue_io_mutex); 911 ret = cache_do_downcall(write_buf, buf, count, cd); 912 mutex_unlock(&queue_io_mutex); 913out: 914 return ret; 915} 916 917static ssize_t cache_downcall(struct address_space *mapping, 918 const char __user *buf, 919 size_t count, struct cache_detail *cd) 920{ 921 struct page *page; 922 char *kaddr; 923 ssize_t ret = -ENOMEM; 924 925 if (count >= PAGE_SIZE) 926 goto out_slow; 927 928 page = find_or_create_page(mapping, 0, GFP_KERNEL); 929 if (!page) 930 goto out_slow; 931 932 kaddr = kmap(page); 933 ret = cache_do_downcall(kaddr, buf, count, cd); 934 kunmap(page); 935 unlock_page(page); 936 put_page(page); 937 return ret; 938out_slow: 939 return cache_slow_downcall(buf, count, cd); 940} 941 942static ssize_t cache_write(struct file *filp, const char __user *buf, 943 size_t count, loff_t *ppos, 944 struct cache_detail *cd) 945{ 946 struct address_space *mapping = filp->f_mapping; 947 struct inode *inode = file_inode(filp); 948 ssize_t ret = -EINVAL; 949 950 if (!cd->cache_parse) 951 goto out; 952 953 inode_lock(inode); 954 ret = cache_downcall(mapping, buf, count, cd); 955 inode_unlock(inode); 956out: 957 return ret; 958} 959 960static DECLARE_WAIT_QUEUE_HEAD(queue_wait); 961 962static __poll_t cache_poll(struct file *filp, poll_table *wait, 963 struct cache_detail *cd) 964{ 965 __poll_t mask; 966 struct cache_reader *rp = filp->private_data; 967 struct cache_queue *cq; 968 969 poll_wait(filp, &queue_wait, wait); 970 971 /* alway allow write */ 972 mask = EPOLLOUT | EPOLLWRNORM; 973 974 if (!rp) 975 return mask; 976 977 spin_lock(&queue_lock); 978 979 for (cq= &rp->q; &cq->list != &cd->queue; 980 cq = list_entry(cq->list.next, struct cache_queue, list)) 981 if (!cq->reader) { 982 mask |= EPOLLIN | EPOLLRDNORM; 983 break; 984 } 985 spin_unlock(&queue_lock); 986 return mask; 987} 988 989static int cache_ioctl(struct inode *ino, struct file *filp, 990 unsigned int cmd, unsigned long arg, 991 struct cache_detail *cd) 992{ 993 int len = 0; 994 struct cache_reader *rp = filp->private_data; 995 struct cache_queue *cq; 996 997 if (cmd != FIONREAD || !rp) 998 return -EINVAL; 999 1000 spin_lock(&queue_lock); 1001 1002 /* only find the length remaining in current request, 1003 * or the length of the next request 1004 */ 1005 for (cq= &rp->q; &cq->list != &cd->queue; 1006 cq = list_entry(cq->list.next, struct cache_queue, list)) 1007 if (!cq->reader) { 1008 struct cache_request *cr = 1009 container_of(cq, struct cache_request, q); 1010 len = cr->len - rp->offset; 1011 break; 1012 } 1013 spin_unlock(&queue_lock); 1014 1015 return put_user(len, (int __user *)arg); 1016} 1017 1018static int cache_open(struct inode *inode, struct file *filp, 1019 struct cache_detail *cd) 1020{ 1021 struct cache_reader *rp = NULL; 1022 1023 if (!cd || !try_module_get(cd->owner)) 1024 return -EACCES; 1025 nonseekable_open(inode, filp); 1026 if (filp->f_mode & FMODE_READ) { 1027 rp = kmalloc(sizeof(*rp), GFP_KERNEL); 1028 if (!rp) { 1029 module_put(cd->owner); 1030 return -ENOMEM; 1031 } 1032 rp->offset = 0; 1033 rp->q.reader = 1; 1034 atomic_inc(&cd->readers); 1035 spin_lock(&queue_lock); 1036 list_add(&rp->q.list, &cd->queue); 1037 spin_unlock(&queue_lock); 1038 } 1039 filp->private_data = rp; 1040 return 0; 1041} 1042 1043static int cache_release(struct inode *inode, struct file *filp, 1044 struct cache_detail *cd) 1045{ 1046 struct cache_reader *rp = filp->private_data; 1047 1048 if (rp) { 1049 spin_lock(&queue_lock); 1050 if (rp->offset) { 1051 struct cache_queue *cq; 1052 for (cq= &rp->q; &cq->list != &cd->queue; 1053 cq = list_entry(cq->list.next, struct cache_queue, list)) 1054 if (!cq->reader) { 1055 container_of(cq, struct cache_request, q) 1056 ->readers--; 1057 break; 1058 } 1059 rp->offset = 0; 1060 } 1061 list_del(&rp->q.list); 1062 spin_unlock(&queue_lock); 1063 1064 filp->private_data = NULL; 1065 kfree(rp); 1066 1067 cd->last_close = seconds_since_boot(); 1068 atomic_dec(&cd->readers); 1069 } 1070 module_put(cd->owner); 1071 return 0; 1072} 1073 1074 1075 1076static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch) 1077{ 1078 struct cache_queue *cq, *tmp; 1079 struct cache_request *cr; 1080 struct list_head dequeued; 1081 1082 INIT_LIST_HEAD(&dequeued); 1083 spin_lock(&queue_lock); 1084 list_for_each_entry_safe(cq, tmp, &detail->queue, list) 1085 if (!cq->reader) { 1086 cr = container_of(cq, struct cache_request, q); 1087 if (cr->item != ch) 1088 continue; 1089 if (test_bit(CACHE_PENDING, &ch->flags)) 1090 /* Lost a race and it is pending again */ 1091 break; 1092 if (cr->readers != 0) 1093 continue; 1094 list_move(&cr->q.list, &dequeued); 1095 } 1096 spin_unlock(&queue_lock); 1097 while (!list_empty(&dequeued)) { 1098 cr = list_entry(dequeued.next, struct cache_request, q.list); 1099 list_del(&cr->q.list); 1100 cache_put(cr->item, detail); 1101 kfree(cr->buf); 1102 kfree(cr); 1103 } 1104} 1105 1106/* 1107 * Support routines for text-based upcalls. 1108 * Fields are separated by spaces. 1109 * Fields are either mangled to quote space tab newline slosh with slosh 1110 * or a hexified with a leading \x 1111 * Record is terminated with newline. 1112 * 1113 */ 1114 1115void qword_add(char **bpp, int *lp, char *str) 1116{ 1117 char *bp = *bpp; 1118 int len = *lp; 1119 int ret; 1120 1121 if (len < 0) return; 1122 1123 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t"); 1124 if (ret >= len) { 1125 bp += len; 1126 len = -1; 1127 } else { 1128 bp += ret; 1129 len -= ret; 1130 *bp++ = ' '; 1131 len--; 1132 } 1133 *bpp = bp; 1134 *lp = len; 1135} 1136EXPORT_SYMBOL_GPL(qword_add); 1137 1138void qword_addhex(char **bpp, int *lp, char *buf, int blen) 1139{ 1140 char *bp = *bpp; 1141 int len = *lp; 1142 1143 if (len < 0) return; 1144 1145 if (len > 2) { 1146 *bp++ = '\\'; 1147 *bp++ = 'x'; 1148 len -= 2; 1149 while (blen && len >= 2) { 1150 bp = hex_byte_pack(bp, *buf++); 1151 len -= 2; 1152 blen--; 1153 } 1154 } 1155 if (blen || len<1) len = -1; 1156 else { 1157 *bp++ = ' '; 1158 len--; 1159 } 1160 *bpp = bp; 1161 *lp = len; 1162} 1163EXPORT_SYMBOL_GPL(qword_addhex); 1164 1165static void warn_no_listener(struct cache_detail *detail) 1166{ 1167 if (detail->last_warn != detail->last_close) { 1168 detail->last_warn = detail->last_close; 1169 if (detail->warn_no_listener) 1170 detail->warn_no_listener(detail, detail->last_close != 0); 1171 } 1172} 1173 1174static bool cache_listeners_exist(struct cache_detail *detail) 1175{ 1176 if (atomic_read(&detail->readers)) 1177 return true; 1178 if (detail->last_close == 0) 1179 /* This cache was never opened */ 1180 return false; 1181 if (detail->last_close < seconds_since_boot() - 30) 1182 /* 1183 * We allow for the possibility that someone might 1184 * restart a userspace daemon without restarting the 1185 * server; but after 30 seconds, we give up. 1186 */ 1187 return false; 1188 return true; 1189} 1190 1191/* 1192 * register an upcall request to user-space and queue it up for read() by the 1193 * upcall daemon. 1194 * 1195 * Each request is at most one page long. 1196 */ 1197int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h) 1198{ 1199 1200 char *buf; 1201 struct cache_request *crq; 1202 int ret = 0; 1203 1204 if (!detail->cache_request) 1205 return -EINVAL; 1206 1207 if (!cache_listeners_exist(detail)) { 1208 warn_no_listener(detail); 1209 return -EINVAL; 1210 } 1211 if (test_bit(CACHE_CLEANED, &h->flags)) 1212 /* Too late to make an upcall */ 1213 return -EAGAIN; 1214 1215 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1216 if (!buf) 1217 return -EAGAIN; 1218 1219 crq = kmalloc(sizeof (*crq), GFP_KERNEL); 1220 if (!crq) { 1221 kfree(buf); 1222 return -EAGAIN; 1223 } 1224 1225 crq->q.reader = 0; 1226 crq->buf = buf; 1227 crq->len = 0; 1228 crq->readers = 0; 1229 spin_lock(&queue_lock); 1230 if (test_bit(CACHE_PENDING, &h->flags)) { 1231 crq->item = cache_get(h); 1232 list_add_tail(&crq->q.list, &detail->queue); 1233 } else 1234 /* Lost a race, no longer PENDING, so don't enqueue */ 1235 ret = -EAGAIN; 1236 spin_unlock(&queue_lock); 1237 wake_up(&queue_wait); 1238 if (ret == -EAGAIN) { 1239 kfree(buf); 1240 kfree(crq); 1241 } 1242 return ret; 1243} 1244EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); 1245 1246/* 1247 * parse a message from user-space and pass it 1248 * to an appropriate cache 1249 * Messages are, like requests, separated into fields by 1250 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal 1251 * 1252 * Message is 1253 * reply cachename expiry key ... content.... 1254 * 1255 * key and content are both parsed by cache 1256 */ 1257 1258int qword_get(char **bpp, char *dest, int bufsize) 1259{ 1260 /* return bytes copied, or -1 on error */ 1261 char *bp = *bpp; 1262 int len = 0; 1263 1264 while (*bp == ' ') bp++; 1265 1266 if (bp[0] == '\\' && bp[1] == 'x') { 1267 /* HEX STRING */ 1268 bp += 2; 1269 while (len < bufsize - 1) { 1270 int h, l; 1271 1272 h = hex_to_bin(bp[0]); 1273 if (h < 0) 1274 break; 1275 1276 l = hex_to_bin(bp[1]); 1277 if (l < 0) 1278 break; 1279 1280 *dest++ = (h << 4) | l; 1281 bp += 2; 1282 len++; 1283 } 1284 } else { 1285 /* text with \nnn octal quoting */ 1286 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) { 1287 if (*bp == '\\' && 1288 isodigit(bp[1]) && (bp[1] <= '3') && 1289 isodigit(bp[2]) && 1290 isodigit(bp[3])) { 1291 int byte = (*++bp -'0'); 1292 bp++; 1293 byte = (byte << 3) | (*bp++ - '0'); 1294 byte = (byte << 3) | (*bp++ - '0'); 1295 *dest++ = byte; 1296 len++; 1297 } else { 1298 *dest++ = *bp++; 1299 len++; 1300 } 1301 } 1302 } 1303 1304 if (*bp != ' ' && *bp != '\n' && *bp != '\0') 1305 return -1; 1306 while (*bp == ' ') bp++; 1307 *bpp = bp; 1308 *dest = '\0'; 1309 return len; 1310} 1311EXPORT_SYMBOL_GPL(qword_get); 1312 1313 1314/* 1315 * support /proc/net/rpc/$CACHENAME/content 1316 * as a seqfile. 1317 * We call ->cache_show passing NULL for the item to 1318 * get a header, then pass each real item in the cache 1319 */ 1320 1321static void *__cache_seq_start(struct seq_file *m, loff_t *pos) 1322{ 1323 loff_t n = *pos; 1324 unsigned int hash, entry; 1325 struct cache_head *ch; 1326 struct cache_detail *cd = m->private; 1327 1328 if (!n--) 1329 return SEQ_START_TOKEN; 1330 hash = n >> 32; 1331 entry = n & ((1LL<<32) - 1); 1332 1333 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list) 1334 if (!entry--) 1335 return ch; 1336 n &= ~((1LL<<32) - 1); 1337 do { 1338 hash++; 1339 n += 1LL<<32; 1340 } while(hash < cd->hash_size && 1341 hlist_empty(&cd->hash_table[hash])); 1342 if (hash >= cd->hash_size) 1343 return NULL; 1344 *pos = n+1; 1345 return hlist_entry_safe(rcu_dereference_raw( 1346 hlist_first_rcu(&cd->hash_table[hash])), 1347 struct cache_head, cache_list); 1348} 1349 1350static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos) 1351{ 1352 struct cache_head *ch = p; 1353 int hash = (*pos >> 32); 1354 struct cache_detail *cd = m->private; 1355 1356 if (p == SEQ_START_TOKEN) 1357 hash = 0; 1358 else if (ch->cache_list.next == NULL) { 1359 hash++; 1360 *pos += 1LL<<32; 1361 } else { 1362 ++*pos; 1363 return hlist_entry_safe(rcu_dereference_raw( 1364 hlist_next_rcu(&ch->cache_list)), 1365 struct cache_head, cache_list); 1366 } 1367 *pos &= ~((1LL<<32) - 1); 1368 while (hash < cd->hash_size && 1369 hlist_empty(&cd->hash_table[hash])) { 1370 hash++; 1371 *pos += 1LL<<32; 1372 } 1373 if (hash >= cd->hash_size) 1374 return NULL; 1375 ++*pos; 1376 return hlist_entry_safe(rcu_dereference_raw( 1377 hlist_first_rcu(&cd->hash_table[hash])), 1378 struct cache_head, cache_list); 1379} 1380EXPORT_SYMBOL_GPL(cache_seq_next); 1381 1382void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos) 1383 __acquires(RCU) 1384{ 1385 rcu_read_lock(); 1386 return __cache_seq_start(m, pos); 1387} 1388EXPORT_SYMBOL_GPL(cache_seq_start_rcu); 1389 1390void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos) 1391{ 1392 return cache_seq_next(file, p, pos); 1393} 1394EXPORT_SYMBOL_GPL(cache_seq_next_rcu); 1395 1396void cache_seq_stop_rcu(struct seq_file *m, void *p) 1397 __releases(RCU) 1398{ 1399 rcu_read_unlock(); 1400} 1401EXPORT_SYMBOL_GPL(cache_seq_stop_rcu); 1402 1403static int c_show(struct seq_file *m, void *p) 1404{ 1405 struct cache_head *cp = p; 1406 struct cache_detail *cd = m->private; 1407 1408 if (p == SEQ_START_TOKEN) 1409 return cd->cache_show(m, cd, NULL); 1410 1411 ifdebug(CACHE) 1412 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", 1413 convert_to_wallclock(cp->expiry_time), 1414 kref_read(&cp->ref), cp->flags); 1415 cache_get(cp); 1416 if (cache_check(cd, cp, NULL)) 1417 /* cache_check does a cache_put on failure */ 1418 seq_printf(m, "# "); 1419 else { 1420 if (cache_is_expired(cd, cp)) 1421 seq_printf(m, "# "); 1422 cache_put(cp, cd); 1423 } 1424 1425 return cd->cache_show(m, cd, cp); 1426} 1427 1428static const struct seq_operations cache_content_op = { 1429 .start = cache_seq_start_rcu, 1430 .next = cache_seq_next_rcu, 1431 .stop = cache_seq_stop_rcu, 1432 .show = c_show, 1433}; 1434 1435static int content_open(struct inode *inode, struct file *file, 1436 struct cache_detail *cd) 1437{ 1438 struct seq_file *seq; 1439 int err; 1440 1441 if (!cd || !try_module_get(cd->owner)) 1442 return -EACCES; 1443 1444 err = seq_open(file, &cache_content_op); 1445 if (err) { 1446 module_put(cd->owner); 1447 return err; 1448 } 1449 1450 seq = file->private_data; 1451 seq->private = cd; 1452 return 0; 1453} 1454 1455static int content_release(struct inode *inode, struct file *file, 1456 struct cache_detail *cd) 1457{ 1458 int ret = seq_release(inode, file); 1459 module_put(cd->owner); 1460 return ret; 1461} 1462 1463static int open_flush(struct inode *inode, struct file *file, 1464 struct cache_detail *cd) 1465{ 1466 if (!cd || !try_module_get(cd->owner)) 1467 return -EACCES; 1468 return nonseekable_open(inode, file); 1469} 1470 1471static int release_flush(struct inode *inode, struct file *file, 1472 struct cache_detail *cd) 1473{ 1474 module_put(cd->owner); 1475 return 0; 1476} 1477 1478static ssize_t read_flush(struct file *file, char __user *buf, 1479 size_t count, loff_t *ppos, 1480 struct cache_detail *cd) 1481{ 1482 char tbuf[22]; 1483 size_t len; 1484 1485 len = snprintf(tbuf, sizeof(tbuf), "%lu\n", 1486 convert_to_wallclock(cd->flush_time)); 1487 return simple_read_from_buffer(buf, count, ppos, tbuf, len); 1488} 1489 1490static ssize_t write_flush(struct file *file, const char __user *buf, 1491 size_t count, loff_t *ppos, 1492 struct cache_detail *cd) 1493{ 1494 char tbuf[20]; 1495 char *ep; 1496 time_t now; 1497 1498 if (*ppos || count > sizeof(tbuf)-1) 1499 return -EINVAL; 1500 if (copy_from_user(tbuf, buf, count)) 1501 return -EFAULT; 1502 tbuf[count] = 0; 1503 simple_strtoul(tbuf, &ep, 0); 1504 if (*ep && *ep != '\n') 1505 return -EINVAL; 1506 /* Note that while we check that 'buf' holds a valid number, 1507 * we always ignore the value and just flush everything. 1508 * Making use of the number leads to races. 1509 */ 1510 1511 now = seconds_since_boot(); 1512 /* Always flush everything, so behave like cache_purge() 1513 * Do this by advancing flush_time to the current time, 1514 * or by one second if it has already reached the current time. 1515 * Newly added cache entries will always have ->last_refresh greater 1516 * that ->flush_time, so they don't get flushed prematurely. 1517 */ 1518 1519 if (cd->flush_time >= now) 1520 now = cd->flush_time + 1; 1521 1522 cd->flush_time = now; 1523 cd->nextcheck = now; 1524 cache_flush(); 1525 1526 *ppos += count; 1527 return count; 1528} 1529 1530static ssize_t cache_read_procfs(struct file *filp, char __user *buf, 1531 size_t count, loff_t *ppos) 1532{ 1533 struct cache_detail *cd = PDE_DATA(file_inode(filp)); 1534 1535 return cache_read(filp, buf, count, ppos, cd); 1536} 1537 1538static ssize_t cache_write_procfs(struct file *filp, const char __user *buf, 1539 size_t count, loff_t *ppos) 1540{ 1541 struct cache_detail *cd = PDE_DATA(file_inode(filp)); 1542 1543 return cache_write(filp, buf, count, ppos, cd); 1544} 1545 1546static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait) 1547{ 1548 struct cache_detail *cd = PDE_DATA(file_inode(filp)); 1549 1550 return cache_poll(filp, wait, cd); 1551} 1552 1553static long cache_ioctl_procfs(struct file *filp, 1554 unsigned int cmd, unsigned long arg) 1555{ 1556 struct inode *inode = file_inode(filp); 1557 struct cache_detail *cd = PDE_DATA(inode); 1558 1559 return cache_ioctl(inode, filp, cmd, arg, cd); 1560} 1561 1562static int cache_open_procfs(struct inode *inode, struct file *filp) 1563{ 1564 struct cache_detail *cd = PDE_DATA(inode); 1565 1566 return cache_open(inode, filp, cd); 1567} 1568 1569static int cache_release_procfs(struct inode *inode, struct file *filp) 1570{ 1571 struct cache_detail *cd = PDE_DATA(inode); 1572 1573 return cache_release(inode, filp, cd); 1574} 1575 1576static const struct file_operations cache_file_operations_procfs = { 1577 .owner = THIS_MODULE, 1578 .llseek = no_llseek, 1579 .read = cache_read_procfs, 1580 .write = cache_write_procfs, 1581 .poll = cache_poll_procfs, 1582 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */ 1583 .open = cache_open_procfs, 1584 .release = cache_release_procfs, 1585}; 1586 1587static int content_open_procfs(struct inode *inode, struct file *filp) 1588{ 1589 struct cache_detail *cd = PDE_DATA(inode); 1590 1591 return content_open(inode, filp, cd); 1592} 1593 1594static int content_release_procfs(struct inode *inode, struct file *filp) 1595{ 1596 struct cache_detail *cd = PDE_DATA(inode); 1597 1598 return content_release(inode, filp, cd); 1599} 1600 1601static const struct file_operations content_file_operations_procfs = { 1602 .open = content_open_procfs, 1603 .read = seq_read, 1604 .llseek = seq_lseek, 1605 .release = content_release_procfs, 1606}; 1607 1608static int open_flush_procfs(struct inode *inode, struct file *filp) 1609{ 1610 struct cache_detail *cd = PDE_DATA(inode); 1611 1612 return open_flush(inode, filp, cd); 1613} 1614 1615static int release_flush_procfs(struct inode *inode, struct file *filp) 1616{ 1617 struct cache_detail *cd = PDE_DATA(inode); 1618 1619 return release_flush(inode, filp, cd); 1620} 1621 1622static ssize_t read_flush_procfs(struct file *filp, char __user *buf, 1623 size_t count, loff_t *ppos) 1624{ 1625 struct cache_detail *cd = PDE_DATA(file_inode(filp)); 1626 1627 return read_flush(filp, buf, count, ppos, cd); 1628} 1629 1630static ssize_t write_flush_procfs(struct file *filp, 1631 const char __user *buf, 1632 size_t count, loff_t *ppos) 1633{ 1634 struct cache_detail *cd = PDE_DATA(file_inode(filp)); 1635 1636 return write_flush(filp, buf, count, ppos, cd); 1637} 1638 1639static const struct file_operations cache_flush_operations_procfs = { 1640 .open = open_flush_procfs, 1641 .read = read_flush_procfs, 1642 .write = write_flush_procfs, 1643 .release = release_flush_procfs, 1644 .llseek = no_llseek, 1645}; 1646 1647static void remove_cache_proc_entries(struct cache_detail *cd) 1648{ 1649 if (cd->procfs) { 1650 proc_remove(cd->procfs); 1651 cd->procfs = NULL; 1652 } 1653} 1654 1655#ifdef CONFIG_PROC_FS 1656static int create_cache_proc_entries(struct cache_detail *cd, struct net *net) 1657{ 1658 struct proc_dir_entry *p; 1659 struct sunrpc_net *sn; 1660 1661 sn = net_generic(net, sunrpc_net_id); 1662 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc); 1663 if (cd->procfs == NULL) 1664 goto out_nomem; 1665 1666 p = proc_create_data("flush", S_IFREG | 0600, 1667 cd->procfs, &cache_flush_operations_procfs, cd); 1668 if (p == NULL) 1669 goto out_nomem; 1670 1671 if (cd->cache_request || cd->cache_parse) { 1672 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs, 1673 &cache_file_operations_procfs, cd); 1674 if (p == NULL) 1675 goto out_nomem; 1676 } 1677 if (cd->cache_show) { 1678 p = proc_create_data("content", S_IFREG | 0400, cd->procfs, 1679 &content_file_operations_procfs, cd); 1680 if (p == NULL) 1681 goto out_nomem; 1682 } 1683 return 0; 1684out_nomem: 1685 remove_cache_proc_entries(cd); 1686 return -ENOMEM; 1687} 1688#else /* CONFIG_PROC_FS */ 1689static int create_cache_proc_entries(struct cache_detail *cd, struct net *net) 1690{ 1691 return 0; 1692} 1693#endif 1694 1695void __init cache_initialize(void) 1696{ 1697 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean); 1698} 1699 1700int cache_register_net(struct cache_detail *cd, struct net *net) 1701{ 1702 int ret; 1703 1704 sunrpc_init_cache_detail(cd); 1705 ret = create_cache_proc_entries(cd, net); 1706 if (ret) 1707 sunrpc_destroy_cache_detail(cd); 1708 return ret; 1709} 1710EXPORT_SYMBOL_GPL(cache_register_net); 1711 1712void cache_unregister_net(struct cache_detail *cd, struct net *net) 1713{ 1714 remove_cache_proc_entries(cd); 1715 sunrpc_destroy_cache_detail(cd); 1716} 1717EXPORT_SYMBOL_GPL(cache_unregister_net); 1718 1719struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net) 1720{ 1721 struct cache_detail *cd; 1722 int i; 1723 1724 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL); 1725 if (cd == NULL) 1726 return ERR_PTR(-ENOMEM); 1727 1728 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head), 1729 GFP_KERNEL); 1730 if (cd->hash_table == NULL) { 1731 kfree(cd); 1732 return ERR_PTR(-ENOMEM); 1733 } 1734 1735 for (i = 0; i < cd->hash_size; i++) 1736 INIT_HLIST_HEAD(&cd->hash_table[i]); 1737 cd->net = net; 1738 return cd; 1739} 1740EXPORT_SYMBOL_GPL(cache_create_net); 1741 1742void cache_destroy_net(struct cache_detail *cd, struct net *net) 1743{ 1744 kfree(cd->hash_table); 1745 kfree(cd); 1746} 1747EXPORT_SYMBOL_GPL(cache_destroy_net); 1748 1749static ssize_t cache_read_pipefs(struct file *filp, char __user *buf, 1750 size_t count, loff_t *ppos) 1751{ 1752 struct cache_detail *cd = RPC_I(file_inode(filp))->private; 1753 1754 return cache_read(filp, buf, count, ppos, cd); 1755} 1756 1757static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf, 1758 size_t count, loff_t *ppos) 1759{ 1760 struct cache_detail *cd = RPC_I(file_inode(filp))->private; 1761 1762 return cache_write(filp, buf, count, ppos, cd); 1763} 1764 1765static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait) 1766{ 1767 struct cache_detail *cd = RPC_I(file_inode(filp))->private; 1768 1769 return cache_poll(filp, wait, cd); 1770} 1771 1772static long cache_ioctl_pipefs(struct file *filp, 1773 unsigned int cmd, unsigned long arg) 1774{ 1775 struct inode *inode = file_inode(filp); 1776 struct cache_detail *cd = RPC_I(inode)->private; 1777 1778 return cache_ioctl(inode, filp, cmd, arg, cd); 1779} 1780 1781static int cache_open_pipefs(struct inode *inode, struct file *filp) 1782{ 1783 struct cache_detail *cd = RPC_I(inode)->private; 1784 1785 return cache_open(inode, filp, cd); 1786} 1787 1788static int cache_release_pipefs(struct inode *inode, struct file *filp) 1789{ 1790 struct cache_detail *cd = RPC_I(inode)->private; 1791 1792 return cache_release(inode, filp, cd); 1793} 1794 1795const struct file_operations cache_file_operations_pipefs = { 1796 .owner = THIS_MODULE, 1797 .llseek = no_llseek, 1798 .read = cache_read_pipefs, 1799 .write = cache_write_pipefs, 1800 .poll = cache_poll_pipefs, 1801 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */ 1802 .open = cache_open_pipefs, 1803 .release = cache_release_pipefs, 1804}; 1805 1806static int content_open_pipefs(struct inode *inode, struct file *filp) 1807{ 1808 struct cache_detail *cd = RPC_I(inode)->private; 1809 1810 return content_open(inode, filp, cd); 1811} 1812 1813static int content_release_pipefs(struct inode *inode, struct file *filp) 1814{ 1815 struct cache_detail *cd = RPC_I(inode)->private; 1816 1817 return content_release(inode, filp, cd); 1818} 1819 1820const struct file_operations content_file_operations_pipefs = { 1821 .open = content_open_pipefs, 1822 .read = seq_read, 1823 .llseek = seq_lseek, 1824 .release = content_release_pipefs, 1825}; 1826 1827static int open_flush_pipefs(struct inode *inode, struct file *filp) 1828{ 1829 struct cache_detail *cd = RPC_I(inode)->private; 1830 1831 return open_flush(inode, filp, cd); 1832} 1833 1834static int release_flush_pipefs(struct inode *inode, struct file *filp) 1835{ 1836 struct cache_detail *cd = RPC_I(inode)->private; 1837 1838 return release_flush(inode, filp, cd); 1839} 1840 1841static ssize_t read_flush_pipefs(struct file *filp, char __user *buf, 1842 size_t count, loff_t *ppos) 1843{ 1844 struct cache_detail *cd = RPC_I(file_inode(filp))->private; 1845 1846 return read_flush(filp, buf, count, ppos, cd); 1847} 1848 1849static ssize_t write_flush_pipefs(struct file *filp, 1850 const char __user *buf, 1851 size_t count, loff_t *ppos) 1852{ 1853 struct cache_detail *cd = RPC_I(file_inode(filp))->private; 1854 1855 return write_flush(filp, buf, count, ppos, cd); 1856} 1857 1858const struct file_operations cache_flush_operations_pipefs = { 1859 .open = open_flush_pipefs, 1860 .read = read_flush_pipefs, 1861 .write = write_flush_pipefs, 1862 .release = release_flush_pipefs, 1863 .llseek = no_llseek, 1864}; 1865 1866int sunrpc_cache_register_pipefs(struct dentry *parent, 1867 const char *name, umode_t umode, 1868 struct cache_detail *cd) 1869{ 1870 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd); 1871 if (IS_ERR(dir)) 1872 return PTR_ERR(dir); 1873 cd->pipefs = dir; 1874 return 0; 1875} 1876EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs); 1877 1878void sunrpc_cache_unregister_pipefs(struct cache_detail *cd) 1879{ 1880 if (cd->pipefs) { 1881 rpc_remove_cache_dir(cd->pipefs); 1882 cd->pipefs = NULL; 1883 } 1884} 1885EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs); 1886 1887void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h) 1888{ 1889 spin_lock(&cd->hash_lock); 1890 if (!hlist_unhashed(&h->cache_list)){ 1891 hlist_del_init_rcu(&h->cache_list); 1892 cd->entries--; 1893 spin_unlock(&cd->hash_lock); 1894 cache_put(h, cd); 1895 } else 1896 spin_unlock(&cd->hash_lock); 1897} 1898EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);