Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 7dfc0063022078a80fe5774815723c185e4b7b57 328 lines 9.4 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * include/linux/sunrpc/cache.h 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#ifndef _LINUX_SUNRPC_CACHE_H_ 12#define _LINUX_SUNRPC_CACHE_H_ 13 14#include <linux/kref.h> 15#include <linux/slab.h> 16#include <linux/atomic.h> 17#include <linux/kstrtox.h> 18#include <linux/proc_fs.h> 19#include <linux/wait.h> 20 21/* 22 * Each cache requires: 23 * - A 'struct cache_detail' which contains information specific to the cache 24 * for common code to use. 25 * - An item structure that must contain a "struct cache_head" 26 * - A lookup function defined using DefineCacheLookup 27 * - A 'put' function that can release a cache item. It will only 28 * be called after cache_put has succeed, so there are guarantee 29 * to be no references. 30 * - A function to calculate a hash of an item's key. 31 * 32 * as well as assorted code fragments (e.g. compare keys) and numbers 33 * (e.g. hash size, goal_age, etc). 34 * 35 * Each cache must be registered so that it can be cleaned regularly. 36 * When the cache is unregistered, it is flushed completely. 37 * 38 * Entries have a ref count and a 'hashed' flag which counts the existence 39 * in the hash table. 40 * We only expire entries when refcount is zero. 41 * Existence in the cache is counted the refcount. 42 */ 43 44/* Every cache item has a common header that is used 45 * for expiring and refreshing entries. 46 * 47 */ 48struct cache_head { 49 struct hlist_node cache_list; 50 time64_t expiry_time; /* After time expiry_time, don't use 51 * the data */ 52 time64_t last_refresh; /* If CACHE_PENDING, this is when upcall was 53 * sent, else this is when update was 54 * received, though it is alway set to 55 * be *after* ->flush_time. 56 */ 57 struct kref ref; 58 unsigned long flags; 59}; 60 61/* cache_head.flags */ 62enum { 63 CACHE_VALID, /* Entry contains valid data */ 64 CACHE_NEGATIVE, /* Negative entry - there is no match for the key */ 65 CACHE_PENDING, /* An upcall has been sent but no reply received yet*/ 66 CACHE_CLEANED, /* Entry has been cleaned from cache */ 67}; 68 69#define CACHE_NEW_EXPIRY 120 /* keep new things pending confirmation for 120 seconds */ 70 71struct cache_detail { 72 struct module * owner; 73 int hash_size; 74 struct hlist_head * hash_table; 75 spinlock_t hash_lock; 76 77 char *name; 78 void (*cache_put)(struct kref *); 79 80 int (*cache_upcall)(struct cache_detail *, 81 struct cache_head *); 82 83 void (*cache_request)(struct cache_detail *cd, 84 struct cache_head *ch, 85 char **bpp, int *blen); 86 87 int (*cache_parse)(struct cache_detail *, 88 char *buf, int len); 89 90 int (*cache_show)(struct seq_file *m, 91 struct cache_detail *cd, 92 struct cache_head *h); 93 void (*warn_no_listener)(struct cache_detail *cd, 94 int has_died); 95 96 struct cache_head * (*alloc)(void); 97 void (*flush)(void); 98 int (*match)(struct cache_head *orig, struct cache_head *new); 99 void (*init)(struct cache_head *orig, struct cache_head *new); 100 void (*update)(struct cache_head *orig, struct cache_head *new); 101 102 /* fields below this comment are for internal use 103 * and should not be touched by cache owners 104 */ 105 time64_t flush_time; /* flush all cache items with 106 * last_refresh at or earlier 107 * than this. last_refresh 108 * is never set at or earlier 109 * than this. 110 */ 111 struct list_head others; 112 time64_t nextcheck; 113 int entries; 114 115 /* fields for communication over channel */ 116 struct list_head requests; 117 struct list_head readers; 118 spinlock_t queue_lock; 119 wait_queue_head_t queue_wait; 120 u64 next_seqno; 121 122 atomic_t writers; /* how many time is /channel open */ 123 time64_t last_close; /* if no writers, when did last close */ 124 time64_t last_warn; /* when we last warned about no writers */ 125 126 union { 127 struct proc_dir_entry *procfs; 128 struct dentry *pipefs; 129 }; 130 struct net *net; 131}; 132 133/* this must be embedded in any request structure that 134 * identifies an object that will want a callback on 135 * a cache fill 136 */ 137struct cache_req { 138 struct cache_deferred_req *(*defer)(struct cache_req *req); 139 unsigned long thread_wait; /* How long (jiffies) we can block the 140 * current thread to wait for updates. 141 */ 142}; 143 144/* this must be embedded in a deferred_request that is being 145 * delayed awaiting cache-fill 146 */ 147struct cache_deferred_req { 148 struct hlist_node hash; /* on hash chain */ 149 struct list_head recent; /* on fifo */ 150 struct cache_head *item; /* cache item we wait on */ 151 void *owner; /* we might need to discard all defered requests 152 * owned by someone */ 153 void (*revisit)(struct cache_deferred_req *req, 154 int too_many); 155}; 156 157/* 158 * timestamps kept in the cache are expressed in seconds 159 * since boot. This is the best for measuring differences in 160 * real time. 161 * This reimplemnts ktime_get_boottime_seconds() in a slightly 162 * faster but less accurate way. When we end up converting 163 * back to wallclock (CLOCK_REALTIME), that error often 164 * cancels out during the reverse operation. 165 */ 166static inline time64_t seconds_since_boot(void) 167{ 168 struct timespec64 boot; 169 getboottime64(&boot); 170 return ktime_get_real_seconds() - boot.tv_sec; 171} 172 173static inline time64_t convert_to_wallclock(time64_t sinceboot) 174{ 175 struct timespec64 boot; 176 getboottime64(&boot); 177 return boot.tv_sec + sinceboot; 178} 179 180extern const struct file_operations cache_file_operations_pipefs; 181extern const struct file_operations content_file_operations_pipefs; 182extern const struct file_operations cache_flush_operations_pipefs; 183 184extern struct cache_head * 185sunrpc_cache_lookup_rcu(struct cache_detail *detail, 186 struct cache_head *key, int hash); 187extern struct cache_head * 188sunrpc_cache_update(struct cache_detail *detail, 189 struct cache_head *new, struct cache_head *old, int hash); 190 191extern int 192sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h); 193extern int 194sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail, 195 struct cache_head *h); 196 197 198extern void cache_clean_deferred(void *owner); 199 200static inline struct cache_head *cache_get(struct cache_head *h) 201{ 202 kref_get(&h->ref); 203 return h; 204} 205 206static inline struct cache_head *cache_get_rcu(struct cache_head *h) 207{ 208 if (kref_get_unless_zero(&h->ref)) 209 return h; 210 return NULL; 211} 212 213static inline void cache_put(struct cache_head *h, struct cache_detail *cd) 214{ 215 if (kref_read(&h->ref) <= 2 && 216 h->expiry_time < cd->nextcheck) 217 cd->nextcheck = h->expiry_time; 218 kref_put(&h->ref, cd->cache_put); 219} 220 221static inline bool cache_is_expired(struct cache_detail *detail, struct cache_head *h) 222{ 223 if (h->expiry_time < seconds_since_boot()) 224 return true; 225 if (!test_bit(CACHE_VALID, &h->flags)) 226 return false; 227 return detail->flush_time >= h->last_refresh; 228} 229 230extern int cache_check_rcu(struct cache_detail *detail, 231 struct cache_head *h, struct cache_req *rqstp); 232extern int cache_check(struct cache_detail *detail, 233 struct cache_head *h, struct cache_req *rqstp); 234extern void cache_flush(void); 235extern void cache_purge(struct cache_detail *detail); 236#define NEVER (0x7FFFFFFF) 237extern void __init cache_initialize(void); 238extern int cache_register_net(struct cache_detail *cd, struct net *net); 239extern void cache_unregister_net(struct cache_detail *cd, struct net *net); 240 241extern struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net); 242extern void cache_destroy_net(struct cache_detail *cd, struct net *net); 243 244extern void sunrpc_init_cache_detail(struct cache_detail *cd); 245extern void sunrpc_destroy_cache_detail(struct cache_detail *cd); 246extern int sunrpc_cache_register_pipefs(struct dentry *parent, const char *, 247 umode_t, struct cache_detail *); 248extern void sunrpc_cache_unregister_pipefs(struct cache_detail *); 249extern void sunrpc_cache_unhash(struct cache_detail *, struct cache_head *); 250 251/* Must store cache_detail in seq_file->private if using next three functions */ 252extern void *cache_seq_start_rcu(struct seq_file *file, loff_t *pos); 253extern void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos); 254extern void cache_seq_stop_rcu(struct seq_file *file, void *p); 255 256extern void qword_add(char **bpp, int *lp, char *str); 257extern void qword_addhex(char **bpp, int *lp, char *buf, int blen); 258extern int qword_get(char **bpp, char *dest, int bufsize); 259 260static inline int get_int(char **bpp, int *anint) 261{ 262 char buf[50]; 263 char *ep; 264 int rv; 265 int len = qword_get(bpp, buf, sizeof(buf)); 266 267 if (len < 0) 268 return -EINVAL; 269 if (len == 0) 270 return -ENOENT; 271 272 rv = simple_strtol(buf, &ep, 0); 273 if (*ep) 274 return -EINVAL; 275 276 *anint = rv; 277 return 0; 278} 279 280static inline int get_uint(char **bpp, unsigned int *anint) 281{ 282 char buf[50]; 283 int len = qword_get(bpp, buf, sizeof(buf)); 284 285 if (len < 0) 286 return -EINVAL; 287 if (len == 0) 288 return -ENOENT; 289 290 if (kstrtouint(buf, 0, anint)) 291 return -EINVAL; 292 293 return 0; 294} 295 296static inline int get_time(char **bpp, time64_t *time) 297{ 298 char buf[50]; 299 long long ll; 300 int len = qword_get(bpp, buf, sizeof(buf)); 301 302 if (len < 0) 303 return -EINVAL; 304 if (len == 0) 305 return -ENOENT; 306 307 if (kstrtoll(buf, 0, &ll)) 308 return -EINVAL; 309 310 *time = ll; 311 return 0; 312} 313 314static inline int get_expiry(char **bpp, time64_t *rvp) 315{ 316 int error; 317 struct timespec64 boot; 318 319 error = get_time(bpp, rvp); 320 if (error) 321 return error; 322 323 getboottime64(&boot); 324 (*rvp) -= boot.tv_sec; 325 return 0; 326} 327 328#endif /* _LINUX_SUNRPC_CACHE_H_ */