cache.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. atomic_t refcnt;
  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 cache_head *,
  64. struct cache_detail*);
  65. void (*cache_request)(struct cache_detail *cd,
  66. struct cache_head *h,
  67. char **bpp, int *blen);
  68. int (*cache_parse)(struct cache_detail *,
  69. char *buf, int len);
  70. int (*cache_show)(struct seq_file *m,
  71. struct cache_detail *cd,
  72. struct cache_head *h);
  73. struct cache_head * (*alloc)(void);
  74. int (*match)(struct cache_head *orig, struct cache_head *new);
  75. void (*init)(struct cache_head *orig, struct cache_head *new);
  76. void (*update)(struct cache_head *orig, struct cache_head *new);
  77. /* fields below this comment are for internal use
  78. * and should not be touched by cache owners
  79. */
  80. time_t flush_time; /* flush all cache items with last_refresh
  81. * earlier than this */
  82. struct list_head others;
  83. time_t nextcheck;
  84. int entries;
  85. /* fields for communication over channel */
  86. struct list_head queue;
  87. struct proc_dir_entry *proc_ent;
  88. struct proc_dir_entry *flush_ent, *channel_ent, *content_ent;
  89. atomic_t readers; /* how many time is /chennel open */
  90. time_t last_close; /* if no readers, when did last close */
  91. time_t last_warn; /* when we last warned about no readers */
  92. void (*warn_no_listener)(struct cache_detail *cd);
  93. };
  94. /* this must be embedded in any request structure that
  95. * identifies an object that will want a callback on
  96. * a cache fill
  97. */
  98. struct cache_req {
  99. struct cache_deferred_req *(*defer)(struct cache_req *req);
  100. };
  101. /* this must be embedded in a deferred_request that is being
  102. * delayed awaiting cache-fill
  103. */
  104. struct cache_deferred_req {
  105. struct list_head hash; /* on hash chain */
  106. struct list_head recent; /* on fifo */
  107. struct cache_head *item; /* cache item we wait on */
  108. time_t recv_time;
  109. void *owner; /* we might need to discard all defered requests
  110. * owned by someone */
  111. void (*revisit)(struct cache_deferred_req *req,
  112. int too_many);
  113. };
  114. extern struct cache_head *
  115. sunrpc_cache_lookup(struct cache_detail *detail,
  116. struct cache_head *key, int hash);
  117. extern struct cache_head *
  118. sunrpc_cache_update(struct cache_detail *detail,
  119. struct cache_head *new, struct cache_head *old, int hash);
  120. #define cache_for_each(pos, detail, index, member) \
  121. for (({read_lock(&(detail)->hash_lock); index = (detail)->hash_size;}) ; \
  122. ({if (index==0)read_unlock(&(detail)->hash_lock); index--;}); \
  123. ) \
  124. for (pos = container_of((detail)->hash_table[index], typeof(*pos), member); \
  125. &pos->member; \
  126. pos = container_of(pos->member.next, typeof(*pos), member))
  127. extern void cache_clean_deferred(void *owner);
  128. static inline struct cache_head *cache_get(struct cache_head *h)
  129. {
  130. atomic_inc(&h->refcnt);
  131. return h;
  132. }
  133. static inline int cache_put(struct cache_head *h, struct cache_detail *cd)
  134. {
  135. if (atomic_read(&h->refcnt) <= 2 &&
  136. h->expiry_time < cd->nextcheck)
  137. cd->nextcheck = h->expiry_time;
  138. return atomic_dec_and_test(&h->refcnt);
  139. }
  140. extern void cache_init(struct cache_head *h);
  141. extern int cache_check(struct cache_detail *detail,
  142. struct cache_head *h, struct cache_req *rqstp);
  143. extern void cache_flush(void);
  144. extern void cache_purge(struct cache_detail *detail);
  145. #define NEVER (0x7FFFFFFF)
  146. extern void cache_register(struct cache_detail *cd);
  147. extern int cache_unregister(struct cache_detail *cd);
  148. extern void qword_add(char **bpp, int *lp, char *str);
  149. extern void qword_addhex(char **bpp, int *lp, char *buf, int blen);
  150. extern int qword_get(char **bpp, char *dest, int bufsize);
  151. static inline int get_int(char **bpp, int *anint)
  152. {
  153. char buf[50];
  154. char *ep;
  155. int rv;
  156. int len = qword_get(bpp, buf, 50);
  157. if (len < 0) return -EINVAL;
  158. if (len ==0) return -ENOENT;
  159. rv = simple_strtol(buf, &ep, 0);
  160. if (*ep) return -EINVAL;
  161. *anint = rv;
  162. return 0;
  163. }
  164. static inline time_t get_expiry(char **bpp)
  165. {
  166. int rv;
  167. if (get_int(bpp, &rv))
  168. return 0;
  169. if (rv < 0)
  170. return 0;
  171. return rv;
  172. }
  173. #endif /* _LINUX_SUNRPC_CACHE_H_ */