Storage and file systems

On this page
  1. What a file system provides
  2. The abstraction
  3. Storage devices
  4. The UNIX file system API
  5. Design constraints
  6. Three designs
  7. FAT
  8. FFS (Berkeley Fast File System)
  9. NTFS
  10. Directories

How file systems turn slow, block-based devices into named, persistent files. Disk and flash characteristics, the UNIX file API, and three designs compared: FAT, the Berkeley Fast File System and NTFS.

What a file system provides

A file system is an abstraction on top of persistent storage (magnetic disk, flash memory). Devices give storage that usually survives crashes, block-level random access, and large capacity at low cost – but slow performance: a magnetic disk read takes 10–20 million processor instructions.

As illusionist, the file system hides the limits of physical storage:

  • Persistence, even if a crash happens during an update, a block gets corrupted or flash wears out.
  • Naming: named data instead of block numbers, directories instead of flat storage, byte-addressable data on block devices.
  • Performance: caching, data placement and data structure organisation.
  • Controlled access to shared data.

The abstraction

File
A named collection of data: a linear sequence of bytes, read and written or memory-mapped.
Directory
A group of named files or subdirectories: a mapping from file name to the location of the file's metadata.
Path
A string that uniquely identifies a file or directory, e.g. /cse/www/education/courses/cse451/12au.
Hard link
A link from a name to a metadata location.
Soft link
A link from a name to another name.
Mount
A mapping from a name in one file system to the root of another.

A file system should also leave itself in a valid state after an OS crash or disk error, and get close to the hardware's speed in the average case.

Storage devices

Magnetic diskFlash memory
ReliabilityRarely corruptedRarely corrupted
CostLarge capacity at low costIntermediate (about 50× disk)
AccessBlock-level random accessBlock-level random access
PerformanceSlow for random access, better for streamingGood for reads, worse for random writes

Disk access time

Reading a block costs seek time (moving the arm to the track) + rotational delay (waiting for the block to come under the head) + transfer time. Rotation often dominates, even when the seek is short.

In the OSTEP disk simulator (12 blocks per track, 30 time units per block, 360 per revolution, 40 per track of seek), reading block 18 from a head halfway through block 6 costs seek 40, rotation 305 and transfer 30. The seek happens while the disk rotates, so it is part of the 345 units of rotation the head has to wait anyway.

Disk scheduling: FIFO serves requests in arrival order; SSTF (shortest seek time first) serves the closest one. For requests 0, 35, 18, FIFO goes 0 → 35 → 18 (735 units) and SSTF goes 0 → 18 → 35 (525 units). SSTF can starve far-away requests.

The UNIX file system API

create, link, unlink, createdir, rmdir
Create a file, link to it, remove a link; create or remove a directory.
open, close, read, write, seek
Open and close a file; seek resets the current position.
fsync
File changes can be cached; fsync forces them to disk, like a memory barrier. close does not guarantee that.

See UNIX I/O for why open combines create and open in one atomic call.

Exercise: timing file operations

Create a file, write 100 KB, flush, and delete it, timing each step with gettimeofday():

gettimeofday(&start, NULL);
int fd = creat(argv[1], S_IRUSR | S_IWUSR);   // or open(..., O_CREAT|O_WRONLY|O_TRUNC, ...)
gettimeofday(&now, NULL);

data = malloc(100 * 1024);
memset(data, 0x41, 100 * 1024);
if (write(fd, data, 100 * 1024) != 100 * 1024)
    perror("Failed to write");

fflush(NULL);          // flush; fsync(fd) makes it durable on the device
unlink(argv[1]);       // delete the name
free(data);
close(fd);

Check what write returns – it may write fewer bytes than asked. unlink removes the name; if the file is still open, its data stays until the last descriptor is closed. A monotonic clock is better for timing than the wall clock, which can jump.

Design constraints

  • Small files: small blocks for storage efficiency; concurrent operations are more efficient than sequential; files used together should be stored together.
  • Large files: large blocks; contiguous allocation for sequential access; efficient lookup for random access.
  • When a file is created, you may not know whether it will be small or large, persistent or temporary, or used sequentially or randomly.

A file system needs:

  • Directories: file name → file metadata. Directories are stored as files.
  • File metadata: how to find the file's data blocks.
  • A free map: the list of free blocks.

The design challenges are the index structure (how to find a file's blocks), index granularity (block size), free space, locality, and reliability after a crash mid-operation.

Three designs

FATFFSNTFS
Index structureLinked listTree (fixed, asymmetric)Tree (dynamic)
GranularityBlockBlockExtent
Free spaceFAT arrayBitmap (fixed location)Bitmap (a file)
LocalityDefragmentationBlock groups, reserve spaceExtents, best fit, defrag

FAT

Microsoft's File Allocation Table is a linear map of all blocks on disk. Each file is a linked list of blocks: the file's entry points to its first block, and each FAT entry points to the next. Simple and still widely used, e.g. on USB drives and cameras.

ProsCons
Easy to find a free block, append to a file, delete a file. Small file access is slow; random access is very slow (follow the list); fragmentation – a file's blocks and a directory's files get scattered, worse as the disk fills.

Volume size: FAT32 entries are 32 bits with 4 reserved, so 28 bits address blocks. With 4096-byte blocks the largest volume is 228 × 212 = 240 bytes = 1 TB. (The 2024 solution also mentions the 4 GB maximum file size.)

For an embedded device such as a camera, FAT is the better fit: it is simple and cheap to implement, and the camera mostly appends files and doesn't need owners, permissions or journalling.

FFS (Berkeley Fast File System)

FFS keeps an inode table – an array of inodes, analogous to the FAT table. An inode holds the file's metadata (owner, group, permissions such as set-uid, access times, and so on – but not its name or parent directory, which live in directories) and pointers to its data:

Pointers (4 KB blocks)Reaches
12 direct pointers48 KB
1 indirect pointer → a block of 1K pointers+ 4 MB
1 doubly indirect → 1K indirect blocks+ 4 GB
1 triply indirect → 1K doubly indirect+ 4 TB

This is an asymmetric tree: small files have a shallow tree and are stored efficiently; large files get a deep tree with efficient random access; sparse files only fill in the pointers they need.

Permissions are per file, stored in the inode.

Locality

  • Block groups: sets of nearby cylinders. Files in the same directory go in the same group; subdirectories go in different groups.
  • The inode table is spread across the disk, so inodes and the free bitmap are near the file blocks.
  • First-fit allocation: small files may be fragmented, large files are mostly contiguous.

The 2024 exam (Q25) uses this: file /a 4, file /b 2 put a and b in group 0; dir /c starts group 1; after delete /c and file /c 3, c is a file in / and goes back to group 0.

ProsCons
Efficient storage for small and large files; locality for small and large files; locality for metadata and data. Inefficient for tiny files (a 1-byte file needs an inode and a data block); inefficient when a file is mostly contiguous (no equivalent of superpages); 10–20% of space must stay free to prevent fragmentation.

NTFS

  • A Master File Table (MFT) with flexible 1 KB records for metadata – and for the data itself if the file is small. A file's name is stored in its MFT record.
  • Extents: block pointers that cover runs of blocks (Linux ext4 does the same). When creating a file, a hint about its size can be given.
  • Journalling for reliability.

Small files fit in their MFT record; medium files have a list of extents; large files use indirect MFT records.

Directories

Directories are stored as files. Looking up a path is recursive: read the root directory to find the next name's inode, read that directory, and so on. Small directories use a linear search; large directories use a B-tree.

On earlier exams

Practise this topic in the quiz (16 questions)