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