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
- #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>
- int main()
- {
- char *buff = (char *)malloc(sizeof(struct ext2_group_desc));
- struct ext2_group_desc * gdesc = (struct ext2_group_desc *)malloc(sizeof(struct ext2_group_desc));
- //open any partition for testing,must be ext2/ext3.
- int fd = open("/dev/hda3",O_RDONLY);
- //skip the boot block and super block
- lseek(fd,4096,SEEK_CUR);
- //read the raw data from disk to buff
- read(fd,buff,sizeof(struct ext2_group_desc));
- //copy buffer to gdesc, you can use casting or union for this.
- memcpy((void *)gdesc,(void *)buff,sizeof(struct ext2_group_desc));
- //At this position you can be assured that you read group descriptor successfully.
- close(fd);
- return 0;
- }