Quiz 2

File Systems — Inodes, Directories, File Operations

957 words
5 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# File Systems — Inodes, Directories, File Operations ## 🎯 Learning Objectives - Explain the role of inodes in Unix file systems - Differentiate between contiguous, linked, and indexed allocation - Calculate maximum file size for indexed allocation schemes - Trace directory operations and path resolution - Compare...

File Systems — Inodes, Directories, File Operations

🎯 Learning Objectives

  • Explain the role of inodes in Unix file systems
  • Differentiate between contiguous, linked, and indexed allocation
  • Calculate maximum file size for indexed allocation schemes
  • Trace directory operations and path resolution
  • Compare RAID levels 0, 1, 5, 6, 10

1. File Concepts

1.1 File Attributes

AttributeDescription
NameHuman-readable identifier
IdentifierUnique file descriptor (inode number)
TypeRegular, directory, device, etc.
LocationPointer to file data on disk
SizeCurrent file size
ProtectionRead/write/execute permissions
TimestampsCreation, access, modification times

1.2 File Operations

c
int fd = open("file.txt", O_RDWR);  // Open file → returns file descriptor
char buf[1024];
int n = read(fd, buf, 1024);        // Read bytes
int n = write(fd, buf, n);          // Write bytes
off_t pos = lseek(fd, 100, SEEK_SET); // Reposition file offset
int ret = close(fd);                // Close file

2. Directory Structure

2.1 Single-Level vs Tree-Structured

(Diagram)

2.2 Path Resolution

(Diagram)

3. Inodes (Index Nodes)

3.1 Intuition

An inode is the metadata structure for a file in Unix systems. It stores everything about a file except its name (which is stored in the directory).

3.2 Inode Structure

(Diagram) Typical inode fields in ext2/ext3:
FieldBytesDescription
Mode2File type and permissions
UID2Owner user ID
Size4File size in bytes
GID2Group ID
Links count2Number of hard links
Blocks4Number of blocks
Timestamps4×3atime, mtime, ctime
Direct blocks12×412 direct pointers (48K @ 4KB blocks)
Single indirect4Points to block of block pointers
Double indirect4Points to block of indirect blocks
Triple indirect4Points to block of double indirect

3.3 Maximum File Size Calculation

Given:
  • Block size = 4KB (4096 bytes)
  • Block pointer = 4 bytes
  • Pointers per block = 4096 / 4 = 1024
LevelCalculationSize
Direct (12)12 × 4KB48 KB
Single indirect1024 × 4KB4 MB
Double indirect1024 × 1024 × 4KB4 GB
Triple indirect1024 × 1024 × 1024 × 4KB4 TB
Maximum file size48KB + 4MB + 4GB + 4TB~4 TB

4. File Allocation Methods

4.1 Contiguous Allocation

File occupies consecutive blocks on disk.
ProsCons
Fast sequential accessExternal fragmentation
Simple implementationNeed to know file size upfront

4.2 Linked Allocation

Each block points to the next block.
ProsCons
No external fragmentationVery slow random access
Can grow dynamicallyLink overhead, reliability

4.3 Indexed Allocation

All block pointers are stored in an index block.
ProsCons
Fast random accessIndex block overhead for small files
No fragmentationMaximum file size limited by index block

5. Free Space Management

5.1 Bitmap

A string of bits where each bit represents a free block (1 = free, 0 = allocated).
pseudo
111001010111...
ProsCons
Very fast (single instruction test)Must be in memory for efficiency
SimpleWastes space for small bitmaps

5.2 Free List

Linked list of free block addresses.
ProsCons
No extra space overheadSlower (traverse for allocation)
Works for any sizeFragmentation possible

6. RAID

LevelDescriptionMin DisksProsCons
RAID 0Striping (no redundancy)2Best performanceAny disk failure = data loss
RAID 1Mirroring2Best reliability (N-1 failures)50% capacity loss
RAID 5Striping + Parity3Good balanceSlow writes (parity calc)
RAID 6Striping + Dual Parity4Survives 2 disk failuresEven slower writes
RAID 10Mirror + Stripe4Fast + reliableExpensive (50% loss)

7. Common Pitfalls

Pitfall 1: Deleting an open file

In Unix, you can rm a file while someone has it open. The file data isn't freed until all file descriptors are closed. The inode persists but the directory entry is removed (link count → 0).
  • Hard link: Same inode, different directory entry. Can't cross file systems.
  • Symbolic link: Special file pointing to pathname. Can cross file systems.

Pitfall 3: Inode exhaustion

Creating many tiny files exhausts the fixed inode table even if plenty of disk space remains.

8. 📝 Practice Questions

Q1: Calculate the maximum file size for a system with 1KB blocks, 4-byte pointers, and the same 12 direct + indirect scheme.
Answer:
  • Direct: 12 × 1KB = 12KB
  • Single: (1024/4) × 1KB = 256 × 1KB = 256KB
  • Double: 256 × 256 × 1KB = 256 × 256KB = 64MB
  • Triple: 256 × 256 × 256 × 1KB = 256 × 64MB = 16GB
  • Max: 12KB + 256KB + 64MB + 16GB ≈ 16.06GB Q2: What is stored in an inode vs what is stored in a directory entry?
Answer: Inode stores metadata (permissions, timestamps, block pointers). Directory entry stores just the name and inode number. The inode doesn't know its filename! Q3: Why does RAID 5 have a "write penalty"?
Answer: A single write requires: read old data, read old parity, write new data, write new parity = 4 I/O operations for 1 write. This is the "RAID 5 write penalty." Q4: Explain why symbolic links can cross file systems but hard links cannot.
Answer: Hard links share the same inode number, which is unique only within a single file system. Symbolic links store a pathname string, which can reference any file accessible through the namespace. Q5: A file has 12 direct blocks, single, double, and triple indirect. Block size = 4KB. How many blocks needed to store a 5GB file?
Answer: A 5GB file needs:
  • 12 direct blocks = 48KB (0.01% of file)
  • 1 single indirect block containing 1024 pointers → 4MB
  • 1 double indirect block + 1024 indirect blocks → 4GB (via pointers)
  • Triple indirect for the remaining ~1GB

9. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.