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