0

I am trying to read super block, group descriptor and inodes struct info of ext2 formatted disk image. So I like to know can I access these info_s with my userspace structures? So I like to create struct_s that maps to ext2 struct namely these struct_s.

    struct ext2_group_desc
    {
        __le32  bg_block_bitmap;        /* Blocks bitmap block */
        __le32  bg_inode_bitmap;        /* Inodes bitmap block */
        __le32  bg_inode_table;     /* Inodes table block */
        __le16  bg_free_blocks_count;   /* Free blocks count */
        __le16  bg_free_inodes_count;   /* Free inodes count */
        __le16  bg_used_dirs_count; /* Directories count */
        __le16  bg_pad;
        __le32  bg_reserved[3];
    };

struct ext2_sb_info {
    unsigned long s_frag_size;  /* Size of a fragment in bytes */
    unsigned long s_frags_per_block;/* Number of fragments per block */
    unsigned long s_inodes_per_block;/* Number of inodes per block */
    unsigned long s_frags_per_group;/* Number of fragments in a group */
    unsigned long s_blocks_per_group;/* Number of blocks in a group */
    unsigned long s_inodes_per_group;/* Number of inodes in a group */
    unsigned long s_itb_per_group;  /* Number of inode table blocks per group */
    unsigned long s_gdb_count;  /* Number of group descriptor blocks */
    unsigned long s_desc_per_block; /* Number of group descriptors per block */
    unsigned long s_groups_count;   /* Number of groups in the fs */
    unsigned long s_overhead_last;  /* Last calculated overhead */
    unsigned long s_blocks_last;    /* Last seen block count */
    struct buffer_head * s_sbh; /* Buffer containing the super block */
    struct ext2_super_block * s_es; /* Pointer to the super block in the buffer */
    struct buffer_head ** s_group_desc;
    unsigned long  s_mount_opt;
    unsigned long s_sb_block;
    kuid_t s_resuid;
    kgid_t s_resgid;
    unsigned short s_mount_state;
    unsigned short s_pad;
    int s_addr_per_block_bits;
    int s_desc_per_block_bits;
    int s_inode_size;
    int s_first_ino;
    spinlock_t s_next_gen_lock;
    u32 s_next_generation;
    unsigned long s_dir_count;
    u8 *s_debts;
    struct percpu_counter s_freeblocks_counter;
    struct percpu_counter s_freeinodes_counter;
    struct percpu_counter s_dirs_counter;
    struct blockgroup_lock *s_blockgroup_lock;
    /* root of the per fs reservation window tree */
    spinlock_t s_rsv_window_lock;
    struct rb_root s_rsv_window_root;
    struct ext2_reserve_window_node s_rsv_window_head;
    /*
     * s_lock protects against concurrent modifications of s_mount_state,
     * s_blocks_last, s_overhead_last and the content of superblock's
     * buffer pointed to by sbi->s_es.
     *
     * Note: It is used in ext2_show_options() to provide a consistent view
     * of the mount options.
     */
    spinlock_t s_lock;
    struct mb_cache *s_ea_block_cache;
    struct dax_device *s_daxdev;
};

also what other struct I must include ?

and one for inode, and rest of the structures representations in ext2 formatted filesystem image. So I assume there is an offset in image mounted with ext2 that can be used to pull the superblock and different offset to pull rest of the structs (group descriptors and inodes,dentry,etc)

So the first challenge I faced is that there is lack of info on alternatives to types like __le32 etc. So question is how can I convert kernel ext2 filesystem structs to user space implementation. So I can read these info_s in my program. The program is just a information tool that I like to work on. Can anyone please tell me this. Thanks for helping

user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    I think you should be using libext2fs. – Ian Abbott Dec 13 '21 at 15:25
  • @IanAbbott is there any doc on offset values of super blocks group descriptor and inodes of ext2 – user786 Dec 13 '21 at 15:39
  • There isn't much documentation apart from a GNU-Info format manual that describes some of the functions. For the rest, you'll need to figure it out from the source code. Note, the source code for libext2fs is part of the e2fsprogs source code. – Ian Abbott Dec 13 '21 at 16:29
  • `__le32` is just a 32-bit unsigned integer type. So you could define it via `typedef uint32_t __le32`. Note, that for interpret value of this type you need to convert it from little-endian to natural byte order. See e.g. [that answer](https://stackoverflow.com/a/6961239/3440745). – Tsyvarev Dec 13 '21 at 17:16
  • @IanAbbott can u please tell what is super_block->s_log_block_size is this the size I need to increase as files/directories are added to file system and decrease as files are removed from the file system. Can u please tell me this also – user786 Dec 14 '21 at 06:52
  • `super_block->s_log_block_size` is 10 less than the logarithm to the base 2 of the block size. So the block size is `1u << (super_block->s_log_block_size + 10)` or `1024u << super_block_size`. – Ian Abbott Dec 14 '21 at 10:00
  • @IanAbbott also number of group descriptor can be found with `unsigned int group_count = 1 + (super.s_blocks_count-1) / super.s_blocks_per_group;` and the offset of first group descriptor struct in ext2 is 1024+1024 escaping boot section and super block struct space. the Question is what would be the offset of second group descriptor if my storage had multiple group descriptors. Can u please tell me this only – user786 Dec 14 '21 at 10:29
  • from this link it says this `As it turns out, the Group Descriptor block contains group descriptors for each group, added one after another.` https://stackoverflow.com/questions/27236451/ext2-group-descriptors – user786 Dec 14 '21 at 10:33
  • and the group descriptor block is skipping 1024+1024 block offset. so third block is group descriptor block. how many block descriptors can there be does it depend on file population on device. means more files more number of group descriptors to cover all the inodes in inode bitmap – user786 Dec 14 '21 at 10:36
  • There is an online book about the ext2 filesystem here (it also covers ext3 and ext4 features a bit): [The Second Extended File System](https://www.nongnu.org/ext2-doc/ext2.html) – Ian Abbott Dec 14 '21 at 10:53

0 Answers0