CacheFS

CacheFS is written by David Howells / Red Hat, Inc. It is a general cache facility and can be used to cache NFS mounts.

The Linux-cachefs mailing list can be found at https://www.redhat.com/mailman/listinfo/linux-cachefs.

Kernel requirements
For this installation, you need at least version 2.6.30. Note that some features might be experimental, so you might have to enable CONFIG_EXPERIMENTAL under General setup. [*] Prompt for development and/or incomplete code/drivers

First of all, extended attributes are required on the filesystem that is going to host the cache. Assuming you use ext3, enable the following under File systems. <*> Ext3 journalling file system support [*]  Ext3 extended attributes

Next, we're going to enable the caches under File systems > Caches <*> General filesystem local caching manager <*>  Filesystem caching on files

And last but not least, don't forget to enable the caching on the network file system itself under File systems > Network File Systems. Currently, only NFS and AFS are supported. Enable whichever you're going to use.   NFS client support [*]  Provide NFS client caching support (EXPERIMENTAL)   Andrew File System support (AFS) (EXPERIMENTAL) [*]  Provide AFS client caching support (EXPERIMENTAL)

Now build your new kernel the usual way.

Filesystem requirements
CacheFS can only be used with user_xattr support in your filesystem. You can either add it to the mount options in fstab, or enable it on the filesystem.

To enable it in fstab, add user_xattr to the fourth column. Assuming the FS-Cache is on / and /dev/sdaX is mounted on /. /dev/sdaX   /               ext3    noatime,user_xattr         0 1

To enable it on the filesystem with tune2fs for ext2 / ext3. Replace sdaX with your actual partition. tune2fs -o user_xattr /dev/sdaX

Afterwards you'll see it now has a default mount option. donald ~ # tune2fs -l /dev/sdaX | grep 'Default mount options' Default mount options:   user_xattr

Cachefilesd
The cachefilesd daemon is not in the main tree, but an ebuild for cachefilesd can be found at http://bugs.gentoo.org/show_bug.cgi?id=275014.

emerge cachefilesd mkdir -p /var/fscache rc-update add cachefilesd default /etc/init.d/cachefilesd start

Working with FS-Cache
In order to use the FS-Cache, you must mount with the fsc-option. For example. localhost ~ # mount -t nfs -o fsc ip:/source/dir /destination/dir

Or in fstab ip:/source/dir   /destination/dir    nfs    fsc    0 0

When the cache is bound properly, you'll find 2 directories under /var/fscache.
 * cache
 * graveyard

Cache entries will be found under cache like cryptic \@4a/I0nfs/\@41 and so on.

Testing
For Testing you can copy some files, like this time cp source destination time cp source /dev/null time cp source destination
 * From NFS to Hd, read from NFS and writing 2 times on Hd (cache init)
 * Read only from Cache to Ram or /dev/null,
 * From Cache to Hd, read from Hd and write to Hd

One Big File
The File is about 350 MB and it's written 2 times on hd to initialize the cache. localhost ~ # time cp /nfsmount/oneBig.file. real   6m10.907s user   0m0.172s sys    0m12.161s

Copy from cache, file is read from hd localhost ~ # time cp /nfsmount/oneBig.file /dev/null real   1m42.042s user   0m0.144s sys    0m52.467s

Copy from cache, read from and write to hd localhost ~ # time cp /nfsmount/oneBig.file. real   3m33.246s user   0m0.176s sys    1m1.348s

Many small files
For this test, we copy 1.8G of kernel sources.

Normal NFS copy, no cache localhost ~ # time cp -r /nfsmount/usr/src. real   33m47.274s user   0m6.192s sys    1m45.919s

Files are written 2 times on hd (cache init) localhost ~ # time cp -r /nfsmount/usr/src/. real   73m52.464s user   0m5.440s sys    3m55.663s

With cache, read from hd localhost ~ # time tar cf /dev/null /mnt/root/usr/src tar: Removing leading `/' from member names real   18m59.711s user   0m2.640s sys    0m32.150s

Conclusion
The cache is good while reading files. As soon as many small files are copied, causes the cache opposite. Particularly on slow harddrives. Solution: Put cache to another harddrive.