netfs-api.txt 30 KB

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