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