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