David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 1 | /* General netfs cache on cache files internal defs |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 12 | #ifdef pr_fmt |
| 13 | #undef pr_fmt |
| 14 | #endif |
| 15 | |
| 16 | #define pr_fmt(fmt) "CacheFiles: " fmt |
| 17 | |
| 18 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 19 | #include <linux/fscache-cache.h> |
| 20 | #include <linux/timer.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/security.h> |
| 24 | |
| 25 | struct cachefiles_cache; |
| 26 | struct cachefiles_object; |
| 27 | |
| 28 | extern unsigned cachefiles_debug; |
| 29 | #define CACHEFILES_DEBUG_KENTER 1 |
| 30 | #define CACHEFILES_DEBUG_KLEAVE 2 |
| 31 | #define CACHEFILES_DEBUG_KDEBUG 4 |
| 32 | |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 33 | #define cachefiles_gfp (__GFP_RECLAIM | __GFP_NORETRY | __GFP_NOMEMALLOC) |
David Howells | 5f4f9f4 | 2012-12-20 21:52:33 +0000 | [diff] [blame] | 34 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 35 | /* |
| 36 | * node records |
| 37 | */ |
| 38 | struct cachefiles_object { |
| 39 | struct fscache_object fscache; /* fscache handle */ |
| 40 | struct cachefiles_lookup_data *lookup_data; /* cached lookup data */ |
| 41 | struct dentry *dentry; /* the file/dir representing this object */ |
| 42 | struct dentry *backer; /* backing file */ |
| 43 | loff_t i_size; /* object size */ |
| 44 | unsigned long flags; |
| 45 | #define CACHEFILES_OBJECT_ACTIVE 0 /* T if marked active */ |
| 46 | atomic_t usage; /* object usage count */ |
| 47 | uint8_t type; /* object type */ |
| 48 | uint8_t new; /* T if object new */ |
| 49 | spinlock_t work_lock; |
| 50 | struct rb_node active_node; /* link in active tree (dentry is key) */ |
| 51 | }; |
| 52 | |
| 53 | extern struct kmem_cache *cachefiles_object_jar; |
| 54 | |
| 55 | /* |
| 56 | * Cache files cache definition |
| 57 | */ |
| 58 | struct cachefiles_cache { |
| 59 | struct fscache_cache cache; /* FS-Cache record */ |
| 60 | struct vfsmount *mnt; /* mountpoint holding the cache */ |
| 61 | struct dentry *graveyard; /* directory into which dead objects go */ |
| 62 | struct file *cachefilesd; /* manager daemon handle */ |
| 63 | const struct cred *cache_cred; /* security override for accessing cache */ |
| 64 | struct mutex daemon_mutex; /* command serialisation mutex */ |
| 65 | wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */ |
| 66 | struct rb_root active_nodes; /* active nodes (can't be culled) */ |
| 67 | rwlock_t active_lock; /* lock for active_nodes */ |
| 68 | atomic_t gravecounter; /* graveyard uniquifier */ |
| 69 | unsigned frun_percent; /* when to stop culling (% files) */ |
| 70 | unsigned fcull_percent; /* when to start culling (% files) */ |
| 71 | unsigned fstop_percent; /* when to stop allocating (% files) */ |
| 72 | unsigned brun_percent; /* when to stop culling (% blocks) */ |
| 73 | unsigned bcull_percent; /* when to start culling (% blocks) */ |
| 74 | unsigned bstop_percent; /* when to stop allocating (% blocks) */ |
| 75 | unsigned bsize; /* cache's block size */ |
| 76 | unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */ |
| 77 | uint64_t frun; /* when to stop culling */ |
| 78 | uint64_t fcull; /* when to start culling */ |
| 79 | uint64_t fstop; /* when to stop allocating */ |
| 80 | sector_t brun; /* when to stop culling */ |
| 81 | sector_t bcull; /* when to start culling */ |
| 82 | sector_t bstop; /* when to stop allocating */ |
| 83 | unsigned long flags; |
| 84 | #define CACHEFILES_READY 0 /* T if cache prepared */ |
| 85 | #define CACHEFILES_DEAD 1 /* T if cache dead */ |
| 86 | #define CACHEFILES_CULLING 2 /* T if cull engaged */ |
| 87 | #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */ |
| 88 | char *rootdirname; /* name of cache root directory */ |
| 89 | char *secctx; /* LSM security context */ |
| 90 | char *tag; /* cache binding tag */ |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * backing file read tracking |
| 95 | */ |
| 96 | struct cachefiles_one_read { |
| 97 | wait_queue_t monitor; /* link into monitored waitqueue */ |
| 98 | struct page *back_page; /* backing file page we're waiting for */ |
| 99 | struct page *netfs_page; /* netfs page we're going to fill */ |
| 100 | struct fscache_retrieval *op; /* retrieval op covering this */ |
| 101 | struct list_head op_link; /* link in op's todo list */ |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * backing file write tracking |
| 106 | */ |
| 107 | struct cachefiles_one_write { |
| 108 | struct page *netfs_page; /* netfs page to copy */ |
| 109 | struct cachefiles_object *object; |
| 110 | struct list_head obj_link; /* link in object's lists */ |
| 111 | fscache_rw_complete_t end_io_func; |
| 112 | void *context; |
| 113 | }; |
| 114 | |
| 115 | /* |
| 116 | * auxiliary data xattr buffer |
| 117 | */ |
| 118 | struct cachefiles_xattr { |
| 119 | uint16_t len; |
| 120 | uint8_t type; |
| 121 | uint8_t data[]; |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | * note change of state for daemon |
| 126 | */ |
| 127 | static inline void cachefiles_state_changed(struct cachefiles_cache *cache) |
| 128 | { |
| 129 | set_bit(CACHEFILES_STATE_CHANGED, &cache->flags); |
| 130 | wake_up_all(&cache->daemon_pollwq); |
| 131 | } |
| 132 | |
| 133 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 134 | * bind.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 135 | */ |
| 136 | extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args); |
| 137 | extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache); |
| 138 | |
| 139 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 140 | * daemon.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 141 | */ |
| 142 | extern const struct file_operations cachefiles_daemon_fops; |
| 143 | |
| 144 | extern int cachefiles_has_space(struct cachefiles_cache *cache, |
| 145 | unsigned fnr, unsigned bnr); |
| 146 | |
| 147 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 148 | * interface.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 149 | */ |
| 150 | extern const struct fscache_cache_ops cachefiles_cache_ops; |
| 151 | |
| 152 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 153 | * key.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 154 | */ |
| 155 | extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type); |
| 156 | |
| 157 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 158 | * namei.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 159 | */ |
| 160 | extern int cachefiles_delete_object(struct cachefiles_cache *cache, |
| 161 | struct cachefiles_object *object); |
| 162 | extern int cachefiles_walk_to_object(struct cachefiles_object *parent, |
| 163 | struct cachefiles_object *object, |
| 164 | const char *key, |
| 165 | struct cachefiles_xattr *auxdata); |
| 166 | extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, |
| 167 | struct dentry *dir, |
| 168 | const char *name); |
| 169 | |
| 170 | extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, |
| 171 | char *filename); |
| 172 | |
| 173 | extern int cachefiles_check_in_use(struct cachefiles_cache *cache, |
| 174 | struct dentry *dir, char *filename); |
| 175 | |
| 176 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 177 | * proc.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 178 | */ |
| 179 | #ifdef CONFIG_CACHEFILES_HISTOGRAM |
| 180 | extern atomic_t cachefiles_lookup_histogram[HZ]; |
| 181 | extern atomic_t cachefiles_mkdir_histogram[HZ]; |
| 182 | extern atomic_t cachefiles_create_histogram[HZ]; |
| 183 | |
| 184 | extern int __init cachefiles_proc_init(void); |
| 185 | extern void cachefiles_proc_cleanup(void); |
| 186 | static inline |
| 187 | void cachefiles_hist(atomic_t histogram[], unsigned long start_jif) |
| 188 | { |
| 189 | unsigned long jif = jiffies - start_jif; |
| 190 | if (jif >= HZ) |
| 191 | jif = HZ - 1; |
| 192 | atomic_inc(&histogram[jif]); |
| 193 | } |
| 194 | |
| 195 | #else |
| 196 | #define cachefiles_proc_init() (0) |
| 197 | #define cachefiles_proc_cleanup() do {} while (0) |
| 198 | #define cachefiles_hist(hist, start_jif) do {} while (0) |
| 199 | #endif |
| 200 | |
| 201 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 202 | * rdwr.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 203 | */ |
| 204 | extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *, |
| 205 | struct page *, gfp_t); |
| 206 | extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *, |
| 207 | struct list_head *, unsigned *, |
| 208 | gfp_t); |
| 209 | extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *, |
| 210 | gfp_t); |
| 211 | extern int cachefiles_allocate_pages(struct fscache_retrieval *, |
| 212 | struct list_head *, unsigned *, gfp_t); |
| 213 | extern int cachefiles_write_page(struct fscache_storage *, struct page *); |
| 214 | extern void cachefiles_uncache_page(struct fscache_object *, struct page *); |
| 215 | |
| 216 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 217 | * security.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 218 | */ |
| 219 | extern int cachefiles_get_security_ID(struct cachefiles_cache *cache); |
| 220 | extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache, |
| 221 | struct dentry *root, |
| 222 | const struct cred **_saved_cred); |
| 223 | |
| 224 | static inline void cachefiles_begin_secure(struct cachefiles_cache *cache, |
| 225 | const struct cred **_saved_cred) |
| 226 | { |
| 227 | *_saved_cred = override_creds(cache->cache_cred); |
| 228 | } |
| 229 | |
| 230 | static inline void cachefiles_end_secure(struct cachefiles_cache *cache, |
| 231 | const struct cred *saved_cred) |
| 232 | { |
| 233 | revert_creds(saved_cred); |
| 234 | } |
| 235 | |
| 236 | /* |
David Howells | 911e690 | 2009-05-27 15:46:55 +0100 | [diff] [blame] | 237 | * xattr.c |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 238 | */ |
| 239 | extern int cachefiles_check_object_type(struct cachefiles_object *object); |
| 240 | extern int cachefiles_set_object_xattr(struct cachefiles_object *object, |
| 241 | struct cachefiles_xattr *auxdata); |
| 242 | extern int cachefiles_update_object_xattr(struct cachefiles_object *object, |
| 243 | struct cachefiles_xattr *auxdata); |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 244 | extern int cachefiles_check_auxdata(struct cachefiles_object *object); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 245 | extern int cachefiles_check_object_xattr(struct cachefiles_object *object, |
| 246 | struct cachefiles_xattr *auxdata); |
| 247 | extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache, |
| 248 | struct dentry *dentry); |
| 249 | |
| 250 | |
| 251 | /* |
| 252 | * error handling |
| 253 | */ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 254 | |
| 255 | #define cachefiles_io_error(___cache, FMT, ...) \ |
| 256 | do { \ |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 257 | pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 258 | fscache_io_error(&(___cache)->cache); \ |
| 259 | set_bit(CACHEFILES_DEAD, &(___cache)->flags); \ |
| 260 | } while (0) |
| 261 | |
| 262 | #define cachefiles_io_error_obj(object, FMT, ...) \ |
| 263 | do { \ |
| 264 | struct cachefiles_cache *___cache; \ |
| 265 | \ |
| 266 | ___cache = container_of((object)->fscache.cache, \ |
| 267 | struct cachefiles_cache, cache); \ |
| 268 | cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \ |
| 269 | } while (0) |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * debug tracing |
| 274 | */ |
| 275 | #define dbgprintk(FMT, ...) \ |
| 276 | printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__) |
| 277 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 278 | #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__) |
| 279 | #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__) |
| 280 | #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__) |
| 281 | |
| 282 | |
| 283 | #if defined(__KDEBUG) |
| 284 | #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__) |
| 285 | #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__) |
| 286 | #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__) |
| 287 | |
| 288 | #elif defined(CONFIG_CACHEFILES_DEBUG) |
| 289 | #define _enter(FMT, ...) \ |
| 290 | do { \ |
| 291 | if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \ |
| 292 | kenter(FMT, ##__VA_ARGS__); \ |
| 293 | } while (0) |
| 294 | |
| 295 | #define _leave(FMT, ...) \ |
| 296 | do { \ |
| 297 | if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \ |
| 298 | kleave(FMT, ##__VA_ARGS__); \ |
| 299 | } while (0) |
| 300 | |
| 301 | #define _debug(FMT, ...) \ |
| 302 | do { \ |
| 303 | if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \ |
| 304 | kdebug(FMT, ##__VA_ARGS__); \ |
| 305 | } while (0) |
| 306 | |
| 307 | #else |
David Howells | 12fdff3 | 2010-08-12 16:54:57 +0100 | [diff] [blame] | 308 | #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__) |
| 309 | #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__) |
| 310 | #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | #if 1 /* defined(__KDEBUGALL) */ |
| 314 | |
| 315 | #define ASSERT(X) \ |
| 316 | do { \ |
| 317 | if (unlikely(!(X))) { \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 318 | pr_err("\n"); \ |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 319 | pr_err("Assertion failed\n"); \ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 320 | BUG(); \ |
| 321 | } \ |
| 322 | } while (0) |
| 323 | |
| 324 | #define ASSERTCMP(X, OP, Y) \ |
| 325 | do { \ |
| 326 | if (unlikely(!((X) OP (Y)))) { \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 327 | pr_err("\n"); \ |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 328 | pr_err("Assertion failed\n"); \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 329 | pr_err("%lx " #OP " %lx is false\n", \ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 330 | (unsigned long)(X), (unsigned long)(Y)); \ |
| 331 | BUG(); \ |
| 332 | } \ |
| 333 | } while (0) |
| 334 | |
| 335 | #define ASSERTIF(C, X) \ |
| 336 | do { \ |
| 337 | if (unlikely((C) && !(X))) { \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 338 | pr_err("\n"); \ |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 339 | pr_err("Assertion failed\n"); \ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 340 | BUG(); \ |
| 341 | } \ |
| 342 | } while (0) |
| 343 | |
| 344 | #define ASSERTIFCMP(C, X, OP, Y) \ |
| 345 | do { \ |
| 346 | if (unlikely((C) && !((X) OP (Y)))) { \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 347 | pr_err("\n"); \ |
Fabian Frederick | 0227d6ab | 2014-06-06 14:37:33 -0700 | [diff] [blame] | 348 | pr_err("Assertion failed\n"); \ |
Fabian Frederick | 4e1eb88 | 2014-06-06 14:37:32 -0700 | [diff] [blame] | 349 | pr_err("%lx " #OP " %lx is false\n", \ |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 350 | (unsigned long)(X), (unsigned long)(Y)); \ |
| 351 | BUG(); \ |
| 352 | } \ |
| 353 | } while (0) |
| 354 | |
| 355 | #else |
| 356 | |
| 357 | #define ASSERT(X) do {} while (0) |
| 358 | #define ASSERTCMP(X, OP, Y) do {} while (0) |
| 359 | #define ASSERTIF(C, X) do {} while (0) |
| 360 | #define ASSERTIFCMP(C, X, OP, Y) do {} while (0) |
| 361 | |
| 362 | #endif |