Sub topic 18
Managing free space
The file system must know which blocks are free before it can allocate one. This chapter covers the four classic ways to track free blocks (bit vector, linked list, grouping, counting) and the arithmetic GATE asks about them. It assumes you know the allocation schemes from the previous chapters and can convert between KB, MB and GB using powers of two.
What the free-space manager must do
Whatever structure it uses, the free-space manager has to support three operations:
- Allocate one block, or a run of contiguous blocks.
- Release a block when a file shrinks or is deleted.
- Report how many blocks are free.
The methods differ in how much space they use, how fast they find a free block, and how well they find contiguous runs. Contiguous runs matter for contiguous allocation and for sequential-read speed.
Bit vector (bitmap)
A bit vector has one bit per block. This chapter uses the convention that 1 means free and 0 means allocated. Some books reverse it, so check the question.
Its size is fixed by the disk size. For a 1 TB disk with 4 KB blocks:
- Number of blocks = 2^40 / 2^12 = 2^28.
- Bitmap size = 2^28 bits = 2^25 bytes = 32 MB.
To find the first free block, skip every all-zero word, then find the first 1 bit in the first non-zero word. Most CPUs have an instruction for finding the first set bit. If the first two 32-bit words are zero and the third has its first 1 at offset 5, the block number is 2 × 32 + 5 = 69.
class BitmapAllocator:
def __init__(self, n_blocks):
self.bits = [1] * n_blocks # 1 = free, 0 = allocated
def allocate_first_free(self):
for i, b in enumerate(self.bits):
if b:
self.bits[i] = 0
return i
return None
def allocate_run(self, n):
run = 0
for i, b in enumerate(self.bits):
run = run + 1 if b else 0
if run == n:
start = i - n + 1
for j in range(start, i + 1):
self.bits[j] = 0
return start
return None
def free(self, i):
self.bits[i] = 1
disk = BitmapAllocator(16)
print(disk.allocate_run(4)) # 0
print(disk.allocate_run(4)) # 4
disk.free(1)
disk.free(2)
print(disk.allocate_run(3)) # 8 (blocks 1-2 form a run of only 2)
print(disk.allocate_first_free()) # 1
print(sum(disk.bits)) # 5 free blocks remainStrengths of a bitmap:
- It is simple.
- It finds contiguous runs easily, because neighbouring blocks are neighbouring bits.
- It makes it easy to allocate close to a file's existing blocks.
Weaknesses:
- Its size grows with the disk, whether the disk is empty or full.
- Searching is fast only if the bitmap is cached in memory. A 32 MB bitmap is affordable. A bitmap for a very large disk may not be.
- Every allocation or free changes the bitmap, so the modified bitmap block must also be written to disk. This is a crash-consistency issue (see the journaling chapter).
Linked list of free blocks
The file system keeps a pointer to the first free block. Each free block stores the number of the next free block. The list costs no extra space, because the pointers live inside blocks that are free anyway.
Allocating one block is fast: take the head and move the head to its pointer. Reading the pointer costs one disk read, though. Allocating n contiguous blocks is slow, because you must walk the list, and the list order has no relation to disk position. FAT folds this idea into the table itself: a free entry is marked 0, and the system scans the table for zeros.
Grouping
Grouping is a variation on the linked list. The first free block stores the addresses of n other free blocks. The first n − 1 of those are free blocks to hand out. The last one is a block that holds another n addresses.
With 4 KB blocks and 4-byte pointers, one block holds 4096 / 4 = 1024 addresses. That gives 1023 usable free addresses and one link to the next group. A large set of free blocks can be found with one read, not one read per block.
Counting
Free space often comes in runs, especially under contiguous allocation. Counting stores each run as a pair (first block, number of blocks). A run of 500 free blocks takes one entry instead of 500.
The list is shorter when runs are long. When free space is badly fragmented, entries are mostly (block, 1) and the list is no better than a plain list. Systems usually keep these entries in a balanced tree sorted by address, so searching and merging adjacent runs is efficient. ZFS uses a related idea called space maps.
Comparing the methods
| Method | Extra space | Allocate one block | Find n contiguous blocks | Main weakness |
|---|---|---|---|---|
| Bit vector | 1 bit per block, always | Fast if cached | Easy | Large bitmap on big disks |
| Linked list | None (pointers in free blocks) | Fast (take the head) | Slow | Walking the list costs disk reads |
| Grouping | None | Fast, one read per group | Hard | Still no contiguity information |
| Counting | One entry per run | Fast | Easy when runs are long | Fragmentation inflates the list |
The bitmap versus free-list size question
GATE often asks when a bitmap is smaller than a list of free block numbers. Let N be the number of blocks on the disk, F the number of free blocks, and D the bits in a block number.
- Bitmap size = N bits.
- Free list of block numbers = F × D bits.
The list is smaller when F × D < N, that is, when F < N / D. With 16-bit block numbers, the list wins only if less than 1/16 (6.25%) of the disk is free. A nearly full disk favours a list, and a mostly empty one favours a bitmap.
Practical points
- Deleting a file returns its blocks to the free structure. Only metadata changes, so the data stays on disk until it is overwritten. SSDs also need a TRIM command, which tells the drive that these blocks are no longer in use.
- Allocation policy matters as much as the structure. A bitmap lets the allocator prefer blocks near the file's existing blocks. A plain linked list cannot.
- A crash between "block allocated" and "bitmap updated" can leak a block or, worse, give the same block to two files. Journaling exists to prevent this.
Exercise
A disk is 512 GB and uses 4 KB blocks. How large is a bit-vector free-space map in MB?
Show answer
Blocks = 2^39 / 2^12 = 2^27. The map needs 2^27 bits = 2^24 bytes = 16 MB.
Exercise
A disk has N blocks, and block numbers are 32 bits. A file system keeps a list of free block numbers instead of a bitmap. What fraction of the disk must be free for the list to be smaller than the bitmap?
Show answer
The list uses F × 32 bits and the bitmap uses N bits. The list is smaller when F × 32 < N, so F < N / 32. The list is smaller when fewer than 1/32 (about 3.1%) of the blocks are free.
Exercise
Using 4 KB blocks and 4-byte pointers, how many free blocks does one grouping block describe, counting the pointer to the next group?
Show answer
One block holds 4096 / 4 = 1024 pointers. 1023 point to free blocks, and the last points to the next grouping block.