xref: /OK3568_Linux_fs/kernel/net/sunrpc/cache.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * net/sunrpc/cache.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Generic code for various authentication-related caches
6*4882a593Smuzhiyun  * used by sunrpc clients and servers.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/file.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/signal.h>
16*4882a593Smuzhiyun #include <linux/sched.h>
17*4882a593Smuzhiyun #include <linux/kmod.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/ctype.h>
21*4882a593Smuzhiyun #include <linux/string_helpers.h>
22*4882a593Smuzhiyun #include <linux/uaccess.h>
23*4882a593Smuzhiyun #include <linux/poll.h>
24*4882a593Smuzhiyun #include <linux/seq_file.h>
25*4882a593Smuzhiyun #include <linux/proc_fs.h>
26*4882a593Smuzhiyun #include <linux/net.h>
27*4882a593Smuzhiyun #include <linux/workqueue.h>
28*4882a593Smuzhiyun #include <linux/mutex.h>
29*4882a593Smuzhiyun #include <linux/pagemap.h>
30*4882a593Smuzhiyun #include <asm/ioctls.h>
31*4882a593Smuzhiyun #include <linux/sunrpc/types.h>
32*4882a593Smuzhiyun #include <linux/sunrpc/cache.h>
33*4882a593Smuzhiyun #include <linux/sunrpc/stats.h>
34*4882a593Smuzhiyun #include <linux/sunrpc/rpc_pipe_fs.h>
35*4882a593Smuzhiyun #include <trace/events/sunrpc.h>
36*4882a593Smuzhiyun #include "netns.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define	 RPCDBG_FACILITY RPCDBG_CACHE
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
41*4882a593Smuzhiyun static void cache_revisit_request(struct cache_head *item);
42*4882a593Smuzhiyun 
cache_init(struct cache_head * h,struct cache_detail * detail)43*4882a593Smuzhiyun static void cache_init(struct cache_head *h, struct cache_detail *detail)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	time64_t now = seconds_since_boot();
46*4882a593Smuzhiyun 	INIT_HLIST_NODE(&h->cache_list);
47*4882a593Smuzhiyun 	h->flags = 0;
48*4882a593Smuzhiyun 	kref_init(&h->ref);
49*4882a593Smuzhiyun 	h->expiry_time = now + CACHE_NEW_EXPIRY;
50*4882a593Smuzhiyun 	if (now <= detail->flush_time)
51*4882a593Smuzhiyun 		/* ensure it isn't already expired */
52*4882a593Smuzhiyun 		now = detail->flush_time + 1;
53*4882a593Smuzhiyun 	h->last_refresh = now;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static void cache_fresh_unlocked(struct cache_head *head,
57*4882a593Smuzhiyun 				struct cache_detail *detail);
58*4882a593Smuzhiyun 
sunrpc_cache_find_rcu(struct cache_detail * detail,struct cache_head * key,int hash)59*4882a593Smuzhiyun static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
60*4882a593Smuzhiyun 						struct cache_head *key,
61*4882a593Smuzhiyun 						int hash)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	struct hlist_head *head = &detail->hash_table[hash];
64*4882a593Smuzhiyun 	struct cache_head *tmp;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	rcu_read_lock();
67*4882a593Smuzhiyun 	hlist_for_each_entry_rcu(tmp, head, cache_list) {
68*4882a593Smuzhiyun 		if (!detail->match(tmp, key))
69*4882a593Smuzhiyun 			continue;
70*4882a593Smuzhiyun 		if (test_bit(CACHE_VALID, &tmp->flags) &&
71*4882a593Smuzhiyun 		    cache_is_expired(detail, tmp))
72*4882a593Smuzhiyun 			continue;
73*4882a593Smuzhiyun 		tmp = cache_get_rcu(tmp);
74*4882a593Smuzhiyun 		rcu_read_unlock();
75*4882a593Smuzhiyun 		return tmp;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	rcu_read_unlock();
78*4882a593Smuzhiyun 	return NULL;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
sunrpc_begin_cache_remove_entry(struct cache_head * ch,struct cache_detail * cd)81*4882a593Smuzhiyun static void sunrpc_begin_cache_remove_entry(struct cache_head *ch,
82*4882a593Smuzhiyun 					    struct cache_detail *cd)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	/* Must be called under cd->hash_lock */
85*4882a593Smuzhiyun 	hlist_del_init_rcu(&ch->cache_list);
86*4882a593Smuzhiyun 	set_bit(CACHE_CLEANED, &ch->flags);
87*4882a593Smuzhiyun 	cd->entries --;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
sunrpc_end_cache_remove_entry(struct cache_head * ch,struct cache_detail * cd)90*4882a593Smuzhiyun static void sunrpc_end_cache_remove_entry(struct cache_head *ch,
91*4882a593Smuzhiyun 					  struct cache_detail *cd)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	cache_fresh_unlocked(ch, cd);
94*4882a593Smuzhiyun 	cache_put(ch, cd);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
sunrpc_cache_add_entry(struct cache_detail * detail,struct cache_head * key,int hash)97*4882a593Smuzhiyun static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
98*4882a593Smuzhiyun 						 struct cache_head *key,
99*4882a593Smuzhiyun 						 int hash)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	struct cache_head *new, *tmp, *freeme = NULL;
102*4882a593Smuzhiyun 	struct hlist_head *head = &detail->hash_table[hash];
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	new = detail->alloc();
105*4882a593Smuzhiyun 	if (!new)
106*4882a593Smuzhiyun 		return NULL;
107*4882a593Smuzhiyun 	/* must fully initialise 'new', else
108*4882a593Smuzhiyun 	 * we might get lose if we need to
109*4882a593Smuzhiyun 	 * cache_put it soon.
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	cache_init(new, detail);
112*4882a593Smuzhiyun 	detail->init(new, key);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	spin_lock(&detail->hash_lock);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* check if entry appeared while we slept */
117*4882a593Smuzhiyun 	hlist_for_each_entry_rcu(tmp, head, cache_list,
118*4882a593Smuzhiyun 				 lockdep_is_held(&detail->hash_lock)) {
119*4882a593Smuzhiyun 		if (!detail->match(tmp, key))
120*4882a593Smuzhiyun 			continue;
121*4882a593Smuzhiyun 		if (test_bit(CACHE_VALID, &tmp->flags) &&
122*4882a593Smuzhiyun 		    cache_is_expired(detail, tmp)) {
123*4882a593Smuzhiyun 			sunrpc_begin_cache_remove_entry(tmp, detail);
124*4882a593Smuzhiyun 			trace_cache_entry_expired(detail, tmp);
125*4882a593Smuzhiyun 			freeme = tmp;
126*4882a593Smuzhiyun 			break;
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 		cache_get(tmp);
129*4882a593Smuzhiyun 		spin_unlock(&detail->hash_lock);
130*4882a593Smuzhiyun 		cache_put(new, detail);
131*4882a593Smuzhiyun 		return tmp;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	hlist_add_head_rcu(&new->cache_list, head);
135*4882a593Smuzhiyun 	detail->entries++;
136*4882a593Smuzhiyun 	cache_get(new);
137*4882a593Smuzhiyun 	spin_unlock(&detail->hash_lock);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (freeme)
140*4882a593Smuzhiyun 		sunrpc_end_cache_remove_entry(freeme, detail);
141*4882a593Smuzhiyun 	return new;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
sunrpc_cache_lookup_rcu(struct cache_detail * detail,struct cache_head * key,int hash)144*4882a593Smuzhiyun struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
145*4882a593Smuzhiyun 					   struct cache_head *key, int hash)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct cache_head *ret;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	ret = sunrpc_cache_find_rcu(detail, key, hash);
150*4882a593Smuzhiyun 	if (ret)
151*4882a593Smuzhiyun 		return ret;
152*4882a593Smuzhiyun 	/* Didn't find anything, insert an empty entry */
153*4882a593Smuzhiyun 	return sunrpc_cache_add_entry(detail, key, hash);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
158*4882a593Smuzhiyun 
cache_fresh_locked(struct cache_head * head,time64_t expiry,struct cache_detail * detail)159*4882a593Smuzhiyun static void cache_fresh_locked(struct cache_head *head, time64_t expiry,
160*4882a593Smuzhiyun 			       struct cache_detail *detail)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	time64_t now = seconds_since_boot();
163*4882a593Smuzhiyun 	if (now <= detail->flush_time)
164*4882a593Smuzhiyun 		/* ensure it isn't immediately treated as expired */
165*4882a593Smuzhiyun 		now = detail->flush_time + 1;
166*4882a593Smuzhiyun 	head->expiry_time = expiry;
167*4882a593Smuzhiyun 	head->last_refresh = now;
168*4882a593Smuzhiyun 	smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
169*4882a593Smuzhiyun 	set_bit(CACHE_VALID, &head->flags);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
cache_fresh_unlocked(struct cache_head * head,struct cache_detail * detail)172*4882a593Smuzhiyun static void cache_fresh_unlocked(struct cache_head *head,
173*4882a593Smuzhiyun 				 struct cache_detail *detail)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
176*4882a593Smuzhiyun 		cache_revisit_request(head);
177*4882a593Smuzhiyun 		cache_dequeue(detail, head);
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
cache_make_negative(struct cache_detail * detail,struct cache_head * h)181*4882a593Smuzhiyun static void cache_make_negative(struct cache_detail *detail,
182*4882a593Smuzhiyun 				struct cache_head *h)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	set_bit(CACHE_NEGATIVE, &h->flags);
185*4882a593Smuzhiyun 	trace_cache_entry_make_negative(detail, h);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
cache_entry_update(struct cache_detail * detail,struct cache_head * h,struct cache_head * new)188*4882a593Smuzhiyun static void cache_entry_update(struct cache_detail *detail,
189*4882a593Smuzhiyun 			       struct cache_head *h,
190*4882a593Smuzhiyun 			       struct cache_head *new)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	if (!test_bit(CACHE_NEGATIVE, &new->flags)) {
193*4882a593Smuzhiyun 		detail->update(h, new);
194*4882a593Smuzhiyun 		trace_cache_entry_update(detail, h);
195*4882a593Smuzhiyun 	} else {
196*4882a593Smuzhiyun 		cache_make_negative(detail, h);
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
sunrpc_cache_update(struct cache_detail * detail,struct cache_head * new,struct cache_head * old,int hash)200*4882a593Smuzhiyun struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
201*4882a593Smuzhiyun 				       struct cache_head *new, struct cache_head *old, int hash)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	/* The 'old' entry is to be replaced by 'new'.
204*4882a593Smuzhiyun 	 * If 'old' is not VALID, we update it directly,
205*4882a593Smuzhiyun 	 * otherwise we need to replace it
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	struct cache_head *tmp;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (!test_bit(CACHE_VALID, &old->flags)) {
210*4882a593Smuzhiyun 		spin_lock(&detail->hash_lock);
211*4882a593Smuzhiyun 		if (!test_bit(CACHE_VALID, &old->flags)) {
212*4882a593Smuzhiyun 			cache_entry_update(detail, old, new);
213*4882a593Smuzhiyun 			cache_fresh_locked(old, new->expiry_time, detail);
214*4882a593Smuzhiyun 			spin_unlock(&detail->hash_lock);
215*4882a593Smuzhiyun 			cache_fresh_unlocked(old, detail);
216*4882a593Smuzhiyun 			return old;
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 		spin_unlock(&detail->hash_lock);
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 	/* We need to insert a new entry */
221*4882a593Smuzhiyun 	tmp = detail->alloc();
222*4882a593Smuzhiyun 	if (!tmp) {
223*4882a593Smuzhiyun 		cache_put(old, detail);
224*4882a593Smuzhiyun 		return NULL;
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 	cache_init(tmp, detail);
227*4882a593Smuzhiyun 	detail->init(tmp, old);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	spin_lock(&detail->hash_lock);
230*4882a593Smuzhiyun 	cache_entry_update(detail, tmp, new);
231*4882a593Smuzhiyun 	hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
232*4882a593Smuzhiyun 	detail->entries++;
233*4882a593Smuzhiyun 	cache_get(tmp);
234*4882a593Smuzhiyun 	cache_fresh_locked(tmp, new->expiry_time, detail);
235*4882a593Smuzhiyun 	cache_fresh_locked(old, 0, detail);
236*4882a593Smuzhiyun 	spin_unlock(&detail->hash_lock);
237*4882a593Smuzhiyun 	cache_fresh_unlocked(tmp, detail);
238*4882a593Smuzhiyun 	cache_fresh_unlocked(old, detail);
239*4882a593Smuzhiyun 	cache_put(old, detail);
240*4882a593Smuzhiyun 	return tmp;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_update);
243*4882a593Smuzhiyun 
cache_is_valid(struct cache_head * h)244*4882a593Smuzhiyun static inline int cache_is_valid(struct cache_head *h)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	if (!test_bit(CACHE_VALID, &h->flags))
247*4882a593Smuzhiyun 		return -EAGAIN;
248*4882a593Smuzhiyun 	else {
249*4882a593Smuzhiyun 		/* entry is valid */
250*4882a593Smuzhiyun 		if (test_bit(CACHE_NEGATIVE, &h->flags))
251*4882a593Smuzhiyun 			return -ENOENT;
252*4882a593Smuzhiyun 		else {
253*4882a593Smuzhiyun 			/*
254*4882a593Smuzhiyun 			 * In combination with write barrier in
255*4882a593Smuzhiyun 			 * sunrpc_cache_update, ensures that anyone
256*4882a593Smuzhiyun 			 * using the cache entry after this sees the
257*4882a593Smuzhiyun 			 * updated contents:
258*4882a593Smuzhiyun 			 */
259*4882a593Smuzhiyun 			smp_rmb();
260*4882a593Smuzhiyun 			return 0;
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
try_to_negate_entry(struct cache_detail * detail,struct cache_head * h)265*4882a593Smuzhiyun static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	int rv;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	spin_lock(&detail->hash_lock);
270*4882a593Smuzhiyun 	rv = cache_is_valid(h);
271*4882a593Smuzhiyun 	if (rv == -EAGAIN) {
272*4882a593Smuzhiyun 		cache_make_negative(detail, h);
273*4882a593Smuzhiyun 		cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
274*4882a593Smuzhiyun 				   detail);
275*4882a593Smuzhiyun 		rv = -ENOENT;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 	spin_unlock(&detail->hash_lock);
278*4882a593Smuzhiyun 	cache_fresh_unlocked(h, detail);
279*4882a593Smuzhiyun 	return rv;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun  * This is the generic cache management routine for all
284*4882a593Smuzhiyun  * the authentication caches.
285*4882a593Smuzhiyun  * It checks the currency of a cache item and will (later)
286*4882a593Smuzhiyun  * initiate an upcall to fill it if needed.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  *
289*4882a593Smuzhiyun  * Returns 0 if the cache_head can be used, or cache_puts it and returns
290*4882a593Smuzhiyun  * -EAGAIN if upcall is pending and request has been queued
291*4882a593Smuzhiyun  * -ETIMEDOUT if upcall failed or request could not be queue or
292*4882a593Smuzhiyun  *           upcall completed but item is still invalid (implying that
293*4882a593Smuzhiyun  *           the cache item has been replaced with a newer one).
294*4882a593Smuzhiyun  * -ENOENT if cache entry was negative
295*4882a593Smuzhiyun  */
cache_check(struct cache_detail * detail,struct cache_head * h,struct cache_req * rqstp)296*4882a593Smuzhiyun int cache_check(struct cache_detail *detail,
297*4882a593Smuzhiyun 		    struct cache_head *h, struct cache_req *rqstp)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	int rv;
300*4882a593Smuzhiyun 	time64_t refresh_age, age;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* First decide return status as best we can */
303*4882a593Smuzhiyun 	rv = cache_is_valid(h);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/* now see if we want to start an upcall */
306*4882a593Smuzhiyun 	refresh_age = (h->expiry_time - h->last_refresh);
307*4882a593Smuzhiyun 	age = seconds_since_boot() - h->last_refresh;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	if (rqstp == NULL) {
310*4882a593Smuzhiyun 		if (rv == -EAGAIN)
311*4882a593Smuzhiyun 			rv = -ENOENT;
312*4882a593Smuzhiyun 	} else if (rv == -EAGAIN ||
313*4882a593Smuzhiyun 		   (h->expiry_time != 0 && age > refresh_age/2)) {
314*4882a593Smuzhiyun 		dprintk("RPC:       Want update, refage=%lld, age=%lld\n",
315*4882a593Smuzhiyun 				refresh_age, age);
316*4882a593Smuzhiyun 		switch (detail->cache_upcall(detail, h)) {
317*4882a593Smuzhiyun 		case -EINVAL:
318*4882a593Smuzhiyun 			rv = try_to_negate_entry(detail, h);
319*4882a593Smuzhiyun 			break;
320*4882a593Smuzhiyun 		case -EAGAIN:
321*4882a593Smuzhiyun 			cache_fresh_unlocked(h, detail);
322*4882a593Smuzhiyun 			break;
323*4882a593Smuzhiyun 		}
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (rv == -EAGAIN) {
327*4882a593Smuzhiyun 		if (!cache_defer_req(rqstp, h)) {
328*4882a593Smuzhiyun 			/*
329*4882a593Smuzhiyun 			 * Request was not deferred; handle it as best
330*4882a593Smuzhiyun 			 * we can ourselves:
331*4882a593Smuzhiyun 			 */
332*4882a593Smuzhiyun 			rv = cache_is_valid(h);
333*4882a593Smuzhiyun 			if (rv == -EAGAIN)
334*4882a593Smuzhiyun 				rv = -ETIMEDOUT;
335*4882a593Smuzhiyun 		}
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 	if (rv)
338*4882a593Smuzhiyun 		cache_put(h, detail);
339*4882a593Smuzhiyun 	return rv;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_check);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun  * caches need to be periodically cleaned.
345*4882a593Smuzhiyun  * For this we maintain a list of cache_detail and
346*4882a593Smuzhiyun  * a current pointer into that list and into the table
347*4882a593Smuzhiyun  * for that entry.
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  * Each time cache_clean is called it finds the next non-empty entry
350*4882a593Smuzhiyun  * in the current table and walks the list in that entry
351*4882a593Smuzhiyun  * looking for entries that can be removed.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * An entry gets removed if:
354*4882a593Smuzhiyun  * - The expiry is before current time
355*4882a593Smuzhiyun  * - The last_refresh time is before the flush_time for that cache
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * later we might drop old entries with non-NEVER expiry if that table
358*4882a593Smuzhiyun  * is getting 'full' for some definition of 'full'
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * The question of "how often to scan a table" is an interesting one
361*4882a593Smuzhiyun  * and is answered in part by the use of the "nextcheck" field in the
362*4882a593Smuzhiyun  * cache_detail.
363*4882a593Smuzhiyun  * When a scan of a table begins, the nextcheck field is set to a time
364*4882a593Smuzhiyun  * that is well into the future.
365*4882a593Smuzhiyun  * While scanning, if an expiry time is found that is earlier than the
366*4882a593Smuzhiyun  * current nextcheck time, nextcheck is set to that expiry time.
367*4882a593Smuzhiyun  * If the flush_time is ever set to a time earlier than the nextcheck
368*4882a593Smuzhiyun  * time, the nextcheck time is then set to that flush_time.
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * A table is then only scanned if the current time is at least
371*4882a593Smuzhiyun  * the nextcheck time.
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  */
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun static LIST_HEAD(cache_list);
376*4882a593Smuzhiyun static DEFINE_SPINLOCK(cache_list_lock);
377*4882a593Smuzhiyun static struct cache_detail *current_detail;
378*4882a593Smuzhiyun static int current_index;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun static void do_cache_clean(struct work_struct *work);
381*4882a593Smuzhiyun static struct delayed_work cache_cleaner;
382*4882a593Smuzhiyun 
sunrpc_init_cache_detail(struct cache_detail * cd)383*4882a593Smuzhiyun void sunrpc_init_cache_detail(struct cache_detail *cd)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	spin_lock_init(&cd->hash_lock);
386*4882a593Smuzhiyun 	INIT_LIST_HEAD(&cd->queue);
387*4882a593Smuzhiyun 	spin_lock(&cache_list_lock);
388*4882a593Smuzhiyun 	cd->nextcheck = 0;
389*4882a593Smuzhiyun 	cd->entries = 0;
390*4882a593Smuzhiyun 	atomic_set(&cd->writers, 0);
391*4882a593Smuzhiyun 	cd->last_close = 0;
392*4882a593Smuzhiyun 	cd->last_warn = -1;
393*4882a593Smuzhiyun 	list_add(&cd->others, &cache_list);
394*4882a593Smuzhiyun 	spin_unlock(&cache_list_lock);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	/* start the cleaning process */
397*4882a593Smuzhiyun 	queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
400*4882a593Smuzhiyun 
sunrpc_destroy_cache_detail(struct cache_detail * cd)401*4882a593Smuzhiyun void sunrpc_destroy_cache_detail(struct cache_detail *cd)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	cache_purge(cd);
404*4882a593Smuzhiyun 	spin_lock(&cache_list_lock);
405*4882a593Smuzhiyun 	spin_lock(&cd->hash_lock);
406*4882a593Smuzhiyun 	if (current_detail == cd)
407*4882a593Smuzhiyun 		current_detail = NULL;
408*4882a593Smuzhiyun 	list_del_init(&cd->others);
409*4882a593Smuzhiyun 	spin_unlock(&cd->hash_lock);
410*4882a593Smuzhiyun 	spin_unlock(&cache_list_lock);
411*4882a593Smuzhiyun 	if (list_empty(&cache_list)) {
412*4882a593Smuzhiyun 		/* module must be being unloaded so its safe to kill the worker */
413*4882a593Smuzhiyun 		cancel_delayed_work_sync(&cache_cleaner);
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /* clean cache tries to find something to clean
419*4882a593Smuzhiyun  * and cleans it.
420*4882a593Smuzhiyun  * It returns 1 if it cleaned something,
421*4882a593Smuzhiyun  *            0 if it didn't find anything this time
422*4882a593Smuzhiyun  *           -1 if it fell off the end of the list.
423*4882a593Smuzhiyun  */
cache_clean(void)424*4882a593Smuzhiyun static int cache_clean(void)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	int rv = 0;
427*4882a593Smuzhiyun 	struct list_head *next;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	spin_lock(&cache_list_lock);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	/* find a suitable table if we don't already have one */
432*4882a593Smuzhiyun 	while (current_detail == NULL ||
433*4882a593Smuzhiyun 	    current_index >= current_detail->hash_size) {
434*4882a593Smuzhiyun 		if (current_detail)
435*4882a593Smuzhiyun 			next = current_detail->others.next;
436*4882a593Smuzhiyun 		else
437*4882a593Smuzhiyun 			next = cache_list.next;
438*4882a593Smuzhiyun 		if (next == &cache_list) {
439*4882a593Smuzhiyun 			current_detail = NULL;
440*4882a593Smuzhiyun 			spin_unlock(&cache_list_lock);
441*4882a593Smuzhiyun 			return -1;
442*4882a593Smuzhiyun 		}
443*4882a593Smuzhiyun 		current_detail = list_entry(next, struct cache_detail, others);
444*4882a593Smuzhiyun 		if (current_detail->nextcheck > seconds_since_boot())
445*4882a593Smuzhiyun 			current_index = current_detail->hash_size;
446*4882a593Smuzhiyun 		else {
447*4882a593Smuzhiyun 			current_index = 0;
448*4882a593Smuzhiyun 			current_detail->nextcheck = seconds_since_boot()+30*60;
449*4882a593Smuzhiyun 		}
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* find a non-empty bucket in the table */
453*4882a593Smuzhiyun 	while (current_detail &&
454*4882a593Smuzhiyun 	       current_index < current_detail->hash_size &&
455*4882a593Smuzhiyun 	       hlist_empty(&current_detail->hash_table[current_index]))
456*4882a593Smuzhiyun 		current_index++;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* find a cleanable entry in the bucket and clean it, or set to next bucket */
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	if (current_detail && current_index < current_detail->hash_size) {
461*4882a593Smuzhiyun 		struct cache_head *ch = NULL;
462*4882a593Smuzhiyun 		struct cache_detail *d;
463*4882a593Smuzhiyun 		struct hlist_head *head;
464*4882a593Smuzhiyun 		struct hlist_node *tmp;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		spin_lock(&current_detail->hash_lock);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		/* Ok, now to clean this strand */
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 		head = &current_detail->hash_table[current_index];
471*4882a593Smuzhiyun 		hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
472*4882a593Smuzhiyun 			if (current_detail->nextcheck > ch->expiry_time)
473*4882a593Smuzhiyun 				current_detail->nextcheck = ch->expiry_time+1;
474*4882a593Smuzhiyun 			if (!cache_is_expired(current_detail, ch))
475*4882a593Smuzhiyun 				continue;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 			sunrpc_begin_cache_remove_entry(ch, current_detail);
478*4882a593Smuzhiyun 			trace_cache_entry_expired(current_detail, ch);
479*4882a593Smuzhiyun 			rv = 1;
480*4882a593Smuzhiyun 			break;
481*4882a593Smuzhiyun 		}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 		spin_unlock(&current_detail->hash_lock);
484*4882a593Smuzhiyun 		d = current_detail;
485*4882a593Smuzhiyun 		if (!ch)
486*4882a593Smuzhiyun 			current_index ++;
487*4882a593Smuzhiyun 		spin_unlock(&cache_list_lock);
488*4882a593Smuzhiyun 		if (ch)
489*4882a593Smuzhiyun 			sunrpc_end_cache_remove_entry(ch, d);
490*4882a593Smuzhiyun 	} else
491*4882a593Smuzhiyun 		spin_unlock(&cache_list_lock);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	return rv;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun  * We want to regularly clean the cache, so we need to schedule some work ...
498*4882a593Smuzhiyun  */
do_cache_clean(struct work_struct * work)499*4882a593Smuzhiyun static void do_cache_clean(struct work_struct *work)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	int delay;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (list_empty(&cache_list))
504*4882a593Smuzhiyun 		return;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (cache_clean() == -1)
507*4882a593Smuzhiyun 		delay = round_jiffies_relative(30*HZ);
508*4882a593Smuzhiyun 	else
509*4882a593Smuzhiyun 		delay = 5;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	queue_delayed_work(system_power_efficient_wq, &cache_cleaner, delay);
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun /*
516*4882a593Smuzhiyun  * Clean all caches promptly.  This just calls cache_clean
517*4882a593Smuzhiyun  * repeatedly until we are sure that every cache has had a chance to
518*4882a593Smuzhiyun  * be fully cleaned
519*4882a593Smuzhiyun  */
cache_flush(void)520*4882a593Smuzhiyun void cache_flush(void)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	while (cache_clean() != -1)
523*4882a593Smuzhiyun 		cond_resched();
524*4882a593Smuzhiyun 	while (cache_clean() != -1)
525*4882a593Smuzhiyun 		cond_resched();
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_flush);
528*4882a593Smuzhiyun 
cache_purge(struct cache_detail * detail)529*4882a593Smuzhiyun void cache_purge(struct cache_detail *detail)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	struct cache_head *ch = NULL;
532*4882a593Smuzhiyun 	struct hlist_head *head = NULL;
533*4882a593Smuzhiyun 	int i = 0;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	spin_lock(&detail->hash_lock);
536*4882a593Smuzhiyun 	if (!detail->entries) {
537*4882a593Smuzhiyun 		spin_unlock(&detail->hash_lock);
538*4882a593Smuzhiyun 		return;
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
542*4882a593Smuzhiyun 	for (i = 0; i < detail->hash_size; i++) {
543*4882a593Smuzhiyun 		head = &detail->hash_table[i];
544*4882a593Smuzhiyun 		while (!hlist_empty(head)) {
545*4882a593Smuzhiyun 			ch = hlist_entry(head->first, struct cache_head,
546*4882a593Smuzhiyun 					 cache_list);
547*4882a593Smuzhiyun 			sunrpc_begin_cache_remove_entry(ch, detail);
548*4882a593Smuzhiyun 			spin_unlock(&detail->hash_lock);
549*4882a593Smuzhiyun 			sunrpc_end_cache_remove_entry(ch, detail);
550*4882a593Smuzhiyun 			spin_lock(&detail->hash_lock);
551*4882a593Smuzhiyun 		}
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 	spin_unlock(&detail->hash_lock);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_purge);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun /*
559*4882a593Smuzhiyun  * Deferral and Revisiting of Requests.
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * If a cache lookup finds a pending entry, we
562*4882a593Smuzhiyun  * need to defer the request and revisit it later.
563*4882a593Smuzhiyun  * All deferred requests are stored in a hash table,
564*4882a593Smuzhiyun  * indexed by "struct cache_head *".
565*4882a593Smuzhiyun  * As it may be wasteful to store a whole request
566*4882a593Smuzhiyun  * structure, we allow the request to provide a
567*4882a593Smuzhiyun  * deferred form, which must contain a
568*4882a593Smuzhiyun  * 'struct cache_deferred_req'
569*4882a593Smuzhiyun  * This cache_deferred_req contains a method to allow
570*4882a593Smuzhiyun  * it to be revisited when cache info is available
571*4882a593Smuzhiyun  */
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun #define	DFR_HASHSIZE	(PAGE_SIZE/sizeof(struct list_head))
574*4882a593Smuzhiyun #define	DFR_HASH(item)	((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun #define	DFR_MAX	300	/* ??? */
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun static DEFINE_SPINLOCK(cache_defer_lock);
579*4882a593Smuzhiyun static LIST_HEAD(cache_defer_list);
580*4882a593Smuzhiyun static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
581*4882a593Smuzhiyun static int cache_defer_cnt;
582*4882a593Smuzhiyun 
__unhash_deferred_req(struct cache_deferred_req * dreq)583*4882a593Smuzhiyun static void __unhash_deferred_req(struct cache_deferred_req *dreq)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	hlist_del_init(&dreq->hash);
586*4882a593Smuzhiyun 	if (!list_empty(&dreq->recent)) {
587*4882a593Smuzhiyun 		list_del_init(&dreq->recent);
588*4882a593Smuzhiyun 		cache_defer_cnt--;
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
__hash_deferred_req(struct cache_deferred_req * dreq,struct cache_head * item)592*4882a593Smuzhiyun static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	int hash = DFR_HASH(item);
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dreq->recent);
597*4882a593Smuzhiyun 	hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
setup_deferral(struct cache_deferred_req * dreq,struct cache_head * item,int count_me)600*4882a593Smuzhiyun static void setup_deferral(struct cache_deferred_req *dreq,
601*4882a593Smuzhiyun 			   struct cache_head *item,
602*4882a593Smuzhiyun 			   int count_me)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	dreq->item = item;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	spin_lock(&cache_defer_lock);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	__hash_deferred_req(dreq, item);
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (count_me) {
612*4882a593Smuzhiyun 		cache_defer_cnt++;
613*4882a593Smuzhiyun 		list_add(&dreq->recent, &cache_defer_list);
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	spin_unlock(&cache_defer_lock);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun struct thread_deferred_req {
621*4882a593Smuzhiyun 	struct cache_deferred_req handle;
622*4882a593Smuzhiyun 	struct completion completion;
623*4882a593Smuzhiyun };
624*4882a593Smuzhiyun 
cache_restart_thread(struct cache_deferred_req * dreq,int too_many)625*4882a593Smuzhiyun static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct thread_deferred_req *dr =
628*4882a593Smuzhiyun 		container_of(dreq, struct thread_deferred_req, handle);
629*4882a593Smuzhiyun 	complete(&dr->completion);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
cache_wait_req(struct cache_req * req,struct cache_head * item)632*4882a593Smuzhiyun static void cache_wait_req(struct cache_req *req, struct cache_head *item)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	struct thread_deferred_req sleeper;
635*4882a593Smuzhiyun 	struct cache_deferred_req *dreq = &sleeper.handle;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
638*4882a593Smuzhiyun 	dreq->revisit = cache_restart_thread;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	setup_deferral(dreq, item, 0);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (!test_bit(CACHE_PENDING, &item->flags) ||
643*4882a593Smuzhiyun 	    wait_for_completion_interruptible_timeout(
644*4882a593Smuzhiyun 		    &sleeper.completion, req->thread_wait) <= 0) {
645*4882a593Smuzhiyun 		/* The completion wasn't completed, so we need
646*4882a593Smuzhiyun 		 * to clean up
647*4882a593Smuzhiyun 		 */
648*4882a593Smuzhiyun 		spin_lock(&cache_defer_lock);
649*4882a593Smuzhiyun 		if (!hlist_unhashed(&sleeper.handle.hash)) {
650*4882a593Smuzhiyun 			__unhash_deferred_req(&sleeper.handle);
651*4882a593Smuzhiyun 			spin_unlock(&cache_defer_lock);
652*4882a593Smuzhiyun 		} else {
653*4882a593Smuzhiyun 			/* cache_revisit_request already removed
654*4882a593Smuzhiyun 			 * this from the hash table, but hasn't
655*4882a593Smuzhiyun 			 * called ->revisit yet.  It will very soon
656*4882a593Smuzhiyun 			 * and we need to wait for it.
657*4882a593Smuzhiyun 			 */
658*4882a593Smuzhiyun 			spin_unlock(&cache_defer_lock);
659*4882a593Smuzhiyun 			wait_for_completion(&sleeper.completion);
660*4882a593Smuzhiyun 		}
661*4882a593Smuzhiyun 	}
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun 
cache_limit_defers(void)664*4882a593Smuzhiyun static void cache_limit_defers(void)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	/* Make sure we haven't exceed the limit of allowed deferred
667*4882a593Smuzhiyun 	 * requests.
668*4882a593Smuzhiyun 	 */
669*4882a593Smuzhiyun 	struct cache_deferred_req *discard = NULL;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	if (cache_defer_cnt <= DFR_MAX)
672*4882a593Smuzhiyun 		return;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	spin_lock(&cache_defer_lock);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	/* Consider removing either the first or the last */
677*4882a593Smuzhiyun 	if (cache_defer_cnt > DFR_MAX) {
678*4882a593Smuzhiyun 		if (prandom_u32() & 1)
679*4882a593Smuzhiyun 			discard = list_entry(cache_defer_list.next,
680*4882a593Smuzhiyun 					     struct cache_deferred_req, recent);
681*4882a593Smuzhiyun 		else
682*4882a593Smuzhiyun 			discard = list_entry(cache_defer_list.prev,
683*4882a593Smuzhiyun 					     struct cache_deferred_req, recent);
684*4882a593Smuzhiyun 		__unhash_deferred_req(discard);
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 	spin_unlock(&cache_defer_lock);
687*4882a593Smuzhiyun 	if (discard)
688*4882a593Smuzhiyun 		discard->revisit(discard, 1);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun /* Return true if and only if a deferred request is queued. */
cache_defer_req(struct cache_req * req,struct cache_head * item)692*4882a593Smuzhiyun static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct cache_deferred_req *dreq;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (req->thread_wait) {
697*4882a593Smuzhiyun 		cache_wait_req(req, item);
698*4882a593Smuzhiyun 		if (!test_bit(CACHE_PENDING, &item->flags))
699*4882a593Smuzhiyun 			return false;
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 	dreq = req->defer(req);
702*4882a593Smuzhiyun 	if (dreq == NULL)
703*4882a593Smuzhiyun 		return false;
704*4882a593Smuzhiyun 	setup_deferral(dreq, item, 1);
705*4882a593Smuzhiyun 	if (!test_bit(CACHE_PENDING, &item->flags))
706*4882a593Smuzhiyun 		/* Bit could have been cleared before we managed to
707*4882a593Smuzhiyun 		 * set up the deferral, so need to revisit just in case
708*4882a593Smuzhiyun 		 */
709*4882a593Smuzhiyun 		cache_revisit_request(item);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	cache_limit_defers();
712*4882a593Smuzhiyun 	return true;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun 
cache_revisit_request(struct cache_head * item)715*4882a593Smuzhiyun static void cache_revisit_request(struct cache_head *item)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun 	struct cache_deferred_req *dreq;
718*4882a593Smuzhiyun 	struct list_head pending;
719*4882a593Smuzhiyun 	struct hlist_node *tmp;
720*4882a593Smuzhiyun 	int hash = DFR_HASH(item);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pending);
723*4882a593Smuzhiyun 	spin_lock(&cache_defer_lock);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
726*4882a593Smuzhiyun 		if (dreq->item == item) {
727*4882a593Smuzhiyun 			__unhash_deferred_req(dreq);
728*4882a593Smuzhiyun 			list_add(&dreq->recent, &pending);
729*4882a593Smuzhiyun 		}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	spin_unlock(&cache_defer_lock);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	while (!list_empty(&pending)) {
734*4882a593Smuzhiyun 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
735*4882a593Smuzhiyun 		list_del_init(&dreq->recent);
736*4882a593Smuzhiyun 		dreq->revisit(dreq, 0);
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
cache_clean_deferred(void * owner)740*4882a593Smuzhiyun void cache_clean_deferred(void *owner)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	struct cache_deferred_req *dreq, *tmp;
743*4882a593Smuzhiyun 	struct list_head pending;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pending);
747*4882a593Smuzhiyun 	spin_lock(&cache_defer_lock);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
750*4882a593Smuzhiyun 		if (dreq->owner == owner) {
751*4882a593Smuzhiyun 			__unhash_deferred_req(dreq);
752*4882a593Smuzhiyun 			list_add(&dreq->recent, &pending);
753*4882a593Smuzhiyun 		}
754*4882a593Smuzhiyun 	}
755*4882a593Smuzhiyun 	spin_unlock(&cache_defer_lock);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	while (!list_empty(&pending)) {
758*4882a593Smuzhiyun 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
759*4882a593Smuzhiyun 		list_del_init(&dreq->recent);
760*4882a593Smuzhiyun 		dreq->revisit(dreq, 1);
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun /*
765*4882a593Smuzhiyun  * communicate with user-space
766*4882a593Smuzhiyun  *
767*4882a593Smuzhiyun  * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
768*4882a593Smuzhiyun  * On read, you get a full request, or block.
769*4882a593Smuzhiyun  * On write, an update request is processed.
770*4882a593Smuzhiyun  * Poll works if anything to read, and always allows write.
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  * Implemented by linked list of requests.  Each open file has
773*4882a593Smuzhiyun  * a ->private that also exists in this list.  New requests are added
774*4882a593Smuzhiyun  * to the end and may wakeup and preceding readers.
775*4882a593Smuzhiyun  * New readers are added to the head.  If, on read, an item is found with
776*4882a593Smuzhiyun  * CACHE_UPCALLING clear, we free it from the list.
777*4882a593Smuzhiyun  *
778*4882a593Smuzhiyun  */
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun static DEFINE_SPINLOCK(queue_lock);
781*4882a593Smuzhiyun static DEFINE_MUTEX(queue_io_mutex);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun struct cache_queue {
784*4882a593Smuzhiyun 	struct list_head	list;
785*4882a593Smuzhiyun 	int			reader;	/* if 0, then request */
786*4882a593Smuzhiyun };
787*4882a593Smuzhiyun struct cache_request {
788*4882a593Smuzhiyun 	struct cache_queue	q;
789*4882a593Smuzhiyun 	struct cache_head	*item;
790*4882a593Smuzhiyun 	char			* buf;
791*4882a593Smuzhiyun 	int			len;
792*4882a593Smuzhiyun 	int			readers;
793*4882a593Smuzhiyun };
794*4882a593Smuzhiyun struct cache_reader {
795*4882a593Smuzhiyun 	struct cache_queue	q;
796*4882a593Smuzhiyun 	int			offset;	/* if non-0, we have a refcnt on next request */
797*4882a593Smuzhiyun };
798*4882a593Smuzhiyun 
cache_request(struct cache_detail * detail,struct cache_request * crq)799*4882a593Smuzhiyun static int cache_request(struct cache_detail *detail,
800*4882a593Smuzhiyun 			       struct cache_request *crq)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	char *bp = crq->buf;
803*4882a593Smuzhiyun 	int len = PAGE_SIZE;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	detail->cache_request(detail, crq->item, &bp, &len);
806*4882a593Smuzhiyun 	if (len < 0)
807*4882a593Smuzhiyun 		return -EAGAIN;
808*4882a593Smuzhiyun 	return PAGE_SIZE - len;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun 
cache_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos,struct cache_detail * cd)811*4882a593Smuzhiyun static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
812*4882a593Smuzhiyun 			  loff_t *ppos, struct cache_detail *cd)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun 	struct cache_reader *rp = filp->private_data;
815*4882a593Smuzhiyun 	struct cache_request *rq;
816*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
817*4882a593Smuzhiyun 	int err;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	if (count == 0)
820*4882a593Smuzhiyun 		return 0;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	inode_lock(inode); /* protect against multiple concurrent
823*4882a593Smuzhiyun 			      * readers on this file */
824*4882a593Smuzhiyun  again:
825*4882a593Smuzhiyun 	spin_lock(&queue_lock);
826*4882a593Smuzhiyun 	/* need to find next request */
827*4882a593Smuzhiyun 	while (rp->q.list.next != &cd->queue &&
828*4882a593Smuzhiyun 	       list_entry(rp->q.list.next, struct cache_queue, list)
829*4882a593Smuzhiyun 	       ->reader) {
830*4882a593Smuzhiyun 		struct list_head *next = rp->q.list.next;
831*4882a593Smuzhiyun 		list_move(&rp->q.list, next);
832*4882a593Smuzhiyun 	}
833*4882a593Smuzhiyun 	if (rp->q.list.next == &cd->queue) {
834*4882a593Smuzhiyun 		spin_unlock(&queue_lock);
835*4882a593Smuzhiyun 		inode_unlock(inode);
836*4882a593Smuzhiyun 		WARN_ON_ONCE(rp->offset);
837*4882a593Smuzhiyun 		return 0;
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 	rq = container_of(rp->q.list.next, struct cache_request, q.list);
840*4882a593Smuzhiyun 	WARN_ON_ONCE(rq->q.reader);
841*4882a593Smuzhiyun 	if (rp->offset == 0)
842*4882a593Smuzhiyun 		rq->readers++;
843*4882a593Smuzhiyun 	spin_unlock(&queue_lock);
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	if (rq->len == 0) {
846*4882a593Smuzhiyun 		err = cache_request(cd, rq);
847*4882a593Smuzhiyun 		if (err < 0)
848*4882a593Smuzhiyun 			goto out;
849*4882a593Smuzhiyun 		rq->len = err;
850*4882a593Smuzhiyun 	}
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
853*4882a593Smuzhiyun 		err = -EAGAIN;
854*4882a593Smuzhiyun 		spin_lock(&queue_lock);
855*4882a593Smuzhiyun 		list_move(&rp->q.list, &rq->q.list);
856*4882a593Smuzhiyun 		spin_unlock(&queue_lock);
857*4882a593Smuzhiyun 	} else {
858*4882a593Smuzhiyun 		if (rp->offset + count > rq->len)
859*4882a593Smuzhiyun 			count = rq->len - rp->offset;
860*4882a593Smuzhiyun 		err = -EFAULT;
861*4882a593Smuzhiyun 		if (copy_to_user(buf, rq->buf + rp->offset, count))
862*4882a593Smuzhiyun 			goto out;
863*4882a593Smuzhiyun 		rp->offset += count;
864*4882a593Smuzhiyun 		if (rp->offset >= rq->len) {
865*4882a593Smuzhiyun 			rp->offset = 0;
866*4882a593Smuzhiyun 			spin_lock(&queue_lock);
867*4882a593Smuzhiyun 			list_move(&rp->q.list, &rq->q.list);
868*4882a593Smuzhiyun 			spin_unlock(&queue_lock);
869*4882a593Smuzhiyun 		}
870*4882a593Smuzhiyun 		err = 0;
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun  out:
873*4882a593Smuzhiyun 	if (rp->offset == 0) {
874*4882a593Smuzhiyun 		/* need to release rq */
875*4882a593Smuzhiyun 		spin_lock(&queue_lock);
876*4882a593Smuzhiyun 		rq->readers--;
877*4882a593Smuzhiyun 		if (rq->readers == 0 &&
878*4882a593Smuzhiyun 		    !test_bit(CACHE_PENDING, &rq->item->flags)) {
879*4882a593Smuzhiyun 			list_del(&rq->q.list);
880*4882a593Smuzhiyun 			spin_unlock(&queue_lock);
881*4882a593Smuzhiyun 			cache_put(rq->item, cd);
882*4882a593Smuzhiyun 			kfree(rq->buf);
883*4882a593Smuzhiyun 			kfree(rq);
884*4882a593Smuzhiyun 		} else
885*4882a593Smuzhiyun 			spin_unlock(&queue_lock);
886*4882a593Smuzhiyun 	}
887*4882a593Smuzhiyun 	if (err == -EAGAIN)
888*4882a593Smuzhiyun 		goto again;
889*4882a593Smuzhiyun 	inode_unlock(inode);
890*4882a593Smuzhiyun 	return err ? err :  count;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun 
cache_do_downcall(char * kaddr,const char __user * buf,size_t count,struct cache_detail * cd)893*4882a593Smuzhiyun static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
894*4882a593Smuzhiyun 				 size_t count, struct cache_detail *cd)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	ssize_t ret;
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	if (count == 0)
899*4882a593Smuzhiyun 		return -EINVAL;
900*4882a593Smuzhiyun 	if (copy_from_user(kaddr, buf, count))
901*4882a593Smuzhiyun 		return -EFAULT;
902*4882a593Smuzhiyun 	kaddr[count] = '\0';
903*4882a593Smuzhiyun 	ret = cd->cache_parse(cd, kaddr, count);
904*4882a593Smuzhiyun 	if (!ret)
905*4882a593Smuzhiyun 		ret = count;
906*4882a593Smuzhiyun 	return ret;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun 
cache_slow_downcall(const char __user * buf,size_t count,struct cache_detail * cd)909*4882a593Smuzhiyun static ssize_t cache_slow_downcall(const char __user *buf,
910*4882a593Smuzhiyun 				   size_t count, struct cache_detail *cd)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	static char write_buf[32768]; /* protected by queue_io_mutex */
913*4882a593Smuzhiyun 	ssize_t ret = -EINVAL;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	if (count >= sizeof(write_buf))
916*4882a593Smuzhiyun 		goto out;
917*4882a593Smuzhiyun 	mutex_lock(&queue_io_mutex);
918*4882a593Smuzhiyun 	ret = cache_do_downcall(write_buf, buf, count, cd);
919*4882a593Smuzhiyun 	mutex_unlock(&queue_io_mutex);
920*4882a593Smuzhiyun out:
921*4882a593Smuzhiyun 	return ret;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun 
cache_downcall(struct address_space * mapping,const char __user * buf,size_t count,struct cache_detail * cd)924*4882a593Smuzhiyun static ssize_t cache_downcall(struct address_space *mapping,
925*4882a593Smuzhiyun 			      const char __user *buf,
926*4882a593Smuzhiyun 			      size_t count, struct cache_detail *cd)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun 	struct page *page;
929*4882a593Smuzhiyun 	char *kaddr;
930*4882a593Smuzhiyun 	ssize_t ret = -ENOMEM;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	if (count >= PAGE_SIZE)
933*4882a593Smuzhiyun 		goto out_slow;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	page = find_or_create_page(mapping, 0, GFP_KERNEL);
936*4882a593Smuzhiyun 	if (!page)
937*4882a593Smuzhiyun 		goto out_slow;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	kaddr = kmap(page);
940*4882a593Smuzhiyun 	ret = cache_do_downcall(kaddr, buf, count, cd);
941*4882a593Smuzhiyun 	kunmap(page);
942*4882a593Smuzhiyun 	unlock_page(page);
943*4882a593Smuzhiyun 	put_page(page);
944*4882a593Smuzhiyun 	return ret;
945*4882a593Smuzhiyun out_slow:
946*4882a593Smuzhiyun 	return cache_slow_downcall(buf, count, cd);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
cache_write(struct file * filp,const char __user * buf,size_t count,loff_t * ppos,struct cache_detail * cd)949*4882a593Smuzhiyun static ssize_t cache_write(struct file *filp, const char __user *buf,
950*4882a593Smuzhiyun 			   size_t count, loff_t *ppos,
951*4882a593Smuzhiyun 			   struct cache_detail *cd)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 	struct address_space *mapping = filp->f_mapping;
954*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
955*4882a593Smuzhiyun 	ssize_t ret = -EINVAL;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	if (!cd->cache_parse)
958*4882a593Smuzhiyun 		goto out;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	inode_lock(inode);
961*4882a593Smuzhiyun 	ret = cache_downcall(mapping, buf, count, cd);
962*4882a593Smuzhiyun 	inode_unlock(inode);
963*4882a593Smuzhiyun out:
964*4882a593Smuzhiyun 	return ret;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
968*4882a593Smuzhiyun 
cache_poll(struct file * filp,poll_table * wait,struct cache_detail * cd)969*4882a593Smuzhiyun static __poll_t cache_poll(struct file *filp, poll_table *wait,
970*4882a593Smuzhiyun 			       struct cache_detail *cd)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	__poll_t mask;
973*4882a593Smuzhiyun 	struct cache_reader *rp = filp->private_data;
974*4882a593Smuzhiyun 	struct cache_queue *cq;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	poll_wait(filp, &queue_wait, wait);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	/* alway allow write */
979*4882a593Smuzhiyun 	mask = EPOLLOUT | EPOLLWRNORM;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	if (!rp)
982*4882a593Smuzhiyun 		return mask;
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	spin_lock(&queue_lock);
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	for (cq= &rp->q; &cq->list != &cd->queue;
987*4882a593Smuzhiyun 	     cq = list_entry(cq->list.next, struct cache_queue, list))
988*4882a593Smuzhiyun 		if (!cq->reader) {
989*4882a593Smuzhiyun 			mask |= EPOLLIN | EPOLLRDNORM;
990*4882a593Smuzhiyun 			break;
991*4882a593Smuzhiyun 		}
992*4882a593Smuzhiyun 	spin_unlock(&queue_lock);
993*4882a593Smuzhiyun 	return mask;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun 
cache_ioctl(struct inode * ino,struct file * filp,unsigned int cmd,unsigned long arg,struct cache_detail * cd)996*4882a593Smuzhiyun static int cache_ioctl(struct inode *ino, struct file *filp,
997*4882a593Smuzhiyun 		       unsigned int cmd, unsigned long arg,
998*4882a593Smuzhiyun 		       struct cache_detail *cd)
999*4882a593Smuzhiyun {
1000*4882a593Smuzhiyun 	int len = 0;
1001*4882a593Smuzhiyun 	struct cache_reader *rp = filp->private_data;
1002*4882a593Smuzhiyun 	struct cache_queue *cq;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	if (cmd != FIONREAD || !rp)
1005*4882a593Smuzhiyun 		return -EINVAL;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	spin_lock(&queue_lock);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	/* only find the length remaining in current request,
1010*4882a593Smuzhiyun 	 * or the length of the next request
1011*4882a593Smuzhiyun 	 */
1012*4882a593Smuzhiyun 	for (cq= &rp->q; &cq->list != &cd->queue;
1013*4882a593Smuzhiyun 	     cq = list_entry(cq->list.next, struct cache_queue, list))
1014*4882a593Smuzhiyun 		if (!cq->reader) {
1015*4882a593Smuzhiyun 			struct cache_request *cr =
1016*4882a593Smuzhiyun 				container_of(cq, struct cache_request, q);
1017*4882a593Smuzhiyun 			len = cr->len - rp->offset;
1018*4882a593Smuzhiyun 			break;
1019*4882a593Smuzhiyun 		}
1020*4882a593Smuzhiyun 	spin_unlock(&queue_lock);
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	return put_user(len, (int __user *)arg);
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
cache_open(struct inode * inode,struct file * filp,struct cache_detail * cd)1025*4882a593Smuzhiyun static int cache_open(struct inode *inode, struct file *filp,
1026*4882a593Smuzhiyun 		      struct cache_detail *cd)
1027*4882a593Smuzhiyun {
1028*4882a593Smuzhiyun 	struct cache_reader *rp = NULL;
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	if (!cd || !try_module_get(cd->owner))
1031*4882a593Smuzhiyun 		return -EACCES;
1032*4882a593Smuzhiyun 	nonseekable_open(inode, filp);
1033*4882a593Smuzhiyun 	if (filp->f_mode & FMODE_READ) {
1034*4882a593Smuzhiyun 		rp = kmalloc(sizeof(*rp), GFP_KERNEL);
1035*4882a593Smuzhiyun 		if (!rp) {
1036*4882a593Smuzhiyun 			module_put(cd->owner);
1037*4882a593Smuzhiyun 			return -ENOMEM;
1038*4882a593Smuzhiyun 		}
1039*4882a593Smuzhiyun 		rp->offset = 0;
1040*4882a593Smuzhiyun 		rp->q.reader = 1;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 		spin_lock(&queue_lock);
1043*4882a593Smuzhiyun 		list_add(&rp->q.list, &cd->queue);
1044*4882a593Smuzhiyun 		spin_unlock(&queue_lock);
1045*4882a593Smuzhiyun 	}
1046*4882a593Smuzhiyun 	if (filp->f_mode & FMODE_WRITE)
1047*4882a593Smuzhiyun 		atomic_inc(&cd->writers);
1048*4882a593Smuzhiyun 	filp->private_data = rp;
1049*4882a593Smuzhiyun 	return 0;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun 
cache_release(struct inode * inode,struct file * filp,struct cache_detail * cd)1052*4882a593Smuzhiyun static int cache_release(struct inode *inode, struct file *filp,
1053*4882a593Smuzhiyun 			 struct cache_detail *cd)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	struct cache_reader *rp = filp->private_data;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (rp) {
1058*4882a593Smuzhiyun 		spin_lock(&queue_lock);
1059*4882a593Smuzhiyun 		if (rp->offset) {
1060*4882a593Smuzhiyun 			struct cache_queue *cq;
1061*4882a593Smuzhiyun 			for (cq= &rp->q; &cq->list != &cd->queue;
1062*4882a593Smuzhiyun 			     cq = list_entry(cq->list.next, struct cache_queue, list))
1063*4882a593Smuzhiyun 				if (!cq->reader) {
1064*4882a593Smuzhiyun 					container_of(cq, struct cache_request, q)
1065*4882a593Smuzhiyun 						->readers--;
1066*4882a593Smuzhiyun 					break;
1067*4882a593Smuzhiyun 				}
1068*4882a593Smuzhiyun 			rp->offset = 0;
1069*4882a593Smuzhiyun 		}
1070*4882a593Smuzhiyun 		list_del(&rp->q.list);
1071*4882a593Smuzhiyun 		spin_unlock(&queue_lock);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 		filp->private_data = NULL;
1074*4882a593Smuzhiyun 		kfree(rp);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	}
1077*4882a593Smuzhiyun 	if (filp->f_mode & FMODE_WRITE) {
1078*4882a593Smuzhiyun 		atomic_dec(&cd->writers);
1079*4882a593Smuzhiyun 		cd->last_close = seconds_since_boot();
1080*4882a593Smuzhiyun 	}
1081*4882a593Smuzhiyun 	module_put(cd->owner);
1082*4882a593Smuzhiyun 	return 0;
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 
cache_dequeue(struct cache_detail * detail,struct cache_head * ch)1087*4882a593Smuzhiyun static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun 	struct cache_queue *cq, *tmp;
1090*4882a593Smuzhiyun 	struct cache_request *cr;
1091*4882a593Smuzhiyun 	struct list_head dequeued;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dequeued);
1094*4882a593Smuzhiyun 	spin_lock(&queue_lock);
1095*4882a593Smuzhiyun 	list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1096*4882a593Smuzhiyun 		if (!cq->reader) {
1097*4882a593Smuzhiyun 			cr = container_of(cq, struct cache_request, q);
1098*4882a593Smuzhiyun 			if (cr->item != ch)
1099*4882a593Smuzhiyun 				continue;
1100*4882a593Smuzhiyun 			if (test_bit(CACHE_PENDING, &ch->flags))
1101*4882a593Smuzhiyun 				/* Lost a race and it is pending again */
1102*4882a593Smuzhiyun 				break;
1103*4882a593Smuzhiyun 			if (cr->readers != 0)
1104*4882a593Smuzhiyun 				continue;
1105*4882a593Smuzhiyun 			list_move(&cr->q.list, &dequeued);
1106*4882a593Smuzhiyun 		}
1107*4882a593Smuzhiyun 	spin_unlock(&queue_lock);
1108*4882a593Smuzhiyun 	while (!list_empty(&dequeued)) {
1109*4882a593Smuzhiyun 		cr = list_entry(dequeued.next, struct cache_request, q.list);
1110*4882a593Smuzhiyun 		list_del(&cr->q.list);
1111*4882a593Smuzhiyun 		cache_put(cr->item, detail);
1112*4882a593Smuzhiyun 		kfree(cr->buf);
1113*4882a593Smuzhiyun 		kfree(cr);
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun /*
1118*4882a593Smuzhiyun  * Support routines for text-based upcalls.
1119*4882a593Smuzhiyun  * Fields are separated by spaces.
1120*4882a593Smuzhiyun  * Fields are either mangled to quote space tab newline slosh with slosh
1121*4882a593Smuzhiyun  * or a hexified with a leading \x
1122*4882a593Smuzhiyun  * Record is terminated with newline.
1123*4882a593Smuzhiyun  *
1124*4882a593Smuzhiyun  */
1125*4882a593Smuzhiyun 
qword_add(char ** bpp,int * lp,char * str)1126*4882a593Smuzhiyun void qword_add(char **bpp, int *lp, char *str)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun 	char *bp = *bpp;
1129*4882a593Smuzhiyun 	int len = *lp;
1130*4882a593Smuzhiyun 	int ret;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	if (len < 0) return;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1135*4882a593Smuzhiyun 	if (ret >= len) {
1136*4882a593Smuzhiyun 		bp += len;
1137*4882a593Smuzhiyun 		len = -1;
1138*4882a593Smuzhiyun 	} else {
1139*4882a593Smuzhiyun 		bp += ret;
1140*4882a593Smuzhiyun 		len -= ret;
1141*4882a593Smuzhiyun 		*bp++ = ' ';
1142*4882a593Smuzhiyun 		len--;
1143*4882a593Smuzhiyun 	}
1144*4882a593Smuzhiyun 	*bpp = bp;
1145*4882a593Smuzhiyun 	*lp = len;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(qword_add);
1148*4882a593Smuzhiyun 
qword_addhex(char ** bpp,int * lp,char * buf,int blen)1149*4882a593Smuzhiyun void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1150*4882a593Smuzhiyun {
1151*4882a593Smuzhiyun 	char *bp = *bpp;
1152*4882a593Smuzhiyun 	int len = *lp;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	if (len < 0) return;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	if (len > 2) {
1157*4882a593Smuzhiyun 		*bp++ = '\\';
1158*4882a593Smuzhiyun 		*bp++ = 'x';
1159*4882a593Smuzhiyun 		len -= 2;
1160*4882a593Smuzhiyun 		while (blen && len >= 2) {
1161*4882a593Smuzhiyun 			bp = hex_byte_pack(bp, *buf++);
1162*4882a593Smuzhiyun 			len -= 2;
1163*4882a593Smuzhiyun 			blen--;
1164*4882a593Smuzhiyun 		}
1165*4882a593Smuzhiyun 	}
1166*4882a593Smuzhiyun 	if (blen || len<1) len = -1;
1167*4882a593Smuzhiyun 	else {
1168*4882a593Smuzhiyun 		*bp++ = ' ';
1169*4882a593Smuzhiyun 		len--;
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 	*bpp = bp;
1172*4882a593Smuzhiyun 	*lp = len;
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(qword_addhex);
1175*4882a593Smuzhiyun 
warn_no_listener(struct cache_detail * detail)1176*4882a593Smuzhiyun static void warn_no_listener(struct cache_detail *detail)
1177*4882a593Smuzhiyun {
1178*4882a593Smuzhiyun 	if (detail->last_warn != detail->last_close) {
1179*4882a593Smuzhiyun 		detail->last_warn = detail->last_close;
1180*4882a593Smuzhiyun 		if (detail->warn_no_listener)
1181*4882a593Smuzhiyun 			detail->warn_no_listener(detail, detail->last_close != 0);
1182*4882a593Smuzhiyun 	}
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun 
cache_listeners_exist(struct cache_detail * detail)1185*4882a593Smuzhiyun static bool cache_listeners_exist(struct cache_detail *detail)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun 	if (atomic_read(&detail->writers))
1188*4882a593Smuzhiyun 		return true;
1189*4882a593Smuzhiyun 	if (detail->last_close == 0)
1190*4882a593Smuzhiyun 		/* This cache was never opened */
1191*4882a593Smuzhiyun 		return false;
1192*4882a593Smuzhiyun 	if (detail->last_close < seconds_since_boot() - 30)
1193*4882a593Smuzhiyun 		/*
1194*4882a593Smuzhiyun 		 * We allow for the possibility that someone might
1195*4882a593Smuzhiyun 		 * restart a userspace daemon without restarting the
1196*4882a593Smuzhiyun 		 * server; but after 30 seconds, we give up.
1197*4882a593Smuzhiyun 		 */
1198*4882a593Smuzhiyun 		 return false;
1199*4882a593Smuzhiyun 	return true;
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun /*
1203*4882a593Smuzhiyun  * register an upcall request to user-space and queue it up for read() by the
1204*4882a593Smuzhiyun  * upcall daemon.
1205*4882a593Smuzhiyun  *
1206*4882a593Smuzhiyun  * Each request is at most one page long.
1207*4882a593Smuzhiyun  */
cache_pipe_upcall(struct cache_detail * detail,struct cache_head * h)1208*4882a593Smuzhiyun static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1209*4882a593Smuzhiyun {
1210*4882a593Smuzhiyun 	char *buf;
1211*4882a593Smuzhiyun 	struct cache_request *crq;
1212*4882a593Smuzhiyun 	int ret = 0;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	if (test_bit(CACHE_CLEANED, &h->flags))
1215*4882a593Smuzhiyun 		/* Too late to make an upcall */
1216*4882a593Smuzhiyun 		return -EAGAIN;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1219*4882a593Smuzhiyun 	if (!buf)
1220*4882a593Smuzhiyun 		return -EAGAIN;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1223*4882a593Smuzhiyun 	if (!crq) {
1224*4882a593Smuzhiyun 		kfree(buf);
1225*4882a593Smuzhiyun 		return -EAGAIN;
1226*4882a593Smuzhiyun 	}
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	crq->q.reader = 0;
1229*4882a593Smuzhiyun 	crq->buf = buf;
1230*4882a593Smuzhiyun 	crq->len = 0;
1231*4882a593Smuzhiyun 	crq->readers = 0;
1232*4882a593Smuzhiyun 	spin_lock(&queue_lock);
1233*4882a593Smuzhiyun 	if (test_bit(CACHE_PENDING, &h->flags)) {
1234*4882a593Smuzhiyun 		crq->item = cache_get(h);
1235*4882a593Smuzhiyun 		list_add_tail(&crq->q.list, &detail->queue);
1236*4882a593Smuzhiyun 		trace_cache_entry_upcall(detail, h);
1237*4882a593Smuzhiyun 	} else
1238*4882a593Smuzhiyun 		/* Lost a race, no longer PENDING, so don't enqueue */
1239*4882a593Smuzhiyun 		ret = -EAGAIN;
1240*4882a593Smuzhiyun 	spin_unlock(&queue_lock);
1241*4882a593Smuzhiyun 	wake_up(&queue_wait);
1242*4882a593Smuzhiyun 	if (ret == -EAGAIN) {
1243*4882a593Smuzhiyun 		kfree(buf);
1244*4882a593Smuzhiyun 		kfree(crq);
1245*4882a593Smuzhiyun 	}
1246*4882a593Smuzhiyun 	return ret;
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun 
sunrpc_cache_pipe_upcall(struct cache_detail * detail,struct cache_head * h)1249*4882a593Smuzhiyun int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1250*4882a593Smuzhiyun {
1251*4882a593Smuzhiyun 	if (test_and_set_bit(CACHE_PENDING, &h->flags))
1252*4882a593Smuzhiyun 		return 0;
1253*4882a593Smuzhiyun 	return cache_pipe_upcall(detail, h);
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1256*4882a593Smuzhiyun 
sunrpc_cache_pipe_upcall_timeout(struct cache_detail * detail,struct cache_head * h)1257*4882a593Smuzhiyun int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
1258*4882a593Smuzhiyun 				     struct cache_head *h)
1259*4882a593Smuzhiyun {
1260*4882a593Smuzhiyun 	if (!cache_listeners_exist(detail)) {
1261*4882a593Smuzhiyun 		warn_no_listener(detail);
1262*4882a593Smuzhiyun 		trace_cache_entry_no_listener(detail, h);
1263*4882a593Smuzhiyun 		return -EINVAL;
1264*4882a593Smuzhiyun 	}
1265*4882a593Smuzhiyun 	return sunrpc_cache_pipe_upcall(detail, h);
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun /*
1270*4882a593Smuzhiyun  * parse a message from user-space and pass it
1271*4882a593Smuzhiyun  * to an appropriate cache
1272*4882a593Smuzhiyun  * Messages are, like requests, separated into fields by
1273*4882a593Smuzhiyun  * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1274*4882a593Smuzhiyun  *
1275*4882a593Smuzhiyun  * Message is
1276*4882a593Smuzhiyun  *   reply cachename expiry key ... content....
1277*4882a593Smuzhiyun  *
1278*4882a593Smuzhiyun  * key and content are both parsed by cache
1279*4882a593Smuzhiyun  */
1280*4882a593Smuzhiyun 
qword_get(char ** bpp,char * dest,int bufsize)1281*4882a593Smuzhiyun int qword_get(char **bpp, char *dest, int bufsize)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	/* return bytes copied, or -1 on error */
1284*4882a593Smuzhiyun 	char *bp = *bpp;
1285*4882a593Smuzhiyun 	int len = 0;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	while (*bp == ' ') bp++;
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	if (bp[0] == '\\' && bp[1] == 'x') {
1290*4882a593Smuzhiyun 		/* HEX STRING */
1291*4882a593Smuzhiyun 		bp += 2;
1292*4882a593Smuzhiyun 		while (len < bufsize - 1) {
1293*4882a593Smuzhiyun 			int h, l;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 			h = hex_to_bin(bp[0]);
1296*4882a593Smuzhiyun 			if (h < 0)
1297*4882a593Smuzhiyun 				break;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 			l = hex_to_bin(bp[1]);
1300*4882a593Smuzhiyun 			if (l < 0)
1301*4882a593Smuzhiyun 				break;
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 			*dest++ = (h << 4) | l;
1304*4882a593Smuzhiyun 			bp += 2;
1305*4882a593Smuzhiyun 			len++;
1306*4882a593Smuzhiyun 		}
1307*4882a593Smuzhiyun 	} else {
1308*4882a593Smuzhiyun 		/* text with \nnn octal quoting */
1309*4882a593Smuzhiyun 		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1310*4882a593Smuzhiyun 			if (*bp == '\\' &&
1311*4882a593Smuzhiyun 			    isodigit(bp[1]) && (bp[1] <= '3') &&
1312*4882a593Smuzhiyun 			    isodigit(bp[2]) &&
1313*4882a593Smuzhiyun 			    isodigit(bp[3])) {
1314*4882a593Smuzhiyun 				int byte = (*++bp -'0');
1315*4882a593Smuzhiyun 				bp++;
1316*4882a593Smuzhiyun 				byte = (byte << 3) | (*bp++ - '0');
1317*4882a593Smuzhiyun 				byte = (byte << 3) | (*bp++ - '0');
1318*4882a593Smuzhiyun 				*dest++ = byte;
1319*4882a593Smuzhiyun 				len++;
1320*4882a593Smuzhiyun 			} else {
1321*4882a593Smuzhiyun 				*dest++ = *bp++;
1322*4882a593Smuzhiyun 				len++;
1323*4882a593Smuzhiyun 			}
1324*4882a593Smuzhiyun 		}
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1328*4882a593Smuzhiyun 		return -1;
1329*4882a593Smuzhiyun 	while (*bp == ' ') bp++;
1330*4882a593Smuzhiyun 	*bpp = bp;
1331*4882a593Smuzhiyun 	*dest = '\0';
1332*4882a593Smuzhiyun 	return len;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(qword_get);
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun /*
1338*4882a593Smuzhiyun  * support /proc/net/rpc/$CACHENAME/content
1339*4882a593Smuzhiyun  * as a seqfile.
1340*4882a593Smuzhiyun  * We call ->cache_show passing NULL for the item to
1341*4882a593Smuzhiyun  * get a header, then pass each real item in the cache
1342*4882a593Smuzhiyun  */
1343*4882a593Smuzhiyun 
__cache_seq_start(struct seq_file * m,loff_t * pos)1344*4882a593Smuzhiyun static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
1345*4882a593Smuzhiyun {
1346*4882a593Smuzhiyun 	loff_t n = *pos;
1347*4882a593Smuzhiyun 	unsigned int hash, entry;
1348*4882a593Smuzhiyun 	struct cache_head *ch;
1349*4882a593Smuzhiyun 	struct cache_detail *cd = m->private;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	if (!n--)
1352*4882a593Smuzhiyun 		return SEQ_START_TOKEN;
1353*4882a593Smuzhiyun 	hash = n >> 32;
1354*4882a593Smuzhiyun 	entry = n & ((1LL<<32) - 1);
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
1357*4882a593Smuzhiyun 		if (!entry--)
1358*4882a593Smuzhiyun 			return ch;
1359*4882a593Smuzhiyun 	n &= ~((1LL<<32) - 1);
1360*4882a593Smuzhiyun 	do {
1361*4882a593Smuzhiyun 		hash++;
1362*4882a593Smuzhiyun 		n += 1LL<<32;
1363*4882a593Smuzhiyun 	} while(hash < cd->hash_size &&
1364*4882a593Smuzhiyun 		hlist_empty(&cd->hash_table[hash]));
1365*4882a593Smuzhiyun 	if (hash >= cd->hash_size)
1366*4882a593Smuzhiyun 		return NULL;
1367*4882a593Smuzhiyun 	*pos = n+1;
1368*4882a593Smuzhiyun 	return hlist_entry_safe(rcu_dereference_raw(
1369*4882a593Smuzhiyun 				hlist_first_rcu(&cd->hash_table[hash])),
1370*4882a593Smuzhiyun 				struct cache_head, cache_list);
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun 
cache_seq_next(struct seq_file * m,void * p,loff_t * pos)1373*4882a593Smuzhiyun static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
1374*4882a593Smuzhiyun {
1375*4882a593Smuzhiyun 	struct cache_head *ch = p;
1376*4882a593Smuzhiyun 	int hash = (*pos >> 32);
1377*4882a593Smuzhiyun 	struct cache_detail *cd = m->private;
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (p == SEQ_START_TOKEN)
1380*4882a593Smuzhiyun 		hash = 0;
1381*4882a593Smuzhiyun 	else if (ch->cache_list.next == NULL) {
1382*4882a593Smuzhiyun 		hash++;
1383*4882a593Smuzhiyun 		*pos += 1LL<<32;
1384*4882a593Smuzhiyun 	} else {
1385*4882a593Smuzhiyun 		++*pos;
1386*4882a593Smuzhiyun 		return hlist_entry_safe(rcu_dereference_raw(
1387*4882a593Smuzhiyun 					hlist_next_rcu(&ch->cache_list)),
1388*4882a593Smuzhiyun 					struct cache_head, cache_list);
1389*4882a593Smuzhiyun 	}
1390*4882a593Smuzhiyun 	*pos &= ~((1LL<<32) - 1);
1391*4882a593Smuzhiyun 	while (hash < cd->hash_size &&
1392*4882a593Smuzhiyun 	       hlist_empty(&cd->hash_table[hash])) {
1393*4882a593Smuzhiyun 		hash++;
1394*4882a593Smuzhiyun 		*pos += 1LL<<32;
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 	if (hash >= cd->hash_size)
1397*4882a593Smuzhiyun 		return NULL;
1398*4882a593Smuzhiyun 	++*pos;
1399*4882a593Smuzhiyun 	return hlist_entry_safe(rcu_dereference_raw(
1400*4882a593Smuzhiyun 				hlist_first_rcu(&cd->hash_table[hash])),
1401*4882a593Smuzhiyun 				struct cache_head, cache_list);
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun 
cache_seq_start_rcu(struct seq_file * m,loff_t * pos)1404*4882a593Smuzhiyun void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1405*4882a593Smuzhiyun 	__acquires(RCU)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun 	rcu_read_lock();
1408*4882a593Smuzhiyun 	return __cache_seq_start(m, pos);
1409*4882a593Smuzhiyun }
1410*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1411*4882a593Smuzhiyun 
cache_seq_next_rcu(struct seq_file * file,void * p,loff_t * pos)1412*4882a593Smuzhiyun void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1413*4882a593Smuzhiyun {
1414*4882a593Smuzhiyun 	return cache_seq_next(file, p, pos);
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1417*4882a593Smuzhiyun 
cache_seq_stop_rcu(struct seq_file * m,void * p)1418*4882a593Smuzhiyun void cache_seq_stop_rcu(struct seq_file *m, void *p)
1419*4882a593Smuzhiyun 	__releases(RCU)
1420*4882a593Smuzhiyun {
1421*4882a593Smuzhiyun 	rcu_read_unlock();
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1424*4882a593Smuzhiyun 
c_show(struct seq_file * m,void * p)1425*4882a593Smuzhiyun static int c_show(struct seq_file *m, void *p)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun 	struct cache_head *cp = p;
1428*4882a593Smuzhiyun 	struct cache_detail *cd = m->private;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (p == SEQ_START_TOKEN)
1431*4882a593Smuzhiyun 		return cd->cache_show(m, cd, NULL);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	ifdebug(CACHE)
1434*4882a593Smuzhiyun 		seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
1435*4882a593Smuzhiyun 			   convert_to_wallclock(cp->expiry_time),
1436*4882a593Smuzhiyun 			   kref_read(&cp->ref), cp->flags);
1437*4882a593Smuzhiyun 	cache_get(cp);
1438*4882a593Smuzhiyun 	if (cache_check(cd, cp, NULL))
1439*4882a593Smuzhiyun 		/* cache_check does a cache_put on failure */
1440*4882a593Smuzhiyun 		seq_puts(m, "# ");
1441*4882a593Smuzhiyun 	else {
1442*4882a593Smuzhiyun 		if (cache_is_expired(cd, cp))
1443*4882a593Smuzhiyun 			seq_puts(m, "# ");
1444*4882a593Smuzhiyun 		cache_put(cp, cd);
1445*4882a593Smuzhiyun 	}
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	return cd->cache_show(m, cd, cp);
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun static const struct seq_operations cache_content_op = {
1451*4882a593Smuzhiyun 	.start	= cache_seq_start_rcu,
1452*4882a593Smuzhiyun 	.next	= cache_seq_next_rcu,
1453*4882a593Smuzhiyun 	.stop	= cache_seq_stop_rcu,
1454*4882a593Smuzhiyun 	.show	= c_show,
1455*4882a593Smuzhiyun };
1456*4882a593Smuzhiyun 
content_open(struct inode * inode,struct file * file,struct cache_detail * cd)1457*4882a593Smuzhiyun static int content_open(struct inode *inode, struct file *file,
1458*4882a593Smuzhiyun 			struct cache_detail *cd)
1459*4882a593Smuzhiyun {
1460*4882a593Smuzhiyun 	struct seq_file *seq;
1461*4882a593Smuzhiyun 	int err;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	if (!cd || !try_module_get(cd->owner))
1464*4882a593Smuzhiyun 		return -EACCES;
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 	err = seq_open(file, &cache_content_op);
1467*4882a593Smuzhiyun 	if (err) {
1468*4882a593Smuzhiyun 		module_put(cd->owner);
1469*4882a593Smuzhiyun 		return err;
1470*4882a593Smuzhiyun 	}
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	seq = file->private_data;
1473*4882a593Smuzhiyun 	seq->private = cd;
1474*4882a593Smuzhiyun 	return 0;
1475*4882a593Smuzhiyun }
1476*4882a593Smuzhiyun 
content_release(struct inode * inode,struct file * file,struct cache_detail * cd)1477*4882a593Smuzhiyun static int content_release(struct inode *inode, struct file *file,
1478*4882a593Smuzhiyun 		struct cache_detail *cd)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun 	int ret = seq_release(inode, file);
1481*4882a593Smuzhiyun 	module_put(cd->owner);
1482*4882a593Smuzhiyun 	return ret;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun 
open_flush(struct inode * inode,struct file * file,struct cache_detail * cd)1485*4882a593Smuzhiyun static int open_flush(struct inode *inode, struct file *file,
1486*4882a593Smuzhiyun 			struct cache_detail *cd)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun 	if (!cd || !try_module_get(cd->owner))
1489*4882a593Smuzhiyun 		return -EACCES;
1490*4882a593Smuzhiyun 	return nonseekable_open(inode, file);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun 
release_flush(struct inode * inode,struct file * file,struct cache_detail * cd)1493*4882a593Smuzhiyun static int release_flush(struct inode *inode, struct file *file,
1494*4882a593Smuzhiyun 			struct cache_detail *cd)
1495*4882a593Smuzhiyun {
1496*4882a593Smuzhiyun 	module_put(cd->owner);
1497*4882a593Smuzhiyun 	return 0;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun 
read_flush(struct file * file,char __user * buf,size_t count,loff_t * ppos,struct cache_detail * cd)1500*4882a593Smuzhiyun static ssize_t read_flush(struct file *file, char __user *buf,
1501*4882a593Smuzhiyun 			  size_t count, loff_t *ppos,
1502*4882a593Smuzhiyun 			  struct cache_detail *cd)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun 	char tbuf[22];
1505*4882a593Smuzhiyun 	size_t len;
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
1508*4882a593Smuzhiyun 			convert_to_wallclock(cd->flush_time));
1509*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, count, ppos, tbuf, len);
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun 
write_flush(struct file * file,const char __user * buf,size_t count,loff_t * ppos,struct cache_detail * cd)1512*4882a593Smuzhiyun static ssize_t write_flush(struct file *file, const char __user *buf,
1513*4882a593Smuzhiyun 			   size_t count, loff_t *ppos,
1514*4882a593Smuzhiyun 			   struct cache_detail *cd)
1515*4882a593Smuzhiyun {
1516*4882a593Smuzhiyun 	char tbuf[20];
1517*4882a593Smuzhiyun 	char *ep;
1518*4882a593Smuzhiyun 	time64_t now;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	if (*ppos || count > sizeof(tbuf)-1)
1521*4882a593Smuzhiyun 		return -EINVAL;
1522*4882a593Smuzhiyun 	if (copy_from_user(tbuf, buf, count))
1523*4882a593Smuzhiyun 		return -EFAULT;
1524*4882a593Smuzhiyun 	tbuf[count] = 0;
1525*4882a593Smuzhiyun 	simple_strtoul(tbuf, &ep, 0);
1526*4882a593Smuzhiyun 	if (*ep && *ep != '\n')
1527*4882a593Smuzhiyun 		return -EINVAL;
1528*4882a593Smuzhiyun 	/* Note that while we check that 'buf' holds a valid number,
1529*4882a593Smuzhiyun 	 * we always ignore the value and just flush everything.
1530*4882a593Smuzhiyun 	 * Making use of the number leads to races.
1531*4882a593Smuzhiyun 	 */
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	now = seconds_since_boot();
1534*4882a593Smuzhiyun 	/* Always flush everything, so behave like cache_purge()
1535*4882a593Smuzhiyun 	 * Do this by advancing flush_time to the current time,
1536*4882a593Smuzhiyun 	 * or by one second if it has already reached the current time.
1537*4882a593Smuzhiyun 	 * Newly added cache entries will always have ->last_refresh greater
1538*4882a593Smuzhiyun 	 * that ->flush_time, so they don't get flushed prematurely.
1539*4882a593Smuzhiyun 	 */
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	if (cd->flush_time >= now)
1542*4882a593Smuzhiyun 		now = cd->flush_time + 1;
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	cd->flush_time = now;
1545*4882a593Smuzhiyun 	cd->nextcheck = now;
1546*4882a593Smuzhiyun 	cache_flush();
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	if (cd->flush)
1549*4882a593Smuzhiyun 		cd->flush();
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	*ppos += count;
1552*4882a593Smuzhiyun 	return count;
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun 
cache_read_procfs(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1555*4882a593Smuzhiyun static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1556*4882a593Smuzhiyun 				 size_t count, loff_t *ppos)
1557*4882a593Smuzhiyun {
1558*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(file_inode(filp));
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	return cache_read(filp, buf, count, ppos, cd);
1561*4882a593Smuzhiyun }
1562*4882a593Smuzhiyun 
cache_write_procfs(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)1563*4882a593Smuzhiyun static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1564*4882a593Smuzhiyun 				  size_t count, loff_t *ppos)
1565*4882a593Smuzhiyun {
1566*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(file_inode(filp));
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	return cache_write(filp, buf, count, ppos, cd);
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun 
cache_poll_procfs(struct file * filp,poll_table * wait)1571*4882a593Smuzhiyun static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
1572*4882a593Smuzhiyun {
1573*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(file_inode(filp));
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	return cache_poll(filp, wait, cd);
1576*4882a593Smuzhiyun }
1577*4882a593Smuzhiyun 
cache_ioctl_procfs(struct file * filp,unsigned int cmd,unsigned long arg)1578*4882a593Smuzhiyun static long cache_ioctl_procfs(struct file *filp,
1579*4882a593Smuzhiyun 			       unsigned int cmd, unsigned long arg)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
1582*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	return cache_ioctl(inode, filp, cmd, arg, cd);
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun 
cache_open_procfs(struct inode * inode,struct file * filp)1587*4882a593Smuzhiyun static int cache_open_procfs(struct inode *inode, struct file *filp)
1588*4882a593Smuzhiyun {
1589*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	return cache_open(inode, filp, cd);
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun 
cache_release_procfs(struct inode * inode,struct file * filp)1594*4882a593Smuzhiyun static int cache_release_procfs(struct inode *inode, struct file *filp)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun 	return cache_release(inode, filp, cd);
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun static const struct proc_ops cache_channel_proc_ops = {
1602*4882a593Smuzhiyun 	.proc_lseek	= no_llseek,
1603*4882a593Smuzhiyun 	.proc_read	= cache_read_procfs,
1604*4882a593Smuzhiyun 	.proc_write	= cache_write_procfs,
1605*4882a593Smuzhiyun 	.proc_poll	= cache_poll_procfs,
1606*4882a593Smuzhiyun 	.proc_ioctl	= cache_ioctl_procfs, /* for FIONREAD */
1607*4882a593Smuzhiyun 	.proc_open	= cache_open_procfs,
1608*4882a593Smuzhiyun 	.proc_release	= cache_release_procfs,
1609*4882a593Smuzhiyun };
1610*4882a593Smuzhiyun 
content_open_procfs(struct inode * inode,struct file * filp)1611*4882a593Smuzhiyun static int content_open_procfs(struct inode *inode, struct file *filp)
1612*4882a593Smuzhiyun {
1613*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun 	return content_open(inode, filp, cd);
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun 
content_release_procfs(struct inode * inode,struct file * filp)1618*4882a593Smuzhiyun static int content_release_procfs(struct inode *inode, struct file *filp)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	return content_release(inode, filp, cd);
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun static const struct proc_ops content_proc_ops = {
1626*4882a593Smuzhiyun 	.proc_open	= content_open_procfs,
1627*4882a593Smuzhiyun 	.proc_read	= seq_read,
1628*4882a593Smuzhiyun 	.proc_lseek	= seq_lseek,
1629*4882a593Smuzhiyun 	.proc_release	= content_release_procfs,
1630*4882a593Smuzhiyun };
1631*4882a593Smuzhiyun 
open_flush_procfs(struct inode * inode,struct file * filp)1632*4882a593Smuzhiyun static int open_flush_procfs(struct inode *inode, struct file *filp)
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	return open_flush(inode, filp, cd);
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun 
release_flush_procfs(struct inode * inode,struct file * filp)1639*4882a593Smuzhiyun static int release_flush_procfs(struct inode *inode, struct file *filp)
1640*4882a593Smuzhiyun {
1641*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(inode);
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 	return release_flush(inode, filp, cd);
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun 
read_flush_procfs(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1646*4882a593Smuzhiyun static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1647*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(file_inode(filp));
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	return read_flush(filp, buf, count, ppos, cd);
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun 
write_flush_procfs(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)1654*4882a593Smuzhiyun static ssize_t write_flush_procfs(struct file *filp,
1655*4882a593Smuzhiyun 				  const char __user *buf,
1656*4882a593Smuzhiyun 				  size_t count, loff_t *ppos)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun 	struct cache_detail *cd = PDE_DATA(file_inode(filp));
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	return write_flush(filp, buf, count, ppos, cd);
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun static const struct proc_ops cache_flush_proc_ops = {
1664*4882a593Smuzhiyun 	.proc_open	= open_flush_procfs,
1665*4882a593Smuzhiyun 	.proc_read	= read_flush_procfs,
1666*4882a593Smuzhiyun 	.proc_write	= write_flush_procfs,
1667*4882a593Smuzhiyun 	.proc_release	= release_flush_procfs,
1668*4882a593Smuzhiyun 	.proc_lseek	= no_llseek,
1669*4882a593Smuzhiyun };
1670*4882a593Smuzhiyun 
remove_cache_proc_entries(struct cache_detail * cd)1671*4882a593Smuzhiyun static void remove_cache_proc_entries(struct cache_detail *cd)
1672*4882a593Smuzhiyun {
1673*4882a593Smuzhiyun 	if (cd->procfs) {
1674*4882a593Smuzhiyun 		proc_remove(cd->procfs);
1675*4882a593Smuzhiyun 		cd->procfs = NULL;
1676*4882a593Smuzhiyun 	}
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun #ifdef CONFIG_PROC_FS
create_cache_proc_entries(struct cache_detail * cd,struct net * net)1680*4882a593Smuzhiyun static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1681*4882a593Smuzhiyun {
1682*4882a593Smuzhiyun 	struct proc_dir_entry *p;
1683*4882a593Smuzhiyun 	struct sunrpc_net *sn;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	sn = net_generic(net, sunrpc_net_id);
1686*4882a593Smuzhiyun 	cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1687*4882a593Smuzhiyun 	if (cd->procfs == NULL)
1688*4882a593Smuzhiyun 		goto out_nomem;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	p = proc_create_data("flush", S_IFREG | 0600,
1691*4882a593Smuzhiyun 			     cd->procfs, &cache_flush_proc_ops, cd);
1692*4882a593Smuzhiyun 	if (p == NULL)
1693*4882a593Smuzhiyun 		goto out_nomem;
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	if (cd->cache_request || cd->cache_parse) {
1696*4882a593Smuzhiyun 		p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1697*4882a593Smuzhiyun 				     &cache_channel_proc_ops, cd);
1698*4882a593Smuzhiyun 		if (p == NULL)
1699*4882a593Smuzhiyun 			goto out_nomem;
1700*4882a593Smuzhiyun 	}
1701*4882a593Smuzhiyun 	if (cd->cache_show) {
1702*4882a593Smuzhiyun 		p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1703*4882a593Smuzhiyun 				     &content_proc_ops, cd);
1704*4882a593Smuzhiyun 		if (p == NULL)
1705*4882a593Smuzhiyun 			goto out_nomem;
1706*4882a593Smuzhiyun 	}
1707*4882a593Smuzhiyun 	return 0;
1708*4882a593Smuzhiyun out_nomem:
1709*4882a593Smuzhiyun 	remove_cache_proc_entries(cd);
1710*4882a593Smuzhiyun 	return -ENOMEM;
1711*4882a593Smuzhiyun }
1712*4882a593Smuzhiyun #else /* CONFIG_PROC_FS */
create_cache_proc_entries(struct cache_detail * cd,struct net * net)1713*4882a593Smuzhiyun static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1714*4882a593Smuzhiyun {
1715*4882a593Smuzhiyun 	return 0;
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun #endif
1718*4882a593Smuzhiyun 
cache_initialize(void)1719*4882a593Smuzhiyun void __init cache_initialize(void)
1720*4882a593Smuzhiyun {
1721*4882a593Smuzhiyun 	INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun 
cache_register_net(struct cache_detail * cd,struct net * net)1724*4882a593Smuzhiyun int cache_register_net(struct cache_detail *cd, struct net *net)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun 	int ret;
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	sunrpc_init_cache_detail(cd);
1729*4882a593Smuzhiyun 	ret = create_cache_proc_entries(cd, net);
1730*4882a593Smuzhiyun 	if (ret)
1731*4882a593Smuzhiyun 		sunrpc_destroy_cache_detail(cd);
1732*4882a593Smuzhiyun 	return ret;
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_register_net);
1735*4882a593Smuzhiyun 
cache_unregister_net(struct cache_detail * cd,struct net * net)1736*4882a593Smuzhiyun void cache_unregister_net(struct cache_detail *cd, struct net *net)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	remove_cache_proc_entries(cd);
1739*4882a593Smuzhiyun 	sunrpc_destroy_cache_detail(cd);
1740*4882a593Smuzhiyun }
1741*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_unregister_net);
1742*4882a593Smuzhiyun 
cache_create_net(const struct cache_detail * tmpl,struct net * net)1743*4882a593Smuzhiyun struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
1744*4882a593Smuzhiyun {
1745*4882a593Smuzhiyun 	struct cache_detail *cd;
1746*4882a593Smuzhiyun 	int i;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1749*4882a593Smuzhiyun 	if (cd == NULL)
1750*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
1753*4882a593Smuzhiyun 				 GFP_KERNEL);
1754*4882a593Smuzhiyun 	if (cd->hash_table == NULL) {
1755*4882a593Smuzhiyun 		kfree(cd);
1756*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	for (i = 0; i < cd->hash_size; i++)
1760*4882a593Smuzhiyun 		INIT_HLIST_HEAD(&cd->hash_table[i]);
1761*4882a593Smuzhiyun 	cd->net = net;
1762*4882a593Smuzhiyun 	return cd;
1763*4882a593Smuzhiyun }
1764*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_create_net);
1765*4882a593Smuzhiyun 
cache_destroy_net(struct cache_detail * cd,struct net * net)1766*4882a593Smuzhiyun void cache_destroy_net(struct cache_detail *cd, struct net *net)
1767*4882a593Smuzhiyun {
1768*4882a593Smuzhiyun 	kfree(cd->hash_table);
1769*4882a593Smuzhiyun 	kfree(cd);
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cache_destroy_net);
1772*4882a593Smuzhiyun 
cache_read_pipefs(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1773*4882a593Smuzhiyun static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1774*4882a593Smuzhiyun 				 size_t count, loff_t *ppos)
1775*4882a593Smuzhiyun {
1776*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1777*4882a593Smuzhiyun 
1778*4882a593Smuzhiyun 	return cache_read(filp, buf, count, ppos, cd);
1779*4882a593Smuzhiyun }
1780*4882a593Smuzhiyun 
cache_write_pipefs(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)1781*4882a593Smuzhiyun static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1782*4882a593Smuzhiyun 				  size_t count, loff_t *ppos)
1783*4882a593Smuzhiyun {
1784*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	return cache_write(filp, buf, count, ppos, cd);
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun 
cache_poll_pipefs(struct file * filp,poll_table * wait)1789*4882a593Smuzhiyun static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	return cache_poll(filp, wait, cd);
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun 
cache_ioctl_pipefs(struct file * filp,unsigned int cmd,unsigned long arg)1796*4882a593Smuzhiyun static long cache_ioctl_pipefs(struct file *filp,
1797*4882a593Smuzhiyun 			      unsigned int cmd, unsigned long arg)
1798*4882a593Smuzhiyun {
1799*4882a593Smuzhiyun 	struct inode *inode = file_inode(filp);
1800*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	return cache_ioctl(inode, filp, cmd, arg, cd);
1803*4882a593Smuzhiyun }
1804*4882a593Smuzhiyun 
cache_open_pipefs(struct inode * inode,struct file * filp)1805*4882a593Smuzhiyun static int cache_open_pipefs(struct inode *inode, struct file *filp)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	return cache_open(inode, filp, cd);
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun 
cache_release_pipefs(struct inode * inode,struct file * filp)1812*4882a593Smuzhiyun static int cache_release_pipefs(struct inode *inode, struct file *filp)
1813*4882a593Smuzhiyun {
1814*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	return cache_release(inode, filp, cd);
1817*4882a593Smuzhiyun }
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun const struct file_operations cache_file_operations_pipefs = {
1820*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1821*4882a593Smuzhiyun 	.llseek		= no_llseek,
1822*4882a593Smuzhiyun 	.read		= cache_read_pipefs,
1823*4882a593Smuzhiyun 	.write		= cache_write_pipefs,
1824*4882a593Smuzhiyun 	.poll		= cache_poll_pipefs,
1825*4882a593Smuzhiyun 	.unlocked_ioctl	= cache_ioctl_pipefs, /* for FIONREAD */
1826*4882a593Smuzhiyun 	.open		= cache_open_pipefs,
1827*4882a593Smuzhiyun 	.release	= cache_release_pipefs,
1828*4882a593Smuzhiyun };
1829*4882a593Smuzhiyun 
content_open_pipefs(struct inode * inode,struct file * filp)1830*4882a593Smuzhiyun static int content_open_pipefs(struct inode *inode, struct file *filp)
1831*4882a593Smuzhiyun {
1832*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun 	return content_open(inode, filp, cd);
1835*4882a593Smuzhiyun }
1836*4882a593Smuzhiyun 
content_release_pipefs(struct inode * inode,struct file * filp)1837*4882a593Smuzhiyun static int content_release_pipefs(struct inode *inode, struct file *filp)
1838*4882a593Smuzhiyun {
1839*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	return content_release(inode, filp, cd);
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun const struct file_operations content_file_operations_pipefs = {
1845*4882a593Smuzhiyun 	.open		= content_open_pipefs,
1846*4882a593Smuzhiyun 	.read		= seq_read,
1847*4882a593Smuzhiyun 	.llseek		= seq_lseek,
1848*4882a593Smuzhiyun 	.release	= content_release_pipefs,
1849*4882a593Smuzhiyun };
1850*4882a593Smuzhiyun 
open_flush_pipefs(struct inode * inode,struct file * filp)1851*4882a593Smuzhiyun static int open_flush_pipefs(struct inode *inode, struct file *filp)
1852*4882a593Smuzhiyun {
1853*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 	return open_flush(inode, filp, cd);
1856*4882a593Smuzhiyun }
1857*4882a593Smuzhiyun 
release_flush_pipefs(struct inode * inode,struct file * filp)1858*4882a593Smuzhiyun static int release_flush_pipefs(struct inode *inode, struct file *filp)
1859*4882a593Smuzhiyun {
1860*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(inode)->private;
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	return release_flush(inode, filp, cd);
1863*4882a593Smuzhiyun }
1864*4882a593Smuzhiyun 
read_flush_pipefs(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1865*4882a593Smuzhiyun static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1866*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
1867*4882a593Smuzhiyun {
1868*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	return read_flush(filp, buf, count, ppos, cd);
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun 
write_flush_pipefs(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)1873*4882a593Smuzhiyun static ssize_t write_flush_pipefs(struct file *filp,
1874*4882a593Smuzhiyun 				  const char __user *buf,
1875*4882a593Smuzhiyun 				  size_t count, loff_t *ppos)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun 	struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	return write_flush(filp, buf, count, ppos, cd);
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun const struct file_operations cache_flush_operations_pipefs = {
1883*4882a593Smuzhiyun 	.open		= open_flush_pipefs,
1884*4882a593Smuzhiyun 	.read		= read_flush_pipefs,
1885*4882a593Smuzhiyun 	.write		= write_flush_pipefs,
1886*4882a593Smuzhiyun 	.release	= release_flush_pipefs,
1887*4882a593Smuzhiyun 	.llseek		= no_llseek,
1888*4882a593Smuzhiyun };
1889*4882a593Smuzhiyun 
sunrpc_cache_register_pipefs(struct dentry * parent,const char * name,umode_t umode,struct cache_detail * cd)1890*4882a593Smuzhiyun int sunrpc_cache_register_pipefs(struct dentry *parent,
1891*4882a593Smuzhiyun 				 const char *name, umode_t umode,
1892*4882a593Smuzhiyun 				 struct cache_detail *cd)
1893*4882a593Smuzhiyun {
1894*4882a593Smuzhiyun 	struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1895*4882a593Smuzhiyun 	if (IS_ERR(dir))
1896*4882a593Smuzhiyun 		return PTR_ERR(dir);
1897*4882a593Smuzhiyun 	cd->pipefs = dir;
1898*4882a593Smuzhiyun 	return 0;
1899*4882a593Smuzhiyun }
1900*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1901*4882a593Smuzhiyun 
sunrpc_cache_unregister_pipefs(struct cache_detail * cd)1902*4882a593Smuzhiyun void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun 	if (cd->pipefs) {
1905*4882a593Smuzhiyun 		rpc_remove_cache_dir(cd->pipefs);
1906*4882a593Smuzhiyun 		cd->pipefs = NULL;
1907*4882a593Smuzhiyun 	}
1908*4882a593Smuzhiyun }
1909*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1910*4882a593Smuzhiyun 
sunrpc_cache_unhash(struct cache_detail * cd,struct cache_head * h)1911*4882a593Smuzhiyun void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1912*4882a593Smuzhiyun {
1913*4882a593Smuzhiyun 	spin_lock(&cd->hash_lock);
1914*4882a593Smuzhiyun 	if (!hlist_unhashed(&h->cache_list)){
1915*4882a593Smuzhiyun 		sunrpc_begin_cache_remove_entry(h, cd);
1916*4882a593Smuzhiyun 		spin_unlock(&cd->hash_lock);
1917*4882a593Smuzhiyun 		sunrpc_end_cache_remove_entry(h, cd);
1918*4882a593Smuzhiyun 	} else
1919*4882a593Smuzhiyun 		spin_unlock(&cd->hash_lock);
1920*4882a593Smuzhiyun }
1921*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);
1922