internal.h 11 KB

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