cache.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * include/linux/sunrpc/cache.h
  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. #ifndef _LINUX_SUNRPC_CACHE_H_
  13. #define _LINUX_SUNRPC_CACHE_H_
  14. #include <linux/slab.h>
  15. #include <asm/atomic.h>
  16. #include <linux/proc_fs.h>
  17. /*
  18. * Each cache requires:
  19. * - A 'struct cache_detail' which contains information specific to the cache
  20. * for common code to use.
  21. * - An item structure that must contain a "struct cache_head"
  22. * - A lookup function defined using DefineCacheLookup
  23. * - A 'put' function that can release a cache item. It will only
  24. * be called after cache_put has succeed, so there are guarantee
  25. * to be no references.
  26. * - A function to calculate a hash of an item's key.
  27. *
  28. * as well as assorted code fragments (e.g. compare keys) and numbers
  29. * (e.g. hash size, goal_age, etc).
  30. *
  31. * Each cache must be registered so that it can be cleaned regularly.
  32. * When the cache is unregistered, it is flushed completely.
  33. *
  34. * Entries have a ref count and a 'hashed' flag which counts the existance
  35. * in the hash table.
  36. * We only expire entries when refcount is zero.
  37. * Existance in the cache is counted the refcount.
  38. */
  39. /* Every cache item has a common header that is used
  40. * for expiring and refreshing entries.
  41. *
  42. */
  43. struct cache_head {
  44. struct cache_head * next;
  45. time_t expiry_time; /* After time time, don't use the data */
  46. time_t last_refresh; /* If CACHE_PENDING, this is when upcall
  47. * was sent, else this is when update was received
  48. */
  49. struct kref ref;
  50. unsigned long flags;
  51. };
  52. #define CACHE_VALID 0 /* Entry contains valid data */
  53. #define CACHE_NEGATIVE 1 /* Negative entry - there is no match for the key */
  54. #define CACHE_PENDING 2 /* An upcall has been sent but no reply received yet*/
  55. #define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */
  56. struct cache_detail {
  57. struct module * owner;
  58. int hash_size;
  59. struct cache_head ** hash_table;
  60. rwlock_t hash_lock;
  61. atomic_t inuse; /* active user-space update or lookup */
  62. char *name;
  63. void (*cache_put)(struct kref *);
  64. void (*cache_request)(struct cache_detail *cd,
  65. struct cache_head *h,
  66. char **bpp, int *blen);
  67. int (*cache_parse)(struct cache_detail *,
  68. char *buf, int len);
  69. int (*cache_show)(struct seq_file *m,
  70. struct cache_detail *cd,
  71. struct cache_head *h);
  72. struct cache_head * (*alloc)(void);
  73. int (*match)(struct cache_head *orig, struct cache_head *new);
  74. void (*init)(struct cache_head *orig, struct cache_head *new);
  75. void (*update)(struct cache_head *orig, struct cache_head *new);
  76. /* fields below this comment are for internal use
  77. * and should not be touched by cache owners
  78. */
  79. time_t flush_time; /* flush all cache items with last_refresh
  80. * earlier than this */
  81. struct list_head others;
  82. time_t nextcheck;
  83. int entries;
  84. /* fields for communication over channel */
  85. struct list_head queue;
  86. struct proc_dir_entry *proc_ent;
  87. struct proc_dir_entry *flush_ent, *channel_ent, *content_ent;
  88. atomic_t readers; /* how many time is /chennel open */
  89. time_t last_close; /* if no readers, when did last close */
  90. time_t last_warn; /* when we last warned about no readers */
  91. void (*warn_no_listener)(struct cache_detail *cd);
  92. };
  93. /* this must be embedded in any request structure that
  94. * identifies an object that will want a callback on
  95. * a cache fill
  96. */
  97. struct cache_req {
  98. struct cache_deferred_req *(*defer)(struct cache_req *req);
  99. };
  100. /* this must be embedded in a deferred_request that is being
  101. * delayed awaiting cache-fill
  102. */
  103. struct cache_deferred_req {
  104. struct list_head hash; /* on hash chain */
  105. struct list_head recent; /* on fifo */
  106. struct cache_head *item; /* cache item we wait on */
  107. time_t recv_time;
  108. void *owner; /* we might need to discard all defered requests
  109. * owned by someone */
  110. void (*revisit)(struct cache_deferred_req *req,
  111. int too_many);
  112. };
  113. extern struct cache_head *
  114. sunrpc_cache_lookup(struct cache_detail *detail,
  115. struct cache_head *key, int hash);
  116. extern struct cache_head *
  117. sunrpc_cache_update(struct cache_detail *detail,
  118. struct cache_head *new, struct cache_head *old, int hash);
  119. #define cache_for_each(pos, detail, index, member) \
  120. for (({read_lock(&(detail)->hash_lock); index = (detail)->hash_size;}) ; \
  121. ({if (index==0)read_unlock(&(detail)->hash_lock); index--;}); \
  122. ) \
  123. for (pos = container_of((detail)->hash_table[index], typeof(*pos), member); \
  124. &pos->member; \
  125. pos = container_of(pos->member.next, typeof(*pos), member))
  126. extern void cache_clean_deferred(void *owner);
  127. static inline struct cache_head *cache_get(struct cache_head *h)
  128. {
  129. kref_get(&h->ref);
  130. return h;
  131. }
  132. static inline void cache_put(struct cache_head *h, struct cache_detail *cd)
  133. {
  134. if (atomic_read(&h->ref.refcount) <= 2 &&
  135. h->expiry_time < cd->nextcheck)
  136. cd->nextcheck = h->expiry_time;
  137. kref_put(&h->ref, cd->cache_put);
  138. }
  139. static inline int cache_valid(struct cache_head *h)
  140. {
  141. /* If an item has been unhashed pending removal when
  142. * the refcount drops to 0, the expiry_time will be
  143. * set to 0. We don't want to consider such items
  144. * valid in this context even though CACHE_VALID is
  145. * set.
  146. */
  147. return (h->expiry_time != 0 && test_bit(CACHE_VALID, &h->flags));
  148. }
  149. extern int cache_check(struct cache_detail *detail,
  150. struct cache_head *h, struct cache_req *rqstp);
  151. extern void cache_flush(void);
  152. extern void cache_purge(struct cache_detail *detail);
  153. #define NEVER (0x7FFFFFFF)
  154. extern void cache_register(struct cache_detail *cd);
  155. extern int cache_unregister(struct cache_detail *cd);
  156. extern void qword_add(char **bpp, int *lp, char *str);
  157. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  158. extern int qword_get(char **bpp, char *dest, int bufsize);
  159. static inline int get_int(char **bpp, int *anint)
  160. {
  161. char buf[50];
  162. char *ep;
  163. int rv;
  164. int len = qword_get(bpp, buf, 50);
  165. if (len < 0) return -EINVAL;
  166. if (len ==0) return -ENOENT;
  167. rv = simple_strtol(buf, &ep, 0);
  168. if (*ep) return -EINVAL;
  169. *anint = rv;
  170. return 0;
  171. }
  172. static inline time_t get_expiry(char **bpp)
  173. {
  174. int rv;
  175. if (get_int(bpp, &rv))
  176. return 0;
  177. if (rv < 0)
  178. return 0;
  179. return rv;
  180. }
  181. #endif /* _LINUX_SUNRPC_CACHE_H_ */