How to enable local file caching for NFS share on Linux

If you often mount and access a remote NFS share on your system, you will probably want to know how to improve NFS file access performance. One possibility is using file caching. In Linux, there is a caching filesystem called FS-Cache which enables file caching for network file systems such as NFS. FS-Cache is built into the Linux kernel 2.6.30 and higher.

In order for FS-Cache to operate, it needs cache back-end which provides actual storage for caching. One such cache back-end is cachefiles. Therefore, once you set up cachefiles, it will automatically enable file caching for NFS shares.

In this tutorial, I will describe how to enable local file caching for NFS shares by using cachefiles.

Requirements for Setting Up CacheFiles

One requirement for setting up cachefiles is that local filesystem support user-defined extended file attributes (i.e., xattr), because cachefiles use xattr to store extra information for cache maintenance.

If your local filesystem is ext4-type, you don't need to worry about this since xattr is enabled in ext4 by default.

However, if you are using ext3 filesystem, then you need to mount the local filesystem with "user_xattr" option. To do so, edit /etc/mtab to add "user_xattr" mount option to the disk partition that will be used by cachefiles for file caching. For example, assuming that /dev/hda1 is such a partition:

/dev/hda1 / ext3 rw,user_xattr  0 0

After modifying /etc/fstab, reload it by running:

$ sudo mount -o remount /

Set Up CacheFiles

In order to set up cache back-end using cachefiles, you need to install cachefilesd, a userspace daemon for managing cachefiles.

To install cachefilesd on Ubuntu or Debian:

$ sudo apt-get install cachefilesd

To install cachefilesd on CentOS, Fedora or RedHat:

$ sudo yum install cachefilesd
$ sudo chkconfig cachefilesd on

After installation, enable cachefilesd by editing its configuration file as follows.

$ sudo vi /etc/default/cachefilesd

Next, mount a remote NFS share with fsc option:

$ sudo vi /etc/fstab /mnt nfs rw,hard,intr,fsc

Alternatively, if you mount the remote NFS share from the command line, specify fsc as a command-line option:

$ sudo mount -t nfs /mnt -o fsc

Finally, restart cachefilesd:

$ sudo service cachefilesd restart

At this point, file caching should be enabled for the mounted NFS share, which means that previously accessed files in the mounted NFS share will be retrieved from local file cache.

If you want to flush NFS file cache for any reason, simply restart cachefilesd.

$ sudo service cachefilesd restart

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

6 thoughts on “How to enable local file caching for NFS share on Linux

  1. Thanks for the link, man. I wonder how much caching is done. Will it consume all the available memory if it is used to access to large VM disk images on NFS storage?

  2. Some typos/errors:

    - it's not /etc/mtab which youn need to edit but /etc/fstab
    - 'sudo mount -a' won't work (at least on CentOS 6) for already mounted filesystems like /, you have to 'sudo mount -o remount /'

    Other than that, great article!

Leave a comment

Your email address will not be published. Required fields are marked *