internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include <linux/fscache-cache.h>
  12. #include <linux/timer.h>
  13. #include <linux/wait.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/security.h>
  16. struct cachefiles_cache;
  17. struct cachefiles_object;
  18. extern unsigned cachefiles_debug;
  19. #define CACHEFILES_DEBUG_KENTER 1
  20. #define CACHEFILES_DEBUG_KLEAVE 2
  21. #define CACHEFILES_DEBUG_KDEBUG 4
  22. /*
  23. * node records
  24. */
  25. struct cachefiles_object {
  26. struct fscache_object fscache; /* fscache handle */
  27. struct cachefiles_lookup_data *lookup_data; /* cached lookup data */
  28. struct dentry *dentry; /* the file/dir representing this object */
  29. struct dentry *backer; /* backing file */
  30. loff_t i_size; /* object size */
  31. unsigned long flags;
  32. #define CACHEFILES_OBJECT_ACTIVE 0 /* T if marked active */
  33. #define CACHEFILES_OBJECT_BURIED 1 /* T if preemptively buried */
  34. atomic_t usage; /* object usage count */
  35. uint8_t type; /* object type */
  36. uint8_t new; /* T if object new */
  37. spinlock_t work_lock;
  38. struct rb_node active_node; /* link in active tree (dentry is key) */
  39. };
  40. extern struct kmem_cache *cachefiles_object_jar;
  41. /*
  42. * Cache files cache definition
  43. */
  44. struct cachefiles_cache {
  45. struct fscache_cache cache; /* FS-Cache record */
  46. struct vfsmount *mnt; /* mountpoint holding the cache */
  47. struct dentry *graveyard; /* directory into which dead objects go */
  48. struct file *cachefilesd; /* manager daemon handle */
  49. const struct cred *cache_cred; /* security override for accessing cache */
  50. struct mutex daemon_mutex; /* command serialisation mutex */
  51. wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
  52. struct rb_root active_nodes; /* active nodes (can't be culled) */
  53. rwlock_t active_lock; /* lock for active_nodes */
  54. atomic_t gravecounter; /* graveyard uniquifier */
  55. unsigned frun_percent; /* when to stop culling (% files) */
  56. unsigned fcull_percent; /* when to start culling (% files) */
  57. unsigned fstop_percent; /* when to stop allocating (% files) */
  58. unsigned brun_percent; /* when to stop culling (% blocks) */
  59. unsigned bcull_percent; /* when to start culling (% blocks) */
  60. unsigned bstop_percent; /* when to stop allocating (% blocks) */
  61. unsigned bsize; /* cache's block size */
  62. unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */
  63. uint64_t frun; /* when to stop culling */
  64. uint64_t fcull; /* when to start culling */
  65. uint64_t fstop; /* when to stop allocating */
  66. sector_t brun; /* when to stop culling */
  67. sector_t bcull; /* when to start culling */
  68. sector_t bstop; /* when to stop allocating */
  69. unsigned long flags;
  70. #define CACHEFILES_READY 0 /* T if cache prepared */
  71. #define CACHEFILES_DEAD 1 /* T if cache dead */
  72. #define CACHEFILES_CULLING 2 /* T if cull engaged */
  73. #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
  74. char *rootdirname; /* name of cache root directory */
  75. char *secctx; /* LSM security context */
  76. char *tag; /* cache binding tag */
  77. };
  78. /*
  79. * backing file read tracking
  80. */
  81. struct cachefiles_one_read {
  82. wait_queue_t monitor; /* link into monitored waitqueue */
  83. struct page *back_page; /* backing file page we're waiting for */
  84. struct page *netfs_page; /* netfs page we're going to fill */
  85. struct fscache_retrieval *op; /* retrieval op covering this */
  86. struct list_head op_link; /* link in op's todo list */
  87. };
  88. /*
  89. * backing file write tracking
  90. */
  91. struct cachefiles_one_write {
  92. struct page *netfs_page; /* netfs page to copy */
  93. struct cachefiles_object *object;
  94. struct list_head obj_link; /* link in object's lists */
  95. fscache_rw_complete_t end_io_func;
  96. void *context;
  97. };
  98. /*
  99. * auxiliary data xattr buffer
  100. */
  101. struct cachefiles_xattr {
  102. uint16_t len;
  103. uint8_t type;
  104. uint8_t data[];
  105. };
  106. /*
  107. * note change of state for daemon
  108. */
  109. static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
  110. {
  111. set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  112. wake_up_all(&cache->daemon_pollwq);
  113. }
  114. /*
  115. * bind.c
  116. */
  117. extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
  118. extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
  119. /*
  120. * daemon.c
  121. */
  122. extern const struct file_operations cachefiles_daemon_fops;
  123. extern int cachefiles_has_space(struct cachefiles_cache *cache,
  124. unsigned fnr, unsigned bnr);
  125. /*
  126. * interface.c
  127. */
  128. extern const struct fscache_cache_ops cachefiles_cache_ops;
  129. /*
  130. * key.c
  131. */
  132. extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
  133. /*
  134. * namei.c
  135. */
  136. extern int cachefiles_delete_object(struct cachefiles_cache *cache,
  137. struct cachefiles_object *object);
  138. extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
  139. struct cachefiles_object *object,
  140. const char *key,
  141. struct cachefiles_xattr *auxdata);
  142. extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  143. struct dentry *dir,
  144. const char *name);
  145. extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  146. char *filename);
  147. extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
  148. struct dentry *dir, char *filename);
  149. /*
  150. * proc.c
  151. */
  152. #ifdef CONFIG_CACHEFILES_HISTOGRAM
  153. extern atomic_t cachefiles_lookup_histogram[HZ];
  154. extern atomic_t cachefiles_mkdir_histogram[HZ];
  155. extern atomic_t cachefiles_create_histogram[HZ];
  156. extern int __init cachefiles_proc_init(void);
  157. extern void cachefiles_proc_cleanup(void);
  158. static inline
  159. void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
  160. {
  161. unsigned long jif = jiffies - start_jif;
  162. if (jif >= HZ)
  163. jif = HZ - 1;
  164. atomic_inc(&histogram[jif]);
  165. }
  166. #else
  167. #define cachefiles_proc_init() (0)
  168. #define cachefiles_proc_cleanup() do {} while (0)
  169. #define cachefiles_hist(hist, start_jif) do {} while (0)
  170. #endif
  171. /*
  172. * rdwr.c
  173. */
  174. extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
  175. struct page *, gfp_t);
  176. extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
  177. struct list_head *, unsigned *,
  178. gfp_t);
  179. extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
  180. gfp_t);
  181. extern int cachefiles_allocate_pages(struct fscache_retrieval *,
  182. struct list_head *, unsigned *, gfp_t);
  183. extern int cachefiles_write_page(struct fscache_storage *, struct page *);
  184. extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
  185. /*
  186. * security.c
  187. */
  188. extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
  189. extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
  190. struct dentry *root,
  191. const struct cred **_saved_cred);
  192. static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
  193. const struct cred **_saved_cred)
  194. {
  195. *_saved_cred = override_creds(cache->cache_cred);
  196. }
  197. static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
  198. const struct cred *saved_cred)
  199. {
  200. revert_creds(saved_cred);
  201. }
  202. /*
  203. * xattr.c
  204. */
  205. extern int cachefiles_check_object_type(struct cachefiles_object *object);
  206. extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
  207. struct cachefiles_xattr *auxdata);
  208. extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
  209. struct cachefiles_xattr *auxdata);
  210. extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
  211. struct cachefiles_xattr *auxdata);
  212. extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  213. struct dentry *dentry);
  214. /*
  215. * error handling
  216. */
  217. #define kerror(FMT, ...) printk(KERN_ERR "CacheFiles: "FMT"\n", ##__VA_ARGS__)
  218. #define cachefiles_io_error(___cache, FMT, ...) \
  219. do { \
  220. kerror("I/O Error: " FMT, ##__VA_ARGS__); \
  221. fscache_io_error(&(___cache)->cache); \
  222. set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
  223. } while (0)
  224. #define cachefiles_io_error_obj(object, FMT, ...) \
  225. do { \
  226. struct cachefiles_cache *___cache; \
  227. \
  228. ___cache = container_of((object)->fscache.cache, \
  229. struct cachefiles_cache, cache); \
  230. cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
  231. } while (0)
  232. /*
  233. * debug tracing
  234. */
  235. #define dbgprintk(FMT, ...) \
  236. printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  237. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  238. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  239. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  240. #if defined(__KDEBUG)
  241. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  242. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  243. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  244. #elif defined(CONFIG_CACHEFILES_DEBUG)
  245. #define _enter(FMT, ...) \
  246. do { \
  247. if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
  248. kenter(FMT, ##__VA_ARGS__); \
  249. } while (0)
  250. #define _leave(FMT, ...) \
  251. do { \
  252. if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
  253. kleave(FMT, ##__VA_ARGS__); \
  254. } while (0)
  255. #define _debug(FMT, ...) \
  256. do { \
  257. if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
  258. kdebug(FMT, ##__VA_ARGS__); \
  259. } while (0)
  260. #else
  261. #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  262. #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  263. #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  264. #endif
  265. #if 1 /* defined(__KDEBUGALL) */
  266. #define ASSERT(X) \
  267. do { \
  268. if (unlikely(!(X))) { \
  269. printk(KERN_ERR "\n"); \
  270. printk(KERN_ERR "CacheFiles: Assertion failed\n"); \
  271. BUG(); \
  272. } \
  273. } while (0)
  274. #define ASSERTCMP(X, OP, Y) \
  275. do { \
  276. if (unlikely(!((X) OP (Y)))) { \
  277. printk(KERN_ERR "\n"); \
  278. printk(KERN_ERR "CacheFiles: Assertion failed\n"); \
  279. printk(KERN_ERR "%lx " #OP " %lx is false\n", \
  280. (unsigned long)(X), (unsigned long)(Y)); \
  281. BUG(); \
  282. } \
  283. } while (0)
  284. #define ASSERTIF(C, X) \
  285. do { \
  286. if (unlikely((C) && !(X))) { \
  287. printk(KERN_ERR "\n"); \
  288. printk(KERN_ERR "CacheFiles: Assertion failed\n"); \
  289. BUG(); \
  290. } \
  291. } while (0)
  292. #define ASSERTIFCMP(C, X, OP, Y) \
  293. do { \
  294. if (unlikely((C) && !((X) OP (Y)))) { \
  295. printk(KERN_ERR "\n"); \
  296. printk(KERN_ERR "CacheFiles: Assertion failed\n"); \
  297. printk(KERN_ERR "%lx " #OP " %lx is false\n", \
  298. (unsigned long)(X), (unsigned long)(Y)); \
  299. BUG(); \
  300. } \
  301. } while (0)
  302. #else
  303. #define ASSERT(X) do {} while (0)
  304. #define ASSERTCMP(X, OP, Y) do {} while (0)
  305. #define ASSERTIF(C, X) do {} while (0)
  306. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  307. #endif