Student Seminar Report & Project Report With Presentation (PPT,PDF,DOC,ZIP)

Full Version: EXT2 FILE SYSTEM
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
presented by:
Kamal Khatri

[attachment=11528]
Abstract
We present a etx2 file system . The Linux file system architecture is an interesting example of abstracting complexity. A file system is nothing more than the way the computer stores and retrieves all your files. These files include your documents, programs, help files, games, music etc. In the Windows world we have the concept of files and folders. A folder (also known as a directory) is nothing more than a container for different files so that you can organise them better. In Linux, the same concept holds true -- you have files, and you have folders in which you organise these files.
Introduction
In this paper, we present a etx2 file system . ext2 was the official filesystem of Linux when it was still in its infancy (the early to mid nineties).
Most Linux systems now use ext3 or another advanced filesystem. ext2 was phased out because it had reliability problems (if the power went out) and was inefficient when dealing with massive files or directories. However, ext2 is perfect for teaching, because it’s a production implementation of most of the filesystem concepts .
A new memory stick comes formatted for Windows. That means that it uses the vfat filesystem, which is simpler and less capable than ext2. Our first step will be to replace the vfat format with ext2. (Note: if for some reason the stick came without formatting, we could create a formatted
partition on it using fdisk. If we were working with a floppy, we would format it first using
fdformat.) Insert the memory stick into one of the USB ports on the left side of the monitor screen. Creating the ext2 file system is simple using the mke2fs command. However, we first have to make sure that the vfat drive is not mount (connected to the file system directory tree). Modern Linux systems automatically detect insertion of a memory stick and mount it for you. We can see the devices currently mounted using the mount command. The result should look somewhat like the following:
/dev/hda5 on / type ext3 (rw,noatime)
none on /proc type proc (rw)
none on /proc/bus/usb type usbfs (rw)
none on /sys type sysfs (rw)
/dev/hda7 on /boot type ext3 (rw,noatime)
/dev/hda1 on /mnt/windows type ntfs (ro,umask=0022,nls=iso8859-1
mount and umount require root access, so you’ll probably have to use sudo to invoke them.Note too that most of the low-level access to the raw device requires root access, so you’ll probablyhave to run your code with sudo, too. But now, you have your very own teeny hard drive to mess with!
Physical Layout of an ext2 partition
The first block in any ext2 partition is reserved for the partition's boot sector. (The rest of the partition is made up of block groups). All block groups are the same size. Because they are stored sequentially, the kernel can easily ¯nd a block group on a disk just from its index numbeindex number
ext2 filesystem structure
The first 1024 bytes of a volume are the boot block. This area is reserved for partition boot sectors and aren’t used by ext2. The rest of the disk is divided into block groups. Small devices, like floppies, contain only 1 block group. But your USB drive should have several (16 or so). All of the structures we’ll be discussing are defined in the linux/ext2.h header file, which is available on your linux boxes under usr/include/linux/ext2.h.
The Superblock
An Ext2 disk superblock is stored in an ext2_super_block structure.The superblock contains all the pertinent global information for the entire drive. Information such as: the total number of blocks on disk, the size of the blocks, the number of free blocks, and so forth. The structure of the superblock is described by struct ext2 super block (which occurs around line 315 of the ext2.h header file). I’ve extracted some of the more pertinent
fields below:
struct ext2_super_block {
__le32 s_inodes_count; /* Inodes count */
__le32 s_blocks_count; /* Blocks count */
__le32 s_r_blocks_count; /* Reserved blocks count */
__le32 s_free_blocks_count; /* Free blocks count */
__le32 s_free_inodes_count; /* Free inodes count */
__le32 s_first_data_block; /* First Data Block */
__le32 s_log_block_size; /* Block size */
...
__le32 s_blocks_per_group; /* # Blocks per group */
...
__le32 s_inodes_per_group; /* # Inodes per group */
3
__le16 s_magic; /* Magic signature */
...
__le32 s_first_ino; /* First non-reserved inode */
__le16 s_inode_size; /* size of inode structure */
In the superblock, block size is contained in s log block size. This value expresses block
size as a power of two and using 1024 as the unit. This means that an s log block size value
of 0 means 1024 byte blocks, a value of 1 means 2048 byte blocks, etc. If you need to calculate the block size in bytes from this value, you should use the following code:
unsigned int block_size = 1024 << super.s_log_block_size;
The superblock also tells us how many blocks are in a block group. This value is contained in the s blocks per group field. Going back to the mke2fs output, we see that the block group
size is 8192. Now you can see that on a floppy (capacity 1.44 MB), only one block group would fit. But on our memory sticks, there’s room for multiple blocks.
Q1: With 8192 blocks per group, how large would a block have to be for one block group to exactly fill your USB drive?
Q2: What would the s log block size field be for the block size from Q1?
The superblock is located at offset 1024 on the drive (those first bytes are reserved for the boot block). Here’s some C code to read in the superblock:
#include <linux/ext2_fs.h>
#define BASE_OFFSET 1024
#define USB_DEVICE "/dev/sda1"
. . .
int open_usb(struct ext2_super_block* super){
int fd;
struct ext2_group_desc* group = malloc(sizeof(struct ext2_group_desc));
/* open USB device */
fd = open(USB_DEVICE, O_RDONLY); //opening the device for reading
if(fd < 0){ //some kind of error occurred
perror(USB_DEVICE);
exit(1); //we give up at this point
}
/* Now we read in Mr. Superblock */
/* seeking across the ’disk’ to the superblock location */
lseek(fd, BASE_OFFSET, SEEK_SET);
/*actually reading in the bytes */
read(fd, super, sizeof(struct ext2_super_block));
/* Some sanity checks */
/* Make sure we’re reading an EXT2 filesystem */
if(super->s_magic != EXT2_SUPER_MAGIC){
fprintf(stderr, "Not an Ext2 filesystem!\n");
exit(1);
}
block_size = 1024 << super->s_log_block_size;
return fd;
}
int main(void){
struct ext2_super_block usb_block;
int file_descriptor;
file_descriptor = open_usb(&usb_block);
return 0;
}