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 <asm/ioctls.h> 31 #include <linux/sunrpc/types.h> 32 #include <linux/sunrpc/cache.h> 33 #include <linux/sunrpc/stats.h> 34 35 #define RPCDBG_FACILITY RPCDBG_CACHE 36 37 static int cache_defer_req(struct cache_req *req, struct cache_head *item); 38 static void cache_revisit_request(struct cache_head *item); 39 40 static void cache_init(struct cache_head *h) 41 { 42 time_t now = get_seconds(); 43 h->next = NULL; 44 h->flags = 0; 45 kref_init(&h->ref); 46 h->expiry_time = now + CACHE_NEW_EXPIRY; 47 h->last_refresh = now; 48 } 49 50 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail, 51 struct cache_head *key, int hash) 52 { 53 struct cache_head **head, **hp; 54 struct cache_head *new = NULL; 55 56 head = &detail->hash_table[hash]; 57 58 read_lock(&detail->hash_lock); 59 60 for (hp=head; *hp != NULL ; hp = &(*hp)->next) { 61 struct cache_head *tmp = *hp; 62 if (detail->match(tmp, key)) { 63 cache_get(tmp); 64 read_unlock(&detail->hash_lock); 65 return tmp; 66 } 67 } 68 read_unlock(&detail->hash_lock); 69 /* Didn't find anything, insert an empty entry */ 70 71 new = detail->alloc(); 72 if (!new) 73 return NULL; 74 /* must fully initialise 'new', else 75 * we might get lose if we need to 76 * cache_put it soon. 77 */ 78 cache_init(new); 79 detail->init(new, key); 80 81 write_lock(&detail->hash_lock); 82 83 /* check if entry appeared while we slept */ 84 for (hp=head; *hp != NULL ; hp = &(*hp)->next) { 85 struct cache_head *tmp = *hp; 86 if (detail->match(tmp, key)) { 87 cache_get(tmp); 88 write_unlock(&detail->hash_lock); 89 cache_put(new, detail); 90 return tmp; 91 } 92 } 93 new->next = *head; 94 *head = new; 95 detail->entries++; 96 cache_get(new); 97 write_unlock(&detail->hash_lock); 98 99 return new; 100 } 101 EXPORT_SYMBOL(sunrpc_cache_lookup); 102 103 104 static void queue_loose(struct cache_detail *detail, struct cache_head *ch); 105 106 static int cache_fresh_locked(struct cache_head *head, time_t expiry) 107 { 108 head->expiry_time = expiry; 109 head->last_refresh = get_seconds(); 110 return !test_and_set_bit(CACHE_VALID, &head->flags); 111 } 112 113 static void cache_fresh_unlocked(struct cache_head *head, 114 struct cache_detail *detail, int new) 115 { 116 if (new) 117 cache_revisit_request(head); 118 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) { 119 cache_revisit_request(head); 120 queue_loose(detail, head); 121 } 122 } 123 124 struct cache_head *sunrpc_cache_update(struct cache_detail *detail, 125 struct cache_head *new, struct cache_head *old, int hash) 126 { 127 /* The 'old' entry is to be replaced by 'new'. 128 * If 'old' is not VALID, we update it directly, 129 * otherwise we need to replace it 130 */ 131 struct cache_head **head; 132 struct cache_head *tmp; 133 int is_new; 134 135 if (!test_bit(CACHE_VALID, &old->flags)) { 136 write_lock(&detail->hash_lock); 137 if (!test_bit(CACHE_VALID, &old->flags)) { 138 if (test_bit(CACHE_NEGATIVE, &new->flags)) 139 set_bit(CACHE_NEGATIVE, &old->flags); 140 else 141 detail->update(old, new); 142 is_new = cache_fresh_locked(old, new->expiry_time); 143 write_unlock(&detail->hash_lock); 144 cache_fresh_unlocked(old, detail, is_new); 145 return old; 146 } 147 write_unlock(&detail->hash_lock); 148 } 149 /* We need to insert a new entry */ 150 tmp = detail->alloc(); 151 if (!tmp) { 152 cache_put(old, detail); 153 return NULL; 154 } 155 cache_init(tmp); 156 detail->init(tmp, old); 157 head = &detail->hash_table[hash]; 158 159 write_lock(&detail->hash_lock); 160 if (test_bit(CACHE_NEGATIVE, &new->flags)) 161 set_bit(CACHE_NEGATIVE, &tmp->flags); 162 else 163 detail->update(tmp, new); 164 tmp->next = *head; 165 *head = tmp; 166 detail->entries++; 167 cache_get(tmp); 168 is_new = cache_fresh_locked(tmp, new->expiry_time); 169 cache_fresh_locked(old, 0); 170 write_unlock(&detail->hash_lock); 171 cache_fresh_unlocked(tmp, detail, is_new); 172 cache_fresh_unlocked(old, detail, 0); 173 cache_put(old, detail); 174 return tmp; 175 } 176 EXPORT_SYMBOL(sunrpc_cache_update); 177 178 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h); 179 /* 180 * This is the generic cache management routine for all 181 * the authentication caches. 182 * It checks the currency of a cache item and will (later) 183 * initiate an upcall to fill it if needed. 184 * 185 * 186 * Returns 0 if the cache_head can be used, or cache_puts it and returns 187 * -EAGAIN if upcall is pending, 188 * -ETIMEDOUT if upcall failed and should be retried, 189 * -ENOENT if cache entry was negative 190 */ 191 int cache_check(struct cache_detail *detail, 192 struct cache_head *h, struct cache_req *rqstp) 193 { 194 int rv; 195 long refresh_age, age; 196 197 /* First decide return status as best we can */ 198 if (!test_bit(CACHE_VALID, &h->flags) || 199 h->expiry_time < get_seconds()) 200 rv = -EAGAIN; 201 else if (detail->flush_time > h->last_refresh) 202 rv = -EAGAIN; 203 else { 204 /* entry is valid */ 205 if (test_bit(CACHE_NEGATIVE, &h->flags)) 206 rv = -ENOENT; 207 else rv = 0; 208 } 209 210 /* now see if we want to start an upcall */ 211 refresh_age = (h->expiry_time - h->last_refresh); 212 age = get_seconds() - h->last_refresh; 213 214 if (rqstp == NULL) { 215 if (rv == -EAGAIN) 216 rv = -ENOENT; 217 } else if (rv == -EAGAIN || age > refresh_age/2) { 218 dprintk("RPC: Want update, refage=%ld, age=%ld\n", 219 refresh_age, age); 220 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) { 221 switch (cache_make_upcall(detail, h)) { 222 case -EINVAL: 223 clear_bit(CACHE_PENDING, &h->flags); 224 if (rv == -EAGAIN) { 225 set_bit(CACHE_NEGATIVE, &h->flags); 226 cache_fresh_unlocked(h, detail, 227 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY)); 228 rv = -ENOENT; 229 } 230 break; 231 232 case -EAGAIN: 233 clear_bit(CACHE_PENDING, &h->flags); 234 cache_revisit_request(h); 235 break; 236 } 237 } 238 } 239 240 if (rv == -EAGAIN) 241 if (cache_defer_req(rqstp, h) != 0) 242 rv = -ETIMEDOUT; 243 244 if (rv) 245 cache_put(h, detail); 246 return rv; 247 } 248 249 /* 250 * caches need to be periodically cleaned. 251 * For this we maintain a list of cache_detail and 252 * a current pointer into that list and into the table 253 * for that entry. 254 * 255 * Each time clean_cache is called it finds the next non-empty entry 256 * in the current table and walks the list in that entry 257 * looking for entries that can be removed. 258 * 259 * An entry gets removed if: 260 * - The expiry is before current time 261 * - The last_refresh time is before the flush_time for that cache 262 * 263 * later we might drop old entries with non-NEVER expiry if that table 264 * is getting 'full' for some definition of 'full' 265 * 266 * The question of "how often to scan a table" is an interesting one 267 * and is answered in part by the use of the "nextcheck" field in the 268 * cache_detail. 269 * When a scan of a table begins, the nextcheck field is set to a time 270 * that is well into the future. 271 * While scanning, if an expiry time is found that is earlier than the 272 * current nextcheck time, nextcheck is set to that expiry time. 273 * If the flush_time is ever set to a time earlier than the nextcheck 274 * time, the nextcheck time is then set to that flush_time. 275 * 276 * A table is then only scanned if the current time is at least 277 * the nextcheck time. 278 * 279 */ 280 281 static LIST_HEAD(cache_list); 282 static DEFINE_SPINLOCK(cache_list_lock); 283 static struct cache_detail *current_detail; 284 static int current_index; 285 286 static const struct file_operations cache_file_operations; 287 static const struct file_operations content_file_operations; 288 static const struct file_operations cache_flush_operations; 289 290 static void do_cache_clean(struct work_struct *work); 291 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean); 292 293 void cache_register(struct cache_detail *cd) 294 { 295 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc); 296 if (cd->proc_ent) { 297 struct proc_dir_entry *p; 298 cd->proc_ent->owner = cd->owner; 299 cd->channel_ent = cd->content_ent = NULL; 300 301 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR, 302 cd->proc_ent); 303 cd->flush_ent = p; 304 if (p) { 305 p->proc_fops = &cache_flush_operations; 306 p->owner = cd->owner; 307 p->data = cd; 308 } 309 310 if (cd->cache_request || cd->cache_parse) { 311 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR, 312 cd->proc_ent); 313 cd->channel_ent = p; 314 if (p) { 315 p->proc_fops = &cache_file_operations; 316 p->owner = cd->owner; 317 p->data = cd; 318 } 319 } 320 if (cd->cache_show) { 321 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR, 322 cd->proc_ent); 323 cd->content_ent = p; 324 if (p) { 325 p->proc_fops = &content_file_operations; 326 p->owner = cd->owner; 327 p->data = cd; 328 } 329 } 330 } 331 rwlock_init(&cd->hash_lock); 332 INIT_LIST_HEAD(&cd->queue); 333 spin_lock(&cache_list_lock); 334 cd->nextcheck = 0; 335 cd->entries = 0; 336 atomic_set(&cd->readers, 0); 337 cd->last_close = 0; 338 cd->last_warn = -1; 339 list_add(&cd->others, &cache_list); 340 spin_unlock(&cache_list_lock); 341 342 /* start the cleaning process */ 343 schedule_delayed_work(&cache_cleaner, 0); 344 } 345 346 int cache_unregister(struct cache_detail *cd) 347 { 348 cache_purge(cd); 349 spin_lock(&cache_list_lock); 350 write_lock(&cd->hash_lock); 351 if (cd->entries || atomic_read(&cd->inuse)) { 352 write_unlock(&cd->hash_lock); 353 spin_unlock(&cache_list_lock); 354 return -EBUSY; 355 } 356 if (current_detail == cd) 357 current_detail = NULL; 358 list_del_init(&cd->others); 359 write_unlock(&cd->hash_lock); 360 spin_unlock(&cache_list_lock); 361 if (cd->proc_ent) { 362 if (cd->flush_ent) 363 remove_proc_entry("flush", cd->proc_ent); 364 if (cd->channel_ent) 365 remove_proc_entry("channel", cd->proc_ent); 366 if (cd->content_ent) 367 remove_proc_entry("content", cd->proc_ent); 368 369 cd->proc_ent = NULL; 370 remove_proc_entry(cd->name, proc_net_rpc); 371 } 372 if (list_empty(&cache_list)) { 373 /* module must be being unloaded so its safe to kill the worker */ 374 cancel_delayed_work(&cache_cleaner); 375 flush_scheduled_work(); 376 } 377 return 0; 378 } 379 380 /* clean cache tries to find something to clean 381 * and cleans it. 382 * It returns 1 if it cleaned something, 383 * 0 if it didn't find anything this time 384 * -1 if it fell off the end of the list. 385 */ 386 static int cache_clean(void) 387 { 388 int rv = 0; 389 struct list_head *next; 390 391 spin_lock(&cache_list_lock); 392 393 /* find a suitable table if we don't already have one */ 394 while (current_detail == NULL || 395 current_index >= current_detail->hash_size) { 396 if (current_detail) 397 next = current_detail->others.next; 398 else 399 next = cache_list.next; 400 if (next == &cache_list) { 401 current_detail = NULL; 402 spin_unlock(&cache_list_lock); 403 return -1; 404 } 405 current_detail = list_entry(next, struct cache_detail, others); 406 if (current_detail->nextcheck > get_seconds()) 407 current_index = current_detail->hash_size; 408 else { 409 current_index = 0; 410 current_detail->nextcheck = get_seconds()+30*60; 411 } 412 } 413 414 /* find a non-empty bucket in the table */ 415 while (current_detail && 416 current_index < current_detail->hash_size && 417 current_detail->hash_table[current_index] == NULL) 418 current_index++; 419 420 /* find a cleanable entry in the bucket and clean it, or set to next bucket */ 421 422 if (current_detail && current_index < current_detail->hash_size) { 423 struct cache_head *ch, **cp; 424 struct cache_detail *d; 425 426 write_lock(¤t_detail->hash_lock); 427 428 /* Ok, now to clean this strand */ 429 430 cp = & current_detail->hash_table[current_index]; 431 ch = *cp; 432 for (; ch; cp= & ch->next, ch= *cp) { 433 if (current_detail->nextcheck > ch->expiry_time) 434 current_detail->nextcheck = ch->expiry_time+1; 435 if (ch->expiry_time >= get_seconds() 436 && ch->last_refresh >= current_detail->flush_time 437 ) 438 continue; 439 if (test_and_clear_bit(CACHE_PENDING, &ch->flags)) 440 queue_loose(current_detail, ch); 441 442 if (atomic_read(&ch->ref.refcount) == 1) 443 break; 444 } 445 if (ch) { 446 *cp = ch->next; 447 ch->next = NULL; 448 current_detail->entries--; 449 rv = 1; 450 } 451 write_unlock(¤t_detail->hash_lock); 452 d = current_detail; 453 if (!ch) 454 current_index ++; 455 spin_unlock(&cache_list_lock); 456 if (ch) 457 cache_put(ch, d); 458 } else 459 spin_unlock(&cache_list_lock); 460 461 return rv; 462 } 463 464 /* 465 * We want to regularly clean the cache, so we need to schedule some work ... 466 */ 467 static void do_cache_clean(struct work_struct *work) 468 { 469 int delay = 5; 470 if (cache_clean() == -1) 471 delay = 30*HZ; 472 473 if (list_empty(&cache_list)) 474 delay = 0; 475 476 if (delay) 477 schedule_delayed_work(&cache_cleaner, delay); 478 } 479 480 481 /* 482 * Clean all caches promptly. This just calls cache_clean 483 * repeatedly until we are sure that every cache has had a chance to 484 * be fully cleaned 485 */ 486 void cache_flush(void) 487 { 488 while (cache_clean() != -1) 489 cond_resched(); 490 while (cache_clean() != -1) 491 cond_resched(); 492 } 493 494 void cache_purge(struct cache_detail *detail) 495 { 496 detail->flush_time = LONG_MAX; 497 detail->nextcheck = get_seconds(); 498 cache_flush(); 499 detail->flush_time = 1; 500 } 501 502 503 504 /* 505 * Deferral and Revisiting of Requests. 506 * 507 * If a cache lookup finds a pending entry, we 508 * need to defer the request and revisit it later. 509 * All deferred requests are stored in a hash table, 510 * indexed by "struct cache_head *". 511 * As it may be wasteful to store a whole request 512 * structure, we allow the request to provide a 513 * deferred form, which must contain a 514 * 'struct cache_deferred_req' 515 * This cache_deferred_req contains a method to allow 516 * it to be revisited when cache info is available 517 */ 518 519 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head)) 520 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE) 521 522 #define DFR_MAX 300 /* ??? */ 523 524 static DEFINE_SPINLOCK(cache_defer_lock); 525 static LIST_HEAD(cache_defer_list); 526 static struct list_head cache_defer_hash[DFR_HASHSIZE]; 527 static int cache_defer_cnt; 528 529 static int cache_defer_req(struct cache_req *req, struct cache_head *item) 530 { 531 struct cache_deferred_req *dreq; 532 int hash = DFR_HASH(item); 533 534 if (cache_defer_cnt >= DFR_MAX) { 535 /* too much in the cache, randomly drop this one, 536 * or continue and drop the oldest below 537 */ 538 if (net_random()&1) 539 return -ETIMEDOUT; 540 } 541 dreq = req->defer(req); 542 if (dreq == NULL) 543 return -ETIMEDOUT; 544 545 dreq->item = item; 546 dreq->recv_time = get_seconds(); 547 548 spin_lock(&cache_defer_lock); 549 550 list_add(&dreq->recent, &cache_defer_list); 551 552 if (cache_defer_hash[hash].next == NULL) 553 INIT_LIST_HEAD(&cache_defer_hash[hash]); 554 list_add(&dreq->hash, &cache_defer_hash[hash]); 555 556 /* it is in, now maybe clean up */ 557 dreq = NULL; 558 if (++cache_defer_cnt > DFR_MAX) { 559 dreq = list_entry(cache_defer_list.prev, 560 struct cache_deferred_req, recent); 561 list_del(&dreq->recent); 562 list_del(&dreq->hash); 563 cache_defer_cnt--; 564 } 565 spin_unlock(&cache_defer_lock); 566 567 if (dreq) { 568 /* there was one too many */ 569 dreq->revisit(dreq, 1); 570 } 571 if (!test_bit(CACHE_PENDING, &item->flags)) { 572 /* must have just been validated... */ 573 cache_revisit_request(item); 574 } 575 return 0; 576 } 577 578 static void cache_revisit_request(struct cache_head *item) 579 { 580 struct cache_deferred_req *dreq; 581 struct list_head pending; 582 583 struct list_head *lp; 584 int hash = DFR_HASH(item); 585 586 INIT_LIST_HEAD(&pending); 587 spin_lock(&cache_defer_lock); 588 589 lp = cache_defer_hash[hash].next; 590 if (lp) { 591 while (lp != &cache_defer_hash[hash]) { 592 dreq = list_entry(lp, struct cache_deferred_req, hash); 593 lp = lp->next; 594 if (dreq->item == item) { 595 list_del(&dreq->hash); 596 list_move(&dreq->recent, &pending); 597 cache_defer_cnt--; 598 } 599 } 600 } 601 spin_unlock(&cache_defer_lock); 602 603 while (!list_empty(&pending)) { 604 dreq = list_entry(pending.next, struct cache_deferred_req, recent); 605 list_del_init(&dreq->recent); 606 dreq->revisit(dreq, 0); 607 } 608 } 609 610 void cache_clean_deferred(void *owner) 611 { 612 struct cache_deferred_req *dreq, *tmp; 613 struct list_head pending; 614 615 616 INIT_LIST_HEAD(&pending); 617 spin_lock(&cache_defer_lock); 618 619 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) { 620 if (dreq->owner == owner) { 621 list_del(&dreq->hash); 622 list_move(&dreq->recent, &pending); 623 cache_defer_cnt--; 624 } 625 } 626 spin_unlock(&cache_defer_lock); 627 628 while (!list_empty(&pending)) { 629 dreq = list_entry(pending.next, struct cache_deferred_req, recent); 630 list_del_init(&dreq->recent); 631 dreq->revisit(dreq, 1); 632 } 633 } 634 635 /* 636 * communicate with user-space 637 * 638 * We have a magic /proc file - /proc/sunrpc/cache 639 * On read, you get a full request, or block 640 * On write, an update request is processed 641 * Poll works if anything to read, and always allows write 642 * 643 * Implemented by linked list of requests. Each open file has 644 * a ->private that also exists in this list. New request are added 645 * to the end and may wakeup and preceding readers. 646 * New readers are added to the head. If, on read, an item is found with 647 * CACHE_UPCALLING clear, we free it from the list. 648 * 649 */ 650 651 static DEFINE_SPINLOCK(queue_lock); 652 static DEFINE_MUTEX(queue_io_mutex); 653 654 struct cache_queue { 655 struct list_head list; 656 int reader; /* if 0, then request */ 657 }; 658 struct cache_request { 659 struct cache_queue q; 660 struct cache_head *item; 661 char * buf; 662 int len; 663 int readers; 664 }; 665 struct cache_reader { 666 struct cache_queue q; 667 int offset; /* if non-0, we have a refcnt on next request */ 668 }; 669 670 static ssize_t 671 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) 672 { 673 struct cache_reader *rp = filp->private_data; 674 struct cache_request *rq; 675 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; 676 int err; 677 678 if (count == 0) 679 return 0; 680 681 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent 682 * readers on this file */ 683 again: 684 spin_lock(&queue_lock); 685 /* need to find next request */ 686 while (rp->q.list.next != &cd->queue && 687 list_entry(rp->q.list.next, struct cache_queue, list) 688 ->reader) { 689 struct list_head *next = rp->q.list.next; 690 list_move(&rp->q.list, next); 691 } 692 if (rp->q.list.next == &cd->queue) { 693 spin_unlock(&queue_lock); 694 mutex_unlock(&queue_io_mutex); 695 BUG_ON(rp->offset); 696 return 0; 697 } 698 rq = container_of(rp->q.list.next, struct cache_request, q.list); 699 BUG_ON(rq->q.reader); 700 if (rp->offset == 0) 701 rq->readers++; 702 spin_unlock(&queue_lock); 703 704 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { 705 err = -EAGAIN; 706 spin_lock(&queue_lock); 707 list_move(&rp->q.list, &rq->q.list); 708 spin_unlock(&queue_lock); 709 } else { 710 if (rp->offset + count > rq->len) 711 count = rq->len - rp->offset; 712 err = -EFAULT; 713 if (copy_to_user(buf, rq->buf + rp->offset, count)) 714 goto out; 715 rp->offset += count; 716 if (rp->offset >= rq->len) { 717 rp->offset = 0; 718 spin_lock(&queue_lock); 719 list_move(&rp->q.list, &rq->q.list); 720 spin_unlock(&queue_lock); 721 } 722 err = 0; 723 } 724 out: 725 if (rp->offset == 0) { 726 /* need to release rq */ 727 spin_lock(&queue_lock); 728 rq->readers--; 729 if (rq->readers == 0 && 730 !test_bit(CACHE_PENDING, &rq->item->flags)) { 731 list_del(&rq->q.list); 732 spin_unlock(&queue_lock); 733 cache_put(rq->item, cd); 734 kfree(rq->buf); 735 kfree(rq); 736 } else 737 spin_unlock(&queue_lock); 738 } 739 if (err == -EAGAIN) 740 goto again; 741 mutex_unlock(&queue_io_mutex); 742 return err ? err : count; 743 } 744 745 static char write_buf[8192]; /* protected by queue_io_mutex */ 746 747 static ssize_t 748 cache_write(struct file *filp, const char __user *buf, size_t count, 749 loff_t *ppos) 750 { 751 int err; 752 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; 753 754 if (count == 0) 755 return 0; 756 if (count >= sizeof(write_buf)) 757 return -EINVAL; 758 759 mutex_lock(&queue_io_mutex); 760 761 if (copy_from_user(write_buf, buf, count)) { 762 mutex_unlock(&queue_io_mutex); 763 return -EFAULT; 764 } 765 write_buf[count] = '\0'; 766 if (cd->cache_parse) 767 err = cd->cache_parse(cd, write_buf, count); 768 else 769 err = -EINVAL; 770 771 mutex_unlock(&queue_io_mutex); 772 return err ? err : count; 773 } 774 775 static DECLARE_WAIT_QUEUE_HEAD(queue_wait); 776 777 static unsigned int 778 cache_poll(struct file *filp, poll_table *wait) 779 { 780 unsigned int mask; 781 struct cache_reader *rp = filp->private_data; 782 struct cache_queue *cq; 783 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; 784 785 poll_wait(filp, &queue_wait, wait); 786 787 /* alway allow write */ 788 mask = POLL_OUT | POLLWRNORM; 789 790 if (!rp) 791 return mask; 792 793 spin_lock(&queue_lock); 794 795 for (cq= &rp->q; &cq->list != &cd->queue; 796 cq = list_entry(cq->list.next, struct cache_queue, list)) 797 if (!cq->reader) { 798 mask |= POLLIN | POLLRDNORM; 799 break; 800 } 801 spin_unlock(&queue_lock); 802 return mask; 803 } 804 805 static int 806 cache_ioctl(struct inode *ino, struct file *filp, 807 unsigned int cmd, unsigned long arg) 808 { 809 int len = 0; 810 struct cache_reader *rp = filp->private_data; 811 struct cache_queue *cq; 812 struct cache_detail *cd = PDE(ino)->data; 813 814 if (cmd != FIONREAD || !rp) 815 return -EINVAL; 816 817 spin_lock(&queue_lock); 818 819 /* only find the length remaining in current request, 820 * or the length of the next request 821 */ 822 for (cq= &rp->q; &cq->list != &cd->queue; 823 cq = list_entry(cq->list.next, struct cache_queue, list)) 824 if (!cq->reader) { 825 struct cache_request *cr = 826 container_of(cq, struct cache_request, q); 827 len = cr->len - rp->offset; 828 break; 829 } 830 spin_unlock(&queue_lock); 831 832 return put_user(len, (int __user *)arg); 833 } 834 835 static int 836 cache_open(struct inode *inode, struct file *filp) 837 { 838 struct cache_reader *rp = NULL; 839 840 nonseekable_open(inode, filp); 841 if (filp->f_mode & FMODE_READ) { 842 struct cache_detail *cd = PDE(inode)->data; 843 844 rp = kmalloc(sizeof(*rp), GFP_KERNEL); 845 if (!rp) 846 return -ENOMEM; 847 rp->offset = 0; 848 rp->q.reader = 1; 849 atomic_inc(&cd->readers); 850 spin_lock(&queue_lock); 851 list_add(&rp->q.list, &cd->queue); 852 spin_unlock(&queue_lock); 853 } 854 filp->private_data = rp; 855 return 0; 856 } 857 858 static int 859 cache_release(struct inode *inode, struct file *filp) 860 { 861 struct cache_reader *rp = filp->private_data; 862 struct cache_detail *cd = PDE(inode)->data; 863 864 if (rp) { 865 spin_lock(&queue_lock); 866 if (rp->offset) { 867 struct cache_queue *cq; 868 for (cq= &rp->q; &cq->list != &cd->queue; 869 cq = list_entry(cq->list.next, struct cache_queue, list)) 870 if (!cq->reader) { 871 container_of(cq, struct cache_request, q) 872 ->readers--; 873 break; 874 } 875 rp->offset = 0; 876 } 877 list_del(&rp->q.list); 878 spin_unlock(&queue_lock); 879 880 filp->private_data = NULL; 881 kfree(rp); 882 883 cd->last_close = get_seconds(); 884 atomic_dec(&cd->readers); 885 } 886 return 0; 887 } 888 889 890 891 static const struct file_operations cache_file_operations = { 892 .owner = THIS_MODULE, 893 .llseek = no_llseek, 894 .read = cache_read, 895 .write = cache_write, 896 .poll = cache_poll, 897 .ioctl = cache_ioctl, /* for FIONREAD */ 898 .open = cache_open, 899 .release = cache_release, 900 }; 901 902 903 static void queue_loose(struct cache_detail *detail, struct cache_head *ch) 904 { 905 struct cache_queue *cq; 906 spin_lock(&queue_lock); 907 list_for_each_entry(cq, &detail->queue, list) 908 if (!cq->reader) { 909 struct cache_request *cr = container_of(cq, struct cache_request, q); 910 if (cr->item != ch) 911 continue; 912 if (cr->readers != 0) 913 continue; 914 list_del(&cr->q.list); 915 spin_unlock(&queue_lock); 916 cache_put(cr->item, detail); 917 kfree(cr->buf); 918 kfree(cr); 919 return; 920 } 921 spin_unlock(&queue_lock); 922 } 923 924 /* 925 * Support routines for text-based upcalls. 926 * Fields are separated by spaces. 927 * Fields are either mangled to quote space tab newline slosh with slosh 928 * or a hexified with a leading \x 929 * Record is terminated with newline. 930 * 931 */ 932 933 void qword_add(char **bpp, int *lp, char *str) 934 { 935 char *bp = *bpp; 936 int len = *lp; 937 char c; 938 939 if (len < 0) return; 940 941 while ((c=*str++) && len) 942 switch(c) { 943 case ' ': 944 case '\t': 945 case '\n': 946 case '\\': 947 if (len >= 4) { 948 *bp++ = '\\'; 949 *bp++ = '0' + ((c & 0300)>>6); 950 *bp++ = '0' + ((c & 0070)>>3); 951 *bp++ = '0' + ((c & 0007)>>0); 952 } 953 len -= 4; 954 break; 955 default: 956 *bp++ = c; 957 len--; 958 } 959 if (c || len <1) len = -1; 960 else { 961 *bp++ = ' '; 962 len--; 963 } 964 *bpp = bp; 965 *lp = len; 966 } 967 968 void qword_addhex(char **bpp, int *lp, char *buf, int blen) 969 { 970 char *bp = *bpp; 971 int len = *lp; 972 973 if (len < 0) return; 974 975 if (len > 2) { 976 *bp++ = '\\'; 977 *bp++ = 'x'; 978 len -= 2; 979 while (blen && len >= 2) { 980 unsigned char c = *buf++; 981 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); 982 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); 983 len -= 2; 984 blen--; 985 } 986 } 987 if (blen || len<1) len = -1; 988 else { 989 *bp++ = ' '; 990 len--; 991 } 992 *bpp = bp; 993 *lp = len; 994 } 995 996 static void warn_no_listener(struct cache_detail *detail) 997 { 998 if (detail->last_warn != detail->last_close) { 999 detail->last_warn = detail->last_close; 1000 if (detail->warn_no_listener) 1001 detail->warn_no_listener(detail); 1002 } 1003 } 1004 1005 /* 1006 * register an upcall request to user-space. 1007 * Each request is at most one page long. 1008 */ 1009 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h) 1010 { 1011 1012 char *buf; 1013 struct cache_request *crq; 1014 char *bp; 1015 int len; 1016 1017 if (detail->cache_request == NULL) 1018 return -EINVAL; 1019 1020 if (atomic_read(&detail->readers) == 0 && 1021 detail->last_close < get_seconds() - 30) { 1022 warn_no_listener(detail); 1023 return -EINVAL; 1024 } 1025 1026 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1027 if (!buf) 1028 return -EAGAIN; 1029 1030 crq = kmalloc(sizeof (*crq), GFP_KERNEL); 1031 if (!crq) { 1032 kfree(buf); 1033 return -EAGAIN; 1034 } 1035 1036 bp = buf; len = PAGE_SIZE; 1037 1038 detail->cache_request(detail, h, &bp, &len); 1039 1040 if (len < 0) { 1041 kfree(buf); 1042 kfree(crq); 1043 return -EAGAIN; 1044 } 1045 crq->q.reader = 0; 1046 crq->item = cache_get(h); 1047 crq->buf = buf; 1048 crq->len = PAGE_SIZE - len; 1049 crq->readers = 0; 1050 spin_lock(&queue_lock); 1051 list_add_tail(&crq->q.list, &detail->queue); 1052 spin_unlock(&queue_lock); 1053 wake_up(&queue_wait); 1054 return 0; 1055 } 1056 1057 /* 1058 * parse a message from user-space and pass it 1059 * to an appropriate cache 1060 * Messages are, like requests, separated into fields by 1061 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal 1062 * 1063 * Message is 1064 * reply cachename expiry key ... content.... 1065 * 1066 * key and content are both parsed by cache 1067 */ 1068 1069 #define isodigit(c) (isdigit(c) && c <= '7') 1070 int qword_get(char **bpp, char *dest, int bufsize) 1071 { 1072 /* return bytes copied, or -1 on error */ 1073 char *bp = *bpp; 1074 int len = 0; 1075 1076 while (*bp == ' ') bp++; 1077 1078 if (bp[0] == '\\' && bp[1] == 'x') { 1079 /* HEX STRING */ 1080 bp += 2; 1081 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) { 1082 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; 1083 bp++; 1084 byte <<= 4; 1085 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; 1086 *dest++ = byte; 1087 bp++; 1088 len++; 1089 } 1090 } else { 1091 /* text with \nnn octal quoting */ 1092 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) { 1093 if (*bp == '\\' && 1094 isodigit(bp[1]) && (bp[1] <= '3') && 1095 isodigit(bp[2]) && 1096 isodigit(bp[3])) { 1097 int byte = (*++bp -'0'); 1098 bp++; 1099 byte = (byte << 3) | (*bp++ - '0'); 1100 byte = (byte << 3) | (*bp++ - '0'); 1101 *dest++ = byte; 1102 len++; 1103 } else { 1104 *dest++ = *bp++; 1105 len++; 1106 } 1107 } 1108 } 1109 1110 if (*bp != ' ' && *bp != '\n' && *bp != '\0') 1111 return -1; 1112 while (*bp == ' ') bp++; 1113 *bpp = bp; 1114 *dest = '\0'; 1115 return len; 1116 } 1117 1118 1119 /* 1120 * support /proc/sunrpc/cache/$CACHENAME/content 1121 * as a seqfile. 1122 * We call ->cache_show passing NULL for the item to 1123 * get a header, then pass each real item in the cache 1124 */ 1125 1126 struct handle { 1127 struct cache_detail *cd; 1128 }; 1129 1130 static void *c_start(struct seq_file *m, loff_t *pos) 1131 { 1132 loff_t n = *pos; 1133 unsigned hash, entry; 1134 struct cache_head *ch; 1135 struct cache_detail *cd = ((struct handle*)m->private)->cd; 1136 1137 1138 read_lock(&cd->hash_lock); 1139 if (!n--) 1140 return SEQ_START_TOKEN; 1141 hash = n >> 32; 1142 entry = n & ((1LL<<32) - 1); 1143 1144 for (ch=cd->hash_table[hash]; ch; ch=ch->next) 1145 if (!entry--) 1146 return ch; 1147 n &= ~((1LL<<32) - 1); 1148 do { 1149 hash++; 1150 n += 1LL<<32; 1151 } while(hash < cd->hash_size && 1152 cd->hash_table[hash]==NULL); 1153 if (hash >= cd->hash_size) 1154 return NULL; 1155 *pos = n+1; 1156 return cd->hash_table[hash]; 1157 } 1158 1159 static void *c_next(struct seq_file *m, void *p, loff_t *pos) 1160 { 1161 struct cache_head *ch = p; 1162 int hash = (*pos >> 32); 1163 struct cache_detail *cd = ((struct handle*)m->private)->cd; 1164 1165 if (p == SEQ_START_TOKEN) 1166 hash = 0; 1167 else if (ch->next == NULL) { 1168 hash++; 1169 *pos += 1LL<<32; 1170 } else { 1171 ++*pos; 1172 return ch->next; 1173 } 1174 *pos &= ~((1LL<<32) - 1); 1175 while (hash < cd->hash_size && 1176 cd->hash_table[hash] == NULL) { 1177 hash++; 1178 *pos += 1LL<<32; 1179 } 1180 if (hash >= cd->hash_size) 1181 return NULL; 1182 ++*pos; 1183 return cd->hash_table[hash]; 1184 } 1185 1186 static void c_stop(struct seq_file *m, void *p) 1187 { 1188 struct cache_detail *cd = ((struct handle*)m->private)->cd; 1189 read_unlock(&cd->hash_lock); 1190 } 1191 1192 static int c_show(struct seq_file *m, void *p) 1193 { 1194 struct cache_head *cp = p; 1195 struct cache_detail *cd = ((struct handle*)m->private)->cd; 1196 1197 if (p == SEQ_START_TOKEN) 1198 return cd->cache_show(m, cd, NULL); 1199 1200 ifdebug(CACHE) 1201 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", 1202 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags); 1203 cache_get(cp); 1204 if (cache_check(cd, cp, NULL)) 1205 /* cache_check does a cache_put on failure */ 1206 seq_printf(m, "# "); 1207 else 1208 cache_put(cp, cd); 1209 1210 return cd->cache_show(m, cd, cp); 1211 } 1212 1213 static struct seq_operations cache_content_op = { 1214 .start = c_start, 1215 .next = c_next, 1216 .stop = c_stop, 1217 .show = c_show, 1218 }; 1219 1220 static int content_open(struct inode *inode, struct file *file) 1221 { 1222 int res; 1223 struct handle *han; 1224 struct cache_detail *cd = PDE(inode)->data; 1225 1226 han = kmalloc(sizeof(*han), GFP_KERNEL); 1227 if (han == NULL) 1228 return -ENOMEM; 1229 1230 han->cd = cd; 1231 1232 res = seq_open(file, &cache_content_op); 1233 if (res) 1234 kfree(han); 1235 else 1236 ((struct seq_file *)file->private_data)->private = han; 1237 1238 return res; 1239 } 1240 1241 static const struct file_operations content_file_operations = { 1242 .open = content_open, 1243 .read = seq_read, 1244 .llseek = seq_lseek, 1245 .release = seq_release_private, 1246 }; 1247 1248 static ssize_t read_flush(struct file *file, char __user *buf, 1249 size_t count, loff_t *ppos) 1250 { 1251 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data; 1252 char tbuf[20]; 1253 unsigned long p = *ppos; 1254 int len; 1255 1256 sprintf(tbuf, "%lu\n", cd->flush_time); 1257 len = strlen(tbuf); 1258 if (p >= len) 1259 return 0; 1260 len -= p; 1261 if (len > count) len = count; 1262 if (copy_to_user(buf, (void*)(tbuf+p), len)) 1263 len = -EFAULT; 1264 else 1265 *ppos += len; 1266 return len; 1267 } 1268 1269 static ssize_t write_flush(struct file * file, const char __user * buf, 1270 size_t count, loff_t *ppos) 1271 { 1272 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data; 1273 char tbuf[20]; 1274 char *ep; 1275 long flushtime; 1276 if (*ppos || count > sizeof(tbuf)-1) 1277 return -EINVAL; 1278 if (copy_from_user(tbuf, buf, count)) 1279 return -EFAULT; 1280 tbuf[count] = 0; 1281 flushtime = simple_strtoul(tbuf, &ep, 0); 1282 if (*ep && *ep != '\n') 1283 return -EINVAL; 1284 1285 cd->flush_time = flushtime; 1286 cd->nextcheck = get_seconds(); 1287 cache_flush(); 1288 1289 *ppos += count; 1290 return count; 1291 } 1292 1293 static const struct file_operations cache_flush_operations = { 1294 .open = nonseekable_open, 1295 .read = read_flush, 1296 .write = write_flush, 1297 }; 1298