internal.h 11 KB

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