2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Request reply cache. This is currently a global cache, but this may
|
|
|
|
* change in the future and be a per-client cache.
|
|
|
|
*
|
|
|
|
* This code is heavily inspired by the 44BSD implementation, although
|
|
|
|
* it does things a bit differently.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
|
|
|
|
*/
|
|
|
|
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2013-01-29 03:41:07 +08:00
|
|
|
#include <linux/sunrpc/clnt.h>
|
2013-02-04 21:18:02 +08:00
|
|
|
#include <linux/highmem.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
|
2009-12-04 02:30:56 +08:00
|
|
|
#include "nfsd.h"
|
|
|
|
#include "cache.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#define HASHSIZE 64
|
|
|
|
|
2009-04-01 04:28:13 +08:00
|
|
|
static struct hlist_head * cache_hash;
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct list_head lru_head;
|
2013-01-29 03:41:10 +08:00
|
|
|
static struct kmem_cache *drc_slab;
|
2013-02-04 21:18:01 +08:00
|
|
|
static unsigned int num_drc_entries;
|
2013-02-04 21:18:02 +08:00
|
|
|
static unsigned int max_drc_entries;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-04-01 04:28:13 +08:00
|
|
|
/*
|
|
|
|
* Calculate the hash index from an XID.
|
|
|
|
*/
|
|
|
|
static inline u32 request_hash(u32 xid)
|
|
|
|
{
|
|
|
|
u32 h = xid;
|
|
|
|
h ^= (xid >> 24);
|
|
|
|
return h & (HASHSIZE-1);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
|
2013-02-04 21:18:05 +08:00
|
|
|
static void cache_cleaner_func(struct work_struct *unused);
|
2013-02-04 21:18:06 +08:00
|
|
|
static int nfsd_reply_cache_shrink(struct shrinker *shrink,
|
|
|
|
struct shrink_control *sc);
|
|
|
|
|
|
|
|
struct shrinker nfsd_reply_cache_shrinker = {
|
|
|
|
.shrink = nfsd_reply_cache_shrink,
|
|
|
|
.seeks = 1,
|
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-04-01 04:28:13 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* 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);
|
2013-02-04 21:18:05 +08:00
|
|
|
static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
/*
|
|
|
|
* Put a cap on the size of the DRC based on the amount of available
|
|
|
|
* low memory in the machine.
|
|
|
|
*
|
|
|
|
* 64MB: 8192
|
|
|
|
* 128MB: 11585
|
|
|
|
* 256MB: 16384
|
|
|
|
* 512MB: 23170
|
|
|
|
* 1GB: 32768
|
|
|
|
* 2GB: 46340
|
|
|
|
* 4GB: 65536
|
|
|
|
* 8GB: 92681
|
|
|
|
* 16GB: 131072
|
|
|
|
*
|
|
|
|
* ...with a hard cap of 256k entries. In the worst case, each entry will be
|
|
|
|
* ~1k, so the above numbers should give a rough max of the amount of memory
|
|
|
|
* used in k.
|
|
|
|
*/
|
|
|
|
static unsigned int
|
|
|
|
nfsd_cache_size_limit(void)
|
|
|
|
{
|
|
|
|
unsigned int limit;
|
|
|
|
unsigned long low_pages = totalram_pages - totalhigh_pages;
|
|
|
|
|
|
|
|
limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
|
|
|
|
return min_t(unsigned int, limit, 256*1024);
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:41:11 +08:00
|
|
|
static struct svc_cacherep *
|
|
|
|
nfsd_reply_cache_alloc(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct svc_cacherep *rp;
|
2013-01-29 03:41:11 +08:00
|
|
|
|
|
|
|
rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
|
|
|
|
if (rp) {
|
|
|
|
rp->c_state = RC_UNUSED;
|
|
|
|
rp->c_type = RC_NOCACHE;
|
|
|
|
INIT_LIST_HEAD(&rp->c_lru);
|
|
|
|
INIT_HLIST_NODE(&rp->c_hash);
|
|
|
|
}
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
|
|
|
|
{
|
2013-01-29 03:41:12 +08:00
|
|
|
if (rp->c_type == RC_REPLBUFF)
|
2013-01-29 03:41:11 +08:00
|
|
|
kfree(rp->c_replvec.iov_base);
|
2013-02-04 21:18:02 +08:00
|
|
|
hlist_del(&rp->c_hash);
|
2013-01-29 03:41:11 +08:00
|
|
|
list_del(&rp->c_lru);
|
2013-02-04 21:18:01 +08:00
|
|
|
--num_drc_entries;
|
2013-01-29 03:41:11 +08:00
|
|
|
kmem_cache_free(drc_slab, rp);
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:18:04 +08:00
|
|
|
static void
|
|
|
|
nfsd_reply_cache_free(struct svc_cacherep *rp)
|
|
|
|
{
|
|
|
|
spin_lock(&cache_lock);
|
|
|
|
nfsd_reply_cache_free_locked(rp);
|
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:41:11 +08:00
|
|
|
int nfsd_reply_cache_init(void)
|
|
|
|
{
|
2013-02-04 21:18:06 +08:00
|
|
|
register_shrinker(&nfsd_reply_cache_shrinker);
|
2013-01-29 03:41:10 +08:00
|
|
|
drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
|
|
|
|
0, 0, NULL);
|
|
|
|
if (!drc_slab)
|
|
|
|
goto out_nomem;
|
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
|
2009-04-01 04:28:13 +08:00
|
|
|
if (!cache_hash)
|
2007-11-10 03:10:56 +08:00
|
|
|
goto out_nomem;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
INIT_LIST_HEAD(&lru_head);
|
|
|
|
max_drc_entries = nfsd_cache_size_limit();
|
|
|
|
num_drc_entries = 0;
|
2007-11-10 03:10:56 +08:00
|
|
|
return 0;
|
|
|
|
out_nomem:
|
|
|
|
printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
|
|
|
|
nfsd_reply_cache_shutdown();
|
|
|
|
return -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-11-10 03:10:56 +08:00
|
|
|
void nfsd_reply_cache_shutdown(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct svc_cacherep *rp;
|
|
|
|
|
2013-02-04 21:18:06 +08:00
|
|
|
unregister_shrinker(&nfsd_reply_cache_shrinker);
|
2013-02-04 21:18:05 +08:00
|
|
|
cancel_delayed_work_sync(&cache_cleaner);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
while (!list_empty(&lru_head)) {
|
|
|
|
rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
|
2013-01-29 03:41:11 +08:00
|
|
|
nfsd_reply_cache_free_locked(rp);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:28:13 +08:00
|
|
|
kfree (cache_hash);
|
|
|
|
cache_hash = NULL;
|
2013-01-29 03:41:10 +08:00
|
|
|
|
|
|
|
if (drc_slab) {
|
|
|
|
kmem_cache_destroy(drc_slab);
|
|
|
|
drc_slab = NULL;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-02-04 21:18:05 +08:00
|
|
|
* Move cache entry to end of LRU list, and queue the cleaner to run if it's
|
|
|
|
* not already scheduled.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
lru_put_end(struct svc_cacherep *rp)
|
|
|
|
{
|
2013-02-04 21:18:00 +08:00
|
|
|
rp->c_timestamp = jiffies;
|
2006-06-26 15:24:46 +08:00
|
|
|
list_move_tail(&rp->c_lru, &lru_head);
|
2013-02-04 21:18:05 +08:00
|
|
|
schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move a cache entry from one hash list to another
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
hash_refile(struct svc_cacherep *rp)
|
|
|
|
{
|
|
|
|
hlist_del_init(&rp->c_hash);
|
2009-04-01 04:28:13 +08:00
|
|
|
hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-01-29 03:41:13 +08:00
|
|
|
static inline bool
|
|
|
|
nfsd_cache_entry_expired(struct svc_cacherep *rp)
|
|
|
|
{
|
|
|
|
return rp->c_state != RC_INPROG &&
|
|
|
|
time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:18:05 +08:00
|
|
|
/*
|
|
|
|
* Walk the LRU list and prune off entries that are older than RC_EXPIRE.
|
|
|
|
* Also prune the oldest ones when the total exceeds the max number of entries.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
prune_cache_entries(void)
|
|
|
|
{
|
|
|
|
struct svc_cacherep *rp, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
|
|
|
|
if (!nfsd_cache_entry_expired(rp) &&
|
|
|
|
num_drc_entries <= max_drc_entries)
|
|
|
|
break;
|
|
|
|
nfsd_reply_cache_free_locked(rp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditionally rearm the job. If we cleaned out the list, then
|
|
|
|
* cancel any pending run (since there won't be any work to do).
|
|
|
|
* Otherwise, we rearm the job or modify the existing one to run in
|
|
|
|
* RC_EXPIRE since we just ran the pruner.
|
|
|
|
*/
|
|
|
|
if (list_empty(&lru_head))
|
|
|
|
cancel_delayed_work(&cache_cleaner);
|
|
|
|
else
|
|
|
|
mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cache_cleaner_func(struct work_struct *unused)
|
|
|
|
{
|
|
|
|
spin_lock(&cache_lock);
|
|
|
|
prune_cache_entries();
|
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:18:06 +08:00
|
|
|
static int
|
|
|
|
nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
|
|
|
|
{
|
|
|
|
unsigned int num;
|
|
|
|
|
|
|
|
spin_lock(&cache_lock);
|
|
|
|
if (sc->nr_to_scan)
|
|
|
|
prune_cache_entries();
|
|
|
|
num = num_drc_entries;
|
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2013-01-29 03:41:14 +08:00
|
|
|
/*
|
|
|
|
* Search the request hash for an entry that matches the given rqstp.
|
|
|
|
* Must be called with cache_lock held. Returns the found entry or
|
|
|
|
* NULL on failure.
|
|
|
|
*/
|
|
|
|
static struct svc_cacherep *
|
|
|
|
nfsd_cache_search(struct svc_rqst *rqstp)
|
|
|
|
{
|
|
|
|
struct svc_cacherep *rp;
|
|
|
|
struct hlist_node *hn;
|
|
|
|
struct hlist_head *rh;
|
|
|
|
__be32 xid = rqstp->rq_xid;
|
|
|
|
u32 proto = rqstp->rq_prot,
|
|
|
|
vers = rqstp->rq_vers,
|
|
|
|
proc = rqstp->rq_proc;
|
|
|
|
|
|
|
|
rh = &cache_hash[request_hash(xid)];
|
|
|
|
hlist_for_each_entry(rp, hn, rh, c_hash) {
|
2013-02-04 21:18:04 +08:00
|
|
|
if (xid == rp->c_xid && proc == rp->c_proc &&
|
2013-01-29 03:41:14 +08:00
|
|
|
proto == rp->c_prot && vers == rp->c_vers &&
|
|
|
|
rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
|
|
|
|
rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Try to find an entry matching the current call in the cache. When none
|
|
|
|
* is found, we grab the oldest unlocked entry off the LRU list.
|
|
|
|
* Note that no operation within the loop may sleep.
|
|
|
|
*/
|
|
|
|
int
|
2011-01-25 01:11:02 +08:00
|
|
|
nfsd_cache_lookup(struct svc_rqst *rqstp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-02-04 21:18:02 +08:00
|
|
|
struct svc_cacherep *rp, *found;
|
2006-10-20 14:29:02 +08:00
|
|
|
__be32 xid = rqstp->rq_xid;
|
|
|
|
u32 proto = rqstp->rq_prot,
|
2005-04-17 06:20:36 +08:00
|
|
|
vers = rqstp->rq_vers,
|
|
|
|
proc = rqstp->rq_proc;
|
|
|
|
unsigned long age;
|
2011-01-25 01:11:02 +08:00
|
|
|
int type = rqstp->rq_cachetype;
|
2005-04-17 06:20:36 +08:00
|
|
|
int rtn;
|
|
|
|
|
|
|
|
rqstp->rq_cacherep = NULL;
|
2013-02-04 21:18:03 +08:00
|
|
|
if (type == RC_NOCACHE) {
|
2005-04-17 06:20:36 +08:00
|
|
|
nfsdstats.rcnocache++;
|
|
|
|
return RC_DOIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&cache_lock);
|
|
|
|
rtn = RC_DOIT;
|
|
|
|
|
2013-01-29 03:41:14 +08:00
|
|
|
rp = nfsd_cache_search(rqstp);
|
2013-02-04 21:18:02 +08:00
|
|
|
if (rp)
|
2013-01-29 03:41:14 +08:00
|
|
|
goto found_entry;
|
2013-02-04 21:18:02 +08:00
|
|
|
|
|
|
|
/* Try to use the first entry on the LRU */
|
|
|
|
if (!list_empty(&lru_head)) {
|
|
|
|
rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
|
|
|
|
if (nfsd_cache_entry_expired(rp) ||
|
2013-02-04 21:18:05 +08:00
|
|
|
num_drc_entries >= max_drc_entries) {
|
|
|
|
lru_put_end(rp);
|
|
|
|
prune_cache_entries();
|
2013-02-04 21:18:02 +08:00
|
|
|
goto setup_entry;
|
2013-02-04 21:18:05 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
rp = nfsd_reply_cache_alloc();
|
|
|
|
if (!rp) {
|
|
|
|
dprintk("nfsd: unable to allocate DRC entry!\n");
|
|
|
|
return RC_DOIT;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-02-04 21:18:02 +08:00
|
|
|
spin_lock(&cache_lock);
|
|
|
|
++num_drc_entries;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must search again just in case someone inserted one
|
|
|
|
* after we dropped the lock above.
|
|
|
|
*/
|
|
|
|
found = nfsd_cache_search(rqstp);
|
|
|
|
if (found) {
|
|
|
|
nfsd_reply_cache_free_locked(rp);
|
|
|
|
rp = found;
|
|
|
|
goto found_entry;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
/*
|
|
|
|
* We're keeping the one we just allocated. Are we now over the
|
|
|
|
* limit? Prune one off the tip of the LRU in trade for the one we
|
|
|
|
* just allocated if so.
|
|
|
|
*/
|
|
|
|
if (num_drc_entries >= max_drc_entries)
|
|
|
|
nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
|
|
|
|
struct svc_cacherep, c_lru));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-02-04 21:18:02 +08:00
|
|
|
setup_entry:
|
|
|
|
nfsdstats.rcmisses++;
|
2005-04-17 06:20:36 +08:00
|
|
|
rqstp->rq_cacherep = rp;
|
|
|
|
rp->c_state = RC_INPROG;
|
|
|
|
rp->c_xid = xid;
|
|
|
|
rp->c_proc = proc;
|
2013-01-29 03:41:07 +08:00
|
|
|
rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
|
|
|
|
rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
|
2005-04-17 06:20:36 +08:00
|
|
|
rp->c_prot = proto;
|
|
|
|
rp->c_vers = vers;
|
|
|
|
|
|
|
|
hash_refile(rp);
|
2013-02-04 21:18:00 +08:00
|
|
|
lru_put_end(rp);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* release any buffer */
|
|
|
|
if (rp->c_type == RC_REPLBUFF) {
|
|
|
|
kfree(rp->c_replvec.iov_base);
|
|
|
|
rp->c_replvec.iov_base = NULL;
|
|
|
|
}
|
|
|
|
rp->c_type = RC_NOCACHE;
|
|
|
|
out:
|
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
return rtn;
|
|
|
|
|
|
|
|
found_entry:
|
2013-02-04 21:18:02 +08:00
|
|
|
nfsdstats.rchits++;
|
2005-04-17 06:20:36 +08:00
|
|
|
/* We found a matching entry which is either in progress or done. */
|
|
|
|
age = jiffies - rp->c_timestamp;
|
|
|
|
lru_put_end(rp);
|
|
|
|
|
|
|
|
rtn = RC_DROPIT;
|
|
|
|
/* Request being processed or excessive rexmits */
|
|
|
|
if (rp->c_state == RC_INPROG || age < RC_DELAY)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* From the hall of fame of impractical attacks:
|
|
|
|
* Is this a user who tries to snoop on the cache? */
|
|
|
|
rtn = RC_DOIT;
|
|
|
|
if (!rqstp->rq_secure && rp->c_secure)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Compose RPC reply header */
|
|
|
|
switch (rp->c_type) {
|
|
|
|
case RC_NOCACHE:
|
|
|
|
break;
|
|
|
|
case RC_REPLSTAT:
|
|
|
|
svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
|
|
|
|
rtn = RC_REPLY;
|
|
|
|
break;
|
|
|
|
case RC_REPLBUFF:
|
|
|
|
if (!nfsd_cache_append(rqstp, &rp->c_replvec))
|
|
|
|
goto out; /* should not happen */
|
|
|
|
rtn = RC_REPLY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
|
2013-02-04 21:18:02 +08:00
|
|
|
nfsd_reply_cache_free_locked(rp);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update a cache entry. This is called from nfsd_dispatch when
|
|
|
|
* the procedure has been executed and the complete reply is in
|
|
|
|
* rqstp->rq_res.
|
|
|
|
*
|
|
|
|
* We're copying around data here rather than swapping buffers because
|
|
|
|
* the toplevel loop requires max-sized buffers, which would be a waste
|
|
|
|
* of memory for a cache with a max reply size of 100 bytes (diropokres).
|
|
|
|
*
|
|
|
|
* If we should start to use different types of cache entries tailored
|
|
|
|
* specifically for attrstat and fh's, we may save even more space.
|
|
|
|
*
|
|
|
|
* Also note that a cachetype of RC_NOCACHE can legally be passed when
|
|
|
|
* nfsd failed to encode a reply that otherwise would have been cached.
|
|
|
|
* In this case, nfsd_cache_update is called with statp == NULL.
|
|
|
|
*/
|
|
|
|
void
|
2006-10-20 14:29:02 +08:00
|
|
|
nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-02-04 21:18:03 +08:00
|
|
|
struct svc_cacherep *rp = rqstp->rq_cacherep;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
|
|
|
|
int len;
|
|
|
|
|
2013-02-04 21:18:03 +08:00
|
|
|
if (!rp)
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
|
|
|
|
len >>= 2;
|
2009-04-01 04:28:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Don't cache excessive amounts of data and XDR failures */
|
|
|
|
if (!statp || len > (256 >> 2)) {
|
2013-02-04 21:18:04 +08:00
|
|
|
nfsd_reply_cache_free(rp);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cachetype) {
|
|
|
|
case RC_REPLSTAT:
|
|
|
|
if (len != 1)
|
|
|
|
printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
|
|
|
|
rp->c_replstat = *statp;
|
|
|
|
break;
|
|
|
|
case RC_REPLBUFF:
|
|
|
|
cachv = &rp->c_replvec;
|
|
|
|
cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
|
|
|
|
if (!cachv->iov_base) {
|
2013-02-04 21:18:04 +08:00
|
|
|
nfsd_reply_cache_free(rp);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cachv->iov_len = len << 2;
|
|
|
|
memcpy(cachv->iov_base, statp, len << 2);
|
|
|
|
break;
|
2013-02-04 21:18:04 +08:00
|
|
|
case RC_NOCACHE:
|
|
|
|
nfsd_reply_cache_free(rp);
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
spin_lock(&cache_lock);
|
|
|
|
lru_put_end(rp);
|
|
|
|
rp->c_secure = rqstp->rq_secure;
|
|
|
|
rp->c_type = cachetype;
|
|
|
|
rp->c_state = RC_DONE;
|
|
|
|
spin_unlock(&cache_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy cached reply to current reply buffer. Should always fit.
|
|
|
|
* FIXME as reply is in a page, we should just attach the page, and
|
|
|
|
* keep a refcount....
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
|
|
|
|
{
|
|
|
|
struct kvec *vec = &rqstp->rq_res.head[0];
|
|
|
|
|
|
|
|
if (vec->iov_len + data->iov_len > PAGE_SIZE) {
|
|
|
|
printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
|
|
|
|
data->iov_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
|
|
|
|
vec->iov_len += data->iov_len;
|
|
|
|
return 1;
|
|
|
|
}
|