~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sunrpc/cache.c

Version: ~ [ linux-5.19-rc3 ] ~ [ linux-5.18.5 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.48 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.123 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.199 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.248 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.284 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.319 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp