A Simple File System

What is a File System?

A file system is a data structure used by the operating system to store, retrieve, and organize data on a hard disk or other storage device by invoking the disk's internal functions. The goal of a file system is to provide an abstraction between the user and their stored data. Moreover, users can create files, directories, and specify file details without directly invoking disk functions. Then, when a user or user application invokes the file system interface, the file system will begin to access the data blocks of the disk to which it is mounted through using the core file system structures such as inodes and blocks.

In this project I endeavored to implement a very simple file system using C. It seeks to highlight the primary data structures and access structures used by the file system. My file system is a disk-based file system and uses a disk image file to simulate how the file system would interact with true hardware.

Source code can be found at: github

Disk Emulator

The disk emulator allows the filesystem to store its data as if it was a physical disk of a computer. It is actually just a single disk image file that the file system can write to as if it was actually writing onto a physical disk.

Has the following properites of a true disk:

Open Function:

  1. Allocates disk structure and sets attributes
  2. Opens a file or disk for our file system to be stored on
  3. Depending on the number of blocks specified, truncates (shortens/ extends) the file into the desired size

Read & Write Functions:

  1. Only reads / writes to the one specified block number at a time
  2. Uses the lseek() system call to move to specified block within the disk file
  3. Uses the read() / write() system calls to access or make changes to the disk at the specific block

File System Disk Layout

The first block of the disk contains the superblock which contains:

1. Magic Number: a sequence of bytes that identifies the file system type

2. Total number of blocks which is the same number of blocks on disk

3. Only allows these operations to be done on an entire disk block

4. Total number of inodes

10% of the blocks are reserved for storing inodes which are essentially files represented by a number (low-level file name) and contain:

Metadeta: such as size and if the inode is being used (valid)

Direct Pointers: 5 pointers to blocks containing file data which are just block numbers

Indirect Pointers: The block number of a block containing pointers to 128 other possible blocks

File System Interface