1 ==========================
2 FS-CACHE CACHE BACKEND API
3 ==========================
5 The FS-Cache system provides an API by which actual caches can be supplied to
6 FS-Cache for it to then serve out to network filesystems and other interested
9 This API is declared in <linux/fscache-cache.h>.
12 ====================================
13 INITIALISING AND REGISTERING A CACHE
14 ====================================
16 To start off, a cache definition must be initialised and registered for each
17 cache the backend wants to make available. For instance, CacheFS does this in
18 the fill_super() operation on mounting.
20 The cache definition (struct fscache_cache) should be initialised by calling:
22 void fscache_init_cache(struct fscache_cache *cache,
23 struct fscache_cache_ops *ops,
29 (*) "cache" is a pointer to the cache definition;
31 (*) "ops" is a pointer to the table of operations that the backend supports on
34 (*) and a format and printf-style arguments for constructing a label for the
38 The cache should then be registered with FS-Cache by passing a pointer to the
39 previously initialised cache definition to:
41 int fscache_add_cache(struct fscache_cache *cache,
42 struct fscache_object *fsdef,
45 Two extra arguments should also be supplied:
47 (*) "fsdef" which should point to the object representation for the FS-Cache
48 master index in this cache. Netfs primary index entries will be created
51 (*) "tagname" which, if given, should be a text string naming this cache. If
52 this is NULL, the identifier will be used instead. For CacheFS, the
53 identifier is set to name the underlying block device and the tag can be
56 This function may return -ENOMEM if it ran out of memory or -EEXIST if the tag
57 is already in use. 0 will be returned on success.
64 A cache can be withdrawn from the system by calling this function with a
65 pointer to the cache definition:
67 void fscache_withdraw_cache(struct fscache_cache *cache)
69 In CacheFS's case, this is called by put_super().
76 FS-Cache provides some utilities that a cache backend may make use of:
78 (*) Find the parent of an object:
80 struct fscache_object *
81 fscache_find_parent_object(struct fscache_object *object)
83 This allows a backend to find the logical parent of an index or data file
84 in the cache hierarchy.
86 (*) Note occurrence of an I/O error in a cache:
88 void fscache_io_error(struct fscache_cache *cache)
90 This tells FS-Cache that an I/O error occurred in the cache. After this
91 has been called, only resource dissociation operations (object and page
92 release) will be passed from the netfs to the cache backend for the
95 This does not actually withdraw the cache. That must be done separately.
97 (*) Get an extra reference to a read or write context:
99 void *fscache_get_context(struct fscache_cookie *cookie, void *context)
101 and release a reference:
103 void *fscache_put_context(struct fscache_cookie *cookie, void *context)
105 These should be used to maintain the presence of the read or write context
106 passed to the cache read/write functions. This context must then be
107 passed to the I/O completion function.
110 ========================
111 RELEVANT DATA STRUCTURES
112 ========================
114 (*) Index/Data file FS-Cache representation cookie:
116 struct fscache_cookie {
117 struct fscache_object_def *def;
118 struct fscache_netfs *netfs;
123 The fields that might be of use to the backend describe the object
124 definition, the netfs definition and the netfs's data for this cookie.
125 The object definition contain functions supplied by the netfs for loading
126 and matching index entries; these are required to provide some of the
129 (*) In-cache object representation:
131 struct fscache_object {
132 struct fscache_cache *cache;
133 struct fscache_cookie *cookie;
135 #define FSCACHE_OBJECT_RECYCLING 1
139 Structures of this type should be allocated by the cache backend and
140 passed to FS-Cache when requested by the appropriate cache operation. In
141 the case of CacheFS, they're embedded in CacheFS's internal object
144 Each object contains a pointer to the cookie that represents the object it
145 is backing. It also contains a flag that indicates whether the object is
146 being retired when put_object() is called. This should be initialised by
147 calling fscache_object_init(object).
154 The cache backend provides FS-Cache with a table of operations that can be
155 performed on the denizens of the cache. These are held in a structure of type:
157 struct fscache_cache_ops
159 (*) Name of cache provider [mandatory]:
163 This isn't strictly an operation, but should be pointed at a string naming
166 (*) Object lookup [mandatory]:
168 struct fscache_object *(*lookup_object)(struct fscache_cache *cache,
169 struct fscache_object *parent,
170 struct fscache_cookie *cookie)
172 This method is used to look up an object in the specified cache, given a
173 pointer to the parent object and the cookie to which the object will be
174 attached. This should instantiate that object in the cache if it can, or
175 return -ENOBUFS or -ENOMEM if it can't.
177 (*) Increment object refcount [mandatory]:
179 struct fscache_object *(*grab_object)(struct fscache_object *object)
181 This method is called to increment the reference count on an object. It
182 may fail (for instance if the cache is being withdrawn) by returning NULL.
183 It should return the object pointer if successful.
185 (*) Lock/Unlock object [mandatory]:
187 void (*lock_object)(struct fscache_object *object)
188 void (*unlock_object)(struct fscache_object *object)
190 These methods are used to exclusively lock an object. It must be possible
191 to schedule with the lock held, so a spinlock isn't sufficient.
193 (*) Pin/Unpin object [optional]:
195 int (*pin_object)(struct fscache_object *object)
196 void (*unpin_object)(struct fscache_object *object)
198 These methods are used to pin an object into the cache. Once pinned an
199 object cannot be reclaimed to make space. Return -ENOSPC if there's not
200 enough space in the cache to permit this.
202 (*) Update object [mandatory]:
204 int (*update_object)(struct fscache_object *object)
206 This is called to update the index entry for the specified object. The
207 new information should be in object->cookie->netfs_data. This can be
208 obtained by calling object->cookie->def->get_aux()/get_attr().
210 (*) Release object reference [mandatory]:
212 void (*put_object)(struct fscache_object *object)
214 This method is used to discard a reference to an object. The object may
215 be destroyed when all the references held by FS-Cache are released.
217 (*) Synchronise a cache [mandatory]:
219 void (*sync)(struct fscache_cache *cache)
221 This is called to ask the backend to synchronise a cache with its backing
224 (*) Dissociate a cache [mandatory]:
226 void (*dissociate_pages)(struct fscache_cache *cache)
228 This is called to ask a cache to perform any page dissociations as part of
231 (*) Set the data size on a cache file [mandatory]:
233 int (*set_i_size)(struct fscache_object *object, loff_t i_size);
235 This is called to indicate to the cache the maximum size a file may reach.
236 The cache may use this to reserve space on the cache. It may also return
237 -ENOBUFS to indicate that insufficient space is available to expand the
238 metadata used to track the data. It should return 0 if successful or
239 -ENOMEM or -EIO on error.
241 (*) Reserve cache space for an object's data [optional]:
243 int (*reserve_space)(struct fscache_object *object, loff_t size);
245 This is called to request that cache space be reserved to hold the data
246 for an object and the metadata used to track it. Zero size should be
247 taken as request to cancel a reservation.
249 This should return 0 if successful, -ENOSPC if there isn't enough space
250 available, or -ENOMEM or -EIO on other errors.
252 The reservation may exceed the size of the object, thus permitting future
253 expansion. If the amount of space consumed by an object would exceed the
254 reservation, it's permitted to refuse requests to allocate pages, but not
255 required. An object may be pruned down to its reservation size if larger
258 (*) Request page be read from cache [mandatory]:
260 int (*read_or_alloc_page)(struct fscache_object *object,
262 fscache_rw_complete_t end_io_func,
266 This is called to attempt to read a netfs page from the cache, or to
267 reserve a backing block if not. FS-Cache will have done as much checking
268 as it can before calling, but most of the work belongs to the backend.
270 If there's no page in the cache, then -ENODATA should be returned if the
271 backend managed to reserve a backing block; -ENOBUFS, -ENOMEM or -EIO if
274 If there is a page in the cache, then a read operation should be queued
275 and 0 returned. When the read finishes, end_io_func() should be called
276 with the following arguments:
278 (*end_io_func)(object->cookie->netfs_data,
283 The mark_pages_cached() cookie operation should be called for the page if
284 any cache metadata is retained. This will indicate to the netfs that the
285 page needs explicit uncaching. This operation takes a pagevec, thus
286 allowing several pages to be marked at once.
288 (*) Request pages be read from cache [mandatory]:
290 int (*read_or_alloc_pages)(struct fscache_object *object,
291 struct address_space *mapping,
292 struct list_head *pages,
294 fscache_rw_complete_t end_io_func,
298 This is like the previous operation, except it will be handed a list of
299 pages instead of one page. Any pages on which a read operation is started
300 must be added to the page cache for the specified mapping and also to the
301 LRU. Such pages must also be removed from the pages list and nr_pages
302 decremented per page.
304 If there was an error such as -ENOMEM, then that should be returned; else
305 if one or more pages couldn't be read or allocated, then -ENOBUFS should
306 be returned; else if one or more pages couldn't be read, then -ENODATA
307 should be returned. If all the pages are dispatched then 0 should be
310 (*) Request page be allocated in the cache [mandatory]:
312 int (*allocate_page)(struct fscache_object *object,
316 This is like read_or_alloc_page(), except that it shouldn't read from the
317 cache, even if there's data there that could be retrieved. It should,
318 however, set up any internal metadata required such that write_page() can
321 If there's no backing block available, then -ENOBUFS should be returned
322 (or -ENOMEM or -EIO if there were other problems). If a block is
323 successfully allocated, then the netfs page should be marked and 0
326 (*) Request page be written to cache [mandatory]:
328 int (*write_page)(struct fscache_object *object,
330 fscache_rw_complete_t end_io_func,
334 This is called to write from a page on which there was a previously
335 successful read_or_alloc_page() call. FS-Cache filters out pages that
338 If there's no backing block available, then -ENOBUFS should be returned
339 (or -ENOMEM or -EIO if there were other problems).
341 If the write operation could be queued, then 0 should be returned. When
342 the write completes, end_io_func() should be called with the following
345 (*end_io_func)(object->cookie->netfs_data,
350 (*) Discard retained per-page metadata [mandatory]:
352 void (*uncache_pages)(struct fscache_object *object,
353 struct pagevec *pagevec)
355 This is called when one or more netfs pages are being evicted from the
356 pagecache. The cache backend should tear down any internal representation
357 or tracking it maintains.