Overall Organization

  1. On-Disk Organization

    • a series of blocks (e.g. size of each block is 4KB).
    • the blocks are addressed from 0 to N-1.
  2. File System Organization

    1. Metadata Region: tracks data and file system information

      1. 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).
      2. 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
      3. 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).
    2. Data Region: stores user data and occupies most space

File Organization

Inode

  1. Inode

    • Contains all the information about a file.
    • e.g. file type, size, protection information, time information, etc.
  2. 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.
  3. 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.

Multi-Level Index

  1. 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.
  2. Double Indirect Pointer: points to pointers to indirect blocks

    • The maximum file size = (12 + 1024 $\times$ 1024) $\times$ 4KB $\approx$ 1 million 4KB blocks.
  3. 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

  1. Directory

    • Directory contains a list of (entry name, inode number) pairs.
    • Each directory has two extra file:
      • . : current directory
      • .. : parent directory
    • For example: dir has three files : foo, bar, foobar_is_a_pretty_longname.
  2. 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)
    1. Traverse the pathname and thus locate the desired inode.
    2. Begin at the root of the file system (/).
    3. File system reads in the block that contains inode number 2 (the root inode number in most Unix file system).
    4. Look inside of the content of root to find pointer to data blocks.
    5. By reading in one or more directory data block, and find “foo” directory finally.
    6. Tranverse recursively the path name until the desired inode (“bar”).
    7. Check the finale permissions (權限), allocate a file descriptor for this process and return the file descriptor.
  • read() and close()

    1. 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).
    2. When the file is closed, the file descriptor should be disallocated.
      • NO disk I/Os take place.
  • 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:
      1. read the data bitmap
      2. write the data bitmap (to reflect its new state to disk)
      3. read the inode
      4. write the inode
      5. 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

  1. 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).
  2. Solutions:

    1. 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.
    2. 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.