// SPDX-License-Identifier: GPL-2.0-only /* * net/sunrpc/cache.c * * Generic code for various authentication-related caches * used by sunrpc clients and servers. * * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
*/
new = detail->alloc(); if (!new) return NULL; /* must fully initialise 'new', else * we might get lose if we need to * cache_put it soon.
*/
cache_init(new, detail);
detail->init(new, key);
spin_lock(&detail->hash_lock);
/* check if entry appeared while we slept */
hlist_for_each_entry_rcu(tmp, head, cache_list,
lockdep_is_held(&detail->hash_lock)) { if (!detail->match(tmp, key)) continue; if (test_bit(CACHE_VALID, &tmp->flags) &&
cache_is_expired(detail, tmp)) {
sunrpc_begin_cache_remove_entry(tmp, detail);
trace_cache_entry_expired(detail, tmp);
freeme = tmp; break;
}
cache_get(tmp);
spin_unlock(&detail->hash_lock);
cache_put(new, detail); return tmp;
}
struct cache_head *sunrpc_cache_update(struct cache_detail *detail, struct cache_head *new, struct cache_head *old, int hash)
{ /* The 'old' entry is to be replaced by 'new'. * If 'old' is not VALID, we update it directly, * otherwise we need to replace it
*/ struct cache_head *tmp;
if (!test_bit(CACHE_VALID, &old->flags)) {
spin_lock(&detail->hash_lock); if (!test_bit(CACHE_VALID, &old->flags)) {
cache_entry_update(detail, old, new);
cache_fresh_locked(old, new->expiry_time, detail);
spin_unlock(&detail->hash_lock);
cache_fresh_unlocked(old, detail); return old;
}
spin_unlock(&detail->hash_lock);
} /* We need to insert a new entry */
tmp = detail->alloc(); if (!tmp) {
cache_put(old, detail); return NULL;
}
cache_init(tmp, detail);
detail->init(tmp, old);
staticinlineint cache_is_valid(struct cache_head *h)
{ if (!test_bit(CACHE_VALID, &h->flags)) return -EAGAIN; else { /* entry is valid */ if (test_bit(CACHE_NEGATIVE, &h->flags)) return -ENOENT; else { /* * In combination with write barrier in * sunrpc_cache_update, ensures that anyone * using the cache entry after this sees the * updated contents:
*/
smp_rmb(); return 0;
}
}
}
staticint try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
{ int rv;
if (rv == -EAGAIN) { if (!cache_defer_req(rqstp, h)) { /* * Request was not deferred; handle it as best * we can ourselves:
*/
rv = cache_is_valid(h); if (rv == -EAGAIN)
rv = -ETIMEDOUT;
}
}
return rv;
}
EXPORT_SYMBOL_GPL(cache_check_rcu);
/* * This is the generic cache management routine for all * the authentication caches. * It checks the currency of a cache item and will (later) * initiate an upcall to fill it if needed. * * * Returns 0 if the cache_head can be used, or cache_puts it and returns * -EAGAIN if upcall is pending and request has been queued * -ETIMEDOUT if upcall failed or request could not be queue or * upcall completed but item is still invalid (implying that * the cache item has been replaced with a newer one). * -ENOENT if cache entry was negative
*/ int cache_check(struct cache_detail *detail, struct cache_head *h, struct cache_req *rqstp)
{ int rv;
/* * caches need to be periodically cleaned. * For this we maintain a list of cache_detail and * a current pointer into that list and into the table * for that entry. * * Each time cache_clean is called it finds the next non-empty entry * in the current table and walks the list in that entry * looking for entries that can be removed. * * An entry gets removed if: * - The expiry is before current time * - The last_refresh time is before the flush_time for that cache * * later we might drop old entries with non-NEVER expiry if that table * is getting 'full' for some definition of 'full' * * The question of "how often to scan a table" is an interesting one * and is answered in part by the use of the "nextcheck" field in the * cache_detail. * When a scan of a table begins, the nextcheck field is set to a time * that is well into the future. * While scanning, if an expiry time is found that is earlier than the * current nextcheck time, nextcheck is set to that expiry time. * If the flush_time is ever set to a time earlier than the nextcheck * time, the nextcheck time is then set to that flush_time. * * A table is then only scanned if the current time is at least * the nextcheck time. *
*/
/* start the cleaning process */
queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
}
EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
void sunrpc_destroy_cache_detail(struct cache_detail *cd)
{
cache_purge(cd);
spin_lock(&cache_list_lock);
spin_lock(&cd->hash_lock); if (current_detail == cd)
current_detail = NULL;
list_del_init(&cd->others);
spin_unlock(&cd->hash_lock);
spin_unlock(&cache_list_lock); if (list_empty(&cache_list)) { /* module must be being unloaded so its safe to kill the worker */
cancel_delayed_work_sync(&cache_cleaner);
}
}
EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
/* clean cache tries to find something to clean * and cleans it. * It returns 1 if it cleaned something, * 0 if it didn't find anything this time * -1 if it fell off the end of the list.
*/ staticint cache_clean(void)
{ int rv = 0; struct list_head *next;
spin_lock(&cache_list_lock);
/* find a suitable table if we don't already have one */ while (current_detail == NULL ||
current_index >= current_detail->hash_size) { if (current_detail)
next = current_detail->others.next; else
next = cache_list.next; if (next == &cache_list) {
current_detail = NULL;
spin_unlock(&cache_list_lock); return -1;
}
current_detail = list_entry(next, struct cache_detail, others); if (current_detail->nextcheck > seconds_since_boot())
current_index = current_detail->hash_size; else {
current_index = 0;
current_detail->nextcheck = seconds_since_boot()+30*60;
}
}
spin_lock(¤t_detail->hash_lock);
/* find a non-empty bucket in the table */ while (current_index < current_detail->hash_size &&
hlist_empty(¤t_detail->hash_table[current_index]))
current_index++;
/* find a cleanable entry in the bucket and clean it, or set to next bucket */ if (current_index < current_detail->hash_size) { struct cache_head *ch = NULL; struct cache_detail *d; struct hlist_head *head; struct hlist_node *tmp;
/* Ok, now to clean this strand */
head = ¤t_detail->hash_table[current_index];
hlist_for_each_entry_safe(ch, tmp, head, cache_list) { if (current_detail->nextcheck > ch->expiry_time)
current_detail->nextcheck = ch->expiry_time+1; if (!cache_is_expired(current_detail, ch)) continue;
/* * Clean all caches promptly. This just calls cache_clean * repeatedly until we are sure that every cache has had a chance to * be fully cleaned
*/ void cache_flush(void)
{ while (cache_clean() != -1)
cond_resched(); while (cache_clean() != -1)
cond_resched();
}
EXPORT_SYMBOL_GPL(cache_flush);
void cache_purge(struct cache_detail *detail)
{ struct cache_head *ch = NULL; struct hlist_head *head = NULL; int i = 0;
spin_lock(&detail->hash_lock); if (!detail->entries) {
spin_unlock(&detail->hash_lock); return;
}
dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name); for (i = 0; i < detail->hash_size; i++) {
head = &detail->hash_table[i]; while (!hlist_empty(head)) {
ch = hlist_entry(head->first, struct cache_head,
cache_list);
sunrpc_begin_cache_remove_entry(ch, detail);
spin_unlock(&detail->hash_lock);
sunrpc_end_cache_remove_entry(ch, detail);
spin_lock(&detail->hash_lock);
}
}
spin_unlock(&detail->hash_lock);
}
EXPORT_SYMBOL_GPL(cache_purge);
/* * Deferral and Revisiting of Requests. * * If a cache lookup finds a pending entry, we * need to defer the request and revisit it later. * All deferred requests are stored in a hash table, * indexed by "struct cache_head *". * As it may be wasteful to store a whole request * structure, we allow the request to provide a * deferred form, which must contain a * 'struct cache_deferred_req' * This cache_deferred_req contains a method to allow * it to be revisited when cache info is available
*/
if (!test_bit(CACHE_PENDING, &item->flags) ||
wait_for_completion_interruptible_timeout(
&sleeper.completion, req->thread_wait) <= 0) { /* The completion wasn't completed, so we need * to clean up
*/
spin_lock(&cache_defer_lock); if (!hlist_unhashed(&sleeper.handle.hash)) {
__unhash_deferred_req(&sleeper.handle);
spin_unlock(&cache_defer_lock);
} else { /* cache_revisit_request already removed * this from the hash table, but hasn't * called ->revisit yet. It will very soon * and we need to wait for it.
*/
spin_unlock(&cache_defer_lock);
wait_for_completion(&sleeper.completion);
}
}
}
staticvoid cache_limit_defers(void)
{ /* Make sure we haven't exceed the limit of allowed deferred * requests.
*/ struct cache_deferred_req *discard = NULL;
if (cache_defer_cnt <= DFR_MAX) return;
spin_lock(&cache_defer_lock);
/* Consider removing either the first or the last */ if (cache_defer_cnt > DFR_MAX) { if (get_random_u32_below(2))
discard = list_entry(cache_defer_list.next, struct cache_deferred_req, recent); else
discard = list_entry(cache_defer_list.prev, struct cache_deferred_req, recent);
__unhash_deferred_req(discard);
}
spin_unlock(&cache_defer_lock); if (discard)
discard->revisit(discard, 1);
}
/* Return true if and only if a deferred request is queued. */ staticbool cache_defer_req(struct cache_req *req, struct cache_head *item)
{ struct cache_deferred_req *dreq;
if (!cache_defer_immediately()) {
cache_wait_req(req, item); if (!test_bit(CACHE_PENDING, &item->flags)) returnfalse;
}
dreq = req->defer(req); if (dreq == NULL) returnfalse;
setup_deferral(dreq, item, 1); if (!test_bit(CACHE_PENDING, &item->flags)) /* Bit could have been cleared before we managed to * set up the deferral, so need to revisit just in case
*/
cache_revisit_request(item);
/* * communicate with user-space * * We have a magic /proc file - /proc/net/rpc/<cachename>/channel. * On read, you get a full request, or block. * On write, an update request is processed. * Poll works if anything to read, and always allows write. * * Implemented by linked list of requests. Each open file has * a ->private that also exists in this list. New requests are added * to the end and may wakeup and preceding readers. * New readers are added to the head. If, on read, an item is found with * CACHE_UPCALLING clear, we free it from the list. *
*/
static DEFINE_SPINLOCK(queue_lock);
struct cache_queue { struct list_head list; int reader; /* if 0, then request */
}; struct cache_request { struct cache_queue q; struct cache_head *item; char * buf; int len; int readers;
}; struct cache_reader { struct cache_queue q; int offset; /* if non-0, we have a refcnt on next request */
};
staticint cache_request(struct cache_detail *detail, struct cache_request *crq)
{ char *bp = crq->buf; int len = PAGE_SIZE;
/* only find the length remaining in current request, * or the length of the next request
*/ for (cq= &rp->q; &cq->list != &cd->queue;
cq = list_entry(cq->list.next, struct cache_queue, list)) if (!cq->reader) { struct cache_request *cr =
container_of(cq, struct cache_request, q);
len = cr->len - rp->offset; break;
}
spin_unlock(&queue_lock);
spin_lock(&queue_lock);
list_for_each_entry_safe(cq, tmp, &detail->queue, list) if (!cq->reader) {
cr = container_of(cq, struct cache_request, q); if (cr->item != ch) continue; if (test_bit(CACHE_PENDING, &ch->flags)) /* Lost a race and it is pending again */ break; if (cr->readers != 0) continue;
list_move(&cr->q.list, &dequeued);
}
spin_unlock(&queue_lock); while (!list_empty(&dequeued)) {
cr = list_entry(dequeued.next, struct cache_request, q.list);
list_del(&cr->q.list);
cache_put(cr->item, detail);
kfree(cr->buf);
kfree(cr);
}
}
/* * Support routines for text-based upcalls. * Fields are separated by spaces. * Fields are either mangled to quote space tab newline slosh with slosh * or a hexified with a leading \x * Record is terminated with newline. *
*/
void qword_add(char **bpp, int *lp, char *str)
{ char *bp = *bpp; int len = *lp; int ret;
if (len < 0) return;
ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t"); if (ret >= len) {
bp += len;
len = -1;
} else {
bp += ret;
len -= ret;
*bp++ = ' ';
len--;
}
*bpp = bp;
*lp = len;
}
EXPORT_SYMBOL_GPL(qword_add);
void qword_addhex(char **bpp, int *lp, char *buf, int blen)
{ char *bp = *bpp; int len = *lp;
if (len < 0) return;
if (len > 2) {
*bp++ = '\\';
*bp++ = 'x';
len -= 2; while (blen && len >= 2) {
bp = hex_byte_pack(bp, *buf++);
len -= 2;
blen--;
}
} if (blen || len<1) len = -1; else {
*bp++ = ' ';
len--;
}
*bpp = bp;
*lp = len;
}
EXPORT_SYMBOL_GPL(qword_addhex);
staticbool cache_listeners_exist(struct cache_detail *detail)
{ if (atomic_read(&detail->writers)) returntrue; if (detail->last_close == 0) /* This cache was never opened */ returnfalse; if (detail->last_close < seconds_since_boot() - 30) /* * We allow for the possibility that someone might * restart a userspace daemon without restarting the * server; but after 30 seconds, we give up.
*/ returnfalse; returntrue;
}
/* * register an upcall request to user-space and queue it up for read() by the * upcall daemon. * * Each request is at most one page long.
*/ staticint cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
{ char *buf; struct cache_request *crq; int ret = 0;
if (test_bit(CACHE_CLEANED, &h->flags)) /* Too late to make an upcall */ return -EAGAIN;
buf = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!buf) return -EAGAIN;
/* * parse a message from user-space and pass it * to an appropriate cache * Messages are, like requests, separated into fields by * spaces and dequotes as \xHEXSTRING or embedded \nnn octal * * Message is * reply cachename expiry key ... content.... * * key and content are both parsed by cache
*/
int qword_get(char **bpp, char *dest, int bufsize)
{ /* return bytes copied, or -1 on error */ char *bp = *bpp; int len = 0;
while (*bp == ' ') bp++;
if (bp[0] == '\\' && bp[1] == 'x') { /* HEX STRING */
bp += 2; while (len < bufsize - 1) { int h, l;
/* * support /proc/net/rpc/$CACHENAME/content * as a seqfile. * We call ->cache_show passing NULL for the item to * get a header, then pass each real item in the cache
*/
if (*ppos || count > sizeof(tbuf)-1) return -EINVAL; if (copy_from_user(tbuf, buf, count)) return -EFAULT;
tbuf[count] = 0;
simple_strtoul(tbuf, &ep, 0); if (*ep && *ep != '\n') return -EINVAL; /* Note that while we check that 'buf' holds a valid number, * we always ignore the value and just flush everything. * Making use of the number leads to races.
*/
now = seconds_since_boot(); /* Always flush everything, so behave like cache_purge() * Do this by advancing flush_time to the current time, * or by one second if it has already reached the current time. * Newly added cache entries will always have ->last_refresh greater * that ->flush_time, so they don't get flushed prematurely.
*/
if (cd->flush_time >= now)
now = cd->flush_time + 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.