Merge branch 'for-2.6.35' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[safe/jmp/linux-2.6] / fs / nfsd / nfscache.c
index d852ebb..4666a20 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * linux/fs/nfsd/nfscache.c
- *
  * Request reply cache. This is currently a global cache, but this may
  * change in the future and be a per-client cache.
  *
  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  */
 
-#include <linux/kernel.h>
-#include <linux/time.h>
 #include <linux/slab.h>
-#include <linux/string.h>
-#include <linux/spinlock.h>
-#include <linux/list.h>
 
-#include <linux/sunrpc/svc.h>
-#include <linux/nfsd/nfsd.h>
-#include <linux/nfsd/cache.h>
+#include "nfsd.h"
+#include "cache.h"
 
 /* Size of reply cache. Common values are:
  * 4.3BSD:     128
  */
 #define CACHESIZE              1024
 #define HASHSIZE               64
-#define REQHASH(xid)           ((((xid) >> 24) ^ (xid)) & (HASHSIZE-1))
 
-static struct hlist_head *     hash_list;
+static struct hlist_head *     cache_hash;
 static struct list_head        lru_head;
 static int                     cache_disabled = 1;
 
+/*
+ * Calculate the hash index from an XID.
+ */
+static inline u32 request_hash(u32 xid)
+{
+       u32 h = xid;
+       h ^= (xid >> 24);
+       return h & (HASHSIZE-1);
+}
+
 static int     nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
 
-/* 
+/*
  * locking for the reply cache:
  * A cache entry is "single use" if c_state == RC_INPROG
  * Otherwise, it when accessing _prev or _next, the lock must be held.
  */
 static DEFINE_SPINLOCK(cache_lock);
 
-void
-nfsd_cache_init(void)
+int nfsd_reply_cache_init(void)
 {
        struct svc_cacherep     *rp;
        int                     i;
 
        INIT_LIST_HEAD(&lru_head);
        i = CACHESIZE;
-       while(i) {
+       while (i) {
                rp = kmalloc(sizeof(*rp), GFP_KERNEL);
-               if (!rp) break;
+               if (!rp)
+                       goto out_nomem;
                list_add(&rp->c_lru, &lru_head);
                rp->c_state = RC_UNUSED;
                rp->c_type = RC_NOCACHE;
@@ -62,24 +63,19 @@ nfsd_cache_init(void)
                i--;
        }
 
-       if (i)
-               printk (KERN_ERR "nfsd: cannot allocate all %d cache entries, only got %d\n",
-                       CACHESIZE, CACHESIZE-i);
-
-       hash_list = kmalloc (HASHSIZE * sizeof(struct hlist_head), GFP_KERNEL);
-       if (!hash_list) {
-               nfsd_cache_shutdown();
-               printk (KERN_ERR "nfsd: cannot allocate %Zd bytes for hash list\n",
-                       HASHSIZE * sizeof(struct hlist_head));
-               return;
-       }
-       memset(hash_list, 0, HASHSIZE * sizeof(struct hlist_head));
+       cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
+       if (!cache_hash)
+               goto out_nomem;
 
        cache_disabled = 0;
+       return 0;
+out_nomem:
+       printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
+       nfsd_reply_cache_shutdown();
+       return -ENOMEM;
 }
 
-void
-nfsd_cache_shutdown(void)
+void nfsd_reply_cache_shutdown(void)
 {
        struct svc_cacherep     *rp;
 
@@ -93,8 +89,8 @@ nfsd_cache_shutdown(void)
 
        cache_disabled = 1;
 
-       kfree (hash_list);
-       hash_list = NULL;
+       kfree (cache_hash);
+       cache_hash = NULL;
 }
 
 /*
@@ -103,8 +99,7 @@ nfsd_cache_shutdown(void)
 static void
 lru_put_end(struct svc_cacherep *rp)
 {
-       list_del(&rp->c_lru);
-       list_add_tail(&rp->c_lru, &lru_head);
+       list_move_tail(&rp->c_lru, &lru_head);
 }
 
 /*
@@ -114,7 +109,7 @@ static void
 hash_refile(struct svc_cacherep *rp)
 {
        hlist_del_init(&rp->c_hash);
-       hlist_add_head(&rp->c_hash, hash_list + REQHASH(rp->c_xid));
+       hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
 }
 
 /*
@@ -128,8 +123,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
        struct hlist_node       *hn;
        struct hlist_head       *rh;
        struct svc_cacherep     *rp;
-       u32                     xid = rqstp->rq_xid,
-                               proto =  rqstp->rq_prot,
+       __be32                  xid = rqstp->rq_xid;
+       u32                     proto =  rqstp->rq_prot,
                                vers = rqstp->rq_vers,
                                proc = rqstp->rq_proc;
        unsigned long           age;
@@ -144,7 +139,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
        spin_lock(&cache_lock);
        rtn = RC_DOIT;
 
-       rh = &hash_list[REQHASH(xid)];
+       rh = &cache_hash[request_hash(xid)];
        hlist_for_each_entry(rp, hn, rh, c_hash) {
                if (rp->c_state != RC_UNUSED &&
                    xid == rp->c_xid && proc == rp->c_proc &&
@@ -171,8 +166,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
        }
        }
 
-       /* This should not happen */
-       if (rp == NULL) {
+       /* All entries on the LRU are in-progress. This should not happen */
+       if (&rp->c_lru == &lru_head) {
                static int      complaints;
 
                printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
@@ -187,7 +182,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
        rp->c_state = RC_INPROG;
        rp->c_xid = xid;
        rp->c_proc = proc;
-       rp->c_addr = rqstp->rq_addr;
+       memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr));
        rp->c_prot = proto;
        rp->c_vers = vers;
        rp->c_timestamp = jiffies;
@@ -259,7 +254,7 @@ found_entry:
  * In this case, nfsd_cache_update is called with statp == NULL.
  */
 void
-nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
+nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
 {
        struct svc_cacherep *rp;
        struct kvec     *resv = &rqstp->rq_res.head[0], *cachv;
@@ -270,7 +265,7 @@ nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
 
        len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
        len >>= 2;
-       
+
        /* Don't cache excessive amounts of data and XDR failures */
        if (!statp || len > (256 >> 2)) {
                rp->c_state = RC_UNUSED;