xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/caching/backend-api.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun==========================
4*4882a593SmuzhiyunFS-Cache Cache backend API
5*4882a593Smuzhiyun==========================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThe FS-Cache system provides an API by which actual caches can be supplied to
8*4882a593SmuzhiyunFS-Cache for it to then serve out to network filesystems and other interested
9*4882a593Smuzhiyunparties.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThis API is declared in <linux/fscache-cache.h>.
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunInitialising and Registering a Cache
15*4882a593Smuzhiyun====================================
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunTo start off, a cache definition must be initialised and registered for each
18*4882a593Smuzhiyuncache the backend wants to make available.  For instance, CacheFS does this in
19*4882a593Smuzhiyunthe fill_super() operation on mounting.
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunThe cache definition (struct fscache_cache) should be initialised by calling::
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun	void fscache_init_cache(struct fscache_cache *cache,
24*4882a593Smuzhiyun				struct fscache_cache_ops *ops,
25*4882a593Smuzhiyun				const char *idfmt,
26*4882a593Smuzhiyun				...);
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunWhere:
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun   * "cache" is a pointer to the cache definition;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun   * "ops" is a pointer to the table of operations that the backend supports on
33*4882a593Smuzhiyun     this cache; and
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun   * "idfmt" is a format and printf-style arguments for constructing a label
36*4882a593Smuzhiyun     for the cache.
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunThe cache should then be registered with FS-Cache by passing a pointer to the
40*4882a593Smuzhiyunpreviously initialised cache definition to::
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun	int fscache_add_cache(struct fscache_cache *cache,
43*4882a593Smuzhiyun			      struct fscache_object *fsdef,
44*4882a593Smuzhiyun			      const char *tagname);
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunTwo extra arguments should also be supplied:
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun   * "fsdef" which should point to the object representation for the FS-Cache
49*4882a593Smuzhiyun     master index in this cache.  Netfs primary index entries will be created
50*4882a593Smuzhiyun     here.  FS-Cache keeps the caller's reference to the index object if
51*4882a593Smuzhiyun     successful and will release it upon withdrawal of the cache.
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun   * "tagname" which, if given, should be a text string naming this cache.  If
54*4882a593Smuzhiyun     this is NULL, the identifier will be used instead.  For CacheFS, the
55*4882a593Smuzhiyun     identifier is set to name the underlying block device and the tag can be
56*4882a593Smuzhiyun     supplied by mount.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunThis function may return -ENOMEM if it ran out of memory or -EEXIST if the tag
59*4882a593Smuzhiyunis already in use.  0 will be returned on success.
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunUnregistering a Cache
63*4882a593Smuzhiyun=====================
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunA cache can be withdrawn from the system by calling this function with a
66*4882a593Smuzhiyunpointer to the cache definition::
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun	void fscache_withdraw_cache(struct fscache_cache *cache);
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunIn CacheFS's case, this is called by put_super().
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunSecurity
74*4882a593Smuzhiyun========
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunThe cache methods are executed one of two contexts:
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun (1) that of the userspace process that issued the netfs operation that caused
79*4882a593Smuzhiyun     the cache method to be invoked, or
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun (2) that of one of the processes in the FS-Cache thread pool.
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunIn either case, this may not be an appropriate context in which to access the
84*4882a593Smuzhiyuncache.
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunThe calling process's fsuid, fsgid and SELinux security identities may need to
87*4882a593Smuzhiyunbe masqueraded for the duration of the cache driver's access to the cache.
88*4882a593SmuzhiyunThis is left to the cache to handle; FS-Cache makes no effort in this regard.
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun
91*4882a593SmuzhiyunControl and Statistics Presentation
92*4882a593Smuzhiyun===================================
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunThe cache may present data to the outside world through FS-Cache's interfaces
95*4882a593Smuzhiyunin sysfs and procfs - the former for control and the latter for statistics.
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunA sysfs directory called /sys/fs/fscache/<cachetag>/ is created if CONFIG_SYSFS
98*4882a593Smuzhiyunis enabled.  This is accessible through the kobject struct fscache_cache::kobj
99*4882a593Smuzhiyunand is for use by the cache as it sees fit.
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunRelevant Data Structures
103*4882a593Smuzhiyun========================
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun   * Index/Data file FS-Cache representation cookie::
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun	struct fscache_cookie {
108*4882a593Smuzhiyun		struct fscache_object_def	*def;
109*4882a593Smuzhiyun		struct fscache_netfs		*netfs;
110*4882a593Smuzhiyun		void				*netfs_data;
111*4882a593Smuzhiyun		...
112*4882a593Smuzhiyun	};
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun     The fields that might be of use to the backend describe the object
115*4882a593Smuzhiyun     definition, the netfs definition and the netfs's data for this cookie.
116*4882a593Smuzhiyun     The object definition contain functions supplied by the netfs for loading
117*4882a593Smuzhiyun     and matching index entries; these are required to provide some of the
118*4882a593Smuzhiyun     cache operations.
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun   * In-cache object representation::
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun	struct fscache_object {
124*4882a593Smuzhiyun		int				debug_id;
125*4882a593Smuzhiyun		enum {
126*4882a593Smuzhiyun			FSCACHE_OBJECT_RECYCLING,
127*4882a593Smuzhiyun			...
128*4882a593Smuzhiyun		}				state;
129*4882a593Smuzhiyun		spinlock_t			lock
130*4882a593Smuzhiyun		struct fscache_cache		*cache;
131*4882a593Smuzhiyun		struct fscache_cookie		*cookie;
132*4882a593Smuzhiyun		...
133*4882a593Smuzhiyun	};
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun     Structures of this type should be allocated by the cache backend and
136*4882a593Smuzhiyun     passed to FS-Cache when requested by the appropriate cache operation.  In
137*4882a593Smuzhiyun     the case of CacheFS, they're embedded in CacheFS's internal object
138*4882a593Smuzhiyun     structures.
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun     The debug_id is a simple integer that can be used in debugging messages
141*4882a593Smuzhiyun     that refer to a particular object.  In such a case it should be printed
142*4882a593Smuzhiyun     using "OBJ%x" to be consistent with FS-Cache.
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun     Each object contains a pointer to the cookie that represents the object it
145*4882a593Smuzhiyun     is backing.  An object should retired when put_object() is called if it is
146*4882a593Smuzhiyun     in state FSCACHE_OBJECT_RECYCLING.  The fscache_object struct should be
147*4882a593Smuzhiyun     initialised by calling fscache_object_init(object).
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun   * FS-Cache operation record::
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun	struct fscache_operation {
153*4882a593Smuzhiyun		atomic_t		usage;
154*4882a593Smuzhiyun		struct fscache_object	*object;
155*4882a593Smuzhiyun		unsigned long		flags;
156*4882a593Smuzhiyun	#define FSCACHE_OP_EXCLUSIVE
157*4882a593Smuzhiyun		void (*processor)(struct fscache_operation *op);
158*4882a593Smuzhiyun		void (*release)(struct fscache_operation *op);
159*4882a593Smuzhiyun		...
160*4882a593Smuzhiyun	};
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun     FS-Cache has a pool of threads that it uses to give CPU time to the
163*4882a593Smuzhiyun     various asynchronous operations that need to be done as part of driving
164*4882a593Smuzhiyun     the cache.  These are represented by the above structure.  The processor
165*4882a593Smuzhiyun     method is called to give the op CPU time, and the release method to get
166*4882a593Smuzhiyun     rid of it when its usage count reaches 0.
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun     An operation can be made exclusive upon an object by setting the
169*4882a593Smuzhiyun     appropriate flag before enqueuing it with fscache_enqueue_operation().  If
170*4882a593Smuzhiyun     an operation needs more processing time, it should be enqueued again.
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun   * FS-Cache retrieval operation record::
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun	struct fscache_retrieval {
176*4882a593Smuzhiyun		struct fscache_operation op;
177*4882a593Smuzhiyun		struct address_space	*mapping;
178*4882a593Smuzhiyun		struct list_head	*to_do;
179*4882a593Smuzhiyun		...
180*4882a593Smuzhiyun	};
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun     A structure of this type is allocated by FS-Cache to record retrieval and
183*4882a593Smuzhiyun     allocation requests made by the netfs.  This struct is then passed to the
184*4882a593Smuzhiyun     backend to do the operation.  The backend may get extra refs to it by
185*4882a593Smuzhiyun     calling fscache_get_retrieval() and refs may be discarded by calling
186*4882a593Smuzhiyun     fscache_put_retrieval().
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun     A retrieval operation can be used by the backend to do retrieval work.  To
189*4882a593Smuzhiyun     do this, the retrieval->op.processor method pointer should be set
190*4882a593Smuzhiyun     appropriately by the backend and fscache_enqueue_retrieval() called to
191*4882a593Smuzhiyun     submit it to the thread pool.  CacheFiles, for example, uses this to queue
192*4882a593Smuzhiyun     page examination when it detects PG_lock being cleared.
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun     The to_do field is an empty list available for the cache backend to use as
195*4882a593Smuzhiyun     it sees fit.
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun   * FS-Cache storage operation record::
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun	struct fscache_storage {
201*4882a593Smuzhiyun		struct fscache_operation op;
202*4882a593Smuzhiyun		pgoff_t			store_limit;
203*4882a593Smuzhiyun		...
204*4882a593Smuzhiyun	};
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun     A structure of this type is allocated by FS-Cache to record outstanding
207*4882a593Smuzhiyun     writes to be made.  FS-Cache itself enqueues this operation and invokes
208*4882a593Smuzhiyun     the write_page() method on the object at appropriate times to effect
209*4882a593Smuzhiyun     storage.
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun
212*4882a593SmuzhiyunCache Operations
213*4882a593Smuzhiyun================
214*4882a593Smuzhiyun
215*4882a593SmuzhiyunThe cache backend provides FS-Cache with a table of operations that can be
216*4882a593Smuzhiyunperformed on the denizens of the cache.  These are held in a structure of type:
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun	::
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun	    struct fscache_cache_ops
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun   * Name of cache provider [mandatory]::
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun	const char *name
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun     This isn't strictly an operation, but should be pointed at a string naming
227*4882a593Smuzhiyun     the backend.
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun   * Allocate a new object [mandatory]::
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun	struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
233*4882a593Smuzhiyun					       struct fscache_cookie *cookie)
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun     This method is used to allocate a cache object representation to back a
236*4882a593Smuzhiyun     cookie in a particular cache.  fscache_object_init() should be called on
237*4882a593Smuzhiyun     the object to initialise it prior to returning.
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun     This function may also be used to parse the index key to be used for
240*4882a593Smuzhiyun     multiple lookup calls to turn it into a more convenient form.  FS-Cache
241*4882a593Smuzhiyun     will call the lookup_complete() method to allow the cache to release the
242*4882a593Smuzhiyun     form once lookup is complete or aborted.
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun   * Look up and create object [mandatory]::
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun	void (*lookup_object)(struct fscache_object *object)
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun     This method is used to look up an object, given that the object is already
250*4882a593Smuzhiyun     allocated and attached to the cookie.  This should instantiate that object
251*4882a593Smuzhiyun     in the cache if it can.
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun     The method should call fscache_object_lookup_negative() as soon as
254*4882a593Smuzhiyun     possible if it determines the object doesn't exist in the cache.  If the
255*4882a593Smuzhiyun     object is found to exist and the netfs indicates that it is valid then
256*4882a593Smuzhiyun     fscache_obtained_object() should be called once the object is in a
257*4882a593Smuzhiyun     position to have data stored in it.  Similarly, fscache_obtained_object()
258*4882a593Smuzhiyun     should also be called once a non-present object has been created.
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun     If a lookup error occurs, fscache_object_lookup_error() should be called
261*4882a593Smuzhiyun     to abort the lookup of that object.
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun   * Release lookup data [mandatory]::
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun	void (*lookup_complete)(struct fscache_object *object)
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun     This method is called to ask the cache to release any resources it was
269*4882a593Smuzhiyun     using to perform a lookup.
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun   * Increment object refcount [mandatory]::
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun	struct fscache_object *(*grab_object)(struct fscache_object *object)
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun     This method is called to increment the reference count on an object.  It
277*4882a593Smuzhiyun     may fail (for instance if the cache is being withdrawn) by returning NULL.
278*4882a593Smuzhiyun     It should return the object pointer if successful.
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun   * Lock/Unlock object [mandatory]::
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun	void (*lock_object)(struct fscache_object *object)
284*4882a593Smuzhiyun	void (*unlock_object)(struct fscache_object *object)
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun     These methods are used to exclusively lock an object.  It must be possible
287*4882a593Smuzhiyun     to schedule with the lock held, so a spinlock isn't sufficient.
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun   * Pin/Unpin object [optional]::
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun	int (*pin_object)(struct fscache_object *object)
293*4882a593Smuzhiyun	void (*unpin_object)(struct fscache_object *object)
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun     These methods are used to pin an object into the cache.  Once pinned an
296*4882a593Smuzhiyun     object cannot be reclaimed to make space.  Return -ENOSPC if there's not
297*4882a593Smuzhiyun     enough space in the cache to permit this.
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun   * Check coherency state of an object [mandatory]::
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun	int (*check_consistency)(struct fscache_object *object)
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun     This method is called to have the cache check the saved auxiliary data of
305*4882a593Smuzhiyun     the object against the netfs's idea of the state.  0 should be returned
306*4882a593Smuzhiyun     if they're consistent and -ESTALE otherwise.  -ENOMEM and -ERESTARTSYS
307*4882a593Smuzhiyun     may also be returned.
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun   * Update object [mandatory]::
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun	int (*update_object)(struct fscache_object *object)
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun     This is called to update the index entry for the specified object.  The
314*4882a593Smuzhiyun     new information should be in object->cookie->netfs_data.  This can be
315*4882a593Smuzhiyun     obtained by calling object->cookie->def->get_aux()/get_attr().
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun   * Invalidate data object [mandatory]::
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun	int (*invalidate_object)(struct fscache_operation *op)
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun     This is called to invalidate a data object (as pointed to by op->object).
323*4882a593Smuzhiyun     All the data stored for this object should be discarded and an
324*4882a593Smuzhiyun     attr_changed operation should be performed.  The caller will follow up
325*4882a593Smuzhiyun     with an object update operation.
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun     fscache_op_complete() must be called on op before returning.
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun   * Discard object [mandatory]::
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun	void (*drop_object)(struct fscache_object *object)
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun     This method is called to indicate that an object has been unbound from its
335*4882a593Smuzhiyun     cookie, and that the cache should release the object's resources and
336*4882a593Smuzhiyun     retire it if it's in state FSCACHE_OBJECT_RECYCLING.
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun     This method should not attempt to release any references held by the
339*4882a593Smuzhiyun     caller.  The caller will invoke the put_object() method as appropriate.
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun   * Release object reference [mandatory]::
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun	void (*put_object)(struct fscache_object *object)
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun     This method is used to discard a reference to an object.  The object may
347*4882a593Smuzhiyun     be freed when all the references to it are released.
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun   * Synchronise a cache [mandatory]::
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun	void (*sync)(struct fscache_cache *cache)
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun     This is called to ask the backend to synchronise a cache with its backing
355*4882a593Smuzhiyun     device.
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun   * Dissociate a cache [mandatory]::
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun	void (*dissociate_pages)(struct fscache_cache *cache)
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun     This is called to ask a cache to perform any page dissociations as part of
363*4882a593Smuzhiyun     cache withdrawal.
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun   * Notification that the attributes on a netfs file changed [mandatory]::
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun	int (*attr_changed)(struct fscache_object *object);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun     This is called to indicate to the cache that certain attributes on a netfs
371*4882a593Smuzhiyun     file have changed (for example the maximum size a file may reach).  The
372*4882a593Smuzhiyun     cache can read these from the netfs by calling the cookie's get_attr()
373*4882a593Smuzhiyun     method.
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun     The cache may use the file size information to reserve space on the cache.
376*4882a593Smuzhiyun     It should also call fscache_set_store_limit() to indicate to FS-Cache the
377*4882a593Smuzhiyun     highest byte it's willing to store for an object.
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun     This method may return -ve if an error occurred or the cache object cannot
380*4882a593Smuzhiyun     be expanded.  In such a case, the object will be withdrawn from service.
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun     This operation is run asynchronously from FS-Cache's thread pool, and
383*4882a593Smuzhiyun     storage and retrieval operations from the netfs are excluded during the
384*4882a593Smuzhiyun     execution of this operation.
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun   * Reserve cache space for an object's data [optional]::
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun	int (*reserve_space)(struct fscache_object *object, loff_t size);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun     This is called to request that cache space be reserved to hold the data
392*4882a593Smuzhiyun     for an object and the metadata used to track it.  Zero size should be
393*4882a593Smuzhiyun     taken as request to cancel a reservation.
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun     This should return 0 if successful, -ENOSPC if there isn't enough space
396*4882a593Smuzhiyun     available, or -ENOMEM or -EIO on other errors.
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun     The reservation may exceed the current size of the object, thus permitting
399*4882a593Smuzhiyun     future expansion.  If the amount of space consumed by an object would
400*4882a593Smuzhiyun     exceed the reservation, it's permitted to refuse requests to allocate
401*4882a593Smuzhiyun     pages, but not required.  An object may be pruned down to its reservation
402*4882a593Smuzhiyun     size if larger than that already.
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun   * Request page be read from cache [mandatory]::
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun	int (*read_or_alloc_page)(struct fscache_retrieval *op,
408*4882a593Smuzhiyun				  struct page *page,
409*4882a593Smuzhiyun				  gfp_t gfp)
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun     This is called to attempt to read a netfs page from the cache, or to
412*4882a593Smuzhiyun     reserve a backing block if not.  FS-Cache will have done as much checking
413*4882a593Smuzhiyun     as it can before calling, but most of the work belongs to the backend.
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun     If there's no page in the cache, then -ENODATA should be returned if the
416*4882a593Smuzhiyun     backend managed to reserve a backing block; -ENOBUFS or -ENOMEM if it
417*4882a593Smuzhiyun     didn't.
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun     If there is suitable data in the cache, then a read operation should be
420*4882a593Smuzhiyun     queued and 0 returned.  When the read finishes, fscache_end_io() should be
421*4882a593Smuzhiyun     called.
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun     The fscache_mark_pages_cached() should be called for the page if any cache
424*4882a593Smuzhiyun     metadata is retained.  This will indicate to the netfs that the page needs
425*4882a593Smuzhiyun     explicit uncaching.  This operation takes a pagevec, thus allowing several
426*4882a593Smuzhiyun     pages to be marked at once.
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun     The retrieval record pointed to by op should be retained for each page
429*4882a593Smuzhiyun     queued and released when I/O on the page has been formally ended.
430*4882a593Smuzhiyun     fscache_get/put_retrieval() are available for this purpose.
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun     The retrieval record may be used to get CPU time via the FS-Cache thread
433*4882a593Smuzhiyun     pool.  If this is desired, the op->op.processor should be set to point to
434*4882a593Smuzhiyun     the appropriate processing routine, and fscache_enqueue_retrieval() should
435*4882a593Smuzhiyun     be called at an appropriate point to request CPU time.  For instance, the
436*4882a593Smuzhiyun     retrieval routine could be enqueued upon the completion of a disk read.
437*4882a593Smuzhiyun     The to_do field in the retrieval record is provided to aid in this.
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun     If an I/O error occurs, fscache_io_error() should be called and -ENOBUFS
440*4882a593Smuzhiyun     returned if possible or fscache_end_io() called with a suitable error
441*4882a593Smuzhiyun     code.
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun     fscache_put_retrieval() should be called after a page or pages are dealt
444*4882a593Smuzhiyun     with.  This will complete the operation when all pages are dealt with.
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun   * Request pages be read from cache [mandatory]::
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun	int (*read_or_alloc_pages)(struct fscache_retrieval *op,
450*4882a593Smuzhiyun				   struct list_head *pages,
451*4882a593Smuzhiyun				   unsigned *nr_pages,
452*4882a593Smuzhiyun				   gfp_t gfp)
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun     This is like the read_or_alloc_page() method, except it is handed a list
455*4882a593Smuzhiyun     of pages instead of one page.  Any pages on which a read operation is
456*4882a593Smuzhiyun     started must be added to the page cache for the specified mapping and also
457*4882a593Smuzhiyun     to the LRU.  Such pages must also be removed from the pages list and
458*4882a593Smuzhiyun     ``*nr_pages`` decremented per page.
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun     If there was an error such as -ENOMEM, then that should be returned; else
461*4882a593Smuzhiyun     if one or more pages couldn't be read or allocated, then -ENOBUFS should
462*4882a593Smuzhiyun     be returned; else if one or more pages couldn't be read, then -ENODATA
463*4882a593Smuzhiyun     should be returned.  If all the pages are dispatched then 0 should be
464*4882a593Smuzhiyun     returned.
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun   * Request page be allocated in the cache [mandatory]::
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun	int (*allocate_page)(struct fscache_retrieval *op,
470*4882a593Smuzhiyun			     struct page *page,
471*4882a593Smuzhiyun			     gfp_t gfp)
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun     This is like the read_or_alloc_page() method, except that it shouldn't
474*4882a593Smuzhiyun     read from the cache, even if there's data there that could be retrieved.
475*4882a593Smuzhiyun     It should, however, set up any internal metadata required such that
476*4882a593Smuzhiyun     the write_page() method can write to the cache.
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun     If there's no backing block available, then -ENOBUFS should be returned
479*4882a593Smuzhiyun     (or -ENOMEM if there were other problems).  If a block is successfully
480*4882a593Smuzhiyun     allocated, then the netfs page should be marked and 0 returned.
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun   * Request pages be allocated in the cache [mandatory]::
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun	int (*allocate_pages)(struct fscache_retrieval *op,
486*4882a593Smuzhiyun			      struct list_head *pages,
487*4882a593Smuzhiyun			      unsigned *nr_pages,
488*4882a593Smuzhiyun			      gfp_t gfp)
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun     This is an multiple page version of the allocate_page() method.  pages and
491*4882a593Smuzhiyun     nr_pages should be treated as for the read_or_alloc_pages() method.
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun   * Request page be written to cache [mandatory]::
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun	int (*write_page)(struct fscache_storage *op,
497*4882a593Smuzhiyun			  struct page *page);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun     This is called to write from a page on which there was a previously
500*4882a593Smuzhiyun     successful read_or_alloc_page() call or similar.  FS-Cache filters out
501*4882a593Smuzhiyun     pages that don't have mappings.
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun     This method is called asynchronously from the FS-Cache thread pool.  It is
504*4882a593Smuzhiyun     not required to actually store anything, provided -ENODATA is then
505*4882a593Smuzhiyun     returned to the next read of this page.
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun     If an error occurred, then a negative error code should be returned,
508*4882a593Smuzhiyun     otherwise zero should be returned.  FS-Cache will take appropriate action
509*4882a593Smuzhiyun     in response to an error, such as withdrawing this object.
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun     If this method returns success then FS-Cache will inform the netfs
512*4882a593Smuzhiyun     appropriately.
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun   * Discard retained per-page metadata [mandatory]::
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun	void (*uncache_page)(struct fscache_object *object, struct page *page)
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun     This is called when a netfs page is being evicted from the pagecache.  The
520*4882a593Smuzhiyun     cache backend should tear down any internal representation or tracking it
521*4882a593Smuzhiyun     maintains for this page.
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun
524*4882a593SmuzhiyunFS-Cache Utilities
525*4882a593Smuzhiyun==================
526*4882a593Smuzhiyun
527*4882a593SmuzhiyunFS-Cache provides some utilities that a cache backend may make use of:
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun   * Note occurrence of an I/O error in a cache::
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun	void fscache_io_error(struct fscache_cache *cache)
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun     This tells FS-Cache that an I/O error occurred in the cache.  After this
534*4882a593Smuzhiyun     has been called, only resource dissociation operations (object and page
535*4882a593Smuzhiyun     release) will be passed from the netfs to the cache backend for the
536*4882a593Smuzhiyun     specified cache.
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun     This does not actually withdraw the cache.  That must be done separately.
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun   * Invoke the retrieval I/O completion function::
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun	void fscache_end_io(struct fscache_retrieval *op, struct page *page,
544*4882a593Smuzhiyun			    int error);
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun     This is called to note the end of an attempt to retrieve a page.  The
547*4882a593Smuzhiyun     error value should be 0 if successful and an error otherwise.
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun   * Record that one or more pages being retrieved or allocated have been dealt
551*4882a593Smuzhiyun     with::
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun	void fscache_retrieval_complete(struct fscache_retrieval *op,
554*4882a593Smuzhiyun					int n_pages);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun     This is called to record the fact that one or more pages have been dealt
557*4882a593Smuzhiyun     with and are no longer the concern of this operation.  When the number of
558*4882a593Smuzhiyun     pages remaining in the operation reaches 0, the operation will be
559*4882a593Smuzhiyun     completed.
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun   * Record operation completion::
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun	void fscache_op_complete(struct fscache_operation *op);
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun     This is called to record the completion of an operation.  This deducts
567*4882a593Smuzhiyun     this operation from the parent object's run state, potentially permitting
568*4882a593Smuzhiyun     one or more pending operations to start running.
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun   * Set highest store limit::
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun	void fscache_set_store_limit(struct fscache_object *object,
574*4882a593Smuzhiyun				     loff_t i_size);
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun     This sets the limit FS-Cache imposes on the highest byte it's willing to
577*4882a593Smuzhiyun     try and store for a netfs.  Any page over this limit is automatically
578*4882a593Smuzhiyun     rejected by fscache_read_alloc_page() and co with -ENOBUFS.
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun   * Mark pages as being cached::
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun	void fscache_mark_pages_cached(struct fscache_retrieval *op,
584*4882a593Smuzhiyun				       struct pagevec *pagevec);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun     This marks a set of pages as being cached.  After this has been called,
587*4882a593Smuzhiyun     the netfs must call fscache_uncache_page() to unmark the pages.
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun   * Perform coherency check on an object::
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun	enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
593*4882a593Smuzhiyun						const void *data,
594*4882a593Smuzhiyun						uint16_t datalen);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun     This asks the netfs to perform a coherency check on an object that has
597*4882a593Smuzhiyun     just been looked up.  The cookie attached to the object will determine the
598*4882a593Smuzhiyun     netfs to use.  data and datalen should specify where the auxiliary data
599*4882a593Smuzhiyun     retrieved from the cache can be found.
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun     One of three values will be returned:
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun	FSCACHE_CHECKAUX_OKAY
604*4882a593Smuzhiyun	    The coherency data indicates the object is valid as is.
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun	FSCACHE_CHECKAUX_NEEDS_UPDATE
607*4882a593Smuzhiyun	    The coherency data needs updating, but otherwise the object is
608*4882a593Smuzhiyun	    valid.
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun	FSCACHE_CHECKAUX_OBSOLETE
611*4882a593Smuzhiyun	    The coherency data indicates that the object is obsolete and should
612*4882a593Smuzhiyun	    be discarded.
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun   * Initialise a freshly allocated object::
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun	void fscache_object_init(struct fscache_object *object);
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun     This initialises all the fields in an object representation.
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun   * Indicate the destruction of an object::
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun	void fscache_object_destroyed(struct fscache_cache *cache);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun     This must be called to inform FS-Cache that an object that belonged to a
627*4882a593Smuzhiyun     cache has been destroyed and deallocated.  This will allow continuation
628*4882a593Smuzhiyun     of the cache withdrawal process when it is stopped pending destruction of
629*4882a593Smuzhiyun     all the objects.
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun   * Indicate negative lookup on an object::
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun	void fscache_object_lookup_negative(struct fscache_object *object);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun     This is called to indicate to FS-Cache that a lookup process for an object
637*4882a593Smuzhiyun     found a negative result.
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun     This changes the state of an object to permit reads pending on lookup
640*4882a593Smuzhiyun     completion to go off and start fetching data from the netfs server as it's
641*4882a593Smuzhiyun     known at this point that there can't be any data in the cache.
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun     This may be called multiple times on an object.  Only the first call is
644*4882a593Smuzhiyun     significant - all subsequent calls are ignored.
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun   * Indicate an object has been obtained::
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun	void fscache_obtained_object(struct fscache_object *object);
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun     This is called to indicate to FS-Cache that a lookup process for an object
652*4882a593Smuzhiyun     produced a positive result, or that an object was created.  This should
653*4882a593Smuzhiyun     only be called once for any particular object.
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun     This changes the state of an object to indicate:
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun	(1) if no call to fscache_object_lookup_negative() has been made on
658*4882a593Smuzhiyun	    this object, that there may be data available, and that reads can
659*4882a593Smuzhiyun	    now go and look for it; and
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun        (2) that writes may now proceed against this object.
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun   * Indicate that object lookup failed::
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun	void fscache_object_lookup_error(struct fscache_object *object);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun     This marks an object as having encountered a fatal error (usually EIO)
669*4882a593Smuzhiyun     and causes it to move into a state whereby it will be withdrawn as soon
670*4882a593Smuzhiyun     as possible.
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun   * Indicate that a stale object was found and discarded::
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun	void fscache_object_retrying_stale(struct fscache_object *object);
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun     This is called to indicate that the lookup procedure found an object in
678*4882a593Smuzhiyun     the cache that the netfs decided was stale.  The object has been
679*4882a593Smuzhiyun     discarded from the cache and the lookup will be performed again.
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun   * Indicate that the caching backend killed an object::
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun	void fscache_object_mark_killed(struct fscache_object *object,
685*4882a593Smuzhiyun					enum fscache_why_object_killed why);
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun     This is called to indicate that the cache backend preemptively killed an
688*4882a593Smuzhiyun     object.  The why parameter should be set to indicate the reason:
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun	FSCACHE_OBJECT_IS_STALE
691*4882a593Smuzhiyun	    - the object was stale and needs discarding.
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun	FSCACHE_OBJECT_NO_SPACE
694*4882a593Smuzhiyun	    - there was insufficient cache space
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun	FSCACHE_OBJECT_WAS_RETIRED
697*4882a593Smuzhiyun	    - the object was retired when relinquished.
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun	FSCACHE_OBJECT_WAS_CULLED
700*4882a593Smuzhiyun	    - the object was culled to make space.
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun   * Get and release references on a retrieval record::
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun	void fscache_get_retrieval(struct fscache_retrieval *op);
706*4882a593Smuzhiyun	void fscache_put_retrieval(struct fscache_retrieval *op);
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun     These two functions are used to retain a retrieval record while doing
709*4882a593Smuzhiyun     asynchronous data retrieval and block allocation.
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun   * Enqueue a retrieval record for processing::
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun	void fscache_enqueue_retrieval(struct fscache_retrieval *op);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun     This enqueues a retrieval record for processing by the FS-Cache thread
717*4882a593Smuzhiyun     pool.  One of the threads in the pool will invoke the retrieval record's
718*4882a593Smuzhiyun     op->op.processor callback function.  This function may be called from
719*4882a593Smuzhiyun     within the callback function.
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun   * List of object state names::
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun	const char *fscache_object_states[];
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun     For debugging purposes, this may be used to turn the state that an object
727*4882a593Smuzhiyun     is in into a text string for display purposes.
728