0

So to pull super block in file system (i.e. if my sda storage is ext2 formatted) is easy. I just need to skip 1024 bytes to get the super block fro sda storage

lseek(fd, 1024, SEEK_SET);
read(fd, &super_block, sizeof(super_block));

and to pull the group descriptor is also super easy (only if I understood correctly from looking at code)

lseek(fd, 1024 + [block_size_ext_1024_bytes]=1024, SEEK_SET);
read(fd, &block_group, sizeof(block_group));
or
lseek(fd, 1024 + 1024, SEEK_SET);
read(fd, &block_group, sizeof(block_group));

1024=Base offset

But I am not feeling at confort because the real challege I found is to pull inode is only I have file name. I know file names are stored in directory struct so first challege is to extract directory struct from there and in directory struct I can get the inode number. and from Inode number I can extract inode struct. but I do not know how to extract directory struct in ext2 formatted image. Can anyone please telll me this? thanks

user786
  • 3,902
  • 4
  • 40
  • 72
  • There's no specific "directory" structure on disk. A directory is a normal file, where the data is an inode-filename mapping. It's well-documented, and such documentation could be easily found with by putting e.g. `ext2 inode directory` in your favorite search engine. – Some programmer dude Dec 14 '21 at 07:31
  • @Someprogrammerdude u mentioned `where the data is an inode-filename mapping` so is it inode number mapping with filename or what this mapping is I need two things to map between? So `is it my choice to keep this mapping info anywhere` if I am mapping inode number to filaname? where I can save to retrieve inode number from filename. Can I save it in some text file stored on some other computer. is it correct? – user786 Dec 14 '21 at 07:38
  • Please read e.g. https://piazza.com/class_profile/get_resource/il71xfllx3l16f/inz4wsb2m0w2oz (my first hit when using `ext2 inode directory` in Google). – Some programmer dude Dec 14 '21 at 07:40
  • @Someprogrammerdude it says free blocks on small floppy can be found in blocks from 41 to -1439, the floppy showed in the pdf that u sent. so the offset of first free block on floppy device is `first_free_block=(Base offset+1024x41)` can this offset be same for large storage devices too if using ext2 filesystem. Can u please tell me this – user786 Dec 14 '21 at 07:57

1 Answers1

0

Yes pulling super block is just a matter of skipping Base_Offset=1024 bytes in ext2 and then reading it like so

lseek(fd, BASE_OFFSET + block_size, SEEK_SET);
//BASE_OFFSET for EXT2 == 1024
read(fd, &super_block, sizeof(super_block));
block_size = 1024 << super_block.s_log_block_size;
printf("Block size is [%d]\n",super_block.s_log_block_size);

The size of a super-block is given by s_log_block_size. This value expresses the size of a block as a power of 2, using 1024(specifically for ext2) bytes as the unit. Thus, 0 denotes 1024-byte blocks, 1 denotes 2048-byte blocks, and so on. To calculate the size in bytes of a block:

unsigned int block_size = 1024 << super.s_log_block_size;   /* block 

super.s_log_block_size always 0 if need to hardcode 1024 and super.s_log_block_size is multiple of 2 so if 1024 block size super.s_log_block_size is should be 0

Then I can extract group descriptor. So for my image there is only one group descrptor. I dont know how many descriptor will I have if I have 1TB of storage as I do have this and file system is ext4. May be someone will tell me this.

Like this to extract group descriptor by further moving forward 1024 bytes

  lseek(fd, BASE_OFFSET + block_size, SEEK_SET);
  read(fd, &block_group, sizeof(block_group));

I think this gives the idea of finding out how many group desciptors are there in storage in ext2

unsigned int group_count = 1 + (super_block.s_blocks_count-1) / super_block.s_blocks_per_group;

so for example On my device image it has 128 blocks so first block always Boot info, second block contains super block, the third block contains first group descriptor -- still like to know what would be the offset of my second group descriptor if I had more space on my storage. Please someone shed light on this

Moving on, to extract specific inode the formula is this to seek the offset of specific inode

lseek(fd, BLOCK_OFFSET(block_group->bg_inode_table)+(inode_no-1)*sizeof(struct ext2_inode), SEEK_SET);

bg_inode_table can be used to extract inode

The group descriptor tells us the location of the block/[inode bitmaps] and of the inode table (described later) through the bg_block_bitmap, bg_inode_bitmap and bg_inode_table fields.

Now to extract root inode=(should be ino_num=2) for example I just need to do

lseek(fd, BLOCK_OFFSET(block_group->bg_inode_table)+(2-1)*sizeof(struct    ext2_inode),
   SEEK_SET);

The block number of the first block of the inode table is stored in the bg_inode_table field of the group descriptor.

so inode table came to help in find specific inode

To extract the directory struct I just need to use inode.i_block[0] array. filled in last step

each i_block element is number that can be used in this way. basically a pointer points to actual blocks containing content of file with inode

  lseek(...BASE_OFFSET+(i_block[x]-1)*block_size...)

block_size always 1024 for ext2

This way I can read the block at whose base contain directory struct in ext2 file system

read

  void *block;
  read(fd, block, block_size);  

and the above line give me first directory mapped to specific inode

I can simple do a loop to get all entries

http://www.science.smith.edu/~nhowe/teaching/csc262/oldlabs/ext2.html

user786
  • 3,902
  • 4
  • 40
  • 72