internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #ifdef pr_fmt
  24. #undef pr_fmt
  25. #endif
  26. #define pr_fmt(fmt) "FS-Cache: " fmt
  27. #include <linux/fscache-cache.h>
  28. #include <trace/events/fscache.h>
  29. #include <linux/sched.h>
  30. #include <linux/seq_file.h>
  31. #define FSCACHE_MIN_THREADS 4
  32. #define FSCACHE_MAX_THREADS 32
  33. /*
  34. * cache.c
  35. */
  36. extern struct list_head fscache_cache_list;
  37. extern struct rw_semaphore fscache_addremove_sem;
  38. extern struct fscache_cache *fscache_select_cache_for_object(
  39. struct fscache_cookie *);
  40. /*
  41. * cookie.c
  42. */
  43. extern struct kmem_cache *fscache_cookie_jar;
  44. extern void fscache_free_cookie(struct fscache_cookie *);
  45. extern struct fscache_cookie *fscache_alloc_cookie(struct fscache_cookie *,
  46. const struct fscache_cookie_def *,
  47. const void *, size_t,
  48. const void *, size_t,
  49. void *, loff_t);
  50. extern struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *);
  51. extern void fscache_cookie_put(struct fscache_cookie *,
  52. enum fscache_cookie_trace);
  53. /*
  54. * fsdef.c
  55. */
  56. extern struct fscache_cookie fscache_fsdef_index;
  57. extern struct fscache_cookie_def fscache_fsdef_netfs_def;
  58. /*
  59. * histogram.c
  60. */
  61. #ifdef CONFIG_FSCACHE_HISTOGRAM
  62. extern atomic_t fscache_obj_instantiate_histogram[HZ];
  63. extern atomic_t fscache_objs_histogram[HZ];
  64. extern atomic_t fscache_ops_histogram[HZ];
  65. extern atomic_t fscache_retrieval_delay_histogram[HZ];
  66. extern atomic_t fscache_retrieval_histogram[HZ];
  67. static inline void fscache_hist(atomic_t histogram[], unsigned long start_jif)
  68. {
  69. unsigned long jif = jiffies - start_jif;
  70. if (jif >= HZ)
  71. jif = HZ - 1;
  72. atomic_inc(&histogram[jif]);
  73. }
  74. extern const struct seq_operations fscache_histogram_ops;
  75. #else
  76. #define fscache_hist(hist, start_jif) do {} while (0)
  77. #endif
  78. /*
  79. * main.c
  80. */
  81. extern unsigned fscache_defer_lookup;
  82. extern unsigned fscache_defer_create;
  83. extern unsigned fscache_debug;
  84. extern struct kobject *fscache_root;
  85. extern struct workqueue_struct *fscache_object_wq;
  86. extern struct workqueue_struct *fscache_op_wq;
  87. DECLARE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
  88. static inline bool fscache_object_congested(void)
  89. {
  90. return workqueue_congested(WORK_CPU_UNBOUND, fscache_object_wq);
  91. }
  92. /*
  93. * object.c
  94. */
  95. extern void fscache_enqueue_object(struct fscache_object *);
  96. /*
  97. * object-list.c
  98. */
  99. #ifdef CONFIG_FSCACHE_OBJECT_LIST
  100. extern const struct file_operations fscache_objlist_fops;
  101. extern void fscache_objlist_add(struct fscache_object *);
  102. extern void fscache_objlist_remove(struct fscache_object *);
  103. #else
  104. #define fscache_objlist_add(object) do {} while(0)
  105. #define fscache_objlist_remove(object) do {} while(0)
  106. #endif
  107. /*
  108. * operation.c
  109. */
  110. extern int fscache_submit_exclusive_op(struct fscache_object *,
  111. struct fscache_operation *);
  112. extern int fscache_submit_op(struct fscache_object *,
  113. struct fscache_operation *);
  114. extern int fscache_cancel_op(struct fscache_operation *, bool);
  115. extern void fscache_cancel_all_ops(struct fscache_object *);
  116. extern void fscache_abort_object(struct fscache_object *);
  117. extern void fscache_start_operations(struct fscache_object *);
  118. extern void fscache_operation_gc(struct work_struct *);
  119. /*
  120. * page.c
  121. */
  122. extern int fscache_wait_for_deferred_lookup(struct fscache_cookie *);
  123. extern int fscache_wait_for_operation_activation(struct fscache_object *,
  124. struct fscache_operation *,
  125. atomic_t *,
  126. atomic_t *);
  127. extern void fscache_invalidate_writes(struct fscache_cookie *);
  128. /*
  129. * proc.c
  130. */
  131. #ifdef CONFIG_PROC_FS
  132. extern int __init fscache_proc_init(void);
  133. extern void fscache_proc_cleanup(void);
  134. #else
  135. #define fscache_proc_init() (0)
  136. #define fscache_proc_cleanup() do {} while (0)
  137. #endif
  138. /*
  139. * stats.c
  140. */
  141. #ifdef CONFIG_FSCACHE_STATS
  142. extern atomic_t fscache_n_ops_processed[FSCACHE_MAX_THREADS];
  143. extern atomic_t fscache_n_objs_processed[FSCACHE_MAX_THREADS];
  144. extern atomic_t fscache_n_op_pend;
  145. extern atomic_t fscache_n_op_run;
  146. extern atomic_t fscache_n_op_enqueue;
  147. extern atomic_t fscache_n_op_deferred_release;
  148. extern atomic_t fscache_n_op_initialised;
  149. extern atomic_t fscache_n_op_release;
  150. extern atomic_t fscache_n_op_gc;
  151. extern atomic_t fscache_n_op_cancelled;
  152. extern atomic_t fscache_n_op_rejected;
  153. extern atomic_t fscache_n_attr_changed;
  154. extern atomic_t fscache_n_attr_changed_ok;
  155. extern atomic_t fscache_n_attr_changed_nobufs;
  156. extern atomic_t fscache_n_attr_changed_nomem;
  157. extern atomic_t fscache_n_attr_changed_calls;
  158. extern atomic_t fscache_n_allocs;
  159. extern atomic_t fscache_n_allocs_ok;
  160. extern atomic_t fscache_n_allocs_wait;
  161. extern atomic_t fscache_n_allocs_nobufs;
  162. extern atomic_t fscache_n_allocs_intr;
  163. extern atomic_t fscache_n_allocs_object_dead;
  164. extern atomic_t fscache_n_alloc_ops;
  165. extern atomic_t fscache_n_alloc_op_waits;
  166. extern atomic_t fscache_n_retrievals;
  167. extern atomic_t fscache_n_retrievals_ok;
  168. extern atomic_t fscache_n_retrievals_wait;
  169. extern atomic_t fscache_n_retrievals_nodata;
  170. extern atomic_t fscache_n_retrievals_nobufs;
  171. extern atomic_t fscache_n_retrievals_intr;
  172. extern atomic_t fscache_n_retrievals_nomem;
  173. extern atomic_t fscache_n_retrievals_object_dead;
  174. extern atomic_t fscache_n_retrieval_ops;
  175. extern atomic_t fscache_n_retrieval_op_waits;
  176. extern atomic_t fscache_n_stores;
  177. extern atomic_t fscache_n_stores_ok;
  178. extern atomic_t fscache_n_stores_again;
  179. extern atomic_t fscache_n_stores_nobufs;
  180. extern atomic_t fscache_n_stores_oom;
  181. extern atomic_t fscache_n_store_ops;
  182. extern atomic_t fscache_n_store_calls;
  183. extern atomic_t fscache_n_store_pages;
  184. extern atomic_t fscache_n_store_radix_deletes;
  185. extern atomic_t fscache_n_store_pages_over_limit;
  186. extern atomic_t fscache_n_store_vmscan_not_storing;
  187. extern atomic_t fscache_n_store_vmscan_gone;
  188. extern atomic_t fscache_n_store_vmscan_busy;
  189. extern atomic_t fscache_n_store_vmscan_cancelled;
  190. extern atomic_t fscache_n_store_vmscan_wait;
  191. extern atomic_t fscache_n_marks;
  192. extern atomic_t fscache_n_uncaches;
  193. extern atomic_t fscache_n_acquires;
  194. extern atomic_t fscache_n_acquires_null;
  195. extern atomic_t fscache_n_acquires_no_cache;
  196. extern atomic_t fscache_n_acquires_ok;
  197. extern atomic_t fscache_n_acquires_nobufs;
  198. extern atomic_t fscache_n_acquires_oom;
  199. extern atomic_t fscache_n_invalidates;
  200. extern atomic_t fscache_n_invalidates_run;
  201. extern atomic_t fscache_n_updates;
  202. extern atomic_t fscache_n_updates_null;
  203. extern atomic_t fscache_n_updates_run;
  204. extern atomic_t fscache_n_relinquishes;
  205. extern atomic_t fscache_n_relinquishes_null;
  206. extern atomic_t fscache_n_relinquishes_waitcrt;
  207. extern atomic_t fscache_n_relinquishes_retire;
  208. extern atomic_t fscache_n_cookie_index;
  209. extern atomic_t fscache_n_cookie_data;
  210. extern atomic_t fscache_n_cookie_special;
  211. extern atomic_t fscache_n_object_alloc;
  212. extern atomic_t fscache_n_object_no_alloc;
  213. extern atomic_t fscache_n_object_lookups;
  214. extern atomic_t fscache_n_object_lookups_negative;
  215. extern atomic_t fscache_n_object_lookups_positive;
  216. extern atomic_t fscache_n_object_lookups_timed_out;
  217. extern atomic_t fscache_n_object_created;
  218. extern atomic_t fscache_n_object_avail;
  219. extern atomic_t fscache_n_object_dead;
  220. extern atomic_t fscache_n_checkaux_none;
  221. extern atomic_t fscache_n_checkaux_okay;
  222. extern atomic_t fscache_n_checkaux_update;
  223. extern atomic_t fscache_n_checkaux_obsolete;
  224. extern atomic_t fscache_n_cop_alloc_object;
  225. extern atomic_t fscache_n_cop_lookup_object;
  226. extern atomic_t fscache_n_cop_lookup_complete;
  227. extern atomic_t fscache_n_cop_grab_object;
  228. extern atomic_t fscache_n_cop_invalidate_object;
  229. extern atomic_t fscache_n_cop_update_object;
  230. extern atomic_t fscache_n_cop_drop_object;
  231. extern atomic_t fscache_n_cop_put_object;
  232. extern atomic_t fscache_n_cop_sync_cache;
  233. extern atomic_t fscache_n_cop_attr_changed;
  234. extern atomic_t fscache_n_cop_read_or_alloc_page;
  235. extern atomic_t fscache_n_cop_read_or_alloc_pages;
  236. extern atomic_t fscache_n_cop_allocate_page;
  237. extern atomic_t fscache_n_cop_allocate_pages;
  238. extern atomic_t fscache_n_cop_write_page;
  239. extern atomic_t fscache_n_cop_uncache_page;
  240. extern atomic_t fscache_n_cop_dissociate_pages;
  241. extern atomic_t fscache_n_cache_no_space_reject;
  242. extern atomic_t fscache_n_cache_stale_objects;
  243. extern atomic_t fscache_n_cache_retired_objects;
  244. extern atomic_t fscache_n_cache_culled_objects;
  245. static inline void fscache_stat(atomic_t *stat)
  246. {
  247. atomic_inc(stat);
  248. }
  249. static inline void fscache_stat_d(atomic_t *stat)
  250. {
  251. atomic_dec(stat);
  252. }
  253. #define __fscache_stat(stat) (stat)
  254. int fscache_stats_show(struct seq_file *m, void *v);
  255. #else
  256. #define __fscache_stat(stat) (NULL)
  257. #define fscache_stat(stat) do {} while (0)
  258. #define fscache_stat_d(stat) do {} while (0)
  259. #endif
  260. /*
  261. * raise an event on an object
  262. * - if the event is not masked for that object, then the object is
  263. * queued for attention by the thread pool.
  264. */
  265. static inline void fscache_raise_event(struct fscache_object *object,
  266. unsigned event)
  267. {
  268. BUG_ON(event >= NR_FSCACHE_OBJECT_EVENTS);
  269. #if 0
  270. printk("*** fscache_raise_event(OBJ%d{%lx},%x)\n",
  271. object->debug_id, object->event_mask, (1 << event));
  272. #endif
  273. if (!test_and_set_bit(event, &object->events) &&
  274. test_bit(event, &object->event_mask))
  275. fscache_enqueue_object(object);
  276. }
  277. static inline void fscache_cookie_get(struct fscache_cookie *cookie,
  278. enum fscache_cookie_trace where)
  279. {
  280. int usage = atomic_inc_return(&cookie->usage);
  281. trace_fscache_cookie(cookie, where, usage);
  282. }
  283. /*
  284. * get an extra reference to a netfs retrieval context
  285. */
  286. static inline
  287. void *fscache_get_context(struct fscache_cookie *cookie, void *context)
  288. {
  289. if (cookie->def->get_context)
  290. cookie->def->get_context(cookie->netfs_data, context);
  291. return context;
  292. }
  293. /*
  294. * release a reference to a netfs retrieval context
  295. */
  296. static inline
  297. void fscache_put_context(struct fscache_cookie *cookie, void *context)
  298. {
  299. if (cookie->def->put_context)
  300. cookie->def->put_context(cookie->netfs_data, context);
  301. }
  302. /*
  303. * Update the auxiliary data on a cookie.
  304. */
  305. static inline
  306. void fscache_update_aux(struct fscache_cookie *cookie, const void *aux_data)
  307. {
  308. void *p;
  309. if (!aux_data)
  310. return;
  311. if (cookie->aux_len <= sizeof(cookie->inline_aux))
  312. p = cookie->inline_aux;
  313. else
  314. p = cookie->aux;
  315. if (memcmp(p, aux_data, cookie->aux_len) != 0) {
  316. memcpy(p, aux_data, cookie->aux_len);
  317. set_bit(FSCACHE_COOKIE_AUX_UPDATED, &cookie->flags);
  318. }
  319. }
  320. /*****************************************************************************/
  321. /*
  322. * debug tracing
  323. */
  324. #define dbgprintk(FMT, ...) \
  325. printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  326. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  327. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  328. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  329. #define kjournal(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  330. #ifdef __KDEBUG
  331. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  332. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  333. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  334. #elif defined(CONFIG_FSCACHE_DEBUG)
  335. #define _enter(FMT, ...) \
  336. do { \
  337. if (__do_kdebug(ENTER)) \
  338. kenter(FMT, ##__VA_ARGS__); \
  339. } while (0)
  340. #define _leave(FMT, ...) \
  341. do { \
  342. if (__do_kdebug(LEAVE)) \
  343. kleave(FMT, ##__VA_ARGS__); \
  344. } while (0)
  345. #define _debug(FMT, ...) \
  346. do { \
  347. if (__do_kdebug(DEBUG)) \
  348. kdebug(FMT, ##__VA_ARGS__); \
  349. } while (0)
  350. #else
  351. #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  352. #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  353. #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  354. #endif
  355. /*
  356. * determine whether a particular optional debugging point should be logged
  357. * - we need to go through three steps to persuade cpp to correctly join the
  358. * shorthand in FSCACHE_DEBUG_LEVEL with its prefix
  359. */
  360. #define ____do_kdebug(LEVEL, POINT) \
  361. unlikely((fscache_debug & \
  362. (FSCACHE_POINT_##POINT << (FSCACHE_DEBUG_ ## LEVEL * 3))))
  363. #define ___do_kdebug(LEVEL, POINT) \
  364. ____do_kdebug(LEVEL, POINT)
  365. #define __do_kdebug(POINT) \
  366. ___do_kdebug(FSCACHE_DEBUG_LEVEL, POINT)
  367. #define FSCACHE_DEBUG_CACHE 0
  368. #define FSCACHE_DEBUG_COOKIE 1
  369. #define FSCACHE_DEBUG_PAGE 2
  370. #define FSCACHE_DEBUG_OPERATION 3
  371. #define FSCACHE_POINT_ENTER 1
  372. #define FSCACHE_POINT_LEAVE 2
  373. #define FSCACHE_POINT_DEBUG 4
  374. #ifndef FSCACHE_DEBUG_LEVEL
  375. #define FSCACHE_DEBUG_LEVEL CACHE
  376. #endif
  377. /*
  378. * assertions
  379. */
  380. #if 1 /* defined(__KDEBUGALL) */
  381. #define ASSERT(X) \
  382. do { \
  383. if (unlikely(!(X))) { \
  384. pr_err("\n"); \
  385. pr_err("Assertion failed\n"); \
  386. BUG(); \
  387. } \
  388. } while (0)
  389. #define ASSERTCMP(X, OP, Y) \
  390. do { \
  391. if (unlikely(!((X) OP (Y)))) { \
  392. pr_err("\n"); \
  393. pr_err("Assertion failed\n"); \
  394. pr_err("%lx " #OP " %lx is false\n", \
  395. (unsigned long)(X), (unsigned long)(Y)); \
  396. BUG(); \
  397. } \
  398. } while (0)
  399. #define ASSERTIF(C, X) \
  400. do { \
  401. if (unlikely((C) && !(X))) { \
  402. pr_err("\n"); \
  403. pr_err("Assertion failed\n"); \
  404. BUG(); \
  405. } \
  406. } while (0)
  407. #define ASSERTIFCMP(C, X, OP, Y) \
  408. do { \
  409. if (unlikely((C) && !((X) OP (Y)))) { \
  410. pr_err("\n"); \
  411. pr_err("Assertion failed\n"); \
  412. pr_err("%lx " #OP " %lx is false\n", \
  413. (unsigned long)(X), (unsigned long)(Y)); \
  414. BUG(); \
  415. } \
  416. } while (0)
  417. #else
  418. #define ASSERT(X) do {} while (0)
  419. #define ASSERTCMP(X, OP, Y) do {} while (0)
  420. #define ASSERTIF(C, X) do {} while (0)
  421. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  422. #endif /* assert or not */