internal.h 11 KB

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