FAT File System
Cluster Chains
Read aloud is unavailable in this browser.
The Linked Architecture of File Storage
When you save a 1 MB document to a FAT-formatted drive, the file doesn't occupy a single contiguous region. Instead, it's distributed across multiple clusters—allocation units scattered throughout the disk's data region. The File Allocation Table serves as the map connecting these pieces.
This cluster-chain architecture is FAT's defining characteristic. It provides the flexibility to grow, shrink, and delete files without moving data, but it also introduces challenges: fragmentation degrades read performance, chain corruption can lose entire files, and random access requires traversing the chain.
Mastering cluster chains means understanding how files truly live on disk—and why defragmentation makes such a dramatic difference on FAT volumes.
By the end of this page, you will understand cluster chain construction, traversal algorithms, allocation strategies, fragmentation causes and effects, and the techniques used to optimize and repair cluster chains. You'll also learn to implement efficient chain operations.
Anatomy of a Cluster Chain
A cluster chain is a linked list of disk allocation units that together constitute a file (or directory). Each file has:
- Starting cluster — Stored in the file's directory entry
- Chain links — FAT entries pointing to subsequent clusters
- Terminator — An end-of-chain marker in the final cluster's FAT entry
Let's examine a concrete example:
1234567891011121314151617181920212223242526272829
File: "report.pdf" (Size: 185,000 bytes)Cluster size: 4,096 bytes (4 KB)Clusters needed: ceil(185,000 / 4,096) = 46 clusters Directory Entry:┌────────────────┬───────────────┬──────────────┐│ Filename │ First Cluster │ File Size ││ REPORT .PDF │ 100 │ 185,000 │└────────────────┴───────────────┴──────────────┘ FAT Table (partial view):┌─────────┬───────────┬───────────────────────────────┐│ Cluster │ FAT Entry │ Meaning │├─────────┼───────────┼───────────────────────────────┤│ 100 │ 101 │ → Next cluster is 101 ││ 101 │ 102 │ → Next cluster is 102 ││ 102 │ 107 │ → Next cluster is 107 (gap!) ││ 103 │ 0 │ (Free cluster) ││ 104 │ 0 │ (Free cluster) ││ 105 │ 0 │ (Free cluster) ││ 106 │ 0 │ (Free cluster) ││ 107 │ 108 │ → Next cluster is 108 ││ ... │ ... │ ... ││ 144 │ 145 │ → Next cluster is 145 ││ 145 │ 0xFFFF │ → END OF CHAIN │└─────────┴───────────┴───────────────────────────────┘ The chain: 100 → 101 → 102 → 107 → 108 → ... → 145 → ENDNotice: Clusters 103-106 were not available when file was writtenKey Observations:
-
The chain can be fragmented — Cluster 102 jumps to 107, skipping clusters 103-106 (which may belong to other files or be free).
-
File size determines content bounds — The last cluster (145) contains only partial data. File size tells us exactly how many bytes are valid.
-
FAT is the authoritative map — Without the FAT, we cannot determine which clusters belong to which file or their correct order.
-
Cluster 0 and 1 are reserved — Valid data clusters start at cluster 2; entries 0 and 1 contain special values.
Chain Traversal Algorithms
Traversing a cluster chain is fundamental to all FAT file operations. Let's examine the core algorithms:
Complete Chain Traversal (Sequential Read):
123456789101112131415161718192021222324252627282930313233343536373839404142
// Read an entire file by traversing its cluster chainssize_t readFileComplete(DirectoryEntry *entry, uint8_t *buffer) { uint32_t cluster = getFirstCluster(entry); uint32_t fileSize = entry->fileSize; size_t bytesRemaining = fileSize; size_t totalRead = 0; size_t clusterBytes = bytesPerSector * sectorsPerCluster; while (!isEndOfChain(cluster) && bytesRemaining > 0) { // Validate cluster is in valid range if (cluster < 2 || cluster >= totalDataClusters + 2) { errno = EIO; // Corrupted chain return -1; } // Calculate how many bytes to read from this cluster size_t bytesToRead = (bytesRemaining < clusterBytes) ? bytesRemaining : clusterBytes; // Read cluster data from disk uint32_t sector = clusterToSector(cluster); if (readSectors(sector, sectorsPerCluster, buffer + totalRead) < 0) { return -1; // Disk error } totalRead += bytesToRead; bytesRemaining -= bytesToRead; // Follow chain to next cluster cluster = getFATEntry(cluster); } // Verify we read exactly the expected amount if (bytesRemaining > 0 && isEndOfChain(cluster)) { // Chain ended before file size - corruption errno = EIO; return -1; } return totalRead;}Random Access (Seek to Position):
FAT's linked structure makes random access O(n) where n is the cluster index. To read byte 500,000/cluster of a file, we must traverse the chain:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
// Seek to a specific offset in a file// Returns the cluster containing that offset and offset within clusterint seekToPosition(uint32_t firstCluster, size_t offset, uint32_t *outCluster, size_t *outClusterOffset) { size_t clusterBytes = bytesPerSector * sectorsPerCluster; // Calculate which cluster index we need size_t targetClusterIndex = offset / clusterBytes; // Traverse chain to reach target cluster uint32_t cluster = firstCluster; size_t currentIndex = 0; while (currentIndex < targetClusterIndex) { if (isEndOfChain(cluster) || isBadCluster(cluster)) { return -1; // Offset beyond file end or bad chain } cluster = getFATEntry(cluster); currentIndex++; // Safety check for infinite loops (corrupted FAT) if (currentIndex > totalDataClusters) { return -1; // Chain loops - corruption } } *outCluster = cluster; *outClusterOffset = offset % clusterBytes; return 0;} // Performance problem: Reading byte 0 is O(1), but reading // byte 1,000,000 in a 4KB cluster file requires traversing// ~244 FAT entries. Each random read repeats this traversal! // Optimization: Cache the cluster chaintypedef struct { uint32_t *clusters; // Array of cluster numbers in order size_t clusterCount; // Number of clusters in chain size_t clusterBytes; // Bytes per cluster} ClusterChainCache; // Build cache for a file (do once, reuse for all accesses)ClusterChainCache *buildChainCache(uint32_t firstCluster) { ClusterChainCache *cache = malloc(sizeof(ClusterChainCache)); // First pass: count clusters cache->clusterCount = countClustersInChain(firstCluster); cache->clusters = malloc(cache->clusterCount * sizeof(uint32_t)); cache->clusterBytes = bytesPerSector * sectorsPerCluster; // Second pass: record cluster numbers uint32_t cluster = firstCluster; for (size_t i = 0; i < cache->clusterCount; i++) { cache->clusters[i] = cluster; cluster = getFATEntry(cluster); } return cache;} // Random access with cache: O(1)!uint32_t getClusterAtOffset(ClusterChainCache *cache, size_t offset) { size_t index = offset / cache->clusterBytes; if (index >= cache->clusterCount) return 0; // Beyond file return cache->clusters[index];}Real operating systems cache cluster chains for open files. When you open() a file, the OS often reads the entire FAT chain into memory, making subsequent seek() and read() operations O(1). This is why opening a heavily fragmented file may take longer, but once open, access is fast.
Chain Allocation Strategies
When creating or extending a file, the file system must allocate free clusters and link them into the chain. The allocation strategy significantly impacts fragmentation and performance.
Basic Allocation Algorithm:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
// Extend a file's cluster chain by allocating additional clusters// Returns 0 on success, -1 on failure (disk full)int extendClusterChain(uint32_t lastCluster, size_t clustersNeeded, uint32_t *newFirstCluster) { uint32_t prevCluster = lastCluster; uint32_t firstAllocated = 0; size_t allocated = 0; // Get hint for where to start searching (FAT32 FSInfo) uint32_t searchStart = (fsInfo && lastCluster > 0) ? lastCluster + 1 // Try to allocate contiguously : (fsInfo ? fsInfo->nextFree : 2); // Search for free clusters uint32_t cluster = searchStart; while (allocated < clustersNeeded) { // Wrap around if we reach the end if (cluster >= totalDataClusters + 2) { cluster = 2; } // Check if we've searched the entire disk if (cluster == searchStart && allocated > 0) { // We've looped without finding enough clusters freeAllocatedClusters(firstAllocated); return -1; // Disk full } // Check if cluster is free if (getFATEntry(cluster) == FREE_CLUSTER) { // Allocate this cluster setFATEntry(cluster, END_OF_CHAIN); if (firstAllocated == 0) { firstAllocated = cluster; } // Link previous cluster to this one if (prevCluster != 0) { setFATEntry(prevCluster, cluster); } prevCluster = cluster; allocated++; } cluster++; } // Update FSInfo hints (FAT32) if (fsInfo) { fsInfo->freeClusterCount -= allocated; fsInfo->nextFree = cluster; } *newFirstCluster = firstAllocated; return 0;}Allocation Strategies:
| Strategy | Description | Pros | Cons |
|---|---|---|---|
| First-Fit | Allocate first free cluster found | Simple, fast allocation | Maximum fragmentation |
| Next-Fit | Continue from last allocation point | Good locality for sequential writes | Still fragments over time |
| Best-Fit Contiguous | Find smallest contiguous run that fits | Minimal fragmentation | Expensive search, may fail with space available |
| Pre-allocation | Reserve clusters before writing | Ensures contiguous runs | May waste space if file smaller than expected |
Most FAT implementations use Next-Fit:
The FSInfo structure in FAT32 stores a nextFree hint, pointing to where allocation last occurred. New allocations start searching from this point, which tends to:
- Keep sequentially-written files contiguous (if space permits)
- Distribute wear across the entire volume (important for flash storage)
- Avoid repeatedly scanning already-allocated regions
However, after deletions create gaps, Next-Fit cannot guarantee contiguous allocation.
On SSD and flash media, the file system's allocation pattern interacts with the device's wear-leveling controller. FAT's simple approach actually works well here—the device controller remaps logical clusters to physical cells, making the file system's physical layout less critical than on spinning disks.
Fragmentation: Causes and Effects
Fragmentation occurs when a file's clusters are not physically contiguous on disk. This is an inherent consequence of FAT's linked allocation design.
How Fragmentation Develops:
12345678910111213141516171819202122232425262728293031
INITIAL STATE - Fresh disk, all clusters free:Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: FREE FREE FREE FREE FREE FREE FREE... STEP 1 - Create file A (3 clusters):Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: A-1 A-2 A-3 FREE FREE FREE FREE...File A chain: 2 → 3 → 4 → END (contiguous!) STEP 2 - Create file B (2 clusters):Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: A-1 A-2 A-3 B-1 B-2 FREE FREE...File B chain: 5 → 6 → END (contiguous!) STEP 3 - Delete file A:Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: FREE FREE FREE B-1 B-2 FREE FREE...(Clusters 2,3,4 now free - file B blocks the gap) STEP 4 - Create file C (5 clusters):Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: C-1 C-2 C-3 B-1 B-2 C-4 C-5 FREE...File C chain: 2 → 3 → 4 → 7 → 8 → END*** FILE C IS FRAGMENTED! ***(Clusters split: 2,3,4 and 7,8 - file B in the middle) STEP 5 - Extend file B (3 more clusters):Clusters: [2][3][4][5][6][7][8][9][10][11][12]...Status: C C C B-1 B-2 C C B-3 B-4 B-5...File B chain: 5 → 6 → 9 → 10 → 11 → END*** FILE B IS NOW FRAGMENTED TOO! ***Performance Impact of Fragmentation:
On traditional spinning hard drives, fragmentation is devastating:
- •Seek time — Moving the disk head between non-contiguous clusters takes 5-15ms per seek. Reading a file fragmented across 100 locations adds 0.5-1.5 seconds of pure seek overhead.
- •Rotational latency — After seeking, the disk must wait for the target sector to rotate under the head (average 4-8ms on a 7200 RPM drive).
- •Broken read-ahead — Disk controllers optimize sequential reads by prefetching. Fragmentation defeats this optimization.
- •Increased wear — More head movements accelerate mechanical wear on spinning drives.
| Scenario | Effective Speed | Time for 1 GB File |
|---|---|---|
| Contiguous (sequential) | ~150 MB/s | ~7 seconds |
| Light fragmentation (10 fragments) | ~80 MB/s | ~13 seconds |
| Heavy fragmentation (1000 fragments) | ~5 MB/s | ~200 seconds |
| Extreme fragmentation (per-cluster) | ~0.5 MB/s | ~33 minutes |
Solid-state drives have no seek time—any cluster can be read as fast as any other. Fragmentation has minimal impact on SSD read performance. However, excessive fragmentation still increases metadata overhead and can complicate the SSD's internal garbage collection. Most experts recommend NOT defragmenting SSDs (to avoid unnecessary write wear).
Defragmentation: Restoring Contiguity
Defragmentation reorganizes files so their clusters are contiguous, restoring optimal read performance on spinning disks.
Defragmentation Algorithm:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
function defragmentVolume(): // Phase 1: Build file map for each file in volume: file.clusterChain = readClusterChain(file.firstCluster) file.fragments = countFragments(file.clusterChain) // Sort files by size or fragmentation level files = sortByPriority(files) // Phase 2: Compact files to beginning of data region targetCluster = 2 // First data cluster for each file in files: if file.firstCluster != targetCluster: // Move file to contiguous location starting at targetCluster newChain = relocateFile(file, targetCluster) // Update directory entry with new first cluster updateDirectoryEntry(file, newChain[0]) // Next file starts after this one targetCluster += file.clusterCount // Phase 3: Update FAT and FSInfo updateFreeClusterInfo() function relocateFile(file, targetStart): newChain = [] sourceCluster = file.firstCluster destCluster = targetStart while not isEndOfChain(sourceCluster): // Copy data from source to destination data = readCluster(sourceCluster) writeCluster(destCluster, data) // Update FAT entries setFATEntry(sourceCluster, FREE_CLUSTER) if newChain is not empty: setFATEntry(newChain.last(), destCluster) setFATEntry(destCluster, END_OF_CHAIN) // Temporary newChain.append(destCluster) sourceCluster = getFATEntry(sourceCluster) destCluster += 1 return newChainDefragmentation Considerations:
- •Time intensive — Moving data is slow. A heavily fragmented 500 GB drive may take hours to defragment.
- •Risk of data loss — If power fails during relocation, data may be lost or corrupted. Good defragmenters use careful transaction ordering.
- •Free space required — Some space is needed as a temporary buffer during relocation.
- •SSD harm — Defragmenting SSDs wastes write cycles without benefit. Modern Windows skips defrag for SSDs.
- •Temporary performance drop — During defragmentation, file access competes with the relocation process.
- •Spinning hard drives (HDD)
- •Noticeable slowdown in file access
- •After mass file deletions/moves
- •Before imaging/cloning a drive
- •Solid-state drives (SSD)
- •USB flash drives (limited write cycles)
- •SD cards and memory cards
- •Network or cloud storage
Chain Corruption and Recovery
Cluster chains can become corrupted due to power loss, hardware failure, or software bugs. Understanding corruption types is essential for recovery.
Types of Chain Corruption:
| Type | Description | Detection | Recovery |
|---|---|---|---|
| Lost Cluster | Cluster marked used but not in any chain | Compare FAT to directory entries | Create file or mark free |
| Cross-link | Same cluster in multiple file chains | Build cluster-to-file map | Copy cluster, truncate one chain |
| Circular Chain | Chain loops back to earlier cluster | Detect revisited clusters during traversal | Truncate at loop point |
| Invalid Link | FAT entry points to out-of-range cluster | Validate cluster numbers during traversal | Truncate chain before bad link |
| Size Mismatch | File size doesn't match cluster count | Compare size to chain length | Adjust size or truncate chain |
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
// Validate a cluster chain for corruptiontypedef enum { CHAIN_OK, CHAIN_LOOP, CHAIN_INVALID_CLUSTER, CHAIN_SIZE_MISMATCH} ChainStatus; ChainStatus validateClusterChain(uint32_t firstCluster, uint32_t expectedSize) { // Use Floyd's cycle detection (tortoise and hare) uint32_t slow = firstCluster; uint32_t fast = firstCluster; size_t clusterBytes = bytesPerSector * sectorsPerCluster; size_t chainBytes = 0; // Visited bitmap for another layer of loop detection uint8_t *visited = calloc((totalDataClusters + 7) / 8, 1); while (!isEndOfChain(slow)) { // Check for invalid cluster number if (slow < 2 || slow >= totalDataClusters + 2) { free(visited); return CHAIN_INVALID_CLUSTER; } // Check if we've visited this cluster before uint32_t idx = slow - 2; if (visited[idx / 8] & (1 << (idx % 8))) { free(visited); return CHAIN_LOOP; } visited[idx / 8] |= (1 << (idx % 8)); chainBytes += clusterBytes; slow = getFATEntry(slow); // Move fast pointer twice if (!isEndOfChain(fast)) { fast = getFATEntry(fast); if (!isEndOfChain(fast)) { fast = getFATEntry(fast); } } // Floyd's: if slow meets fast, there's a loop if (slow == fast && !isEndOfChain(slow)) { free(visited); return CHAIN_LOOP; } } free(visited); // Verify size matches chain length (allow for partial last cluster) size_t expectedClusters = (expectedSize + clusterBytes - 1) / clusterBytes; size_t actualClusters = chainBytes / clusterBytes; if (expectedClusters != actualClusters) { return CHAIN_SIZE_MISMATCH; } return CHAIN_OK;}Remember that FAT maintains two identical copies of the allocation table. Recovery tools compare these copies to detect corruption. If one is damaged, the other may provide correct chain information. Always check both FATs when diagnosing corruption.
Performance Optimization Techniques
Operating systems employ several techniques to optimize cluster chain performance:
- •FAT Caching — Keep the entire FAT (or frequently-accessed regions) in RAM. This makes chain traversal a memory operation, not a disk operation. For a 32 GB FAT32 volume with 4 KB clusters, the FAT is only ~32 MB.
- •Chain Caching — Cache the complete cluster list for open files. After initial traversal, all random access becomes O(1).
- •Extent Representation — Internally represent chains as extents (start cluster + length) rather than per-cluster linked lists. A contiguous 100-cluster run becomes one extent, not 100 links.
- •Read-ahead Clustering — When reading a cluster, prefetch subsequent clusters in the chain if they're likely to be accessed soon (sequential read detection).
- •Delayed Allocation — Buffer writes in memory and allocate clusters only when flushing. This allows allocating clusters contiguously even for random writes.
- •Write Clustering — Accumulate small writes and commit them as a single cluster write, reducing FAT updates.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
// Represent a cluster chain as extents for efficiencytypedef struct { uint32_t startCluster; uint32_t clusterCount;} Extent; typedef struct { Extent *extents; size_t extentCount; size_t totalClusters;} ExtentList; // Convert FAT chain to extent listExtentList *chainToExtents(uint32_t firstCluster) { ExtentList *list = malloc(sizeof(ExtentList)); list->extents = malloc(128 * sizeof(Extent)); // Initial capacity list->extentCount = 0; list->totalClusters = 0; uint32_t cluster = firstCluster; uint32_t extentStart = cluster; uint32_t extentLength = 0; while (!isEndOfChain(cluster)) { uint32_t next = getFATEntry(cluster); extentLength++; list->totalClusters++; // Check if next cluster is contiguous if (next != cluster + 1 || isEndOfChain(next)) { // End current extent list->extents[list->extentCount].startCluster = extentStart; list->extents[list->extentCount].clusterCount = extentLength; list->extentCount++; // Start new extent extentStart = next; extentLength = 0; } cluster = next; } return list;} // Find cluster at logical position using extent list: O(log n)uint32_t getClusterFromExtents(ExtentList *list, size_t clusterIndex) { size_t remaining = clusterIndex; for (size_t i = 0; i < list->extentCount; i++) { if (remaining < list->extents[i].clusterCount) { return list->extents[i].startCluster + remaining; } remaining -= list->extents[i].clusterCount; } return 0; // Beyond file} // Benefits:// - Contiguous file: 1 extent, O(1) lookup// - Moderately fragmented: few extents, fast lookup// - Heavily fragmented: degrades to O(n), but still faster than FAT traversalKEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.