fscache.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==========================
  3. General Filesystem Caching
  4. ==========================
  5. Overview
  6. ========
  7. This facility is a general purpose cache for network filesystems, though it
  8. could be used for caching other things such as ISO9660 filesystems too.
  9. FS-Cache mediates between cache backends (such as CacheFiles) and network
  10. filesystems::
  11. +---------+
  12. | | +--------------+
  13. | NFS |--+ | |
  14. | | | +-->| CacheFS |
  15. +---------+ | +----------+ | | /dev/hda5 |
  16. | | | | +--------------+
  17. +---------+ +-------------->| | |
  18. | | +-------+ | |--+
  19. | AFS |----->| | | FS-Cache |
  20. | | | netfs |-->| |--+
  21. +---------+ +-->| lib | | | |
  22. | | | | | | +--------------+
  23. +---------+ | +-------+ +----------+ | | |
  24. | | | +-->| CacheFiles |
  25. | 9P |--+ | /var/cache |
  26. | | +--------------+
  27. +---------+
  28. Or to look at it another way, FS-Cache is a module that provides a caching
  29. facility to a network filesystem such that the cache is transparent to the
  30. user::
  31. +---------+
  32. | |
  33. | Server |
  34. | |
  35. +---------+
  36. | NETWORK
  37. ~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. |
  39. | +----------+
  40. V | |
  41. +---------+ | |
  42. | | | |
  43. | NFS |----->| FS-Cache |
  44. | | | |--+
  45. +---------+ | | | +--------------+ +--------------+
  46. | | | | | | | |
  47. V +----------+ +-->| CacheFiles |-->| Ext3 |
  48. +---------+ | /var/cache | | /dev/sda6 |
  49. | | +--------------+ +--------------+
  50. | VFS | ^ ^
  51. | | | |
  52. +---------+ +--------------+ |
  53. | KERNEL SPACE | |
  54. ~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|~~~~~~|~~~~
  55. | USER SPACE | |
  56. V | |
  57. +---------+ +--------------+
  58. | | | |
  59. | Process | | cachefilesd |
  60. | | | |
  61. +---------+ +--------------+
  62. FS-Cache does not follow the idea of completely loading every netfs file
  63. opened in its entirety into a cache before permitting it to be accessed and
  64. then serving the pages out of that cache rather than the netfs inode because:
  65. (1) It must be practical to operate without a cache.
  66. (2) The size of any accessible file must not be limited to the size of the
  67. cache.
  68. (3) The combined size of all opened files (this includes mapped libraries)
  69. must not be limited to the size of the cache.
  70. (4) The user should not be forced to download an entire file just to do a
  71. one-off access of a small portion of it (such as might be done with the
  72. "file" program).
  73. It instead serves the cache out in chunks as and when requested by the netfs
  74. using it.
  75. FS-Cache provides the following facilities:
  76. * More than one cache can be used at once. Caches can be selected
  77. explicitly by use of tags.
  78. * Caches can be added / removed at any time, even whilst being accessed.
  79. * The netfs is provided with an interface that allows either party to
  80. withdraw caching facilities from a file (required for (2)).
  81. * The interface to the netfs returns as few errors as possible, preferring
  82. rather to let the netfs remain oblivious.
  83. * There are three types of cookie: cache, volume and data file cookies.
  84. Cache cookies represent the cache as a whole and are not normally visible
  85. to the netfs; the netfs gets a volume cookie to represent a collection of
  86. files (typically something that a netfs would get for a superblock); and
  87. data file cookies are used to cache data (something that would be got for
  88. an inode).
  89. * Volumes are matched using a key. This is a printable string that is used
  90. to encode all the information that might be needed to distinguish one
  91. superblock, say, from another. This would be a compound of things like
  92. cell name or server address, volume name or share path. It must be a
  93. valid pathname.
  94. * Cookies are matched using a key. This is a binary blob and is used to
  95. represent the object within a volume (so the volume key need not form
  96. part of the blob). This might include things like an inode number and
  97. uniquifier or a file handle.
  98. * Cookie resources are set up and pinned by marking the cookie in-use.
  99. This prevents the backing resources from being culled. Timed garbage
  100. collection is employed to eliminate cookies that haven't been used for a
  101. short while, thereby reducing resource overload. This is intended to be
  102. used when a file is opened or closed.
  103. A cookie can be marked in-use multiple times simultaneously; each mark
  104. must be unused.
  105. * Begin/end access functions are provided to delay cache withdrawal for the
  106. duration of an operation and prevent structs from being freed whilst
  107. we're looking at them.
  108. * Data I/O is done by asynchronous DIO to/from a buffer described by the
  109. netfs using an iov_iter.
  110. * An invalidation facility is available to discard data from the cache and
  111. to deal with I/O that's in progress that is accessing old data.
  112. * Cookies can be "retired" upon release, thereby causing the object to be
  113. removed from the cache.
  114. The netfs API to FS-Cache can be found in:
  115. Documentation/filesystems/caching/netfs-api.rst
  116. The cache backend API to FS-Cache can be found in:
  117. Documentation/filesystems/caching/backend-api.rst
  118. Statistical Information
  119. =======================
  120. If FS-Cache is compiled with the following options enabled::
  121. CONFIG_FSCACHE_STATS=y
  122. then it will gather certain statistics and display them through:
  123. /proc/fs/fscache/stats
  124. This shows counts of a number of events that can happen in FS-Cache:
  125. +--------------+-------+-------------------------------------------------------+
  126. |CLASS |EVENT |MEANING |
  127. +==============+=======+=======================================================+
  128. |Cookies |n=N |Number of data storage cookies allocated |
  129. + +-------+-------------------------------------------------------+
  130. | |v=N |Number of volume index cookies allocated |
  131. + +-------+-------------------------------------------------------+
  132. | |vcol=N |Number of volume index key collisions |
  133. + +-------+-------------------------------------------------------+
  134. | |voom=N |Number of OOM events when allocating volume cookies |
  135. +--------------+-------+-------------------------------------------------------+
  136. |Acquire |n=N |Number of acquire cookie requests seen |
  137. + +-------+-------------------------------------------------------+
  138. | |ok=N |Number of acq reqs succeeded |
  139. + +-------+-------------------------------------------------------+
  140. | |oom=N |Number of acq reqs failed on ENOMEM |
  141. +--------------+-------+-------------------------------------------------------+
  142. |LRU |n=N |Number of cookies currently on the LRU |
  143. + +-------+-------------------------------------------------------+
  144. | |exp=N |Number of cookies expired off of the LRU |
  145. + +-------+-------------------------------------------------------+
  146. | |rmv=N |Number of cookies removed from the LRU |
  147. + +-------+-------------------------------------------------------+
  148. | |drp=N |Number of LRU'd cookies relinquished/withdrawn |
  149. + +-------+-------------------------------------------------------+
  150. | |at=N |Time till next LRU cull (jiffies) |
  151. +--------------+-------+-------------------------------------------------------+
  152. |Invals |n=N |Number of invalidations |
  153. +--------------+-------+-------------------------------------------------------+
  154. |Updates |n=N |Number of update cookie requests seen |
  155. + +-------+-------------------------------------------------------+
  156. | |rsz=N |Number of resize requests |
  157. + +-------+-------------------------------------------------------+
  158. | |rsn=N |Number of skipped resize requests |
  159. +--------------+-------+-------------------------------------------------------+
  160. |Relinqs |n=N |Number of relinquish cookie requests seen |
  161. + +-------+-------------------------------------------------------+
  162. | |rtr=N |Number of rlq reqs with retire=true |
  163. + +-------+-------------------------------------------------------+
  164. | |drop=N |Number of cookies no longer blocking re-acquisition |
  165. +--------------+-------+-------------------------------------------------------+
  166. |NoSpace |nwr=N |Number of write requests refused due to lack of space |
  167. + +-------+-------------------------------------------------------+
  168. | |ncr=N |Number of create requests refused due to lack of space |
  169. + +-------+-------------------------------------------------------+
  170. | |cull=N |Number of objects culled to make space |
  171. +--------------+-------+-------------------------------------------------------+
  172. |IO |rd=N |Number of read operations in the cache |
  173. + +-------+-------------------------------------------------------+
  174. | |wr=N |Number of write operations in the cache |
  175. +--------------+-------+-------------------------------------------------------+
  176. Netfslib will also add some stats counters of its own.
  177. Cache List
  178. ==========
  179. FS-Cache provides a list of cache cookies:
  180. /proc/fs/fscache/cookies
  181. This will look something like::
  182. # cat /proc/fs/fscache/caches
  183. CACHE REF VOLS OBJS ACCES S NAME
  184. ======== ===== ===== ===== ===== = ===============
  185. 00000001 2 1 2123 1 A default
  186. where the columns are:
  187. ======= ===============================================================
  188. COLUMN DESCRIPTION
  189. ======= ===============================================================
  190. CACHE Cache cookie debug ID (also appears in traces)
  191. REF Number of references on the cache cookie
  192. VOLS Number of volumes cookies in this cache
  193. OBJS Number of cache objects in use
  194. ACCES Number of accesses pinning the cache
  195. S State
  196. NAME Name of the cache.
  197. ======= ===============================================================
  198. The state can be (-) Inactive, (P)reparing, (A)ctive, (E)rror or (W)ithdrawing.
  199. Volume List
  200. ===========
  201. FS-Cache provides a list of volume cookies:
  202. /proc/fs/fscache/volumes
  203. This will look something like::
  204. VOLUME REF nCOOK ACC FL CACHE KEY
  205. ======== ===== ===== === == =============== ================
  206. 00000001 55 54 1 00 default afs,example.com,100058
  207. where the columns are:
  208. ======= ===============================================================
  209. COLUMN DESCRIPTION
  210. ======= ===============================================================
  211. VOLUME The volume cookie debug ID (also appears in traces)
  212. REF Number of references on the volume cookie
  213. nCOOK Number of cookies in the volume
  214. ACC Number of accesses pinning the cache
  215. FL Flags on the volume cookie
  216. CACHE Name of the cache or "-"
  217. KEY The indexing key for the volume
  218. ======= ===============================================================
  219. Cookie List
  220. ===========
  221. FS-Cache provides a list of cookies:
  222. /proc/fs/fscache/cookies
  223. This will look something like::
  224. # head /proc/fs/fscache/cookies
  225. COOKIE VOLUME REF ACT ACC S FL DEF
  226. ======== ======== === === === = == ================
  227. 00000435 00000001 1 0 -1 - 08 0000000201d080070000000000000000, 0000000000000000
  228. 00000436 00000001 1 0 -1 - 00 0000005601d080080000000000000000, 0000000000000051
  229. 00000437 00000001 1 0 -1 - 08 00023b3001d0823f0000000000000000, 0000000000000000
  230. 00000438 00000001 1 0 -1 - 08 0000005801d0807b0000000000000000, 0000000000000000
  231. 00000439 00000001 1 0 -1 - 08 00023b3201d080a10000000000000000, 0000000000000000
  232. 0000043a 00000001 1 0 -1 - 08 00023b3401d080a30000000000000000, 0000000000000000
  233. 0000043b 00000001 1 0 -1 - 08 00023b3601d080b30000000000000000, 0000000000000000
  234. 0000043c 00000001 1 0 -1 - 08 00023b3801d080b40000000000000000, 0000000000000000
  235. where the columns are:
  236. ======= ===============================================================
  237. COLUMN DESCRIPTION
  238. ======= ===============================================================
  239. COOKIE The cookie debug ID (also appears in traces)
  240. VOLUME The parent volume cookie debug ID
  241. REF Number of references on the volume cookie
  242. ACT Number of times the cookie is marked for in use
  243. ACC Number of access pins in the cookie
  244. S State of the cookie
  245. FL Flags on the cookie
  246. DEF Key, auxiliary data
  247. ======= ===============================================================
  248. Debugging
  249. =========
  250. If CONFIG_NETFS_DEBUG is enabled, the FS-Cache facility and NETFS support can
  251. have runtime debugging enabled by adjusting the value in::
  252. /sys/module/netfs/parameters/debug
  253. This is a bitmask of debugging streams to enable:
  254. ======= ======= =============================== =======================
  255. BIT VALUE STREAM POINT
  256. ======= ======= =============================== =======================
  257. 0 1 Cache management Function entry trace
  258. 1 2 Function exit trace
  259. 2 4 General
  260. 3 8 Cookie management Function entry trace
  261. 4 16 Function exit trace
  262. 5 32 General
  263. 6-8 (Not used)
  264. 9 512 I/O operation management Function entry trace
  265. 10 1024 Function exit trace
  266. 11 2048 General
  267. ======= ======= =============================== =======================
  268. The appropriate set of values should be OR'd together and the result written to
  269. the control file. For example::
  270. echo $((1|8|512)) >/sys/module/netfs/parameters/debug
  271. will turn on all function entry debugging.