Overview
A file system is the method and data structure that an operating system uses to control how data is stored and retrieved. Without a file system, data placed in a storage medium would be one large body of data with no way to tell where one piece of information stops and the next begins.
Core Concepts
- File Metadata: Information about the file (size, owner, permissions, timestamps) stored separately from the actual content.
- Inodes (Index Nodes): In Unix-like systems, an inode stores all metadata about a file except its name and the actual data blocks. The filename is stored in a directory entry.
- Journaling: A technique where changes are recorded in a “journal” before being applied to the main file system. This allows the OS to recover quickly from a crash without needing a full disk scan.
- Allocation Methods:
- Contiguous: Files are stored in a single block of sequential sectors. Fast but leads to external fragmentation.
- Linked: Files are stored as a linked list of blocks. No external fragmentation, but slow sequential access.
- Indexed: Uses an index block (like an inode) to track all blocks belonging to a file.
- FAT vs. NTFS/ext4:
- FAT32: Simple, highly compatible, but lacks journaling and has a 4GB file size limit.
- NTFS: Windows standard; supports journaling, permissions, and large files.
- ext4: Linux standard; highly efficient, supports journaling and huge volumes.
Code Examples
# Checking file system info on Linux
df -h # Disk free space
ls -i <filename> # Show the inode of a file
Use Cases
- Data Persistence: Ensuring that documents, photos, and applications are stored permanently on non-volatile storage.
- Organization: Creating hierarchical folder structures to manage millions of files.
- Permissions: Restricting access to sensitive files using owner/group/others permissions.
Gotchas
- Fragmentation: When files are split into non-contiguous blocks over time, slowing down mechanical hard drives (HDDs).
- Data Loss: Corruption of the file system metadata (e.g., a corrupted FAT table) can make all files on a disk inaccessible.
