xref: /linux/net/sunrpc/cache.c (revision e9dc122166b8d863d3057a66ada04838e5548e52)
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>
311da177e4SLinus Torvalds #include <asm/ioctls.h>
321da177e4SLinus Torvalds #include <linux/sunrpc/types.h>
331da177e4SLinus Torvalds #include <linux/sunrpc/cache.h>
341da177e4SLinus Torvalds #include <linux/sunrpc/stats.h>
358854e82dSTrond Myklebust #include <linux/sunrpc/rpc_pipe_fs.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #define	 RPCDBG_FACILITY RPCDBG_CACHE
381da177e4SLinus Torvalds 
39e0bb89efSJ.Bruce Fields static int cache_defer_req(struct cache_req *req, struct cache_head *item);
401da177e4SLinus Torvalds static void cache_revisit_request(struct cache_head *item);
411da177e4SLinus Torvalds 
4274cae61aSAdrian Bunk static void cache_init(struct cache_head *h)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	time_t now = get_seconds();
451da177e4SLinus Torvalds 	h->next = NULL;
461da177e4SLinus Torvalds 	h->flags = 0;
47baab935fSNeilBrown 	kref_init(&h->ref);
481da177e4SLinus Torvalds 	h->expiry_time = now + CACHE_NEW_EXPIRY;
491da177e4SLinus Torvalds 	h->last_refresh = now;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
5215a5f6bdSNeilBrown struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
5315a5f6bdSNeilBrown 				       struct cache_head *key, int hash)
5415a5f6bdSNeilBrown {
5515a5f6bdSNeilBrown 	struct cache_head **head,  **hp;
5615a5f6bdSNeilBrown 	struct cache_head *new = NULL;
5715a5f6bdSNeilBrown 
5815a5f6bdSNeilBrown 	head = &detail->hash_table[hash];
5915a5f6bdSNeilBrown 
6015a5f6bdSNeilBrown 	read_lock(&detail->hash_lock);
6115a5f6bdSNeilBrown 
6215a5f6bdSNeilBrown 	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
6315a5f6bdSNeilBrown 		struct cache_head *tmp = *hp;
6415a5f6bdSNeilBrown 		if (detail->match(tmp, key)) {
6515a5f6bdSNeilBrown 			cache_get(tmp);
6615a5f6bdSNeilBrown 			read_unlock(&detail->hash_lock);
6715a5f6bdSNeilBrown 			return tmp;
6815a5f6bdSNeilBrown 		}
6915a5f6bdSNeilBrown 	}
7015a5f6bdSNeilBrown 	read_unlock(&detail->hash_lock);
7115a5f6bdSNeilBrown 	/* Didn't find anything, insert an empty entry */
7215a5f6bdSNeilBrown 
7315a5f6bdSNeilBrown 	new = detail->alloc();
7415a5f6bdSNeilBrown 	if (!new)
7515a5f6bdSNeilBrown 		return NULL;
762f34931fSNeil Brown 	/* must fully initialise 'new', else
772f34931fSNeil Brown 	 * we might get lose if we need to
782f34931fSNeil Brown 	 * cache_put it soon.
792f34931fSNeil Brown 	 */
8015a5f6bdSNeilBrown 	cache_init(new);
812f34931fSNeil Brown 	detail->init(new, key);
8215a5f6bdSNeilBrown 
8315a5f6bdSNeilBrown 	write_lock(&detail->hash_lock);
8415a5f6bdSNeilBrown 
8515a5f6bdSNeilBrown 	/* check if entry appeared while we slept */
8615a5f6bdSNeilBrown 	for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
8715a5f6bdSNeilBrown 		struct cache_head *tmp = *hp;
8815a5f6bdSNeilBrown 		if (detail->match(tmp, key)) {
8915a5f6bdSNeilBrown 			cache_get(tmp);
9015a5f6bdSNeilBrown 			write_unlock(&detail->hash_lock);
91baab935fSNeilBrown 			cache_put(new, detail);
9215a5f6bdSNeilBrown 			return tmp;
9315a5f6bdSNeilBrown 		}
9415a5f6bdSNeilBrown 	}
9515a5f6bdSNeilBrown 	new->next = *head;
9615a5f6bdSNeilBrown 	*head = new;
9715a5f6bdSNeilBrown 	detail->entries++;
9815a5f6bdSNeilBrown 	cache_get(new);
9915a5f6bdSNeilBrown 	write_unlock(&detail->hash_lock);
10015a5f6bdSNeilBrown 
10115a5f6bdSNeilBrown 	return new;
10215a5f6bdSNeilBrown }
10324c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
10415a5f6bdSNeilBrown 
105ebd0cb1aSNeilBrown 
106f866a819SNeilBrown static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
107ebd0cb1aSNeilBrown 
108ebd0cb1aSNeilBrown static int cache_fresh_locked(struct cache_head *head, time_t expiry)
109ebd0cb1aSNeilBrown {
110ebd0cb1aSNeilBrown 	head->expiry_time = expiry;
111ebd0cb1aSNeilBrown 	head->last_refresh = get_seconds();
112ebd0cb1aSNeilBrown 	return !test_and_set_bit(CACHE_VALID, &head->flags);
113ebd0cb1aSNeilBrown }
114ebd0cb1aSNeilBrown 
115ebd0cb1aSNeilBrown static void cache_fresh_unlocked(struct cache_head *head,
116ebd0cb1aSNeilBrown 			struct cache_detail *detail, int new)
117ebd0cb1aSNeilBrown {
118ebd0cb1aSNeilBrown 	if (new)
119ebd0cb1aSNeilBrown 		cache_revisit_request(head);
120ebd0cb1aSNeilBrown 	if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
121ebd0cb1aSNeilBrown 		cache_revisit_request(head);
122f866a819SNeilBrown 		cache_dequeue(detail, head);
123ebd0cb1aSNeilBrown 	}
124ebd0cb1aSNeilBrown }
125ebd0cb1aSNeilBrown 
12615a5f6bdSNeilBrown struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
12715a5f6bdSNeilBrown 				       struct cache_head *new, struct cache_head *old, int hash)
12815a5f6bdSNeilBrown {
12915a5f6bdSNeilBrown 	/* The 'old' entry is to be replaced by 'new'.
13015a5f6bdSNeilBrown 	 * If 'old' is not VALID, we update it directly,
13115a5f6bdSNeilBrown 	 * otherwise we need to replace it
13215a5f6bdSNeilBrown 	 */
13315a5f6bdSNeilBrown 	struct cache_head **head;
13415a5f6bdSNeilBrown 	struct cache_head *tmp;
135ebd0cb1aSNeilBrown 	int is_new;
13615a5f6bdSNeilBrown 
13715a5f6bdSNeilBrown 	if (!test_bit(CACHE_VALID, &old->flags)) {
13815a5f6bdSNeilBrown 		write_lock(&detail->hash_lock);
13915a5f6bdSNeilBrown 		if (!test_bit(CACHE_VALID, &old->flags)) {
14015a5f6bdSNeilBrown 			if (test_bit(CACHE_NEGATIVE, &new->flags))
14115a5f6bdSNeilBrown 				set_bit(CACHE_NEGATIVE, &old->flags);
14215a5f6bdSNeilBrown 			else
14315a5f6bdSNeilBrown 				detail->update(old, new);
144ebd0cb1aSNeilBrown 			is_new = cache_fresh_locked(old, new->expiry_time);
14515a5f6bdSNeilBrown 			write_unlock(&detail->hash_lock);
146ebd0cb1aSNeilBrown 			cache_fresh_unlocked(old, detail, is_new);
14715a5f6bdSNeilBrown 			return old;
14815a5f6bdSNeilBrown 		}
14915a5f6bdSNeilBrown 		write_unlock(&detail->hash_lock);
15015a5f6bdSNeilBrown 	}
15115a5f6bdSNeilBrown 	/* We need to insert a new entry */
15215a5f6bdSNeilBrown 	tmp = detail->alloc();
15315a5f6bdSNeilBrown 	if (!tmp) {
154baab935fSNeilBrown 		cache_put(old, detail);
15515a5f6bdSNeilBrown 		return NULL;
15615a5f6bdSNeilBrown 	}
15715a5f6bdSNeilBrown 	cache_init(tmp);
15815a5f6bdSNeilBrown 	detail->init(tmp, old);
15915a5f6bdSNeilBrown 	head = &detail->hash_table[hash];
16015a5f6bdSNeilBrown 
16115a5f6bdSNeilBrown 	write_lock(&detail->hash_lock);
16215a5f6bdSNeilBrown 	if (test_bit(CACHE_NEGATIVE, &new->flags))
16315a5f6bdSNeilBrown 		set_bit(CACHE_NEGATIVE, &tmp->flags);
16415a5f6bdSNeilBrown 	else
16515a5f6bdSNeilBrown 		detail->update(tmp, new);
16615a5f6bdSNeilBrown 	tmp->next = *head;
16715a5f6bdSNeilBrown 	*head = tmp;
168f2d39586SNeilBrown 	detail->entries++;
16915a5f6bdSNeilBrown 	cache_get(tmp);
170ebd0cb1aSNeilBrown 	is_new = cache_fresh_locked(tmp, new->expiry_time);
171ebd0cb1aSNeilBrown 	cache_fresh_locked(old, 0);
17215a5f6bdSNeilBrown 	write_unlock(&detail->hash_lock);
173ebd0cb1aSNeilBrown 	cache_fresh_unlocked(tmp, detail, is_new);
174ebd0cb1aSNeilBrown 	cache_fresh_unlocked(old, detail, 0);
175baab935fSNeilBrown 	cache_put(old, detail);
17615a5f6bdSNeilBrown 	return tmp;
17715a5f6bdSNeilBrown }
17824c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_update);
1791da177e4SLinus Torvalds 
180bc74b4f5STrond Myklebust static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
181bc74b4f5STrond Myklebust {
182bc74b4f5STrond Myklebust 	if (!cd->cache_upcall)
183bc74b4f5STrond Myklebust 		return -EINVAL;
184bc74b4f5STrond Myklebust 	return cd->cache_upcall(cd, h);
185bc74b4f5STrond Myklebust }
186989a19b9SNeilBrown 
187989a19b9SNeilBrown static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
188989a19b9SNeilBrown {
189989a19b9SNeilBrown 	if (!test_bit(CACHE_VALID, &h->flags) ||
190989a19b9SNeilBrown 	    h->expiry_time < get_seconds())
191989a19b9SNeilBrown 		return -EAGAIN;
192989a19b9SNeilBrown 	else if (detail->flush_time > h->last_refresh)
193989a19b9SNeilBrown 		return -EAGAIN;
194989a19b9SNeilBrown 	else {
195989a19b9SNeilBrown 		/* entry is valid */
196989a19b9SNeilBrown 		if (test_bit(CACHE_NEGATIVE, &h->flags))
197989a19b9SNeilBrown 			return -ENOENT;
198989a19b9SNeilBrown 		else
199989a19b9SNeilBrown 			return 0;
200989a19b9SNeilBrown 	}
201989a19b9SNeilBrown }
202*e9dc1221SJ. Bruce Fields 
2031da177e4SLinus Torvalds /*
2041da177e4SLinus Torvalds  * This is the generic cache management routine for all
2051da177e4SLinus Torvalds  * the authentication caches.
2061da177e4SLinus Torvalds  * It checks the currency of a cache item and will (later)
2071da177e4SLinus Torvalds  * initiate an upcall to fill it if needed.
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  *
2101da177e4SLinus Torvalds  * Returns 0 if the cache_head can be used, or cache_puts it and returns
211989a19b9SNeilBrown  * -EAGAIN if upcall is pending and request has been queued
212989a19b9SNeilBrown  * -ETIMEDOUT if upcall failed or request could not be queue or
213989a19b9SNeilBrown  *           upcall completed but item is still invalid (implying that
214989a19b9SNeilBrown  *           the cache item has been replaced with a newer one).
2151da177e4SLinus Torvalds  * -ENOENT if cache entry was negative
2161da177e4SLinus Torvalds  */
2171da177e4SLinus Torvalds int cache_check(struct cache_detail *detail,
2181da177e4SLinus Torvalds 		    struct cache_head *h, struct cache_req *rqstp)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	int rv;
2211da177e4SLinus Torvalds 	long refresh_age, age;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	/* First decide return status as best we can */
224989a19b9SNeilBrown 	rv = cache_is_valid(detail, h);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/* now see if we want to start an upcall */
2271da177e4SLinus Torvalds 	refresh_age = (h->expiry_time - h->last_refresh);
2281da177e4SLinus Torvalds 	age = get_seconds() - h->last_refresh;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (rqstp == NULL) {
2311da177e4SLinus Torvalds 		if (rv == -EAGAIN)
2321da177e4SLinus Torvalds 			rv = -ENOENT;
2331da177e4SLinus Torvalds 	} else if (rv == -EAGAIN || age > refresh_age/2) {
23446121cf7SChuck Lever 		dprintk("RPC:       Want update, refage=%ld, age=%ld\n",
23546121cf7SChuck Lever 				refresh_age, age);
2361da177e4SLinus Torvalds 		if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
2371da177e4SLinus Torvalds 			switch (cache_make_upcall(detail, h)) {
2381da177e4SLinus Torvalds 			case -EINVAL:
2391da177e4SLinus Torvalds 				clear_bit(CACHE_PENDING, &h->flags);
2405c4d2639SNeilBrown 				cache_revisit_request(h);
2411da177e4SLinus Torvalds 				if (rv == -EAGAIN) {
2421da177e4SLinus Torvalds 					set_bit(CACHE_NEGATIVE, &h->flags);
243ebd0cb1aSNeilBrown 					cache_fresh_unlocked(h, detail,
244ebd0cb1aSNeilBrown 					     cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
2451da177e4SLinus Torvalds 					rv = -ENOENT;
2461da177e4SLinus Torvalds 				}
2471da177e4SLinus Torvalds 				break;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 			case -EAGAIN:
2501da177e4SLinus Torvalds 				clear_bit(CACHE_PENDING, &h->flags);
2511da177e4SLinus Torvalds 				cache_revisit_request(h);
2521da177e4SLinus Torvalds 				break;
2531da177e4SLinus Torvalds 			}
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 
257989a19b9SNeilBrown 	if (rv == -EAGAIN) {
258989a19b9SNeilBrown 		if (cache_defer_req(rqstp, h) == 0) {
259989a19b9SNeilBrown 			/* Request is not deferred */
260989a19b9SNeilBrown 			rv = cache_is_valid(detail, h);
2611da177e4SLinus Torvalds 			if (rv == -EAGAIN)
262e0bb89efSJ.Bruce Fields 				rv = -ETIMEDOUT;
263989a19b9SNeilBrown 		}
264989a19b9SNeilBrown 	}
2654013edeaSNeilBrown 	if (rv)
266baab935fSNeilBrown 		cache_put(h, detail);
2671da177e4SLinus Torvalds 	return rv;
2681da177e4SLinus Torvalds }
26924c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_check);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds  * caches need to be periodically cleaned.
2731da177e4SLinus Torvalds  * For this we maintain a list of cache_detail and
2741da177e4SLinus Torvalds  * a current pointer into that list and into the table
2751da177e4SLinus Torvalds  * for that entry.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * Each time clean_cache is called it finds the next non-empty entry
2781da177e4SLinus Torvalds  * in the current table and walks the list in that entry
2791da177e4SLinus Torvalds  * looking for entries that can be removed.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * An entry gets removed if:
2821da177e4SLinus Torvalds  * - The expiry is before current time
2831da177e4SLinus Torvalds  * - The last_refresh time is before the flush_time for that cache
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * later we might drop old entries with non-NEVER expiry if that table
2861da177e4SLinus Torvalds  * is getting 'full' for some definition of 'full'
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * The question of "how often to scan a table" is an interesting one
2891da177e4SLinus Torvalds  * and is answered in part by the use of the "nextcheck" field in the
2901da177e4SLinus Torvalds  * cache_detail.
2911da177e4SLinus Torvalds  * When a scan of a table begins, the nextcheck field is set to a time
2921da177e4SLinus Torvalds  * that is well into the future.
2931da177e4SLinus Torvalds  * While scanning, if an expiry time is found that is earlier than the
2941da177e4SLinus Torvalds  * current nextcheck time, nextcheck is set to that expiry time.
2951da177e4SLinus Torvalds  * If the flush_time is ever set to a time earlier than the nextcheck
2961da177e4SLinus Torvalds  * time, the nextcheck time is then set to that flush_time.
2971da177e4SLinus Torvalds  *
2981da177e4SLinus Torvalds  * A table is then only scanned if the current time is at least
2991da177e4SLinus Torvalds  * the nextcheck time.
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static LIST_HEAD(cache_list);
3041da177e4SLinus Torvalds static DEFINE_SPINLOCK(cache_list_lock);
3051da177e4SLinus Torvalds static struct cache_detail *current_detail;
3061da177e4SLinus Torvalds static int current_index;
3071da177e4SLinus Torvalds 
30865f27f38SDavid Howells static void do_cache_clean(struct work_struct *work);
30965f27f38SDavid Howells static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
3101da177e4SLinus Torvalds 
3115b7a1b9fSTrond Myklebust static void sunrpc_init_cache_detail(struct cache_detail *cd)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	rwlock_init(&cd->hash_lock);
3141da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cd->queue);
3151da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3161da177e4SLinus Torvalds 	cd->nextcheck = 0;
3171da177e4SLinus Torvalds 	cd->entries = 0;
3181da177e4SLinus Torvalds 	atomic_set(&cd->readers, 0);
3191da177e4SLinus Torvalds 	cd->last_close = 0;
3201da177e4SLinus Torvalds 	cd->last_warn = -1;
3211da177e4SLinus Torvalds 	list_add(&cd->others, &cache_list);
3221da177e4SLinus Torvalds 	spin_unlock(&cache_list_lock);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* start the cleaning process */
32552bad64dSDavid Howells 	schedule_delayed_work(&cache_cleaner, 0);
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3285b7a1b9fSTrond Myklebust static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	cache_purge(cd);
3311da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3321da177e4SLinus Torvalds 	write_lock(&cd->hash_lock);
3331da177e4SLinus Torvalds 	if (cd->entries || atomic_read(&cd->inuse)) {
3341da177e4SLinus Torvalds 		write_unlock(&cd->hash_lock);
3351da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
336df95a9d4SJ. Bruce Fields 		goto out;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	if (current_detail == cd)
3391da177e4SLinus Torvalds 		current_detail = NULL;
3401da177e4SLinus Torvalds 	list_del_init(&cd->others);
3411da177e4SLinus Torvalds 	write_unlock(&cd->hash_lock);
3421da177e4SLinus Torvalds 	spin_unlock(&cache_list_lock);
3431da177e4SLinus Torvalds 	if (list_empty(&cache_list)) {
3441da177e4SLinus Torvalds 		/* module must be being unloaded so its safe to kill the worker */
3454011cd97STrond Myklebust 		cancel_delayed_work_sync(&cache_cleaner);
3461da177e4SLinus Torvalds 	}
347df95a9d4SJ. Bruce Fields 	return;
348df95a9d4SJ. Bruce Fields out:
349df95a9d4SJ. Bruce Fields 	printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds /* clean cache tries to find something to clean
3531da177e4SLinus Torvalds  * and cleans it.
3541da177e4SLinus Torvalds  * It returns 1 if it cleaned something,
3551da177e4SLinus Torvalds  *            0 if it didn't find anything this time
3561da177e4SLinus Torvalds  *           -1 if it fell off the end of the list.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds static int cache_clean(void)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	int rv = 0;
3611da177e4SLinus Torvalds 	struct list_head *next;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	spin_lock(&cache_list_lock);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* find a suitable table if we don't already have one */
3661da177e4SLinus Torvalds 	while (current_detail == NULL ||
3671da177e4SLinus Torvalds 	    current_index >= current_detail->hash_size) {
3681da177e4SLinus Torvalds 		if (current_detail)
3691da177e4SLinus Torvalds 			next = current_detail->others.next;
3701da177e4SLinus Torvalds 		else
3711da177e4SLinus Torvalds 			next = cache_list.next;
3721da177e4SLinus Torvalds 		if (next == &cache_list) {
3731da177e4SLinus Torvalds 			current_detail = NULL;
3741da177e4SLinus Torvalds 			spin_unlock(&cache_list_lock);
3751da177e4SLinus Torvalds 			return -1;
3761da177e4SLinus Torvalds 		}
3771da177e4SLinus Torvalds 		current_detail = list_entry(next, struct cache_detail, others);
3781da177e4SLinus Torvalds 		if (current_detail->nextcheck > get_seconds())
3791da177e4SLinus Torvalds 			current_index = current_detail->hash_size;
3801da177e4SLinus Torvalds 		else {
3811da177e4SLinus Torvalds 			current_index = 0;
3821da177e4SLinus Torvalds 			current_detail->nextcheck = get_seconds()+30*60;
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* find a non-empty bucket in the table */
3871da177e4SLinus Torvalds 	while (current_detail &&
3881da177e4SLinus Torvalds 	       current_index < current_detail->hash_size &&
3891da177e4SLinus Torvalds 	       current_detail->hash_table[current_index] == NULL)
3901da177e4SLinus Torvalds 		current_index++;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* find a cleanable entry in the bucket and clean it, or set to next bucket */
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (current_detail && current_index < current_detail->hash_size) {
3951da177e4SLinus Torvalds 		struct cache_head *ch, **cp;
3961da177e4SLinus Torvalds 		struct cache_detail *d;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		write_lock(&current_detail->hash_lock);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		/* Ok, now to clean this strand */
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 		cp = & current_detail->hash_table[current_index];
4031da177e4SLinus Torvalds 		ch = *cp;
4041da177e4SLinus Torvalds 		for (; ch; cp= & ch->next, ch= *cp) {
4051da177e4SLinus Torvalds 			if (current_detail->nextcheck > ch->expiry_time)
4061da177e4SLinus Torvalds 				current_detail->nextcheck = ch->expiry_time+1;
4071da177e4SLinus Torvalds 			if (ch->expiry_time >= get_seconds()
4081da177e4SLinus Torvalds 			    && ch->last_refresh >= current_detail->flush_time
4091da177e4SLinus Torvalds 				)
4101da177e4SLinus Torvalds 				continue;
4111da177e4SLinus Torvalds 			if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
412f866a819SNeilBrown 				cache_dequeue(current_detail, ch);
4131da177e4SLinus Torvalds 
414baab935fSNeilBrown 			if (atomic_read(&ch->ref.refcount) == 1)
4151da177e4SLinus Torvalds 				break;
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 		if (ch) {
4181da177e4SLinus Torvalds 			*cp = ch->next;
4191da177e4SLinus Torvalds 			ch->next = NULL;
4201da177e4SLinus Torvalds 			current_detail->entries--;
4211da177e4SLinus Torvalds 			rv = 1;
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 		write_unlock(&current_detail->hash_lock);
4241da177e4SLinus Torvalds 		d = current_detail;
4251da177e4SLinus Torvalds 		if (!ch)
4261da177e4SLinus Torvalds 			current_index ++;
4271da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
4285c4d2639SNeilBrown 		if (ch) {
4295c4d2639SNeilBrown 			cache_revisit_request(ch);
430baab935fSNeilBrown 			cache_put(ch, d);
4315c4d2639SNeilBrown 		}
4321da177e4SLinus Torvalds 	} else
4331da177e4SLinus Torvalds 		spin_unlock(&cache_list_lock);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	return rv;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds  * We want to regularly clean the cache, so we need to schedule some work ...
4401da177e4SLinus Torvalds  */
44165f27f38SDavid Howells static void do_cache_clean(struct work_struct *work)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	int delay = 5;
4441da177e4SLinus Torvalds 	if (cache_clean() == -1)
4456aad89c8SAnton Blanchard 		delay = round_jiffies_relative(30*HZ);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (list_empty(&cache_list))
4481da177e4SLinus Torvalds 		delay = 0;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (delay)
4511da177e4SLinus Torvalds 		schedule_delayed_work(&cache_cleaner, delay);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
4561da177e4SLinus Torvalds  * Clean all caches promptly.  This just calls cache_clean
4571da177e4SLinus Torvalds  * repeatedly until we are sure that every cache has had a chance to
4581da177e4SLinus Torvalds  * be fully cleaned
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds void cache_flush(void)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	while (cache_clean() != -1)
4631da177e4SLinus Torvalds 		cond_resched();
4641da177e4SLinus Torvalds 	while (cache_clean() != -1)
4651da177e4SLinus Torvalds 		cond_resched();
4661da177e4SLinus Torvalds }
46724c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_flush);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds void cache_purge(struct cache_detail *detail)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	detail->flush_time = LONG_MAX;
4721da177e4SLinus Torvalds 	detail->nextcheck = get_seconds();
4731da177e4SLinus Torvalds 	cache_flush();
4741da177e4SLinus Torvalds 	detail->flush_time = 1;
4751da177e4SLinus Torvalds }
47624c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(cache_purge);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * Deferral and Revisiting of Requests.
4811da177e4SLinus Torvalds  *
4821da177e4SLinus Torvalds  * If a cache lookup finds a pending entry, we
4831da177e4SLinus Torvalds  * need to defer the request and revisit it later.
4841da177e4SLinus Torvalds  * All deferred requests are stored in a hash table,
4851da177e4SLinus Torvalds  * indexed by "struct cache_head *".
4861da177e4SLinus Torvalds  * As it may be wasteful to store a whole request
4871da177e4SLinus Torvalds  * structure, we allow the request to provide a
4881da177e4SLinus Torvalds  * deferred form, which must contain a
4891da177e4SLinus Torvalds  * 'struct cache_deferred_req'
4901da177e4SLinus Torvalds  * This cache_deferred_req contains a method to allow
4911da177e4SLinus Torvalds  * it to be revisited when cache info is available
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds #define	DFR_HASHSIZE	(PAGE_SIZE/sizeof(struct list_head))
4951da177e4SLinus Torvalds #define	DFR_HASH(item)	((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds #define	DFR_MAX	300	/* ??? */
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds static DEFINE_SPINLOCK(cache_defer_lock);
5001da177e4SLinus Torvalds static LIST_HEAD(cache_defer_list);
5011da177e4SLinus Torvalds static struct list_head cache_defer_hash[DFR_HASHSIZE];
5021da177e4SLinus Torvalds static int cache_defer_cnt;
5031da177e4SLinus Torvalds 
504e0bb89efSJ.Bruce Fields static int cache_defer_req(struct cache_req *req, struct cache_head *item)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	struct cache_deferred_req *dreq;
5071da177e4SLinus Torvalds 	int hash = DFR_HASH(item);
5081da177e4SLinus Torvalds 
50901f3bd1fSJ.Bruce Fields 	if (cache_defer_cnt >= DFR_MAX) {
51001f3bd1fSJ.Bruce Fields 		/* too much in the cache, randomly drop this one,
51101f3bd1fSJ.Bruce Fields 		 * or continue and drop the oldest below
51201f3bd1fSJ.Bruce Fields 		 */
51301f3bd1fSJ.Bruce Fields 		if (net_random()&1)
514989a19b9SNeilBrown 			return 0;
51501f3bd1fSJ.Bruce Fields 	}
5161da177e4SLinus Torvalds 	dreq = req->defer(req);
5171da177e4SLinus Torvalds 	if (dreq == NULL)
518989a19b9SNeilBrown 		return 0;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	dreq->item = item;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	list_add(&dreq->recent, &cache_defer_list);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (cache_defer_hash[hash].next == NULL)
5271da177e4SLinus Torvalds 		INIT_LIST_HEAD(&cache_defer_hash[hash]);
5281da177e4SLinus Torvalds 	list_add(&dreq->hash, &cache_defer_hash[hash]);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	/* it is in, now maybe clean up */
5311da177e4SLinus Torvalds 	dreq = NULL;
5321da177e4SLinus Torvalds 	if (++cache_defer_cnt > DFR_MAX) {
5331da177e4SLinus Torvalds 		dreq = list_entry(cache_defer_list.prev,
53401f3bd1fSJ.Bruce Fields 				  struct cache_deferred_req, recent);
5351da177e4SLinus Torvalds 		list_del(&dreq->recent);
5361da177e4SLinus Torvalds 		list_del(&dreq->hash);
5371da177e4SLinus Torvalds 		cache_defer_cnt--;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (dreq) {
5421da177e4SLinus Torvalds 		/* there was one too many */
5431da177e4SLinus Torvalds 		dreq->revisit(dreq, 1);
5441da177e4SLinus Torvalds 	}
5454013edeaSNeilBrown 	if (!test_bit(CACHE_PENDING, &item->flags)) {
5461da177e4SLinus Torvalds 		/* must have just been validated... */
5471da177e4SLinus Torvalds 		cache_revisit_request(item);
548e0bb89efSJ.Bruce Fields 		return 0;
5491da177e4SLinus Torvalds 	}
550989a19b9SNeilBrown 	return 1;
551989a19b9SNeilBrown }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds static void cache_revisit_request(struct cache_head *item)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct cache_deferred_req *dreq;
5561da177e4SLinus Torvalds 	struct list_head pending;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	struct list_head *lp;
5591da177e4SLinus Torvalds 	int hash = DFR_HASH(item);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&pending);
5621da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	lp = cache_defer_hash[hash].next;
5651da177e4SLinus Torvalds 	if (lp) {
5661da177e4SLinus Torvalds 		while (lp != &cache_defer_hash[hash]) {
5671da177e4SLinus Torvalds 			dreq = list_entry(lp, struct cache_deferred_req, hash);
5681da177e4SLinus Torvalds 			lp = lp->next;
5691da177e4SLinus Torvalds 			if (dreq->item == item) {
5701da177e4SLinus Torvalds 				list_del(&dreq->hash);
5711da177e4SLinus Torvalds 				list_move(&dreq->recent, &pending);
5721da177e4SLinus Torvalds 				cache_defer_cnt--;
5731da177e4SLinus Torvalds 			}
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	while (!list_empty(&pending)) {
5791da177e4SLinus Torvalds 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
5801da177e4SLinus Torvalds 		list_del_init(&dreq->recent);
5811da177e4SLinus Torvalds 		dreq->revisit(dreq, 0);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds void cache_clean_deferred(void *owner)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	struct cache_deferred_req *dreq, *tmp;
5881da177e4SLinus Torvalds 	struct list_head pending;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	INIT_LIST_HEAD(&pending);
5921da177e4SLinus Torvalds 	spin_lock(&cache_defer_lock);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
5951da177e4SLinus Torvalds 		if (dreq->owner == owner) {
5961da177e4SLinus Torvalds 			list_del(&dreq->hash);
5971da177e4SLinus Torvalds 			list_move(&dreq->recent, &pending);
5981da177e4SLinus Torvalds 			cache_defer_cnt--;
5991da177e4SLinus Torvalds 		}
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 	spin_unlock(&cache_defer_lock);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	while (!list_empty(&pending)) {
6041da177e4SLinus Torvalds 		dreq = list_entry(pending.next, struct cache_deferred_req, recent);
6051da177e4SLinus Torvalds 		list_del_init(&dreq->recent);
6061da177e4SLinus Torvalds 		dreq->revisit(dreq, 1);
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
6111da177e4SLinus Torvalds  * communicate with user-space
6121da177e4SLinus Torvalds  *
613a490c681SJ. Bruce Fields  * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
614a490c681SJ. Bruce Fields  * On read, you get a full request, or block.
615a490c681SJ. Bruce Fields  * On write, an update request is processed.
616a490c681SJ. Bruce Fields  * Poll works if anything to read, and always allows write.
6171da177e4SLinus Torvalds  *
6181da177e4SLinus Torvalds  * Implemented by linked list of requests.  Each open file has
619a490c681SJ. Bruce Fields  * a ->private that also exists in this list.  New requests are added
6201da177e4SLinus Torvalds  * to the end and may wakeup and preceding readers.
6211da177e4SLinus Torvalds  * New readers are added to the head.  If, on read, an item is found with
6221da177e4SLinus Torvalds  * CACHE_UPCALLING clear, we free it from the list.
6231da177e4SLinus Torvalds  *
6241da177e4SLinus Torvalds  */
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds static DEFINE_SPINLOCK(queue_lock);
6274a3e2f71SArjan van de Ven static DEFINE_MUTEX(queue_io_mutex);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds struct cache_queue {
6301da177e4SLinus Torvalds 	struct list_head	list;
6311da177e4SLinus Torvalds 	int			reader;	/* if 0, then request */
6321da177e4SLinus Torvalds };
6331da177e4SLinus Torvalds struct cache_request {
6341da177e4SLinus Torvalds 	struct cache_queue	q;
6351da177e4SLinus Torvalds 	struct cache_head	*item;
6361da177e4SLinus Torvalds 	char			* buf;
6371da177e4SLinus Torvalds 	int			len;
6381da177e4SLinus Torvalds 	int			readers;
6391da177e4SLinus Torvalds };
6401da177e4SLinus Torvalds struct cache_reader {
6411da177e4SLinus Torvalds 	struct cache_queue	q;
6421da177e4SLinus Torvalds 	int			offset;	/* if non-0, we have a refcnt on next request */
6431da177e4SLinus Torvalds };
6441da177e4SLinus Torvalds 
645173912a6STrond Myklebust static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
646173912a6STrond Myklebust 			  loff_t *ppos, struct cache_detail *cd)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
6491da177e4SLinus Torvalds 	struct cache_request *rq;
650da77005fSTrond Myklebust 	struct inode *inode = filp->f_path.dentry->d_inode;
6511da177e4SLinus Torvalds 	int err;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (count == 0)
6541da177e4SLinus Torvalds 		return 0;
6551da177e4SLinus Torvalds 
656da77005fSTrond Myklebust 	mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
6571da177e4SLinus Torvalds 			      * readers on this file */
6581da177e4SLinus Torvalds  again:
6591da177e4SLinus Torvalds 	spin_lock(&queue_lock);
6601da177e4SLinus Torvalds 	/* need to find next request */
6611da177e4SLinus Torvalds 	while (rp->q.list.next != &cd->queue &&
6621da177e4SLinus Torvalds 	       list_entry(rp->q.list.next, struct cache_queue, list)
6631da177e4SLinus Torvalds 	       ->reader) {
6641da177e4SLinus Torvalds 		struct list_head *next = rp->q.list.next;
6651da177e4SLinus Torvalds 		list_move(&rp->q.list, next);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	if (rp->q.list.next == &cd->queue) {
6681da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
669da77005fSTrond Myklebust 		mutex_unlock(&inode->i_mutex);
67009a62660SKris Katterjohn 		BUG_ON(rp->offset);
6711da177e4SLinus Torvalds 		return 0;
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 	rq = container_of(rp->q.list.next, struct cache_request, q.list);
67409a62660SKris Katterjohn 	BUG_ON(rq->q.reader);
6751da177e4SLinus Torvalds 	if (rp->offset == 0)
6761da177e4SLinus Torvalds 		rq->readers++;
6771da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
6801da177e4SLinus Torvalds 		err = -EAGAIN;
6811da177e4SLinus Torvalds 		spin_lock(&queue_lock);
6821da177e4SLinus Torvalds 		list_move(&rp->q.list, &rq->q.list);
6831da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
6841da177e4SLinus Torvalds 	} else {
6851da177e4SLinus Torvalds 		if (rp->offset + count > rq->len)
6861da177e4SLinus Torvalds 			count = rq->len - rp->offset;
6871da177e4SLinus Torvalds 		err = -EFAULT;
6881da177e4SLinus Torvalds 		if (copy_to_user(buf, rq->buf + rp->offset, count))
6891da177e4SLinus Torvalds 			goto out;
6901da177e4SLinus Torvalds 		rp->offset += count;
6911da177e4SLinus Torvalds 		if (rp->offset >= rq->len) {
6921da177e4SLinus Torvalds 			rp->offset = 0;
6931da177e4SLinus Torvalds 			spin_lock(&queue_lock);
6941da177e4SLinus Torvalds 			list_move(&rp->q.list, &rq->q.list);
6951da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
6961da177e4SLinus Torvalds 		}
6971da177e4SLinus Torvalds 		err = 0;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds  out:
7001da177e4SLinus Torvalds 	if (rp->offset == 0) {
7011da177e4SLinus Torvalds 		/* need to release rq */
7021da177e4SLinus Torvalds 		spin_lock(&queue_lock);
7031da177e4SLinus Torvalds 		rq->readers--;
7041da177e4SLinus Torvalds 		if (rq->readers == 0 &&
7051da177e4SLinus Torvalds 		    !test_bit(CACHE_PENDING, &rq->item->flags)) {
7061da177e4SLinus Torvalds 			list_del(&rq->q.list);
7071da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
708baab935fSNeilBrown 			cache_put(rq->item, cd);
7091da177e4SLinus Torvalds 			kfree(rq->buf);
7101da177e4SLinus Torvalds 			kfree(rq);
7111da177e4SLinus Torvalds 		} else
7121da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	if (err == -EAGAIN)
7151da177e4SLinus Torvalds 		goto again;
716da77005fSTrond Myklebust 	mutex_unlock(&inode->i_mutex);
7171da177e4SLinus Torvalds 	return err ? err :  count;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
720da77005fSTrond Myklebust static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
721da77005fSTrond Myklebust 				 size_t count, struct cache_detail *cd)
7221da177e4SLinus Torvalds {
723da77005fSTrond Myklebust 	ssize_t ret;
7241da177e4SLinus Torvalds 
725da77005fSTrond Myklebust 	if (copy_from_user(kaddr, buf, count))
7261da177e4SLinus Torvalds 		return -EFAULT;
727da77005fSTrond Myklebust 	kaddr[count] = '\0';
728da77005fSTrond Myklebust 	ret = cd->cache_parse(cd, kaddr, count);
729da77005fSTrond Myklebust 	if (!ret)
730da77005fSTrond Myklebust 		ret = count;
731da77005fSTrond Myklebust 	return ret;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734da77005fSTrond Myklebust static ssize_t cache_slow_downcall(const char __user *buf,
735da77005fSTrond Myklebust 				   size_t count, struct cache_detail *cd)
736da77005fSTrond Myklebust {
7371da177e4SLinus Torvalds 	static char write_buf[8192]; /* protected by queue_io_mutex */
738da77005fSTrond Myklebust 	ssize_t ret = -EINVAL;
739da77005fSTrond Myklebust 
740da77005fSTrond Myklebust 	if (count >= sizeof(write_buf))
741da77005fSTrond Myklebust 		goto out;
742da77005fSTrond Myklebust 	mutex_lock(&queue_io_mutex);
743da77005fSTrond Myklebust 	ret = cache_do_downcall(write_buf, buf, count, cd);
7444a3e2f71SArjan van de Ven 	mutex_unlock(&queue_io_mutex);
745da77005fSTrond Myklebust out:
746da77005fSTrond Myklebust 	return ret;
747da77005fSTrond Myklebust }
748da77005fSTrond Myklebust 
749da77005fSTrond Myklebust static ssize_t cache_downcall(struct address_space *mapping,
750da77005fSTrond Myklebust 			      const char __user *buf,
751da77005fSTrond Myklebust 			      size_t count, struct cache_detail *cd)
752da77005fSTrond Myklebust {
753da77005fSTrond Myklebust 	struct page *page;
754da77005fSTrond Myklebust 	char *kaddr;
755da77005fSTrond Myklebust 	ssize_t ret = -ENOMEM;
756da77005fSTrond Myklebust 
757da77005fSTrond Myklebust 	if (count >= PAGE_CACHE_SIZE)
758da77005fSTrond Myklebust 		goto out_slow;
759da77005fSTrond Myklebust 
760da77005fSTrond Myklebust 	page = find_or_create_page(mapping, 0, GFP_KERNEL);
761da77005fSTrond Myklebust 	if (!page)
762da77005fSTrond Myklebust 		goto out_slow;
763da77005fSTrond Myklebust 
764da77005fSTrond Myklebust 	kaddr = kmap(page);
765da77005fSTrond Myklebust 	ret = cache_do_downcall(kaddr, buf, count, cd);
766da77005fSTrond Myklebust 	kunmap(page);
767da77005fSTrond Myklebust 	unlock_page(page);
768da77005fSTrond Myklebust 	page_cache_release(page);
769da77005fSTrond Myklebust 	return ret;
770da77005fSTrond Myklebust out_slow:
771da77005fSTrond Myklebust 	return cache_slow_downcall(buf, count, cd);
772da77005fSTrond Myklebust }
7731da177e4SLinus Torvalds 
774173912a6STrond Myklebust static ssize_t cache_write(struct file *filp, const char __user *buf,
775173912a6STrond Myklebust 			   size_t count, loff_t *ppos,
776173912a6STrond Myklebust 			   struct cache_detail *cd)
7771da177e4SLinus Torvalds {
778da77005fSTrond Myklebust 	struct address_space *mapping = filp->f_mapping;
779da77005fSTrond Myklebust 	struct inode *inode = filp->f_path.dentry->d_inode;
780da77005fSTrond Myklebust 	ssize_t ret = -EINVAL;
7811da177e4SLinus Torvalds 
782da77005fSTrond Myklebust 	if (!cd->cache_parse)
783da77005fSTrond Myklebust 		goto out;
7841da177e4SLinus Torvalds 
785da77005fSTrond Myklebust 	mutex_lock(&inode->i_mutex);
786da77005fSTrond Myklebust 	ret = cache_downcall(mapping, buf, count, cd);
787da77005fSTrond Myklebust 	mutex_unlock(&inode->i_mutex);
788da77005fSTrond Myklebust out:
789da77005fSTrond Myklebust 	return ret;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
7931da177e4SLinus Torvalds 
794173912a6STrond Myklebust static unsigned int cache_poll(struct file *filp, poll_table *wait,
795173912a6STrond Myklebust 			       struct cache_detail *cd)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	unsigned int mask;
7981da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
7991da177e4SLinus Torvalds 	struct cache_queue *cq;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	poll_wait(filp, &queue_wait, wait);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* alway allow write */
8041da177e4SLinus Torvalds 	mask = POLL_OUT | POLLWRNORM;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	if (!rp)
8071da177e4SLinus Torvalds 		return mask;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	spin_lock(&queue_lock);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	for (cq= &rp->q; &cq->list != &cd->queue;
8121da177e4SLinus Torvalds 	     cq = list_entry(cq->list.next, struct cache_queue, list))
8131da177e4SLinus Torvalds 		if (!cq->reader) {
8141da177e4SLinus Torvalds 			mask |= POLLIN | POLLRDNORM;
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
8181da177e4SLinus Torvalds 	return mask;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
821173912a6STrond Myklebust static int cache_ioctl(struct inode *ino, struct file *filp,
822173912a6STrond Myklebust 		       unsigned int cmd, unsigned long arg,
823173912a6STrond Myklebust 		       struct cache_detail *cd)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	int len = 0;
8261da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
8271da177e4SLinus Torvalds 	struct cache_queue *cq;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	if (cmd != FIONREAD || !rp)
8301da177e4SLinus Torvalds 		return -EINVAL;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	spin_lock(&queue_lock);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	/* only find the length remaining in current request,
8351da177e4SLinus Torvalds 	 * or the length of the next request
8361da177e4SLinus Torvalds 	 */
8371da177e4SLinus Torvalds 	for (cq= &rp->q; &cq->list != &cd->queue;
8381da177e4SLinus Torvalds 	     cq = list_entry(cq->list.next, struct cache_queue, list))
8391da177e4SLinus Torvalds 		if (!cq->reader) {
8401da177e4SLinus Torvalds 			struct cache_request *cr =
8411da177e4SLinus Torvalds 				container_of(cq, struct cache_request, q);
8421da177e4SLinus Torvalds 			len = cr->len - rp->offset;
8431da177e4SLinus Torvalds 			break;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	return put_user(len, (int __user *)arg);
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
850173912a6STrond Myklebust static int cache_open(struct inode *inode, struct file *filp,
851173912a6STrond Myklebust 		      struct cache_detail *cd)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	struct cache_reader *rp = NULL;
8541da177e4SLinus Torvalds 
855f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
856f7e86ab9STrond Myklebust 		return -EACCES;
8571da177e4SLinus Torvalds 	nonseekable_open(inode, filp);
8581da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_READ) {
8591da177e4SLinus Torvalds 		rp = kmalloc(sizeof(*rp), GFP_KERNEL);
8601da177e4SLinus Torvalds 		if (!rp)
8611da177e4SLinus Torvalds 			return -ENOMEM;
8621da177e4SLinus Torvalds 		rp->offset = 0;
8631da177e4SLinus Torvalds 		rp->q.reader = 1;
8641da177e4SLinus Torvalds 		atomic_inc(&cd->readers);
8651da177e4SLinus Torvalds 		spin_lock(&queue_lock);
8661da177e4SLinus Torvalds 		list_add(&rp->q.list, &cd->queue);
8671da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 	filp->private_data = rp;
8701da177e4SLinus Torvalds 	return 0;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
873173912a6STrond Myklebust static int cache_release(struct inode *inode, struct file *filp,
874173912a6STrond Myklebust 			 struct cache_detail *cd)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	struct cache_reader *rp = filp->private_data;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	if (rp) {
8791da177e4SLinus Torvalds 		spin_lock(&queue_lock);
8801da177e4SLinus Torvalds 		if (rp->offset) {
8811da177e4SLinus Torvalds 			struct cache_queue *cq;
8821da177e4SLinus Torvalds 			for (cq= &rp->q; &cq->list != &cd->queue;
8831da177e4SLinus Torvalds 			     cq = list_entry(cq->list.next, struct cache_queue, list))
8841da177e4SLinus Torvalds 				if (!cq->reader) {
8851da177e4SLinus Torvalds 					container_of(cq, struct cache_request, q)
8861da177e4SLinus Torvalds 						->readers--;
8871da177e4SLinus Torvalds 					break;
8881da177e4SLinus Torvalds 				}
8891da177e4SLinus Torvalds 			rp->offset = 0;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds 		list_del(&rp->q.list);
8921da177e4SLinus Torvalds 		spin_unlock(&queue_lock);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		filp->private_data = NULL;
8951da177e4SLinus Torvalds 		kfree(rp);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 		cd->last_close = get_seconds();
8981da177e4SLinus Torvalds 		atomic_dec(&cd->readers);
8991da177e4SLinus Torvalds 	}
900f7e86ab9STrond Myklebust 	module_put(cd->owner);
9011da177e4SLinus Torvalds 	return 0;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 
906f866a819SNeilBrown static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	struct cache_queue *cq;
9091da177e4SLinus Torvalds 	spin_lock(&queue_lock);
9101da177e4SLinus Torvalds 	list_for_each_entry(cq, &detail->queue, list)
9111da177e4SLinus Torvalds 		if (!cq->reader) {
9121da177e4SLinus Torvalds 			struct cache_request *cr = container_of(cq, struct cache_request, q);
9131da177e4SLinus Torvalds 			if (cr->item != ch)
9141da177e4SLinus Torvalds 				continue;
9151da177e4SLinus Torvalds 			if (cr->readers != 0)
9164013edeaSNeilBrown 				continue;
9171da177e4SLinus Torvalds 			list_del(&cr->q.list);
9181da177e4SLinus Torvalds 			spin_unlock(&queue_lock);
919baab935fSNeilBrown 			cache_put(cr->item, detail);
9201da177e4SLinus Torvalds 			kfree(cr->buf);
9211da177e4SLinus Torvalds 			kfree(cr);
9221da177e4SLinus Torvalds 			return;
9231da177e4SLinus Torvalds 		}
9241da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds /*
9281da177e4SLinus Torvalds  * Support routines for text-based upcalls.
9291da177e4SLinus Torvalds  * Fields are separated by spaces.
9301da177e4SLinus Torvalds  * Fields are either mangled to quote space tab newline slosh with slosh
9311da177e4SLinus Torvalds  * or a hexified with a leading \x
9321da177e4SLinus Torvalds  * Record is terminated with newline.
9331da177e4SLinus Torvalds  *
9341da177e4SLinus Torvalds  */
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds void qword_add(char **bpp, int *lp, char *str)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	char *bp = *bpp;
9391da177e4SLinus Torvalds 	int len = *lp;
9401da177e4SLinus Torvalds 	char c;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	if (len < 0) return;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	while ((c=*str++) && len)
9451da177e4SLinus Torvalds 		switch(c) {
9461da177e4SLinus Torvalds 		case ' ':
9471da177e4SLinus Torvalds 		case '\t':
9481da177e4SLinus Torvalds 		case '\n':
9491da177e4SLinus Torvalds 		case '\\':
9501da177e4SLinus Torvalds 			if (len >= 4) {
9511da177e4SLinus Torvalds 				*bp++ = '\\';
9521da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0300)>>6);
9531da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0070)>>3);
9541da177e4SLinus Torvalds 				*bp++ = '0' + ((c & 0007)>>0);
9551da177e4SLinus Torvalds 			}
9561da177e4SLinus Torvalds 			len -= 4;
9571da177e4SLinus Torvalds 			break;
9581da177e4SLinus Torvalds 		default:
9591da177e4SLinus Torvalds 			*bp++ = c;
9601da177e4SLinus Torvalds 			len--;
9611da177e4SLinus Torvalds 		}
9621da177e4SLinus Torvalds 	if (c || len <1) len = -1;
9631da177e4SLinus Torvalds 	else {
9641da177e4SLinus Torvalds 		*bp++ = ' ';
9651da177e4SLinus Torvalds 		len--;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 	*bpp = bp;
9681da177e4SLinus Torvalds 	*lp = len;
9691da177e4SLinus Torvalds }
97024c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_add);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds void qword_addhex(char **bpp, int *lp, char *buf, int blen)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	char *bp = *bpp;
9751da177e4SLinus Torvalds 	int len = *lp;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if (len < 0) return;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	if (len > 2) {
9801da177e4SLinus Torvalds 		*bp++ = '\\';
9811da177e4SLinus Torvalds 		*bp++ = 'x';
9821da177e4SLinus Torvalds 		len -= 2;
9831da177e4SLinus Torvalds 		while (blen && len >= 2) {
9841da177e4SLinus Torvalds 			unsigned char c = *buf++;
9851da177e4SLinus Torvalds 			*bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
9861da177e4SLinus Torvalds 			*bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
9871da177e4SLinus Torvalds 			len -= 2;
9881da177e4SLinus Torvalds 			blen--;
9891da177e4SLinus Torvalds 		}
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 	if (blen || len<1) len = -1;
9921da177e4SLinus Torvalds 	else {
9931da177e4SLinus Torvalds 		*bp++ = ' ';
9941da177e4SLinus Torvalds 		len--;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	*bpp = bp;
9971da177e4SLinus Torvalds 	*lp = len;
9981da177e4SLinus Torvalds }
99924c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_addhex);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds static void warn_no_listener(struct cache_detail *detail)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	if (detail->last_warn != detail->last_close) {
10041da177e4SLinus Torvalds 		detail->last_warn = detail->last_close;
10051da177e4SLinus Torvalds 		if (detail->warn_no_listener)
10062da8ca26STrond Myklebust 			detail->warn_no_listener(detail, detail->last_close != 0);
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds /*
1011bc74b4f5STrond Myklebust  * register an upcall request to user-space and queue it up for read() by the
1012bc74b4f5STrond Myklebust  * upcall daemon.
1013bc74b4f5STrond Myklebust  *
10141da177e4SLinus Torvalds  * Each request is at most one page long.
10151da177e4SLinus Torvalds  */
1016bc74b4f5STrond Myklebust int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1017bc74b4f5STrond Myklebust 		void (*cache_request)(struct cache_detail *,
1018bc74b4f5STrond Myklebust 				      struct cache_head *,
1019bc74b4f5STrond Myklebust 				      char **,
1020bc74b4f5STrond Myklebust 				      int *))
10211da177e4SLinus Torvalds {
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	char *buf;
10241da177e4SLinus Torvalds 	struct cache_request *crq;
10251da177e4SLinus Torvalds 	char *bp;
10261da177e4SLinus Torvalds 	int len;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	if (atomic_read(&detail->readers) == 0 &&
10291da177e4SLinus Torvalds 	    detail->last_close < get_seconds() - 30) {
10301da177e4SLinus Torvalds 			warn_no_listener(detail);
10311da177e4SLinus Torvalds 			return -EINVAL;
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
10351da177e4SLinus Torvalds 	if (!buf)
10361da177e4SLinus Torvalds 		return -EAGAIN;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	crq = kmalloc(sizeof (*crq), GFP_KERNEL);
10391da177e4SLinus Torvalds 	if (!crq) {
10401da177e4SLinus Torvalds 		kfree(buf);
10411da177e4SLinus Torvalds 		return -EAGAIN;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	bp = buf; len = PAGE_SIZE;
10451da177e4SLinus Torvalds 
1046bc74b4f5STrond Myklebust 	cache_request(detail, h, &bp, &len);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	if (len < 0) {
10491da177e4SLinus Torvalds 		kfree(buf);
10501da177e4SLinus Torvalds 		kfree(crq);
10511da177e4SLinus Torvalds 		return -EAGAIN;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 	crq->q.reader = 0;
10541da177e4SLinus Torvalds 	crq->item = cache_get(h);
10551da177e4SLinus Torvalds 	crq->buf = buf;
10561da177e4SLinus Torvalds 	crq->len = PAGE_SIZE - len;
10571da177e4SLinus Torvalds 	crq->readers = 0;
10581da177e4SLinus Torvalds 	spin_lock(&queue_lock);
10591da177e4SLinus Torvalds 	list_add_tail(&crq->q.list, &detail->queue);
10601da177e4SLinus Torvalds 	spin_unlock(&queue_lock);
10611da177e4SLinus Torvalds 	wake_up(&queue_wait);
10621da177e4SLinus Torvalds 	return 0;
10631da177e4SLinus Torvalds }
1064bc74b4f5STrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds /*
10671da177e4SLinus Torvalds  * parse a message from user-space and pass it
10681da177e4SLinus Torvalds  * to an appropriate cache
10691da177e4SLinus Torvalds  * Messages are, like requests, separated into fields by
10701da177e4SLinus Torvalds  * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
10711da177e4SLinus Torvalds  *
10721da177e4SLinus Torvalds  * Message is
10731da177e4SLinus Torvalds  *   reply cachename expiry key ... content....
10741da177e4SLinus Torvalds  *
10751da177e4SLinus Torvalds  * key and content are both parsed by cache
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds #define isodigit(c) (isdigit(c) && c <= '7')
10791da177e4SLinus Torvalds int qword_get(char **bpp, char *dest, int bufsize)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	/* return bytes copied, or -1 on error */
10821da177e4SLinus Torvalds 	char *bp = *bpp;
10831da177e4SLinus Torvalds 	int len = 0;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	while (*bp == ' ') bp++;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	if (bp[0] == '\\' && bp[1] == 'x') {
10881da177e4SLinus Torvalds 		/* HEX STRING */
10891da177e4SLinus Torvalds 		bp += 2;
10901da177e4SLinus Torvalds 		while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
10911da177e4SLinus Torvalds 			int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
10921da177e4SLinus Torvalds 			bp++;
10931da177e4SLinus Torvalds 			byte <<= 4;
10941da177e4SLinus Torvalds 			byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
10951da177e4SLinus Torvalds 			*dest++ = byte;
10961da177e4SLinus Torvalds 			bp++;
10971da177e4SLinus Torvalds 			len++;
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 	} else {
11001da177e4SLinus Torvalds 		/* text with \nnn octal quoting */
11011da177e4SLinus Torvalds 		while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
11021da177e4SLinus Torvalds 			if (*bp == '\\' &&
11031da177e4SLinus Torvalds 			    isodigit(bp[1]) && (bp[1] <= '3') &&
11041da177e4SLinus Torvalds 			    isodigit(bp[2]) &&
11051da177e4SLinus Torvalds 			    isodigit(bp[3])) {
11061da177e4SLinus Torvalds 				int byte = (*++bp -'0');
11071da177e4SLinus Torvalds 				bp++;
11081da177e4SLinus Torvalds 				byte = (byte << 3) | (*bp++ - '0');
11091da177e4SLinus Torvalds 				byte = (byte << 3) | (*bp++ - '0');
11101da177e4SLinus Torvalds 				*dest++ = byte;
11111da177e4SLinus Torvalds 				len++;
11121da177e4SLinus Torvalds 			} else {
11131da177e4SLinus Torvalds 				*dest++ = *bp++;
11141da177e4SLinus Torvalds 				len++;
11151da177e4SLinus Torvalds 			}
11161da177e4SLinus Torvalds 		}
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	if (*bp != ' ' && *bp != '\n' && *bp != '\0')
11201da177e4SLinus Torvalds 		return -1;
11211da177e4SLinus Torvalds 	while (*bp == ' ') bp++;
11221da177e4SLinus Torvalds 	*bpp = bp;
11231da177e4SLinus Torvalds 	*dest = '\0';
11241da177e4SLinus Torvalds 	return len;
11251da177e4SLinus Torvalds }
112624c3767eSTrond Myklebust EXPORT_SYMBOL_GPL(qword_get);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds /*
11301da177e4SLinus Torvalds  * support /proc/sunrpc/cache/$CACHENAME/content
11311da177e4SLinus Torvalds  * as a seqfile.
11321da177e4SLinus Torvalds  * We call ->cache_show passing NULL for the item to
11331da177e4SLinus Torvalds  * get a header, then pass each real item in the cache
11341da177e4SLinus Torvalds  */
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds struct handle {
11371da177e4SLinus Torvalds 	struct cache_detail *cd;
11381da177e4SLinus Torvalds };
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds static void *c_start(struct seq_file *m, loff_t *pos)
11419a429c49SEric Dumazet 	__acquires(cd->hash_lock)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	loff_t n = *pos;
11441da177e4SLinus Torvalds 	unsigned hash, entry;
11451da177e4SLinus Torvalds 	struct cache_head *ch;
11461da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	read_lock(&cd->hash_lock);
11501da177e4SLinus Torvalds 	if (!n--)
11511da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
11521da177e4SLinus Torvalds 	hash = n >> 32;
11531da177e4SLinus Torvalds 	entry = n & ((1LL<<32) - 1);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	for (ch=cd->hash_table[hash]; ch; ch=ch->next)
11561da177e4SLinus Torvalds 		if (!entry--)
11571da177e4SLinus Torvalds 			return ch;
11581da177e4SLinus Torvalds 	n &= ~((1LL<<32) - 1);
11591da177e4SLinus Torvalds 	do {
11601da177e4SLinus Torvalds 		hash++;
11611da177e4SLinus Torvalds 		n += 1LL<<32;
11621da177e4SLinus Torvalds 	} while(hash < cd->hash_size &&
11631da177e4SLinus Torvalds 		cd->hash_table[hash]==NULL);
11641da177e4SLinus Torvalds 	if (hash >= cd->hash_size)
11651da177e4SLinus Torvalds 		return NULL;
11661da177e4SLinus Torvalds 	*pos = n+1;
11671da177e4SLinus Torvalds 	return cd->hash_table[hash];
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds static void *c_next(struct seq_file *m, void *p, loff_t *pos)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	struct cache_head *ch = p;
11731da177e4SLinus Torvalds 	int hash = (*pos >> 32);
11741da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (p == SEQ_START_TOKEN)
11771da177e4SLinus Torvalds 		hash = 0;
11781da177e4SLinus Torvalds 	else if (ch->next == NULL) {
11791da177e4SLinus Torvalds 		hash++;
11801da177e4SLinus Torvalds 		*pos += 1LL<<32;
11811da177e4SLinus Torvalds 	} else {
11821da177e4SLinus Torvalds 		++*pos;
11831da177e4SLinus Torvalds 		return ch->next;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 	*pos &= ~((1LL<<32) - 1);
11861da177e4SLinus Torvalds 	while (hash < cd->hash_size &&
11871da177e4SLinus Torvalds 	       cd->hash_table[hash] == NULL) {
11881da177e4SLinus Torvalds 		hash++;
11891da177e4SLinus Torvalds 		*pos += 1LL<<32;
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	if (hash >= cd->hash_size)
11921da177e4SLinus Torvalds 		return NULL;
11931da177e4SLinus Torvalds 	++*pos;
11941da177e4SLinus Torvalds 	return cd->hash_table[hash];
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds static void c_stop(struct seq_file *m, void *p)
11989a429c49SEric Dumazet 	__releases(cd->hash_lock)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
12011da177e4SLinus Torvalds 	read_unlock(&cd->hash_lock);
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds static int c_show(struct seq_file *m, void *p)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	struct cache_head *cp = p;
12071da177e4SLinus Torvalds 	struct cache_detail *cd = ((struct handle*)m->private)->cd;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	if (p == SEQ_START_TOKEN)
12101da177e4SLinus Torvalds 		return cd->cache_show(m, cd, NULL);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	ifdebug(CACHE)
12134013edeaSNeilBrown 		seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1214baab935fSNeilBrown 			   cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
12151da177e4SLinus Torvalds 	cache_get(cp);
12161da177e4SLinus Torvalds 	if (cache_check(cd, cp, NULL))
12171da177e4SLinus Torvalds 		/* cache_check does a cache_put on failure */
12181da177e4SLinus Torvalds 		seq_printf(m, "# ");
12191da177e4SLinus Torvalds 	else
12201da177e4SLinus Torvalds 		cache_put(cp, cd);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	return cd->cache_show(m, cd, cp);
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
122556b3d975SPhilippe De Muyter static const struct seq_operations cache_content_op = {
12261da177e4SLinus Torvalds 	.start	= c_start,
12271da177e4SLinus Torvalds 	.next	= c_next,
12281da177e4SLinus Torvalds 	.stop	= c_stop,
12291da177e4SLinus Torvalds 	.show	= c_show,
12301da177e4SLinus Torvalds };
12311da177e4SLinus Torvalds 
1232173912a6STrond Myklebust static int content_open(struct inode *inode, struct file *file,
1233173912a6STrond Myklebust 			struct cache_detail *cd)
12341da177e4SLinus Torvalds {
12351da177e4SLinus Torvalds 	struct handle *han;
12361da177e4SLinus Torvalds 
1237f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
1238f7e86ab9STrond Myklebust 		return -EACCES;
1239ec931035SPavel Emelyanov 	han = __seq_open_private(file, &cache_content_op, sizeof(*han));
12401da177e4SLinus Torvalds 	if (han == NULL)
12411da177e4SLinus Torvalds 		return -ENOMEM;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	han->cd = cd;
1244ec931035SPavel Emelyanov 	return 0;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
1247f7e86ab9STrond Myklebust static int content_release(struct inode *inode, struct file *file,
1248f7e86ab9STrond Myklebust 		struct cache_detail *cd)
1249f7e86ab9STrond Myklebust {
1250f7e86ab9STrond Myklebust 	int ret = seq_release_private(inode, file);
1251f7e86ab9STrond Myklebust 	module_put(cd->owner);
1252f7e86ab9STrond Myklebust 	return ret;
1253f7e86ab9STrond Myklebust }
1254f7e86ab9STrond Myklebust 
1255f7e86ab9STrond Myklebust static int open_flush(struct inode *inode, struct file *file,
1256f7e86ab9STrond Myklebust 			struct cache_detail *cd)
1257f7e86ab9STrond Myklebust {
1258f7e86ab9STrond Myklebust 	if (!cd || !try_module_get(cd->owner))
1259f7e86ab9STrond Myklebust 		return -EACCES;
1260f7e86ab9STrond Myklebust 	return nonseekable_open(inode, file);
1261f7e86ab9STrond Myklebust }
1262f7e86ab9STrond Myklebust 
1263f7e86ab9STrond Myklebust static int release_flush(struct inode *inode, struct file *file,
1264f7e86ab9STrond Myklebust 			struct cache_detail *cd)
1265f7e86ab9STrond Myklebust {
1266f7e86ab9STrond Myklebust 	module_put(cd->owner);
1267f7e86ab9STrond Myklebust 	return 0;
1268f7e86ab9STrond Myklebust }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds static ssize_t read_flush(struct file *file, char __user *buf,
1271173912a6STrond Myklebust 			  size_t count, loff_t *ppos,
1272173912a6STrond Myklebust 			  struct cache_detail *cd)
12731da177e4SLinus Torvalds {
12741da177e4SLinus Torvalds 	char tbuf[20];
12751da177e4SLinus Torvalds 	unsigned long p = *ppos;
127601b2969aSChuck Lever 	size_t len;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	sprintf(tbuf, "%lu\n", cd->flush_time);
12791da177e4SLinus Torvalds 	len = strlen(tbuf);
12801da177e4SLinus Torvalds 	if (p >= len)
12811da177e4SLinus Torvalds 		return 0;
12821da177e4SLinus Torvalds 	len -= p;
128301b2969aSChuck Lever 	if (len > count)
128401b2969aSChuck Lever 		len = count;
12851da177e4SLinus Torvalds 	if (copy_to_user(buf, (void*)(tbuf+p), len))
128601b2969aSChuck Lever 		return -EFAULT;
12871da177e4SLinus Torvalds 	*ppos += len;
12881da177e4SLinus Torvalds 	return len;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds static ssize_t write_flush(struct file *file, const char __user *buf,
1292173912a6STrond Myklebust 			   size_t count, loff_t *ppos,
1293173912a6STrond Myklebust 			   struct cache_detail *cd)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	char tbuf[20];
12961da177e4SLinus Torvalds 	char *ep;
12971da177e4SLinus Torvalds 	long flushtime;
12981da177e4SLinus Torvalds 	if (*ppos || count > sizeof(tbuf)-1)
12991da177e4SLinus Torvalds 		return -EINVAL;
13001da177e4SLinus Torvalds 	if (copy_from_user(tbuf, buf, count))
13011da177e4SLinus Torvalds 		return -EFAULT;
13021da177e4SLinus Torvalds 	tbuf[count] = 0;
13031da177e4SLinus Torvalds 	flushtime = simple_strtoul(tbuf, &ep, 0);
13041da177e4SLinus Torvalds 	if (*ep && *ep != '\n')
13051da177e4SLinus Torvalds 		return -EINVAL;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	cd->flush_time = flushtime;
13081da177e4SLinus Torvalds 	cd->nextcheck = get_seconds();
13091da177e4SLinus Torvalds 	cache_flush();
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	*ppos += count;
13121da177e4SLinus Torvalds 	return count;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
1315173912a6STrond Myklebust static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1316173912a6STrond Myklebust 				 size_t count, loff_t *ppos)
1317173912a6STrond Myklebust {
1318173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1319173912a6STrond Myklebust 
1320173912a6STrond Myklebust 	return cache_read(filp, buf, count, ppos, cd);
1321173912a6STrond Myklebust }
1322173912a6STrond Myklebust 
1323173912a6STrond Myklebust static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1324173912a6STrond Myklebust 				  size_t count, loff_t *ppos)
1325173912a6STrond Myklebust {
1326173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1327173912a6STrond Myklebust 
1328173912a6STrond Myklebust 	return cache_write(filp, buf, count, ppos, cd);
1329173912a6STrond Myklebust }
1330173912a6STrond Myklebust 
1331173912a6STrond Myklebust static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1332173912a6STrond Myklebust {
1333173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1334173912a6STrond Myklebust 
1335173912a6STrond Myklebust 	return cache_poll(filp, wait, cd);
1336173912a6STrond Myklebust }
1337173912a6STrond Myklebust 
1338173912a6STrond Myklebust static int cache_ioctl_procfs(struct inode *inode, struct file *filp,
1339173912a6STrond Myklebust 			      unsigned int cmd, unsigned long arg)
1340173912a6STrond Myklebust {
1341173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1342173912a6STrond Myklebust 
1343173912a6STrond Myklebust 	return cache_ioctl(inode, filp, cmd, arg, cd);
1344173912a6STrond Myklebust }
1345173912a6STrond Myklebust 
1346173912a6STrond Myklebust static int cache_open_procfs(struct inode *inode, struct file *filp)
1347173912a6STrond Myklebust {
1348173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1349173912a6STrond Myklebust 
1350173912a6STrond Myklebust 	return cache_open(inode, filp, cd);
1351173912a6STrond Myklebust }
1352173912a6STrond Myklebust 
1353173912a6STrond Myklebust static int cache_release_procfs(struct inode *inode, struct file *filp)
1354173912a6STrond Myklebust {
1355173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1356173912a6STrond Myklebust 
1357173912a6STrond Myklebust 	return cache_release(inode, filp, cd);
1358173912a6STrond Myklebust }
1359173912a6STrond Myklebust 
1360173912a6STrond Myklebust static const struct file_operations cache_file_operations_procfs = {
1361173912a6STrond Myklebust 	.owner		= THIS_MODULE,
1362173912a6STrond Myklebust 	.llseek		= no_llseek,
1363173912a6STrond Myklebust 	.read		= cache_read_procfs,
1364173912a6STrond Myklebust 	.write		= cache_write_procfs,
1365173912a6STrond Myklebust 	.poll		= cache_poll_procfs,
1366173912a6STrond Myklebust 	.ioctl		= cache_ioctl_procfs, /* for FIONREAD */
1367173912a6STrond Myklebust 	.open		= cache_open_procfs,
1368173912a6STrond Myklebust 	.release	= cache_release_procfs,
13691da177e4SLinus Torvalds };
1370173912a6STrond Myklebust 
1371173912a6STrond Myklebust static int content_open_procfs(struct inode *inode, struct file *filp)
1372173912a6STrond Myklebust {
1373173912a6STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1374173912a6STrond Myklebust 
1375173912a6STrond Myklebust 	return content_open(inode, filp, cd);
1376173912a6STrond Myklebust }
1377173912a6STrond Myklebust 
1378f7e86ab9STrond Myklebust static int content_release_procfs(struct inode *inode, struct file *filp)
1379f7e86ab9STrond Myklebust {
1380f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1381f7e86ab9STrond Myklebust 
1382f7e86ab9STrond Myklebust 	return content_release(inode, filp, cd);
1383f7e86ab9STrond Myklebust }
1384f7e86ab9STrond Myklebust 
1385173912a6STrond Myklebust static const struct file_operations content_file_operations_procfs = {
1386173912a6STrond Myklebust 	.open		= content_open_procfs,
1387173912a6STrond Myklebust 	.read		= seq_read,
1388173912a6STrond Myklebust 	.llseek		= seq_lseek,
1389f7e86ab9STrond Myklebust 	.release	= content_release_procfs,
1390173912a6STrond Myklebust };
1391173912a6STrond Myklebust 
1392f7e86ab9STrond Myklebust static int open_flush_procfs(struct inode *inode, struct file *filp)
1393f7e86ab9STrond Myklebust {
1394f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1395f7e86ab9STrond Myklebust 
1396f7e86ab9STrond Myklebust 	return open_flush(inode, filp, cd);
1397f7e86ab9STrond Myklebust }
1398f7e86ab9STrond Myklebust 
1399f7e86ab9STrond Myklebust static int release_flush_procfs(struct inode *inode, struct file *filp)
1400f7e86ab9STrond Myklebust {
1401f7e86ab9STrond Myklebust 	struct cache_detail *cd = PDE(inode)->data;
1402f7e86ab9STrond Myklebust 
1403f7e86ab9STrond Myklebust 	return release_flush(inode, filp, cd);
1404f7e86ab9STrond Myklebust }
1405f7e86ab9STrond Myklebust 
1406173912a6STrond Myklebust static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1407173912a6STrond Myklebust 			    size_t count, loff_t *ppos)
1408173912a6STrond Myklebust {
1409173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1410173912a6STrond Myklebust 
1411173912a6STrond Myklebust 	return read_flush(filp, buf, count, ppos, cd);
1412173912a6STrond Myklebust }
1413173912a6STrond Myklebust 
1414173912a6STrond Myklebust static ssize_t write_flush_procfs(struct file *filp,
1415173912a6STrond Myklebust 				  const char __user *buf,
1416173912a6STrond Myklebust 				  size_t count, loff_t *ppos)
1417173912a6STrond Myklebust {
1418173912a6STrond Myklebust 	struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1419173912a6STrond Myklebust 
1420173912a6STrond Myklebust 	return write_flush(filp, buf, count, ppos, cd);
1421173912a6STrond Myklebust }
1422173912a6STrond Myklebust 
1423173912a6STrond Myklebust static const struct file_operations cache_flush_operations_procfs = {
1424f7e86ab9STrond Myklebust 	.open		= open_flush_procfs,
1425173912a6STrond Myklebust 	.read		= read_flush_procfs,
1426173912a6STrond Myklebust 	.write		= write_flush_procfs,
1427f7e86ab9STrond Myklebust 	.release	= release_flush_procfs,
1428173912a6STrond Myklebust };
1429173912a6STrond Myklebust 
1430173912a6STrond Myklebust static void remove_cache_proc_entries(struct cache_detail *cd)
1431173912a6STrond Myklebust {
1432173912a6STrond Myklebust 	if (cd->u.procfs.proc_ent == NULL)
1433173912a6STrond Myklebust 		return;
1434173912a6STrond Myklebust 	if (cd->u.procfs.flush_ent)
1435173912a6STrond Myklebust 		remove_proc_entry("flush", cd->u.procfs.proc_ent);
1436173912a6STrond Myklebust 	if (cd->u.procfs.channel_ent)
1437173912a6STrond Myklebust 		remove_proc_entry("channel", cd->u.procfs.proc_ent);
1438173912a6STrond Myklebust 	if (cd->u.procfs.content_ent)
1439173912a6STrond Myklebust 		remove_proc_entry("content", cd->u.procfs.proc_ent);
1440173912a6STrond Myklebust 	cd->u.procfs.proc_ent = NULL;
1441173912a6STrond Myklebust 	remove_proc_entry(cd->name, proc_net_rpc);
1442173912a6STrond Myklebust }
1443173912a6STrond Myklebust 
1444173912a6STrond Myklebust #ifdef CONFIG_PROC_FS
1445173912a6STrond Myklebust static int create_cache_proc_entries(struct cache_detail *cd)
1446173912a6STrond Myklebust {
1447173912a6STrond Myklebust 	struct proc_dir_entry *p;
1448173912a6STrond Myklebust 
1449173912a6STrond Myklebust 	cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1450173912a6STrond Myklebust 	if (cd->u.procfs.proc_ent == NULL)
1451173912a6STrond Myklebust 		goto out_nomem;
1452173912a6STrond Myklebust 	cd->u.procfs.channel_ent = NULL;
1453173912a6STrond Myklebust 	cd->u.procfs.content_ent = NULL;
1454173912a6STrond Myklebust 
1455173912a6STrond Myklebust 	p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1456173912a6STrond Myklebust 			     cd->u.procfs.proc_ent,
1457173912a6STrond Myklebust 			     &cache_flush_operations_procfs, cd);
1458173912a6STrond Myklebust 	cd->u.procfs.flush_ent = p;
1459173912a6STrond Myklebust 	if (p == NULL)
1460173912a6STrond Myklebust 		goto out_nomem;
1461173912a6STrond Myklebust 
1462173912a6STrond Myklebust 	if (cd->cache_upcall || cd->cache_parse) {
1463173912a6STrond Myklebust 		p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1464173912a6STrond Myklebust 				     cd->u.procfs.proc_ent,
1465173912a6STrond Myklebust 				     &cache_file_operations_procfs, cd);
1466173912a6STrond Myklebust 		cd->u.procfs.channel_ent = p;
1467173912a6STrond Myklebust 		if (p == NULL)
1468173912a6STrond Myklebust 			goto out_nomem;
1469173912a6STrond Myklebust 	}
1470173912a6STrond Myklebust 	if (cd->cache_show) {
1471173912a6STrond Myklebust 		p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1472173912a6STrond Myklebust 				cd->u.procfs.proc_ent,
1473173912a6STrond Myklebust 				&content_file_operations_procfs, cd);
1474173912a6STrond Myklebust 		cd->u.procfs.content_ent = p;
1475173912a6STrond Myklebust 		if (p == NULL)
1476173912a6STrond Myklebust 			goto out_nomem;
1477173912a6STrond Myklebust 	}
1478173912a6STrond Myklebust 	return 0;
1479173912a6STrond Myklebust out_nomem:
1480173912a6STrond Myklebust 	remove_cache_proc_entries(cd);
1481173912a6STrond Myklebust 	return -ENOMEM;
1482173912a6STrond Myklebust }
1483173912a6STrond Myklebust #else /* CONFIG_PROC_FS */
1484173912a6STrond Myklebust static int create_cache_proc_entries(struct cache_detail *cd)
1485173912a6STrond Myklebust {
1486173912a6STrond Myklebust 	return 0;
1487173912a6STrond Myklebust }
1488173912a6STrond Myklebust #endif
1489173912a6STrond Myklebust 
1490173912a6STrond Myklebust int cache_register(struct cache_detail *cd)
1491173912a6STrond Myklebust {
1492173912a6STrond Myklebust 	int ret;
1493173912a6STrond Myklebust 
1494173912a6STrond Myklebust 	sunrpc_init_cache_detail(cd);
1495173912a6STrond Myklebust 	ret = create_cache_proc_entries(cd);
1496173912a6STrond Myklebust 	if (ret)
1497173912a6STrond Myklebust 		sunrpc_destroy_cache_detail(cd);
1498173912a6STrond Myklebust 	return ret;
1499173912a6STrond Myklebust }
1500173912a6STrond Myklebust EXPORT_SYMBOL_GPL(cache_register);
1501173912a6STrond Myklebust 
1502173912a6STrond Myklebust void cache_unregister(struct cache_detail *cd)
1503173912a6STrond Myklebust {
1504173912a6STrond Myklebust 	remove_cache_proc_entries(cd);
1505173912a6STrond Myklebust 	sunrpc_destroy_cache_detail(cd);
1506173912a6STrond Myklebust }
1507173912a6STrond Myklebust EXPORT_SYMBOL_GPL(cache_unregister);
15088854e82dSTrond Myklebust 
15098854e82dSTrond Myklebust static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
15108854e82dSTrond Myklebust 				 size_t count, loff_t *ppos)
15118854e82dSTrond Myklebust {
15128854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
15138854e82dSTrond Myklebust 
15148854e82dSTrond Myklebust 	return cache_read(filp, buf, count, ppos, cd);
15158854e82dSTrond Myklebust }
15168854e82dSTrond Myklebust 
15178854e82dSTrond Myklebust static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
15188854e82dSTrond Myklebust 				  size_t count, loff_t *ppos)
15198854e82dSTrond Myklebust {
15208854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
15218854e82dSTrond Myklebust 
15228854e82dSTrond Myklebust 	return cache_write(filp, buf, count, ppos, cd);
15238854e82dSTrond Myklebust }
15248854e82dSTrond Myklebust 
15258854e82dSTrond Myklebust static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
15268854e82dSTrond Myklebust {
15278854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
15288854e82dSTrond Myklebust 
15298854e82dSTrond Myklebust 	return cache_poll(filp, wait, cd);
15308854e82dSTrond Myklebust }
15318854e82dSTrond Myklebust 
15328854e82dSTrond Myklebust static int cache_ioctl_pipefs(struct inode *inode, struct file *filp,
15338854e82dSTrond Myklebust 			      unsigned int cmd, unsigned long arg)
15348854e82dSTrond Myklebust {
15358854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
15368854e82dSTrond Myklebust 
15378854e82dSTrond Myklebust 	return cache_ioctl(inode, filp, cmd, arg, cd);
15388854e82dSTrond Myklebust }
15398854e82dSTrond Myklebust 
15408854e82dSTrond Myklebust static int cache_open_pipefs(struct inode *inode, struct file *filp)
15418854e82dSTrond Myklebust {
15428854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
15438854e82dSTrond Myklebust 
15448854e82dSTrond Myklebust 	return cache_open(inode, filp, cd);
15458854e82dSTrond Myklebust }
15468854e82dSTrond Myklebust 
15478854e82dSTrond Myklebust static int cache_release_pipefs(struct inode *inode, struct file *filp)
15488854e82dSTrond Myklebust {
15498854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
15508854e82dSTrond Myklebust 
15518854e82dSTrond Myklebust 	return cache_release(inode, filp, cd);
15528854e82dSTrond Myklebust }
15538854e82dSTrond Myklebust 
15548854e82dSTrond Myklebust const struct file_operations cache_file_operations_pipefs = {
15558854e82dSTrond Myklebust 	.owner		= THIS_MODULE,
15568854e82dSTrond Myklebust 	.llseek		= no_llseek,
15578854e82dSTrond Myklebust 	.read		= cache_read_pipefs,
15588854e82dSTrond Myklebust 	.write		= cache_write_pipefs,
15598854e82dSTrond Myklebust 	.poll		= cache_poll_pipefs,
15608854e82dSTrond Myklebust 	.ioctl		= cache_ioctl_pipefs, /* for FIONREAD */
15618854e82dSTrond Myklebust 	.open		= cache_open_pipefs,
15628854e82dSTrond Myklebust 	.release	= cache_release_pipefs,
15638854e82dSTrond Myklebust };
15648854e82dSTrond Myklebust 
15658854e82dSTrond Myklebust static int content_open_pipefs(struct inode *inode, struct file *filp)
15668854e82dSTrond Myklebust {
15678854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
15688854e82dSTrond Myklebust 
15698854e82dSTrond Myklebust 	return content_open(inode, filp, cd);
15708854e82dSTrond Myklebust }
15718854e82dSTrond Myklebust 
1572f7e86ab9STrond Myklebust static int content_release_pipefs(struct inode *inode, struct file *filp)
1573f7e86ab9STrond Myklebust {
1574f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1575f7e86ab9STrond Myklebust 
1576f7e86ab9STrond Myklebust 	return content_release(inode, filp, cd);
1577f7e86ab9STrond Myklebust }
1578f7e86ab9STrond Myklebust 
15798854e82dSTrond Myklebust const struct file_operations content_file_operations_pipefs = {
15808854e82dSTrond Myklebust 	.open		= content_open_pipefs,
15818854e82dSTrond Myklebust 	.read		= seq_read,
15828854e82dSTrond Myklebust 	.llseek		= seq_lseek,
1583f7e86ab9STrond Myklebust 	.release	= content_release_pipefs,
15848854e82dSTrond Myklebust };
15858854e82dSTrond Myklebust 
1586f7e86ab9STrond Myklebust static int open_flush_pipefs(struct inode *inode, struct file *filp)
1587f7e86ab9STrond Myklebust {
1588f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1589f7e86ab9STrond Myklebust 
1590f7e86ab9STrond Myklebust 	return open_flush(inode, filp, cd);
1591f7e86ab9STrond Myklebust }
1592f7e86ab9STrond Myklebust 
1593f7e86ab9STrond Myklebust static int release_flush_pipefs(struct inode *inode, struct file *filp)
1594f7e86ab9STrond Myklebust {
1595f7e86ab9STrond Myklebust 	struct cache_detail *cd = RPC_I(inode)->private;
1596f7e86ab9STrond Myklebust 
1597f7e86ab9STrond Myklebust 	return release_flush(inode, filp, cd);
1598f7e86ab9STrond Myklebust }
1599f7e86ab9STrond Myklebust 
16008854e82dSTrond Myklebust static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
16018854e82dSTrond Myklebust 			    size_t count, loff_t *ppos)
16028854e82dSTrond Myklebust {
16038854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
16048854e82dSTrond Myklebust 
16058854e82dSTrond Myklebust 	return read_flush(filp, buf, count, ppos, cd);
16068854e82dSTrond Myklebust }
16078854e82dSTrond Myklebust 
16088854e82dSTrond Myklebust static ssize_t write_flush_pipefs(struct file *filp,
16098854e82dSTrond Myklebust 				  const char __user *buf,
16108854e82dSTrond Myklebust 				  size_t count, loff_t *ppos)
16118854e82dSTrond Myklebust {
16128854e82dSTrond Myklebust 	struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
16138854e82dSTrond Myklebust 
16148854e82dSTrond Myklebust 	return write_flush(filp, buf, count, ppos, cd);
16158854e82dSTrond Myklebust }
16168854e82dSTrond Myklebust 
16178854e82dSTrond Myklebust const struct file_operations cache_flush_operations_pipefs = {
1618f7e86ab9STrond Myklebust 	.open		= open_flush_pipefs,
16198854e82dSTrond Myklebust 	.read		= read_flush_pipefs,
16208854e82dSTrond Myklebust 	.write		= write_flush_pipefs,
1621f7e86ab9STrond Myklebust 	.release	= release_flush_pipefs,
16228854e82dSTrond Myklebust };
16238854e82dSTrond Myklebust 
16248854e82dSTrond Myklebust int sunrpc_cache_register_pipefs(struct dentry *parent,
16258854e82dSTrond Myklebust 				 const char *name, mode_t umode,
16268854e82dSTrond Myklebust 				 struct cache_detail *cd)
16278854e82dSTrond Myklebust {
16288854e82dSTrond Myklebust 	struct qstr q;
16298854e82dSTrond Myklebust 	struct dentry *dir;
16308854e82dSTrond Myklebust 	int ret = 0;
16318854e82dSTrond Myklebust 
16328854e82dSTrond Myklebust 	sunrpc_init_cache_detail(cd);
16338854e82dSTrond Myklebust 	q.name = name;
16348854e82dSTrond Myklebust 	q.len = strlen(name);
16358854e82dSTrond Myklebust 	q.hash = full_name_hash(q.name, q.len);
16368854e82dSTrond Myklebust 	dir = rpc_create_cache_dir(parent, &q, umode, cd);
16378854e82dSTrond Myklebust 	if (!IS_ERR(dir))
16388854e82dSTrond Myklebust 		cd->u.pipefs.dir = dir;
16398854e82dSTrond Myklebust 	else {
16408854e82dSTrond Myklebust 		sunrpc_destroy_cache_detail(cd);
16418854e82dSTrond Myklebust 		ret = PTR_ERR(dir);
16428854e82dSTrond Myklebust 	}
16438854e82dSTrond Myklebust 	return ret;
16448854e82dSTrond Myklebust }
16458854e82dSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
16468854e82dSTrond Myklebust 
16478854e82dSTrond Myklebust void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
16488854e82dSTrond Myklebust {
16498854e82dSTrond Myklebust 	rpc_remove_cache_dir(cd->u.pipefs.dir);
16508854e82dSTrond Myklebust 	cd->u.pipefs.dir = NULL;
16518854e82dSTrond Myklebust 	sunrpc_destroy_cache_detail(cd);
16528854e82dSTrond Myklebust }
16538854e82dSTrond Myklebust EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
16548854e82dSTrond Myklebust 
1655