Linux ext2 file system

Ext2

The Second Extended Filesystem (ext2fs) is a rewrite of the original Extended Filesystem and as such, is also based around the concept of «inodes.» Ext2 served as the de facto filesystem of Linux for nearly a decade from the early 1990s to the early 2000s when it was superseded by the journaling file systems ext3 and ReiserFS. It has native support for UNIX ownership / access rights, symbolic- and hard-links, and other properties that are common among UNIX-like operating systems. Organizationally, it divides disk space up into groups called «block groups.» Having these groups results in distribution of data across the disk which helps to minimize head movement as well as the impact of fragmentation. Further, some (if not all) groups are required to contain backups of important data that can be used to rebuild the file system in the event of disaster.

Note: Most of the information here is based off of work done by Dave Poirier on the ext2-doc project (see the links section) which is graciously released under the GNU Free Documentation License. Be sure to buy him a beer the next time you see him.

Contents

  • 1 Basic Concepts
    • 1.1 What is a Block?
    • 1.2 What is a Block Group?
    • 1.3 What is an Inode?
    • 2.1 Locating the Superblock
    • 2.2 Determining the Number of Block Groups
    • 2.3 Base Superblock Fields
      • 2.3.1 File System States
      • 2.3.2 Error Handling Methods
      • 2.3.3 Creator Operating System IDs
      • 2.4.1 Optional Feature Flags
      • 2.4.2 Required Feature Flags
      • 2.4.3 Read-Only Feature Flags
      • 3.1 Locating the Block Group Descriptor Table
      • 3.2 Block Group Descriptor
      • 4.1 Determining which Block Group contains an Inode
      • 4.2 Finding an inode inside of a Block Group
      • 4.3 Reading the contents of an inode
      • 4.4 Inode Data Structure
        • 4.4.1 Inode Type and Permissions
        • 4.4.2 Inode Flags
        • 4.4.3 OS Specific Value 1
        • 4.4.4 OS Specific Value 2
        • 4.6.1 Directory Entry Type Indicators
        • 5.1 How To Read An Inode
        • 5.2 How To Read the Root Directory
        • 6.1 External Links

        Basic Concepts

        Important Note: All values are little-endian unless otherwise specified

        What is a Block?

        The Ext2 file system divides up disk space into logical blocks of contiguous space. The size of blocks need not be the same size as the sector size of the disk the file system resides on. The size of blocks can be determined by reading the field starting at byte 24 in the Superblock.

        What is a Block Group?

        Blocks, along with inodes, are divided up into «block groups.» These are nothing more than contiguous groups of blocks.

        Each block group reserves a few of its blocks for special purposes such as:

        • A bitmap of free/allocated blocks within the group
        • A bitmap of allocated inodes within the group
        • A table of inode structures that belong to the group
        • Depending upon the revision of Ext2 used, some or all block groups may also contain a backup copy of the Superblock and the Block Group Descriptor Table.

        What is an Inode?

        An inode is a structure on the disk that represents a file, directory, symbolic link, etc. Inodes do not contain the data of the file / directory / etc. that they represent. Instead, they link to the blocks that actually contain the data. This lets the inodes themselves have a well-defined size which lets them be placed in easily indexed arrays. Each block group has an array of inodes it is responsible for, and conversely every inode within a file system belongs to one of such tables (and one of such block groups).

        Superblock

        The first step in implementing an Ext2 driver is to find, extract, and parse the superblock. The Superblock contains all information about the layout of the file system and possibly contains other important information like what optional features were used to create the file system. Once you have finished with the Superblock, the next step is to look at the Block Group Descriptor Table

        Locating the Superblock

        The Superblock is always located at byte 1024 from the beginning of the volume and is exactly 1024 bytes in length. For example, if the disk uses 512 byte sectors, the Superblock will begin at LBA 2 and will occupy all of sector 2 and 3.

        Determining the Number of Block Groups

        From the Superblock, extract the size of each block, the total number of inodes, the total number of blocks, the number of blocks per block group, and the number of inodes in each block group. From this information we can infer the number of block groups there are by:

        • Rounding up the total number of blocks divided by the number of blocks per block group
        • Rounding up the total number of inodes divided by the number of inodes per block group
        • Both (and check them against each other)

        Base Superblock Fields

        These fields are present in all versions of Ext2

        File System States

        Error Handling Methods

        >>»>
        Value Operating System
        0 Linux
        1 GNU HURD
        2 MASIX (an operating system developed by Rémy Card, one of the developers of ext2)
        3 FreeBSD
        4 Other «Lites» (BSD4.4-Lite derivatives such as NetBSD, OpenBSD, XNU/Darwin, etc.)

        Extended Superblock Fields

        These fields are only present if Major version (specified in the base superblock fields), is greater than or equal to 1.

        Optional Feature Flags

        These are optional features for an implementation to support, but offer performance or reliability gains to implementations that do support them.

        >>»>

        Flag Value Description
        0x0001 Compression is used
        0x0002 Directory entries contain a type field
        0x0004 File system needs to replay its journal
        0x0008 File system uses a journal device

        Read-Only Feature Flags

        These features, if present on a file system, are required in order for an implementation to write to the file system, but are not required to read from the file system.

        Источник

        Читайте также:  Tar резервное копирование linux
        Оцените статью
        Adblock
        detector