netfs-api.txt 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. ===============================
  2. FS-CACHE NETWORK FILESYSTEM API
  3. ===============================
  4. There's an API by which a network filesystem can make use of the FS-Cache
  5. facilities. This is based around a number of principles:
  6. (1) Caches can store a number of different object types. There are two main
  7. object types: indices and files. The first is a special type used by
  8. FS-Cache to make finding objects faster and to make retiring of groups of
  9. objects easier.
  10. (2) Every index, file or other object is represented by a cookie. This cookie
  11. may or may not have anything associated with it, but the netfs doesn't
  12. need to care.
  13. (3) Barring the top-level index (one entry per cached netfs), the index
  14. hierarchy for each netfs is structured according the whim of the netfs.
  15. This API is declared in <linux/fscache.h>.
  16. This document contains the following sections:
  17. (1) Network filesystem definition
  18. (2) Index definition
  19. (3) Object definition
  20. (4) Network filesystem (un)registration
  21. (5) Cache tag lookup
  22. (6) Index registration
  23. (7) Data file registration
  24. (8) Miscellaneous object registration
  25. (9) Setting the data file size
  26. (10) Page alloc/read/write
  27. (11) Page uncaching
  28. (12) Index and data file consistency
  29. (13) Cookie enablement
  30. (14) Miscellaneous cookie operations
  31. (15) Cookie unregistration
  32. (16) Index invalidation
  33. (17) Data file invalidation
  34. (18) FS-Cache specific page flags.
  35. =============================
  36. NETWORK FILESYSTEM DEFINITION
  37. =============================
  38. FS-Cache needs a description of the network filesystem. This is specified
  39. using a record of the following structure:
  40. struct fscache_netfs {
  41. uint32_t version;
  42. const char *name;
  43. struct fscache_cookie *primary_index;
  44. ...
  45. };
  46. This first two fields should be filled in before registration, and the third
  47. will be filled in by the registration function; any other fields should just be
  48. ignored and are for internal use only.
  49. The fields are:
  50. (1) The name of the netfs (used as the key in the toplevel index).
  51. (2) The version of the netfs (if the name matches but the version doesn't, the
  52. entire in-cache hierarchy for this netfs will be scrapped and begun
  53. afresh).
  54. (3) The cookie representing the primary index will be allocated according to
  55. another parameter passed into the registration function.
  56. For example, kAFS (linux/fs/afs/) uses the following definitions to describe
  57. itself:
  58. struct fscache_netfs afs_cache_netfs = {
  59. .version = 0,
  60. .name = "afs",
  61. };
  62. ================
  63. INDEX DEFINITION
  64. ================
  65. Indices are used for two purposes:
  66. (1) To aid the finding of a file based on a series of keys (such as AFS's
  67. "cell", "volume ID", "vnode ID").
  68. (2) To make it easier to discard a subset of all the files cached based around
  69. a particular key - for instance to mirror the removal of an AFS volume.
  70. However, since it's unlikely that any two netfs's are going to want to define
  71. their index hierarchies in quite the same way, FS-Cache tries to impose as few
  72. restraints as possible on how an index is structured and where it is placed in
  73. the tree. The netfs can even mix indices and data files at the same level, but
  74. it's not recommended.
  75. Each index entry consists of a key of indeterminate length plus some auxiliary
  76. data, also of indeterminate length.
  77. There are some limits on indices:
  78. (1) Any index containing non-index objects should be restricted to a single
  79. cache. Any such objects created within an index will be created in the
  80. first cache only. The cache in which an index is created can be
  81. controlled by cache tags (see below).
  82. (2) The entry data must be atomically journallable, so it is limited to about
  83. 400 bytes at present. At least 400 bytes will be available.
  84. (3) The depth of the index tree should be judged with care as the search
  85. function is recursive. Too many layers will run the kernel out of stack.
  86. =================
  87. OBJECT DEFINITION
  88. =================
  89. To define an object, a structure of the following type should be filled out:
  90. struct fscache_cookie_def
  91. {
  92. uint8_t name[16];
  93. uint8_t type;
  94. struct fscache_cache_tag *(*select_cache)(
  95. const void *parent_netfs_data,
  96. const void *cookie_netfs_data);
  97. enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
  98. const void *data,
  99. uint16_t datalen,
  100. loff_t object_size);
  101. void (*get_context)(void *cookie_netfs_data, void *context);
  102. void (*put_context)(void *cookie_netfs_data, void *context);
  103. void (*mark_pages_cached)(void *cookie_netfs_data,
  104. struct address_space *mapping,
  105. struct pagevec *cached_pvec);
  106. };
  107. This has the following fields:
  108. (1) The type of the object [mandatory].
  109. This is one of the following values:
  110. (*) FSCACHE_COOKIE_TYPE_INDEX
  111. This defines an index, which is a special FS-Cache type.
  112. (*) FSCACHE_COOKIE_TYPE_DATAFILE
  113. This defines an ordinary data file.
  114. (*) Any other value between 2 and 255
  115. This defines an extraordinary object such as an XATTR.
  116. (2) The name of the object type (NUL terminated unless all 16 chars are used)
  117. [optional].
  118. (3) A function to select the cache in which to store an index [optional].
  119. This function is invoked when an index needs to be instantiated in a cache
  120. during the instantiation of a non-index object. Only the immediate index
  121. parent for the non-index object will be queried. Any indices above that
  122. in the hierarchy may be stored in multiple caches. This function does not
  123. need to be supplied for any non-index object or any index that will only
  124. have index children.
  125. If this function is not supplied or if it returns NULL then the first
  126. cache in the parent's list will be chosen, or failing that, the first
  127. cache in the master list.
  128. (4) A function to check the auxiliary data [optional].
  129. This function will be called to check that a match found in the cache for
  130. this object is valid. For instance with AFS it could check the auxiliary
  131. data against the data version number returned by the server to determine
  132. whether the index entry in a cache is still valid.
  133. If this function is absent, it will be assumed that matching objects in a
  134. cache are always valid.
  135. The function is also passed the cache's idea of the object size and may
  136. use this to manage coherency also.
  137. If present, the function should return one of the following values:
  138. (*) FSCACHE_CHECKAUX_OKAY - the entry is okay as is
  139. (*) FSCACHE_CHECKAUX_NEEDS_UPDATE - the entry requires update
  140. (*) FSCACHE_CHECKAUX_OBSOLETE - the entry should be deleted
  141. This function can also be used to extract data from the auxiliary data in
  142. the cache and copy it into the netfs's structures.
  143. (5) A pair of functions to manage contexts for the completion callback
  144. [optional].
  145. The cache read/write functions are passed a context which is then passed
  146. to the I/O completion callback function. To ensure this context remains
  147. valid until after the I/O completion is called, two functions may be
  148. provided: one to get an extra reference on the context, and one to drop a
  149. reference to it.
  150. If the context is not used or is a type of object that won't go out of
  151. scope, then these functions are not required. These functions are not
  152. required for indices as indices may not contain data. These functions may
  153. be called in interrupt context and so may not sleep.
  154. (6) A function to mark a page as retaining cache metadata [optional].
  155. This is called by the cache to indicate that it is retaining in-memory
  156. information for this page and that the netfs should uncache the page when
  157. it has finished. This does not indicate whether there's data on the disk
  158. or not. Note that several pages at once may be presented for marking.
  159. The PG_fscache bit is set on the pages before this function would be
  160. called, so the function need not be provided if this is sufficient.
  161. This function is not required for indices as they're not permitted data.
  162. (7) A function to unmark all the pages retaining cache metadata [mandatory].
  163. This is called by FS-Cache to indicate that a backing store is being
  164. unbound from a cookie and that all the marks on the pages should be
  165. cleared to prevent confusion. Note that the cache will have torn down all
  166. its tracking information so that the pages don't need to be explicitly
  167. uncached.
  168. This function is not required for indices as they're not permitted data.
  169. ===================================
  170. NETWORK FILESYSTEM (UN)REGISTRATION
  171. ===================================
  172. The first step is to declare the network filesystem to the cache. This also
  173. involves specifying the layout of the primary index (for AFS, this would be the
  174. "cell" level).
  175. The registration function is:
  176. int fscache_register_netfs(struct fscache_netfs *netfs);
  177. It just takes a pointer to the netfs definition. It returns 0 or an error as
  178. appropriate.
  179. For kAFS, registration is done as follows:
  180. ret = fscache_register_netfs(&afs_cache_netfs);
  181. The last step is, of course, unregistration:
  182. void fscache_unregister_netfs(struct fscache_netfs *netfs);
  183. ================
  184. CACHE TAG LOOKUP
  185. ================
  186. FS-Cache permits the use of more than one cache. To permit particular index
  187. subtrees to be bound to particular caches, the second step is to look up cache
  188. representation tags. This step is optional; it can be left entirely up to
  189. FS-Cache as to which cache should be used. The problem with doing that is that
  190. FS-Cache will always pick the first cache that was registered.
  191. To get the representation for a named tag:
  192. struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name);
  193. This takes a text string as the name and returns a representation of a tag. It
  194. will never return an error. It may return a dummy tag, however, if it runs out
  195. of memory; this will inhibit caching with this tag.
  196. Any representation so obtained must be released by passing it to this function:
  197. void fscache_release_cache_tag(struct fscache_cache_tag *tag);
  198. The tag will be retrieved by FS-Cache when it calls the object definition
  199. operation select_cache().
  200. ==================
  201. INDEX REGISTRATION
  202. ==================
  203. The third step is to inform FS-Cache about part of an index hierarchy that can
  204. be used to locate files. This is done by requesting a cookie for each index in
  205. the path to the file:
  206. struct fscache_cookie *
  207. fscache_acquire_cookie(struct fscache_cookie *parent,
  208. const struct fscache_object_def *def,
  209. const void *index_key,
  210. size_t index_key_len,
  211. const void *aux_data,
  212. size_t aux_data_len,
  213. void *netfs_data,
  214. loff_t object_size,
  215. bool enable);
  216. This function creates an index entry in the index represented by parent,
  217. filling in the index entry by calling the operations pointed to by def.
  218. A unique key that represents the object within the parent must be pointed to by
  219. index_key and is of length index_key_len.
  220. An optional blob of auxiliary data that is to be stored within the cache can be
  221. pointed to with aux_data and should be of length aux_data_len. This would
  222. typically be used for storing coherency data.
  223. The netfs may pass an arbitrary value in netfs_data and this will be presented
  224. to it in the event of any calling back. This may also be used in tracing or
  225. logging of messages.
  226. The cache tracks the size of the data attached to an object and this set to be
  227. object_size. For indices, this should be 0. This value will be passed to the
  228. ->check_aux() callback.
  229. Note that this function never returns an error - all errors are handled
  230. internally. It may, however, return NULL to indicate no cookie. It is quite
  231. acceptable to pass this token back to this function as the parent to another
  232. acquisition (or even to the relinquish cookie, read page and write page
  233. functions - see below).
  234. Note also that no indices are actually created in a cache until a non-index
  235. object needs to be created somewhere down the hierarchy. Furthermore, an index
  236. may be created in several different caches independently at different times.
  237. This is all handled transparently, and the netfs doesn't see any of it.
  238. A cookie will be created in the disabled state if enabled is false. A cookie
  239. must be enabled to do anything with it. A disabled cookie can be enabled by
  240. calling fscache_enable_cookie() (see below).
  241. For example, with AFS, a cell would be added to the primary index. This index
  242. entry would have a dependent inode containing volume mappings within this cell:
  243. cell->cache =
  244. fscache_acquire_cookie(afs_cache_netfs.primary_index,
  245. &afs_cell_cache_index_def,
  246. cell->name, strlen(cell->name),
  247. NULL, 0,
  248. cell, 0, true);
  249. And then a particular volume could be added to that index by ID, creating
  250. another index for vnodes (AFS inode equivalents):
  251. volume->cache =
  252. fscache_acquire_cookie(volume->cell->cache,
  253. &afs_volume_cache_index_def,
  254. &volume->vid, sizeof(volume->vid),
  255. NULL, 0,
  256. volume, 0, true);
  257. ======================
  258. DATA FILE REGISTRATION
  259. ======================
  260. The fourth step is to request a data file be created in the cache. This is
  261. identical to index cookie acquisition. The only difference is that the type in
  262. the object definition should be something other than index type.
  263. vnode->cache =
  264. fscache_acquire_cookie(volume->cache,
  265. &afs_vnode_cache_object_def,
  266. &key, sizeof(key),
  267. &aux, sizeof(aux),
  268. vnode, vnode->status.size, true);
  269. =================================
  270. MISCELLANEOUS OBJECT REGISTRATION
  271. =================================
  272. An optional step is to request an object of miscellaneous type be created in
  273. the cache. This is almost identical to index cookie acquisition. The only
  274. difference is that the type in the object definition should be something other
  275. than index type. Whilst the parent object could be an index, it's more likely
  276. it would be some other type of object such as a data file.
  277. xattr->cache =
  278. fscache_acquire_cookie(vnode->cache,
  279. &afs_xattr_cache_object_def,
  280. &xattr->name, strlen(xattr->name),
  281. NULL, 0,
  282. xattr, strlen(xattr->val), true);
  283. Miscellaneous objects might be used to store extended attributes or directory
  284. entries for example.
  285. ==========================
  286. SETTING THE DATA FILE SIZE
  287. ==========================
  288. The fifth step is to set the physical attributes of the file, such as its size.
  289. This doesn't automatically reserve any space in the cache, but permits the
  290. cache to adjust its metadata for data tracking appropriately:
  291. int fscache_attr_changed(struct fscache_cookie *cookie);
  292. The cache will return -ENOBUFS if there is no backing cache or if there is no
  293. space to allocate any extra metadata required in the cache.
  294. Note that attempts to read or write data pages in the cache over this size may
  295. be rebuffed with -ENOBUFS.
  296. This operation schedules an attribute adjustment to happen asynchronously at
  297. some point in the future, and as such, it may happen after the function returns
  298. to the caller. The attribute adjustment excludes read and write operations.
  299. =====================
  300. PAGE ALLOC/READ/WRITE
  301. =====================
  302. And the sixth step is to store and retrieve pages in the cache. There are
  303. three functions that are used to do this.
  304. Note:
  305. (1) A page should not be re-read or re-allocated without uncaching it first.
  306. (2) A read or allocated page must be uncached when the netfs page is released
  307. from the pagecache.
  308. (3) A page should only be written to the cache if previous read or allocated.
  309. This permits the cache to maintain its page tracking in proper order.
  310. PAGE READ
  311. ---------
  312. Firstly, the netfs should ask FS-Cache to examine the caches and read the
  313. contents cached for a particular page of a particular file if present, or else
  314. allocate space to store the contents if not:
  315. typedef
  316. void (*fscache_rw_complete_t)(struct page *page,
  317. void *context,
  318. int error);
  319. int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
  320. struct page *page,
  321. fscache_rw_complete_t end_io_func,
  322. void *context,
  323. gfp_t gfp);
  324. The cookie argument must specify a cookie for an object that isn't an index,
  325. the page specified will have the data loaded into it (and is also used to
  326. specify the page number), and the gfp argument is used to control how any
  327. memory allocations made are satisfied.
  328. If the cookie indicates the inode is not cached:
  329. (1) The function will return -ENOBUFS.
  330. Else if there's a copy of the page resident in the cache:
  331. (1) The mark_pages_cached() cookie operation will be called on that page.
  332. (2) The function will submit a request to read the data from the cache's
  333. backing device directly into the page specified.
  334. (3) The function will return 0.
  335. (4) When the read is complete, end_io_func() will be invoked with:
  336. (*) The netfs data supplied when the cookie was created.
  337. (*) The page descriptor.
  338. (*) The context argument passed to the above function. This will be
  339. maintained with the get_context/put_context functions mentioned above.
  340. (*) An argument that's 0 on success or negative for an error code.
  341. If an error occurs, it should be assumed that the page contains no usable
  342. data. fscache_readpages_cancel() may need to be called.
  343. end_io_func() will be called in process context if the read is results in
  344. an error, but it might be called in interrupt context if the read is
  345. successful.
  346. Otherwise, if there's not a copy available in cache, but the cache may be able
  347. to store the page:
  348. (1) The mark_pages_cached() cookie operation will be called on that page.
  349. (2) A block may be reserved in the cache and attached to the object at the
  350. appropriate place.
  351. (3) The function will return -ENODATA.
  352. This function may also return -ENOMEM or -EINTR, in which case it won't have
  353. read any data from the cache.
  354. PAGE ALLOCATE
  355. -------------
  356. Alternatively, if there's not expected to be any data in the cache for a page
  357. because the file has been extended, a block can simply be allocated instead:
  358. int fscache_alloc_page(struct fscache_cookie *cookie,
  359. struct page *page,
  360. gfp_t gfp);
  361. This is similar to the fscache_read_or_alloc_page() function, except that it
  362. never reads from the cache. It will return 0 if a block has been allocated,
  363. rather than -ENODATA as the other would. One or the other must be performed
  364. before writing to the cache.
  365. The mark_pages_cached() cookie operation will be called on the page if
  366. successful.
  367. PAGE WRITE
  368. ----------
  369. Secondly, if the netfs changes the contents of the page (either due to an
  370. initial download or if a user performs a write), then the page should be
  371. written back to the cache:
  372. int fscache_write_page(struct fscache_cookie *cookie,
  373. struct page *page,
  374. loff_t object_size,
  375. gfp_t gfp);
  376. The cookie argument must specify a data file cookie, the page specified should
  377. contain the data to be written (and is also used to specify the page number),
  378. object_size is the revised size of the object and the gfp argument is used to
  379. control how any memory allocations made are satisfied.
  380. The page must have first been read or allocated successfully and must not have
  381. been uncached before writing is performed.
  382. If the cookie indicates the inode is not cached then:
  383. (1) The function will return -ENOBUFS.
  384. Else if space can be allocated in the cache to hold this page:
  385. (1) PG_fscache_write will be set on the page.
  386. (2) The function will submit a request to write the data to cache's backing
  387. device directly from the page specified.
  388. (3) The function will return 0.
  389. (4) When the write is complete PG_fscache_write is cleared on the page and
  390. anyone waiting for that bit will be woken up.
  391. Else if there's no space available in the cache, -ENOBUFS will be returned. It
  392. is also possible for the PG_fscache_write bit to be cleared when no write took
  393. place if unforeseen circumstances arose (such as a disk error).
  394. Writing takes place asynchronously.
  395. MULTIPLE PAGE READ
  396. ------------------
  397. A facility is provided to read several pages at once, as requested by the
  398. readpages() address space operation:
  399. int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
  400. struct address_space *mapping,
  401. struct list_head *pages,
  402. int *nr_pages,
  403. fscache_rw_complete_t end_io_func,
  404. void *context,
  405. gfp_t gfp);
  406. This works in a similar way to fscache_read_or_alloc_page(), except:
  407. (1) Any page it can retrieve data for is removed from pages and nr_pages and
  408. dispatched for reading to the disk. Reads of adjacent pages on disk may
  409. be merged for greater efficiency.
  410. (2) The mark_pages_cached() cookie operation will be called on several pages
  411. at once if they're being read or allocated.
  412. (3) If there was an general error, then that error will be returned.
  413. Else if some pages couldn't be allocated or read, then -ENOBUFS will be
  414. returned.
  415. Else if some pages couldn't be read but were allocated, then -ENODATA will
  416. be returned.
  417. Otherwise, if all pages had reads dispatched, then 0 will be returned, the
  418. list will be empty and *nr_pages will be 0.
  419. (4) end_io_func will be called once for each page being read as the reads
  420. complete. It will be called in process context if error != 0, but it may
  421. be called in interrupt context if there is no error.
  422. Note that a return of -ENODATA, -ENOBUFS or any other error does not preclude
  423. some of the pages being read and some being allocated. Those pages will have
  424. been marked appropriately and will need uncaching.
  425. CANCELLATION OF UNREAD PAGES
  426. ----------------------------
  427. If one or more pages are passed to fscache_read_or_alloc_pages() but not then
  428. read from the cache and also not read from the underlying filesystem then
  429. those pages will need to have any marks and reservations removed. This can be
  430. done by calling:
  431. void fscache_readpages_cancel(struct fscache_cookie *cookie,
  432. struct list_head *pages);
  433. prior to returning to the caller. The cookie argument should be as passed to
  434. fscache_read_or_alloc_pages(). Every page in the pages list will be examined
  435. and any that have PG_fscache set will be uncached.
  436. ==============
  437. PAGE UNCACHING
  438. ==============
  439. To uncache a page, this function should be called:
  440. void fscache_uncache_page(struct fscache_cookie *cookie,
  441. struct page *page);
  442. This function permits the cache to release any in-memory representation it
  443. might be holding for this netfs page. This function must be called once for
  444. each page on which the read or write page functions above have been called to
  445. make sure the cache's in-memory tracking information gets torn down.
  446. Note that pages can't be explicitly deleted from the a data file. The whole
  447. data file must be retired (see the relinquish cookie function below).
  448. Furthermore, note that this does not cancel the asynchronous read or write
  449. operation started by the read/alloc and write functions, so the page
  450. invalidation functions must use:
  451. bool fscache_check_page_write(struct fscache_cookie *cookie,
  452. struct page *page);
  453. to see if a page is being written to the cache, and:
  454. void fscache_wait_on_page_write(struct fscache_cookie *cookie,
  455. struct page *page);
  456. to wait for it to finish if it is.
  457. When releasepage() is being implemented, a special FS-Cache function exists to
  458. manage the heuristics of coping with vmscan trying to eject pages, which may
  459. conflict with the cache trying to write pages to the cache (which may itself
  460. need to allocate memory):
  461. bool fscache_maybe_release_page(struct fscache_cookie *cookie,
  462. struct page *page,
  463. gfp_t gfp);
  464. This takes the netfs cookie, and the page and gfp arguments as supplied to
  465. releasepage(). It will return false if the page cannot be released yet for
  466. some reason and if it returns true, the page has been uncached and can now be
  467. released.
  468. To make a page available for release, this function may wait for an outstanding
  469. storage request to complete, or it may attempt to cancel the storage request -
  470. in which case the page will not be stored in the cache this time.
  471. BULK INODE PAGE UNCACHE
  472. -----------------------
  473. A convenience routine is provided to perform an uncache on all the pages
  474. attached to an inode. This assumes that the pages on the inode correspond on a
  475. 1:1 basis with the pages in the cache.
  476. void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
  477. struct inode *inode);
  478. This takes the netfs cookie that the pages were cached with and the inode that
  479. the pages are attached to. This function will wait for pages to finish being
  480. written to the cache and for the cache to finish with the page generally. No
  481. error is returned.
  482. ===============================
  483. INDEX AND DATA FILE CONSISTENCY
  484. ===============================
  485. To find out whether auxiliary data for an object is up to data within the
  486. cache, the following function can be called:
  487. int fscache_check_consistency(struct fscache_cookie *cookie,
  488. const void *aux_data);
  489. This will call back to the netfs to check whether the auxiliary data associated
  490. with a cookie is correct; if aux_data is non-NULL, it will update the auxiliary
  491. data buffer first. It returns 0 if it is and -ESTALE if it isn't; it may also
  492. return -ENOMEM and -ERESTARTSYS.
  493. To request an update of the index data for an index or other object, the
  494. following function should be called:
  495. void fscache_update_cookie(struct fscache_cookie *cookie,
  496. const void *aux_data);
  497. This function will update the cookie's auxiliary data buffer from aux_data if
  498. that is non-NULL and then schedule this to be stored on disk. The update
  499. method in the parent index definition will be called to transfer the data.
  500. Note that partial updates may happen automatically at other times, such as when
  501. data blocks are added to a data file object.
  502. =================
  503. COOKIE ENABLEMENT
  504. =================
  505. Cookies exist in one of two states: enabled and disabled. If a cookie is
  506. disabled, it ignores all attempts to acquire child cookies; check, update or
  507. invalidate its state; allocate, read or write backing pages - though it is
  508. still possible to uncache pages and relinquish the cookie.
  509. The initial enablement state is set by fscache_acquire_cookie(), but the cookie
  510. can be enabled or disabled later. To disable a cookie, call:
  511. void fscache_disable_cookie(struct fscache_cookie *cookie,
  512. const void *aux_data,
  513. bool invalidate);
  514. If the cookie is not already disabled, this locks the cookie against other
  515. enable and disable ops, marks the cookie as being disabled, discards or
  516. invalidates any backing objects and waits for cessation of activity on any
  517. associated object before unlocking the cookie.
  518. All possible failures are handled internally. The caller should consider
  519. calling fscache_uncache_all_inode_pages() afterwards to make sure all page
  520. markings are cleared up.
  521. Cookies can be enabled or reenabled with:
  522. void fscache_enable_cookie(struct fscache_cookie *cookie,
  523. const void *aux_data,
  524. loff_t object_size,
  525. bool (*can_enable)(void *data),
  526. void *data)
  527. If the cookie is not already enabled, this locks the cookie against other
  528. enable and disable ops, invokes can_enable() and, if the cookie is not an index
  529. cookie, will begin the procedure of acquiring backing objects.
  530. The optional can_enable() function is passed the data argument and returns a
  531. ruling as to whether or not enablement should actually be permitted to begin.
  532. All possible failures are handled internally. The cookie will only be marked
  533. as enabled if provisional backing objects are allocated.
  534. The object's data size is updated from object_size and is passed to the
  535. ->check_aux() function.
  536. In both cases, the cookie's auxiliary data buffer is updated from aux_data if
  537. that is non-NULL inside the enablement lock before proceeding.
  538. ===============================
  539. MISCELLANEOUS COOKIE OPERATIONS
  540. ===============================
  541. There are a number of operations that can be used to control cookies:
  542. (*) Cookie pinning:
  543. int fscache_pin_cookie(struct fscache_cookie *cookie);
  544. void fscache_unpin_cookie(struct fscache_cookie *cookie);
  545. These operations permit data cookies to be pinned into the cache and to
  546. have the pinning removed. They are not permitted on index cookies.
  547. The pinning function will return 0 if successful, -ENOBUFS in the cookie
  548. isn't backed by a cache, -EOPNOTSUPP if the cache doesn't support pinning,
  549. -ENOSPC if there isn't enough space to honour the operation, -ENOMEM or
  550. -EIO if there's any other problem.
  551. (*) Data space reservation:
  552. int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size);
  553. This permits a netfs to request cache space be reserved to store up to the
  554. given amount of a file. It is permitted to ask for more than the current
  555. size of the file to allow for future file expansion.
  556. If size is given as zero then the reservation will be cancelled.
  557. The function will return 0 if successful, -ENOBUFS in the cookie isn't
  558. backed by a cache, -EOPNOTSUPP if the cache doesn't support reservations,
  559. -ENOSPC if there isn't enough space to honour the operation, -ENOMEM or
  560. -EIO if there's any other problem.
  561. Note that this doesn't pin an object in a cache; it can still be culled to
  562. make space if it's not in use.
  563. =====================
  564. COOKIE UNREGISTRATION
  565. =====================
  566. To get rid of a cookie, this function should be called.
  567. void fscache_relinquish_cookie(struct fscache_cookie *cookie,
  568. const void *aux_data,
  569. bool retire);
  570. If retire is non-zero, then the object will be marked for recycling, and all
  571. copies of it will be removed from all active caches in which it is present.
  572. Not only that but all child objects will also be retired.
  573. If retire is zero, then the object may be available again when next the
  574. acquisition function is called. Retirement here will overrule the pinning on a
  575. cookie.
  576. The cookie's auxiliary data will be updated from aux_data if that is non-NULL
  577. so that the cache can lazily update it on disk.
  578. One very important note - relinquish must NOT be called for a cookie unless all
  579. the cookies for "child" indices, objects and pages have been relinquished
  580. first.
  581. ==================
  582. INDEX INVALIDATION
  583. ==================
  584. There is no direct way to invalidate an index subtree. To do this, the caller
  585. should relinquish and retire the cookie they have, and then acquire a new one.
  586. ======================
  587. DATA FILE INVALIDATION
  588. ======================
  589. Sometimes it will be necessary to invalidate an object that contains data.
  590. Typically this will be necessary when the server tells the netfs of a foreign
  591. change - at which point the netfs has to throw away all the state it had for an
  592. inode and reload from the server.
  593. To indicate that a cache object should be invalidated, the following function
  594. can be called:
  595. void fscache_invalidate(struct fscache_cookie *cookie);
  596. This can be called with spinlocks held as it defers the work to a thread pool.
  597. All extant storage, retrieval and attribute change ops at this point are
  598. cancelled and discarded. Some future operations will be rejected until the
  599. cache has had a chance to insert a barrier in the operations queue. After
  600. that, operations will be queued again behind the invalidation operation.
  601. The invalidation operation will perform an attribute change operation and an
  602. auxiliary data update operation as it is very likely these will have changed.
  603. Using the following function, the netfs can wait for the invalidation operation
  604. to have reached a point at which it can start submitting ordinary operations
  605. once again:
  606. void fscache_wait_on_invalidate(struct fscache_cookie *cookie);
  607. ===========================
  608. FS-CACHE SPECIFIC PAGE FLAG
  609. ===========================
  610. FS-Cache makes use of a page flag, PG_private_2, for its own purpose. This is
  611. given the alternative name PG_fscache.
  612. PG_fscache is used to indicate that the page is known by the cache, and that
  613. the cache must be informed if the page is going to go away. It's an indication
  614. to the netfs that the cache has an interest in this page, where an interest may
  615. be a pointer to it, resources allocated or reserved for it, or I/O in progress
  616. upon it.
  617. The netfs can use this information in methods such as releasepage() to
  618. determine whether it needs to uncache a page or update it.
  619. Furthermore, if this bit is set, releasepage() and invalidatepage() operations
  620. will be called on a page to get rid of it, even if PG_private is not set. This
  621. allows caching to attempted on a page before read_cache_pages() to be called
  622. after fscache_read_or_alloc_pages() as the former will try and release pages it
  623. was given under certain circumstances.
  624. This bit does not overlap with such as PG_private. This means that FS-Cache
  625. can be used with a filesystem that uses the block buffering code.
  626. There are a number of operations defined on this flag:
  627. int PageFsCache(struct page *page);
  628. void SetPageFsCache(struct page *page)
  629. void ClearPageFsCache(struct page *page)
  630. int TestSetPageFsCache(struct page *page)
  631. int TestClearPageFsCache(struct page *page)
  632. These functions are bit test, bit set, bit clear, bit test and set and bit
  633. test and clear operations on PG_fscache.