netfs-api.txt 33 KB

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