468,554 Members | 1,862 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Understanding Ext-2 file system:chapter 2

542 Expert 512MB
As per the last discussion(chapter 1), here we'll try to read the group descriptor.
First of all we'll try to understand what is group descriptor.
AS we know, superblock and group descriptor table are duplicated in each block group.
Group descriptor table is an array of group desciptors
Each block group has it's own group descriptor. And it is stored in group discriptor table in sequential manner.
In other word each block group has all group descriptors, as descriptor table is copied in each block group.
That means if we are looking at block group 3, then we can find it's group descriptor at 3 position in descriptor table.
In this article we'll retrieve the first group descriptor structure from table. So without any doubt it will represent the first block group.

Let's see what information can single group descriptor contain:

bg_block_bitmap: Block number of block bitmap
bg_inode_bitmap: Block number of inode bitmap
bg_inode_table: Block number of first inode table block
bg_free_blocks_count: Number of free blocks in the group
bg_free_inodes_count: Number of free inodes in the group
bg_used_dirs_count: Number of directories in the group
bg_pad: Alignment to word
bg_reserved: Nulls to pad out 24 bytes

Group descriptor is represented in linux kernel as "ext2_group_desc" structure.
Abouve fields belongs to same structure.

As we can see from above information, It will give us all information for it's block group.

Lets see how we can code to retrive the group descriptor structure.

Very first block is boot block:1024 B
Later resides superblock
We are assuming that block size is 4096.
After 4096 B group descriptor table can be find out.
So we will open any ext2 partition and seek 4096 bytes, read the first group descriptor.

Expand|Select|Wrap|Line Numbers
  2.     #include<linux/ext2_fs.h>
  3.     #include<sys/types.h>
  4.     #include<sys/stat.h>
  5.     #include<stdio.h>
  6.     #include<unistd.h>
  7.     #include<fcntl.h>
  8.     #include<stdlib.h>
  9.     #include<string.h>
  11.     int main()
  12.     {
  13.         char *buff = (char *)malloc(sizeof(struct ext2_group_desc));
  15.         struct ext2_group_desc * gdesc = (struct ext2_group_desc *)malloc(sizeof(struct ext2_group_desc));
  17.         //open any partition for testing,must be ext2/ext3.
  18.         int fd = open("/dev/hda3",O_RDONLY);
  20.         //skip the boot block and super block
  21.         lseek(fd,4096,SEEK_CUR);
  23.         //read the raw data from disk to buff
  24.         read(fd,buff,sizeof(struct ext2_group_desc));
  26.         //copy buffer to gdesc, you can use casting or union for this.
  27.         memcpy((void *)gdesc,(void *)buff,sizeof(struct ext2_group_desc));
  29.         //At this position you can be assured that you read group descriptor successfully.            
  30.         close(fd);    
  32.         return 0;
  33.     }
Feb 20 '08 #1
0 5450

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Member | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
19 posts views Thread by rdavis7408 | last post: by
3 posts views Thread by Redefined Horizons | last post: by
263 posts views Thread by Malcolm McLean | last post: by
139 posts views Thread by ravi | last post: by
5 posts views Thread by Bob Nelson | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.