Next Previous Contents

12. The superblock

The superblock is a block which contains information which describes the state of the internal filesystem.

The superblock is located at the fixed offset 1024 in the device. Its length is 1024 bytes also.

The superblock, like the group descriptors, is copied on each blocks group boundary for backup purposes. However, only the main copy is used by the kernel.

The superblock contain three types of information:

Follows the superblock definition:

struct ext2_super_block {
        __u32   s_inodes_count;         /* Inodes count */
        __u32   s_blocks_count;         /* Blocks count */
        __u32   s_r_blocks_count;       /* Reserved blocks count */
        __u32   s_free_blocks_count;    /* Free blocks count */
        __u32   s_free_inodes_count;    /* Free inodes count */
        __u32   s_first_data_block;     /* First Data Block */
        __u32   s_log_block_size;       /* Block size */
        __s32   s_log_frag_size;        /* Fragment size */
        __u32   s_blocks_per_group;     /* # Blocks per group */
        __u32   s_frags_per_group;      /* # Fragments per group */
        __u32   s_inodes_per_group;     /* # Inodes per group */
        __u32   s_mtime;                /* Mount time */
        __u32   s_wtime;                /* Write time */
        __u16   s_mnt_count;            /* Mount count */
        __s16   s_max_mnt_count;        /* Maximal mount count */
        __u16   s_magic;                /* Magic signature */
        __u16   s_state;                /* File system state */
        __u16   s_errors;               /* Behaviour when detecting errors */
        __u16   s_pad;
        __u32   s_lastcheck;            /* time of last check */
        __u32   s_checkinterval;        /* max. time between checks */
        __u32   s_creator_os;           /* OS */
        __u32   s_rev_level;            /* Revision level */
        __u16   s_def_resuid;           /* Default uid for reserved blocks */
        __u16   s_def_resgid;           /* Default gid for reserved blocks */
        __u32   s_reserved[235];        /* Padding to the end of the block */

12.1 superblock identification

The ext2 filesystem's superblock is identified by the s_magic field. The current ext2 magic number is 0xEF53. I presume that "EF" means "Extended Filesystem". In versions of the ext2 filesystem prior to 0.2B, the magic number was 0xEF51. Those filesystems are not compatible with the current versions; Specifically, the group descriptors definition is different. I doubt if there still exists such a installation.

12.2 Filesystem fixed parameters

By using the word fixed, I mean fixed with respect to a particular installation. Those variables are usually not fixed with respect to different installations.

The block size is determined by using the s_log_block_size variable. The block size is 1024*pow (2,s_log_block_size) and should be between 1024 and 4096. The available options are 1024, 2048 and 4096.

s_inodes_count contains the total number of available inodes.

s_blocks_count contains the total number of available blocks.

s_first_data_block specifies in which of the device block the superblock is present. The superblock is always present at the fixed offset 1024, but the device block numbering can differ. For example, if the block size is 1024, the superblock will be at block 1 with respect to the device. However, if the block size is 4096, offset 1024 is included in block 0 of the device, and in that case s_first_data_block will contain 0. At least this is how I understood this variable.

s_blocks_per_group contains the number of blocks which are grouped together as a blocks group.

s_inodes_per_group contains the number of inodes available in a group block. I think that this is always the total number of inodes divided by the number of blocks groups.

s_creator_os contains a code number which specifies the operating system which created this specific filesystem:

s_rev_level contains the major version of the ext2 filesystem. Currently this is always 0, as the most recent version is 0.5B. It will probably take some time until we reach version 1.0.

As far as I know, fragments (sub-block allocations) are currently not supported and hence a block is equal to a fragment. As a result, s_log_frag_size and s_frags_per_group are always equal to s_log_block_size and s_blocks_per_group, respectively.

12.3 Ext2fs error handling

The ext2 filesystem error handling is based on the following philosophy:

  1. Identification of problems is done by the kernel code.
  2. The correction task is left to an external utility, such as e2fsck by Theodore Ts'o for automatic analysis and correction, or perhaps debugfs by Theodore Ts'o and EXT2ED by myself, for hand analysis and correction.

The s_state variable is used by the kernel to pass the identification result to third party utilities:

The kernel behavior when an error is found is determined by the user tunable parameter s_errors:

The default behavior is to ignore the error.

12.4 Additional parameters used by e2fsck

Of-course, e2fsck will check the filesystem if errors were detected or if the filesystem is not clean.

In addition, each time the filesystem is mounted, s_mnt_count is incremented. When s_mnt_count reaches s_max_mnt_count, e2fsck will force a check on the filesystem even though it may be clean. It will then zero s_mnt_count. s_max_mnt_count is a tunable parameter.

E2fsck also records the last time in which the file system was checked in the s_lastcheck variable. The user tunable parameter s_checkinterval will contain the number of seconds which are allowed to pass since s_lastcheck until a check is reforced. A value of 0 disables time-based check.

12.5 Additional user tunable parameters

s_r_blocks_count contains the number of disk blocks which are reserved for root, the user whose id number is s_def_resuid and the group whose id number is s_deg_resgid. The kernel will refuse to allocate those last s_r_blocks_count if the user is not one of the above. This is done so that the filesystem will usually not be 100% full, since 100% full filesystems can affect various aspects of operation.

s_def_resuid and s_def_resgid contain the id of the user and of the group who can use the reserved blocks in addition to root.

12.6 Filesystem current state

s_free_blocks_count contains the current number of free blocks in the filesystem.

s_free_inodes_count contains the current number of free inodes in the filesystem.

s_mtime contains the time at which the system was last mounted.

s_wtime contains the last time at which something was changed in the filesystem.

Next Previous Contents