xref: /linux/net/sunrpc/cache.c (revision 1117449276bb909b029ed0b9ba13f53e4784db9d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sunrpc/cache.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Generic code for various authentication-related caches
51da177e4SLinus Torvalds  * used by sunrpc clients and servers.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Released under terms in GPL version 2.  See COPYING.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/fs.h>
151da177e4SLinus Torvalds #include <linux/file.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/signal.h>
181da177e4SLinus Torvalds #include <linux/sched.h>
191da177e4SLinus Torvalds #include <linux/kmod.h>
201da177e4SLinus Torvalds #include <linux/list.h>
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/ctype.h>
231da177e4SLinus Torvalds #include <asm/uaccess.h>
241da177e4SLinus Torvalds #include <linux/poll.h>
251da177e4SLinus Torvalds #include <linux/seq_file.h>
261da177e4SLinus Torvalds #include <linux/proc_fs.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/workqueue.h>
294a3e2f71SArjan van de Ven #include <linux/mutex.h>
30da77005fSTrond Myklebust #include <linux/pagemap.h>
3199df95a2SFrederic Weisbecker #include <linux/smp_lock.h>
321da177e4SLinus Torvalds #include <asm/ioctls.h>
331da177e4SLinus Torvalds #include <linux/sunrpc/types.h>
341da177e4SLinus Torvalds #include <linux/sunrpc/cache.h>
351da177e4SLinus Torvalds #include <linux/sunrpc/stats.h>
368854e82dSTrond Myklebust #include <linux/sunrpc/rpc_pipe_fs.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define	 RPCDBG_FACILITY RPCDBG_CACHE
391da177e4SLinus Torvalds 
40e0bb89efSJ.Bruce Fields static int cache_defer_req(struct cache_req *req, struct cache_head *item);
411da177e4SLinus Torvalds static void cache_revisit_request(struct cache_head *item);
421da177e4SLinus Torvalds 
4374cae61aSAdrian Bunk static void cache_init(struct cache_head *h)
441da177e4SLinus Torvalds {
45c5b29f88SNeilBrown 	time_t now = seconds_since_boot();
461da177e4SLinus Torvalds 	h->next = NULL;
471da177e4SLinus Torvalds 	h->flags = 0;
48baab935fSNeilBrown 	kref_init(&h->ref);
491da177e4SLinus Torvalds 	h->expiry_time = now + CACHE_NEW_EXPIRY;
501da177e4SLinus Torvalds 	h->last_refresh = now;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
532f50d8b6SNeilBrown static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
542f50d8b6SNeilBrown {
55c5b29f88SNeilBrown 	return  (h->expiry_time < seconds_since_boot()) ||
562f50d8b6SNeilBrown 		(detail->flush_time > h->last_refresh);
572f50d8b6SNeilBrown }
582f50d8b6SNeilBrown 
5915a5f6bdSNeilBrown struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
6015a5f6bdSNeilBrown 				       struct cache_head *key, int hash)
6115a5f6bdSNeilBrown {
6215a5f6bdSNeilBrown 	struct cache_head **head,  **hp;
63d202cce8SNeilBrown 	struct cache_head *new = NULL, *freeme = NULL;
6415a5f6bdSNeilBrown 
6515a5f6bdSNeilBrown 	head = &detail->hash_table[hash];
6615a5f6bdSNeilBrown 
6715a5f6bdSNeilBrown 	read_lock(&detail->hash_lock);
6815a5f6bdSNeilBrown 
6915a5f6bdSNeilBrown 	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
7015a5f6bdSNeilBrown 		struct cache_head *tmp = *hp;
7115a5f6bdSNeilBrown 		if (detail->match(tmp, key)) {
72d202cce8SNeilBrown 			if (cache_is_expired(detail, tmp))
73d202cce8SNeilBrown 				/* This entry is expired, we will discard it. */
74d202cce8SNeilBrown 				break;
7515a5f6bdSNeilBrown 			cache_get(tmp);
7615a5f6bdSNeilBrown 			read_unlock(&detail->hash_lock);
7715a5f6bdSNeilBrown 			return tmp;
7815a5f6bdSNeilBrown 		}
7915a5f6bdSNeilBrown 	}
8015a5f6bdSNeilBrown 	read_unlock(&detail->hash_lock);
8115a5f6bdSNeilBrown 	/* Didn't find anything, insert an empty entry */
8215a5f6bdSNeilBrown 
8315a5f6bdSNeilBrown 	new = detail->alloc();
8415a5f6bdSNeilBrown 	if (!new)
8515a5f6bdSNeilBrown 		return NULL;
862f34931fSNeil Brown 	/* must fully initialise 'new', else
872f34931fSNeil Brown 	 * we might get lose if we need to
882f34931fSNeil Brown 	 * cache_put it soon.
892f34931fSNeil Brown 	 */
9015a5f6bdSNeilBrown 	cache_init(new);
912f34931fSNeil Brown 	detail->init(new, key);
9215a5f6bdSNeilBrown 
9315a5f6bdSNeilBrown 	write_lock(&detail->hash_lock);
9415a5f6bdSNeilBrown 
9515a5f6bdSNeilBrown 	/* check if entry appeared while we slept */
9615a5f6bdSNeilBrown 	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
9715a5f6bdSNeilBrown 		struct cache_head *tmp = *hp;
9815a5f6bdSNeilBrown 		if (detail->match(tmp, key)) {
99d202cce8SNeilBrown 			if (cache_is_expired(detail, tmp)) {
100d202cce8SNeilBrown 				*hp = tmp->next;
101d202cce8SNeilBrown 				tmp->next = NULL;
102d202cce8SNeilBrown 				detail->entries --;
103d202cce8SNeilBrown 				freeme = tmp;
104d202cce8SNeilBrown 				break;
105d202cce8SNeilBrown 			}
10615a5f6bdSNeilBrown 			cache_get(tmp);
10715a5f6bdSNeilBrown 			write_unlock(&detail->hash_lock);
108baab935fSNeilBrown 			cache_put(new, detail);
10915a5f6bdSNeilBrown 			return tmp;
11015a5f6bdSNeilBrown 		}
11115a5f6bdSNeilBrown 	}
11215a5f6bdSNeilBrown 	new->next = *head;
11315a5f6bdSNeilBrown 	*head = new;
11415a5f6bdSNeilBrown 	detail->entries++;
11515a5f6bdSNeilBrown 	cache_get(new);
11615a5f6bdSNeilBrown 	write_unlock(&detail->hash_lock);
11715a5f6bdSNeilBrown 
118d202cce8SNeilBrown 	if (freeme)
119d202cce8SNeilBrown 		cache_put(freeme, detail);
12015a5f6bdSNeilBrown 	return new;
12115a5f6bdSNeilBrown }
12224c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
12315a5f6bdSNeilBrown 
124ebd0cb1aSNeilBrown 
125f866a819SNeilBrown static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
126ebd0cb1aSNeilBrown 
127908329f2SNeilBrown static void cache_fresh_locked(struct cache_head *head, time_t expiry)
128ebd0cb1aSNeilBrown {
129ebd0cb1aSNeilBrown 	head->expiry_time = expiry;
130c5b29f88SNeilBrown 	head->last_refresh = seconds_since_boot();
131908329f2SNeilBrown 	set_bit(CACHE_VALID, &head->flags);
132ebd0cb1aSNeilBrown }
133ebd0cb1aSNeilBrown 
134ebd0cb1aSNeilBrown static void cache_fresh_unlocked(struct cache_head *head,
135908329f2SNeilBrown 				 struct cache_detail *detail)
136ebd0cb1aSNeilBrown {
137ebd0cb1aSNeilBrown 	if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
138ebd0cb1aSNeilBrown 		cache_revisit_request(head);
139f866a819SNeilBrown 		cache_dequeue(detail, head);
140ebd0cb1aSNeilBrown 	}
141ebd0cb1aSNeilBrown }
142ebd0cb1aSNeilBrown 
14315a5f6bdSNeilBrown struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
14415a5f6bdSNeilBrown 				       struct cache_head *new, struct cache_head *old, int hash)
14515a5f6bdSNeilBrown {
14615a5f6bdSNeilBrown 	/* The 'old' entry is to be replaced by 'new'.
14715a5f6bdSNeilBrown 	 * If 'old' is not VALID, we update it directly,
14815a5f6bdSNeilBrown 	 * otherwise we need to replace it
14915a5f6bdSNeilBrown 	 */
15015a5f6bdSNeilBrown 	struct cache_head **head;
15115a5f6bdSNeilBrown 	struct cache_head *tmp;
15215a5f6bdSNeilBrown 
15315a5f6bdSNeilBrown 	if (!test_bit(CACHE_VALID, &old->flags)) {
15415a5f6bdSNeilBrown 		write_lock(&detail->hash_lock);
15515a5f6bdSNeilBrown 		if (!test_bit(CACHE_VALID, &old->flags)) {
15615a5f6bdSNeilBrown 			if (test_bit(CACHE_NEGATIVE, &new->flags))
15715a5f6bdSNeilBrown 				set_bit(CACHE_NEGATIVE, &old->flags);
15815a5f6bdSNeilBrown 			else
15915a5f6bdSNeilBrown 				detail->update(old, new);
160908329f2SNeilBrown 			cache_fresh_locked(old, new->expiry_time);
16115a5f6bdSNeilBrown 			write_unlock(&detail->hash_lock);
162908329f2SNeilBrown 			cache_fresh_unlocked(old, detail);
16315a5f6bdSNeilBrown 			return old;
16415a5f6bdSNeilBrown 		}
16515a5f6bdSNeilBrown 		write_unlock(&detail->hash_lock);
16615a5f6bdSNeilBrown 	}
16715a5f6bdSNeilBrown 	/* We need to insert a new entry */
16815a5f6bdSNeilBrown 	tmp = detail->alloc();
16915a5f6bdSNeilBrown 	if (!tmp) {
170baab935fSNeilBrown 		cache_put(old, detail);
17115a5f6bdSNeilBrown 		return NULL;
17215a5f6bdSNeilBrown 	}
17315a5f6bdSNeilBrown 	cache_init(tmp);
17415a5f6bdSNeilBrown 	detail->init(tmp, old);
17515a5f6bdSNeilBrown 	head = &detail->hash_table[hash];
17615a5f6bdSNeilBrown 
17715a5f6bdSNeilBrown 	write_lock(&detail->hash_lock);
17815a5f6bdSNeilBrown 	if (test_bit(CACHE_NEGATIVE, &new->flags))
17915a5f6bdSNeilBrown 		set_bit(CACHE_NEGATIVE, &tmp->flags);
18015a5f6bdSNeilBrown 	else
18115a5f6bdSNeilBrown 		detail->update(tmp, new);
18215a5f6bdSNeilBrown 	tmp->next = *head;
18315a5f6bdSNeilBrown 	*head = tmp;
184f2d39586SNeilBrown 	detail->entries++;
18515a5f6bdSNeilBrown 	cache_get(tmp);
186908329f2SNeilBrown 	cache_fresh_locked(tmp, new->expiry_time);
187ebd0cb1aSNeilBrown 	cache_fresh_locked(old, 0);
18815a5f6bdSNeilBrown 	write_unlock(&detail->hash_lock);
189908329f2SNeilBrown 	cache_fresh_unlocked(tmp, detail);
190908329f2SNeilBrown 	cache_fresh_unlocked(old, detail);
191baab935fSNeilBrown 	cache_put(old, detail);
19215a5f6bdSNeilBrown 	return tmp;
19315a5f6bdSNeilBrown }
19424c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_update);
1951da177e4SLinus Torvalds 
196bc74b4f5STrond Myklebust static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
197bc74b4f5STrond Myklebust {
198bc74b4f5STrond Myklebust 	if (!cd->cache_upcall)
199bc74b4f5STrond Myklebust 		return -EINVAL;
200bc74b4f5STrond Myklebust 	return cd->cache_upcall(cd, h);
201bc74b4f5STrond Myklebust }
202989a19b9SNeilBrown 
203989a19b9SNeilBrown static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
204989a19b9SNeilBrown {
205d202cce8SNeilBrown 	if (!test_bit(CACHE_VALID, &h->flags))
206989a19b9SNeilBrown 		return -EAGAIN;
207989a19b9SNeilBrown 	else {
208989a19b9SNeilBrown 		/* entry is valid */
209989a19b9SNeilBrown 		if (test_bit(CACHE_NEGATIVE, &h->flags))
210989a19b9SNeilBrown 			return -ENOENT;
211989a19b9SNeilBrown 		else
212989a19b9SNeilBrown 			return 0;
213989a19b9SNeilBrown 	}
214989a19b9SNeilBrown }
215e9dc1221SJ. Bruce Fields 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  * This is the generic cache management routine for all
2181da177e4SLinus Torvalds  * the authentication caches.
2191da177e4SLinus Torvalds  * It checks the currency of a cache item and will (later)
2201da177e4SLinus Torvalds  * initiate an upcall to fill it if needed.
2211da177e4SLinus Torvalds  *
2221da177e4SLinus Torvalds  *
2231da177e4SLinus Torvalds  * Returns 0 if the cache_head can be used, or cache_puts it and returns
224989a19b9SNeilBrown  * -EAGAIN if upcall is pending and request has been queued
225989a19b9SNeilBrown  * -ETIMEDOUT if upcall failed or request could not be queue or
226989a19b9SNeilBrown  *           upcall completed but item is still invalid (implying that
227989a19b9SNeilBrown  *           the cache item has been replaced with a newer one).
2281da177e4SLinus Torvalds  * -ENOENT if cache entry was negative
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds int cache_check(struct cache_detail *detail,
2311da177e4SLinus Torvalds 		    struct cache_head *h, struct cache_req *rqstp)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	int rv;
2341da177e4SLinus Torvalds 	long refresh_age, age;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* First decide return status as best we can */
237989a19b9SNeilBrown 	rv = cache_is_valid(detail, h);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* now see if we want to start an upcall */
2401da177e4SLinus Torvalds 	refresh_age = (h->expiry_time - h->last_refresh);
241c5b29f88SNeilBrown 	age = seconds_since_boot() - h->last_refresh;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	if (rqstp == NULL) {
2441da177e4SLinus Torvalds 		if (rv == -EAGAIN)
2451da177e4SLinus Torvalds 			rv = -ENOENT;
2461da177e4SLinus Torvalds 	} else if (rv == -EAGAIN || age > refresh_age/2) {
24746121cf7SChuck Lever 		dprintk("RPC:       Want update, refage=%ld, age=%ld\n",
24846121cf7SChuck Lever 				refresh_age, age);
2491da177e4SLinus Torvalds 		if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
2501da177e4SLinus Torvalds 			switch (cache_make_upcall(detail, h)) {
2511da177e4SLinus Torvalds 			case -EINVAL:
2521da177e4SLinus Torvalds 				clear_bit(CACHE_PENDING, &h->flags);
2535c4d2639SNeilBrown 				cache_revisit_request(h);
2541da177e4SLinus Torvalds 				if (rv == -EAGAIN) {
2551da177e4SLinus Torvalds 					set_bit(CACHE_NEGATIVE, &h->flags);
256c5b29f88SNeilBrown 					cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
257908329f2SNeilBrown 					cache_fresh_unlocked(h, detail);
2581da177e4SLinus Torvalds 					rv = -ENOENT;
2591da177e4SLinus Torvalds 				}
2601da177e4SLinus Torvalds 				break;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 			case -EAGAIN:
2631da177e4SLinus Torvalds 				clear_bit(CACHE_PENDING, &h->flags);
2641da177e4SLinus Torvalds 				cache_revisit_request(h);
2651da177e4SLinus Torvalds 				break;
2661da177e4SLinus Torvalds 			}
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
270989a19b9SNeilBrown 	if (rv == -EAGAIN) {
2719e4c6379SNeilBrown 		if (cache_defer_req(rqstp, h) < 0) {
272989a19b9SNeilBrown 			/* Request is not deferred */
273989a19b9SNeilBrown 			rv = cache_is_valid(detail, h);
2741da177e4SLinus Torvalds 			if (rv == -EAGAIN)
275e0bb89efSJ.Bruce Fields 				rv = -ETIMEDOUT;
276989a19b9SNeilBrown 		}
277989a19b9SNeilBrown 	}
2784013edeaSNeilBrown 	if (rv)
279baab935fSNeilBrown 		cache_put(h, detail);
2801da177e4SLinus Torvalds 	return rv;
2811da177e4SLinus Torvalds }
28224c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_check);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * caches need to be periodically cleaned.
2861da177e4SLinus Torvalds  * For this we maintain a list of cache_detail and
2871da177e4SLinus Torvalds  * a current pointer into that list and into the table
2881da177e4SLinus Torvalds  * for that entry.
2891da177e4SLinus Torvalds  *
2901da177e4SLinus Torvalds  * Each time clean_cache is called it finds the next non-empty entry
2911da177e4SLinus Torvalds  * in the current table and walks the list in that entry
2921da177e4SLinus Torvalds  * looking for entries that can be removed.
2931da177e4SLinus Torvalds  *
2941da177e4SLinus Torvalds  * An entry gets removed if:
2951da177e4SLinus Torvalds  * - The expiry is before current time
2961da177e4SLinus Torvalds  * - The last_refresh time is before the flush_time for that cache
2971da177e4SLinus Torvalds  *
2981da177e4SLinus Torvalds  * later we might drop old entries with non-NEVER expiry if that table
2991da177e4SLinus Torvalds  * is getting 'full' for some definition of 'full'
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  * The question of "how often to scan a table" is an interesting one
3021da177e4SLinus Torvalds  * and is answered in part by the use of the "nextcheck" field in the
3031da177e4SLinus Torvalds  * cache_detail.
3041da177e4SLinus Torvalds  * When a scan of a table begins, the nextcheck field is set to a time
3051da177e4SLinus Torvalds  * that is well into the future.
3061da177e4SLinus Torvalds  * While scanning, if an expiry time is found that is earlier than the
3071da177e4SLinus Torvalds  * current nextcheck time, nextcheck is set to that expiry time.
3081da177e4SLinus Torvalds  * If the flush_time is ever set to a time earlier than the nextcheck
3091da177e4SLinus Torvalds  * time, the nextcheck time is then set to that flush_time.
3101da177e4SLinus Torvalds  *
3111da177e4SLinus Torvalds  * A table is then only scanned if the current time is at least
3121da177e4SLinus Torvalds  * the nextcheck time.
3131da177e4SLinus Torvalds  *
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static LIST_HEAD(cache_list);
3171da177e4SLinus Torvalds static DEFINE_SPINLOCK(cache_list_lock);
3181da177e4SLinus Torvalds static struct cache_detail *current_detail;
3191da177e4SLinus Torvalds static int current_index;
3201da177e4SLinus Torvalds 
32165f27f38SDavid Howells static void do_cache_clean(struct work_struct *work);
3228eab945cSArtem Bityutskiy static struct delayed_work cache_cleaner;
3231da177e4SLinus Torvalds 
3245b7a1b9fSTrond Myklebust static void sunrpc_init_cache_detail(struct cache_detail *cd)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	rwlock_init(&cd->hash_lock);
3271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cd->queue);
3281da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3291da177e4SLinus Torvalds 	cd->nextcheck = 0;
3301da177e4SLinus Torvalds 	cd->entries = 0;
3311da177e4SLinus Torvalds 	atomic_set(&cd->readers, 0);
3321da177e4SLinus Torvalds 	cd->last_close = 0;
3331da177e4SLinus Torvalds 	cd->last_warn = -1;
3341da177e4SLinus Torvalds 	list_add(&cd->others, &cache_list);
3351da177e4SLinus Torvalds 	spin_unlock(&cache_list_lock);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* start the cleaning process */
33852bad64dSDavid Howells 	schedule_delayed_work(&cache_cleaner, 0);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3415b7a1b9fSTrond Myklebust static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	cache_purge(cd);
3441da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3451da177e4SLinus Torvalds 	write_lock(&cd->hash_lock);
3461da177e4SLinus Torvalds 	if (cd->entries || atomic_read(&cd->inuse)) {
3471da177e4SLinus Torvalds 		write_unlock(&cd->hash_lock);
3481da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
349df95a9d4SJ. Bruce Fields 		goto out;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	if (current_detail == cd)
3521da177e4SLinus Torvalds 		current_detail = NULL;
3531da177e4SLinus Torvalds 	list_del_init(&cd->others);
3541da177e4SLinus Torvalds 	write_unlock(&cd->hash_lock);
3551da177e4SLinus Torvalds 	spin_unlock(&cache_list_lock);
3561da177e4SLinus Torvalds 	if (list_empty(&cache_list)) {
3571da177e4SLinus Torvalds 		/* module must be being unloaded so its safe to kill the worker */
3584011cd97STrond Myklebust 		cancel_delayed_work_sync(&cache_cleaner);
3591da177e4SLinus Torvalds 	}
360df95a9d4SJ. Bruce Fields 	return;
361df95a9d4SJ. Bruce Fields out:
362df95a9d4SJ. Bruce Fields 	printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /* clean cache tries to find something to clean
3661da177e4SLinus Torvalds  * and cleans it.
3671da177e4SLinus Torvalds  * It returns 1 if it cleaned something,
3681da177e4SLinus Torvalds  *            0 if it didn't find anything this time
3691da177e4SLinus Torvalds  *           -1 if it fell off the end of the list.
3701da177e4SLinus Torvalds  */
3711da177e4SLinus Torvalds static int cache_clean(void)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	int rv = 0;
3741da177e4SLinus Torvalds 	struct list_head *next;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	/* find a suitable table if we don't already have one */
3791da177e4SLinus Torvalds 	while (current_detail == NULL ||
3801da177e4SLinus Torvalds 	    current_index >= current_detail->hash_size) {
3811da177e4SLinus Torvalds 		if (current_detail)
3821da177e4SLinus Torvalds 			next = current_detail->others.next;
3831da177e4SLinus Torvalds 		else
3841da177e4SLinus Torvalds 			next = cache_list.next;
3851da177e4SLinus Torvalds 		if (next == &cache_list) {
3861da177e4SLinus Torvalds 			current_detail = NULL;
3871da177e4SLinus Torvalds 			spin_unlock(&cache_list_lock);
3881da177e4SLinus Torvalds 			return -1;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		current_detail = list_entry(next, struct cache_detail, others);
391c5b29f88SNeilBrown 		if (current_detail->nextcheck > seconds_since_boot())
3921da177e4SLinus Torvalds 			current_index = current_detail->hash_size;
3931da177e4SLinus Torvalds 		else {
3941da177e4SLinus Torvalds 			current_index = 0;
395c5b29f88SNeilBrown 			current_detail->nextcheck = seconds_since_boot()+30*60;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* find a non-empty bucket in the table */
4001da177e4SLinus Torvalds 	while (current_detail &&
4011da177e4SLinus Torvalds 	       current_index < current_detail->hash_size &&
4021da177e4SLinus Torvalds 	       current_detail->hash_table[current_index] == NULL)
4031da177e4SLinus Torvalds 		current_index++;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/* find a cleanable entry in the bucket and clean it, or set to next bucket */
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (current_detail && current_index < current_detail->hash_size) {
4081da177e4SLinus Torvalds 		struct cache_head *ch, **cp;
4091da177e4SLinus Torvalds 		struct cache_detail *d;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		write_lock(&current_detail->hash_lock);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		/* Ok, now to clean this strand */
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		cp = & current_detail->hash_table[current_index];
4163af4974eSNeilBrown 		for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
4171da177e4SLinus Torvalds 			if (current_detail->nextcheck > ch->expiry_time)
4181da177e4SLinus Torvalds 				current_detail->nextcheck = ch->expiry_time+1;
4192f50d8b6SNeilBrown 			if (!cache_is_expired(current_detail, ch))
4201da177e4SLinus Torvalds 				continue;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 			*cp = ch->next;
4231da177e4SLinus Torvalds 			ch->next = NULL;
4241da177e4SLinus Torvalds 			current_detail->entries--;
4251da177e4SLinus Torvalds 			rv = 1;
4263af4974eSNeilBrown 			break;
4271da177e4SLinus Torvalds 		}
4283af4974eSNeilBrown 
4291da177e4SLinus Torvalds 		write_unlock(&current_detail->hash_lock);
4301da177e4SLinus Torvalds 		d = current_detail;
4311da177e4SLinus Torvalds 		if (!ch)
4321da177e4SLinus Torvalds 			current_index ++;
4331da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
4345c4d2639SNeilBrown 		if (ch) {
4353af4974eSNeilBrown 			if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
4363af4974eSNeilBrown 				cache_dequeue(current_detail, ch);
4375c4d2639SNeilBrown 			cache_revisit_request(ch);
438baab935fSNeilBrown 			cache_put(ch, d);
4395c4d2639SNeilBrown 		}
4401da177e4SLinus Torvalds 	} else
4411da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	return rv;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds  * We want to regularly clean the cache, so we need to schedule some work ...
4481da177e4SLinus Torvalds  */
44965f27f38SDavid Howells static void do_cache_clean(struct work_struct *work)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	int delay = 5;
4521da177e4SLinus Torvalds 	if (cache_clean() == -1)
4536aad89c8SAnton Blanchard 		delay = round_jiffies_relative(30*HZ);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (list_empty(&cache_list))
4561da177e4SLinus Torvalds 		delay = 0;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	if (delay)
4591da177e4SLinus Torvalds 		schedule_delayed_work(&cache_cleaner, delay);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4641da177e4SLinus Torvalds  * Clean all caches promptly.  This just calls cache_clean
4651da177e4SLinus Torvalds  * repeatedly until we are sure that every cache has had a chance to
4661da177e4SLinus Torvalds  * be fully cleaned
4671da177e4SLinus Torvalds  */
4681da177e4SLinus Torvalds void cache_flush(void)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	while (cache_clean() != -1)
4711da177e4SLinus Torvalds 		cond_resched();
4721da177e4SLinus Torvalds 	while (cache_clean() != -1)
4731da177e4SLinus Torvalds 		cond_resched();
4741da177e4SLinus Torvalds }
47524c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_flush);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds void cache_purge(struct cache_detail *detail)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	detail->flush_time = LONG_MAX;
480c5b29f88SNeilBrown 	detail->nextcheck = seconds_since_boot();
4811da177e4SLinus Torvalds 	cache_flush();
4821da177e4SLinus Torvalds 	detail->flush_time = 1;
4831da177e4SLinus Torvalds }
48424c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_purge);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
4881da177e4SLinus Torvalds  * Deferral and Revisiting of Requests.
4891da177e4SLinus Torvalds  *
4901da177e4SLinus Torvalds  * If a cache lookup finds a pending entry, we
4911da177e4SLinus Torvalds  * need to defer the request and revisit it later.
4921da177e4SLinus Torvalds  * All deferred requests are stored in a hash table,
4931da177e4SLinus Torvalds  * indexed by "struct cache_head *".
4941da177e4SLinus Torvalds  * As it may be wasteful to store a whole request
4951da177e4SLinus Torvalds  * structure, we allow the request to provide a
4961da177e4SLinus Torvalds  * deferred form, which must contain a
4971da177e4SLinus Torvalds  * 'struct cache_deferred_req'
4981da177e4SLinus Torvalds  * This cache_deferred_req contains a method to allow
4991da177e4SLinus Torvalds  * it to be revisited when cache info is available
5001da177e4SLinus Torvalds  */
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds #define	DFR_HASHSIZE	(PAGE_SIZE/sizeof(struct list_head))
5031da177e4SLinus Torvalds #define	DFR_HASH(item)	((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds #define	DFR_MAX	300	/* ??? */
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds static DEFINE_SPINLOCK(cache_defer_lock);
5081da177e4SLinus Torvalds static LIST_HEAD(cache_defer_list);
509*11174492SNeilBrown static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
5101da177e4SLinus Torvalds static int cache_defer_cnt;
5111da177e4SLinus Torvalds 
5126610f720SJ. Bruce Fields static void __unhash_deferred_req(struct cache_deferred_req *dreq)
5136610f720SJ. Bruce Fields {
5146610f720SJ. Bruce Fields 	list_del_init(&dreq->recent);
515*11174492SNeilBrown 	hlist_del_init(&dreq->hash);
5166610f720SJ. Bruce Fields 	cache_defer_cnt--;
5176610f720SJ. Bruce Fields }
5186610f720SJ. Bruce Fields 
5196610f720SJ. Bruce Fields static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
5206610f720SJ. Bruce Fields {
5216610f720SJ. Bruce Fields 	int hash = DFR_HASH(item);
5226610f720SJ. Bruce Fields 
5236610f720SJ. Bruce Fields 	list_add(&dreq->recent, &cache_defer_list);
524*11174492SNeilBrown 	hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
5256610f720SJ. Bruce Fields }
5266610f720SJ. Bruce Fields 
5273211af11SJ. Bruce Fields static int setup_deferral(struct cache_deferred_req *dreq, struct cache_head *item)
5281da177e4SLinus Torvalds {
5293211af11SJ. Bruce Fields 	struct cache_deferred_req *discard;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	dreq->item = item;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
5341da177e4SLinus Torvalds 
5356610f720SJ. Bruce Fields 	__hash_deferred_req(dreq, item);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/* it is in, now maybe clean up */
538cd68c374SNeilBrown 	discard = NULL;
5391da177e4SLinus Torvalds 	if (++cache_defer_cnt > DFR_MAX) {
540cd68c374SNeilBrown 		discard = list_entry(cache_defer_list.prev,
54101f3bd1fSJ.Bruce Fields 				     struct cache_deferred_req, recent);
5426610f720SJ. Bruce Fields 		__unhash_deferred_req(discard);
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
5451da177e4SLinus Torvalds 
546cd68c374SNeilBrown 	if (discard)
5471da177e4SLinus Torvalds 		/* there was one too many */
548cd68c374SNeilBrown 		discard->revisit(discard, 1);
549cd68c374SNeilBrown 
5504013edeaSNeilBrown 	if (!test_bit(CACHE_PENDING, &item->flags)) {
5511da177e4SLinus Torvalds 		/* must have just been validated... */
5521da177e4SLinus Torvalds 		cache_revisit_request(item);
5539e4c6379SNeilBrown 		return -EAGAIN;
5541da177e4SLinus Torvalds 	}
5553211af11SJ. Bruce Fields 	return 0;
5563211af11SJ. Bruce Fields }
557f16b6e8dSNeilBrown 
5583211af11SJ. Bruce Fields struct thread_deferred_req {
5593211af11SJ. Bruce Fields 	struct cache_deferred_req handle;
5603211af11SJ. Bruce Fields 	struct completion completion;
5613211af11SJ. Bruce Fields };
5623211af11SJ. Bruce Fields 
5633211af11SJ. Bruce Fields static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
5643211af11SJ. Bruce Fields {
5653211af11SJ. Bruce Fields 	struct thread_deferred_req *dr =
5663211af11SJ. Bruce Fields 		container_of(dreq, struct thread_deferred_req, handle);
5673211af11SJ. Bruce Fields 	complete(&dr->completion);
5683211af11SJ. Bruce Fields }
5693211af11SJ. Bruce Fields 
5703211af11SJ. Bruce Fields static int cache_wait_req(struct cache_req *req, struct cache_head *item)
5713211af11SJ. Bruce Fields {
5723211af11SJ. Bruce Fields 	struct thread_deferred_req sleeper;
5733211af11SJ. Bruce Fields 	struct cache_deferred_req *dreq = &sleeper.handle;
5743211af11SJ. Bruce Fields 	int ret;
5753211af11SJ. Bruce Fields 
5763211af11SJ. Bruce Fields 	sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
5773211af11SJ. Bruce Fields 	dreq->revisit = cache_restart_thread;
5783211af11SJ. Bruce Fields 
5793211af11SJ. Bruce Fields 	ret = setup_deferral(dreq, item);
5803211af11SJ. Bruce Fields 	if (ret)
5813211af11SJ. Bruce Fields 		return ret;
5823211af11SJ. Bruce Fields 
583f16b6e8dSNeilBrown 	if (wait_for_completion_interruptible_timeout(
584f16b6e8dSNeilBrown 		    &sleeper.completion, req->thread_wait) <= 0) {
585f16b6e8dSNeilBrown 		/* The completion wasn't completed, so we need
586f16b6e8dSNeilBrown 		 * to clean up
587f16b6e8dSNeilBrown 		 */
588f16b6e8dSNeilBrown 		spin_lock(&cache_defer_lock);
589*11174492SNeilBrown 		if (!hlist_unhashed(&sleeper.handle.hash)) {
5906610f720SJ. Bruce Fields 			__unhash_deferred_req(&sleeper.handle);
591f16b6e8dSNeilBrown 			spin_unlock(&cache_defer_lock);
592f16b6e8dSNeilBrown 		} else {
593f16b6e8dSNeilBrown 			/* cache_revisit_request already removed
594f16b6e8dSNeilBrown 			 * this from the hash table, but hasn't
595f16b6e8dSNeilBrown 			 * called ->revisit yet.  It will very soon
596f16b6e8dSNeilBrown 			 * and we need to wait for it.
597f16b6e8dSNeilBrown 			 */
598f16b6e8dSNeilBrown 			spin_unlock(&cache_defer_lock);
599f16b6e8dSNeilBrown 			wait_for_completion(&sleeper.completion);
600f16b6e8dSNeilBrown 		}
601f16b6e8dSNeilBrown 	}
602f16b6e8dSNeilBrown 	if (test_bit(CACHE_PENDING, &item->flags)) {
603f16b6e8dSNeilBrown 		/* item is still pending, try request
604f16b6e8dSNeilBrown 		 * deferral
605f16b6e8dSNeilBrown 		 */
6063211af11SJ. Bruce Fields 		return -ETIMEDOUT;
607f16b6e8dSNeilBrown 	}
608f16b6e8dSNeilBrown 	/* only return success if we actually deferred the
609f16b6e8dSNeilBrown 	 * request.  In this case we waited until it was
610f16b6e8dSNeilBrown 	 * answered so no deferral has happened - rather
611f16b6e8dSNeilBrown 	 * an answer already exists.
612f16b6e8dSNeilBrown 	 */
613f16b6e8dSNeilBrown 	return -EEXIST;
614f16b6e8dSNeilBrown }
6153211af11SJ. Bruce Fields 
6163211af11SJ. Bruce Fields static int cache_defer_req(struct cache_req *req, struct cache_head *item)
6173211af11SJ. Bruce Fields {
6183211af11SJ. Bruce Fields 	struct cache_deferred_req *dreq;
6193211af11SJ. Bruce Fields 	int ret;
6203211af11SJ. Bruce Fields 
6213211af11SJ. Bruce Fields 	if (cache_defer_cnt >= DFR_MAX) {
6223211af11SJ. Bruce Fields 		/* too much in the cache, randomly drop this one,
6233211af11SJ. Bruce Fields 		 * or continue and drop the oldest
6243211af11SJ. Bruce Fields 		 */
6253211af11SJ. Bruce Fields 		if (net_random()&1)
6263211af11SJ. Bruce Fields 			return -ENOMEM;
6273211af11SJ. Bruce Fields 	}
6283211af11SJ. Bruce Fields 	if (req->thread_wait) {
6293211af11SJ. Bruce Fields 		ret = cache_wait_req(req, item);
6303211af11SJ. Bruce Fields 		if (ret != -ETIMEDOUT)
6313211af11SJ. Bruce Fields 			return ret;
6323211af11SJ. Bruce Fields 	}
6333211af11SJ. Bruce Fields 	dreq = req->defer(req);
6343211af11SJ. Bruce Fields 	if (dreq == NULL)
6353211af11SJ. Bruce Fields 		return -ENOMEM;
6363211af11SJ. Bruce Fields 	return setup_deferral(dreq, item);
637989a19b9SNeilBrown }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static void cache_revisit_request(struct cache_head *item)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct cache_deferred_req *dreq;
6421da177e4SLinus Torvalds 	struct list_head pending;
643*11174492SNeilBrown 	struct hlist_node *lp, *tmp;
6441da177e4SLinus Torvalds 	int hash = DFR_HASH(item);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	INIT_LIST_HEAD(&pending);
6471da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
6481da177e4SLinus Torvalds 
649*11174492SNeilBrown 	hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
6501da177e4SLinus Torvalds 		if (dreq->item == item) {
6516610f720SJ. Bruce Fields 			__unhash_deferred_req(dreq);
6526610f720SJ. Bruce Fields 			list_add(&dreq->recent, &pending);
6531da177e4SLinus Torvalds 		}
654*11174492SNeilBrown 
6551da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	while (!list_empty(&pending)) {
6581da177e4SLinus Torvalds 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
6591da177e4SLinus Torvalds 		list_del_init(&dreq->recent);
6601da177e4SLinus Torvalds 		dreq->revisit(dreq, 0);
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds void cache_clean_deferred(void *owner)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	struct cache_deferred_req *dreq, *tmp;
6671da177e4SLinus Torvalds 	struct list_head pending;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	INIT_LIST_HEAD(&pending);
6711da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
6746610f720SJ. Bruce Fields 		if (dreq->owner == owner)
6756610f720SJ. Bruce Fields 			__unhash_deferred_req(dreq);
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	while (!list_empty(&pending)) {
6801da177e4SLinus Torvalds 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
6811da177e4SLinus Torvalds 		list_del_init(&dreq->recent);
6821da177e4SLinus Torvalds 		dreq->revisit(dreq, 1);
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds /*
6871da177e4SLinus Torvalds  * communicate with user-space
6881da177e4SLinus Torvalds  *
689a490c681SJ. Bruce Fields  * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
690a490c681SJ. Bruce Fields  * On read, you get a full request, or block.
691a490c681SJ. Bruce Fields  * On write, an update request is processed.
692a490c681SJ. Bruce Fields  * Poll works if anything to read, and always allows write.
6931da177e4SLinus Torvalds  *
6941da177e4SLinus Torvalds  * Implemented by linked list of requests.  Each open file has
695a490c681SJ. Bruce Fields  * a ->private that also exists in this list.  New requests are added
6961da177e4SLinus Torvalds  * to the end and may wakeup and preceding readers.
6971da177e4SLinus Torvalds  * New readers are added to the head.  If, on read, an item is found with
6981da177e4SLinus Torvalds  * CACHE_UPCALLING clear, we free it from the list.
6991da177e4SLinus Torvalds  *
7001da177e4SLinus Torvalds  */
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds static DEFINE_SPINLOCK(queue_lock);
7034a3e2f71SArjan van de Ven static DEFINE_MUTEX(queue_io_mutex);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds struct cache_queue {
7061da177e4SLinus Torvalds 	struct list_head	list;
7071da177e4SLinus Torvalds 	int			reader;	/* if 0, then request */
7081da177e4SLinus Torvalds };
7091da177e4SLinus Torvalds struct cache_request {
7101da177e4SLinus Torvalds 	struct cache_queue	q;
7111da177e4SLinus Torvalds 	struct cache_head	*item;
7121da177e4SLinus Torvalds 	char			* buf;
7131da177e4SLinus Torvalds 	int			len;
7141da177e4SLinus Torvalds 	int			readers;
7151da177e4SLinus Torvalds };
7161da177e4SLinus Torvalds struct cache_reader {
7171da177e4SLinus Torvalds 	struct cache_queue	q;
7181da177e4SLinus Torvalds 	int			offset;	/* if non-0, we have a refcnt on next request */
7191da177e4SLinus Torvalds };
7201da177e4SLinus Torvalds 
721173912a6STrond Myklebust static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
722173912a6STrond Myklebust 			  loff_t *ppos, struct cache_detail *cd)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
7251da177e4SLinus Torvalds 	struct cache_request *rq;
726da77005fSTrond Myklebust 	struct inode *inode = filp->f_path.dentry->d_inode;
7271da177e4SLinus Torvalds 	int err;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	if (count == 0)
7301da177e4SLinus Torvalds 		return 0;
7311da177e4SLinus Torvalds 
732da77005fSTrond Myklebust 	mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
7331da177e4SLinus Torvalds 			      * readers on this file */
7341da177e4SLinus Torvalds  again:
7351da177e4SLinus Torvalds 	spin_lock(&queue_lock);
7361da177e4SLinus Torvalds 	/* need to find next request */
7371da177e4SLinus Torvalds 	while (rp->q.list.next != &cd->queue &&
7381da177e4SLinus Torvalds 	       list_entry(rp->q.list.next, struct cache_queue, list)
7391da177e4SLinus Torvalds 	       ->reader) {
7401da177e4SLinus Torvalds 		struct list_head *next = rp->q.list.next;
7411da177e4SLinus Torvalds 		list_move(&rp->q.list, next);
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 	if (rp->q.list.next == &cd->queue) {
7441da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
745da77005fSTrond Myklebust 		mutex_unlock(&inode->i_mutex);
74609a62660SKris Katterjohn 		BUG_ON(rp->offset);
7471da177e4SLinus Torvalds 		return 0;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	rq = container_of(rp->q.list.next, struct cache_request, q.list);
75009a62660SKris Katterjohn 	BUG_ON(rq->q.reader);
7511da177e4SLinus Torvalds 	if (rp->offset == 0)
7521da177e4SLinus Torvalds 		rq->readers++;
7531da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
7561da177e4SLinus Torvalds 		err = -EAGAIN;
7571da177e4SLinus Torvalds 		spin_lock(&queue_lock);
7581da177e4SLinus Torvalds 		list_move(&rp->q.list, &rq->q.list);
7591da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
7601da177e4SLinus Torvalds 	} else {
7611da177e4SLinus Torvalds 		if (rp->offset + count > rq->len)
7621da177e4SLinus Torvalds 			count = rq->len - rp->offset;
7631da177e4SLinus Torvalds 		err = -EFAULT;
7641da177e4SLinus Torvalds 		if (copy_to_user(buf, rq->buf + rp->offset, count))
7651da177e4SLinus Torvalds 			goto out;
7661da177e4SLinus Torvalds 		rp->offset += count;
7671da177e4SLinus Torvalds 		if (rp->offset >= rq->len) {
7681da177e4SLinus Torvalds 			rp->offset = 0;
7691da177e4SLinus Torvalds 			spin_lock(&queue_lock);
7701da177e4SLinus Torvalds 			list_move(&rp->q.list, &rq->q.list);
7711da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
7721da177e4SLinus Torvalds 		}
7731da177e4SLinus Torvalds 		err = 0;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds  out:
7761da177e4SLinus Torvalds 	if (rp->offset == 0) {
7771da177e4SLinus Torvalds 		/* need to release rq */
7781da177e4SLinus Torvalds 		spin_lock(&queue_lock);
7791da177e4SLinus Torvalds 		rq->readers--;
7801da177e4SLinus Torvalds 		if (rq->readers == 0 &&
7811da177e4SLinus Torvalds 		    !test_bit(CACHE_PENDING, &rq->item->flags)) {
7821da177e4SLinus Torvalds 			list_del(&rq->q.list);
7831da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
784baab935fSNeilBrown 			cache_put(rq->item, cd);
7851da177e4SLinus Torvalds 			kfree(rq->buf);
7861da177e4SLinus Torvalds 			kfree(rq);
7871da177e4SLinus Torvalds 		} else
7881da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 	if (err == -EAGAIN)
7911da177e4SLinus Torvalds 		goto again;
792da77005fSTrond Myklebust 	mutex_unlock(&inode->i_mutex);
7931da177e4SLinus Torvalds 	return err ? err :  count;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
796da77005fSTrond Myklebust static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
797da77005fSTrond Myklebust 				 size_t count, struct cache_detail *cd)
7981da177e4SLinus Torvalds {
799da77005fSTrond Myklebust 	ssize_t ret;
8001da177e4SLinus Torvalds 
801da77005fSTrond Myklebust 	if (copy_from_user(kaddr, buf, count))
8021da177e4SLinus Torvalds 		return -EFAULT;
803da77005fSTrond Myklebust 	kaddr[count] = '\0';
804da77005fSTrond Myklebust 	ret = cd->cache_parse(cd, kaddr, count);
805da77005fSTrond Myklebust 	if (!ret)
806da77005fSTrond Myklebust 		ret = count;
807da77005fSTrond Myklebust 	return ret;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
810da77005fSTrond Myklebust static ssize_t cache_slow_downcall(const char __user *buf,
811da77005fSTrond Myklebust 				   size_t count, struct cache_detail *cd)
812da77005fSTrond Myklebust {
8131da177e4SLinus Torvalds 	static char write_buf[8192]; /* protected by queue_io_mutex */
814da77005fSTrond Myklebust 	ssize_t ret = -EINVAL;
815da77005fSTrond Myklebust 
816da77005fSTrond Myklebust 	if (count >= sizeof(write_buf))
817da77005fSTrond Myklebust 		goto out;
818da77005fSTrond Myklebust 	mutex_lock(&queue_io_mutex);
819da77005fSTrond Myklebust 	ret = cache_do_downcall(write_buf, buf, count, cd);
8204a3e2f71SArjan van de Ven 	mutex_unlock(&queue_io_mutex);
821da77005fSTrond Myklebust out:
822da77005fSTrond Myklebust 	return ret;
823da77005fSTrond Myklebust }
824da77005fSTrond Myklebust 
825da77005fSTrond Myklebust static ssize_t cache_downcall(struct address_space *mapping,
826da77005fSTrond Myklebust 			      const char __user *buf,
827da77005fSTrond Myklebust 			      size_t count, struct cache_detail *cd)
828da77005fSTrond Myklebust {
829da77005fSTrond Myklebust 	struct page *page;
830da77005fSTrond Myklebust 	char *kaddr;
831da77005fSTrond Myklebust 	ssize_t ret = -ENOMEM;
832da77005fSTrond Myklebust 
833da77005fSTrond Myklebust 	if (count >= PAGE_CACHE_SIZE)
834da77005fSTrond Myklebust 		goto out_slow;
835da77005fSTrond Myklebust 
836da77005fSTrond Myklebust 	page = find_or_create_page(mapping, 0, GFP_KERNEL);
837da77005fSTrond Myklebust 	if (!page)
838da77005fSTrond Myklebust 		goto out_slow;
839da77005fSTrond Myklebust 
840da77005fSTrond Myklebust 	kaddr = kmap(page);
841da77005fSTrond Myklebust 	ret = cache_do_downcall(kaddr, buf, count, cd);
842da77005fSTrond Myklebust 	kunmap(page);
843da77005fSTrond Myklebust 	unlock_page(page);
844da77005fSTrond Myklebust 	page_cache_release(page);
845da77005fSTrond Myklebust 	return ret;
846da77005fSTrond Myklebust out_slow:
847da77005fSTrond Myklebust 	return cache_slow_downcall(buf, count, cd);
848da77005fSTrond Myklebust }
8491da177e4SLinus Torvalds 
850173912a6STrond Myklebust static ssize_t cache_write(struct file *filp, const char __user *buf,
851173912a6STrond Myklebust 			   size_t count, loff_t *ppos,
852173912a6STrond Myklebust 			   struct cache_detail *cd)
8531da177e4SLinus Torvalds {
854da77005fSTrond Myklebust 	struct address_space *mapping = filp->f_mapping;
855da77005fSTrond Myklebust 	struct inode *inode = filp->f_path.dentry->d_inode;
856da77005fSTrond Myklebust 	ssize_t ret = -EINVAL;
8571da177e4SLinus Torvalds 
858da77005fSTrond Myklebust 	if (!cd->cache_parse)
859da77005fSTrond Myklebust 		goto out;
8601da177e4SLinus Torvalds 
861da77005fSTrond Myklebust 	mutex_lock(&inode->i_mutex);
862da77005fSTrond Myklebust 	ret = cache_downcall(mapping, buf, count, cd);
863da77005fSTrond Myklebust 	mutex_unlock(&inode->i_mutex);
864da77005fSTrond Myklebust out:
865da77005fSTrond Myklebust 	return ret;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
8691da177e4SLinus Torvalds 
870173912a6STrond Myklebust static unsigned int cache_poll(struct file *filp, poll_table *wait,
871173912a6STrond Myklebust 			       struct cache_detail *cd)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	unsigned int mask;
8741da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
8751da177e4SLinus Torvalds 	struct cache_queue *cq;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	poll_wait(filp, &queue_wait, wait);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/* alway allow write */
8801da177e4SLinus Torvalds 	mask = POLL_OUT | POLLWRNORM;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (!rp)
8831da177e4SLinus Torvalds 		return mask;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	spin_lock(&queue_lock);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	for (cq= &rp->q; &cq->list != &cd->queue;
8881da177e4SLinus Torvalds 	     cq = list_entry(cq->list.next, struct cache_queue, list))
8891da177e4SLinus Torvalds 		if (!cq->reader) {
8901da177e4SLinus Torvalds 			mask |= POLLIN | POLLRDNORM;
8911da177e4SLinus Torvalds 			break;
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
8941da177e4SLinus Torvalds 	return mask;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
897173912a6STrond Myklebust static int cache_ioctl(struct inode *ino, struct file *filp,
898173912a6STrond Myklebust 		       unsigned int cmd, unsigned long arg,
899173912a6STrond Myklebust 		       struct cache_detail *cd)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	int len = 0;
9021da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
9031da177e4SLinus Torvalds 	struct cache_queue *cq;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (cmd != FIONREAD || !rp)
9061da177e4SLinus Torvalds 		return -EINVAL;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	spin_lock(&queue_lock);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	/* only find the length remaining in current request,
9111da177e4SLinus Torvalds 	 * or the length of the next request
9121da177e4SLinus Torvalds 	 */
9131da177e4SLinus Torvalds 	for (cq= &rp->q; &cq->list != &cd->queue;
9141da177e4SLinus Torvalds 	     cq = list_entry(cq->list.next, struct cache_queue, list))
9151da177e4SLinus Torvalds 		if (!cq->reader) {
9161da177e4SLinus Torvalds 			struct cache_request *cr =
9171da177e4SLinus Torvalds 				container_of(cq, struct cache_request, q);
9181da177e4SLinus Torvalds 			len = cr->len - rp->offset;
9191da177e4SLinus Torvalds 			break;
9201da177e4SLinus Torvalds 		}
9211da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	return put_user(len, (int __user *)arg);
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
926173912a6STrond Myklebust static int cache_open(struct inode *inode, struct file *filp,
927173912a6STrond Myklebust 		      struct cache_detail *cd)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	struct cache_reader *rp = NULL;
9301da177e4SLinus Torvalds 
931f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
932f7e86ab9STrond Myklebust 		return -EACCES;
9331da177e4SLinus Torvalds 	nonseekable_open(inode, filp);
9341da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_READ) {
9351da177e4SLinus Torvalds 		rp = kmalloc(sizeof(*rp), GFP_KERNEL);
9361da177e4SLinus Torvalds 		if (!rp)
9371da177e4SLinus Torvalds 			return -ENOMEM;
9381da177e4SLinus Torvalds 		rp->offset = 0;
9391da177e4SLinus Torvalds 		rp->q.reader = 1;
9401da177e4SLinus Torvalds 		atomic_inc(&cd->readers);
9411da177e4SLinus Torvalds 		spin_lock(&queue_lock);
9421da177e4SLinus Torvalds 		list_add(&rp->q.list, &cd->queue);
9431da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 	filp->private_data = rp;
9461da177e4SLinus Torvalds 	return 0;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
949173912a6STrond Myklebust static int cache_release(struct inode *inode, struct file *filp,
950173912a6STrond Myklebust 			 struct cache_detail *cd)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	if (rp) {
9551da177e4SLinus Torvalds 		spin_lock(&queue_lock);
9561da177e4SLinus Torvalds 		if (rp->offset) {
9571da177e4SLinus Torvalds 			struct cache_queue *cq;
9581da177e4SLinus Torvalds 			for (cq= &rp->q; &cq->list != &cd->queue;
9591da177e4SLinus Torvalds 			     cq = list_entry(cq->list.next, struct cache_queue, list))
9601da177e4SLinus Torvalds 				if (!cq->reader) {
9611da177e4SLinus Torvalds 					container_of(cq, struct cache_request, q)
9621da177e4SLinus Torvalds 						->readers--;
9631da177e4SLinus Torvalds 					break;
9641da177e4SLinus Torvalds 				}
9651da177e4SLinus Torvalds 			rp->offset = 0;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		list_del(&rp->q.list);
9681da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		filp->private_data = NULL;
9711da177e4SLinus Torvalds 		kfree(rp);
9721da177e4SLinus Torvalds 
973c5b29f88SNeilBrown 		cd->last_close = seconds_since_boot();
9741da177e4SLinus Torvalds 		atomic_dec(&cd->readers);
9751da177e4SLinus Torvalds 	}
976f7e86ab9STrond Myklebust 	module_put(cd->owner);
9771da177e4SLinus Torvalds 	return 0;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 
982f866a819SNeilBrown static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	struct cache_queue *cq;
9851da177e4SLinus Torvalds 	spin_lock(&queue_lock);
9861da177e4SLinus Torvalds 	list_for_each_entry(cq, &detail->queue, list)
9871da177e4SLinus Torvalds 		if (!cq->reader) {
9881da177e4SLinus Torvalds 			struct cache_request *cr = container_of(cq, struct cache_request, q);
9891da177e4SLinus Torvalds 			if (cr->item != ch)
9901da177e4SLinus Torvalds 				continue;
9911da177e4SLinus Torvalds 			if (cr->readers != 0)
9924013edeaSNeilBrown 				continue;
9931da177e4SLinus Torvalds 			list_del(&cr->q.list);
9941da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
995baab935fSNeilBrown 			cache_put(cr->item, detail);
9961da177e4SLinus Torvalds 			kfree(cr->buf);
9971da177e4SLinus Torvalds 			kfree(cr);
9981da177e4SLinus Torvalds 			return;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds /*
10041da177e4SLinus Torvalds  * Support routines for text-based upcalls.
10051da177e4SLinus Torvalds  * Fields are separated by spaces.
10061da177e4SLinus Torvalds  * Fields are either mangled to quote space tab newline slosh with slosh
10071da177e4SLinus Torvalds  * or a hexified with a leading \x
10081da177e4SLinus Torvalds  * Record is terminated with newline.
10091da177e4SLinus Torvalds  *
10101da177e4SLinus Torvalds  */
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds void qword_add(char **bpp, int *lp, char *str)
10131da177e4SLinus Torvalds {
10141da177e4SLinus Torvalds 	char *bp = *bpp;
10151da177e4SLinus Torvalds 	int len = *lp;
10161da177e4SLinus Torvalds 	char c;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	if (len < 0) return;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	while ((c=*str++) && len)
10211da177e4SLinus Torvalds 		switch(c) {
10221da177e4SLinus Torvalds 		case ' ':
10231da177e4SLinus Torvalds 		case '\t':
10241da177e4SLinus Torvalds 		case '\n':
10251da177e4SLinus Torvalds 		case '\\':
10261da177e4SLinus Torvalds 			if (len >= 4) {
10271da177e4SLinus Torvalds 				*bp++ = '\\';
10281da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0300)>>6);
10291da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0070)>>3);
10301da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0007)>>0);
10311da177e4SLinus Torvalds 			}
10321da177e4SLinus Torvalds 			len -= 4;
10331da177e4SLinus Torvalds 			break;
10341da177e4SLinus Torvalds 		default:
10351da177e4SLinus Torvalds 			*bp++ = c;
10361da177e4SLinus Torvalds 			len--;
10371da177e4SLinus Torvalds 		}
10381da177e4SLinus Torvalds 	if (c || len <1) len = -1;
10391da177e4SLinus Torvalds 	else {
10401da177e4SLinus Torvalds 		*bp++ = ' ';
10411da177e4SLinus Torvalds 		len--;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 	*bpp = bp;
10441da177e4SLinus Torvalds 	*lp = len;
10451da177e4SLinus Torvalds }
104624c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_add);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds void qword_addhex(char **bpp, int *lp, char *buf, int blen)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	char *bp = *bpp;
10511da177e4SLinus Torvalds 	int len = *lp;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	if (len < 0) return;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (len > 2) {
10561da177e4SLinus Torvalds 		*bp++ = '\\';
10571da177e4SLinus Torvalds 		*bp++ = 'x';
10581da177e4SLinus Torvalds 		len -= 2;
10591da177e4SLinus Torvalds 		while (blen && len >= 2) {
10601da177e4SLinus Torvalds 			unsigned char c = *buf++;
10611da177e4SLinus Torvalds 			*bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
10621da177e4SLinus Torvalds 			*bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
10631da177e4SLinus Torvalds 			len -= 2;
10641da177e4SLinus Torvalds 			blen--;
10651da177e4SLinus Torvalds 		}
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 	if (blen || len<1) len = -1;
10681da177e4SLinus Torvalds 	else {
10691da177e4SLinus Torvalds 		*bp++ = ' ';
10701da177e4SLinus Torvalds 		len--;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 	*bpp = bp;
10731da177e4SLinus Torvalds 	*lp = len;
10741da177e4SLinus Torvalds }
107524c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_addhex);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static void warn_no_listener(struct cache_detail *detail)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	if (detail->last_warn != detail->last_close) {
10801da177e4SLinus Torvalds 		detail->last_warn = detail->last_close;
10811da177e4SLinus Torvalds 		if (detail->warn_no_listener)
10822da8ca26STrond Myklebust 			detail->warn_no_listener(detail, detail->last_close != 0);
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds 
108606497524SJ. Bruce Fields static bool cache_listeners_exist(struct cache_detail *detail)
108706497524SJ. Bruce Fields {
108806497524SJ. Bruce Fields 	if (atomic_read(&detail->readers))
108906497524SJ. Bruce Fields 		return true;
109006497524SJ. Bruce Fields 	if (detail->last_close == 0)
109106497524SJ. Bruce Fields 		/* This cache was never opened */
109206497524SJ. Bruce Fields 		return false;
109306497524SJ. Bruce Fields 	if (detail->last_close < seconds_since_boot() - 30)
109406497524SJ. Bruce Fields 		/*
109506497524SJ. Bruce Fields 		 * We allow for the possibility that someone might
109606497524SJ. Bruce Fields 		 * restart a userspace daemon without restarting the
109706497524SJ. Bruce Fields 		 * server; but after 30 seconds, we give up.
109806497524SJ. Bruce Fields 		 */
109906497524SJ. Bruce Fields 		 return false;
110006497524SJ. Bruce Fields 	return true;
110106497524SJ. Bruce Fields }
110206497524SJ. Bruce Fields 
11031da177e4SLinus Torvalds /*
1104bc74b4f5STrond Myklebust  * register an upcall request to user-space and queue it up for read() by the
1105bc74b4f5STrond Myklebust  * upcall daemon.
1106bc74b4f5STrond Myklebust  *
11071da177e4SLinus Torvalds  * Each request is at most one page long.
11081da177e4SLinus Torvalds  */
1109bc74b4f5STrond Myklebust int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1110bc74b4f5STrond Myklebust 		void (*cache_request)(struct cache_detail *,
1111bc74b4f5STrond Myklebust 				      struct cache_head *,
1112bc74b4f5STrond Myklebust 				      char **,
1113bc74b4f5STrond Myklebust 				      int *))
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	char *buf;
11171da177e4SLinus Torvalds 	struct cache_request *crq;
11181da177e4SLinus Torvalds 	char *bp;
11191da177e4SLinus Torvalds 	int len;
11201da177e4SLinus Torvalds 
112106497524SJ. Bruce Fields 	if (!cache_listeners_exist(detail)) {
11221da177e4SLinus Torvalds 		warn_no_listener(detail);
11231da177e4SLinus Torvalds 		return -EINVAL;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
11271da177e4SLinus Torvalds 	if (!buf)
11281da177e4SLinus Torvalds 		return -EAGAIN;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	crq = kmalloc(sizeof (*crq), GFP_KERNEL);
11311da177e4SLinus Torvalds 	if (!crq) {
11321da177e4SLinus Torvalds 		kfree(buf);
11331da177e4SLinus Torvalds 		return -EAGAIN;
11341da177e4SLinus Torvalds 	}
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	bp = buf; len = PAGE_SIZE;
11371da177e4SLinus Torvalds 
1138bc74b4f5STrond Myklebust 	cache_request(detail, h, &bp, &len);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	if (len < 0) {
11411da177e4SLinus Torvalds 		kfree(buf);
11421da177e4SLinus Torvalds 		kfree(crq);
11431da177e4SLinus Torvalds 		return -EAGAIN;
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 	crq->q.reader = 0;
11461da177e4SLinus Torvalds 	crq->item = cache_get(h);
11471da177e4SLinus Torvalds 	crq->buf = buf;
11481da177e4SLinus Torvalds 	crq->len = PAGE_SIZE - len;
11491da177e4SLinus Torvalds 	crq->readers = 0;
11501da177e4SLinus Torvalds 	spin_lock(&queue_lock);
11511da177e4SLinus Torvalds 	list_add_tail(&crq->q.list, &detail->queue);
11521da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
11531da177e4SLinus Torvalds 	wake_up(&queue_wait);
11541da177e4SLinus Torvalds 	return 0;
11551da177e4SLinus Torvalds }
1156bc74b4f5STrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds  * parse a message from user-space and pass it
11601da177e4SLinus Torvalds  * to an appropriate cache
11611da177e4SLinus Torvalds  * Messages are, like requests, separated into fields by
11621da177e4SLinus Torvalds  * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
11631da177e4SLinus Torvalds  *
11641da177e4SLinus Torvalds  * Message is
11651da177e4SLinus Torvalds  *   reply cachename expiry key ... content....
11661da177e4SLinus Torvalds  *
11671da177e4SLinus Torvalds  * key and content are both parsed by cache
11681da177e4SLinus Torvalds  */
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds #define isodigit(c) (isdigit(c) && c <= '7')
11711da177e4SLinus Torvalds int qword_get(char **bpp, char *dest, int bufsize)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	/* return bytes copied, or -1 on error */
11741da177e4SLinus Torvalds 	char *bp = *bpp;
11751da177e4SLinus Torvalds 	int len = 0;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	while (*bp == ' ') bp++;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	if (bp[0] == '\\' && bp[1] == 'x') {
11801da177e4SLinus Torvalds 		/* HEX STRING */
11811da177e4SLinus Torvalds 		bp += 2;
11821da177e4SLinus Torvalds 		while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
11831da177e4SLinus Torvalds 			int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
11841da177e4SLinus Torvalds 			bp++;
11851da177e4SLinus Torvalds 			byte <<= 4;
11861da177e4SLinus Torvalds 			byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
11871da177e4SLinus Torvalds 			*dest++ = byte;
11881da177e4SLinus Torvalds 			bp++;
11891da177e4SLinus Torvalds 			len++;
11901da177e4SLinus Torvalds 		}
11911da177e4SLinus Torvalds 	} else {
11921da177e4SLinus Torvalds 		/* text with \nnn octal quoting */
11931da177e4SLinus Torvalds 		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
11941da177e4SLinus Torvalds 			if (*bp == '\\' &&
11951da177e4SLinus Torvalds 			    isodigit(bp[1]) && (bp[1] <= '3') &&
11961da177e4SLinus Torvalds 			    isodigit(bp[2]) &&
11971da177e4SLinus Torvalds 			    isodigit(bp[3])) {
11981da177e4SLinus Torvalds 				int byte = (*++bp -'0');
11991da177e4SLinus Torvalds 				bp++;
12001da177e4SLinus Torvalds 				byte = (byte << 3) | (*bp++ - '0');
12011da177e4SLinus Torvalds 				byte = (byte << 3) | (*bp++ - '0');
12021da177e4SLinus Torvalds 				*dest++ = byte;
12031da177e4SLinus Torvalds 				len++;
12041da177e4SLinus Torvalds 			} else {
12051da177e4SLinus Torvalds 				*dest++ = *bp++;
12061da177e4SLinus Torvalds 				len++;
12071da177e4SLinus Torvalds 			}
12081da177e4SLinus Torvalds 		}
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if (*bp != ' ' && *bp != '\n' && *bp != '\0')
12121da177e4SLinus Torvalds 		return -1;
12131da177e4SLinus Torvalds 	while (*bp == ' ') bp++;
12141da177e4SLinus Torvalds 	*bpp = bp;
12151da177e4SLinus Torvalds 	*dest = '\0';
12161da177e4SLinus Torvalds 	return len;
12171da177e4SLinus Torvalds }
121824c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_get);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds /*
12221da177e4SLinus Torvalds  * support /proc/sunrpc/cache/$CACHENAME/content
12231da177e4SLinus Torvalds  * as a seqfile.
12241da177e4SLinus Torvalds  * We call ->cache_show passing NULL for the item to
12251da177e4SLinus Torvalds  * get a header, then pass each real item in the cache
12261da177e4SLinus Torvalds  */
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds struct handle {
12291da177e4SLinus Torvalds 	struct cache_detail *cd;
12301da177e4SLinus Torvalds };
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds static void *c_start(struct seq_file *m, loff_t *pos)
12339a429c49SEric Dumazet 	__acquires(cd->hash_lock)
12341da177e4SLinus Torvalds {
12351da177e4SLinus Torvalds 	loff_t n = *pos;
12361da177e4SLinus Torvalds 	unsigned hash, entry;
12371da177e4SLinus Torvalds 	struct cache_head *ch;
12381da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	read_lock(&cd->hash_lock);
12421da177e4SLinus Torvalds 	if (!n--)
12431da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
12441da177e4SLinus Torvalds 	hash = n >> 32;
12451da177e4SLinus Torvalds 	entry = n & ((1LL<<32) - 1);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	for (ch=cd->hash_table[hash]; ch; ch=ch->next)
12481da177e4SLinus Torvalds 		if (!entry--)
12491da177e4SLinus Torvalds 			return ch;
12501da177e4SLinus Torvalds 	n &= ~((1LL<<32) - 1);
12511da177e4SLinus Torvalds 	do {
12521da177e4SLinus Torvalds 		hash++;
12531da177e4SLinus Torvalds 		n += 1LL<<32;
12541da177e4SLinus Torvalds 	} while(hash < cd->hash_size &&
12551da177e4SLinus Torvalds 		cd->hash_table[hash]==NULL);
12561da177e4SLinus Torvalds 	if (hash >= cd->hash_size)
12571da177e4SLinus Torvalds 		return NULL;
12581da177e4SLinus Torvalds 	*pos = n+1;
12591da177e4SLinus Torvalds 	return cd->hash_table[hash];
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds static void *c_next(struct seq_file *m, void *p, loff_t *pos)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct cache_head *ch = p;
12651da177e4SLinus Torvalds 	int hash = (*pos >> 32);
12661da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	if (p == SEQ_START_TOKEN)
12691da177e4SLinus Torvalds 		hash = 0;
12701da177e4SLinus Torvalds 	else if (ch->next == NULL) {
12711da177e4SLinus Torvalds 		hash++;
12721da177e4SLinus Torvalds 		*pos += 1LL<<32;
12731da177e4SLinus Torvalds 	} else {
12741da177e4SLinus Torvalds 		++*pos;
12751da177e4SLinus Torvalds 		return ch->next;
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 	*pos &= ~((1LL<<32) - 1);
12781da177e4SLinus Torvalds 	while (hash < cd->hash_size &&
12791da177e4SLinus Torvalds 	       cd->hash_table[hash] == NULL) {
12801da177e4SLinus Torvalds 		hash++;
12811da177e4SLinus Torvalds 		*pos += 1LL<<32;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 	if (hash >= cd->hash_size)
12841da177e4SLinus Torvalds 		return NULL;
12851da177e4SLinus Torvalds 	++*pos;
12861da177e4SLinus Torvalds 	return cd->hash_table[hash];
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static void c_stop(struct seq_file *m, void *p)
12909a429c49SEric Dumazet 	__releases(cd->hash_lock)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
12931da177e4SLinus Torvalds 	read_unlock(&cd->hash_lock);
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds static int c_show(struct seq_file *m, void *p)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	struct cache_head *cp = p;
12991da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (p == SEQ_START_TOKEN)
13021da177e4SLinus Torvalds 		return cd->cache_show(m, cd, NULL);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	ifdebug(CACHE)
13054013edeaSNeilBrown 		seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1306c5b29f88SNeilBrown 			   convert_to_wallclock(cp->expiry_time),
1307c5b29f88SNeilBrown 			   atomic_read(&cp->ref.refcount), cp->flags);
13081da177e4SLinus Torvalds 	cache_get(cp);
13091da177e4SLinus Torvalds 	if (cache_check(cd, cp, NULL))
13101da177e4SLinus Torvalds 		/* cache_check does a cache_put on failure */
13111da177e4SLinus Torvalds 		seq_printf(m, "# ");
13121da177e4SLinus Torvalds 	else
13131da177e4SLinus Torvalds 		cache_put(cp, cd);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	return cd->cache_show(m, cd, cp);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
131856b3d975SPhilippe De Muyter static const struct seq_operations cache_content_op = {
13191da177e4SLinus Torvalds 	.start	= c_start,
13201da177e4SLinus Torvalds 	.next	= c_next,
13211da177e4SLinus Torvalds 	.stop	= c_stop,
13221da177e4SLinus Torvalds 	.show	= c_show,
13231da177e4SLinus Torvalds };
13241da177e4SLinus Torvalds 
1325173912a6STrond Myklebust static int content_open(struct inode *inode, struct file *file,
1326173912a6STrond Myklebust 			struct cache_detail *cd)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	struct handle *han;
13291da177e4SLinus Torvalds 
1330f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
1331f7e86ab9STrond Myklebust 		return -EACCES;
1332ec931035SPavel Emelyanov 	han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1333a5990ea1SLi Zefan 	if (han == NULL) {
1334a5990ea1SLi Zefan 		module_put(cd->owner);
13351da177e4SLinus Torvalds 		return -ENOMEM;
1336a5990ea1SLi Zefan 	}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	han->cd = cd;
1339ec931035SPavel Emelyanov 	return 0;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342f7e86ab9STrond Myklebust static int content_release(struct inode *inode, struct file *file,
1343f7e86ab9STrond Myklebust 		struct cache_detail *cd)
1344f7e86ab9STrond Myklebust {
1345f7e86ab9STrond Myklebust 	int ret = seq_release_private(inode, file);
1346f7e86ab9STrond Myklebust 	module_put(cd->owner);
1347f7e86ab9STrond Myklebust 	return ret;
1348f7e86ab9STrond Myklebust }
1349f7e86ab9STrond Myklebust 
1350f7e86ab9STrond Myklebust static int open_flush(struct inode *inode, struct file *file,
1351f7e86ab9STrond Myklebust 			struct cache_detail *cd)
1352f7e86ab9STrond Myklebust {
1353f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
1354f7e86ab9STrond Myklebust 		return -EACCES;
1355f7e86ab9STrond Myklebust 	return nonseekable_open(inode, file);
1356f7e86ab9STrond Myklebust }
1357f7e86ab9STrond Myklebust 
1358f7e86ab9STrond Myklebust static int release_flush(struct inode *inode, struct file *file,
1359f7e86ab9STrond Myklebust 			struct cache_detail *cd)
1360f7e86ab9STrond Myklebust {
1361f7e86ab9STrond Myklebust 	module_put(cd->owner);
1362f7e86ab9STrond Myklebust 	return 0;
1363f7e86ab9STrond Myklebust }
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds static ssize_t read_flush(struct file *file, char __user *buf,
1366173912a6STrond Myklebust 			  size_t count, loff_t *ppos,
1367173912a6STrond Myklebust 			  struct cache_detail *cd)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	char tbuf[20];
13701da177e4SLinus Torvalds 	unsigned long p = *ppos;
137101b2969aSChuck Lever 	size_t len;
13721da177e4SLinus Torvalds 
1373c5b29f88SNeilBrown 	sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
13741da177e4SLinus Torvalds 	len = strlen(tbuf);
13751da177e4SLinus Torvalds 	if (p >= len)
13761da177e4SLinus Torvalds 		return 0;
13771da177e4SLinus Torvalds 	len -= p;
137801b2969aSChuck Lever 	if (len > count)
137901b2969aSChuck Lever 		len = count;
13801da177e4SLinus Torvalds 	if (copy_to_user(buf, (void*)(tbuf+p), len))
138101b2969aSChuck Lever 		return -EFAULT;
13821da177e4SLinus Torvalds 	*ppos += len;
13831da177e4SLinus Torvalds 	return len;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds static ssize_t write_flush(struct file *file, const char __user *buf,
1387173912a6STrond Myklebust 			   size_t count, loff_t *ppos,
1388173912a6STrond Myklebust 			   struct cache_detail *cd)
13891da177e4SLinus Torvalds {
13901da177e4SLinus Torvalds 	char tbuf[20];
1391c5b29f88SNeilBrown 	char *bp, *ep;
1392c5b29f88SNeilBrown 
13931da177e4SLinus Torvalds 	if (*ppos || count > sizeof(tbuf)-1)
13941da177e4SLinus Torvalds 		return -EINVAL;
13951da177e4SLinus Torvalds 	if (copy_from_user(tbuf, buf, count))
13961da177e4SLinus Torvalds 		return -EFAULT;
13971da177e4SLinus Torvalds 	tbuf[count] = 0;
1398c5b29f88SNeilBrown 	simple_strtoul(tbuf, &ep, 0);
13991da177e4SLinus Torvalds 	if (*ep && *ep != '\n')
14001da177e4SLinus Torvalds 		return -EINVAL;
14011da177e4SLinus Torvalds 
1402c5b29f88SNeilBrown 	bp = tbuf;
1403c5b29f88SNeilBrown 	cd->flush_time = get_expiry(&bp);
1404c5b29f88SNeilBrown 	cd->nextcheck = seconds_since_boot();
14051da177e4SLinus Torvalds 	cache_flush();
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	*ppos += count;
14081da177e4SLinus Torvalds 	return count;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
1411173912a6STrond Myklebust static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1412173912a6STrond Myklebust 				 size_t count, loff_t *ppos)
1413173912a6STrond Myklebust {
1414173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1415173912a6STrond Myklebust 
1416173912a6STrond Myklebust 	return cache_read(filp, buf, count, ppos, cd);
1417173912a6STrond Myklebust }
1418173912a6STrond Myklebust 
1419173912a6STrond Myklebust static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1420173912a6STrond Myklebust 				  size_t count, loff_t *ppos)
1421173912a6STrond Myklebust {
1422173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1423173912a6STrond Myklebust 
1424173912a6STrond Myklebust 	return cache_write(filp, buf, count, ppos, cd);
1425173912a6STrond Myklebust }
1426173912a6STrond Myklebust 
1427173912a6STrond Myklebust static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1428173912a6STrond Myklebust {
1429173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1430173912a6STrond Myklebust 
1431173912a6STrond Myklebust 	return cache_poll(filp, wait, cd);
1432173912a6STrond Myklebust }
1433173912a6STrond Myklebust 
1434d79b6f4dSFrederic Weisbecker static long cache_ioctl_procfs(struct file *filp,
1435173912a6STrond Myklebust 			       unsigned int cmd, unsigned long arg)
1436173912a6STrond Myklebust {
1437d79b6f4dSFrederic Weisbecker 	long ret;
1438d79b6f4dSFrederic Weisbecker 	struct inode *inode = filp->f_path.dentry->d_inode;
1439173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1440173912a6STrond Myklebust 
1441d79b6f4dSFrederic Weisbecker 	lock_kernel();
1442d79b6f4dSFrederic Weisbecker 	ret = cache_ioctl(inode, filp, cmd, arg, cd);
1443d79b6f4dSFrederic Weisbecker 	unlock_kernel();
1444d79b6f4dSFrederic Weisbecker 
1445d79b6f4dSFrederic Weisbecker 	return ret;
1446173912a6STrond Myklebust }
1447173912a6STrond Myklebust 
1448173912a6STrond Myklebust static int cache_open_procfs(struct inode *inode, struct file *filp)
1449173912a6STrond Myklebust {
1450173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1451173912a6STrond Myklebust 
1452173912a6STrond Myklebust 	return cache_open(inode, filp, cd);
1453173912a6STrond Myklebust }
1454173912a6STrond Myklebust 
1455173912a6STrond Myklebust static int cache_release_procfs(struct inode *inode, struct file *filp)
1456173912a6STrond Myklebust {
1457173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1458173912a6STrond Myklebust 
1459173912a6STrond Myklebust 	return cache_release(inode, filp, cd);
1460173912a6STrond Myklebust }
1461173912a6STrond Myklebust 
1462173912a6STrond Myklebust static const struct file_operations cache_file_operations_procfs = {
1463173912a6STrond Myklebust 	.owner		= THIS_MODULE,
1464173912a6STrond Myklebust 	.llseek		= no_llseek,
1465173912a6STrond Myklebust 	.read		= cache_read_procfs,
1466173912a6STrond Myklebust 	.write		= cache_write_procfs,
1467173912a6STrond Myklebust 	.poll		= cache_poll_procfs,
1468d79b6f4dSFrederic Weisbecker 	.unlocked_ioctl	= cache_ioctl_procfs, /* for FIONREAD */
1469173912a6STrond Myklebust 	.open		= cache_open_procfs,
1470173912a6STrond Myklebust 	.release	= cache_release_procfs,
14711da177e4SLinus Torvalds };
1472173912a6STrond Myklebust 
1473173912a6STrond Myklebust static int content_open_procfs(struct inode *inode, struct file *filp)
1474173912a6STrond Myklebust {
1475173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1476173912a6STrond Myklebust 
1477173912a6STrond Myklebust 	return content_open(inode, filp, cd);
1478173912a6STrond Myklebust }
1479173912a6STrond Myklebust 
1480f7e86ab9STrond Myklebust static int content_release_procfs(struct inode *inode, struct file *filp)
1481f7e86ab9STrond Myklebust {
1482f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1483f7e86ab9STrond Myklebust 
1484f7e86ab9STrond Myklebust 	return content_release(inode, filp, cd);
1485f7e86ab9STrond Myklebust }
1486f7e86ab9STrond Myklebust 
1487173912a6STrond Myklebust static const struct file_operations content_file_operations_procfs = {
1488173912a6STrond Myklebust 	.open		= content_open_procfs,
1489173912a6STrond Myklebust 	.read		= seq_read,
1490173912a6STrond Myklebust 	.llseek		= seq_lseek,
1491f7e86ab9STrond Myklebust 	.release	= content_release_procfs,
1492173912a6STrond Myklebust };
1493173912a6STrond Myklebust 
1494f7e86ab9STrond Myklebust static int open_flush_procfs(struct inode *inode, struct file *filp)
1495f7e86ab9STrond Myklebust {
1496f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1497f7e86ab9STrond Myklebust 
1498f7e86ab9STrond Myklebust 	return open_flush(inode, filp, cd);
1499f7e86ab9STrond Myklebust }
1500f7e86ab9STrond Myklebust 
1501f7e86ab9STrond Myklebust static int release_flush_procfs(struct inode *inode, struct file *filp)
1502f7e86ab9STrond Myklebust {
1503f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1504f7e86ab9STrond Myklebust 
1505f7e86ab9STrond Myklebust 	return release_flush(inode, filp, cd);
1506f7e86ab9STrond Myklebust }
1507f7e86ab9STrond Myklebust 
1508173912a6STrond Myklebust static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1509173912a6STrond Myklebust 			    size_t count, loff_t *ppos)
1510173912a6STrond Myklebust {
1511173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1512173912a6STrond Myklebust 
1513173912a6STrond Myklebust 	return read_flush(filp, buf, count, ppos, cd);
1514173912a6STrond Myklebust }
1515173912a6STrond Myklebust 
1516173912a6STrond Myklebust static ssize_t write_flush_procfs(struct file *filp,
1517173912a6STrond Myklebust 				  const char __user *buf,
1518173912a6STrond Myklebust 				  size_t count, loff_t *ppos)
1519173912a6STrond Myklebust {
1520173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1521173912a6STrond Myklebust 
1522173912a6STrond Myklebust 	return write_flush(filp, buf, count, ppos, cd);
1523173912a6STrond Myklebust }
1524173912a6STrond Myklebust 
1525173912a6STrond Myklebust static const struct file_operations cache_flush_operations_procfs = {
1526f7e86ab9STrond Myklebust 	.open		= open_flush_procfs,
1527173912a6STrond Myklebust 	.read		= read_flush_procfs,
1528173912a6STrond Myklebust 	.write		= write_flush_procfs,
1529f7e86ab9STrond Myklebust 	.release	= release_flush_procfs,
1530173912a6STrond Myklebust };
1531173912a6STrond Myklebust 
1532173912a6STrond Myklebust static void remove_cache_proc_entries(struct cache_detail *cd)
1533173912a6STrond Myklebust {
1534173912a6STrond Myklebust 	if (cd->u.procfs.proc_ent == NULL)
1535173912a6STrond Myklebust 		return;
1536173912a6STrond Myklebust 	if (cd->u.procfs.flush_ent)
1537173912a6STrond Myklebust 		remove_proc_entry("flush", cd->u.procfs.proc_ent);
1538173912a6STrond Myklebust 	if (cd->u.procfs.channel_ent)
1539173912a6STrond Myklebust 		remove_proc_entry("channel", cd->u.procfs.proc_ent);
1540173912a6STrond Myklebust 	if (cd->u.procfs.content_ent)
1541173912a6STrond Myklebust 		remove_proc_entry("content", cd->u.procfs.proc_ent);
1542173912a6STrond Myklebust 	cd->u.procfs.proc_ent = NULL;
1543173912a6STrond Myklebust 	remove_proc_entry(cd->name, proc_net_rpc);
1544173912a6STrond Myklebust }
1545173912a6STrond Myklebust 
1546173912a6STrond Myklebust #ifdef CONFIG_PROC_FS
1547173912a6STrond Myklebust static int create_cache_proc_entries(struct cache_detail *cd)
1548173912a6STrond Myklebust {
1549173912a6STrond Myklebust 	struct proc_dir_entry *p;
1550173912a6STrond Myklebust 
1551173912a6STrond Myklebust 	cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1552173912a6STrond Myklebust 	if (cd->u.procfs.proc_ent == NULL)
1553173912a6STrond Myklebust 		goto out_nomem;
1554173912a6STrond Myklebust 	cd->u.procfs.channel_ent = NULL;
1555173912a6STrond Myklebust 	cd->u.procfs.content_ent = NULL;
1556173912a6STrond Myklebust 
1557173912a6STrond Myklebust 	p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1558173912a6STrond Myklebust 			     cd->u.procfs.proc_ent,
1559173912a6STrond Myklebust 			     &cache_flush_operations_procfs, cd);
1560173912a6STrond Myklebust 	cd->u.procfs.flush_ent = p;
1561173912a6STrond Myklebust 	if (p == NULL)
1562173912a6STrond Myklebust 		goto out_nomem;
1563173912a6STrond Myklebust 
1564173912a6STrond Myklebust 	if (cd->cache_upcall || cd->cache_parse) {
1565173912a6STrond Myklebust 		p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1566173912a6STrond Myklebust 				     cd->u.procfs.proc_ent,
1567173912a6STrond Myklebust 				     &cache_file_operations_procfs, cd);
1568173912a6STrond Myklebust 		cd->u.procfs.channel_ent = p;
1569173912a6STrond Myklebust 		if (p == NULL)
1570173912a6STrond Myklebust 			goto out_nomem;
1571173912a6STrond Myklebust 	}
1572173912a6STrond Myklebust 	if (cd->cache_show) {
1573173912a6STrond Myklebust 		p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1574173912a6STrond Myklebust 				cd->u.procfs.proc_ent,
1575173912a6STrond Myklebust 				&content_file_operations_procfs, cd);
1576173912a6STrond Myklebust 		cd->u.procfs.content_ent = p;
1577173912a6STrond Myklebust 		if (p == NULL)
1578173912a6STrond Myklebust 			goto out_nomem;
1579173912a6STrond Myklebust 	}
1580173912a6STrond Myklebust 	return 0;
1581173912a6STrond Myklebust out_nomem:
1582173912a6STrond Myklebust 	remove_cache_proc_entries(cd);
1583173912a6STrond Myklebust 	return -ENOMEM;
1584173912a6STrond Myklebust }
1585173912a6STrond Myklebust #else /* CONFIG_PROC_FS */
1586173912a6STrond Myklebust static int create_cache_proc_entries(struct cache_detail *cd)
1587173912a6STrond Myklebust {
1588173912a6STrond Myklebust 	return 0;
1589173912a6STrond Myklebust }
1590173912a6STrond Myklebust #endif
1591173912a6STrond Myklebust 
15928eab945cSArtem Bityutskiy void __init cache_initialize(void)
15938eab945cSArtem Bityutskiy {
15948eab945cSArtem Bityutskiy 	INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
15958eab945cSArtem Bityutskiy }
15968eab945cSArtem Bityutskiy 
1597173912a6STrond Myklebust int cache_register(struct cache_detail *cd)
1598173912a6STrond Myklebust {
1599173912a6STrond Myklebust 	int ret;
1600173912a6STrond Myklebust 
1601173912a6STrond Myklebust 	sunrpc_init_cache_detail(cd);
1602173912a6STrond Myklebust 	ret = create_cache_proc_entries(cd);
1603173912a6STrond Myklebust 	if (ret)
1604173912a6STrond Myklebust 		sunrpc_destroy_cache_detail(cd);
1605173912a6STrond Myklebust 	return ret;
1606173912a6STrond Myklebust }
1607173912a6STrond Myklebust EXPORT_SYMBOL_GPL(cache_register);
1608173912a6STrond Myklebust 
1609173912a6STrond Myklebust void cache_unregister(struct cache_detail *cd)
1610173912a6STrond Myklebust {
1611173912a6STrond Myklebust 	remove_cache_proc_entries(cd);
1612173912a6STrond Myklebust 	sunrpc_destroy_cache_detail(cd);
1613173912a6STrond Myklebust }
1614173912a6STrond Myklebust EXPORT_SYMBOL_GPL(cache_unregister);
16158854e82dSTrond Myklebust 
16168854e82dSTrond Myklebust static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
16178854e82dSTrond Myklebust 				 size_t count, loff_t *ppos)
16188854e82dSTrond Myklebust {
16198854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
16208854e82dSTrond Myklebust 
16218854e82dSTrond Myklebust 	return cache_read(filp, buf, count, ppos, cd);
16228854e82dSTrond Myklebust }
16238854e82dSTrond Myklebust 
16248854e82dSTrond Myklebust static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
16258854e82dSTrond Myklebust 				  size_t count, loff_t *ppos)
16268854e82dSTrond Myklebust {
16278854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
16288854e82dSTrond Myklebust 
16298854e82dSTrond Myklebust 	return cache_write(filp, buf, count, ppos, cd);
16308854e82dSTrond Myklebust }
16318854e82dSTrond Myklebust 
16328854e82dSTrond Myklebust static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
16338854e82dSTrond Myklebust {
16348854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
16358854e82dSTrond Myklebust 
16368854e82dSTrond Myklebust 	return cache_poll(filp, wait, cd);
16378854e82dSTrond Myklebust }
16388854e82dSTrond Myklebust 
16399918ff26SFrederic Weisbecker static long cache_ioctl_pipefs(struct file *filp,
16408854e82dSTrond Myklebust 			      unsigned int cmd, unsigned long arg)
16418854e82dSTrond Myklebust {
16429918ff26SFrederic Weisbecker 	struct inode *inode = filp->f_dentry->d_inode;
16438854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
16449918ff26SFrederic Weisbecker 	long ret;
16458854e82dSTrond Myklebust 
16469918ff26SFrederic Weisbecker 	lock_kernel();
16479918ff26SFrederic Weisbecker 	ret = cache_ioctl(inode, filp, cmd, arg, cd);
16489918ff26SFrederic Weisbecker 	unlock_kernel();
16499918ff26SFrederic Weisbecker 
16509918ff26SFrederic Weisbecker 	return ret;
16518854e82dSTrond Myklebust }
16528854e82dSTrond Myklebust 
16538854e82dSTrond Myklebust static int cache_open_pipefs(struct inode *inode, struct file *filp)
16548854e82dSTrond Myklebust {
16558854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
16568854e82dSTrond Myklebust 
16578854e82dSTrond Myklebust 	return cache_open(inode, filp, cd);
16588854e82dSTrond Myklebust }
16598854e82dSTrond Myklebust 
16608854e82dSTrond Myklebust static int cache_release_pipefs(struct inode *inode, struct file *filp)
16618854e82dSTrond Myklebust {
16628854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
16638854e82dSTrond Myklebust 
16648854e82dSTrond Myklebust 	return cache_release(inode, filp, cd);
16658854e82dSTrond Myklebust }
16668854e82dSTrond Myklebust 
16678854e82dSTrond Myklebust const struct file_operations cache_file_operations_pipefs = {
16688854e82dSTrond Myklebust 	.owner		= THIS_MODULE,
16698854e82dSTrond Myklebust 	.llseek		= no_llseek,
16708854e82dSTrond Myklebust 	.read		= cache_read_pipefs,
16718854e82dSTrond Myklebust 	.write		= cache_write_pipefs,
16728854e82dSTrond Myklebust 	.poll		= cache_poll_pipefs,
16739918ff26SFrederic Weisbecker 	.unlocked_ioctl	= cache_ioctl_pipefs, /* for FIONREAD */
16748854e82dSTrond Myklebust 	.open		= cache_open_pipefs,
16758854e82dSTrond Myklebust 	.release	= cache_release_pipefs,
16768854e82dSTrond Myklebust };
16778854e82dSTrond Myklebust 
16788854e82dSTrond Myklebust static int content_open_pipefs(struct inode *inode, struct file *filp)
16798854e82dSTrond Myklebust {
16808854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
16818854e82dSTrond Myklebust 
16828854e82dSTrond Myklebust 	return content_open(inode, filp, cd);
16838854e82dSTrond Myklebust }
16848854e82dSTrond Myklebust 
1685f7e86ab9STrond Myklebust static int content_release_pipefs(struct inode *inode, struct file *filp)
1686f7e86ab9STrond Myklebust {
1687f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1688f7e86ab9STrond Myklebust 
1689f7e86ab9STrond Myklebust 	return content_release(inode, filp, cd);
1690f7e86ab9STrond Myklebust }
1691f7e86ab9STrond Myklebust 
16928854e82dSTrond Myklebust const struct file_operations content_file_operations_pipefs = {
16938854e82dSTrond Myklebust 	.open		= content_open_pipefs,
16948854e82dSTrond Myklebust 	.read		= seq_read,
16958854e82dSTrond Myklebust 	.llseek		= seq_lseek,
1696f7e86ab9STrond Myklebust 	.release	= content_release_pipefs,
16978854e82dSTrond Myklebust };
16988854e82dSTrond Myklebust 
1699f7e86ab9STrond Myklebust static int open_flush_pipefs(struct inode *inode, struct file *filp)
1700f7e86ab9STrond Myklebust {
1701f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1702f7e86ab9STrond Myklebust 
1703f7e86ab9STrond Myklebust 	return open_flush(inode, filp, cd);
1704f7e86ab9STrond Myklebust }
1705f7e86ab9STrond Myklebust 
1706f7e86ab9STrond Myklebust static int release_flush_pipefs(struct inode *inode, struct file *filp)
1707f7e86ab9STrond Myklebust {
1708f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1709f7e86ab9STrond Myklebust 
1710f7e86ab9STrond Myklebust 	return release_flush(inode, filp, cd);
1711f7e86ab9STrond Myklebust }
1712f7e86ab9STrond Myklebust 
17138854e82dSTrond Myklebust static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
17148854e82dSTrond Myklebust 			    size_t count, loff_t *ppos)
17158854e82dSTrond Myklebust {
17168854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
17178854e82dSTrond Myklebust 
17188854e82dSTrond Myklebust 	return read_flush(filp, buf, count, ppos, cd);
17198854e82dSTrond Myklebust }
17208854e82dSTrond Myklebust 
17218854e82dSTrond Myklebust static ssize_t write_flush_pipefs(struct file *filp,
17228854e82dSTrond Myklebust 				  const char __user *buf,
17238854e82dSTrond Myklebust 				  size_t count, loff_t *ppos)
17248854e82dSTrond Myklebust {
17258854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
17268854e82dSTrond Myklebust 
17278854e82dSTrond Myklebust 	return write_flush(filp, buf, count, ppos, cd);
17288854e82dSTrond Myklebust }
17298854e82dSTrond Myklebust 
17308854e82dSTrond Myklebust const struct file_operations cache_flush_operations_pipefs = {
1731f7e86ab9STrond Myklebust 	.open		= open_flush_pipefs,
17328854e82dSTrond Myklebust 	.read		= read_flush_pipefs,
17338854e82dSTrond Myklebust 	.write		= write_flush_pipefs,
1734f7e86ab9STrond Myklebust 	.release	= release_flush_pipefs,
17358854e82dSTrond Myklebust };
17368854e82dSTrond Myklebust 
17378854e82dSTrond Myklebust int sunrpc_cache_register_pipefs(struct dentry *parent,
17388854e82dSTrond Myklebust 				 const char *name, mode_t umode,
17398854e82dSTrond Myklebust 				 struct cache_detail *cd)
17408854e82dSTrond Myklebust {
17418854e82dSTrond Myklebust 	struct qstr q;
17428854e82dSTrond Myklebust 	struct dentry *dir;
17438854e82dSTrond Myklebust 	int ret = 0;
17448854e82dSTrond Myklebust 
17458854e82dSTrond Myklebust 	sunrpc_init_cache_detail(cd);
17468854e82dSTrond Myklebust 	q.name = name;
17478854e82dSTrond Myklebust 	q.len = strlen(name);
17488854e82dSTrond Myklebust 	q.hash = full_name_hash(q.name, q.len);
17498854e82dSTrond Myklebust 	dir = rpc_create_cache_dir(parent, &q, umode, cd);
17508854e82dSTrond Myklebust 	if (!IS_ERR(dir))
17518854e82dSTrond Myklebust 		cd->u.pipefs.dir = dir;
17528854e82dSTrond Myklebust 	else {
17538854e82dSTrond Myklebust 		sunrpc_destroy_cache_detail(cd);
17548854e82dSTrond Myklebust 		ret = PTR_ERR(dir);
17558854e82dSTrond Myklebust 	}
17568854e82dSTrond Myklebust 	return ret;
17578854e82dSTrond Myklebust }
17588854e82dSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
17598854e82dSTrond Myklebust 
17608854e82dSTrond Myklebust void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
17618854e82dSTrond Myklebust {
17628854e82dSTrond Myklebust 	rpc_remove_cache_dir(cd->u.pipefs.dir);
17638854e82dSTrond Myklebust 	cd->u.pipefs.dir = NULL;
17648854e82dSTrond Myklebust 	sunrpc_destroy_cache_detail(cd);
17658854e82dSTrond Myklebust }
17668854e82dSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
17678854e82dSTrond Myklebust 
1768