2020-04-28 00:16:54 +03:00
.. SPDX-License-Identifier: GPL-2.0
==========================
General Filesystem Caching
==========================
Overview
========
This facility is a general purpose cache for network filesystems, though it
could be used for caching other things such as ISO9660 filesystems too.
2021-11-10 16:25:03 +03:00
FS-Cache mediates between cache backends (such as CacheFiles) and network
2020-04-28 00:16:54 +03:00
filesystems::
+---------+
2021-11-10 16:25:03 +03:00
| | +--------------+
| NFS |--+ | |
| | | +-->| CacheFS |
+---------+ | +----------+ | | /dev/hda5 |
| | | | +--------------+
+---------+ +-------------->| | |
| | +-------+ | |--+
| AFS |----->| | | FS-Cache |
| | | netfs |-->| |--+
+---------+ +-->| lib | | | |
| | | | | | +--------------+
+---------+ | +-------+ +----------+ | | |
| | | +-->| CacheFiles |
| 9P |--+ | /var/cache |
| | +--------------+
2020-04-28 00:16:54 +03:00
+---------+
Or to look at it another way, FS-Cache is a module that provides a caching
facility to a network filesystem such that the cache is transparent to the
user::
+---------+
| |
| Server |
| |
+---------+
| NETWORK
~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| +----------+
V | |
+---------+ | |
| | | |
| NFS |----->| FS-Cache |
| | | |--+
+---------+ | | | +--------------+ +--------------+
| | | | | | | |
V +----------+ +-->| CacheFiles |-->| Ext3 |
+---------+ | /var/cache | | /dev/sda6 |
| | +--------------+ +--------------+
| VFS | ^ ^
| | | |
+---------+ +--------------+ |
| KERNEL SPACE | |
~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|~~~~~~|~~~~
| USER SPACE | |
V | |
+---------+ +--------------+
| | | |
| Process | | cachefilesd |
| | | |
+---------+ +--------------+
FS-Cache does not follow the idea of completely loading every netfs file
opened in its entirety into a cache before permitting it to be accessed and
then serving the pages out of that cache rather than the netfs inode because:
(1) It must be practical to operate without a cache.
(2) The size of any accessible file must not be limited to the size of the
cache.
(3) The combined size of all opened files (this includes mapped libraries)
must not be limited to the size of the cache.
(4) The user should not be forced to download an entire file just to do a
one-off access of a small portion of it (such as might be done with the
"file" program).
2021-11-10 16:25:03 +03:00
It instead serves the cache out in chunks as and when requested by the netfs
using it.
2020-04-28 00:16:54 +03:00
FS-Cache provides the following facilities:
2021-11-10 16:25:03 +03:00
* More than one cache can be used at once. Caches can be selected
2020-04-28 00:16:54 +03:00
explicitly by use of tags.
2021-11-10 16:25:03 +03:00
* Caches can be added / removed at any time, even whilst being accessed.
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
* The netfs is provided with an interface that allows either party to
2020-04-28 00:16:54 +03:00
withdraw caching facilities from a file (required for (2)).
2021-11-10 16:25:03 +03:00
* The interface to the netfs returns as few errors as possible, preferring
2020-04-28 00:16:54 +03:00
rather to let the netfs remain oblivious.
2021-11-10 16:25:03 +03:00
* There are three types of cookie: cache, volume and data file cookies.
Cache cookies represent the cache as a whole and are not normally visible
to the netfs; the netfs gets a volume cookie to represent a collection of
files (typically something that a netfs would get for a superblock); and
data file cookies are used to cache data (something that would be got for
an inode).
* Volumes are matched using a key. This is a printable string that is used
to encode all the information that might be needed to distinguish one
superblock, say, from another. This would be a compound of things like
cell name or server address, volume name or share path. It must be a
valid pathname.
* Cookies are matched using a key. This is a binary blob and is used to
represent the object within a volume (so the volume key need not form
part of the blob). This might include things like an inode number and
uniquifier or a file handle.
* Cookie resources are set up and pinned by marking the cookie in-use.
This prevents the backing resources from being culled. Timed garbage
collection is employed to eliminate cookies that haven't been used for a
short while, thereby reducing resource overload. This is intended to be
used when a file is opened or closed.
A cookie can be marked in-use multiple times simultaneously; each mark
must be unused.
* Begin/end access functions are provided to delay cache withdrawal for the
duration of an operation and prevent structs from being freed whilst
we're looking at them.
* Data I/O is done by asynchronous DIO to/from a buffer described by the
netfs using an iov_iter.
* An invalidation facility is available to discard data from the cache and
to deal with I/O that's in progress that is accessing old data.
* Cookies can be "retired" upon release, thereby causing the object to be
removed from the cache.
2020-04-28 00:16:54 +03:00
The netfs API to FS-Cache can be found in:
2020-04-28 00:16:55 +03:00
Documentation/filesystems/caching/netfs-api.rst
2020-04-28 00:16:54 +03:00
The cache backend API to FS-Cache can be found in:
2020-04-28 00:16:58 +03:00
Documentation/filesystems/caching/backend-api.rst
2020-04-28 00:16:54 +03:00
Statistical Information
=======================
If FS-Cache is compiled with the following options enabled::
CONFIG_FSCACHE_STATS=y
2021-11-10 16:25:03 +03:00
then it will gather certain statistics and display them through:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
/proc/fs/fscache/stats
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
This shows counts of a number of events that can happen in FS-Cache:
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
|CLASS |EVENT |MEANING |
+==============+=======+=======================================================+
2021-11-10 16:25:03 +03:00
|Cookies |n=N |Number of data storage cookies allocated |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |v=N |Number of volume index cookies allocated |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |vcol=N |Number of volume index key collisions |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |voom=N |Number of OOM events when allocating volume cookies |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
|Acquire |n=N |Number of acquire cookie requests seen |
+ +-------+-------------------------------------------------------+
| |ok=N |Number of acq reqs succeeded |
+ +-------+-------------------------------------------------------+
| |oom=N |Number of acq reqs failed on ENOMEM |
+--------------+-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
|LRU |n=N |Number of cookies currently on the LRU |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |exp=N |Number of cookies expired off of the LRU |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |rmv=N |Number of cookies removed from the LRU |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |drp=N |Number of LRU'd cookies relinquished/withdrawn |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |at=N |Time till next LRU cull (jiffies) |
+--------------+-------+-------------------------------------------------------+
|Invals |n=N |Number of invalidations |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
|Updates |n=N |Number of update cookie requests seen |
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |rsz=N |Number of resize requests |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |rsn=N |Number of skipped resize requests |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
|Relinqs |n=N |Number of relinquish cookie requests seen |
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |rtr=N |Number of rlq reqs with retire=true |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |drop=N |Number of cookies no longer blocking re-acquisition |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
|NoSpace |nwr=N |Number of write requests refused due to lack of space |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |ncr=N |Number of create requests refused due to lack of space |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |cull=N |Number of objects culled to make space |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
|IO |rd=N |Number of read operations in the cache |
2020-04-28 00:16:54 +03:00
+ +-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
| |wr=N |Number of write operations in the cache |
2020-04-28 00:16:54 +03:00
+--------------+-------+-------------------------------------------------------+
2021-11-10 16:25:03 +03:00
Netfslib will also add some stats counters of its own.
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
Cache List
==========
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
FS-Cache provides a list of cache cookies:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
/proc/fs/fscache/cookies
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
This will look something like::
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
# cat /proc/fs/fscache/caches
CACHE REF VOLS OBJS ACCES S NAME
======== ===== ===== ===== ===== = ===============
00000001 2 1 2123 1 A default
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
where the columns are:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
======= ===============================================================
COLUMN DESCRIPTION
======= ===============================================================
CACHE Cache cookie debug ID (also appears in traces)
REF Number of references on the cache cookie
VOLS Number of volumes cookies in this cache
OBJS Number of cache objects in use
ACCES Number of accesses pinning the cache
S State
NAME Name of the cache.
======= ===============================================================
The state can be (-) Inactive, (P)reparing, (A)ctive, (E)rror or (W)ithdrawing.
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
Volume List
2020-04-28 00:16:54 +03:00
===========
2021-11-10 16:25:03 +03:00
FS-Cache provides a list of volume cookies:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
/proc/fs/fscache/volumes
2020-04-28 00:16:54 +03:00
This will look something like::
2021-11-10 16:25:03 +03:00
VOLUME REF nCOOK ACC FL CACHE KEY
======== ===== ===== === == =============== ================
00000001 55 54 1 00 default afs,example.com,100058
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
where the columns are:
2020-04-28 00:16:54 +03:00
======= ===============================================================
COLUMN DESCRIPTION
======= ===============================================================
2021-11-10 16:25:03 +03:00
VOLUME The volume cookie debug ID (also appears in traces)
REF Number of references on the volume cookie
nCOOK Number of cookies in the volume
ACC Number of accesses pinning the cache
FL Flags on the volume cookie
CACHE Name of the cache or "-"
KEY The indexing key for the volume
2020-04-28 00:16:54 +03:00
======= ===============================================================
2021-11-10 16:25:03 +03:00
Cookie List
===========
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
FS-Cache provides a list of cookies:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
/proc/fs/fscache/cookies
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
This will look something like::
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
# head /proc/fs/fscache/cookies
COOKIE VOLUME REF ACT ACC S FL DEF
======== ======== === === === = == ================
00000435 00000001 1 0 -1 - 08 0000000201d080070000000000000000, 0000000000000000
00000436 00000001 1 0 -1 - 00 0000005601d080080000000000000000, 0000000000000051
00000437 00000001 1 0 -1 - 08 00023b3001d0823f0000000000000000, 0000000000000000
00000438 00000001 1 0 -1 - 08 0000005801d0807b0000000000000000, 0000000000000000
00000439 00000001 1 0 -1 - 08 00023b3201d080a10000000000000000, 0000000000000000
0000043a 00000001 1 0 -1 - 08 00023b3401d080a30000000000000000, 0000000000000000
0000043b 00000001 1 0 -1 - 08 00023b3601d080b30000000000000000, 0000000000000000
0000043c 00000001 1 0 -1 - 08 00023b3801d080b40000000000000000, 0000000000000000
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
where the columns are:
2020-04-28 00:16:54 +03:00
2021-11-10 16:25:03 +03:00
======= ===============================================================
COLUMN DESCRIPTION
======= ===============================================================
COOKIE The cookie debug ID (also appears in traces)
VOLUME The parent volume cookie debug ID
REF Number of references on the volume cookie
ACT Number of times the cookie is marked for in use
ACC Number of access pins in the cookie
S State of the cookie
FL Flags on the cookie
DEF Key, auxiliary data
======= ===============================================================
2020-04-28 00:16:54 +03:00
Debugging
=========
If CONFIG_FSCACHE_DEBUG is enabled, the FS-Cache facility can have runtime
debugging enabled by adjusting the value in::
/sys/module/fscache/parameters/debug
This is a bitmask of debugging streams to enable:
======= ======= =============================== =======================
BIT VALUE STREAM POINT
======= ======= =============================== =======================
0 1 Cache management Function entry trace
1 2 Function exit trace
2 4 General
3 8 Cookie management Function entry trace
4 16 Function exit trace
5 32 General
2021-11-10 16:25:03 +03:00
6-8 (Not used)
9 512 I/O operation management Function entry trace
2020-04-28 00:16:54 +03:00
10 1024 Function exit trace
11 2048 General
======= ======= =============================== =======================
The appropriate set of values should be OR'd together and the result written to
the control file. For example::
2021-11-10 16:25:03 +03:00
echo $((1|8|512)) >/sys/module/fscache/parameters/debug
2020-04-28 00:16:54 +03:00
will turn on all function entry debugging.