backend-api.txt 23 KB

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