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