Commit 9417bd87 authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] kNFSd: Allow sunrpc/svc cache init function to modify the "key"

From: NeilBrown <neilb@cse.unsw.edu.au>

When adding a item to a sunrpc/svc cache that contains kmalloced data it is
usefully to move the malloced data out of the key object into the new cache
object rather than copying (as then we would need to cope with kmalloc
failure and such).  This means modifying the original.

If the kmalloced data forms part of the key, then we must not move the data
out until after the key isn't needed any more.  So this patch moves the
call to "INIT" on a new item (which fills in the key) to *after* the item
has been found (or not), and also makes sure we only call the HASH function
once.

Thanks to "J.  Bruce Fields" <bfields@fieldses.org>

also

 1/ remove unnecessary assignment
 2/ fix comments that lag behind implementation.
parent 16b82dca
...@@ -132,12 +132,14 @@ struct cache_deferred_req { ...@@ -132,12 +132,14 @@ struct cache_deferred_req {
* If "set" == 0 : * If "set" == 0 :
* If an entry is found, it is returned * If an entry is found, it is returned
* If no entry is found, a new non-VALID entry is created. * If no entry is found, a new non-VALID entry is created.
* If "set" == 1 : * If "set" == 1 and INPLACE == 0 :
* If no entry is found a new one is inserted with data from "template" * If no entry is found a new one is inserted with data from "template"
* If a non-CACHE_VALID entry is found, it is updated from template using UPDATE * If a non-CACHE_VALID entry is found, it is updated from template using UPDATE
* If a CACHE_VALID entry is found, a new entry is swapped in with data * If a CACHE_VALID entry is found, a new entry is swapped in with data
* from "template" * from "template"
* If set == 2, we UPDATE, but don't swap. i.e. update in place * If set == 1, and INPLACE == 1 :
* As above, except that if a CACHE_VALID entry is found, we UPDATE in place
* instead of swapping in a new entry.
* *
* If the passed handle has the CACHE_NEGATIVE flag set, then UPDATE is not * If the passed handle has the CACHE_NEGATIVE flag set, then UPDATE is not
* run but insteead CACHE_NEGATIVE is set in any new item. * run but insteead CACHE_NEGATIVE is set in any new item.
...@@ -164,8 +166,8 @@ RTN *FNAME ARGS \ ...@@ -164,8 +166,8 @@ RTN *FNAME ARGS \
RTN *tmp, *new=NULL; \ RTN *tmp, *new=NULL; \
struct cache_head **hp, **head; \ struct cache_head **hp, **head; \
SETUP; \ SETUP; \
retry: \
head = &(DETAIL)->hash_table[HASHFN]; \ head = &(DETAIL)->hash_table[HASHFN]; \
retry: \
if (set||new) write_lock(&(DETAIL)->hash_lock); \ if (set||new) write_lock(&(DETAIL)->hash_lock); \
else read_lock(&(DETAIL)->hash_lock); \ else read_lock(&(DETAIL)->hash_lock); \
for(hp=head; *hp != NULL; hp = &tmp->MEMBER.next) { \ for(hp=head; *hp != NULL; hp = &tmp->MEMBER.next) { \
...@@ -175,6 +177,8 @@ RTN *FNAME ARGS \ ...@@ -175,6 +177,8 @@ RTN *FNAME ARGS \
if (set && !INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags) && !new) \ if (set && !INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags) && !new) \
break; \ break; \
\ \
if (new) \
{INIT;} \
cache_get(&tmp->MEMBER); \ cache_get(&tmp->MEMBER); \
if (set) { \ if (set) { \
if (!INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags))\ if (!INPLACE && test_bit(CACHE_VALID, &tmp->MEMBER.flags))\
...@@ -203,6 +207,7 @@ RTN *FNAME ARGS \ ...@@ -203,6 +207,7 @@ RTN *FNAME ARGS \
} \ } \
/* Didn't find anything */ \ /* Didn't find anything */ \
if (new) { \ if (new) { \
INIT; \
new->MEMBER.next = *head; \ new->MEMBER.next = *head; \
*head = &new->MEMBER; \ *head = &new->MEMBER; \
(DETAIL)->entries ++; \ (DETAIL)->entries ++; \
...@@ -224,8 +229,6 @@ RTN *FNAME ARGS \ ...@@ -224,8 +229,6 @@ RTN *FNAME ARGS \
if (new) { \ if (new) { \
cache_init(&new->MEMBER); \ cache_init(&new->MEMBER); \
cache_get(&new->MEMBER); \ cache_get(&new->MEMBER); \
INIT; \
tmp = new; \
goto retry; \ goto retry; \
} \ } \
return NULL; \ return NULL; \
......
...@@ -150,7 +150,13 @@ DefineCacheLookup(struct auth_domain, ...@@ -150,7 +150,13 @@ DefineCacheLookup(struct auth_domain,
&auth_domain_cache, &auth_domain_cache,
auth_domain_hash(item), auth_domain_hash(item),
auth_domain_match(tmp, item), auth_domain_match(tmp, item),
kfree(new); if(!set) return NULL; kfree(new); if(!set) {
if (new)
write_unlock(&auth_domain_cache.hash_lock);
else
read_unlock(&auth_domain_cache.hash_lock);
return NULL;
}
new=item; atomic_inc(&new->h.refcnt), new=item; atomic_inc(&new->h.refcnt),
/* no update */, /* no update */,
0 /* no inplace updates */ 0 /* no inplace updates */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment