backend-api.txt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. ==========================
  2. FS-CACHE CACHE BACKEND API
  3. ==========================
  4. The FS-Cache system provides an API by which actual caches can be supplied to
  5. FS-Cache for it to then serve out to network filesystems and other interested
  6. parties.
  7. This API is declared in <linux/fscache-cache.h>.
  8. ====================================
  9. INITIALISING AND REGISTERING A CACHE
  10. ====================================
  11. To start off, a cache definition must be initialised and registered for each
  12. cache the backend wants to make available. For instance, CacheFS does this in
  13. the fill_super() operation on mounting.
  14. The cache definition (struct fscache_cache) should be initialised by calling:
  15. void fscache_init_cache(struct fscache_cache *cache,
  16. struct fscache_cache_ops *ops,
  17. const char *idfmt,
  18. ...);
  19. Where:
  20. (*) "cache" is a pointer to the cache definition;
  21. (*) "ops" is a pointer to the table of operations that the backend supports on
  22. this cache; and
  23. (*) "idfmt" is a format and printf-style arguments for constructing a label
  24. for the cache.
  25. The cache should then be registered with FS-Cache by passing a pointer to the
  26. previously initialised cache definition to:
  27. int fscache_add_cache(struct fscache_cache *cache,
  28. struct fscache_object *fsdef,
  29. const char *tagname);
  30. Two extra arguments should also be supplied:
  31. (*) "fsdef" which should point to the object representation for the FS-Cache
  32. master index in this cache. Netfs primary index entries will be created
  33. here. FS-Cache keeps the caller's reference to the index object if
  34. successful and will release it upon withdrawal of the cache.
  35. (*) "tagname" which, if given, should be a text string naming this cache. If
  36. this is NULL, the identifier will be used instead. For CacheFS, the
  37. identifier is set to name the underlying block device and the tag can be
  38. supplied by mount.
  39. This function may return -ENOMEM if it ran out of memory or -EEXIST if the tag
  40. is already in use. 0 will be returned on success.
  41. =====================
  42. UNREGISTERING A CACHE
  43. =====================
  44. A cache can be withdrawn from the system by calling this function with a
  45. pointer to the cache definition:
  46. void fscache_withdraw_cache(struct fscache_cache *cache);
  47. In CacheFS's case, this is called by put_super().
  48. ========
  49. SECURITY
  50. ========
  51. The cache methods are executed one of two contexts:
  52. (1) that of the userspace process that issued the netfs operation that caused
  53. the cache method to be invoked, or
  54. (2) that of one of the processes in the FS-Cache thread pool.
  55. In either case, this may not be an appropriate context in which to access the
  56. cache.
  57. The calling process's fsuid, fsgid and SELinux security identities may need to
  58. be masqueraded for the duration of the cache driver's access to the cache.
  59. This is left to the cache to handle; FS-Cache makes no effort in this regard.
  60. ===================================
  61. CONTROL AND STATISTICS PRESENTATION
  62. ===================================
  63. The cache may present data to the outside world through FS-Cache's interfaces
  64. in sysfs and procfs - the former for control and the latter for statistics.
  65. A sysfs directory called /sys/fs/fscache/<cachetag>/ is created if CONFIG_SYSFS
  66. is enabled. This is accessible through the kobject struct fscache_cache::kobj
  67. and is for use by the cache as it sees fit.
  68. ========================
  69. RELEVANT DATA STRUCTURES
  70. ========================
  71. (*) Index/Data file FS-Cache representation cookie:
  72. struct fscache_cookie {
  73. struct fscache_object_def *def;
  74. struct fscache_netfs *netfs;
  75. void *netfs_data;
  76. ...
  77. };
  78. The fields that might be of use to the backend describe the object
  79. definition, the netfs definition and the netfs's data for this cookie.
  80. The object definition contain functions supplied by the netfs for loading
  81. and matching index entries; these are required to provide some of the
  82. cache operations.
  83. (*) In-cache object representation:
  84. struct fscache_object {
  85. int debug_id;
  86. enum {
  87. FSCACHE_OBJECT_RECYCLING,
  88. ...
  89. } state;
  90. spinlock_t lock
  91. struct fscache_cache *cache;
  92. struct fscache_cookie *cookie;
  93. ...
  94. };
  95. Structures of this type should be allocated by the cache backend and
  96. passed to FS-Cache when requested by the appropriate cache operation. In
  97. the case of CacheFS, they're embedded in CacheFS's internal object
  98. structures.
  99. The debug_id is a simple integer that can be used in debugging messages
  100. that refer to a particular object. In such a case it should be printed
  101. using "OBJ%x" to be consistent with FS-Cache.
  102. Each object contains a pointer to the cookie that represents the object it
  103. is backing. An object should retired when put_object() is called if it is
  104. in state FSCACHE_OBJECT_RECYCLING. The fscache_object struct should be
  105. initialised by calling fscache_object_init(object).
  106. (*) FS-Cache operation record:
  107. struct fscache_operation {
  108. atomic_t usage;
  109. struct fscache_object *object;
  110. unsigned long flags;
  111. #define FSCACHE_OP_EXCLUSIVE
  112. void (*processor)(struct fscache_operation *op);
  113. void (*release)(struct fscache_operation *op);
  114. ...
  115. };
  116. FS-Cache has a pool of threads that it uses to give CPU time to the
  117. various asynchronous operations that need to be done as part of driving
  118. the cache. These are represented by the above structure. The processor
  119. method is called to give the op CPU time, and the release method to get
  120. rid of it when its usage count reaches 0.
  121. An operation can be made exclusive upon an object by setting the
  122. appropriate flag before enqueuing it with fscache_enqueue_operation(). If
  123. an operation needs more processing time, it should be enqueued again.
  124. (*) FS-Cache retrieval operation record:
  125. struct fscache_retrieval {
  126. struct fscache_operation op;
  127. struct address_space *mapping;
  128. struct list_head *to_do;
  129. ...
  130. };
  131. A structure of this type is allocated by FS-Cache to record retrieval and
  132. allocation requests made by the netfs. This struct is then passed to the
  133. backend to do the operation. The backend may get extra refs to it by
  134. calling fscache_get_retrieval() and refs may be discarded by calling
  135. fscache_put_retrieval().
  136. A retrieval operation can be used by the backend to do retrieval work. To
  137. do this, the retrieval->op.processor method pointer should be set
  138. appropriately by the backend and fscache_enqueue_retrieval() called to
  139. submit it to the thread pool. CacheFiles, for example, uses this to queue
  140. page examination when it detects PG_lock being cleared.
  141. The to_do field is an empty list available for the cache backend to use as
  142. it sees fit.
  143. (*) FS-Cache storage operation record:
  144. struct fscache_storage {
  145. struct fscache_operation op;
  146. pgoff_t store_limit;
  147. ...
  148. };
  149. A structure of this type is allocated by FS-Cache to record outstanding
  150. writes to be made. FS-Cache itself enqueues this operation and invokes
  151. the write_page() method on the object at appropriate times to effect
  152. storage.
  153. ================
  154. CACHE OPERATIONS
  155. ================
  156. The cache backend provides FS-Cache with a table of operations that can be
  157. performed on the denizens of the cache. These are held in a structure of type:
  158. struct fscache_cache_ops
  159. (*) Name of cache provider [mandatory]:
  160. const char *name
  161. This isn't strictly an operation, but should be pointed at a string naming
  162. the backend.
  163. (*) Allocate a new object [mandatory]:
  164. struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
  165. struct fscache_cookie *cookie)
  166. This method is used to allocate a cache object representation to back a
  167. cookie in a particular cache. fscache_object_init() should be called on
  168. the object to initialise it prior to returning.
  169. This function may also be used to parse the index key to be used for
  170. multiple lookup calls to turn it into a more convenient form. FS-Cache
  171. will call the lookup_complete() method to allow the cache to release the
  172. form once lookup is complete or aborted.
  173. (*) Look up and create object [mandatory]:
  174. void (*lookup_object)(struct fscache_object *object)
  175. This method is used to look up an object, given that the object is already
  176. allocated and attached to the cookie. This should instantiate that object
  177. in the cache if it can.
  178. The method should call fscache_object_lookup_negative() as soon as
  179. possible if it determines the object doesn't exist in the cache. If the
  180. object is found to exist and the netfs indicates that it is valid then
  181. fscache_obtained_object() should be called once the object is in a
  182. position to have data stored in it. Similarly, fscache_obtained_object()
  183. should also be called once a non-present object has been created.
  184. If a lookup error occurs, fscache_object_lookup_error() should be called
  185. to abort the lookup of that object.
  186. (*) Release lookup data [mandatory]:
  187. void (*lookup_complete)(struct fscache_object *object)
  188. This method is called to ask the cache to release any resources it was
  189. using to perform a lookup.
  190. (*) Increment object refcount [mandatory]:
  191. struct fscache_object *(*grab_object)(struct fscache_object *object)
  192. This method is called to increment the reference count on an object. It
  193. may fail (for instance if the cache is being withdrawn) by returning NULL.
  194. It should return the object pointer if successful.
  195. (*) Lock/Unlock object [mandatory]:
  196. void (*lock_object)(struct fscache_object *object)
  197. void (*unlock_object)(struct fscache_object *object)
  198. These methods are used to exclusively lock an object. It must be possible
  199. to schedule with the lock held, so a spinlock isn't sufficient.
  200. (*) Pin/Unpin object [optional]:
  201. int (*pin_object)(struct fscache_object *object)
  202. void (*unpin_object)(struct fscache_object *object)
  203. These methods are used to pin an object into the cache. Once pinned an
  204. object cannot be reclaimed to make space. Return -ENOSPC if there's not
  205. enough space in the cache to permit this.
  206. (*) Update object [mandatory]:
  207. int (*update_object)(struct fscache_object *object)
  208. This is called to update the index entry for the specified object. The
  209. new information should be in object->cookie->netfs_data. This can be
  210. obtained by calling object->cookie->def->get_aux()/get_attr().
  211. (*) Discard object [mandatory]:
  212. void (*drop_object)(struct fscache_object *object)
  213. This method is called to indicate that an object has been unbound from its
  214. cookie, and that the cache should release the object's resources and
  215. retire it if it's in state FSCACHE_OBJECT_RECYCLING.
  216. This method should not attempt to release any references held by the
  217. caller. The caller will invoke the put_object() method as appropriate.
  218. (*) Release object reference [mandatory]:
  219. void (*put_object)(struct fscache_object *object)
  220. This method is used to discard a reference to an object. The object may
  221. be freed when all the references to it are released.
  222. (*) Synchronise a cache [mandatory]:
  223. void (*sync)(struct fscache_cache *cache)
  224. This is called to ask the backend to synchronise a cache with its backing
  225. device.
  226. (*) Dissociate a cache [mandatory]:
  227. void (*dissociate_pages)(struct fscache_cache *cache)
  228. This is called to ask a cache to perform any page dissociations as part of
  229. cache withdrawal.
  230. (*) Notification that the attributes on a netfs file changed [mandatory]:
  231. int (*attr_changed)(struct fscache_object *object);
  232. This is called to indicate to the cache that certain attributes on a netfs
  233. file have changed (for example the maximum size a file may reach). The
  234. cache can read these from the netfs by calling the cookie's get_attr()
  235. method.
  236. The cache may use the file size information to reserve space on the cache.
  237. It should also call fscache_set_store_limit() to indicate to FS-Cache the
  238. highest byte it's willing to store for an object.
  239. This method may return -ve if an error occurred or the cache object cannot
  240. be expanded. In such a case, the object will be withdrawn from service.
  241. This operation is run asynchronously from FS-Cache's thread pool, and
  242. storage and retrieval operations from the netfs are excluded during the
  243. execution of this operation.
  244. (*) Reserve cache space for an object's data [optional]:
  245. int (*reserve_space)(struct fscache_object *object, loff_t size);
  246. This is called to request that cache space be reserved to hold the data
  247. for an object and the metadata used to track it. Zero size should be
  248. 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.
  251. The reservation may exceed the current size of the object, thus permitting
  252. future expansion. If the amount of space consumed by an object would
  253. exceed the reservation, it's permitted to refuse requests to allocate
  254. pages, but not required. An object may be pruned down to its reservation
  255. size if larger than that already.
  256. (*) Request page be read from cache [mandatory]:
  257. int (*read_or_alloc_page)(struct fscache_retrieval *op,
  258. struct page *page,
  259. gfp_t gfp)
  260. This is called to attempt to read a netfs page from the cache, or to
  261. reserve a backing block if not. FS-Cache will have done as much checking
  262. as it can before calling, but most of the work belongs to the backend.
  263. If there's no page in the cache, then -ENODATA should be returned if the
  264. backend managed to reserve a backing block; -ENOBUFS or -ENOMEM if it
  265. didn't.
  266. If there is suitable data in the cache, then a read operation should be
  267. queued and 0 returned. When the read finishes, fscache_end_io() should be
  268. called.
  269. The fscache_mark_pages_cached() should be called for the page if any cache
  270. metadata is retained. This will indicate to the netfs that the page needs
  271. explicit uncaching. This operation takes a pagevec, thus allowing several
  272. pages to be marked at once.
  273. The retrieval record pointed to by op should be retained for each page
  274. queued and released when I/O on the page has been formally ended.
  275. fscache_get/put_retrieval() are available for this purpose.
  276. The retrieval record may be used to get CPU time via the FS-Cache thread
  277. pool. If this is desired, the op->op.processor should be set to point to
  278. the appropriate processing routine, and fscache_enqueue_retrieval() should
  279. be called at an appropriate point to request CPU time. For instance, the
  280. retrieval routine could be enqueued upon the completion of a disk read.
  281. The to_do field in the retrieval record is provided to aid in this.
  282. If an I/O error occurs, fscache_io_error() should be called and -ENOBUFS
  283. returned if possible or fscache_end_io() called with a suitable error
  284. code.
  285. fscache_put_retrieval() should be called after a page or pages are dealt
  286. with. This will complete the operation when all pages are dealt with.
  287. (*) Request pages be read from cache [mandatory]:
  288. int (*read_or_alloc_pages)(struct fscache_retrieval *op,
  289. struct list_head *pages,
  290. unsigned *nr_pages,
  291. gfp_t gfp)
  292. This is like the read_or_alloc_page() method, except it is handed a list
  293. of pages instead of one page. Any pages on which a read operation is
  294. started must be added to the page cache for the specified mapping and also
  295. to the LRU. Such pages must also be removed from the pages list and
  296. *nr_pages decremented per page.
  297. If there was an error such as -ENOMEM, then that should be returned; else
  298. if one or more pages couldn't be read or allocated, then -ENOBUFS should
  299. be returned; else if one or more pages couldn't be read, then -ENODATA
  300. should be returned. If all the pages are dispatched then 0 should be
  301. returned.
  302. (*) Request page be allocated in the cache [mandatory]:
  303. int (*allocate_page)(struct fscache_retrieval *op,
  304. struct page *page,
  305. gfp_t gfp)
  306. This is like the read_or_alloc_page() method, except that it shouldn't
  307. read from the cache, even if there's data there that could be retrieved.
  308. It should, however, set up any internal metadata required such that
  309. the write_page() method can write to the cache.
  310. If there's no backing block available, then -ENOBUFS should be returned
  311. (or -ENOMEM if there were other problems). If a block is successfully
  312. allocated, then the netfs page should be marked and 0 returned.
  313. (*) Request pages be allocated in the cache [mandatory]:
  314. int (*allocate_pages)(struct fscache_retrieval *op,
  315. struct list_head *pages,
  316. unsigned *nr_pages,
  317. gfp_t gfp)
  318. This is an multiple page version of the allocate_page() method. pages and
  319. nr_pages should be treated as for the read_or_alloc_pages() method.
  320. (*) Request page be written to cache [mandatory]:
  321. int (*write_page)(struct fscache_storage *op,
  322. struct page *page);
  323. This is called to write from a page on which there was a previously
  324. successful read_or_alloc_page() call or similar. FS-Cache filters out
  325. pages that don't have mappings.
  326. This method is called asynchronously from the FS-Cache thread pool. It is
  327. not required to actually store anything, provided -ENODATA is then
  328. returned to the next read of this page.
  329. If an error occurred, then a negative error code should be returned,
  330. otherwise zero should be returned. FS-Cache will take appropriate action
  331. in response to an error, such as withdrawing this object.
  332. If this method returns success then FS-Cache will inform the netfs
  333. appropriately.
  334. (*) Discard retained per-page metadata [mandatory]:
  335. void (*uncache_page)(struct fscache_object *object, struct page *page)
  336. This is called when a netfs page is being evicted from the pagecache. The
  337. cache backend should tear down any internal representation or tracking it
  338. maintains for this page.
  339. ==================
  340. FS-CACHE UTILITIES
  341. ==================
  342. FS-Cache provides some utilities that a cache backend may make use of:
  343. (*) Note occurrence of an I/O error in a cache:
  344. void fscache_io_error(struct fscache_cache *cache)
  345. This tells FS-Cache that an I/O error occurred in the cache. After this
  346. has been called, only resource dissociation operations (object and page
  347. release) will be passed from the netfs to the cache backend for the
  348. specified cache.
  349. This does not actually withdraw the cache. That must be done separately.
  350. (*) Invoke the retrieval I/O completion function:
  351. void fscache_end_io(struct fscache_retrieval *op, struct page *page,
  352. int error);
  353. This is called to note the end of an attempt to retrieve a page. The
  354. error value should be 0 if successful and an error otherwise.
  355. (*) Record that one or more pages being retrieved or allocated have been dealt
  356. with:
  357. void fscache_retrieval_complete(struct fscache_retrieval *op,
  358. int n_pages);
  359. This is called to record the fact that one or more pages have been dealt
  360. with and are no longer the concern of this operation. When the number of
  361. pages remaining in the operation reaches 0, the operation will be
  362. completed.
  363. (*) Record operation completion:
  364. void fscache_op_complete(struct fscache_operation *op);
  365. This is called to record the completion of an operation. This deducts
  366. this operation from the parent object's run state, potentially permitting
  367. one or more pending operations to start running.
  368. (*) Set highest store limit:
  369. void fscache_set_store_limit(struct fscache_object *object,
  370. loff_t i_size);
  371. This sets the limit FS-Cache imposes on the highest byte it's willing to
  372. try and store for a netfs. Any page over this limit is automatically
  373. rejected by fscache_read_alloc_page() and co with -ENOBUFS.
  374. (*) Mark pages as being cached:
  375. void fscache_mark_pages_cached(struct fscache_retrieval *op,
  376. struct pagevec *pagevec);
  377. This marks a set of pages as being cached. After this has been called,
  378. the netfs must call fscache_uncache_page() to unmark the pages.
  379. (*) Perform coherency check on an object:
  380. enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  381. const void *data,
  382. uint16_t datalen);
  383. This asks the netfs to perform a coherency check on an object that has
  384. just been looked up. The cookie attached to the object will determine the
  385. netfs to use. data and datalen should specify where the auxiliary data
  386. retrieved from the cache can be found.
  387. One of three values will be returned:
  388. (*) FSCACHE_CHECKAUX_OKAY
  389. The coherency data indicates the object is valid as is.
  390. (*) FSCACHE_CHECKAUX_NEEDS_UPDATE
  391. The coherency data needs updating, but otherwise the object is
  392. valid.
  393. (*) FSCACHE_CHECKAUX_OBSOLETE
  394. The coherency data indicates that the object is obsolete and should
  395. be discarded.
  396. (*) Initialise a freshly allocated object:
  397. void fscache_object_init(struct fscache_object *object);
  398. This initialises all the fields in an object representation.
  399. (*) Indicate the destruction of an object:
  400. void fscache_object_destroyed(struct fscache_cache *cache);
  401. This must be called to inform FS-Cache that an object that belonged to a
  402. cache has been destroyed and deallocated. This will allow continuation
  403. of the cache withdrawal process when it is stopped pending destruction of
  404. all the objects.
  405. (*) Indicate negative lookup on an object:
  406. void fscache_object_lookup_negative(struct fscache_object *object);
  407. This is called to indicate to FS-Cache that a lookup process for an object
  408. found a negative result.
  409. This changes the state of an object to permit reads pending on lookup
  410. completion to go off and start fetching data from the netfs server as it's
  411. known at this point that there can't be any data in the cache.
  412. This may be called multiple times on an object. Only the first call is
  413. significant - all subsequent calls are ignored.
  414. (*) Indicate an object has been obtained:
  415. void fscache_obtained_object(struct fscache_object *object);
  416. This is called to indicate to FS-Cache that a lookup process for an object
  417. produced a positive result, or that an object was created. This should
  418. only be called once for any particular object.
  419. This changes the state of an object to indicate:
  420. (1) if no call to fscache_object_lookup_negative() has been made on
  421. this object, that there may be data available, and that reads can
  422. now go and look for it; and
  423. (2) that writes may now proceed against this object.
  424. (*) Indicate that object lookup failed:
  425. void fscache_object_lookup_error(struct fscache_object *object);
  426. This marks an object as having encountered a fatal error (usually EIO)
  427. and causes it to move into a state whereby it will be withdrawn as soon
  428. as possible.
  429. (*) Get and release references on a retrieval record:
  430. void fscache_get_retrieval(struct fscache_retrieval *op);
  431. void fscache_put_retrieval(struct fscache_retrieval *op);
  432. These two functions are used to retain a retrieval record whilst doing
  433. asynchronous data retrieval and block allocation.
  434. (*) Enqueue a retrieval record for processing.
  435. void fscache_enqueue_retrieval(struct fscache_retrieval *op);
  436. This enqueues a retrieval record for processing by the FS-Cache thread
  437. pool. One of the threads in the pool will invoke the retrieval record's
  438. op->op.processor callback function. This function may be called from
  439. within the callback function.
  440. (*) List of object state names:
  441. const char *fscache_object_states[];
  442. For debugging purposes, this may be used to turn the state that an object
  443. is in into a text string for display purposes.