internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #include <trace/events/cachefiles.h>
  114. /*
  115. * note change of state for daemon
  116. */
  117. static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
  118. {
  119. set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  120. wake_up_all(&cache->daemon_pollwq);
  121. }
  122. /*
  123. * bind.c
  124. */
  125. extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
  126. extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
  127. /*
  128. * daemon.c
  129. */
  130. extern const struct file_operations cachefiles_daemon_fops;
  131. extern int cachefiles_has_space(struct cachefiles_cache *cache,
  132. unsigned fnr, unsigned bnr);
  133. /*
  134. * interface.c
  135. */
  136. extern const struct fscache_cache_ops cachefiles_cache_ops;
  137. /*
  138. * key.c
  139. */
  140. extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
  141. /*
  142. * namei.c
  143. */
  144. extern void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
  145. struct cachefiles_object *object,
  146. blkcnt_t i_blocks);
  147. extern int cachefiles_delete_object(struct cachefiles_cache *cache,
  148. struct cachefiles_object *object);
  149. extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
  150. struct cachefiles_object *object,
  151. const char *key,
  152. struct cachefiles_xattr *auxdata);
  153. extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  154. struct dentry *dir,
  155. const char *name);
  156. extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  157. char *filename);
  158. extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
  159. struct dentry *dir, char *filename);
  160. /*
  161. * proc.c
  162. */
  163. #ifdef CONFIG_CACHEFILES_HISTOGRAM
  164. extern atomic_t cachefiles_lookup_histogram[HZ];
  165. extern atomic_t cachefiles_mkdir_histogram[HZ];
  166. extern atomic_t cachefiles_create_histogram[HZ];
  167. extern int __init cachefiles_proc_init(void);
  168. extern void cachefiles_proc_cleanup(void);
  169. static inline
  170. void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
  171. {
  172. unsigned long jif = jiffies - start_jif;
  173. if (jif >= HZ)
  174. jif = HZ - 1;
  175. atomic_inc(&histogram[jif]);
  176. }
  177. #else
  178. #define cachefiles_proc_init() (0)
  179. #define cachefiles_proc_cleanup() do {} while (0)
  180. #define cachefiles_hist(hist, start_jif) do {} while (0)
  181. #endif
  182. /*
  183. * rdwr.c
  184. */
  185. extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
  186. struct page *, gfp_t);
  187. extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
  188. struct list_head *, unsigned *,
  189. gfp_t);
  190. extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
  191. gfp_t);
  192. extern int cachefiles_allocate_pages(struct fscache_retrieval *,
  193. struct list_head *, unsigned *, gfp_t);
  194. extern int cachefiles_write_page(struct fscache_storage *, struct page *);
  195. extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
  196. /*
  197. * security.c
  198. */
  199. extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
  200. extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
  201. struct dentry *root,
  202. const struct cred **_saved_cred);
  203. static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
  204. const struct cred **_saved_cred)
  205. {
  206. *_saved_cred = override_creds(cache->cache_cred);
  207. }
  208. static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
  209. const struct cred *saved_cred)
  210. {
  211. revert_creds(saved_cred);
  212. }
  213. /*
  214. * xattr.c
  215. */
  216. extern int cachefiles_check_object_type(struct cachefiles_object *object);
  217. extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
  218. struct cachefiles_xattr *auxdata);
  219. extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
  220. struct cachefiles_xattr *auxdata);
  221. extern int cachefiles_check_auxdata(struct cachefiles_object *object);
  222. extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
  223. struct cachefiles_xattr *auxdata);
  224. extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  225. struct dentry *dentry);
  226. /*
  227. * error handling
  228. */
  229. #define cachefiles_io_error(___cache, FMT, ...) \
  230. do { \
  231. pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
  232. fscache_io_error(&(___cache)->cache); \
  233. set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
  234. } while (0)
  235. #define cachefiles_io_error_obj(object, FMT, ...) \
  236. do { \
  237. struct cachefiles_cache *___cache; \
  238. \
  239. ___cache = container_of((object)->fscache.cache, \
  240. struct cachefiles_cache, cache); \
  241. cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
  242. } while (0)
  243. /*
  244. * debug tracing
  245. */
  246. #define dbgprintk(FMT, ...) \
  247. printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  248. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  249. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  250. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  251. #if defined(__KDEBUG)
  252. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  253. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  254. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  255. #elif defined(CONFIG_CACHEFILES_DEBUG)
  256. #define _enter(FMT, ...) \
  257. do { \
  258. if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
  259. kenter(FMT, ##__VA_ARGS__); \
  260. } while (0)
  261. #define _leave(FMT, ...) \
  262. do { \
  263. if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
  264. kleave(FMT, ##__VA_ARGS__); \
  265. } while (0)
  266. #define _debug(FMT, ...) \
  267. do { \
  268. if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
  269. kdebug(FMT, ##__VA_ARGS__); \
  270. } while (0)
  271. #else
  272. #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  273. #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  274. #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  275. #endif
  276. #if 1 /* defined(__KDEBUGALL) */
  277. #define ASSERT(X) \
  278. do { \
  279. if (unlikely(!(X))) { \
  280. pr_err("\n"); \
  281. pr_err("Assertion failed\n"); \
  282. BUG(); \
  283. } \
  284. } while (0)
  285. #define ASSERTCMP(X, OP, Y) \
  286. do { \
  287. if (unlikely(!((X) OP (Y)))) { \
  288. pr_err("\n"); \
  289. pr_err("Assertion failed\n"); \
  290. pr_err("%lx " #OP " %lx is false\n", \
  291. (unsigned long)(X), (unsigned long)(Y)); \
  292. BUG(); \
  293. } \
  294. } while (0)
  295. #define ASSERTIF(C, X) \
  296. do { \
  297. if (unlikely((C) && !(X))) { \
  298. pr_err("\n"); \
  299. pr_err("Assertion failed\n"); \
  300. BUG(); \
  301. } \
  302. } while (0)
  303. #define ASSERTIFCMP(C, X, OP, Y) \
  304. do { \
  305. if (unlikely((C) && !((X) OP (Y)))) { \
  306. pr_err("\n"); \
  307. pr_err("Assertion failed\n"); \
  308. pr_err("%lx " #OP " %lx is false\n", \
  309. (unsigned long)(X), (unsigned long)(Y)); \
  310. BUG(); \
  311. } \
  312. } while (0)
  313. #else
  314. #define ASSERT(X) do {} while (0)
  315. #define ASSERTCMP(X, OP, Y) do {} while (0)
  316. #define ASSERTIF(C, X) do {} while (0)
  317. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  318. #endif