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