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