Merge tag 'libnvdimm-for-5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdim...
[linux-2.6-microblaze.git] / Documentation / filesystems / caching / cachefiles.rst
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ===============================================
4 CacheFiles: CACHE ON ALREADY MOUNTED FILESYSTEM
5 ===============================================
6
7 .. Contents:
8
9  (*) Overview.
10
11  (*) Requirements.
12
13  (*) Configuration.
14
15  (*) Starting the cache.
16
17  (*) Things to avoid.
18
19  (*) Cache culling.
20
21  (*) Cache structure.
22
23  (*) Security model and SELinux.
24
25  (*) A note on security.
26
27  (*) Statistical information.
28
29  (*) Debugging.
30
31
32
33 Overview
34 ========
35
36 CacheFiles is a caching backend that's meant to use as a cache a directory on
37 an already mounted filesystem of a local type (such as Ext3).
38
39 CacheFiles uses a userspace daemon to do some of the cache management - such as
40 reaping stale nodes and culling.  This is called cachefilesd and lives in
41 /sbin.
42
43 The filesystem and data integrity of the cache are only as good as those of the
44 filesystem providing the backing services.  Note that CacheFiles does not
45 attempt to journal anything since the journalling interfaces of the various
46 filesystems are very specific in nature.
47
48 CacheFiles creates a misc character device - "/dev/cachefiles" - that is used
49 to communication with the daemon.  Only one thing may have this open at once,
50 and while it is open, a cache is at least partially in existence.  The daemon
51 opens this and sends commands down it to control the cache.
52
53 CacheFiles is currently limited to a single cache.
54
55 CacheFiles attempts to maintain at least a certain percentage of free space on
56 the filesystem, shrinking the cache by culling the objects it contains to make
57 space if necessary - see the "Cache Culling" section.  This means it can be
58 placed on the same medium as a live set of data, and will expand to make use of
59 spare space and automatically contract when the set of data requires more
60 space.
61
62
63
64 Requirements
65 ============
66
67 The use of CacheFiles and its daemon requires the following features to be
68 available in the system and in the cache filesystem:
69
70         - dnotify.
71
72         - extended attributes (xattrs).
73
74         - openat() and friends.
75
76         - bmap() support on files in the filesystem (FIBMAP ioctl).
77
78         - The use of bmap() to detect a partial page at the end of the file.
79
80 It is strongly recommended that the "dir_index" option is enabled on Ext3
81 filesystems being used as a cache.
82
83
84 Configuration
85 =============
86
87 The cache is configured by a script in /etc/cachefilesd.conf.  These commands
88 set up cache ready for use.  The following script commands are available:
89
90  brun <N>%, bcull <N>%, bstop <N>%, frun <N>%, fcull <N>%, fstop <N>%
91         Configure the culling limits.  Optional.  See the section on culling
92         The defaults are 7% (run), 5% (cull) and 1% (stop) respectively.
93
94         The commands beginning with a 'b' are file space (block) limits, those
95         beginning with an 'f' are file count limits.
96
97  dir <path>
98         Specify the directory containing the root of the cache.  Mandatory.
99
100  tag <name>
101         Specify a tag to FS-Cache to use in distinguishing multiple caches.
102         Optional.  The default is "CacheFiles".
103
104  debug <mask>
105         Specify a numeric bitmask to control debugging in the kernel module.
106         Optional.  The default is zero (all off).  The following values can be
107         OR'd into the mask to collect various information:
108
109                 ==      =================================================
110                 1       Turn on trace of function entry (_enter() macros)
111                 2       Turn on trace of function exit (_leave() macros)
112                 4       Turn on trace of internal debug points (_debug())
113                 ==      =================================================
114
115         This mask can also be set through sysfs, eg::
116
117                 echo 5 >/sys/modules/cachefiles/parameters/debug
118
119
120 Starting the Cache
121 ==================
122
123 The cache is started by running the daemon.  The daemon opens the cache device,
124 configures the cache and tells it to begin caching.  At that point the cache
125 binds to fscache and the cache becomes live.
126
127 The daemon is run as follows::
128
129         /sbin/cachefilesd [-d]* [-s] [-n] [-f <configfile>]
130
131 The flags are:
132
133  ``-d``
134         Increase the debugging level.  This can be specified multiple times and
135         is cumulative with itself.
136
137  ``-s``
138         Send messages to stderr instead of syslog.
139
140  ``-n``
141         Don't daemonise and go into background.
142
143  ``-f <configfile>``
144         Use an alternative configuration file rather than the default one.
145
146
147 Things to Avoid
148 ===============
149
150 Do not mount other things within the cache as this will cause problems.  The
151 kernel module contains its own very cut-down path walking facility that ignores
152 mountpoints, but the daemon can't avoid them.
153
154 Do not create, rename or unlink files and directories in the cache while the
155 cache is active, as this may cause the state to become uncertain.
156
157 Renaming files in the cache might make objects appear to be other objects (the
158 filename is part of the lookup key).
159
160 Do not change or remove the extended attributes attached to cache files by the
161 cache as this will cause the cache state management to get confused.
162
163 Do not create files or directories in the cache, lest the cache get confused or
164 serve incorrect data.
165
166 Do not chmod files in the cache.  The module creates things with minimal
167 permissions to prevent random users being able to access them directly.
168
169
170 Cache Culling
171 =============
172
173 The cache may need culling occasionally to make space.  This involves
174 discarding objects from the cache that have been used less recently than
175 anything else.  Culling is based on the access time of data objects.  Empty
176 directories are culled if not in use.
177
178 Cache culling is done on the basis of the percentage of blocks and the
179 percentage of files available in the underlying filesystem.  There are six
180 "limits":
181
182  brun, frun
183      If the amount of free space and the number of available files in the cache
184      rises above both these limits, then culling is turned off.
185
186  bcull, fcull
187      If the amount of available space or the number of available files in the
188      cache falls below either of these limits, then culling is started.
189
190  bstop, fstop
191      If the amount of available space or the number of available files in the
192      cache falls below either of these limits, then no further allocation of
193      disk space or files is permitted until culling has raised things above
194      these limits again.
195
196 These must be configured thusly::
197
198         0 <= bstop < bcull < brun < 100
199         0 <= fstop < fcull < frun < 100
200
201 Note that these are percentages of available space and available files, and do
202 _not_ appear as 100 minus the percentage displayed by the "df" program.
203
204 The userspace daemon scans the cache to build up a table of cullable objects.
205 These are then culled in least recently used order.  A new scan of the cache is
206 started as soon as space is made in the table.  Objects will be skipped if
207 their atimes have changed or if the kernel module says it is still using them.
208
209
210 Cache Structure
211 ===============
212
213 The CacheFiles module will create two directories in the directory it was
214 given:
215
216  * cache/
217  * graveyard/
218
219 The active cache objects all reside in the first directory.  The CacheFiles
220 kernel module moves any retired or culled objects that it can't simply unlink
221 to the graveyard from which the daemon will actually delete them.
222
223 The daemon uses dnotify to monitor the graveyard directory, and will delete
224 anything that appears therein.
225
226
227 The module represents index objects as directories with the filename "I..." or
228 "J...".  Note that the "cache/" directory is itself a special index.
229
230 Data objects are represented as files if they have no children, or directories
231 if they do.  Their filenames all begin "D..." or "E...".  If represented as a
232 directory, data objects will have a file in the directory called "data" that
233 actually holds the data.
234
235 Special objects are similar to data objects, except their filenames begin
236 "S..." or "T...".
237
238
239 If an object has children, then it will be represented as a directory.
240 Immediately in the representative directory are a collection of directories
241 named for hash values of the child object keys with an '@' prepended.  Into
242 this directory, if possible, will be placed the representations of the child
243 objects::
244
245          /INDEX    /INDEX     /INDEX                            /DATA FILES
246         /=========/==========/=================================/================
247         cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400
248         cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...DB1ry
249         cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...N22ry
250         cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...FP1ry
251
252
253 If the key is so long that it exceeds NAME_MAX with the decorations added on to
254 it, then it will be cut into pieces, the first few of which will be used to
255 make a nest of directories, and the last one of which will be the objects
256 inside the last directory.  The names of the intermediate directories will have
257 '+' prepended::
258
259         J1223/@23/+xy...z/+kl...m/Epqr
260
261
262 Note that keys are raw data, and not only may they exceed NAME_MAX in size,
263 they may also contain things like '/' and NUL characters, and so they may not
264 be suitable for turning directly into a filename.
265
266 To handle this, CacheFiles will use a suitably printable filename directly and
267 "base-64" encode ones that aren't directly suitable.  The two versions of
268 object filenames indicate the encoding:
269
270         =============== =============== ===============
271         OBJECT TYPE     PRINTABLE       ENCODED
272         =============== =============== ===============
273         Index           "I..."          "J..."
274         Data            "D..."          "E..."
275         Special         "S..."          "T..."
276         =============== =============== ===============
277
278 Intermediate directories are always "@" or "+" as appropriate.
279
280
281 Each object in the cache has an extended attribute label that holds the object
282 type ID (required to distinguish special objects) and the auxiliary data from
283 the netfs.  The latter is used to detect stale objects in the cache and update
284 or retire them.
285
286
287 Note that CacheFiles will erase from the cache any file it doesn't recognise or
288 any file of an incorrect type (such as a FIFO file or a device file).
289
290
291 Security Model and SELinux
292 ==========================
293
294 CacheFiles is implemented to deal properly with the LSM security features of
295 the Linux kernel and the SELinux facility.
296
297 One of the problems that CacheFiles faces is that it is generally acting on
298 behalf of a process, and running in that process's context, and that includes a
299 security context that is not appropriate for accessing the cache - either
300 because the files in the cache are inaccessible to that process, or because if
301 the process creates a file in the cache, that file may be inaccessible to other
302 processes.
303
304 The way CacheFiles works is to temporarily change the security context (fsuid,
305 fsgid and actor security label) that the process acts as - without changing the
306 security context of the process when it the target of an operation performed by
307 some other process (so signalling and suchlike still work correctly).
308
309
310 When the CacheFiles module is asked to bind to its cache, it:
311
312  (1) Finds the security label attached to the root cache directory and uses
313      that as the security label with which it will create files.  By default,
314      this is::
315
316         cachefiles_var_t
317
318  (2) Finds the security label of the process which issued the bind request
319      (presumed to be the cachefilesd daemon), which by default will be::
320
321         cachefilesd_t
322
323      and asks LSM to supply a security ID as which it should act given the
324      daemon's label.  By default, this will be::
325
326         cachefiles_kernel_t
327
328      SELinux transitions the daemon's security ID to the module's security ID
329      based on a rule of this form in the policy::
330
331         type_transition <daemon's-ID> kernel_t : process <module's-ID>;
332
333      For instance::
334
335         type_transition cachefilesd_t kernel_t : process cachefiles_kernel_t;
336
337
338 The module's security ID gives it permission to create, move and remove files
339 and directories in the cache, to find and access directories and files in the
340 cache, to set and access extended attributes on cache objects, and to read and
341 write files in the cache.
342
343 The daemon's security ID gives it only a very restricted set of permissions: it
344 may scan directories, stat files and erase files and directories.  It may
345 not read or write files in the cache, and so it is precluded from accessing the
346 data cached therein; nor is it permitted to create new files in the cache.
347
348
349 There are policy source files available in:
350
351         https://people.redhat.com/~dhowells/fscache/cachefilesd-0.8.tar.bz2
352
353 and later versions.  In that tarball, see the files::
354
355         cachefilesd.te
356         cachefilesd.fc
357         cachefilesd.if
358
359 They are built and installed directly by the RPM.
360
361 If a non-RPM based system is being used, then copy the above files to their own
362 directory and run::
363
364         make -f /usr/share/selinux/devel/Makefile
365         semodule -i cachefilesd.pp
366
367 You will need checkpolicy and selinux-policy-devel installed prior to the
368 build.
369
370
371 By default, the cache is located in /var/fscache, but if it is desirable that
372 it should be elsewhere, than either the above policy files must be altered, or
373 an auxiliary policy must be installed to label the alternate location of the
374 cache.
375
376 For instructions on how to add an auxiliary policy to enable the cache to be
377 located elsewhere when SELinux is in enforcing mode, please see::
378
379         /usr/share/doc/cachefilesd-*/move-cache.txt
380
381 When the cachefilesd rpm is installed; alternatively, the document can be found
382 in the sources.
383
384
385 A Note on Security
386 ==================
387
388 CacheFiles makes use of the split security in the task_struct.  It allocates
389 its own task_security structure, and redirects current->cred to point to it
390 when it acts on behalf of another process, in that process's context.
391
392 The reason it does this is that it calls vfs_mkdir() and suchlike rather than
393 bypassing security and calling inode ops directly.  Therefore the VFS and LSM
394 may deny the CacheFiles access to the cache data because under some
395 circumstances the caching code is running in the security context of whatever
396 process issued the original syscall on the netfs.
397
398 Furthermore, should CacheFiles create a file or directory, the security
399 parameters with that object is created (UID, GID, security label) would be
400 derived from that process that issued the system call, thus potentially
401 preventing other processes from accessing the cache - including CacheFiles's
402 cache management daemon (cachefilesd).
403
404 What is required is to temporarily override the security of the process that
405 issued the system call.  We can't, however, just do an in-place change of the
406 security data as that affects the process as an object, not just as a subject.
407 This means it may lose signals or ptrace events for example, and affects what
408 the process looks like in /proc.
409
410 So CacheFiles makes use of a logical split in the security between the
411 objective security (task->real_cred) and the subjective security (task->cred).
412 The objective security holds the intrinsic security properties of a process and
413 is never overridden.  This is what appears in /proc, and is what is used when a
414 process is the target of an operation by some other process (SIGKILL for
415 example).
416
417 The subjective security holds the active security properties of a process, and
418 may be overridden.  This is not seen externally, and is used whan a process
419 acts upon another object, for example SIGKILLing another process or opening a
420 file.
421
422 LSM hooks exist that allow SELinux (or Smack or whatever) to reject a request
423 for CacheFiles to run in a context of a specific security label, or to create
424 files and directories with another security label.
425
426
427 Statistical Information
428 =======================
429
430 If FS-Cache is compiled with the following option enabled::
431
432         CONFIG_CACHEFILES_HISTOGRAM=y
433
434 then it will gather certain statistics and display them through a proc file.
435
436  /proc/fs/cachefiles/histogram
437
438      ::
439
440         cat /proc/fs/cachefiles/histogram
441         JIFS  SECS  LOOKUPS   MKDIRS    CREATES
442         ===== ===== ========= ========= =========
443
444      This shows the breakdown of the number of times each amount of time
445      between 0 jiffies and HZ-1 jiffies a variety of tasks took to run.  The
446      columns are as follows:
447
448         =======         =======================================================
449         COLUMN          TIME MEASUREMENT
450         =======         =======================================================
451         LOOKUPS         Length of time to perform a lookup on the backing fs
452         MKDIRS          Length of time to perform a mkdir on the backing fs
453         CREATES         Length of time to perform a create on the backing fs
454         =======         =======================================================
455
456      Each row shows the number of events that took a particular range of times.
457      Each step is 1 jiffy in size.  The JIFS column indicates the particular
458      jiffy range covered, and the SECS field the equivalent number of seconds.
459
460
461 Debugging
462 =========
463
464 If CONFIG_CACHEFILES_DEBUG is enabled, the CacheFiles facility can have runtime
465 debugging enabled by adjusting the value in::
466
467         /sys/module/cachefiles/parameters/debug
468
469 This is a bitmask of debugging streams to enable:
470
471         ======= ======= =============================== =======================
472         BIT     VALUE   STREAM                          POINT
473         ======= ======= =============================== =======================
474         0       1       General                         Function entry trace
475         1       2                                       Function exit trace
476         2       4                                       General
477         ======= ======= =============================== =======================
478
479 The appropriate set of values should be OR'd together and the result written to
480 the control file.  For example::
481
482         echo $((1|4|8)) >/sys/module/cachefiles/parameters/debug
483
484 will turn on all function entry debugging.