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