Less known Solaris Features: CacheFS - Part 3: A basic example

Okay … using CacheFS is really easy. Let´s assume, you have an fileserver called theoden We use the directory /export/files as the directory shared by NFS. The client in our example is gandalf.

Preparations

Let´s create a NFS server at first. This is easy. Just share an directory on a Solaris Server. We login onto theoden and execute the following commands with root privileges.

[root@theoden:/]# mkdir /export/files<br />
[root@theoden:/]# share -o rw /export/files<br />
# share<br />
-               /export/files   rw   ""

Okay, of course it would be nice to have some files to play around in this directory. I will use some files of the Solaris Environment.

[root@theoden:/]# cd /export/files
[root@theoden:/export/files]# cp -R /usr/share/doc/pcre/html/* .

Let´s do a quick test, if we can mount the directory:

[root@gandalf:/]# mkdir /files
[root@gandalf:/]# mount theoden:/export/files /files
[root@gandalf:/]# unmount /files

Now you should be able to access the \verb=/export/files= directory on theoden by accessing \verb=/files= on gandalf. There should be no error messages. Okay, at first we have to create the location for our caching directories. Let´s assume we want to place our cache at /var/cachefs/caches/cache1. At first we create the directories above the cache directory. You don´t create the last part of the directory structure manually.

[root@gandalf:/]# mkdir -p /var/cachefs/caches

This directory will the the place where we store our caches for CacheFS. After this step we have to create the cache for the CacheFS.

[root@gandalf:/files]# cfsadmin -c -o maxblocks=60,minblocks=40,threshblocks=50 /var/cachefs/caches/cache1

The directory cache1 is created automatically by the command. In the case the directory already exists, the command will quit without creating the cache. Additionally you have created the cache and you specified some basic parameters to control the behaviour of the cache. Citing the manpage of cfsadmin:

All this parameter can be tuned to preven CacheFS to eat away all the storage available in a filesystem, a behaviour that was quite common to early versions of this feature.

Mounting a filesystem via CacheFS

We have to mount the original filesystem now.

[root@gandalf:/files]# mkdir -p /var/cachefs/backpaths/files
[root@gandalf:/files]# mount -o vers=3 theoden:/export/files /var/cachefs/backpaths/files

You may have noticed the parameter that sets the NFS version to 3. This is nescessary, as CacheFS isn´t supported with NFSv4. Thus you can only use it with NFSv3 and below. The reason of this limitation has it´s foundation in the different way NFSv4 handles inodes. Okay, now we mount the cache filesystem at the old location:

[root@gandalf:/files]# mount -F cachefs -o backfstype=nfs,backpath=/var/cachefs/backpaths/files,cachedir=/var/cachefs/caches/cache1 theoden:/export/files /files

The options of the mount controls some basic parameters of the mount:

From now on every access to the /files directory will be cached by CacheFS. Let´s have a quick look into the /etc/mnttab. There are two important mounts for us:

[root@gandalf:/etc]# cat mnttab<br />
[...]<br />
theoden:/export/files   /var/cachefs/backpaths/files    nfs     vers=3,xattr,dev=4f80001        1219049560<br />
/var/cachefs/backpaths/files    /files  cachefs backfstype=nfs,backpath=/var/cachefs/backpaths/files,cachedir=/var/cachefs/caches/cache1,dev=4fc0001   1219049688

The first mount is our back file system, it´s a normal NFS mountpoint. But the second mount is a special one. This one is the consequence of the mount with the \verb=-F cachefs= option.

Statistics about the cache

While using it, you will see the cache structure at /var/cachefs/caches/cache1 filling up with files. I will explain some of the structure in the next section. But how efficient is this cache? Solaris provides an command to gather some statistics about the cache. With cachefsstat you print out data like hit rate inclusive the absolute number of cache hits and cache misses:

[root@gandalf:/files]# /usr/bin/cachefsstat

    /files
                 cache hit rate:    60% (3 hits, 2 misses)
             consistency checks:      7 (7 pass, 0 fail)
                       modifies:      0
             garbage collection:      0
[root@gandalf:/files]#