Lecture 9 File System

Section 2 Design and Implementation of File System



Yong Xiang, Yu Chen, Guoliang Li, Ju Ren

Spring 2023

Outline

1. Overview

  1. Basic data structures of file system
  2. File cache
  3. File allocation
  4. Example of file access process

The position of file system in kernel

The layered structure of file system

The layered structure of file system in computer system

The user view and kernel view of file system

Virtual file system (VFS)

  • Defines a set of standard data structures and interfaces that are supported by all file systems
  • Disk-based file systems: store data directly on the disk, such as Ext2/3/4, XFS
  • Memory-based file systems: memory-aided data structures, such as directory entries

The functions of virtual file system

  • Purpose: provide the abstraction of different file systems
  • Functions
    • Provide the same file and file system interface
    • Manage the data structures associated with files and file systems
    • Efficient query, traverse file systems
    • Interact with specific file system modules

The Virtual File System unifies the access interfaces of different file systems

Outline

  1. Overview

2. Basic data structures of file system

  1. File cache
  2. File allocation
  3. Example of file access process

The storage view of file system

  • Volume control block (superblock)
  • File control block ( inode/vnode)
  • Directory entry (dir_entry)
  • Data block (data block)

The organization view of file system

Basic data structures of file system

Volume control block (superblock)

One volume control block for each file system

  • Detailed information about the file system
  • Block size, number of free blocks, etc
  • Total number of blocks and inodes, number of unused and used ones
  • The time of a file system was mounted, last write time, and last disk check (fsck) time

File control block (inode)

Each file has an inode (inode/vnode)

  • Size, data block position (points to one or more data blocks)
  • Access mode (read/write/excute)
  • Owner and group
  • Time information: time of creation or status change, time of last read/modified
  • File name in the directory's data block

Bitmap block

Bitmap block ( bitmap inode/dnode)

  • Flag indicating whether inode is used or unused
  • Flag indicating whether dnode is used or unused

Data block (dnode)

  • Data blocks for directories and files
    • Contains directory and file contents
    • Determining the fixed size of a data block during volume formatting
    • Each block has a number for the reference of inode
    • The size of an inode is generally 128B
    • The size of a data block is generally 4KB

Data block of directory

Directory entry (dir_entry)

  • Directory entries are generally cached in memory
    • Each directory has a memory cache (linking to directories or files)
    • The data structure of a directory entry and the tree layout are encoded into a tree data structure
    • Point to the file control block, parent directory, child directory, etc.

Outline

  1. Overview
  2. Basic data structures of file system

3. File cache

  1. File allocation
  2. Example of file access process

Multiple disk cache locations

Data block cache

  • Data blocks are read into memory on demand

    • read() operation
    • Prefetch: Pre-read next data blocks
  • Data blocks are cached after use

    • Assume data will be used again
    • Write operations may be cached and delayed

    Page cache: keep the cached data blocks and memory pages consistent

Virtual page storage -- page cache

Virtual pages in the virtual address space can be mapped to local external storage files

Virtual page storage -- page cache

Virtual pages in the virtual address space can be mapped to local disk (external storage) files

  • Page cache for file data blocks
    • File data blocks are mapped into pages in virtual memory
    • File read/write operations are converted into accesses to memory
    • May result in page faults and/or dirty pages
  • Issue: Page replacement algorithms need to coordinate the number of pages between virtual memory and page cache

File descriptor

  • Each opened file has a file descriptor
  • As an index, it points to corresponding file status information

Open file table

  • Each process has an open file table
  • A system-wide open file table

File lock

Some file systems provide file locks to coordinate file access by multiple processes

  • Mandatory – Access is denied based on lock hold status and access requirements
  • Advisory - Processes can look up the lock status to decide what to do

Outline

  1. Overview
  2. Basic data structures of file system
  3. File cache

4. File allocation

  1. Example of file access process

File size

  • Most files are small
    • Need to support small files
    • Data block space cannot be too large
  • Some files are very large
    • Can support large files
    • Read and write efficiently

File allocation

Allocate file data blocks

  • Allocation methods
    • Continuous allocation
    • Linked allocation
    • Indexed assignment
  • Evaluation criteria
    • Storage efficiency: external fragmentation, etc
    • Read/write performance: access speed

Continuous allocation

The file header specifies the starting block and length

  • Allocation strategy: first fit, best fit, etc
  • Pros:
    • Efficient sequential and random read
  • Cons
    • Frequent allocation leads to fragmentation; large overhead for adding file content

Linked allocation

Data blocks are stored in a linked list

  • Pros: easy to create and scale; almost no fragmentation
  • Cons:
    • Low efficiency for random access; poor reliability
    • If a link is broken, the subsequent data blocks are lost

Linked allocation

  • Explicit connection
  • Implicit connection

Indexed allocation

  • The file header contains a pointer to the index data block
  • The index of the index data block is the pointer to the file data block
  • Pros
    • Easy to create and scale; almost no fragmentation; supports direct access
  • Cons
    • When the file is small, the storage overhead of index is relatively large

How to store large files?

Indexed allocation

  • Linked index block (IB+IB+…)
  • Multi-level index block (IB*IB *…)

Indexed allocation

Multi-level index allocation

Multi-level index allocation

  • The file header contains 13 pointers
    • 10 pointers point to data blocks
    • The 11th pointer points to an index block
    • The 12th pointer points to a second-level index block
    • The 13th pointer points to a third-level index block

Accessing data blocks in large files requires a large number of queries

Comparison of file allocation methods

Free space management

Track and record free data blocks in a volume: data structure?

  • Bitmap: use a bitmap to denote the list of free data blocks
    • 11111111001110101011101111...
    • Di=0D_i = 0 indicates that data block ii is free, otherwise, it is allocated
    • 160GB disk --> 40M data block --> 5MB bitmap
    • Assuming free space is evenly distributed on the disk,
      • n/r data blocks need to be scanned before finding a "0"
        • n = the total number of data blocks on the disk; r = the number of free blocks

Free space management

  • Linked list
  • Index

Outline

  1. Overview
  2. Basic data structures of file system
  3. File cache
  4. File allocation

5. Example of file access process

Example of file system organization

File reading process

File writing process

File system partition

  • Most disks are divided into one or more partitions, each with an independent file system

Summary

  1. Overview
  2. Basic data structures of file system
  3. File cache
  4. File allocation
  5. Example of file access process

![bg right 10% 50%](figs/efs-inode.png)

--- #### Design and implementation of the file system -- cache Virtual page storage -- page cache - Virtual pages in the virtual address space can be mapped to local external memory files - Page cache for file data blocks - File data blocks are mapped into pages in virtual memory - File read/write operations are converted to memory accesses - May result in page faults and/or dirty pages - Problem: The page replacement algorithm needs to coordinate the number of pages between the virtual storage and the page cache