18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * net/sunrpc/cache.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Generic code for various authentication-related caches
68c2ecf20Sopenharmony_ci * used by sunrpc clients and servers.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/fs.h>
138c2ecf20Sopenharmony_ci#include <linux/file.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/signal.h>
168c2ecf20Sopenharmony_ci#include <linux/sched.h>
178c2ecf20Sopenharmony_ci#include <linux/kmod.h>
188c2ecf20Sopenharmony_ci#include <linux/list.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/ctype.h>
218c2ecf20Sopenharmony_ci#include <linux/string_helpers.h>
228c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
238c2ecf20Sopenharmony_ci#include <linux/poll.h>
248c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
258c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
268c2ecf20Sopenharmony_ci#include <linux/net.h>
278c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
288c2ecf20Sopenharmony_ci#include <linux/mutex.h>
298c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
308c2ecf20Sopenharmony_ci#include <asm/ioctls.h>
318c2ecf20Sopenharmony_ci#include <linux/sunrpc/types.h>
328c2ecf20Sopenharmony_ci#include <linux/sunrpc/cache.h>
338c2ecf20Sopenharmony_ci#include <linux/sunrpc/stats.h>
348c2ecf20Sopenharmony_ci#include <linux/sunrpc/rpc_pipe_fs.h>
358c2ecf20Sopenharmony_ci#include <trace/events/sunrpc.h>
368c2ecf20Sopenharmony_ci#include "netns.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define	 RPCDBG_FACILITY RPCDBG_CACHE
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic bool cache_defer_req(struct cache_req *req, struct cache_head *item);
418c2ecf20Sopenharmony_cistatic void cache_revisit_request(struct cache_head *item);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void cache_init(struct cache_head *h, struct cache_detail *detail)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	time64_t now = seconds_since_boot();
468c2ecf20Sopenharmony_ci	INIT_HLIST_NODE(&h->cache_list);
478c2ecf20Sopenharmony_ci	h->flags = 0;
488c2ecf20Sopenharmony_ci	kref_init(&h->ref);
498c2ecf20Sopenharmony_ci	h->expiry_time = now + CACHE_NEW_EXPIRY;
508c2ecf20Sopenharmony_ci	if (now <= detail->flush_time)
518c2ecf20Sopenharmony_ci		/* ensure it isn't already expired */
528c2ecf20Sopenharmony_ci		now = detail->flush_time + 1;
538c2ecf20Sopenharmony_ci	h->last_refresh = now;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void cache_fresh_unlocked(struct cache_head *head,
578c2ecf20Sopenharmony_ci				struct cache_detail *detail);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
608c2ecf20Sopenharmony_ci						struct cache_head *key,
618c2ecf20Sopenharmony_ci						int hash)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct hlist_head *head = &detail->hash_table[hash];
648c2ecf20Sopenharmony_ci	struct cache_head *tmp;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	rcu_read_lock();
678c2ecf20Sopenharmony_ci	hlist_for_each_entry_rcu(tmp, head, cache_list) {
688c2ecf20Sopenharmony_ci		if (!detail->match(tmp, key))
698c2ecf20Sopenharmony_ci			continue;
708c2ecf20Sopenharmony_ci		if (test_bit(CACHE_VALID, &tmp->flags) &&
718c2ecf20Sopenharmony_ci		    cache_is_expired(detail, tmp))
728c2ecf20Sopenharmony_ci			continue;
738c2ecf20Sopenharmony_ci		tmp = cache_get_rcu(tmp);
748c2ecf20Sopenharmony_ci		rcu_read_unlock();
758c2ecf20Sopenharmony_ci		return tmp;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci	rcu_read_unlock();
788c2ecf20Sopenharmony_ci	return NULL;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void sunrpc_begin_cache_remove_entry(struct cache_head *ch,
828c2ecf20Sopenharmony_ci					    struct cache_detail *cd)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	/* Must be called under cd->hash_lock */
858c2ecf20Sopenharmony_ci	hlist_del_init_rcu(&ch->cache_list);
868c2ecf20Sopenharmony_ci	set_bit(CACHE_CLEANED, &ch->flags);
878c2ecf20Sopenharmony_ci	cd->entries --;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void sunrpc_end_cache_remove_entry(struct cache_head *ch,
918c2ecf20Sopenharmony_ci					  struct cache_detail *cd)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	cache_fresh_unlocked(ch, cd);
948c2ecf20Sopenharmony_ci	cache_put(ch, cd);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
988c2ecf20Sopenharmony_ci						 struct cache_head *key,
998c2ecf20Sopenharmony_ci						 int hash)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct cache_head *new, *tmp, *freeme = NULL;
1028c2ecf20Sopenharmony_ci	struct hlist_head *head = &detail->hash_table[hash];
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	new = detail->alloc();
1058c2ecf20Sopenharmony_ci	if (!new)
1068c2ecf20Sopenharmony_ci		return NULL;
1078c2ecf20Sopenharmony_ci	/* must fully initialise 'new', else
1088c2ecf20Sopenharmony_ci	 * we might get lose if we need to
1098c2ecf20Sopenharmony_ci	 * cache_put it soon.
1108c2ecf20Sopenharmony_ci	 */
1118c2ecf20Sopenharmony_ci	cache_init(new, detail);
1128c2ecf20Sopenharmony_ci	detail->init(new, key);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	spin_lock(&detail->hash_lock);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* check if entry appeared while we slept */
1178c2ecf20Sopenharmony_ci	hlist_for_each_entry_rcu(tmp, head, cache_list,
1188c2ecf20Sopenharmony_ci				 lockdep_is_held(&detail->hash_lock)) {
1198c2ecf20Sopenharmony_ci		if (!detail->match(tmp, key))
1208c2ecf20Sopenharmony_ci			continue;
1218c2ecf20Sopenharmony_ci		if (test_bit(CACHE_VALID, &tmp->flags) &&
1228c2ecf20Sopenharmony_ci		    cache_is_expired(detail, tmp)) {
1238c2ecf20Sopenharmony_ci			sunrpc_begin_cache_remove_entry(tmp, detail);
1248c2ecf20Sopenharmony_ci			trace_cache_entry_expired(detail, tmp);
1258c2ecf20Sopenharmony_ci			freeme = tmp;
1268c2ecf20Sopenharmony_ci			break;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci		cache_get(tmp);
1298c2ecf20Sopenharmony_ci		spin_unlock(&detail->hash_lock);
1308c2ecf20Sopenharmony_ci		cache_put(new, detail);
1318c2ecf20Sopenharmony_ci		return tmp;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	hlist_add_head_rcu(&new->cache_list, head);
1358c2ecf20Sopenharmony_ci	detail->entries++;
1368c2ecf20Sopenharmony_ci	cache_get(new);
1378c2ecf20Sopenharmony_ci	spin_unlock(&detail->hash_lock);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (freeme)
1408c2ecf20Sopenharmony_ci		sunrpc_end_cache_remove_entry(freeme, detail);
1418c2ecf20Sopenharmony_ci	return new;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistruct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
1458c2ecf20Sopenharmony_ci					   struct cache_head *key, int hash)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct cache_head *ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	ret = sunrpc_cache_find_rcu(detail, key, hash);
1508c2ecf20Sopenharmony_ci	if (ret)
1518c2ecf20Sopenharmony_ci		return ret;
1528c2ecf20Sopenharmony_ci	/* Didn't find anything, insert an empty entry */
1538c2ecf20Sopenharmony_ci	return sunrpc_cache_add_entry(detail, key, hash);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void cache_fresh_locked(struct cache_head *head, time64_t expiry,
1608c2ecf20Sopenharmony_ci			       struct cache_detail *detail)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	time64_t now = seconds_since_boot();
1638c2ecf20Sopenharmony_ci	if (now <= detail->flush_time)
1648c2ecf20Sopenharmony_ci		/* ensure it isn't immediately treated as expired */
1658c2ecf20Sopenharmony_ci		now = detail->flush_time + 1;
1668c2ecf20Sopenharmony_ci	head->expiry_time = expiry;
1678c2ecf20Sopenharmony_ci	head->last_refresh = now;
1688c2ecf20Sopenharmony_ci	smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
1698c2ecf20Sopenharmony_ci	set_bit(CACHE_VALID, &head->flags);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void cache_fresh_unlocked(struct cache_head *head,
1738c2ecf20Sopenharmony_ci				 struct cache_detail *detail)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
1768c2ecf20Sopenharmony_ci		cache_revisit_request(head);
1778c2ecf20Sopenharmony_ci		cache_dequeue(detail, head);
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic void cache_make_negative(struct cache_detail *detail,
1828c2ecf20Sopenharmony_ci				struct cache_head *h)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	set_bit(CACHE_NEGATIVE, &h->flags);
1858c2ecf20Sopenharmony_ci	trace_cache_entry_make_negative(detail, h);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic void cache_entry_update(struct cache_detail *detail,
1898c2ecf20Sopenharmony_ci			       struct cache_head *h,
1908c2ecf20Sopenharmony_ci			       struct cache_head *new)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	if (!test_bit(CACHE_NEGATIVE, &new->flags)) {
1938c2ecf20Sopenharmony_ci		detail->update(h, new);
1948c2ecf20Sopenharmony_ci		trace_cache_entry_update(detail, h);
1958c2ecf20Sopenharmony_ci	} else {
1968c2ecf20Sopenharmony_ci		cache_make_negative(detail, h);
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistruct cache_head *sunrpc_cache_update(struct cache_detail *detail,
2018c2ecf20Sopenharmony_ci				       struct cache_head *new, struct cache_head *old, int hash)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	/* The 'old' entry is to be replaced by 'new'.
2048c2ecf20Sopenharmony_ci	 * If 'old' is not VALID, we update it directly,
2058c2ecf20Sopenharmony_ci	 * otherwise we need to replace it
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci	struct cache_head *tmp;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (!test_bit(CACHE_VALID, &old->flags)) {
2108c2ecf20Sopenharmony_ci		spin_lock(&detail->hash_lock);
2118c2ecf20Sopenharmony_ci		if (!test_bit(CACHE_VALID, &old->flags)) {
2128c2ecf20Sopenharmony_ci			cache_entry_update(detail, old, new);
2138c2ecf20Sopenharmony_ci			cache_fresh_locked(old, new->expiry_time, detail);
2148c2ecf20Sopenharmony_ci			spin_unlock(&detail->hash_lock);
2158c2ecf20Sopenharmony_ci			cache_fresh_unlocked(old, detail);
2168c2ecf20Sopenharmony_ci			return old;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci		spin_unlock(&detail->hash_lock);
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci	/* We need to insert a new entry */
2218c2ecf20Sopenharmony_ci	tmp = detail->alloc();
2228c2ecf20Sopenharmony_ci	if (!tmp) {
2238c2ecf20Sopenharmony_ci		cache_put(old, detail);
2248c2ecf20Sopenharmony_ci		return NULL;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci	cache_init(tmp, detail);
2278c2ecf20Sopenharmony_ci	detail->init(tmp, old);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	spin_lock(&detail->hash_lock);
2308c2ecf20Sopenharmony_ci	cache_entry_update(detail, tmp, new);
2318c2ecf20Sopenharmony_ci	hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
2328c2ecf20Sopenharmony_ci	detail->entries++;
2338c2ecf20Sopenharmony_ci	cache_get(tmp);
2348c2ecf20Sopenharmony_ci	cache_fresh_locked(tmp, new->expiry_time, detail);
2358c2ecf20Sopenharmony_ci	cache_fresh_locked(old, 0, detail);
2368c2ecf20Sopenharmony_ci	spin_unlock(&detail->hash_lock);
2378c2ecf20Sopenharmony_ci	cache_fresh_unlocked(tmp, detail);
2388c2ecf20Sopenharmony_ci	cache_fresh_unlocked(old, detail);
2398c2ecf20Sopenharmony_ci	cache_put(old, detail);
2408c2ecf20Sopenharmony_ci	return tmp;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_update);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic inline int cache_is_valid(struct cache_head *h)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	if (!test_bit(CACHE_VALID, &h->flags))
2478c2ecf20Sopenharmony_ci		return -EAGAIN;
2488c2ecf20Sopenharmony_ci	else {
2498c2ecf20Sopenharmony_ci		/* entry is valid */
2508c2ecf20Sopenharmony_ci		if (test_bit(CACHE_NEGATIVE, &h->flags))
2518c2ecf20Sopenharmony_ci			return -ENOENT;
2528c2ecf20Sopenharmony_ci		else {
2538c2ecf20Sopenharmony_ci			/*
2548c2ecf20Sopenharmony_ci			 * In combination with write barrier in
2558c2ecf20Sopenharmony_ci			 * sunrpc_cache_update, ensures that anyone
2568c2ecf20Sopenharmony_ci			 * using the cache entry after this sees the
2578c2ecf20Sopenharmony_ci			 * updated contents:
2588c2ecf20Sopenharmony_ci			 */
2598c2ecf20Sopenharmony_ci			smp_rmb();
2608c2ecf20Sopenharmony_ci			return 0;
2618c2ecf20Sopenharmony_ci		}
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	int rv;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	spin_lock(&detail->hash_lock);
2708c2ecf20Sopenharmony_ci	rv = cache_is_valid(h);
2718c2ecf20Sopenharmony_ci	if (rv == -EAGAIN) {
2728c2ecf20Sopenharmony_ci		cache_make_negative(detail, h);
2738c2ecf20Sopenharmony_ci		cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
2748c2ecf20Sopenharmony_ci				   detail);
2758c2ecf20Sopenharmony_ci		rv = -ENOENT;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci	spin_unlock(&detail->hash_lock);
2788c2ecf20Sopenharmony_ci	cache_fresh_unlocked(h, detail);
2798c2ecf20Sopenharmony_ci	return rv;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci/*
2838c2ecf20Sopenharmony_ci * This is the generic cache management routine for all
2848c2ecf20Sopenharmony_ci * the authentication caches.
2858c2ecf20Sopenharmony_ci * It checks the currency of a cache item and will (later)
2868c2ecf20Sopenharmony_ci * initiate an upcall to fill it if needed.
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci *
2898c2ecf20Sopenharmony_ci * Returns 0 if the cache_head can be used, or cache_puts it and returns
2908c2ecf20Sopenharmony_ci * -EAGAIN if upcall is pending and request has been queued
2918c2ecf20Sopenharmony_ci * -ETIMEDOUT if upcall failed or request could not be queue or
2928c2ecf20Sopenharmony_ci *           upcall completed but item is still invalid (implying that
2938c2ecf20Sopenharmony_ci *           the cache item has been replaced with a newer one).
2948c2ecf20Sopenharmony_ci * -ENOENT if cache entry was negative
2958c2ecf20Sopenharmony_ci */
2968c2ecf20Sopenharmony_ciint cache_check(struct cache_detail *detail,
2978c2ecf20Sopenharmony_ci		    struct cache_head *h, struct cache_req *rqstp)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	int rv;
3008c2ecf20Sopenharmony_ci	time64_t refresh_age, age;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* First decide return status as best we can */
3038c2ecf20Sopenharmony_ci	rv = cache_is_valid(h);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* now see if we want to start an upcall */
3068c2ecf20Sopenharmony_ci	refresh_age = (h->expiry_time - h->last_refresh);
3078c2ecf20Sopenharmony_ci	age = seconds_since_boot() - h->last_refresh;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (rqstp == NULL) {
3108c2ecf20Sopenharmony_ci		if (rv == -EAGAIN)
3118c2ecf20Sopenharmony_ci			rv = -ENOENT;
3128c2ecf20Sopenharmony_ci	} else if (rv == -EAGAIN ||
3138c2ecf20Sopenharmony_ci		   (h->expiry_time != 0 && age > refresh_age/2)) {
3148c2ecf20Sopenharmony_ci		dprintk("RPC:       Want update, refage=%lld, age=%lld\n",
3158c2ecf20Sopenharmony_ci				refresh_age, age);
3168c2ecf20Sopenharmony_ci		switch (detail->cache_upcall(detail, h)) {
3178c2ecf20Sopenharmony_ci		case -EINVAL:
3188c2ecf20Sopenharmony_ci			rv = try_to_negate_entry(detail, h);
3198c2ecf20Sopenharmony_ci			break;
3208c2ecf20Sopenharmony_ci		case -EAGAIN:
3218c2ecf20Sopenharmony_ci			cache_fresh_unlocked(h, detail);
3228c2ecf20Sopenharmony_ci			break;
3238c2ecf20Sopenharmony_ci		}
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (rv == -EAGAIN) {
3278c2ecf20Sopenharmony_ci		if (!cache_defer_req(rqstp, h)) {
3288c2ecf20Sopenharmony_ci			/*
3298c2ecf20Sopenharmony_ci			 * Request was not deferred; handle it as best
3308c2ecf20Sopenharmony_ci			 * we can ourselves:
3318c2ecf20Sopenharmony_ci			 */
3328c2ecf20Sopenharmony_ci			rv = cache_is_valid(h);
3338c2ecf20Sopenharmony_ci			if (rv == -EAGAIN)
3348c2ecf20Sopenharmony_ci				rv = -ETIMEDOUT;
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci	if (rv)
3388c2ecf20Sopenharmony_ci		cache_put(h, detail);
3398c2ecf20Sopenharmony_ci	return rv;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_check);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/*
3448c2ecf20Sopenharmony_ci * caches need to be periodically cleaned.
3458c2ecf20Sopenharmony_ci * For this we maintain a list of cache_detail and
3468c2ecf20Sopenharmony_ci * a current pointer into that list and into the table
3478c2ecf20Sopenharmony_ci * for that entry.
3488c2ecf20Sopenharmony_ci *
3498c2ecf20Sopenharmony_ci * Each time cache_clean is called it finds the next non-empty entry
3508c2ecf20Sopenharmony_ci * in the current table and walks the list in that entry
3518c2ecf20Sopenharmony_ci * looking for entries that can be removed.
3528c2ecf20Sopenharmony_ci *
3538c2ecf20Sopenharmony_ci * An entry gets removed if:
3548c2ecf20Sopenharmony_ci * - The expiry is before current time
3558c2ecf20Sopenharmony_ci * - The last_refresh time is before the flush_time for that cache
3568c2ecf20Sopenharmony_ci *
3578c2ecf20Sopenharmony_ci * later we might drop old entries with non-NEVER expiry if that table
3588c2ecf20Sopenharmony_ci * is getting 'full' for some definition of 'full'
3598c2ecf20Sopenharmony_ci *
3608c2ecf20Sopenharmony_ci * The question of "how often to scan a table" is an interesting one
3618c2ecf20Sopenharmony_ci * and is answered in part by the use of the "nextcheck" field in the
3628c2ecf20Sopenharmony_ci * cache_detail.
3638c2ecf20Sopenharmony_ci * When a scan of a table begins, the nextcheck field is set to a time
3648c2ecf20Sopenharmony_ci * that is well into the future.
3658c2ecf20Sopenharmony_ci * While scanning, if an expiry time is found that is earlier than the
3668c2ecf20Sopenharmony_ci * current nextcheck time, nextcheck is set to that expiry time.
3678c2ecf20Sopenharmony_ci * If the flush_time is ever set to a time earlier than the nextcheck
3688c2ecf20Sopenharmony_ci * time, the nextcheck time is then set to that flush_time.
3698c2ecf20Sopenharmony_ci *
3708c2ecf20Sopenharmony_ci * A table is then only scanned if the current time is at least
3718c2ecf20Sopenharmony_ci * the nextcheck time.
3728c2ecf20Sopenharmony_ci *
3738c2ecf20Sopenharmony_ci */
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic LIST_HEAD(cache_list);
3768c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(cache_list_lock);
3778c2ecf20Sopenharmony_cistatic struct cache_detail *current_detail;
3788c2ecf20Sopenharmony_cistatic int current_index;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic void do_cache_clean(struct work_struct *work);
3818c2ecf20Sopenharmony_cistatic struct delayed_work cache_cleaner;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_civoid sunrpc_init_cache_detail(struct cache_detail *cd)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	spin_lock_init(&cd->hash_lock);
3868c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cd->queue);
3878c2ecf20Sopenharmony_ci	spin_lock(&cache_list_lock);
3888c2ecf20Sopenharmony_ci	cd->nextcheck = 0;
3898c2ecf20Sopenharmony_ci	cd->entries = 0;
3908c2ecf20Sopenharmony_ci	atomic_set(&cd->writers, 0);
3918c2ecf20Sopenharmony_ci	cd->last_close = 0;
3928c2ecf20Sopenharmony_ci	cd->last_warn = -1;
3938c2ecf20Sopenharmony_ci	list_add(&cd->others, &cache_list);
3948c2ecf20Sopenharmony_ci	spin_unlock(&cache_list_lock);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* start the cleaning process */
3978c2ecf20Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_civoid sunrpc_destroy_cache_detail(struct cache_detail *cd)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	cache_purge(cd);
4048c2ecf20Sopenharmony_ci	spin_lock(&cache_list_lock);
4058c2ecf20Sopenharmony_ci	spin_lock(&cd->hash_lock);
4068c2ecf20Sopenharmony_ci	if (current_detail == cd)
4078c2ecf20Sopenharmony_ci		current_detail = NULL;
4088c2ecf20Sopenharmony_ci	list_del_init(&cd->others);
4098c2ecf20Sopenharmony_ci	spin_unlock(&cd->hash_lock);
4108c2ecf20Sopenharmony_ci	spin_unlock(&cache_list_lock);
4118c2ecf20Sopenharmony_ci	if (list_empty(&cache_list)) {
4128c2ecf20Sopenharmony_ci		/* module must be being unloaded so its safe to kill the worker */
4138c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&cache_cleaner);
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci/* clean cache tries to find something to clean
4198c2ecf20Sopenharmony_ci * and cleans it.
4208c2ecf20Sopenharmony_ci * It returns 1 if it cleaned something,
4218c2ecf20Sopenharmony_ci *            0 if it didn't find anything this time
4228c2ecf20Sopenharmony_ci *           -1 if it fell off the end of the list.
4238c2ecf20Sopenharmony_ci */
4248c2ecf20Sopenharmony_cistatic int cache_clean(void)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	int rv = 0;
4278c2ecf20Sopenharmony_ci	struct list_head *next;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	spin_lock(&cache_list_lock);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* find a suitable table if we don't already have one */
4328c2ecf20Sopenharmony_ci	while (current_detail == NULL ||
4338c2ecf20Sopenharmony_ci	    current_index >= current_detail->hash_size) {
4348c2ecf20Sopenharmony_ci		if (current_detail)
4358c2ecf20Sopenharmony_ci			next = current_detail->others.next;
4368c2ecf20Sopenharmony_ci		else
4378c2ecf20Sopenharmony_ci			next = cache_list.next;
4388c2ecf20Sopenharmony_ci		if (next == &cache_list) {
4398c2ecf20Sopenharmony_ci			current_detail = NULL;
4408c2ecf20Sopenharmony_ci			spin_unlock(&cache_list_lock);
4418c2ecf20Sopenharmony_ci			return -1;
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci		current_detail = list_entry(next, struct cache_detail, others);
4448c2ecf20Sopenharmony_ci		if (current_detail->nextcheck > seconds_since_boot())
4458c2ecf20Sopenharmony_ci			current_index = current_detail->hash_size;
4468c2ecf20Sopenharmony_ci		else {
4478c2ecf20Sopenharmony_ci			current_index = 0;
4488c2ecf20Sopenharmony_ci			current_detail->nextcheck = seconds_since_boot()+30*60;
4498c2ecf20Sopenharmony_ci		}
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	/* find a non-empty bucket in the table */
4538c2ecf20Sopenharmony_ci	while (current_detail &&
4548c2ecf20Sopenharmony_ci	       current_index < current_detail->hash_size &&
4558c2ecf20Sopenharmony_ci	       hlist_empty(&current_detail->hash_table[current_index]))
4568c2ecf20Sopenharmony_ci		current_index++;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* find a cleanable entry in the bucket and clean it, or set to next bucket */
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (current_detail && current_index < current_detail->hash_size) {
4618c2ecf20Sopenharmony_ci		struct cache_head *ch = NULL;
4628c2ecf20Sopenharmony_ci		struct cache_detail *d;
4638c2ecf20Sopenharmony_ci		struct hlist_head *head;
4648c2ecf20Sopenharmony_ci		struct hlist_node *tmp;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci		spin_lock(&current_detail->hash_lock);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		/* Ok, now to clean this strand */
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci		head = &current_detail->hash_table[current_index];
4718c2ecf20Sopenharmony_ci		hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
4728c2ecf20Sopenharmony_ci			if (current_detail->nextcheck > ch->expiry_time)
4738c2ecf20Sopenharmony_ci				current_detail->nextcheck = ch->expiry_time+1;
4748c2ecf20Sopenharmony_ci			if (!cache_is_expired(current_detail, ch))
4758c2ecf20Sopenharmony_ci				continue;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci			sunrpc_begin_cache_remove_entry(ch, current_detail);
4788c2ecf20Sopenharmony_ci			trace_cache_entry_expired(current_detail, ch);
4798c2ecf20Sopenharmony_ci			rv = 1;
4808c2ecf20Sopenharmony_ci			break;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		spin_unlock(&current_detail->hash_lock);
4848c2ecf20Sopenharmony_ci		d = current_detail;
4858c2ecf20Sopenharmony_ci		if (!ch)
4868c2ecf20Sopenharmony_ci			current_index ++;
4878c2ecf20Sopenharmony_ci		spin_unlock(&cache_list_lock);
4888c2ecf20Sopenharmony_ci		if (ch)
4898c2ecf20Sopenharmony_ci			sunrpc_end_cache_remove_entry(ch, d);
4908c2ecf20Sopenharmony_ci	} else
4918c2ecf20Sopenharmony_ci		spin_unlock(&cache_list_lock);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return rv;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci/*
4978c2ecf20Sopenharmony_ci * We want to regularly clean the cache, so we need to schedule some work ...
4988c2ecf20Sopenharmony_ci */
4998c2ecf20Sopenharmony_cistatic void do_cache_clean(struct work_struct *work)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	int delay;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (list_empty(&cache_list))
5048c2ecf20Sopenharmony_ci		return;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	if (cache_clean() == -1)
5078c2ecf20Sopenharmony_ci		delay = round_jiffies_relative(30*HZ);
5088c2ecf20Sopenharmony_ci	else
5098c2ecf20Sopenharmony_ci		delay = 5;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	queue_delayed_work(system_power_efficient_wq, &cache_cleaner, delay);
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci/*
5168c2ecf20Sopenharmony_ci * Clean all caches promptly.  This just calls cache_clean
5178c2ecf20Sopenharmony_ci * repeatedly until we are sure that every cache has had a chance to
5188c2ecf20Sopenharmony_ci * be fully cleaned
5198c2ecf20Sopenharmony_ci */
5208c2ecf20Sopenharmony_civoid cache_flush(void)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	while (cache_clean() != -1)
5238c2ecf20Sopenharmony_ci		cond_resched();
5248c2ecf20Sopenharmony_ci	while (cache_clean() != -1)
5258c2ecf20Sopenharmony_ci		cond_resched();
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_flush);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_civoid cache_purge(struct cache_detail *detail)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct cache_head *ch = NULL;
5328c2ecf20Sopenharmony_ci	struct hlist_head *head = NULL;
5338c2ecf20Sopenharmony_ci	int i = 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	spin_lock(&detail->hash_lock);
5368c2ecf20Sopenharmony_ci	if (!detail->entries) {
5378c2ecf20Sopenharmony_ci		spin_unlock(&detail->hash_lock);
5388c2ecf20Sopenharmony_ci		return;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
5428c2ecf20Sopenharmony_ci	for (i = 0; i < detail->hash_size; i++) {
5438c2ecf20Sopenharmony_ci		head = &detail->hash_table[i];
5448c2ecf20Sopenharmony_ci		while (!hlist_empty(head)) {
5458c2ecf20Sopenharmony_ci			ch = hlist_entry(head->first, struct cache_head,
5468c2ecf20Sopenharmony_ci					 cache_list);
5478c2ecf20Sopenharmony_ci			sunrpc_begin_cache_remove_entry(ch, detail);
5488c2ecf20Sopenharmony_ci			spin_unlock(&detail->hash_lock);
5498c2ecf20Sopenharmony_ci			sunrpc_end_cache_remove_entry(ch, detail);
5508c2ecf20Sopenharmony_ci			spin_lock(&detail->hash_lock);
5518c2ecf20Sopenharmony_ci		}
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci	spin_unlock(&detail->hash_lock);
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_purge);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci/*
5598c2ecf20Sopenharmony_ci * Deferral and Revisiting of Requests.
5608c2ecf20Sopenharmony_ci *
5618c2ecf20Sopenharmony_ci * If a cache lookup finds a pending entry, we
5628c2ecf20Sopenharmony_ci * need to defer the request and revisit it later.
5638c2ecf20Sopenharmony_ci * All deferred requests are stored in a hash table,
5648c2ecf20Sopenharmony_ci * indexed by "struct cache_head *".
5658c2ecf20Sopenharmony_ci * As it may be wasteful to store a whole request
5668c2ecf20Sopenharmony_ci * structure, we allow the request to provide a
5678c2ecf20Sopenharmony_ci * deferred form, which must contain a
5688c2ecf20Sopenharmony_ci * 'struct cache_deferred_req'
5698c2ecf20Sopenharmony_ci * This cache_deferred_req contains a method to allow
5708c2ecf20Sopenharmony_ci * it to be revisited when cache info is available
5718c2ecf20Sopenharmony_ci */
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci#define	DFR_HASHSIZE	(PAGE_SIZE/sizeof(struct list_head))
5748c2ecf20Sopenharmony_ci#define	DFR_HASH(item)	((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci#define	DFR_MAX	300	/* ??? */
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(cache_defer_lock);
5798c2ecf20Sopenharmony_cistatic LIST_HEAD(cache_defer_list);
5808c2ecf20Sopenharmony_cistatic struct hlist_head cache_defer_hash[DFR_HASHSIZE];
5818c2ecf20Sopenharmony_cistatic int cache_defer_cnt;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic void __unhash_deferred_req(struct cache_deferred_req *dreq)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	hlist_del_init(&dreq->hash);
5868c2ecf20Sopenharmony_ci	if (!list_empty(&dreq->recent)) {
5878c2ecf20Sopenharmony_ci		list_del_init(&dreq->recent);
5888c2ecf20Sopenharmony_ci		cache_defer_cnt--;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	int hash = DFR_HASH(item);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&dreq->recent);
5978c2ecf20Sopenharmony_ci	hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void setup_deferral(struct cache_deferred_req *dreq,
6018c2ecf20Sopenharmony_ci			   struct cache_head *item,
6028c2ecf20Sopenharmony_ci			   int count_me)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	dreq->item = item;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	spin_lock(&cache_defer_lock);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	__hash_deferred_req(dreq, item);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (count_me) {
6128c2ecf20Sopenharmony_ci		cache_defer_cnt++;
6138c2ecf20Sopenharmony_ci		list_add(&dreq->recent, &cache_defer_list);
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	spin_unlock(&cache_defer_lock);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistruct thread_deferred_req {
6218c2ecf20Sopenharmony_ci	struct cache_deferred_req handle;
6228c2ecf20Sopenharmony_ci	struct completion completion;
6238c2ecf20Sopenharmony_ci};
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct thread_deferred_req *dr =
6288c2ecf20Sopenharmony_ci		container_of(dreq, struct thread_deferred_req, handle);
6298c2ecf20Sopenharmony_ci	complete(&dr->completion);
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic void cache_wait_req(struct cache_req *req, struct cache_head *item)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct thread_deferred_req sleeper;
6358c2ecf20Sopenharmony_ci	struct cache_deferred_req *dreq = &sleeper.handle;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
6388c2ecf20Sopenharmony_ci	dreq->revisit = cache_restart_thread;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	setup_deferral(dreq, item, 0);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	if (!test_bit(CACHE_PENDING, &item->flags) ||
6438c2ecf20Sopenharmony_ci	    wait_for_completion_interruptible_timeout(
6448c2ecf20Sopenharmony_ci		    &sleeper.completion, req->thread_wait) <= 0) {
6458c2ecf20Sopenharmony_ci		/* The completion wasn't completed, so we need
6468c2ecf20Sopenharmony_ci		 * to clean up
6478c2ecf20Sopenharmony_ci		 */
6488c2ecf20Sopenharmony_ci		spin_lock(&cache_defer_lock);
6498c2ecf20Sopenharmony_ci		if (!hlist_unhashed(&sleeper.handle.hash)) {
6508c2ecf20Sopenharmony_ci			__unhash_deferred_req(&sleeper.handle);
6518c2ecf20Sopenharmony_ci			spin_unlock(&cache_defer_lock);
6528c2ecf20Sopenharmony_ci		} else {
6538c2ecf20Sopenharmony_ci			/* cache_revisit_request already removed
6548c2ecf20Sopenharmony_ci			 * this from the hash table, but hasn't
6558c2ecf20Sopenharmony_ci			 * called ->revisit yet.  It will very soon
6568c2ecf20Sopenharmony_ci			 * and we need to wait for it.
6578c2ecf20Sopenharmony_ci			 */
6588c2ecf20Sopenharmony_ci			spin_unlock(&cache_defer_lock);
6598c2ecf20Sopenharmony_ci			wait_for_completion(&sleeper.completion);
6608c2ecf20Sopenharmony_ci		}
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic void cache_limit_defers(void)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	/* Make sure we haven't exceed the limit of allowed deferred
6678c2ecf20Sopenharmony_ci	 * requests.
6688c2ecf20Sopenharmony_ci	 */
6698c2ecf20Sopenharmony_ci	struct cache_deferred_req *discard = NULL;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (cache_defer_cnt <= DFR_MAX)
6728c2ecf20Sopenharmony_ci		return;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	spin_lock(&cache_defer_lock);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Consider removing either the first or the last */
6778c2ecf20Sopenharmony_ci	if (cache_defer_cnt > DFR_MAX) {
6788c2ecf20Sopenharmony_ci		if (prandom_u32() & 1)
6798c2ecf20Sopenharmony_ci			discard = list_entry(cache_defer_list.next,
6808c2ecf20Sopenharmony_ci					     struct cache_deferred_req, recent);
6818c2ecf20Sopenharmony_ci		else
6828c2ecf20Sopenharmony_ci			discard = list_entry(cache_defer_list.prev,
6838c2ecf20Sopenharmony_ci					     struct cache_deferred_req, recent);
6848c2ecf20Sopenharmony_ci		__unhash_deferred_req(discard);
6858c2ecf20Sopenharmony_ci	}
6868c2ecf20Sopenharmony_ci	spin_unlock(&cache_defer_lock);
6878c2ecf20Sopenharmony_ci	if (discard)
6888c2ecf20Sopenharmony_ci		discard->revisit(discard, 1);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci/* Return true if and only if a deferred request is queued. */
6928c2ecf20Sopenharmony_cistatic bool cache_defer_req(struct cache_req *req, struct cache_head *item)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct cache_deferred_req *dreq;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	if (req->thread_wait) {
6978c2ecf20Sopenharmony_ci		cache_wait_req(req, item);
6988c2ecf20Sopenharmony_ci		if (!test_bit(CACHE_PENDING, &item->flags))
6998c2ecf20Sopenharmony_ci			return false;
7008c2ecf20Sopenharmony_ci	}
7018c2ecf20Sopenharmony_ci	dreq = req->defer(req);
7028c2ecf20Sopenharmony_ci	if (dreq == NULL)
7038c2ecf20Sopenharmony_ci		return false;
7048c2ecf20Sopenharmony_ci	setup_deferral(dreq, item, 1);
7058c2ecf20Sopenharmony_ci	if (!test_bit(CACHE_PENDING, &item->flags))
7068c2ecf20Sopenharmony_ci		/* Bit could have been cleared before we managed to
7078c2ecf20Sopenharmony_ci		 * set up the deferral, so need to revisit just in case
7088c2ecf20Sopenharmony_ci		 */
7098c2ecf20Sopenharmony_ci		cache_revisit_request(item);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	cache_limit_defers();
7128c2ecf20Sopenharmony_ci	return true;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic void cache_revisit_request(struct cache_head *item)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	struct cache_deferred_req *dreq;
7188c2ecf20Sopenharmony_ci	struct list_head pending;
7198c2ecf20Sopenharmony_ci	struct hlist_node *tmp;
7208c2ecf20Sopenharmony_ci	int hash = DFR_HASH(item);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&pending);
7238c2ecf20Sopenharmony_ci	spin_lock(&cache_defer_lock);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
7268c2ecf20Sopenharmony_ci		if (dreq->item == item) {
7278c2ecf20Sopenharmony_ci			__unhash_deferred_req(dreq);
7288c2ecf20Sopenharmony_ci			list_add(&dreq->recent, &pending);
7298c2ecf20Sopenharmony_ci		}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	spin_unlock(&cache_defer_lock);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	while (!list_empty(&pending)) {
7348c2ecf20Sopenharmony_ci		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
7358c2ecf20Sopenharmony_ci		list_del_init(&dreq->recent);
7368c2ecf20Sopenharmony_ci		dreq->revisit(dreq, 0);
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_civoid cache_clean_deferred(void *owner)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct cache_deferred_req *dreq, *tmp;
7438c2ecf20Sopenharmony_ci	struct list_head pending;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&pending);
7478c2ecf20Sopenharmony_ci	spin_lock(&cache_defer_lock);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
7508c2ecf20Sopenharmony_ci		if (dreq->owner == owner) {
7518c2ecf20Sopenharmony_ci			__unhash_deferred_req(dreq);
7528c2ecf20Sopenharmony_ci			list_add(&dreq->recent, &pending);
7538c2ecf20Sopenharmony_ci		}
7548c2ecf20Sopenharmony_ci	}
7558c2ecf20Sopenharmony_ci	spin_unlock(&cache_defer_lock);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	while (!list_empty(&pending)) {
7588c2ecf20Sopenharmony_ci		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
7598c2ecf20Sopenharmony_ci		list_del_init(&dreq->recent);
7608c2ecf20Sopenharmony_ci		dreq->revisit(dreq, 1);
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci/*
7658c2ecf20Sopenharmony_ci * communicate with user-space
7668c2ecf20Sopenharmony_ci *
7678c2ecf20Sopenharmony_ci * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
7688c2ecf20Sopenharmony_ci * On read, you get a full request, or block.
7698c2ecf20Sopenharmony_ci * On write, an update request is processed.
7708c2ecf20Sopenharmony_ci * Poll works if anything to read, and always allows write.
7718c2ecf20Sopenharmony_ci *
7728c2ecf20Sopenharmony_ci * Implemented by linked list of requests.  Each open file has
7738c2ecf20Sopenharmony_ci * a ->private that also exists in this list.  New requests are added
7748c2ecf20Sopenharmony_ci * to the end and may wakeup and preceding readers.
7758c2ecf20Sopenharmony_ci * New readers are added to the head.  If, on read, an item is found with
7768c2ecf20Sopenharmony_ci * CACHE_UPCALLING clear, we free it from the list.
7778c2ecf20Sopenharmony_ci *
7788c2ecf20Sopenharmony_ci */
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(queue_lock);
7818c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(queue_io_mutex);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistruct cache_queue {
7848c2ecf20Sopenharmony_ci	struct list_head	list;
7858c2ecf20Sopenharmony_ci	int			reader;	/* if 0, then request */
7868c2ecf20Sopenharmony_ci};
7878c2ecf20Sopenharmony_cistruct cache_request {
7888c2ecf20Sopenharmony_ci	struct cache_queue	q;
7898c2ecf20Sopenharmony_ci	struct cache_head	*item;
7908c2ecf20Sopenharmony_ci	char			* buf;
7918c2ecf20Sopenharmony_ci	int			len;
7928c2ecf20Sopenharmony_ci	int			readers;
7938c2ecf20Sopenharmony_ci};
7948c2ecf20Sopenharmony_cistruct cache_reader {
7958c2ecf20Sopenharmony_ci	struct cache_queue	q;
7968c2ecf20Sopenharmony_ci	int			offset;	/* if non-0, we have a refcnt on next request */
7978c2ecf20Sopenharmony_ci};
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_cistatic int cache_request(struct cache_detail *detail,
8008c2ecf20Sopenharmony_ci			       struct cache_request *crq)
8018c2ecf20Sopenharmony_ci{
8028c2ecf20Sopenharmony_ci	char *bp = crq->buf;
8038c2ecf20Sopenharmony_ci	int len = PAGE_SIZE;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	detail->cache_request(detail, crq->item, &bp, &len);
8068c2ecf20Sopenharmony_ci	if (len < 0)
8078c2ecf20Sopenharmony_ci		return -EAGAIN;
8088c2ecf20Sopenharmony_ci	return PAGE_SIZE - len;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
8128c2ecf20Sopenharmony_ci			  loff_t *ppos, struct cache_detail *cd)
8138c2ecf20Sopenharmony_ci{
8148c2ecf20Sopenharmony_ci	struct cache_reader *rp = filp->private_data;
8158c2ecf20Sopenharmony_ci	struct cache_request *rq;
8168c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(filp);
8178c2ecf20Sopenharmony_ci	int err;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	if (count == 0)
8208c2ecf20Sopenharmony_ci		return 0;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	inode_lock(inode); /* protect against multiple concurrent
8238c2ecf20Sopenharmony_ci			      * readers on this file */
8248c2ecf20Sopenharmony_ci again:
8258c2ecf20Sopenharmony_ci	spin_lock(&queue_lock);
8268c2ecf20Sopenharmony_ci	/* need to find next request */
8278c2ecf20Sopenharmony_ci	while (rp->q.list.next != &cd->queue &&
8288c2ecf20Sopenharmony_ci	       list_entry(rp->q.list.next, struct cache_queue, list)
8298c2ecf20Sopenharmony_ci	       ->reader) {
8308c2ecf20Sopenharmony_ci		struct list_head *next = rp->q.list.next;
8318c2ecf20Sopenharmony_ci		list_move(&rp->q.list, next);
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci	if (rp->q.list.next == &cd->queue) {
8348c2ecf20Sopenharmony_ci		spin_unlock(&queue_lock);
8358c2ecf20Sopenharmony_ci		inode_unlock(inode);
8368c2ecf20Sopenharmony_ci		WARN_ON_ONCE(rp->offset);
8378c2ecf20Sopenharmony_ci		return 0;
8388c2ecf20Sopenharmony_ci	}
8398c2ecf20Sopenharmony_ci	rq = container_of(rp->q.list.next, struct cache_request, q.list);
8408c2ecf20Sopenharmony_ci	WARN_ON_ONCE(rq->q.reader);
8418c2ecf20Sopenharmony_ci	if (rp->offset == 0)
8428c2ecf20Sopenharmony_ci		rq->readers++;
8438c2ecf20Sopenharmony_ci	spin_unlock(&queue_lock);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	if (rq->len == 0) {
8468c2ecf20Sopenharmony_ci		err = cache_request(cd, rq);
8478c2ecf20Sopenharmony_ci		if (err < 0)
8488c2ecf20Sopenharmony_ci			goto out;
8498c2ecf20Sopenharmony_ci		rq->len = err;
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
8538c2ecf20Sopenharmony_ci		err = -EAGAIN;
8548c2ecf20Sopenharmony_ci		spin_lock(&queue_lock);
8558c2ecf20Sopenharmony_ci		list_move(&rp->q.list, &rq->q.list);
8568c2ecf20Sopenharmony_ci		spin_unlock(&queue_lock);
8578c2ecf20Sopenharmony_ci	} else {
8588c2ecf20Sopenharmony_ci		if (rp->offset + count > rq->len)
8598c2ecf20Sopenharmony_ci			count = rq->len - rp->offset;
8608c2ecf20Sopenharmony_ci		err = -EFAULT;
8618c2ecf20Sopenharmony_ci		if (copy_to_user(buf, rq->buf + rp->offset, count))
8628c2ecf20Sopenharmony_ci			goto out;
8638c2ecf20Sopenharmony_ci		rp->offset += count;
8648c2ecf20Sopenharmony_ci		if (rp->offset >= rq->len) {
8658c2ecf20Sopenharmony_ci			rp->offset = 0;
8668c2ecf20Sopenharmony_ci			spin_lock(&queue_lock);
8678c2ecf20Sopenharmony_ci			list_move(&rp->q.list, &rq->q.list);
8688c2ecf20Sopenharmony_ci			spin_unlock(&queue_lock);
8698c2ecf20Sopenharmony_ci		}
8708c2ecf20Sopenharmony_ci		err = 0;
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci out:
8738c2ecf20Sopenharmony_ci	if (rp->offset == 0) {
8748c2ecf20Sopenharmony_ci		/* need to release rq */
8758c2ecf20Sopenharmony_ci		spin_lock(&queue_lock);
8768c2ecf20Sopenharmony_ci		rq->readers--;
8778c2ecf20Sopenharmony_ci		if (rq->readers == 0 &&
8788c2ecf20Sopenharmony_ci		    !test_bit(CACHE_PENDING, &rq->item->flags)) {
8798c2ecf20Sopenharmony_ci			list_del(&rq->q.list);
8808c2ecf20Sopenharmony_ci			spin_unlock(&queue_lock);
8818c2ecf20Sopenharmony_ci			cache_put(rq->item, cd);
8828c2ecf20Sopenharmony_ci			kfree(rq->buf);
8838c2ecf20Sopenharmony_ci			kfree(rq);
8848c2ecf20Sopenharmony_ci		} else
8858c2ecf20Sopenharmony_ci			spin_unlock(&queue_lock);
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci	if (err == -EAGAIN)
8888c2ecf20Sopenharmony_ci		goto again;
8898c2ecf20Sopenharmony_ci	inode_unlock(inode);
8908c2ecf20Sopenharmony_ci	return err ? err :  count;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
8948c2ecf20Sopenharmony_ci				 size_t count, struct cache_detail *cd)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	ssize_t ret;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (count == 0)
8998c2ecf20Sopenharmony_ci		return -EINVAL;
9008c2ecf20Sopenharmony_ci	if (copy_from_user(kaddr, buf, count))
9018c2ecf20Sopenharmony_ci		return -EFAULT;
9028c2ecf20Sopenharmony_ci	kaddr[count] = '\0';
9038c2ecf20Sopenharmony_ci	ret = cd->cache_parse(cd, kaddr, count);
9048c2ecf20Sopenharmony_ci	if (!ret)
9058c2ecf20Sopenharmony_ci		ret = count;
9068c2ecf20Sopenharmony_ci	return ret;
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cistatic ssize_t cache_slow_downcall(const char __user *buf,
9108c2ecf20Sopenharmony_ci				   size_t count, struct cache_detail *cd)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	static char write_buf[32768]; /* protected by queue_io_mutex */
9138c2ecf20Sopenharmony_ci	ssize_t ret = -EINVAL;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	if (count >= sizeof(write_buf))
9168c2ecf20Sopenharmony_ci		goto out;
9178c2ecf20Sopenharmony_ci	mutex_lock(&queue_io_mutex);
9188c2ecf20Sopenharmony_ci	ret = cache_do_downcall(write_buf, buf, count, cd);
9198c2ecf20Sopenharmony_ci	mutex_unlock(&queue_io_mutex);
9208c2ecf20Sopenharmony_ciout:
9218c2ecf20Sopenharmony_ci	return ret;
9228c2ecf20Sopenharmony_ci}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_cistatic ssize_t cache_downcall(struct address_space *mapping,
9258c2ecf20Sopenharmony_ci			      const char __user *buf,
9268c2ecf20Sopenharmony_ci			      size_t count, struct cache_detail *cd)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	struct page *page;
9298c2ecf20Sopenharmony_ci	char *kaddr;
9308c2ecf20Sopenharmony_ci	ssize_t ret = -ENOMEM;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	if (count >= PAGE_SIZE)
9338c2ecf20Sopenharmony_ci		goto out_slow;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	page = find_or_create_page(mapping, 0, GFP_KERNEL);
9368c2ecf20Sopenharmony_ci	if (!page)
9378c2ecf20Sopenharmony_ci		goto out_slow;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	kaddr = kmap(page);
9408c2ecf20Sopenharmony_ci	ret = cache_do_downcall(kaddr, buf, count, cd);
9418c2ecf20Sopenharmony_ci	kunmap(page);
9428c2ecf20Sopenharmony_ci	unlock_page(page);
9438c2ecf20Sopenharmony_ci	put_page(page);
9448c2ecf20Sopenharmony_ci	return ret;
9458c2ecf20Sopenharmony_ciout_slow:
9468c2ecf20Sopenharmony_ci	return cache_slow_downcall(buf, count, cd);
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cistatic ssize_t cache_write(struct file *filp, const char __user *buf,
9508c2ecf20Sopenharmony_ci			   size_t count, loff_t *ppos,
9518c2ecf20Sopenharmony_ci			   struct cache_detail *cd)
9528c2ecf20Sopenharmony_ci{
9538c2ecf20Sopenharmony_ci	struct address_space *mapping = filp->f_mapping;
9548c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(filp);
9558c2ecf20Sopenharmony_ci	ssize_t ret = -EINVAL;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	if (!cd->cache_parse)
9588c2ecf20Sopenharmony_ci		goto out;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	inode_lock(inode);
9618c2ecf20Sopenharmony_ci	ret = cache_downcall(mapping, buf, count, cd);
9628c2ecf20Sopenharmony_ci	inode_unlock(inode);
9638c2ecf20Sopenharmony_ciout:
9648c2ecf20Sopenharmony_ci	return ret;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic DECLARE_WAIT_QUEUE_HEAD(queue_wait);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_cistatic __poll_t cache_poll(struct file *filp, poll_table *wait,
9708c2ecf20Sopenharmony_ci			       struct cache_detail *cd)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	__poll_t mask;
9738c2ecf20Sopenharmony_ci	struct cache_reader *rp = filp->private_data;
9748c2ecf20Sopenharmony_ci	struct cache_queue *cq;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	poll_wait(filp, &queue_wait, wait);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	/* alway allow write */
9798c2ecf20Sopenharmony_ci	mask = EPOLLOUT | EPOLLWRNORM;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	if (!rp)
9828c2ecf20Sopenharmony_ci		return mask;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	spin_lock(&queue_lock);
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	for (cq= &rp->q; &cq->list != &cd->queue;
9878c2ecf20Sopenharmony_ci	     cq = list_entry(cq->list.next, struct cache_queue, list))
9888c2ecf20Sopenharmony_ci		if (!cq->reader) {
9898c2ecf20Sopenharmony_ci			mask |= EPOLLIN | EPOLLRDNORM;
9908c2ecf20Sopenharmony_ci			break;
9918c2ecf20Sopenharmony_ci		}
9928c2ecf20Sopenharmony_ci	spin_unlock(&queue_lock);
9938c2ecf20Sopenharmony_ci	return mask;
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic int cache_ioctl(struct inode *ino, struct file *filp,
9978c2ecf20Sopenharmony_ci		       unsigned int cmd, unsigned long arg,
9988c2ecf20Sopenharmony_ci		       struct cache_detail *cd)
9998c2ecf20Sopenharmony_ci{
10008c2ecf20Sopenharmony_ci	int len = 0;
10018c2ecf20Sopenharmony_ci	struct cache_reader *rp = filp->private_data;
10028c2ecf20Sopenharmony_ci	struct cache_queue *cq;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	if (cmd != FIONREAD || !rp)
10058c2ecf20Sopenharmony_ci		return -EINVAL;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	spin_lock(&queue_lock);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	/* only find the length remaining in current request,
10108c2ecf20Sopenharmony_ci	 * or the length of the next request
10118c2ecf20Sopenharmony_ci	 */
10128c2ecf20Sopenharmony_ci	for (cq= &rp->q; &cq->list != &cd->queue;
10138c2ecf20Sopenharmony_ci	     cq = list_entry(cq->list.next, struct cache_queue, list))
10148c2ecf20Sopenharmony_ci		if (!cq->reader) {
10158c2ecf20Sopenharmony_ci			struct cache_request *cr =
10168c2ecf20Sopenharmony_ci				container_of(cq, struct cache_request, q);
10178c2ecf20Sopenharmony_ci			len = cr->len - rp->offset;
10188c2ecf20Sopenharmony_ci			break;
10198c2ecf20Sopenharmony_ci		}
10208c2ecf20Sopenharmony_ci	spin_unlock(&queue_lock);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	return put_user(len, (int __user *)arg);
10238c2ecf20Sopenharmony_ci}
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_cistatic int cache_open(struct inode *inode, struct file *filp,
10268c2ecf20Sopenharmony_ci		      struct cache_detail *cd)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	struct cache_reader *rp = NULL;
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	if (!cd || !try_module_get(cd->owner))
10318c2ecf20Sopenharmony_ci		return -EACCES;
10328c2ecf20Sopenharmony_ci	nonseekable_open(inode, filp);
10338c2ecf20Sopenharmony_ci	if (filp->f_mode & FMODE_READ) {
10348c2ecf20Sopenharmony_ci		rp = kmalloc(sizeof(*rp), GFP_KERNEL);
10358c2ecf20Sopenharmony_ci		if (!rp) {
10368c2ecf20Sopenharmony_ci			module_put(cd->owner);
10378c2ecf20Sopenharmony_ci			return -ENOMEM;
10388c2ecf20Sopenharmony_ci		}
10398c2ecf20Sopenharmony_ci		rp->offset = 0;
10408c2ecf20Sopenharmony_ci		rp->q.reader = 1;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci		spin_lock(&queue_lock);
10438c2ecf20Sopenharmony_ci		list_add(&rp->q.list, &cd->queue);
10448c2ecf20Sopenharmony_ci		spin_unlock(&queue_lock);
10458c2ecf20Sopenharmony_ci	}
10468c2ecf20Sopenharmony_ci	if (filp->f_mode & FMODE_WRITE)
10478c2ecf20Sopenharmony_ci		atomic_inc(&cd->writers);
10488c2ecf20Sopenharmony_ci	filp->private_data = rp;
10498c2ecf20Sopenharmony_ci	return 0;
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_cistatic int cache_release(struct inode *inode, struct file *filp,
10538c2ecf20Sopenharmony_ci			 struct cache_detail *cd)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	struct cache_reader *rp = filp->private_data;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	if (rp) {
10588c2ecf20Sopenharmony_ci		spin_lock(&queue_lock);
10598c2ecf20Sopenharmony_ci		if (rp->offset) {
10608c2ecf20Sopenharmony_ci			struct cache_queue *cq;
10618c2ecf20Sopenharmony_ci			for (cq= &rp->q; &cq->list != &cd->queue;
10628c2ecf20Sopenharmony_ci			     cq = list_entry(cq->list.next, struct cache_queue, list))
10638c2ecf20Sopenharmony_ci				if (!cq->reader) {
10648c2ecf20Sopenharmony_ci					container_of(cq, struct cache_request, q)
10658c2ecf20Sopenharmony_ci						->readers--;
10668c2ecf20Sopenharmony_ci					break;
10678c2ecf20Sopenharmony_ci				}
10688c2ecf20Sopenharmony_ci			rp->offset = 0;
10698c2ecf20Sopenharmony_ci		}
10708c2ecf20Sopenharmony_ci		list_del(&rp->q.list);
10718c2ecf20Sopenharmony_ci		spin_unlock(&queue_lock);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci		filp->private_data = NULL;
10748c2ecf20Sopenharmony_ci		kfree(rp);
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	}
10778c2ecf20Sopenharmony_ci	if (filp->f_mode & FMODE_WRITE) {
10788c2ecf20Sopenharmony_ci		atomic_dec(&cd->writers);
10798c2ecf20Sopenharmony_ci		cd->last_close = seconds_since_boot();
10808c2ecf20Sopenharmony_ci	}
10818c2ecf20Sopenharmony_ci	module_put(cd->owner);
10828c2ecf20Sopenharmony_ci	return 0;
10838c2ecf20Sopenharmony_ci}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_cistatic void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	struct cache_queue *cq, *tmp;
10908c2ecf20Sopenharmony_ci	struct cache_request *cr;
10918c2ecf20Sopenharmony_ci	struct list_head dequeued;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&dequeued);
10948c2ecf20Sopenharmony_ci	spin_lock(&queue_lock);
10958c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cq, tmp, &detail->queue, list)
10968c2ecf20Sopenharmony_ci		if (!cq->reader) {
10978c2ecf20Sopenharmony_ci			cr = container_of(cq, struct cache_request, q);
10988c2ecf20Sopenharmony_ci			if (cr->item != ch)
10998c2ecf20Sopenharmony_ci				continue;
11008c2ecf20Sopenharmony_ci			if (test_bit(CACHE_PENDING, &ch->flags))
11018c2ecf20Sopenharmony_ci				/* Lost a race and it is pending again */
11028c2ecf20Sopenharmony_ci				break;
11038c2ecf20Sopenharmony_ci			if (cr->readers != 0)
11048c2ecf20Sopenharmony_ci				continue;
11058c2ecf20Sopenharmony_ci			list_move(&cr->q.list, &dequeued);
11068c2ecf20Sopenharmony_ci		}
11078c2ecf20Sopenharmony_ci	spin_unlock(&queue_lock);
11088c2ecf20Sopenharmony_ci	while (!list_empty(&dequeued)) {
11098c2ecf20Sopenharmony_ci		cr = list_entry(dequeued.next, struct cache_request, q.list);
11108c2ecf20Sopenharmony_ci		list_del(&cr->q.list);
11118c2ecf20Sopenharmony_ci		cache_put(cr->item, detail);
11128c2ecf20Sopenharmony_ci		kfree(cr->buf);
11138c2ecf20Sopenharmony_ci		kfree(cr);
11148c2ecf20Sopenharmony_ci	}
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci/*
11188c2ecf20Sopenharmony_ci * Support routines for text-based upcalls.
11198c2ecf20Sopenharmony_ci * Fields are separated by spaces.
11208c2ecf20Sopenharmony_ci * Fields are either mangled to quote space tab newline slosh with slosh
11218c2ecf20Sopenharmony_ci * or a hexified with a leading \x
11228c2ecf20Sopenharmony_ci * Record is terminated with newline.
11238c2ecf20Sopenharmony_ci *
11248c2ecf20Sopenharmony_ci */
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_civoid qword_add(char **bpp, int *lp, char *str)
11278c2ecf20Sopenharmony_ci{
11288c2ecf20Sopenharmony_ci	char *bp = *bpp;
11298c2ecf20Sopenharmony_ci	int len = *lp;
11308c2ecf20Sopenharmony_ci	int ret;
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	if (len < 0) return;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
11358c2ecf20Sopenharmony_ci	if (ret >= len) {
11368c2ecf20Sopenharmony_ci		bp += len;
11378c2ecf20Sopenharmony_ci		len = -1;
11388c2ecf20Sopenharmony_ci	} else {
11398c2ecf20Sopenharmony_ci		bp += ret;
11408c2ecf20Sopenharmony_ci		len -= ret;
11418c2ecf20Sopenharmony_ci		*bp++ = ' ';
11428c2ecf20Sopenharmony_ci		len--;
11438c2ecf20Sopenharmony_ci	}
11448c2ecf20Sopenharmony_ci	*bpp = bp;
11458c2ecf20Sopenharmony_ci	*lp = len;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(qword_add);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_civoid qword_addhex(char **bpp, int *lp, char *buf, int blen)
11508c2ecf20Sopenharmony_ci{
11518c2ecf20Sopenharmony_ci	char *bp = *bpp;
11528c2ecf20Sopenharmony_ci	int len = *lp;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	if (len < 0) return;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	if (len > 2) {
11578c2ecf20Sopenharmony_ci		*bp++ = '\\';
11588c2ecf20Sopenharmony_ci		*bp++ = 'x';
11598c2ecf20Sopenharmony_ci		len -= 2;
11608c2ecf20Sopenharmony_ci		while (blen && len >= 2) {
11618c2ecf20Sopenharmony_ci			bp = hex_byte_pack(bp, *buf++);
11628c2ecf20Sopenharmony_ci			len -= 2;
11638c2ecf20Sopenharmony_ci			blen--;
11648c2ecf20Sopenharmony_ci		}
11658c2ecf20Sopenharmony_ci	}
11668c2ecf20Sopenharmony_ci	if (blen || len<1) len = -1;
11678c2ecf20Sopenharmony_ci	else {
11688c2ecf20Sopenharmony_ci		*bp++ = ' ';
11698c2ecf20Sopenharmony_ci		len--;
11708c2ecf20Sopenharmony_ci	}
11718c2ecf20Sopenharmony_ci	*bpp = bp;
11728c2ecf20Sopenharmony_ci	*lp = len;
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(qword_addhex);
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_cistatic void warn_no_listener(struct cache_detail *detail)
11778c2ecf20Sopenharmony_ci{
11788c2ecf20Sopenharmony_ci	if (detail->last_warn != detail->last_close) {
11798c2ecf20Sopenharmony_ci		detail->last_warn = detail->last_close;
11808c2ecf20Sopenharmony_ci		if (detail->warn_no_listener)
11818c2ecf20Sopenharmony_ci			detail->warn_no_listener(detail, detail->last_close != 0);
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_cistatic bool cache_listeners_exist(struct cache_detail *detail)
11868c2ecf20Sopenharmony_ci{
11878c2ecf20Sopenharmony_ci	if (atomic_read(&detail->writers))
11888c2ecf20Sopenharmony_ci		return true;
11898c2ecf20Sopenharmony_ci	if (detail->last_close == 0)
11908c2ecf20Sopenharmony_ci		/* This cache was never opened */
11918c2ecf20Sopenharmony_ci		return false;
11928c2ecf20Sopenharmony_ci	if (detail->last_close < seconds_since_boot() - 30)
11938c2ecf20Sopenharmony_ci		/*
11948c2ecf20Sopenharmony_ci		 * We allow for the possibility that someone might
11958c2ecf20Sopenharmony_ci		 * restart a userspace daemon without restarting the
11968c2ecf20Sopenharmony_ci		 * server; but after 30 seconds, we give up.
11978c2ecf20Sopenharmony_ci		 */
11988c2ecf20Sopenharmony_ci		 return false;
11998c2ecf20Sopenharmony_ci	return true;
12008c2ecf20Sopenharmony_ci}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci/*
12038c2ecf20Sopenharmony_ci * register an upcall request to user-space and queue it up for read() by the
12048c2ecf20Sopenharmony_ci * upcall daemon.
12058c2ecf20Sopenharmony_ci *
12068c2ecf20Sopenharmony_ci * Each request is at most one page long.
12078c2ecf20Sopenharmony_ci */
12088c2ecf20Sopenharmony_cistatic int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	char *buf;
12118c2ecf20Sopenharmony_ci	struct cache_request *crq;
12128c2ecf20Sopenharmony_ci	int ret = 0;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	if (test_bit(CACHE_CLEANED, &h->flags))
12158c2ecf20Sopenharmony_ci		/* Too late to make an upcall */
12168c2ecf20Sopenharmony_ci		return -EAGAIN;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
12198c2ecf20Sopenharmony_ci	if (!buf)
12208c2ecf20Sopenharmony_ci		return -EAGAIN;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	crq = kmalloc(sizeof (*crq), GFP_KERNEL);
12238c2ecf20Sopenharmony_ci	if (!crq) {
12248c2ecf20Sopenharmony_ci		kfree(buf);
12258c2ecf20Sopenharmony_ci		return -EAGAIN;
12268c2ecf20Sopenharmony_ci	}
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	crq->q.reader = 0;
12298c2ecf20Sopenharmony_ci	crq->buf = buf;
12308c2ecf20Sopenharmony_ci	crq->len = 0;
12318c2ecf20Sopenharmony_ci	crq->readers = 0;
12328c2ecf20Sopenharmony_ci	spin_lock(&queue_lock);
12338c2ecf20Sopenharmony_ci	if (test_bit(CACHE_PENDING, &h->flags)) {
12348c2ecf20Sopenharmony_ci		crq->item = cache_get(h);
12358c2ecf20Sopenharmony_ci		list_add_tail(&crq->q.list, &detail->queue);
12368c2ecf20Sopenharmony_ci		trace_cache_entry_upcall(detail, h);
12378c2ecf20Sopenharmony_ci	} else
12388c2ecf20Sopenharmony_ci		/* Lost a race, no longer PENDING, so don't enqueue */
12398c2ecf20Sopenharmony_ci		ret = -EAGAIN;
12408c2ecf20Sopenharmony_ci	spin_unlock(&queue_lock);
12418c2ecf20Sopenharmony_ci	wake_up(&queue_wait);
12428c2ecf20Sopenharmony_ci	if (ret == -EAGAIN) {
12438c2ecf20Sopenharmony_ci		kfree(buf);
12448c2ecf20Sopenharmony_ci		kfree(crq);
12458c2ecf20Sopenharmony_ci	}
12468c2ecf20Sopenharmony_ci	return ret;
12478c2ecf20Sopenharmony_ci}
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ciint sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
12508c2ecf20Sopenharmony_ci{
12518c2ecf20Sopenharmony_ci	if (test_and_set_bit(CACHE_PENDING, &h->flags))
12528c2ecf20Sopenharmony_ci		return 0;
12538c2ecf20Sopenharmony_ci	return cache_pipe_upcall(detail, h);
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ciint sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
12588c2ecf20Sopenharmony_ci				     struct cache_head *h)
12598c2ecf20Sopenharmony_ci{
12608c2ecf20Sopenharmony_ci	if (!cache_listeners_exist(detail)) {
12618c2ecf20Sopenharmony_ci		warn_no_listener(detail);
12628c2ecf20Sopenharmony_ci		trace_cache_entry_no_listener(detail, h);
12638c2ecf20Sopenharmony_ci		return -EINVAL;
12648c2ecf20Sopenharmony_ci	}
12658c2ecf20Sopenharmony_ci	return sunrpc_cache_pipe_upcall(detail, h);
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout);
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci/*
12708c2ecf20Sopenharmony_ci * parse a message from user-space and pass it
12718c2ecf20Sopenharmony_ci * to an appropriate cache
12728c2ecf20Sopenharmony_ci * Messages are, like requests, separated into fields by
12738c2ecf20Sopenharmony_ci * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
12748c2ecf20Sopenharmony_ci *
12758c2ecf20Sopenharmony_ci * Message is
12768c2ecf20Sopenharmony_ci *   reply cachename expiry key ... content....
12778c2ecf20Sopenharmony_ci *
12788c2ecf20Sopenharmony_ci * key and content are both parsed by cache
12798c2ecf20Sopenharmony_ci */
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ciint qword_get(char **bpp, char *dest, int bufsize)
12828c2ecf20Sopenharmony_ci{
12838c2ecf20Sopenharmony_ci	/* return bytes copied, or -1 on error */
12848c2ecf20Sopenharmony_ci	char *bp = *bpp;
12858c2ecf20Sopenharmony_ci	int len = 0;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	while (*bp == ' ') bp++;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	if (bp[0] == '\\' && bp[1] == 'x') {
12908c2ecf20Sopenharmony_ci		/* HEX STRING */
12918c2ecf20Sopenharmony_ci		bp += 2;
12928c2ecf20Sopenharmony_ci		while (len < bufsize - 1) {
12938c2ecf20Sopenharmony_ci			int h, l;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci			h = hex_to_bin(bp[0]);
12968c2ecf20Sopenharmony_ci			if (h < 0)
12978c2ecf20Sopenharmony_ci				break;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci			l = hex_to_bin(bp[1]);
13008c2ecf20Sopenharmony_ci			if (l < 0)
13018c2ecf20Sopenharmony_ci				break;
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci			*dest++ = (h << 4) | l;
13048c2ecf20Sopenharmony_ci			bp += 2;
13058c2ecf20Sopenharmony_ci			len++;
13068c2ecf20Sopenharmony_ci		}
13078c2ecf20Sopenharmony_ci	} else {
13088c2ecf20Sopenharmony_ci		/* text with \nnn octal quoting */
13098c2ecf20Sopenharmony_ci		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
13108c2ecf20Sopenharmony_ci			if (*bp == '\\' &&
13118c2ecf20Sopenharmony_ci			    isodigit(bp[1]) && (bp[1] <= '3') &&
13128c2ecf20Sopenharmony_ci			    isodigit(bp[2]) &&
13138c2ecf20Sopenharmony_ci			    isodigit(bp[3])) {
13148c2ecf20Sopenharmony_ci				int byte = (*++bp -'0');
13158c2ecf20Sopenharmony_ci				bp++;
13168c2ecf20Sopenharmony_ci				byte = (byte << 3) | (*bp++ - '0');
13178c2ecf20Sopenharmony_ci				byte = (byte << 3) | (*bp++ - '0');
13188c2ecf20Sopenharmony_ci				*dest++ = byte;
13198c2ecf20Sopenharmony_ci				len++;
13208c2ecf20Sopenharmony_ci			} else {
13218c2ecf20Sopenharmony_ci				*dest++ = *bp++;
13228c2ecf20Sopenharmony_ci				len++;
13238c2ecf20Sopenharmony_ci			}
13248c2ecf20Sopenharmony_ci		}
13258c2ecf20Sopenharmony_ci	}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	if (*bp != ' ' && *bp != '\n' && *bp != '\0')
13288c2ecf20Sopenharmony_ci		return -1;
13298c2ecf20Sopenharmony_ci	while (*bp == ' ') bp++;
13308c2ecf20Sopenharmony_ci	*bpp = bp;
13318c2ecf20Sopenharmony_ci	*dest = '\0';
13328c2ecf20Sopenharmony_ci	return len;
13338c2ecf20Sopenharmony_ci}
13348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(qword_get);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci/*
13388c2ecf20Sopenharmony_ci * support /proc/net/rpc/$CACHENAME/content
13398c2ecf20Sopenharmony_ci * as a seqfile.
13408c2ecf20Sopenharmony_ci * We call ->cache_show passing NULL for the item to
13418c2ecf20Sopenharmony_ci * get a header, then pass each real item in the cache
13428c2ecf20Sopenharmony_ci */
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_cistatic void *__cache_seq_start(struct seq_file *m, loff_t *pos)
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	loff_t n = *pos;
13478c2ecf20Sopenharmony_ci	unsigned int hash, entry;
13488c2ecf20Sopenharmony_ci	struct cache_head *ch;
13498c2ecf20Sopenharmony_ci	struct cache_detail *cd = m->private;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	if (!n--)
13528c2ecf20Sopenharmony_ci		return SEQ_START_TOKEN;
13538c2ecf20Sopenharmony_ci	hash = n >> 32;
13548c2ecf20Sopenharmony_ci	entry = n & ((1LL<<32) - 1);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
13578c2ecf20Sopenharmony_ci		if (!entry--)
13588c2ecf20Sopenharmony_ci			return ch;
13598c2ecf20Sopenharmony_ci	n &= ~((1LL<<32) - 1);
13608c2ecf20Sopenharmony_ci	do {
13618c2ecf20Sopenharmony_ci		hash++;
13628c2ecf20Sopenharmony_ci		n += 1LL<<32;
13638c2ecf20Sopenharmony_ci	} while(hash < cd->hash_size &&
13648c2ecf20Sopenharmony_ci		hlist_empty(&cd->hash_table[hash]));
13658c2ecf20Sopenharmony_ci	if (hash >= cd->hash_size)
13668c2ecf20Sopenharmony_ci		return NULL;
13678c2ecf20Sopenharmony_ci	*pos = n+1;
13688c2ecf20Sopenharmony_ci	return hlist_entry_safe(rcu_dereference_raw(
13698c2ecf20Sopenharmony_ci				hlist_first_rcu(&cd->hash_table[hash])),
13708c2ecf20Sopenharmony_ci				struct cache_head, cache_list);
13718c2ecf20Sopenharmony_ci}
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_cistatic void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
13748c2ecf20Sopenharmony_ci{
13758c2ecf20Sopenharmony_ci	struct cache_head *ch = p;
13768c2ecf20Sopenharmony_ci	int hash = (*pos >> 32);
13778c2ecf20Sopenharmony_ci	struct cache_detail *cd = m->private;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	if (p == SEQ_START_TOKEN)
13808c2ecf20Sopenharmony_ci		hash = 0;
13818c2ecf20Sopenharmony_ci	else if (ch->cache_list.next == NULL) {
13828c2ecf20Sopenharmony_ci		hash++;
13838c2ecf20Sopenharmony_ci		*pos += 1LL<<32;
13848c2ecf20Sopenharmony_ci	} else {
13858c2ecf20Sopenharmony_ci		++*pos;
13868c2ecf20Sopenharmony_ci		return hlist_entry_safe(rcu_dereference_raw(
13878c2ecf20Sopenharmony_ci					hlist_next_rcu(&ch->cache_list)),
13888c2ecf20Sopenharmony_ci					struct cache_head, cache_list);
13898c2ecf20Sopenharmony_ci	}
13908c2ecf20Sopenharmony_ci	*pos &= ~((1LL<<32) - 1);
13918c2ecf20Sopenharmony_ci	while (hash < cd->hash_size &&
13928c2ecf20Sopenharmony_ci	       hlist_empty(&cd->hash_table[hash])) {
13938c2ecf20Sopenharmony_ci		hash++;
13948c2ecf20Sopenharmony_ci		*pos += 1LL<<32;
13958c2ecf20Sopenharmony_ci	}
13968c2ecf20Sopenharmony_ci	if (hash >= cd->hash_size)
13978c2ecf20Sopenharmony_ci		return NULL;
13988c2ecf20Sopenharmony_ci	++*pos;
13998c2ecf20Sopenharmony_ci	return hlist_entry_safe(rcu_dereference_raw(
14008c2ecf20Sopenharmony_ci				hlist_first_rcu(&cd->hash_table[hash])),
14018c2ecf20Sopenharmony_ci				struct cache_head, cache_list);
14028c2ecf20Sopenharmony_ci}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_civoid *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
14058c2ecf20Sopenharmony_ci	__acquires(RCU)
14068c2ecf20Sopenharmony_ci{
14078c2ecf20Sopenharmony_ci	rcu_read_lock();
14088c2ecf20Sopenharmony_ci	return __cache_seq_start(m, pos);
14098c2ecf20Sopenharmony_ci}
14108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_seq_start_rcu);
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_civoid *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
14138c2ecf20Sopenharmony_ci{
14148c2ecf20Sopenharmony_ci	return cache_seq_next(file, p, pos);
14158c2ecf20Sopenharmony_ci}
14168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_seq_next_rcu);
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_civoid cache_seq_stop_rcu(struct seq_file *m, void *p)
14198c2ecf20Sopenharmony_ci	__releases(RCU)
14208c2ecf20Sopenharmony_ci{
14218c2ecf20Sopenharmony_ci	rcu_read_unlock();
14228c2ecf20Sopenharmony_ci}
14238c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_cistatic int c_show(struct seq_file *m, void *p)
14268c2ecf20Sopenharmony_ci{
14278c2ecf20Sopenharmony_ci	struct cache_head *cp = p;
14288c2ecf20Sopenharmony_ci	struct cache_detail *cd = m->private;
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	if (p == SEQ_START_TOKEN)
14318c2ecf20Sopenharmony_ci		return cd->cache_show(m, cd, NULL);
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	ifdebug(CACHE)
14348c2ecf20Sopenharmony_ci		seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
14358c2ecf20Sopenharmony_ci			   convert_to_wallclock(cp->expiry_time),
14368c2ecf20Sopenharmony_ci			   kref_read(&cp->ref), cp->flags);
14378c2ecf20Sopenharmony_ci	cache_get(cp);
14388c2ecf20Sopenharmony_ci	if (cache_check(cd, cp, NULL))
14398c2ecf20Sopenharmony_ci		/* cache_check does a cache_put on failure */
14408c2ecf20Sopenharmony_ci		seq_puts(m, "# ");
14418c2ecf20Sopenharmony_ci	else {
14428c2ecf20Sopenharmony_ci		if (cache_is_expired(cd, cp))
14438c2ecf20Sopenharmony_ci			seq_puts(m, "# ");
14448c2ecf20Sopenharmony_ci		cache_put(cp, cd);
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	return cd->cache_show(m, cd, cp);
14488c2ecf20Sopenharmony_ci}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_cistatic const struct seq_operations cache_content_op = {
14518c2ecf20Sopenharmony_ci	.start	= cache_seq_start_rcu,
14528c2ecf20Sopenharmony_ci	.next	= cache_seq_next_rcu,
14538c2ecf20Sopenharmony_ci	.stop	= cache_seq_stop_rcu,
14548c2ecf20Sopenharmony_ci	.show	= c_show,
14558c2ecf20Sopenharmony_ci};
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_cistatic int content_open(struct inode *inode, struct file *file,
14588c2ecf20Sopenharmony_ci			struct cache_detail *cd)
14598c2ecf20Sopenharmony_ci{
14608c2ecf20Sopenharmony_ci	struct seq_file *seq;
14618c2ecf20Sopenharmony_ci	int err;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	if (!cd || !try_module_get(cd->owner))
14648c2ecf20Sopenharmony_ci		return -EACCES;
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci	err = seq_open(file, &cache_content_op);
14678c2ecf20Sopenharmony_ci	if (err) {
14688c2ecf20Sopenharmony_ci		module_put(cd->owner);
14698c2ecf20Sopenharmony_ci		return err;
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	seq = file->private_data;
14738c2ecf20Sopenharmony_ci	seq->private = cd;
14748c2ecf20Sopenharmony_ci	return 0;
14758c2ecf20Sopenharmony_ci}
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_cistatic int content_release(struct inode *inode, struct file *file,
14788c2ecf20Sopenharmony_ci		struct cache_detail *cd)
14798c2ecf20Sopenharmony_ci{
14808c2ecf20Sopenharmony_ci	int ret = seq_release(inode, file);
14818c2ecf20Sopenharmony_ci	module_put(cd->owner);
14828c2ecf20Sopenharmony_ci	return ret;
14838c2ecf20Sopenharmony_ci}
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_cistatic int open_flush(struct inode *inode, struct file *file,
14868c2ecf20Sopenharmony_ci			struct cache_detail *cd)
14878c2ecf20Sopenharmony_ci{
14888c2ecf20Sopenharmony_ci	if (!cd || !try_module_get(cd->owner))
14898c2ecf20Sopenharmony_ci		return -EACCES;
14908c2ecf20Sopenharmony_ci	return nonseekable_open(inode, file);
14918c2ecf20Sopenharmony_ci}
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_cistatic int release_flush(struct inode *inode, struct file *file,
14948c2ecf20Sopenharmony_ci			struct cache_detail *cd)
14958c2ecf20Sopenharmony_ci{
14968c2ecf20Sopenharmony_ci	module_put(cd->owner);
14978c2ecf20Sopenharmony_ci	return 0;
14988c2ecf20Sopenharmony_ci}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_cistatic ssize_t read_flush(struct file *file, char __user *buf,
15018c2ecf20Sopenharmony_ci			  size_t count, loff_t *ppos,
15028c2ecf20Sopenharmony_ci			  struct cache_detail *cd)
15038c2ecf20Sopenharmony_ci{
15048c2ecf20Sopenharmony_ci	char tbuf[22];
15058c2ecf20Sopenharmony_ci	size_t len;
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
15088c2ecf20Sopenharmony_ci			convert_to_wallclock(cd->flush_time));
15098c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, count, ppos, tbuf, len);
15108c2ecf20Sopenharmony_ci}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_cistatic ssize_t write_flush(struct file *file, const char __user *buf,
15138c2ecf20Sopenharmony_ci			   size_t count, loff_t *ppos,
15148c2ecf20Sopenharmony_ci			   struct cache_detail *cd)
15158c2ecf20Sopenharmony_ci{
15168c2ecf20Sopenharmony_ci	char tbuf[20];
15178c2ecf20Sopenharmony_ci	char *ep;
15188c2ecf20Sopenharmony_ci	time64_t now;
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	if (*ppos || count > sizeof(tbuf)-1)
15218c2ecf20Sopenharmony_ci		return -EINVAL;
15228c2ecf20Sopenharmony_ci	if (copy_from_user(tbuf, buf, count))
15238c2ecf20Sopenharmony_ci		return -EFAULT;
15248c2ecf20Sopenharmony_ci	tbuf[count] = 0;
15258c2ecf20Sopenharmony_ci	simple_strtoul(tbuf, &ep, 0);
15268c2ecf20Sopenharmony_ci	if (*ep && *ep != '\n')
15278c2ecf20Sopenharmony_ci		return -EINVAL;
15288c2ecf20Sopenharmony_ci	/* Note that while we check that 'buf' holds a valid number,
15298c2ecf20Sopenharmony_ci	 * we always ignore the value and just flush everything.
15308c2ecf20Sopenharmony_ci	 * Making use of the number leads to races.
15318c2ecf20Sopenharmony_ci	 */
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	now = seconds_since_boot();
15348c2ecf20Sopenharmony_ci	/* Always flush everything, so behave like cache_purge()
15358c2ecf20Sopenharmony_ci	 * Do this by advancing flush_time to the current time,
15368c2ecf20Sopenharmony_ci	 * or by one second if it has already reached the current time.
15378c2ecf20Sopenharmony_ci	 * Newly added cache entries will always have ->last_refresh greater
15388c2ecf20Sopenharmony_ci	 * that ->flush_time, so they don't get flushed prematurely.
15398c2ecf20Sopenharmony_ci	 */
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	if (cd->flush_time >= now)
15428c2ecf20Sopenharmony_ci		now = cd->flush_time + 1;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	cd->flush_time = now;
15458c2ecf20Sopenharmony_ci	cd->nextcheck = now;
15468c2ecf20Sopenharmony_ci	cache_flush();
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	if (cd->flush)
15498c2ecf20Sopenharmony_ci		cd->flush();
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	*ppos += count;
15528c2ecf20Sopenharmony_ci	return count;
15538c2ecf20Sopenharmony_ci}
15548c2ecf20Sopenharmony_ci
15558c2ecf20Sopenharmony_cistatic ssize_t cache_read_procfs(struct file *filp, char __user *buf,
15568c2ecf20Sopenharmony_ci				 size_t count, loff_t *ppos)
15578c2ecf20Sopenharmony_ci{
15588c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(file_inode(filp));
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	return cache_read(filp, buf, count, ppos, cd);
15618c2ecf20Sopenharmony_ci}
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_cistatic ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
15648c2ecf20Sopenharmony_ci				  size_t count, loff_t *ppos)
15658c2ecf20Sopenharmony_ci{
15668c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(file_inode(filp));
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	return cache_write(filp, buf, count, ppos, cd);
15698c2ecf20Sopenharmony_ci}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_cistatic __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
15728c2ecf20Sopenharmony_ci{
15738c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(file_inode(filp));
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	return cache_poll(filp, wait, cd);
15768c2ecf20Sopenharmony_ci}
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_cistatic long cache_ioctl_procfs(struct file *filp,
15798c2ecf20Sopenharmony_ci			       unsigned int cmd, unsigned long arg)
15808c2ecf20Sopenharmony_ci{
15818c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(filp);
15828c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	return cache_ioctl(inode, filp, cmd, arg, cd);
15858c2ecf20Sopenharmony_ci}
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_cistatic int cache_open_procfs(struct inode *inode, struct file *filp)
15888c2ecf20Sopenharmony_ci{
15898c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	return cache_open(inode, filp, cd);
15928c2ecf20Sopenharmony_ci}
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_cistatic int cache_release_procfs(struct inode *inode, struct file *filp)
15958c2ecf20Sopenharmony_ci{
15968c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	return cache_release(inode, filp, cd);
15998c2ecf20Sopenharmony_ci}
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_cistatic const struct proc_ops cache_channel_proc_ops = {
16028c2ecf20Sopenharmony_ci	.proc_lseek	= no_llseek,
16038c2ecf20Sopenharmony_ci	.proc_read	= cache_read_procfs,
16048c2ecf20Sopenharmony_ci	.proc_write	= cache_write_procfs,
16058c2ecf20Sopenharmony_ci	.proc_poll	= cache_poll_procfs,
16068c2ecf20Sopenharmony_ci	.proc_ioctl	= cache_ioctl_procfs, /* for FIONREAD */
16078c2ecf20Sopenharmony_ci	.proc_open	= cache_open_procfs,
16088c2ecf20Sopenharmony_ci	.proc_release	= cache_release_procfs,
16098c2ecf20Sopenharmony_ci};
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_cistatic int content_open_procfs(struct inode *inode, struct file *filp)
16128c2ecf20Sopenharmony_ci{
16138c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	return content_open(inode, filp, cd);
16168c2ecf20Sopenharmony_ci}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_cistatic int content_release_procfs(struct inode *inode, struct file *filp)
16198c2ecf20Sopenharmony_ci{
16208c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	return content_release(inode, filp, cd);
16238c2ecf20Sopenharmony_ci}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_cistatic const struct proc_ops content_proc_ops = {
16268c2ecf20Sopenharmony_ci	.proc_open	= content_open_procfs,
16278c2ecf20Sopenharmony_ci	.proc_read	= seq_read,
16288c2ecf20Sopenharmony_ci	.proc_lseek	= seq_lseek,
16298c2ecf20Sopenharmony_ci	.proc_release	= content_release_procfs,
16308c2ecf20Sopenharmony_ci};
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_cistatic int open_flush_procfs(struct inode *inode, struct file *filp)
16338c2ecf20Sopenharmony_ci{
16348c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	return open_flush(inode, filp, cd);
16378c2ecf20Sopenharmony_ci}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_cistatic int release_flush_procfs(struct inode *inode, struct file *filp)
16408c2ecf20Sopenharmony_ci{
16418c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(inode);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	return release_flush(inode, filp, cd);
16448c2ecf20Sopenharmony_ci}
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_cistatic ssize_t read_flush_procfs(struct file *filp, char __user *buf,
16478c2ecf20Sopenharmony_ci			    size_t count, loff_t *ppos)
16488c2ecf20Sopenharmony_ci{
16498c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(file_inode(filp));
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	return read_flush(filp, buf, count, ppos, cd);
16528c2ecf20Sopenharmony_ci}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_cistatic ssize_t write_flush_procfs(struct file *filp,
16558c2ecf20Sopenharmony_ci				  const char __user *buf,
16568c2ecf20Sopenharmony_ci				  size_t count, loff_t *ppos)
16578c2ecf20Sopenharmony_ci{
16588c2ecf20Sopenharmony_ci	struct cache_detail *cd = PDE_DATA(file_inode(filp));
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_ci	return write_flush(filp, buf, count, ppos, cd);
16618c2ecf20Sopenharmony_ci}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_cistatic const struct proc_ops cache_flush_proc_ops = {
16648c2ecf20Sopenharmony_ci	.proc_open	= open_flush_procfs,
16658c2ecf20Sopenharmony_ci	.proc_read	= read_flush_procfs,
16668c2ecf20Sopenharmony_ci	.proc_write	= write_flush_procfs,
16678c2ecf20Sopenharmony_ci	.proc_release	= release_flush_procfs,
16688c2ecf20Sopenharmony_ci	.proc_lseek	= no_llseek,
16698c2ecf20Sopenharmony_ci};
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_cistatic void remove_cache_proc_entries(struct cache_detail *cd)
16728c2ecf20Sopenharmony_ci{
16738c2ecf20Sopenharmony_ci	if (cd->procfs) {
16748c2ecf20Sopenharmony_ci		proc_remove(cd->procfs);
16758c2ecf20Sopenharmony_ci		cd->procfs = NULL;
16768c2ecf20Sopenharmony_ci	}
16778c2ecf20Sopenharmony_ci}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
16808c2ecf20Sopenharmony_cistatic int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
16818c2ecf20Sopenharmony_ci{
16828c2ecf20Sopenharmony_ci	struct proc_dir_entry *p;
16838c2ecf20Sopenharmony_ci	struct sunrpc_net *sn;
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	sn = net_generic(net, sunrpc_net_id);
16868c2ecf20Sopenharmony_ci	cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
16878c2ecf20Sopenharmony_ci	if (cd->procfs == NULL)
16888c2ecf20Sopenharmony_ci		goto out_nomem;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	p = proc_create_data("flush", S_IFREG | 0600,
16918c2ecf20Sopenharmony_ci			     cd->procfs, &cache_flush_proc_ops, cd);
16928c2ecf20Sopenharmony_ci	if (p == NULL)
16938c2ecf20Sopenharmony_ci		goto out_nomem;
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	if (cd->cache_request || cd->cache_parse) {
16968c2ecf20Sopenharmony_ci		p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
16978c2ecf20Sopenharmony_ci				     &cache_channel_proc_ops, cd);
16988c2ecf20Sopenharmony_ci		if (p == NULL)
16998c2ecf20Sopenharmony_ci			goto out_nomem;
17008c2ecf20Sopenharmony_ci	}
17018c2ecf20Sopenharmony_ci	if (cd->cache_show) {
17028c2ecf20Sopenharmony_ci		p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
17038c2ecf20Sopenharmony_ci				     &content_proc_ops, cd);
17048c2ecf20Sopenharmony_ci		if (p == NULL)
17058c2ecf20Sopenharmony_ci			goto out_nomem;
17068c2ecf20Sopenharmony_ci	}
17078c2ecf20Sopenharmony_ci	return 0;
17088c2ecf20Sopenharmony_ciout_nomem:
17098c2ecf20Sopenharmony_ci	remove_cache_proc_entries(cd);
17108c2ecf20Sopenharmony_ci	return -ENOMEM;
17118c2ecf20Sopenharmony_ci}
17128c2ecf20Sopenharmony_ci#else /* CONFIG_PROC_FS */
17138c2ecf20Sopenharmony_cistatic int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
17148c2ecf20Sopenharmony_ci{
17158c2ecf20Sopenharmony_ci	return 0;
17168c2ecf20Sopenharmony_ci}
17178c2ecf20Sopenharmony_ci#endif
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_civoid __init cache_initialize(void)
17208c2ecf20Sopenharmony_ci{
17218c2ecf20Sopenharmony_ci	INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
17228c2ecf20Sopenharmony_ci}
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ciint cache_register_net(struct cache_detail *cd, struct net *net)
17258c2ecf20Sopenharmony_ci{
17268c2ecf20Sopenharmony_ci	int ret;
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	sunrpc_init_cache_detail(cd);
17298c2ecf20Sopenharmony_ci	ret = create_cache_proc_entries(cd, net);
17308c2ecf20Sopenharmony_ci	if (ret)
17318c2ecf20Sopenharmony_ci		sunrpc_destroy_cache_detail(cd);
17328c2ecf20Sopenharmony_ci	return ret;
17338c2ecf20Sopenharmony_ci}
17348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_register_net);
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_civoid cache_unregister_net(struct cache_detail *cd, struct net *net)
17378c2ecf20Sopenharmony_ci{
17388c2ecf20Sopenharmony_ci	remove_cache_proc_entries(cd);
17398c2ecf20Sopenharmony_ci	sunrpc_destroy_cache_detail(cd);
17408c2ecf20Sopenharmony_ci}
17418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_unregister_net);
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_cistruct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
17448c2ecf20Sopenharmony_ci{
17458c2ecf20Sopenharmony_ci	struct cache_detail *cd;
17468c2ecf20Sopenharmony_ci	int i;
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
17498c2ecf20Sopenharmony_ci	if (cd == NULL)
17508c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
17518c2ecf20Sopenharmony_ci
17528c2ecf20Sopenharmony_ci	cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
17538c2ecf20Sopenharmony_ci				 GFP_KERNEL);
17548c2ecf20Sopenharmony_ci	if (cd->hash_table == NULL) {
17558c2ecf20Sopenharmony_ci		kfree(cd);
17568c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
17578c2ecf20Sopenharmony_ci	}
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	for (i = 0; i < cd->hash_size; i++)
17608c2ecf20Sopenharmony_ci		INIT_HLIST_HEAD(&cd->hash_table[i]);
17618c2ecf20Sopenharmony_ci	cd->net = net;
17628c2ecf20Sopenharmony_ci	return cd;
17638c2ecf20Sopenharmony_ci}
17648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_create_net);
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_civoid cache_destroy_net(struct cache_detail *cd, struct net *net)
17678c2ecf20Sopenharmony_ci{
17688c2ecf20Sopenharmony_ci	kfree(cd->hash_table);
17698c2ecf20Sopenharmony_ci	kfree(cd);
17708c2ecf20Sopenharmony_ci}
17718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cache_destroy_net);
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_cistatic ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
17748c2ecf20Sopenharmony_ci				 size_t count, loff_t *ppos)
17758c2ecf20Sopenharmony_ci{
17768c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci	return cache_read(filp, buf, count, ppos, cd);
17798c2ecf20Sopenharmony_ci}
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_cistatic ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
17828c2ecf20Sopenharmony_ci				  size_t count, loff_t *ppos)
17838c2ecf20Sopenharmony_ci{
17848c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	return cache_write(filp, buf, count, ppos, cd);
17878c2ecf20Sopenharmony_ci}
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_cistatic __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
17908c2ecf20Sopenharmony_ci{
17918c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci	return cache_poll(filp, wait, cd);
17948c2ecf20Sopenharmony_ci}
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_cistatic long cache_ioctl_pipefs(struct file *filp,
17978c2ecf20Sopenharmony_ci			      unsigned int cmd, unsigned long arg)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(filp);
18008c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	return cache_ioctl(inode, filp, cmd, arg, cd);
18038c2ecf20Sopenharmony_ci}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_cistatic int cache_open_pipefs(struct inode *inode, struct file *filp)
18068c2ecf20Sopenharmony_ci{
18078c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	return cache_open(inode, filp, cd);
18108c2ecf20Sopenharmony_ci}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_cistatic int cache_release_pipefs(struct inode *inode, struct file *filp)
18138c2ecf20Sopenharmony_ci{
18148c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	return cache_release(inode, filp, cd);
18178c2ecf20Sopenharmony_ci}
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ciconst struct file_operations cache_file_operations_pipefs = {
18208c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
18218c2ecf20Sopenharmony_ci	.llseek		= no_llseek,
18228c2ecf20Sopenharmony_ci	.read		= cache_read_pipefs,
18238c2ecf20Sopenharmony_ci	.write		= cache_write_pipefs,
18248c2ecf20Sopenharmony_ci	.poll		= cache_poll_pipefs,
18258c2ecf20Sopenharmony_ci	.unlocked_ioctl	= cache_ioctl_pipefs, /* for FIONREAD */
18268c2ecf20Sopenharmony_ci	.open		= cache_open_pipefs,
18278c2ecf20Sopenharmony_ci	.release	= cache_release_pipefs,
18288c2ecf20Sopenharmony_ci};
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_cistatic int content_open_pipefs(struct inode *inode, struct file *filp)
18318c2ecf20Sopenharmony_ci{
18328c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	return content_open(inode, filp, cd);
18358c2ecf20Sopenharmony_ci}
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_cistatic int content_release_pipefs(struct inode *inode, struct file *filp)
18388c2ecf20Sopenharmony_ci{
18398c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_ci	return content_release(inode, filp, cd);
18428c2ecf20Sopenharmony_ci}
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ciconst struct file_operations content_file_operations_pipefs = {
18458c2ecf20Sopenharmony_ci	.open		= content_open_pipefs,
18468c2ecf20Sopenharmony_ci	.read		= seq_read,
18478c2ecf20Sopenharmony_ci	.llseek		= seq_lseek,
18488c2ecf20Sopenharmony_ci	.release	= content_release_pipefs,
18498c2ecf20Sopenharmony_ci};
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_cistatic int open_flush_pipefs(struct inode *inode, struct file *filp)
18528c2ecf20Sopenharmony_ci{
18538c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	return open_flush(inode, filp, cd);
18568c2ecf20Sopenharmony_ci}
18578c2ecf20Sopenharmony_ci
18588c2ecf20Sopenharmony_cistatic int release_flush_pipefs(struct inode *inode, struct file *filp)
18598c2ecf20Sopenharmony_ci{
18608c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(inode)->private;
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_ci	return release_flush(inode, filp, cd);
18638c2ecf20Sopenharmony_ci}
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_cistatic ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
18668c2ecf20Sopenharmony_ci			    size_t count, loff_t *ppos)
18678c2ecf20Sopenharmony_ci{
18688c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	return read_flush(filp, buf, count, ppos, cd);
18718c2ecf20Sopenharmony_ci}
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_cistatic ssize_t write_flush_pipefs(struct file *filp,
18748c2ecf20Sopenharmony_ci				  const char __user *buf,
18758c2ecf20Sopenharmony_ci				  size_t count, loff_t *ppos)
18768c2ecf20Sopenharmony_ci{
18778c2ecf20Sopenharmony_ci	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci	return write_flush(filp, buf, count, ppos, cd);
18808c2ecf20Sopenharmony_ci}
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ciconst struct file_operations cache_flush_operations_pipefs = {
18838c2ecf20Sopenharmony_ci	.open		= open_flush_pipefs,
18848c2ecf20Sopenharmony_ci	.read		= read_flush_pipefs,
18858c2ecf20Sopenharmony_ci	.write		= write_flush_pipefs,
18868c2ecf20Sopenharmony_ci	.release	= release_flush_pipefs,
18878c2ecf20Sopenharmony_ci	.llseek		= no_llseek,
18888c2ecf20Sopenharmony_ci};
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ciint sunrpc_cache_register_pipefs(struct dentry *parent,
18918c2ecf20Sopenharmony_ci				 const char *name, umode_t umode,
18928c2ecf20Sopenharmony_ci				 struct cache_detail *cd)
18938c2ecf20Sopenharmony_ci{
18948c2ecf20Sopenharmony_ci	struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
18958c2ecf20Sopenharmony_ci	if (IS_ERR(dir))
18968c2ecf20Sopenharmony_ci		return PTR_ERR(dir);
18978c2ecf20Sopenharmony_ci	cd->pipefs = dir;
18988c2ecf20Sopenharmony_ci	return 0;
18998c2ecf20Sopenharmony_ci}
19008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_civoid sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
19038c2ecf20Sopenharmony_ci{
19048c2ecf20Sopenharmony_ci	if (cd->pipefs) {
19058c2ecf20Sopenharmony_ci		rpc_remove_cache_dir(cd->pipefs);
19068c2ecf20Sopenharmony_ci		cd->pipefs = NULL;
19078c2ecf20Sopenharmony_ci	}
19088c2ecf20Sopenharmony_ci}
19098c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_civoid sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
19128c2ecf20Sopenharmony_ci{
19138c2ecf20Sopenharmony_ci	spin_lock(&cd->hash_lock);
19148c2ecf20Sopenharmony_ci	if (!hlist_unhashed(&h->cache_list)){
19158c2ecf20Sopenharmony_ci		sunrpc_begin_cache_remove_entry(h, cd);
19168c2ecf20Sopenharmony_ci		spin_unlock(&cd->hash_lock);
19178c2ecf20Sopenharmony_ci		sunrpc_end_cache_remove_entry(h, cd);
19188c2ecf20Sopenharmony_ci	} else
19198c2ecf20Sopenharmony_ci		spin_unlock(&cd->hash_lock);
19208c2ecf20Sopenharmony_ci}
19218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunrpc_cache_unhash);
1922