internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* Internal definitions for FS-Cache
  2. *
  3. * Copyright (C) 2004-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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Lock order, in the order in which multiple locks should be obtained:
  13. * - fscache_addremove_sem
  14. * - cookie->lock
  15. * - cookie->parent->lock
  16. * - cache->object_list_lock
  17. * - object->lock
  18. * - object->parent->lock
  19. * - cookie->stores_lock
  20. * - fscache_thread_lock
  21. *
  22. */
  23. #include <linux/fscache-cache.h>
  24. #include <linux/sched.h>
  25. #define FSCACHE_MIN_THREADS 4
  26. #define FSCACHE_MAX_THREADS 32
  27. /*
  28. * cache.c
  29. */
  30. extern struct list_head fscache_cache_list;
  31. extern struct rw_semaphore fscache_addremove_sem;
  32. extern struct fscache_cache *fscache_select_cache_for_object(
  33. struct fscache_cookie *);
  34. /*
  35. * cookie.c
  36. */
  37. extern struct kmem_cache *fscache_cookie_jar;
  38. extern void fscache_cookie_init_once(void *);
  39. extern void __fscache_cookie_put(struct fscache_cookie *);
  40. /*
  41. * fsdef.c
  42. */
  43. extern struct fscache_cookie fscache_fsdef_index;
  44. extern struct fscache_cookie_def fscache_fsdef_netfs_def;
  45. /*
  46. * histogram.c
  47. */
  48. #ifdef CONFIG_FSCACHE_HISTOGRAM
  49. extern atomic_t fscache_obj_instantiate_histogram[HZ];
  50. extern atomic_t fscache_objs_histogram[HZ];
  51. extern atomic_t fscache_ops_histogram[HZ];
  52. extern atomic_t fscache_retrieval_delay_histogram[HZ];
  53. extern atomic_t fscache_retrieval_histogram[HZ];
  54. static inline void fscache_hist(atomic_t histogram[], unsigned long start_jif)
  55. {
  56. unsigned long jif = jiffies - start_jif;
  57. if (jif >= HZ)
  58. jif = HZ - 1;
  59. atomic_inc(&histogram[jif]);
  60. }
  61. extern const struct file_operations fscache_histogram_fops;
  62. #else
  63. #define fscache_hist(hist, start_jif) do {} while (0)
  64. #endif
  65. /*
  66. * main.c
  67. */
  68. extern unsigned fscache_defer_lookup;
  69. extern unsigned fscache_defer_create;
  70. extern unsigned fscache_debug;
  71. extern struct kobject *fscache_root;
  72. extern int fscache_wait_bit(void *);
  73. extern int fscache_wait_bit_interruptible(void *);
  74. /*
  75. * object.c
  76. */
  77. extern const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5];
  78. extern void fscache_withdrawing_object(struct fscache_cache *,
  79. struct fscache_object *);
  80. extern void fscache_enqueue_object(struct fscache_object *);
  81. /*
  82. * object-list.c
  83. */
  84. #ifdef CONFIG_FSCACHE_OBJECT_LIST
  85. extern const struct file_operations fscache_objlist_fops;
  86. extern void fscache_objlist_add(struct fscache_object *);
  87. #else
  88. #define fscache_objlist_add(object) do {} while(0)
  89. #endif
  90. /*
  91. * operation.c
  92. */
  93. extern int fscache_submit_exclusive_op(struct fscache_object *,
  94. struct fscache_operation *);
  95. extern int fscache_submit_op(struct fscache_object *,
  96. struct fscache_operation *);
  97. extern int fscache_cancel_op(struct fscache_operation *);
  98. extern void fscache_abort_object(struct fscache_object *);
  99. extern void fscache_start_operations(struct fscache_object *);
  100. extern void fscache_operation_gc(struct work_struct *);
  101. /*
  102. * proc.c
  103. */
  104. #ifdef CONFIG_PROC_FS
  105. extern int __init fscache_proc_init(void);
  106. extern void fscache_proc_cleanup(void);
  107. #else
  108. #define fscache_proc_init() (0)
  109. #define fscache_proc_cleanup() do {} while (0)
  110. #endif
  111. /*
  112. * stats.c
  113. */
  114. #ifdef CONFIG_FSCACHE_STATS
  115. extern atomic_t fscache_n_ops_processed[FSCACHE_MAX_THREADS];
  116. extern atomic_t fscache_n_objs_processed[FSCACHE_MAX_THREADS];
  117. extern atomic_t fscache_n_op_pend;
  118. extern atomic_t fscache_n_op_run;
  119. extern atomic_t fscache_n_op_enqueue;
  120. extern atomic_t fscache_n_op_deferred_release;
  121. extern atomic_t fscache_n_op_release;
  122. extern atomic_t fscache_n_op_gc;
  123. extern atomic_t fscache_n_op_cancelled;
  124. extern atomic_t fscache_n_op_rejected;
  125. extern atomic_t fscache_n_attr_changed;
  126. extern atomic_t fscache_n_attr_changed_ok;
  127. extern atomic_t fscache_n_attr_changed_nobufs;
  128. extern atomic_t fscache_n_attr_changed_nomem;
  129. extern atomic_t fscache_n_attr_changed_calls;
  130. extern atomic_t fscache_n_allocs;
  131. extern atomic_t fscache_n_allocs_ok;
  132. extern atomic_t fscache_n_allocs_wait;
  133. extern atomic_t fscache_n_allocs_nobufs;
  134. extern atomic_t fscache_n_allocs_intr;
  135. extern atomic_t fscache_n_alloc_ops;
  136. extern atomic_t fscache_n_alloc_op_waits;
  137. extern atomic_t fscache_n_retrievals;
  138. extern atomic_t fscache_n_retrievals_ok;
  139. extern atomic_t fscache_n_retrievals_wait;
  140. extern atomic_t fscache_n_retrievals_nodata;
  141. extern atomic_t fscache_n_retrievals_nobufs;
  142. extern atomic_t fscache_n_retrievals_intr;
  143. extern atomic_t fscache_n_retrievals_nomem;
  144. extern atomic_t fscache_n_retrieval_ops;
  145. extern atomic_t fscache_n_retrieval_op_waits;
  146. extern atomic_t fscache_n_stores;
  147. extern atomic_t fscache_n_stores_ok;
  148. extern atomic_t fscache_n_stores_again;
  149. extern atomic_t fscache_n_stores_nobufs;
  150. extern atomic_t fscache_n_stores_oom;
  151. extern atomic_t fscache_n_store_ops;
  152. extern atomic_t fscache_n_store_calls;
  153. extern atomic_t fscache_n_store_pages;
  154. extern atomic_t fscache_n_store_radix_deletes;
  155. extern atomic_t fscache_n_store_pages_over_limit;
  156. extern atomic_t fscache_n_marks;
  157. extern atomic_t fscache_n_uncaches;
  158. extern atomic_t fscache_n_acquires;
  159. extern atomic_t fscache_n_acquires_null;
  160. extern atomic_t fscache_n_acquires_no_cache;
  161. extern atomic_t fscache_n_acquires_ok;
  162. extern atomic_t fscache_n_acquires_nobufs;
  163. extern atomic_t fscache_n_acquires_oom;
  164. extern atomic_t fscache_n_updates;
  165. extern atomic_t fscache_n_updates_null;
  166. extern atomic_t fscache_n_updates_run;
  167. extern atomic_t fscache_n_relinquishes;
  168. extern atomic_t fscache_n_relinquishes_null;
  169. extern atomic_t fscache_n_relinquishes_waitcrt;
  170. extern atomic_t fscache_n_cookie_index;
  171. extern atomic_t fscache_n_cookie_data;
  172. extern atomic_t fscache_n_cookie_special;
  173. extern atomic_t fscache_n_object_alloc;
  174. extern atomic_t fscache_n_object_no_alloc;
  175. extern atomic_t fscache_n_object_lookups;
  176. extern atomic_t fscache_n_object_lookups_negative;
  177. extern atomic_t fscache_n_object_lookups_positive;
  178. extern atomic_t fscache_n_object_created;
  179. extern atomic_t fscache_n_object_avail;
  180. extern atomic_t fscache_n_object_dead;
  181. extern atomic_t fscache_n_checkaux_none;
  182. extern atomic_t fscache_n_checkaux_okay;
  183. extern atomic_t fscache_n_checkaux_update;
  184. extern atomic_t fscache_n_checkaux_obsolete;
  185. extern atomic_t fscache_n_cop_alloc_object;
  186. extern atomic_t fscache_n_cop_lookup_object;
  187. extern atomic_t fscache_n_cop_lookup_complete;
  188. extern atomic_t fscache_n_cop_grab_object;
  189. extern atomic_t fscache_n_cop_update_object;
  190. extern atomic_t fscache_n_cop_drop_object;
  191. extern atomic_t fscache_n_cop_put_object;
  192. extern atomic_t fscache_n_cop_sync_cache;
  193. extern atomic_t fscache_n_cop_attr_changed;
  194. extern atomic_t fscache_n_cop_read_or_alloc_page;
  195. extern atomic_t fscache_n_cop_read_or_alloc_pages;
  196. extern atomic_t fscache_n_cop_allocate_page;
  197. extern atomic_t fscache_n_cop_allocate_pages;
  198. extern atomic_t fscache_n_cop_write_page;
  199. extern atomic_t fscache_n_cop_uncache_page;
  200. extern atomic_t fscache_n_cop_dissociate_pages;
  201. static inline void fscache_stat(atomic_t *stat)
  202. {
  203. atomic_inc(stat);
  204. }
  205. static inline void fscache_stat_d(atomic_t *stat)
  206. {
  207. atomic_dec(stat);
  208. }
  209. extern const struct file_operations fscache_stats_fops;
  210. #else
  211. #define fscache_stat(stat) do {} while (0)
  212. #endif
  213. /*
  214. * raise an event on an object
  215. * - if the event is not masked for that object, then the object is
  216. * queued for attention by the thread pool.
  217. */
  218. static inline void fscache_raise_event(struct fscache_object *object,
  219. unsigned event)
  220. {
  221. if (!test_and_set_bit(event, &object->events) &&
  222. test_bit(event, &object->event_mask))
  223. fscache_enqueue_object(object);
  224. }
  225. /*
  226. * drop a reference to a cookie
  227. */
  228. static inline void fscache_cookie_put(struct fscache_cookie *cookie)
  229. {
  230. BUG_ON(atomic_read(&cookie->usage) <= 0);
  231. if (atomic_dec_and_test(&cookie->usage))
  232. __fscache_cookie_put(cookie);
  233. }
  234. /*
  235. * get an extra reference to a netfs retrieval context
  236. */
  237. static inline
  238. void *fscache_get_context(struct fscache_cookie *cookie, void *context)
  239. {
  240. if (cookie->def->get_context)
  241. cookie->def->get_context(cookie->netfs_data, context);
  242. return context;
  243. }
  244. /*
  245. * release a reference to a netfs retrieval context
  246. */
  247. static inline
  248. void fscache_put_context(struct fscache_cookie *cookie, void *context)
  249. {
  250. if (cookie->def->put_context)
  251. cookie->def->put_context(cookie->netfs_data, context);
  252. }
  253. /*****************************************************************************/
  254. /*
  255. * debug tracing
  256. */
  257. #define dbgprintk(FMT, ...) \
  258. printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  259. /* make sure we maintain the format strings, even when debugging is disabled */
  260. static inline __attribute__((format(printf, 1, 2)))
  261. void _dbprintk(const char *fmt, ...)
  262. {
  263. }
  264. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  265. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  266. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  267. #define kjournal(FMT, ...) _dbprintk(FMT, ##__VA_ARGS__)
  268. #ifdef __KDEBUG
  269. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  270. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  271. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  272. #elif defined(CONFIG_FSCACHE_DEBUG)
  273. #define _enter(FMT, ...) \
  274. do { \
  275. if (__do_kdebug(ENTER)) \
  276. kenter(FMT, ##__VA_ARGS__); \
  277. } while (0)
  278. #define _leave(FMT, ...) \
  279. do { \
  280. if (__do_kdebug(LEAVE)) \
  281. kleave(FMT, ##__VA_ARGS__); \
  282. } while (0)
  283. #define _debug(FMT, ...) \
  284. do { \
  285. if (__do_kdebug(DEBUG)) \
  286. kdebug(FMT, ##__VA_ARGS__); \
  287. } while (0)
  288. #else
  289. #define _enter(FMT, ...) _dbprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  290. #define _leave(FMT, ...) _dbprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  291. #define _debug(FMT, ...) _dbprintk(FMT, ##__VA_ARGS__)
  292. #endif
  293. /*
  294. * determine whether a particular optional debugging point should be logged
  295. * - we need to go through three steps to persuade cpp to correctly join the
  296. * shorthand in FSCACHE_DEBUG_LEVEL with its prefix
  297. */
  298. #define ____do_kdebug(LEVEL, POINT) \
  299. unlikely((fscache_debug & \
  300. (FSCACHE_POINT_##POINT << (FSCACHE_DEBUG_ ## LEVEL * 3))))
  301. #define ___do_kdebug(LEVEL, POINT) \
  302. ____do_kdebug(LEVEL, POINT)
  303. #define __do_kdebug(POINT) \
  304. ___do_kdebug(FSCACHE_DEBUG_LEVEL, POINT)
  305. #define FSCACHE_DEBUG_CACHE 0
  306. #define FSCACHE_DEBUG_COOKIE 1
  307. #define FSCACHE_DEBUG_PAGE 2
  308. #define FSCACHE_DEBUG_OPERATION 3
  309. #define FSCACHE_POINT_ENTER 1
  310. #define FSCACHE_POINT_LEAVE 2
  311. #define FSCACHE_POINT_DEBUG 4
  312. #ifndef FSCACHE_DEBUG_LEVEL
  313. #define FSCACHE_DEBUG_LEVEL CACHE
  314. #endif
  315. /*
  316. * assertions
  317. */
  318. #if 1 /* defined(__KDEBUGALL) */
  319. #define ASSERT(X) \
  320. do { \
  321. if (unlikely(!(X))) { \
  322. printk(KERN_ERR "\n"); \
  323. printk(KERN_ERR "FS-Cache: Assertion failed\n"); \
  324. BUG(); \
  325. } \
  326. } while (0)
  327. #define ASSERTCMP(X, OP, Y) \
  328. do { \
  329. if (unlikely(!((X) OP (Y)))) { \
  330. printk(KERN_ERR "\n"); \
  331. printk(KERN_ERR "FS-Cache: Assertion failed\n"); \
  332. printk(KERN_ERR "%lx " #OP " %lx is false\n", \
  333. (unsigned long)(X), (unsigned long)(Y)); \
  334. BUG(); \
  335. } \
  336. } while (0)
  337. #define ASSERTIF(C, X) \
  338. do { \
  339. if (unlikely((C) && !(X))) { \
  340. printk(KERN_ERR "\n"); \
  341. printk(KERN_ERR "FS-Cache: Assertion failed\n"); \
  342. BUG(); \
  343. } \
  344. } while (0)
  345. #define ASSERTIFCMP(C, X, OP, Y) \
  346. do { \
  347. if (unlikely((C) && !((X) OP (Y)))) { \
  348. printk(KERN_ERR "\n"); \
  349. printk(KERN_ERR "FS-Cache: Assertion failed\n"); \
  350. printk(KERN_ERR "%lx " #OP " %lx is false\n", \
  351. (unsigned long)(X), (unsigned long)(Y)); \
  352. BUG(); \
  353. } \
  354. } while (0)
  355. #else
  356. #define ASSERT(X) do {} while (0)
  357. #define ASSERTCMP(X, OP, Y) do {} while (0)
  358. #define ASSERTIF(C, X) do {} while (0)
  359. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  360. #endif /* assert or not */