// SPDX-License-Identifier: GPL-2.0 /* * 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>
*/
/* * We use this value to determine the number of hash buckets from the max * cache size, the idea being that when the cache is at its maximum number * of entries, then this should be the average number of entries per bucket.
*/ #define TARGET_BUCKET_SIZE 64
/* * 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. * * XXX: these limits are per-container, so memory used will increase * linearly with number of containers. Maybe that's OK.
*/ staticunsignedint
nfsd_cache_size_limit(void)
{ unsignedint limit; unsignedlong low_pages = totalram_pages() - totalhigh_pages();
/* * Compute the number of hash buckets we need. Divide the max cachesize by * the "target" max bucket size, and round up to next power of two.
*/ staticunsignedint
nfsd_hashsize(unsignedint limit)
{ return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
}
for (i = 0; i < hashsize; i++) {
INIT_LIST_HEAD(&nn->drc_hashtbl[i].lru_head);
spin_lock_init(&nn->drc_hashtbl[i].cache_lock);
}
nn->drc_hashsize = hashsize;
/* * Move cache entry to end of LRU list, and queue the cleaner to run if it's * not already scheduled.
*/ staticvoid
lru_put_end(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp)
{
rp->c_timestamp = jiffies;
list_move_tail(&rp->c_lru, &b->lru_head);
}
/* * Remove and return no more than @max expired entries in bucket @b. * If @max is zero, do not limit the number of removed entries.
*/ staticvoid
nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b, unsignedint max, struct list_head *dispose)
{ unsignedlong expiry = jiffies - RC_EXPIRE; struct nfsd_cacherep *rp, *tmp; unsignedint freed = 0;
lockdep_assert_held(&b->cache_lock);
/* The bucket LRU is ordered oldest-first. */
list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) { /* * Don't free entries attached to calls that are still * in-progress, but do keep scanning the list.
*/ if (rp->c_state == RC_INPROG) continue;
if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries &&
time_before(expiry, rp->c_timestamp)) break;
/** * nfsd_reply_cache_count - count_objects method for the DRC shrinker * @shrink: our registered shrinker context * @sc: garbage collection parameters * * Returns the total number of entries in the duplicate reply cache. To * keep things simple and quick, this is not the number of expired entries * in the cache (ie, the number that would be removed by a call to * nfsd_reply_cache_scan).
*/ staticunsignedlong
nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
{ struct nfsd_net *nn = shrink->private_data;
return atomic_read(&nn->num_drc_entries);
}
/** * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker * @shrink: our registered shrinker context * @sc: garbage collection parameters * * Free expired entries on each bucket's LRU list until we've released * nr_to_scan freed objects. Nothing will be released if the cache * has not exceeded it's max_drc_entries limit. * * Returns the number of entries released by this call.
*/ staticunsignedlong
nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
{ struct nfsd_net *nn = shrink->private_data; unsignedlong freed = 0;
LIST_HEAD(dispose); unsignedint i;
for (i = 0; i < nn->drc_hashsize; i++) { struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i];
/** * nfsd_cache_csum - Checksum incoming NFS Call arguments * @buf: buffer containing a whole RPC Call message * @start: starting byte of the NFS Call header * @remaining: size of the NFS Call header, in bytes * * Compute a weak checksum of the leading bytes of an NFS procedure * call header to help verify that a retransmitted Call matches an * entry in the duplicate reply cache. * * To avoid assumptions about how the RPC message is laid out in * @buf and what else it might contain (eg, a GSS MIC suffix), the * caller passes us the exact location and length of the NFS Call * header. * * Returns a 32-bit checksum value, as defined in RFC 793.
*/ static __wsum nfsd_cache_csum(struct xdr_buf *buf, unsignedint start, unsignedint remaining)
{ unsignedint base, len; struct xdr_buf subbuf;
__wsum csum = 0; void *p; int idx;
if (remaining > RC_CSUMLEN)
remaining = RC_CSUMLEN; if (xdr_buf_subsegment(buf, &subbuf, start, remaining)) return csum;
/* rq_arg.head first */ if (subbuf.head[0].iov_len) {
len = min_t(unsignedint, subbuf.head[0].iov_len, remaining);
csum = csum_partial(subbuf.head[0].iov_base, len, csum);
remaining -= len;
}
/* Continue into page array */
idx = subbuf.page_base / PAGE_SIZE;
base = subbuf.page_base & ~PAGE_MASK; while (remaining) {
p = page_address(subbuf.pages[idx]) + base;
len = min_t(unsignedint, PAGE_SIZE - base, remaining);
csum = csum_partial(p, len, csum);
remaining -= len;
base = 0;
++idx;
} return csum;
}
/* * Search the request hash for an entry that matches the given rqstp. * Must be called with cache_lock held. Returns the found entry or * inserts an empty key on failure.
*/ staticstruct nfsd_cacherep *
nfsd_cache_insert(struct nfsd_drc_bucket *b, struct nfsd_cacherep *key, struct nfsd_net *nn)
{ struct nfsd_cacherep *rp, *ret = key; struct rb_node **p = &b->rb_head.rb_node,
*parent = NULL; unsignedint entries = 0; int cmp;
while (*p != NULL) {
++entries;
parent = *p;
rp = rb_entry(parent, struct nfsd_cacherep, c_node);
cmp = nfsd_cache_key_cmp(key, rp, nn); if (cmp < 0)
p = &parent->rb_left; elseif (cmp > 0)
p = &parent->rb_right; else {
ret = rp; goto out;
}
}
rb_link_node(&key->c_node, parent, p);
rb_insert_color(&key->c_node, &b->rb_head);
out: /* tally hash chain length stats */ if (entries > nn->longest_chain) {
nn->longest_chain = entries;
nn->longest_chain_cachesize = atomic_read(&nn->num_drc_entries);
} elseif (entries == nn->longest_chain) { /* prefer to keep the smallest cachesize possible here */
nn->longest_chain_cachesize = min_t(unsignedint,
nn->longest_chain_cachesize,
atomic_read(&nn->num_drc_entries));
}
lru_put_end(b, ret); return ret;
}
/** * nfsd_cache_lookup - Find an entry in the duplicate reply cache * @rqstp: Incoming Call to find * @start: starting byte in @rqstp->rq_arg of the NFS Call header * @len: size of the NFS Call header, in bytes * @cacherep: OUT: DRC entry for this request * * Try to find an entry matching the current call in the cache. When none * is found, we try to grab the oldest expired entry off the LRU list. If * a suitable one isn't there, then drop the cache_lock and allocate a * new one, then search again in case one got inserted while this thread * didn't hold the lock. * * Return values: * %RC_DOIT: Process the request normally * %RC_REPLY: Reply from cache * %RC_DROPIT: Do not process the request further
*/ int nfsd_cache_lookup(struct svc_rqst *rqstp, unsignedint start, unsignedint len, struct nfsd_cacherep **cacherep)
{ struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); struct nfsd_cacherep *rp, *found;
__wsum csum; struct nfsd_drc_bucket *b; int type = rqstp->rq_cachetype;
LIST_HEAD(dispose); int rtn = RC_DOIT;
if (type == RC_NOCACHE) {
nfsd_stats_rc_nocache_inc(nn); goto out;
}
/* * Since the common case is a cache miss followed by an insert, * preallocate an entry.
*/
rp = nfsd_cacherep_alloc(rqstp, csum, nn); if (!rp) goto out;
b = nfsd_cache_bucket_find(rqstp->rq_xid, nn);
spin_lock(&b->cache_lock);
found = nfsd_cache_insert(b, rp, nn); if (found != rp) goto found_entry;
*cacherep = rp;
rp->c_state = RC_INPROG;
nfsd_prune_bucket_locked(nn, b, 3, &dispose);
spin_unlock(&b->cache_lock);
found_entry: /* We found a matching entry which is either in progress or done. */
nfsd_reply_cache_free_locked(NULL, rp, nn);
nfsd_stats_rc_hits_inc(nn);
rtn = RC_DROPIT;
rp = found;
/* Request being processed */ if (rp->c_state == RC_INPROG) goto out_trace;
/* From the hall of fame of impractical attacks:
* Is this a user who tries to snoop on the cache? */
rtn = RC_DOIT; if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure) goto out_trace;
/* Compose RPC reply header */ switch (rp->c_type) { case RC_NOCACHE: break; case RC_REPLSTAT:
xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat);
rtn = RC_REPLY; break; case RC_REPLBUFF: if (!nfsd_cache_append(rqstp, &rp->c_replvec)) goto out_unlock; /* should not happen */
rtn = RC_REPLY; break; default:
WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type);
}
/** * nfsd_cache_update - Update an entry in the duplicate reply cache. * @rqstp: svc_rqst with a finished Reply * @rp: IN: DRC entry for this request * @cachetype: which cache to update * @statp: pointer to Reply's NFS status code, or NULL * * 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 nfsd_cache_update(struct svc_rqst *rqstp, struct nfsd_cacherep *rp, int cachetype, __be32 *statp)
{ struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); struct kvec *resv = &rqstp->rq_res.head[0], *cachv; struct nfsd_drc_bucket *b; int len;
size_t bufsize = 0;
if (!rp) return;
b = nfsd_cache_bucket_find(rp->c_key.k_xid, nn);
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)) {
nfsd_reply_cache_free(b, rp, nn); return;
}
p = xdr_reserve_space(&rqstp->rq_res_stream, data->iov_len); if (unlikely(!p)) returnfalse;
memcpy(p, data->iov_base, data->iov_len);
xdr_commit_encode(&rqstp->rq_res_stream); returntrue;
}
/* * Note that fields may be added, removed or reordered in the future. Programs * scraping this file for info should test the labels to ensure they're * getting the correct field.
*/ int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
{ struct nfsd_net *nn = net_generic(file_inode(m->file)->i_sb->s_fs_info,
nfsd_net_id);
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.