Persistence
Overall Organization
On-Disk Organization
- a series of blocks (e.g. size of each block is 4KB).
- the blocks are addressed from 0 to N-1.
File System Organization
Metadata Region: tracks data and file system information
Inode Table: the collection of all inodes
- There are 3 to 7 blocks for inode tables and each inode size is 256 bytes.
- The file system can contain $ \frac{4 \cdot 7 \cdot 1024}{256} $ = 80 inodes (i.e. maximum number of files).
Bitmap: to track whether inodes or data blocks are free or allocated
- data bitmap: for data blocks in data region
- inode bitmap: for inode table
Superblock: tracks a file system
- It contains the information for the particular file system (e.g. the number of inodes, the begin location of inode table).
Data Region: stores user data and occupies most space

File Organization
Inode
Inode
- Contains all the information about a file.
- e.g. file type, size, protection information, time information, etc.
Direct Pointers: points to a data block explicitly
- Design of inode: How it refers to where data blocks are.
- File system calculate where the data block that the inode refers to.
- For example, given an inode number 32, calculate the offset into the inode region (32 $\times$ sizeof(inode)) = 8192. Then, adding the start address of the inode table (12KB) + inode region (8KB) = 20 KB and we can get the location of data block.

Storage

- The size of an inode is 60 bytes, and suppose each block number is 4 bytes, so we can store 15 blocks in total.
Suppose each data block is 4KB, the maximum file size is 15 $\times$ 4 = 60KB. - Therefore, it is hard to support files of big sizes.
- The size of an inode is 60 bytes, and suppose each block number is 4 bytes, so we can store 15 blocks in total.
Multi-Level Index
Indirect Pointer: points to a block that contains (multiple) pointers to data blocks
- The maximum file size = (12 + 1024) $\times$ 4KB $\approx$ 4MB.
- Because each inode is 4 bytes and each block is 4KB, so we can hold 1024 inodes, then the file size is 1K $\times$ 4KB = 4MB.
Double Indirect Pointer: points to pointers to indirect blocks
- The maximum file size = (12 + 1024 $\times$ 1024) $\times$ 4KB $\approx$ 1 million 4KB blocks.
Triple Indirect Pointer: points to pointers to pointers to indirect block
- The maximum file size = (12 + 1024 + 1024$^2$) $\times$ 4KB $\approx$ 4GB.
Directory Organization
Directory
- Directory contains a list of (entry name, inode number) pairs.
- Each directory has two extra file:
.: current directory..: parent directory
- For example:
dirhas three files :foo,bar,foobar_is_a_pretty_longname.
Free Space Management
- File system track which inode and data block are free to use.
- Two simple bitmaps are used to manage the free space.
- When file is newly created, it allocated the inode by searching the inode bitmap and update on-disk bitmap.
- Pre-allocation policy is commonly used for allocate contiguous blocks.
Access Paths
Reading File from Disk
open("/foo/bar", O_RDONLY)- Traverse the pathname and thus locate the desired inode.
- Begin at the root of the file system (
/). - File system reads in the block that contains inode number 2 (the root inode number in most Unix file system).
- Look inside of the content of root to find pointer to data blocks.
- By reading in one or more directory data block, and find “foo” directory finally.
- Tranverse recursively the path name until the desired inode (“bar”).
- Check the finale permissions (權限), allocate a file descriptor for this process and return the file descriptor.
read()andclose()- Read in the first block of the file, consulting the inode to find the location of that block.
- Update the inode with a new last accessed time.
- Update in-memory open file table for file descriptor (i.e. the file offset).
- When the file is closed, the file descriptor should be disallocated.
- NO disk I/Os take place.
- Read in the first block of the file, consulting the inode to find the location of that block.
Timeline for reading file from disk:

Writing to Disk
write(): update the file with new contents- Need to update data block and data bitmap.
- Generates 5 I/Os:
- read the data bitmap
- write the data bitmap (to reflect its new state to disk)
- read the inode
- write the inode
- write the actual block itself
- Need to allocate space or directory for creating file, it causes high I/O traffic.
Timeline for writing to disk:

Caching & Buffering
Problems:
- Reading and writing files are expensive because it incurs many I/Os.
- For example, long pathname (
/dir1/dir2/.../dir100/file.txt) - It performs hundreds of reads in order to open the file (one to read the inode of the directory and at least one to read its data).
- For example, long pathname (
- Reading and writing files are expensive because it incurs many I/Os.
Solutions:
Read & Cache
- To reduce the I/O traffic, file system uses system memory (DRAM) to cache.
- Early, file system uses fixed-size cache to hold the popular blocks, but it is wasting memory.
- Now, model system uses dynamic partitioning approach and unified page cache.
- Thus, read I/O can be avoided by the cache.
- To reduce the I/O traffic, file system uses system memory (DRAM) to cache.
Write & Buffer
- Since write traffic has to go to the disk for persistent, cache does not reduce write I/Os.
- File system uses write buffering for writing performance benefits.
- It can delay writes such that the file system batch some updates into a smaller set of I/Os.
- By buffering a number of writes in memory, the file system can schedule the subsequent I/Os.
- Some application force flush data to the disk by calling
fsync()or direct I/O.


