473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Understanding Ext-2 file system:chapter 1

ashitpro
542 Expert 512MB
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_block structure, whose fields are listed below

s_inodes_count : Total number of inodes
s_blocks_count : Filesystem size in blocks
s_r_blocks_count : Number of reserved blocks
s_free_blocks_count : Free blocks counter
s_free_inodes_count : Free inodes counter
s_first_data_block : Number of first useful block (always 1)
s_log_block_size : Block size
s_log_frag_size : Fragment size
s_blocks_per_group : Number of blocks per group
s_frags_per_group : Number of fragments per group
s_inodes_per_group : 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_level : 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_nr : Block group number of this superblock
s_feature_compat : Compatible features bitmap
s_feature_incompat : Incompatible features bitmap
s_feature_ro_compat : 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_usage_bitmap : Used for compression
s_prealloc_blocks : 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.

Expand|Select|Wrap|Line Numbers
  1.  
  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>
  10.  
  11.     #define boot_block_size 1024
  12.  
  13.     int main()
  14.     {
  15.         char *buff = (char *)malloc(sizeof(struct ext2_super_block));
  16.  
  17.         struct ext2_super_block * sblock = (struct ext2_super_block *)malloc(sizeof(struct ext2_super_block));
  18.  
  19.         //open any partition for testing,must be ext2/ext3.
  20.         int fd = open("/dev/hda3",O_RDONLY);
  21.  
  22.         //skip the boot block
  23.         lseek(fd,boot_block_size,SEEK_CUR);
  24.  
  25.         //read the superblock raw data from disk to buff
  26.         read(fd,buff,sizeof(struct ext2_super_block));
  27.  
  28.         //copy buffer to sblock, you can use casting or union for this.
  29.         memcpy((void *)sblock,(void *)buff,sizeof(struct ext2_super_block));
  30.  
  31.         printf("\nmagic number:%u\n",sblock->s_magic);
  32.  
  33.         close(fd);    
  34.  
  35.         return 0;
  36.     }
  37.  
  38.  
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
Feb 15 '08 #1
8 11974
Very Informative! I liked it
Dec 22 '08 #2
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
Jul 7 '09 #3
ashitpro
542 Expert 512MB
Which kernel version you are using?
Do you have the kernel-headers installed for the same kernel?
Jul 8 '09 #4
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
Jul 9 '09 #5
ashitpro
542 Expert 512MB
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
Jul 9 '09 #6
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
Jul 9 '09 #7
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
Jul 9 '09 #8
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.
Nov 1 '10 #9

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

Similar topics

1
by: Member | last post by:
I have a situation that some of you may be of help. I will name the tables specifically so you can understand what I am talking about. I have a COS_species table....the information in it cannot...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
3
by: Redefined Horizons | last post by:
I'm trying to understand the argument flags that are used in the method table of an extension module written in C. First let me ask this question about the method table. Is it an C array named...
3
by: Divick | last post by:
I was reading this section in Bruce Eckel's book which talks about passing and returning large objects ( Chapter 11: References & the Copy-Constructor ), where he explains that how to return big...
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
263
by: Malcolm McLean | last post by:
The webpages for my new book are now up and running. The book, Basic Algorithms, describes many of the fundamental algorithms used in practical programming, with a bias towards graphics. It...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
3
numberwhun
by: numberwhun | last post by:
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language. I am in...
5
by: Bob Nelson | last post by:
Right next to K&R2 on my bookshelf is _C Programming: A Modern Approach_ by Professor K.N. King. The second edition of this book is now available. See this URL for details: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.