Block cache

When a filesystem, for example, is imposed upon a block device, often the filesystem reads several blocks many times. For example, may read some directories, and some inodes very frequently. In a simple system, leads to many reads of the same blocks. Conversely, when writing, writes may be posted all over the disk, and sometimes writes may be made frequently to the same block (like the FAT, or to log files), or frequently-updated directories.

A solution to smooth out reads and writes is to implement a cache of blocks. When a request is made to read a block it need not be read from the block device if there is already a copy in the cache. Conversely, when a block is written, it need not be written straightaway, but is written to the cache and may be written back to disk when the block is next flushed from the cache.

Since block reads and writes are relatively heavyweight (unlike memory accesses), it is possible to implement pure least-recently used: the disk blocks in the cache are held on a list in strict order of recent use. Unfortunately, this may mean that some blocks are written to, then held in the cache for a long time before being written to disk. It is important to record data on the disk reasonably quickly, in case something untoward happens. One solution is a 'write through' cache, where changes are both (a) written to disk and (b) stored in the cache as soon as they are made. Other solutions are possible, which delay writes for efficiency reasons, but guaruntee that the data is written to disk reasonably quickly.


last updated 2 April 1998