Ext2 of Linux file system

Source: Internet
Author: User

 One

    • First, for Linux systems, the system hierarchy is divided into the user layer, the kernel layer, and the physical device layer,

                        

    • For example, in the C language for file writing operations, first C language itself buffers are set to improve read and write efficiency. The C write function calls the Linux system function interface write (), which is under User state. Immediately after write () calls the Linux system kernel function into the kernel layer for writing, which is obviously controlled by the filesystem, and there is still a caching device in the kernel to improve the read and write efficiency, which is then written to disk by the device driver. This is simply a process from calling C read-write functions to manipulating data to disk.

 Two

    • Now that we know where the Linux file system is, here are some basic concepts

1) Sector: sector is not a physical concept, but a unit, size is 521Byte

2) Block: Is the basic unit of a Linux file system, the size is 4096Byte

3) block group: ext2 file System is a block group as the basic unit

4) Inode: Store file basic information (except file name and file type), one file corresponds to one Inode; in ext2 file system, size is 128Byte

    • The next step is to explain the ext2 file system, and ext2 uses the basic structure to manage the data store, each block group is a basic unit, knowing the basic unit and then understanding the file system

                      

Boot block: Boot blocks, size is 1KB, storage disk partition information and boot information; PS: Only 1 copies of a filesystem

Super BLOCK: Mega blocks, size is 1 blocks; In order to improve the robustness of the system, each block group has one (ext4 with sparse copy), and each content is consistent; it is used to describe the file system information of the whole partition;

such as block size, file system version number, time of last mount, etc.

GDT: Block group descriptor, size is a plurality of blocks, the number of blocks is not determined by a number of block group descriptors, the entire partition divided into how many block groups corresponding to the number of block group descriptor.

Each block group descriptor (Groupdescriptor) stores the descriptive information for a block group, such as where the inode table starts in this block group , and where it begins to be a block of data,

The number of free inode and data blocks, and so on. Like a super block, a block descriptor list has a copy at the beginning of each block group, which is very important,

Once the super block is accidentally damaged, the entire partition's data is lost, and once the block group descriptor is accidentally corrupted, the entire block group of data is lost, so they all have multiple copies.

Typically the kernel only uses copies of the 0 block groups, and when performing e2fsck checks for file system consistency, the Super Block and block group descriptor in the 0 block group are copied to the other block groups

So that when the beginning of the 0 block group is accidentally damaged, other copies can be used to recover, thereby reducing the loss.

Block Bitmap: Blocks bitmap, the principle and the BITMAP algorithm consistent (with each bit to represent the data); flag the usage of each block (0 unused, 1 used) the blocks in a block group are used this way:

Data block stores data for all files , such as the block size of a partition is 1024 bytes, a file is 2049 bytes, then you need three data blocks to save,

even if the third block has only one byte to occupy a whole block, the Super block,                                   The block Group Descriptor table, block bitmap, inode bitmap, inode tables store the description information for this block group. So how do you know which blocks have been used to store file data or other descriptive information, and which blocks are still available for free?

Block bitmaps are used to describe which blocks in the entire block group have been used for free, and it itself occupies a block, where each bit represents a block in the block, and this bit is 1 to indicate that the block is used, and that bit is 0 to indicate that the block is free.

Inode Bitmap: Like a block bitmap, itself occupies a block, each of which indicates whether an inode is available;

Inode table: The table in which the inode is stored, thenumber of blocks of inode tables is determined and written to the block group descriptor when it is formatted, andthe default policy for the MKE2FS format tool is

How many inode is allocated for a block group with a 8KB .

Data Blocks: Storage

  

Three

    • Next, describe the inode structure

                            

  , a pointer to a data pointer to a data block, and the last three multilevel pointers in order to extend the data block

Four files into flow

1. Find the GDT first to see where Inodetable is located

2. Find the unused minimum value in the table to be assigned to the file use,

3. Inode bitmap corresponding position from 0 1

4. inode Store file information, Update table

Note: 1) file system is very complex, the above just put into the file roughly flow, the actual system also has idle detection, dynamic distribution, etc.

2) file Delete just will inode Bitma by 1 0, update block bimap more line GDT, so the file is not really deleted

Five directory structure

  A directory occupies one block or more blocks, and the contents of the directory block are as follows:

              

  Note: 1. A symbolic connection is a new record entry that points to a secondary file record entry

2. A hard link is a new record entry that points to this file

  

Attached: Recursively lists the list of files in the directory

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <dirent.h>

#include <stdio.h>

#include <string.h>

#define MAX_PATH 1024

/* dirwalk:apply FCN to all files in Dir */

void Dirwalk (char *dir, Void (*FCN) (char *)) {

Char Name[max_path];

struct Dirent *DP; DIR *DFD;

if (DFD = Opendir (dir)) = = NULL) {

fprintf (stderr, "Dirwalk:can ' t Open%s\n", dir);

Return

}

while (DP = Readdir (DFD)) = NULL) {

if (strcmp (Dp->d_name, ".") = = 0 | | strcmp (Dp->d_name, "..") = = 0)

Continue /* Skip Self and Parent */

if (strlen (dir) +strlen (dp->d_name) +2 > sizeof (name))

fprintf (stderr, "Dirwalk:name%s%s too long\n", dir, Dp->d_name);

else {

sprintf (name, "%s/%s", dir, Dp->d_name);

(*FCN) (name);

}

}

Closedir (DFD);

}

/* Fsize:print the size and name of the file "name" */

void Fsize (char *name) {

struct stat stbuf;

if (stat (name, &stbuf) = =-1) {

fprintf (stderr, "Fsize:can ' t access%s\n", name);

Return

}

if ((Stbuf.st_mode & s_ifmt) = = S_ifdir)

Dirwalk (name, fsize);

printf ("%8ld%s\n", stbuf.st_size, name);

}

int main (int argc, char **argv) {

if (argc = = 1)/* default:current directory */

Fsize (".");

Else

while (--ARGC > 0)

Fsize (*++ARGV);

return 0;

}

  

  

 

Ext2 of the Linux file system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.