剖析 linux file system-Ext2

来源:互联网 发布:老虎证券 客户端软件 编辑:程序博客网 时间:2024/05/29 17:05

The ext2 filesystem was developed by Rémy Card and added to Linux in version 0.99pl7 (March 1993). It was a greatly improved version of his earlier ext filesystem (that again was a small improvement on the minix filesystem), and uses ideas from the Berkeley Fast Filesystem. It is really fast and robust. The main reason people want something else nowadays is that on the modern very large disks an invocation of e2fsck (to check filesystem integrity after a crash or power failure, or just after some predetermined number of boots) takes a long time, like one hour.

The structure of ext2 can be read in the kernel source. The data structures are defined in ext2_fs.h,ext2_fs_i.hext2_fs_sb.h (in include/linux) and the code is in fs/ext2

The superblock:

struct ext2_super_block {        unsigned long s_inodes_count;     /* Inodes count */        unsigned long s_blocks_count;     /* Blocks count */        unsigned long s_r_blocks_count;   /* Reserved blocks count */        unsigned long s_free_blocks_count;/* Free blocks count */        unsigned long s_free_inodes_count;/* Free inodes count */        unsigned long s_first_data_block; /* First Data Block */        unsigned long s_log_block_size;   /* log(Block size) - 10 */        long s_log_frag_size;             /* Fragment size */        unsigned long s_blocks_per_group; /* # Blocks per group */        unsigned long s_frags_per_group;  /* # Fragments per group */        unsigned long s_inodes_per_group; /* # Inodes per group */        unsigned long s_mtime;          /* Mount time */        unsigned long s_wtime;          /* Write time */        unsigned long s_pad;            /* Padding to get the magic signature*/                                        /* at the same offset as in the */                                        /* previous ext fs */        unsigned short s_magic;         /* Magic signature */        unsigned short s_valid;         /* Flag */        unsigned long s_reserved[243];  /* Padding to the end of the block */};
The superblock contains information that is global to the entire filesystem,such as the total numbel of blosks ,and the time of  the last mount.Some parameters,such as the number of blocks reserved for the superuser.
The group descriptors:
Then the group descriptors.Each block group has a group descriptor,and all group  descriptors for all block groups are repeated in each group.All copies except thr first of superblock and group descriptor are never used or updated.They are just there to help recovering from a crash or power failure.
The size of a block group depends on the chosen block size for the ext2 filesystem.Allowed block size are 1024/2048/4096.The blocks of a block grou are represented by bits in a bitmap that file one block.
 struct ext2_group_desc{

        unsigned long bg_block_bitmap;          /* Blocks bitmap block */        unsigned long bg_inode_bitmap;          /* Inodes bitmap block */        unsigned long bg_inode_table;           /* Inodes table block */        unsigned short bg_free_blocks_count;    /* Free blocks count */        unsigned short bg_free_inodes_count;    /* Free inodes count */        unsigned short bg_used_dirs_count;      /* Directories count */        unsigned short bg_pad;        unsigned long bg_reserved[3];
}; 

原创粉丝点击