1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* General filesystem caching backing cache interface
3  *
4  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * NOTE!!! See:
8  *
9  *	Documentation/filesystems/caching/backend-api.rst
10  *
11  * for a description of the cache backend interface declared here.
12  */
13 
14 #ifndef _LINUX_FSCACHE_CACHE_H
15 #define _LINUX_FSCACHE_CACHE_H
16 
17 #include <linux/fscache.h>
18 #include <linux/sched.h>
19 #include <linux/workqueue.h>
20 
21 #define NR_MAXCACHES BITS_PER_LONG
22 
23 struct fscache_cache;
24 struct fscache_cache_ops;
25 struct fscache_object;
26 struct fscache_operation;
27 
28 enum fscache_obj_ref_trace {
29 	fscache_obj_get_add_to_deps,
30 	fscache_obj_get_queue,
31 	fscache_obj_put_alloc_fail,
32 	fscache_obj_put_attach_fail,
33 	fscache_obj_put_drop_obj,
34 	fscache_obj_put_enq_dep,
35 	fscache_obj_put_queue,
36 	fscache_obj_put_work,
37 	fscache_obj_ref__nr_traces
38 };
39 
40 /*
41  * cache tag definition
42  */
43 struct fscache_cache_tag {
44 	struct list_head	link;
45 	struct fscache_cache	*cache;		/* cache referred to by this tag */
46 	unsigned long		flags;
47 #define FSCACHE_TAG_RESERVED	0		/* T if tag is reserved for a cache */
48 	atomic_t		usage;
49 	char			name[];	/* tag name */
50 };
51 
52 /*
53  * cache definition
54  */
55 struct fscache_cache {
56 	const struct fscache_cache_ops *ops;
57 	struct fscache_cache_tag *tag;		/* tag representing this cache */
58 	struct kobject		*kobj;		/* system representation of this cache */
59 	struct list_head	link;		/* link in list of caches */
60 	size_t			max_index_size;	/* maximum size of index data */
61 	char			identifier[36];	/* cache label */
62 
63 	/* node management */
64 	struct work_struct	op_gc;		/* operation garbage collector */
65 	struct list_head	object_list;	/* list of data/index objects */
66 	struct list_head	op_gc_list;	/* list of ops to be deleted */
67 	spinlock_t		object_list_lock;
68 	spinlock_t		op_gc_list_lock;
69 	atomic_t		object_count;	/* no. of live objects in this cache */
70 	struct fscache_object	*fsdef;		/* object for the fsdef index */
71 	unsigned long		flags;
72 #define FSCACHE_IOERROR		0	/* cache stopped on I/O error */
73 #define FSCACHE_CACHE_WITHDRAWN	1	/* cache has been withdrawn */
74 };
75 
76 extern wait_queue_head_t fscache_cache_cleared_wq;
77 
78 /*
79  * operation to be applied to a cache object
80  * - retrieval initiation operations are done in the context of the process
81  *   that issued them, and not in an async thread pool
82  */
83 typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
84 typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
85 typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op);
86 
87 enum fscache_operation_state {
88 	FSCACHE_OP_ST_BLANK,		/* Op is not yet submitted */
89 	FSCACHE_OP_ST_INITIALISED,	/* Op is initialised */
90 	FSCACHE_OP_ST_PENDING,		/* Op is blocked from running */
91 	FSCACHE_OP_ST_IN_PROGRESS,	/* Op is in progress */
92 	FSCACHE_OP_ST_COMPLETE,		/* Op is complete */
93 	FSCACHE_OP_ST_CANCELLED,	/* Op has been cancelled */
94 	FSCACHE_OP_ST_DEAD		/* Op is now dead */
95 };
96 
97 struct fscache_operation {
98 	struct work_struct	work;		/* record for async ops */
99 	struct list_head	pend_link;	/* link in object->pending_ops */
100 	struct fscache_object	*object;	/* object to be operated upon */
101 
102 	unsigned long		flags;
103 #define FSCACHE_OP_TYPE		0x000f	/* operation type */
104 #define FSCACHE_OP_ASYNC	0x0001	/* - async op, processor may sleep for disk */
105 #define FSCACHE_OP_MYTHREAD	0x0002	/* - processing is done be issuing thread, not pool */
106 #define FSCACHE_OP_WAITING	4	/* cleared when op is woken */
107 #define FSCACHE_OP_EXCLUSIVE	5	/* exclusive op, other ops must wait */
108 #define FSCACHE_OP_DEC_READ_CNT	6	/* decrement object->n_reads on destruction */
109 #define FSCACHE_OP_UNUSE_COOKIE	7	/* call fscache_unuse_cookie() on completion */
110 #define FSCACHE_OP_KEEP_FLAGS	0x00f0	/* flags to keep when repurposing an op */
111 
112 	enum fscache_operation_state state;
113 	atomic_t		usage;
114 	unsigned		debug_id;	/* debugging ID */
115 
116 	/* operation processor callback
117 	 * - can be NULL if FSCACHE_OP_WAITING is going to be used to perform
118 	 *   the op in a non-pool thread */
119 	fscache_operation_processor_t processor;
120 
121 	/* Operation cancellation cleanup (optional) */
122 	fscache_operation_cancel_t cancel;
123 
124 	/* operation releaser */
125 	fscache_operation_release_t release;
126 };
127 
128 extern atomic_t fscache_op_debug_id;
129 extern void fscache_op_work_func(struct work_struct *work);
130 
131 extern void fscache_enqueue_operation(struct fscache_operation *);
132 extern void fscache_op_complete(struct fscache_operation *, bool);
133 extern void fscache_put_operation(struct fscache_operation *);
134 extern void fscache_operation_init(struct fscache_cookie *,
135 				   struct fscache_operation *,
136 				   fscache_operation_processor_t,
137 				   fscache_operation_cancel_t,
138 				   fscache_operation_release_t);
139 
140 /*
141  * data read operation
142  */
143 struct fscache_retrieval {
144 	struct fscache_operation op;
145 	struct fscache_cookie	*cookie;	/* The netfs cookie */
146 	struct address_space	*mapping;	/* netfs pages */
147 	fscache_rw_complete_t	end_io_func;	/* function to call on I/O completion */
148 	void			*context;	/* netfs read context (pinned) */
149 	struct list_head	to_do;		/* list of things to be done by the backend */
150 	atomic_t		n_pages;	/* number of pages to be retrieved */
151 };
152 
153 typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
154 					     struct page *page,
155 					     gfp_t gfp);
156 
157 typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op,
158 					      struct list_head *pages,
159 					      unsigned *nr_pages,
160 					      gfp_t gfp);
161 
162 /**
163  * fscache_get_retrieval - Get an extra reference on a retrieval operation
164  * @op: The retrieval operation to get a reference on
165  *
166  * Get an extra reference on a retrieval operation.
167  */
168 static inline
fscache_get_retrieval(struct fscache_retrieval * op)169 struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op)
170 {
171 	atomic_inc(&op->op.usage);
172 	return op;
173 }
174 
175 /**
176  * fscache_enqueue_retrieval - Enqueue a retrieval operation for processing
177  * @op: The retrieval operation affected
178  *
179  * Enqueue a retrieval operation for processing by the FS-Cache thread pool.
180  */
fscache_enqueue_retrieval(struct fscache_retrieval * op)181 static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
182 {
183 	fscache_enqueue_operation(&op->op);
184 }
185 
186 /**
187  * fscache_retrieval_complete - Record (partial) completion of a retrieval
188  * @op: The retrieval operation affected
189  * @n_pages: The number of pages to account for
190  */
fscache_retrieval_complete(struct fscache_retrieval * op,int n_pages)191 static inline void fscache_retrieval_complete(struct fscache_retrieval *op,
192 					      int n_pages)
193 {
194 	if (atomic_sub_return_relaxed(n_pages, &op->n_pages) <= 0)
195 		fscache_op_complete(&op->op, false);
196 }
197 
198 /**
199  * fscache_put_retrieval - Drop a reference to a retrieval operation
200  * @op: The retrieval operation affected
201  *
202  * Drop a reference to a retrieval operation.
203  */
fscache_put_retrieval(struct fscache_retrieval * op)204 static inline void fscache_put_retrieval(struct fscache_retrieval *op)
205 {
206 	fscache_put_operation(&op->op);
207 }
208 
209 /*
210  * cached page storage work item
211  * - used to do three things:
212  *   - batch writes to the cache
213  *   - do cache writes asynchronously
214  *   - defer writes until cache object lookup completion
215  */
216 struct fscache_storage {
217 	struct fscache_operation op;
218 	pgoff_t			store_limit;	/* don't write more than this */
219 };
220 
221 /*
222  * cache operations
223  */
224 struct fscache_cache_ops {
225 	/* name of cache provider */
226 	const char *name;
227 
228 	/* allocate an object record for a cookie */
229 	struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
230 					       struct fscache_cookie *cookie);
231 
232 	/* look up the object for a cookie
233 	 * - return -ETIMEDOUT to be requeued
234 	 */
235 	int (*lookup_object)(struct fscache_object *object);
236 
237 	/* finished looking up */
238 	void (*lookup_complete)(struct fscache_object *object);
239 
240 	/* increment the usage count on this object (may fail if unmounting) */
241 	struct fscache_object *(*grab_object)(struct fscache_object *object,
242 					      enum fscache_obj_ref_trace why);
243 
244 	/* pin an object in the cache */
245 	int (*pin_object)(struct fscache_object *object);
246 
247 	/* unpin an object in the cache */
248 	void (*unpin_object)(struct fscache_object *object);
249 
250 	/* check the consistency between the backing cache and the FS-Cache
251 	 * cookie */
252 	int (*check_consistency)(struct fscache_operation *op);
253 
254 	/* store the updated auxiliary data on an object */
255 	void (*update_object)(struct fscache_object *object);
256 
257 	/* Invalidate an object */
258 	void (*invalidate_object)(struct fscache_operation *op);
259 
260 	/* discard the resources pinned by an object and effect retirement if
261 	 * necessary */
262 	void (*drop_object)(struct fscache_object *object);
263 
264 	/* dispose of a reference to an object */
265 	void (*put_object)(struct fscache_object *object,
266 			   enum fscache_obj_ref_trace why);
267 
268 	/* sync a cache */
269 	void (*sync_cache)(struct fscache_cache *cache);
270 
271 	/* notification that the attributes of a non-index object (such as
272 	 * i_size) have changed */
273 	int (*attr_changed)(struct fscache_object *object);
274 
275 	/* reserve space for an object's data and associated metadata */
276 	int (*reserve_space)(struct fscache_object *object, loff_t i_size);
277 
278 	/* request a backing block for a page be read or allocated in the
279 	 * cache */
280 	fscache_page_retrieval_func_t read_or_alloc_page;
281 
282 	/* request backing blocks for a list of pages be read or allocated in
283 	 * the cache */
284 	fscache_pages_retrieval_func_t read_or_alloc_pages;
285 
286 	/* request a backing block for a page be allocated in the cache so that
287 	 * it can be written directly */
288 	fscache_page_retrieval_func_t allocate_page;
289 
290 	/* request backing blocks for pages be allocated in the cache so that
291 	 * they can be written directly */
292 	fscache_pages_retrieval_func_t allocate_pages;
293 
294 	/* write a page to its backing block in the cache */
295 	int (*write_page)(struct fscache_storage *op, struct page *page);
296 
297 	/* detach backing block from a page (optional)
298 	 * - must release the cookie lock before returning
299 	 * - may sleep
300 	 */
301 	void (*uncache_page)(struct fscache_object *object,
302 			     struct page *page);
303 
304 	/* dissociate a cache from all the pages it was backing */
305 	void (*dissociate_pages)(struct fscache_cache *cache);
306 
307 	/* Begin a read operation for the netfs lib */
308 	int (*begin_read_operation)(struct netfs_read_request *rreq,
309 				    struct fscache_retrieval *op);
310 };
311 
312 extern struct fscache_cookie fscache_fsdef_index;
313 
314 /*
315  * Event list for fscache_object::{event_mask,events}
316  */
317 enum {
318 	FSCACHE_OBJECT_EV_NEW_CHILD,	/* T if object has a new child */
319 	FSCACHE_OBJECT_EV_PARENT_READY,	/* T if object's parent is ready */
320 	FSCACHE_OBJECT_EV_UPDATE,	/* T if object should be updated */
321 	FSCACHE_OBJECT_EV_INVALIDATE,	/* T if cache requested object invalidation */
322 	FSCACHE_OBJECT_EV_CLEARED,	/* T if accessors all gone */
323 	FSCACHE_OBJECT_EV_ERROR,	/* T if fatal error occurred during processing */
324 	FSCACHE_OBJECT_EV_KILL,		/* T if netfs relinquished or cache withdrew object */
325 	NR_FSCACHE_OBJECT_EVENTS
326 };
327 
328 #define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1)
329 
330 /*
331  * States for object state machine.
332  */
333 struct fscache_transition {
334 	unsigned long events;
335 	const struct fscache_state *transit_to;
336 };
337 
338 struct fscache_state {
339 	char name[24];
340 	char short_name[8];
341 	const struct fscache_state *(*work)(struct fscache_object *object,
342 					    int event);
343 	const struct fscache_transition transitions[];
344 };
345 
346 /*
347  * on-disk cache file or index handle
348  */
349 struct fscache_object {
350 	const struct fscache_state *state;	/* Object state machine state */
351 	const struct fscache_transition *oob_table; /* OOB state transition table */
352 	int			debug_id;	/* debugging ID */
353 	int			n_children;	/* number of child objects */
354 	int			n_ops;		/* number of extant ops on object */
355 	int			n_obj_ops;	/* number of object ops outstanding on object */
356 	int			n_in_progress;	/* number of ops in progress */
357 	int			n_exclusive;	/* number of exclusive ops queued or in progress */
358 	atomic_t		n_reads;	/* number of read ops in progress */
359 	spinlock_t		lock;		/* state and operations lock */
360 
361 	unsigned long		lookup_jif;	/* time at which lookup started */
362 	unsigned long		oob_event_mask;	/* OOB events this object is interested in */
363 	unsigned long		event_mask;	/* events this object is interested in */
364 	unsigned long		events;		/* events to be processed by this object
365 						 * (order is important - using fls) */
366 
367 	unsigned long		flags;
368 #define FSCACHE_OBJECT_LOCK		0	/* T if object is busy being processed */
369 #define FSCACHE_OBJECT_PENDING_WRITE	1	/* T if object has pending write */
370 #define FSCACHE_OBJECT_WAITING		2	/* T if object is waiting on its parent */
371 #define FSCACHE_OBJECT_IS_LIVE		3	/* T if object is not withdrawn or relinquished */
372 #define FSCACHE_OBJECT_IS_LOOKED_UP	4	/* T if object has been looked up */
373 #define FSCACHE_OBJECT_IS_AVAILABLE	5	/* T if object has become active */
374 #define FSCACHE_OBJECT_RETIRED		6	/* T if object was retired on relinquishment */
375 #define FSCACHE_OBJECT_KILLED_BY_CACHE	7	/* T if object was killed by the cache */
376 #define FSCACHE_OBJECT_RUN_AFTER_DEAD	8	/* T if object has been dispatched after death */
377 
378 	struct list_head	cache_link;	/* link in cache->object_list */
379 	struct hlist_node	cookie_link;	/* link in cookie->backing_objects */
380 	struct fscache_cache	*cache;		/* cache that supplied this object */
381 	struct fscache_cookie	*cookie;	/* netfs's file/index object */
382 	struct fscache_object	*parent;	/* parent object */
383 	struct work_struct	work;		/* attention scheduling record */
384 	struct list_head	dependents;	/* FIFO of dependent objects */
385 	struct list_head	dep_link;	/* link in parent's dependents list */
386 	struct list_head	pending_ops;	/* unstarted operations on this object */
387 	pgoff_t			store_limit;	/* current storage limit */
388 	loff_t			store_limit_l;	/* current storage limit */
389 };
390 
391 extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *,
392 				struct fscache_cache *);
393 extern void fscache_object_destroy(struct fscache_object *);
394 
395 extern void fscache_object_lookup_negative(struct fscache_object *object);
396 extern void fscache_obtained_object(struct fscache_object *object);
397 
fscache_object_is_live(struct fscache_object * object)398 static inline bool fscache_object_is_live(struct fscache_object *object)
399 {
400 	return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
401 }
402 
fscache_object_is_dying(struct fscache_object * object)403 static inline bool fscache_object_is_dying(struct fscache_object *object)
404 {
405 	return !fscache_object_is_live(object);
406 }
407 
fscache_object_is_available(struct fscache_object * object)408 static inline bool fscache_object_is_available(struct fscache_object *object)
409 {
410 	return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
411 }
412 
fscache_cache_is_broken(struct fscache_object * object)413 static inline bool fscache_cache_is_broken(struct fscache_object *object)
414 {
415 	return test_bit(FSCACHE_IOERROR, &object->cache->flags);
416 }
417 
fscache_object_is_active(struct fscache_object * object)418 static inline bool fscache_object_is_active(struct fscache_object *object)
419 {
420 	return fscache_object_is_available(object) &&
421 		fscache_object_is_live(object) &&
422 		!fscache_cache_is_broken(object);
423 }
424 
425 /**
426  * fscache_object_destroyed - Note destruction of an object in a cache
427  * @cache: The cache from which the object came
428  *
429  * Note the destruction and deallocation of an object record in a cache.
430  */
fscache_object_destroyed(struct fscache_cache * cache)431 static inline void fscache_object_destroyed(struct fscache_cache *cache)
432 {
433 	if (atomic_dec_and_test(&cache->object_count))
434 		wake_up_all(&fscache_cache_cleared_wq);
435 }
436 
437 /**
438  * fscache_object_lookup_error - Note an object encountered an error
439  * @object: The object on which the error was encountered
440  *
441  * Note that an object encountered a fatal error (usually an I/O error) and
442  * that it should be withdrawn as soon as possible.
443  */
fscache_object_lookup_error(struct fscache_object * object)444 static inline void fscache_object_lookup_error(struct fscache_object *object)
445 {
446 	set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
447 }
448 
449 /**
450  * fscache_set_store_limit - Set the maximum size to be stored in an object
451  * @object: The object to set the maximum on
452  * @i_size: The limit to set in bytes
453  *
454  * Set the maximum size an object is permitted to reach, implying the highest
455  * byte that may be written.  Intended to be called by the attr_changed() op.
456  *
457  * See Documentation/filesystems/caching/backend-api.rst for a complete
458  * description.
459  */
460 static inline
fscache_set_store_limit(struct fscache_object * object,loff_t i_size)461 void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
462 {
463 	object->store_limit_l = i_size;
464 	object->store_limit = i_size >> PAGE_SHIFT;
465 	if (i_size & ~PAGE_MASK)
466 		object->store_limit++;
467 }
468 
469 /**
470  * fscache_end_io - End a retrieval operation on a page
471  * @op: The FS-Cache operation covering the retrieval
472  * @page: The page that was to be fetched
473  * @error: The error code (0 if successful)
474  *
475  * Note the end of an operation to retrieve a page, as covered by a particular
476  * operation record.
477  */
fscache_end_io(struct fscache_retrieval * op,struct page * page,int error)478 static inline void fscache_end_io(struct fscache_retrieval *op,
479 				  struct page *page, int error)
480 {
481 	op->end_io_func(page, op->context, error);
482 }
483 
__fscache_use_cookie(struct fscache_cookie * cookie)484 static inline void __fscache_use_cookie(struct fscache_cookie *cookie)
485 {
486 	atomic_inc(&cookie->n_active);
487 }
488 
489 /**
490  * fscache_use_cookie - Request usage of cookie attached to an object
491  * @object: Object description
492  *
493  * Request usage of the cookie attached to an object.  NULL is returned if the
494  * relinquishment had reduced the cookie usage count to 0.
495  */
fscache_use_cookie(struct fscache_object * object)496 static inline bool fscache_use_cookie(struct fscache_object *object)
497 {
498 	struct fscache_cookie *cookie = object->cookie;
499 	return atomic_inc_not_zero(&cookie->n_active) != 0;
500 }
501 
__fscache_unuse_cookie(struct fscache_cookie * cookie)502 static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie)
503 {
504 	return atomic_dec_and_test(&cookie->n_active);
505 }
506 
__fscache_wake_unused_cookie(struct fscache_cookie * cookie)507 static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie)
508 {
509 	wake_up_var(&cookie->n_active);
510 }
511 
512 /**
513  * fscache_unuse_cookie - Cease usage of cookie attached to an object
514  * @object: Object description
515  *
516  * Cease usage of the cookie attached to an object.  When the users count
517  * reaches zero then the cookie relinquishment will be permitted to proceed.
518  */
fscache_unuse_cookie(struct fscache_object * object)519 static inline void fscache_unuse_cookie(struct fscache_object *object)
520 {
521 	struct fscache_cookie *cookie = object->cookie;
522 	if (__fscache_unuse_cookie(cookie))
523 		__fscache_wake_unused_cookie(cookie);
524 }
525 
526 /*
527  * out-of-line cache backend functions
528  */
529 extern __printf(3, 4)
530 void fscache_init_cache(struct fscache_cache *cache,
531 			const struct fscache_cache_ops *ops,
532 			const char *idfmt, ...);
533 
534 extern int fscache_add_cache(struct fscache_cache *cache,
535 			     struct fscache_object *fsdef,
536 			     const char *tagname);
537 extern void fscache_withdraw_cache(struct fscache_cache *cache);
538 
539 extern void fscache_io_error(struct fscache_cache *cache);
540 
541 extern void fscache_mark_page_cached(struct fscache_retrieval *op,
542 				     struct page *page);
543 
544 extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
545 				      struct pagevec *pagevec);
546 
547 extern bool fscache_object_sleep_till_congested(signed long *timeoutp);
548 
549 extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
550 					       const void *data,
551 					       uint16_t datalen,
552 					       loff_t object_size);
553 
554 extern void fscache_object_retrying_stale(struct fscache_object *object);
555 
556 enum fscache_why_object_killed {
557 	FSCACHE_OBJECT_IS_STALE,
558 	FSCACHE_OBJECT_NO_SPACE,
559 	FSCACHE_OBJECT_WAS_RETIRED,
560 	FSCACHE_OBJECT_WAS_CULLED,
561 };
562 extern void fscache_object_mark_killed(struct fscache_object *object,
563 				       enum fscache_why_object_killed why);
564 
565 #endif /* _LINUX_FSCACHE_CACHE_H */
566