FS-Cache: Add the FS-Cache cache backend API and documentation
[linux-2.6/verdex.git] / Documentation / filesystems / caching / backend-api.txt
blob17723053aa91fdd91382f7ab5fcaea1828feccc7
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
7 parties.
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,
24                                 const char *idfmt,
25                                 ...);
27 Where:
29  (*) "cache" is a pointer to the cache definition;
31  (*) "ops" is a pointer to the table of operations that the backend supports on
32      this cache; and
34  (*) "idfmt" is a format and printf-style arguments for constructing a label
35      for the cache.
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,
43                               const char *tagname);
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
49      here.  FS-Cache keeps the caller's reference to the index object if
50      successful and will release it upon withdrawal of the cache.
52  (*) "tagname" which, if given, should be a text string naming this cache.  If
53      this is NULL, the identifier will be used instead.  For CacheFS, the
54      identifier is set to name the underlying block device and the tag can be
55      supplied by mount.
57 This function may return -ENOMEM if it ran out of memory or -EEXIST if the tag
58 is already in use.  0 will be returned on success.
61 =====================
62 UNREGISTERING A CACHE
63 =====================
65 A cache can be withdrawn from the system by calling this function with a
66 pointer to the cache definition:
68         void fscache_withdraw_cache(struct fscache_cache *cache);
70 In CacheFS's case, this is called by put_super().
73 ========
74 SECURITY
75 ========
77 The cache methods are executed one of two contexts:
79  (1) that of the userspace process that issued the netfs operation that caused
80      the cache method to be invoked, or
82  (2) that of one of the processes in the FS-Cache thread pool.
84 In either case, this may not be an appropriate context in which to access the
85 cache.
87 The calling process's fsuid, fsgid and SELinux security identities may need to
88 be masqueraded for the duration of the cache driver's access to the cache.
89 This is left to the cache to handle; FS-Cache makes no effort in this regard.
92 ===================================
93 CONTROL AND STATISTICS PRESENTATION
94 ===================================
96 The cache may present data to the outside world through FS-Cache's interfaces
97 in sysfs and procfs - the former for control and the latter for statistics.
99 A sysfs directory called /sys/fs/fscache/<cachetag>/ is created if CONFIG_SYSFS
100 is enabled.  This is accessible through the kobject struct fscache_cache::kobj
101 and is for use by the cache as it sees fit.
103 The cache driver may create itself a directory named for the cache type in the
104 /proc/fs/fscache/ directory.  This is available if CONFIG_FSCACHE_PROC is
105 enabled and is accessible through:
107         struct proc_dir_entry *proc_fscache;
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;
119                 void                            *netfs_data;
120                 ...
121         };
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
127      cache operations.
130  (*) In-cache object representation:
132         struct fscache_object {
133                 int                             debug_id;
134                 enum {
135                         FSCACHE_OBJECT_RECYCLING,
136                         ...
137                 }                               state;
138                 spinlock_t                      lock
139                 struct fscache_cache            *cache;
140                 struct fscache_cookie           *cookie;
141                 ...
142         };
144      Structures of this type should be allocated by the cache backend and
145      passed to FS-Cache when requested by the appropriate cache operation.  In
146      the case of CacheFS, they're embedded in CacheFS's internal object
147      structures.
149      The debug_id is a simple integer that can be used in debugging messages
150      that refer to a particular object.  In such a case it should be printed
151      using "OBJ%x" to be consistent with FS-Cache.
153      Each object contains a pointer to the cookie that represents the object it
154      is backing.  An object should retired when put_object() is called if it is
155      in state FSCACHE_OBJECT_RECYCLING.  The fscache_object struct should be
156      initialised by calling fscache_object_init(object).
159  (*) FS-Cache operation record:
161         struct fscache_operation {
162                 atomic_t                usage;
163                 struct fscache_object   *object;
164                 unsigned long           flags;
165         #define FSCACHE_OP_EXCLUSIVE
166                 void (*processor)(struct fscache_operation *op);
167                 void (*release)(struct fscache_operation *op);
168                 ...
169         };
171      FS-Cache has a pool of threads that it uses to give CPU time to the
172      various asynchronous operations that need to be done as part of driving
173      the cache.  These are represented by the above structure.  The processor
174      method is called to give the op CPU time, and the release method to get
175      rid of it when its usage count reaches 0.
177      An operation can be made exclusive upon an object by setting the
178      appropriate flag before enqueuing it with fscache_enqueue_operation().  If
179      an operation needs more processing time, it should be enqueued again.
182  (*) FS-Cache retrieval operation record:
184         struct fscache_retrieval {
185                 struct fscache_operation op;
186                 struct address_space    *mapping;
187                 struct list_head        *to_do;
188                 ...
189         };
191      A structure of this type is allocated by FS-Cache to record retrieval and
192      allocation requests made by the netfs.  This struct is then passed to the
193      backend to do the operation.  The backend may get extra refs to it by
194      calling fscache_get_retrieval() and refs may be discarded by calling
195      fscache_put_retrieval().
197      A retrieval operation can be used by the backend to do retrieval work.  To
198      do this, the retrieval->op.processor method pointer should be set
199      appropriately by the backend and fscache_enqueue_retrieval() called to
200      submit it to the thread pool.  CacheFiles, for example, uses this to queue
201      page examination when it detects PG_lock being cleared.
203      The to_do field is an empty list available for the cache backend to use as
204      it sees fit.
207  (*) FS-Cache storage operation record:
209         struct fscache_storage {
210                 struct fscache_operation op;
211                 pgoff_t                 store_limit;
212                 ...
213         };
215      A structure of this type is allocated by FS-Cache to record outstanding
216      writes to be made.  FS-Cache itself enqueues this operation and invokes
217      the write_page() method on the object at appropriate times to effect
218      storage.
221 ================
222 CACHE OPERATIONS
223 ================
225 The cache backend provides FS-Cache with a table of operations that can be
226 performed on the denizens of the cache.  These are held in a structure of type:
228         struct fscache_cache_ops
230  (*) Name of cache provider [mandatory]:
232         const char *name
234      This isn't strictly an operation, but should be pointed at a string naming
235      the backend.
238  (*) Allocate a new object [mandatory]:
240         struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
241                                                struct fscache_cookie *cookie)
243      This method is used to allocate a cache object representation to back a
244      cookie in a particular cache.  fscache_object_init() should be called on
245      the object to initialise it prior to returning.
247      This function may also be used to parse the index key to be used for
248      multiple lookup calls to turn it into a more convenient form.  FS-Cache
249      will call the lookup_complete() method to allow the cache to release the
250      form once lookup is complete or aborted.
253  (*) Look up and create object [mandatory]:
255         void (*lookup_object)(struct fscache_object *object)
257      This method is used to look up an object, given that the object is already
258      allocated and attached to the cookie.  This should instantiate that object
259      in the cache if it can.
261      The method should call fscache_object_lookup_negative() as soon as
262      possible if it determines the object doesn't exist in the cache.  If the
263      object is found to exist and the netfs indicates that it is valid then
264      fscache_obtained_object() should be called once the object is in a
265      position to have data stored in it.  Similarly, fscache_obtained_object()
266      should also be called once a non-present object has been created.
268      If a lookup error occurs, fscache_object_lookup_error() should be called
269      to abort the lookup of that object.
272  (*) Release lookup data [mandatory]:
274         void (*lookup_complete)(struct fscache_object *object)
276      This method is called to ask the cache to release any resources it was
277      using to perform a lookup.
280  (*) Increment object refcount [mandatory]:
282         struct fscache_object *(*grab_object)(struct fscache_object *object)
284      This method is called to increment the reference count on an object.  It
285      may fail (for instance if the cache is being withdrawn) by returning NULL.
286      It should return the object pointer if successful.
289  (*) Lock/Unlock object [mandatory]:
291         void (*lock_object)(struct fscache_object *object)
292         void (*unlock_object)(struct fscache_object *object)
294      These methods are used to exclusively lock an object.  It must be possible
295      to schedule with the lock held, so a spinlock isn't sufficient.
298  (*) Pin/Unpin object [optional]:
300         int (*pin_object)(struct fscache_object *object)
301         void (*unpin_object)(struct fscache_object *object)
303      These methods are used to pin an object into the cache.  Once pinned an
304      object cannot be reclaimed to make space.  Return -ENOSPC if there's not
305      enough space in the cache to permit this.
308  (*) Update object [mandatory]:
310         int (*update_object)(struct fscache_object *object)
312      This is called to update the index entry for the specified object.  The
313      new information should be in object->cookie->netfs_data.  This can be
314      obtained by calling object->cookie->def->get_aux()/get_attr().
317  (*) Discard object [mandatory]:
319         void (*drop_object)(struct fscache_object *object)
321      This method is called to indicate that an object has been unbound from its
322      cookie, and that the cache should release the object's resources and
323      retire it if it's in state FSCACHE_OBJECT_RECYCLING.
325      This method should not attempt to release any references held by the
326      caller.  The caller will invoke the put_object() method as appropriate.
329  (*) Release object reference [mandatory]:
331         void (*put_object)(struct fscache_object *object)
333      This method is used to discard a reference to an object.  The object may
334      be freed when all the references to it are released.
337  (*) Synchronise a cache [mandatory]:
339         void (*sync)(struct fscache_cache *cache)
341      This is called to ask the backend to synchronise a cache with its backing
342      device.
345  (*) Dissociate a cache [mandatory]:
347         void (*dissociate_pages)(struct fscache_cache *cache)
349      This is called to ask a cache to perform any page dissociations as part of
350      cache withdrawal.
353  (*) Notification that the attributes on a netfs file changed [mandatory]:
355         int (*attr_changed)(struct fscache_object *object);
357      This is called to indicate to the cache that certain attributes on a netfs
358      file have changed (for example the maximum size a file may reach).  The
359      cache can read these from the netfs by calling the cookie's get_attr()
360      method.
362      The cache may use the file size information to reserve space on the cache.
363      It should also call fscache_set_store_limit() to indicate to FS-Cache the
364      highest byte it's willing to store for an object.
366      This method may return -ve if an error occurred or the cache object cannot
367      be expanded.  In such a case, the object will be withdrawn from service.
369      This operation is run asynchronously from FS-Cache's thread pool, and
370      storage and retrieval operations from the netfs are excluded during the
371      execution of this operation.
374  (*) Reserve cache space for an object's data [optional]:
376         int (*reserve_space)(struct fscache_object *object, loff_t size);
378      This is called to request that cache space be reserved to hold the data
379      for an object and the metadata used to track it.  Zero size should be
380      taken as request to cancel a reservation.
382      This should return 0 if successful, -ENOSPC if there isn't enough space
383      available, or -ENOMEM or -EIO on other errors.
385      The reservation may exceed the current size of the object, thus permitting
386      future expansion.  If the amount of space consumed by an object would
387      exceed the reservation, it's permitted to refuse requests to allocate
388      pages, but not required.  An object may be pruned down to its reservation
389      size if larger than that already.
392  (*) Request page be read from cache [mandatory]:
394         int (*read_or_alloc_page)(struct fscache_retrieval *op,
395                                   struct page *page,
396                                   gfp_t gfp)
398      This is called to attempt to read a netfs page from the cache, or to
399      reserve a backing block if not.  FS-Cache will have done as much checking
400      as it can before calling, but most of the work belongs to the backend.
402      If there's no page in the cache, then -ENODATA should be returned if the
403      backend managed to reserve a backing block; -ENOBUFS or -ENOMEM if it
404      didn't.
406      If there is suitable data in the cache, then a read operation should be
407      queued and 0 returned.  When the read finishes, fscache_end_io() should be
408      called.
410      The fscache_mark_pages_cached() should be called for the page if any cache
411      metadata is retained.  This will indicate to the netfs that the page needs
412      explicit uncaching.  This operation takes a pagevec, thus allowing several
413      pages to be marked at once.
415      The retrieval record pointed to by op should be retained for each page
416      queued and released when I/O on the page has been formally ended.
417      fscache_get/put_retrieval() are available for this purpose.
419      The retrieval record may be used to get CPU time via the FS-Cache thread
420      pool.  If this is desired, the op->op.processor should be set to point to
421      the appropriate processing routine, and fscache_enqueue_retrieval() should
422      be called at an appropriate point to request CPU time.  For instance, the
423      retrieval routine could be enqueued upon the completion of a disk read.
424      The to_do field in the retrieval record is provided to aid in this.
426      If an I/O error occurs, fscache_io_error() should be called and -ENOBUFS
427      returned if possible or fscache_end_io() called with a suitable error
428      code..
431  (*) Request pages be read from cache [mandatory]:
433         int (*read_or_alloc_pages)(struct fscache_retrieval *op,
434                                    struct list_head *pages,
435                                    unsigned *nr_pages,
436                                    gfp_t gfp)
438      This is like the read_or_alloc_page() method, except it is handed a list
439      of pages instead of one page.  Any pages on which a read operation is
440      started must be added to the page cache for the specified mapping and also
441      to the LRU.  Such pages must also be removed from the pages list and
442      *nr_pages decremented per page.
444      If there was an error such as -ENOMEM, then that should be returned; else
445      if one or more pages couldn't be read or allocated, then -ENOBUFS should
446      be returned; else if one or more pages couldn't be read, then -ENODATA
447      should be returned.  If all the pages are dispatched then 0 should be
448      returned.
451  (*) Request page be allocated in the cache [mandatory]:
453         int (*allocate_page)(struct fscache_retrieval *op,
454                              struct page *page,
455                              gfp_t gfp)
457      This is like the read_or_alloc_page() method, except that it shouldn't
458      read from the cache, even if there's data there that could be retrieved.
459      It should, however, set up any internal metadata required such that
460      the write_page() method can write to the cache.
462      If there's no backing block available, then -ENOBUFS should be returned
463      (or -ENOMEM if there were other problems).  If a block is successfully
464      allocated, then the netfs page should be marked and 0 returned.
467  (*) Request pages be allocated in the cache [mandatory]:
469         int (*allocate_pages)(struct fscache_retrieval *op,
470                               struct list_head *pages,
471                               unsigned *nr_pages,
472                               gfp_t gfp)
474      This is an multiple page version of the allocate_page() method.  pages and
475      nr_pages should be treated as for the read_or_alloc_pages() method.
478  (*) Request page be written to cache [mandatory]:
480         int (*write_page)(struct fscache_storage *op,
481                           struct page *page);
483      This is called to write from a page on which there was a previously
484      successful read_or_alloc_page() call or similar.  FS-Cache filters out
485      pages that don't have mappings.
487      This method is called asynchronously from the FS-Cache thread pool.  It is
488      not required to actually store anything, provided -ENODATA is then
489      returned to the next read of this page.
491      If an error occurred, then a negative error code should be returned,
492      otherwise zero should be returned.  FS-Cache will take appropriate action
493      in response to an error, such as withdrawing this object.
495      If this method returns success then FS-Cache will inform the netfs
496      appropriately.
499  (*) Discard retained per-page metadata [mandatory]:
501         void (*uncache_page)(struct fscache_object *object, struct page *page)
503      This is called when a netfs page is being evicted from the pagecache.  The
504      cache backend should tear down any internal representation or tracking it
505      maintains for this page.
508 ==================
509 FS-CACHE UTILITIES
510 ==================
512 FS-Cache provides some utilities that a cache backend may make use of:
514  (*) Note occurrence of an I/O error in a cache:
516         void fscache_io_error(struct fscache_cache *cache)
518      This tells FS-Cache that an I/O error occurred in the cache.  After this
519      has been called, only resource dissociation operations (object and page
520      release) will be passed from the netfs to the cache backend for the
521      specified cache.
523      This does not actually withdraw the cache.  That must be done separately.
526  (*) Invoke the retrieval I/O completion function:
528         void fscache_end_io(struct fscache_retrieval *op, struct page *page,
529                             int error);
531      This is called to note the end of an attempt to retrieve a page.  The
532      error value should be 0 if successful and an error otherwise.
535  (*) Set highest store limit:
537         void fscache_set_store_limit(struct fscache_object *object,
538                                      loff_t i_size);
540      This sets the limit FS-Cache imposes on the highest byte it's willing to
541      try and store for a netfs.  Any page over this limit is automatically
542      rejected by fscache_read_alloc_page() and co with -ENOBUFS.
545  (*) Mark pages as being cached:
547         void fscache_mark_pages_cached(struct fscache_retrieval *op,
548                                        struct pagevec *pagevec);
550      This marks a set of pages as being cached.  After this has been called,
551      the netfs must call fscache_uncache_page() to unmark the pages.
554  (*) Perform coherency check on an object:
556         enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
557                                                 const void *data,
558                                                 uint16_t datalen);
560      This asks the netfs to perform a coherency check on an object that has
561      just been looked up.  The cookie attached to the object will determine the
562      netfs to use.  data and datalen should specify where the auxiliary data
563      retrieved from the cache can be found.
565      One of three values will be returned:
567         (*) FSCACHE_CHECKAUX_OKAY
569             The coherency data indicates the object is valid as is.
571         (*) FSCACHE_CHECKAUX_NEEDS_UPDATE
573             The coherency data needs updating, but otherwise the object is
574             valid.
576         (*) FSCACHE_CHECKAUX_OBSOLETE
578             The coherency data indicates that the object is obsolete and should
579             be discarded.
582  (*) Initialise a freshly allocated object:
584         void fscache_object_init(struct fscache_object *object);
586      This initialises all the fields in an object representation.
589  (*) Indicate the destruction of an object:
591         void fscache_object_destroyed(struct fscache_cache *cache);
593      This must be called to inform FS-Cache that an object that belonged to a
594      cache has been destroyed and deallocated.  This will allow continuation
595      of the cache withdrawal process when it is stopped pending destruction of
596      all the objects.
599  (*) Indicate negative lookup on an object:
601         void fscache_object_lookup_negative(struct fscache_object *object);
603      This is called to indicate to FS-Cache that a lookup process for an object
604      found a negative result.
606      This changes the state of an object to permit reads pending on lookup
607      completion to go off and start fetching data from the netfs server as it's
608      known at this point that there can't be any data in the cache.
610      This may be called multiple times on an object.  Only the first call is
611      significant - all subsequent calls are ignored.
614  (*) Indicate an object has been obtained:
616         void fscache_obtained_object(struct fscache_object *object);
618      This is called to indicate to FS-Cache that a lookup process for an object
619      produced a positive result, or that an object was created.  This should
620      only be called once for any particular object.
622      This changes the state of an object to indicate:
624         (1) if no call to fscache_object_lookup_negative() has been made on
625             this object, that there may be data available, and that reads can
626             now go and look for it; and
628         (2) that writes may now proceed against this object.
631  (*) Indicate that object lookup failed:
633         void fscache_object_lookup_error(struct fscache_object *object);
635      This marks an object as having encountered a fatal error (usually EIO)
636      and causes it to move into a state whereby it will be withdrawn as soon
637      as possible.
640  (*) Get and release references on a retrieval record:
642         void fscache_get_retrieval(struct fscache_retrieval *op);
643         void fscache_put_retrieval(struct fscache_retrieval *op);
645      These two functions are used to retain a retrieval record whilst doing
646      asynchronous data retrieval and block allocation.
649  (*) Enqueue a retrieval record for processing.
651         void fscache_enqueue_retrieval(struct fscache_retrieval *op);
653      This enqueues a retrieval record for processing by the FS-Cache thread
654      pool.  One of the threads in the pool will invoke the retrieval record's
655      op->op.processor callback function.  This function may be called from
656      within the callback function.
659  (*) List of object state names:
661         const char *fscache_object_states[];
663      For debugging purposes, this may be used to turn the state that an object
664      is in into a text string for display purposes.