Understanding Ext-2 file system:chapter 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    Understanding Ext-2 file system:chapter 1

    Understanding Ext-2 file system:chapter 1

    The first block in each Ext2 partition is never managed by the Ext2 filesystem,
    because it is reserved for the partition boot sector.
    The rest of the Ext2 partition is split into block groups,
    each of which has the layout shown in Image 2.
    As you will notice from the figure, some data structures must fit in exactly one block,
    while others may require more than one block.
    All the block groups in the filesystem have the same size and are stored sequentially,
    thus the kernel can derive the location of a block group in a disk simply from its integer index.



    ext2 partition.

    | Boot Block | Block Group 0 | - - - - - | Block Group n |


    Single ext2 block group.

    | Super Block | Group Descriptors | Data Block Bitmap | Inode Bitmap | Inode Table | Data Blocks |


    As you can see Super Block and Group Descriptors are duplicated in each block group.
    This is done because to maintain the redundancy. If any crash occures,we can easily replace the super block
    and group descriptor from some other partition.

    In this article we'll descuss super block according to programming point of view.
    We'll see how to read the super block.
    Ofcourse there are library called libext2fs which can give us all above data structure easily.
    But don't forget the title of this article, we are about to understand the things inside ext2.
    In further documents I'll explore other data structures too.

    An Ext2 disk superblock is stored in an ext2_super_bloc k structure, whose fields are listed below

    s_inodes_count : Total number of inodes
    s_blocks_count : Filesystem size in blocks
    s_r_blocks_coun t : Number of reserved blocks
    s_free_blocks_c ount : Free blocks counter
    s_free_inodes_c ount : Free inodes counter
    s_first_data_bl ock : Number of first useful block (always 1)
    s_log_block_siz e : Block size
    s_log_frag_size : Fragment size
    s_blocks_per_gr oup : Number of blocks per group
    s_frags_per_gro up : Number of fragments per group
    s_inodes_per_gr oup : Number of inodes per group
    s_mtime : Time of last mount operation
    s_wtime : Time of last write operation
    s_mnt_count : Mount operations counter
    s_max_mnt_count : Number of mount operations before check
    s_magic : Magic signature
    s_state : Status flag
    s_errors : Behavior when detecting errors
    s_minor_rev_lev el : Minor revision level
    s_lastcheck : Time of last check
    s_checkinterval : Time between checks
    s_creator_os : OS where filesystem was created
    s_rev_level : Revision level of the filesystem
    s_def_resuid : Default UID for reserved blocks
    s_def_resgid : Default user group ID for reserved blocks
    s_first_ino : Number of first nonreserved inode
    s_inode_size : Size of on-disk inode structure
    s_block_group_n r : Block group number of this superblock
    s_feature_compa t : Compatible features bitmap
    s_feature_incom pat : Incompatible features bitmap
    s_feature_ro_co mpat : Read-only compatible features bitmap
    s_uuid : 128-bit filesystem identifier
    s_volume_name : Volume name
    s_last_mounted : Pathname of last mount point
    s_algorithm_usa ge_bitmap : Used for compression
    s_prealloc_bloc ks : Number of blocks to preallocate
    s_prealloc_dir_ blocks : Number of blocks to preallocate for directories
    s_padding1 : Alignment to word
    s_reserved : Nulls to pad out 1,024 bytes

    Very small structure isn't it?

    Now we'll see the code to access the super block.

    Code:
            #include<linux/ext2_fs.h>
    	#include<sys/types.h>
    	#include<sys/stat.h>
    	#include<stdio.h>
    	#include<unistd.h>
    	#include<fcntl.h>
    	#include<stdlib.h>
    	#include<string.h>
    
    	#define boot_block_size 1024
    
    	int main()
    	{
    		char *buff = (char *)malloc(sizeof(struct ext2_super_block));
    		
    		struct ext2_super_block * sblock = (struct ext2_super_block *)malloc(sizeof(struct ext2_super_block));
    		
    		//open any partition for testing,must be ext2/ext3.
    		int fd = open("/dev/hda3",O_RDONLY);
    		
    		//skip the boot block
    		lseek(fd,boot_block_size,SEEK_CUR);
    		
    		//read the superblock raw data from disk to buff
    		read(fd,buff,sizeof(struct ext2_super_block));
    		
    		//copy buffer to sblock, you can use casting or union for this.
    		memcpy((void *)sblock,(void *)buff,sizeof(struct ext2_super_block));
    		
    		printf("\nmagic number:%u\n",sblock->s_magic);
    		
    		close(fd);	
    
    		return 0;
    	}
    If you get the magic number as 61267. you read super block successfully.
    I'll show you how to read group descriptors in next article
  • pkulkarni
    New Member
    • Dec 2008
    • 1

    #2
    Very Informative! I liked it

    Comment

    • deepakcoep
      New Member
      • Jul 2009
      • 4

      #3
      hi

      when i run the code(reading super block)
      i got following error
      will u please help me out for the same.


      #gcc readSB.c
      In file included from /usr/include/stdlib.h:438,
      from readSB.c:4:
      /usr/include/sys/types.h:46: error: conflicting types for ‘loff_t’
      /usr/include/linux/types.h:30: error: previous declaration of ‘loff_t’ was here
      /usr/include/sys/types.h:62: error: conflicting types for ‘dev_t’
      /usr/include/linux/types.h:13: error: previous declaration of ‘dev_t’ was here
      In file included from /usr/include/sys/types.h:133,
      from /usr/include/stdlib.h:438,
      from readSB.c:4:
      /usr/include/time.h:105: error: conflicting types for ‘timer_t’
      /usr/include/linux/types.h:22: error: previous declaration of ‘timer_t’ was here
      In file included from /usr/include/stdlib.h:438,
      from readSB.c:4:
      /usr/include/sys/types.h:198: error: conflicting types for ‘int64_t’
      /usr/include/linux/types.h:98: error: previous declaration of ‘int64_t’ was here
      /usr/include/sys/types.h:204: error: conflicting types for ‘u_int64_t’
      /usr/include/linux/types.h:97: error: previous declaration of ‘u_int64_t’ was here
      In file included from /usr/include/sys/types.h:220,
      from /usr/include/stdlib.h:438,
      from readSB.c:4:
      /usr/include/sys/select.h:78: error: conflicting types for ‘fd_set’
      /usr/include/linux/types.h:12: error: previous declaration of ‘fd_set’ was here
      In file included from /usr/include/stdlib.h:438,
      from readSB.c:4:
      /usr/include/sys/types.h:235: error: conflicting types for ‘blkcnt_t’
      /usr/include/linux/types.h:114: error: previous declaration of ‘blkcnt_t’ was here

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Which kernel version you are using?
        Do you have the kernel-headers installed for the same kernel?

        Comment

        • deepakcoep
          New Member
          • Jul 2009
          • 4

          #5
          hi
          above error are corrected by rearrenging the header files.
          do you know how to get find one perticular file.
          means i have name of file.
          i have to proceed like this
          superblock->inode bock->datablock
          now i had read the superblock.
          which value from the superblock content give me link to inode table.
          means how i get access to inode table vai the superblock either directly or indirectly via other way(i only have superblock content and file name).

          thanks

          Comment

          • ashitpro
            Recognized Expert Contributor
            • Aug 2007
            • 542

            #6
            Deepak,
            My suggestion would be, stop writing further code.
            It would be very difficult for you to get the inode and access the file with normal way. You better start looking at 'libext2fs' and its API's.
            I'd already mentioned that, you could read your fs data structures with this library.
            I didn't post any code using this library cause, more or less it does the work with function calls.

            Regards,
            Ash

            Comment

            • deepakcoep
              New Member
              • Jul 2009
              • 4

              #7
              hi ash
              I wanted to know that is there any value in the super block of ext2 fs which give me start address of inode block.
              means superblock contain various details, can i either directly or by manupulation i get starting address of inode block
              i dont want to use any external library
              thanks

              Comment

              • deepakcoep
                New Member
                • Jul 2009
                • 4

                #8
                Hi ash
                I got the way to find file using the file name and super block
                following approch i used
                super block gives the size of itself
                FD Block is next to the Super block
                fd give me start of Inode block
                from inode block i can read the inode structure of root inode(2)
                which gives me start and lenght of file
                thats all
                thanks for help ash
                bye

                Comment

                • labandus
                  New Member
                  • Nov 2010
                  • 1

                  #9
                  Hi guys...

                  Directories(-entries) are in a EXT2 file system
                  managed in a singly linked list.
                  Delete files in the directory causes Gaps or holes to appear in the linked list of the directory.How does a C-source code look like,which would reorganize this list and remove the gaps or Holes.

                  thank you very much for your help.

                  Comment

                  Working...