[LINUX] File System of Linux

The file system of Linux and some commands used are going to be discussed on this post.

Commands used to check the file system:

df filesystem* will display the amount of disk space available on the file system.

du [-s] filename* will summarize the disk usage of the set of files, and recursively for directories. -s option will only display a total for each argument.

 

The structure of the file system:

sssssssssssssssssssss

Boot block: is at the starting part of the file system and is usually in the first sector. This is where the bootstrap program code is stored in.

Super block: is where to save the information of the file system.  (Total number of blocks, the number of i-node available to use, block bitmap available, the size of the block, the number of blocks in use, the number of blocks available to use, etc.)

i-list: is the list of i-nodes that express each file. One block consists of about 40 i-nodes.

Data block: consists of the blocks to save the file’s data.

 

File status and i-node:

The file status is all the information about a file. The information includes the number of blocks, the file’s type, permission, the number of links, user ID, group ID, the file’s size, the time of the last modification, and the name of the file.

We can use ls -l filename or stat filename to check the file status.

Every file has its own i-node. The i-node contains all the information about each file.

File’s size The size of the file shown in kilobytes
File’s type The type of the file
File permission A permission to read/write/execute for owner, group and others
The number of hard links The number of hard links of the file
Owner and group ID of the file’s owner and group where owner is a part of
File’s size The size of the file shown in bytes
Time of the last access Time when the file is accessed for the last time
Time of the last modification Time when the file is modified for the last time (might be the time of file creation)
Data block’s address The address of the data block where the actual data is stored in

The structure of i-node of a file is as follows:

sssssssssssssssssddddddddddddd

The i-node consists of not only the file status information but also the block pointers to store the address of the data block to save the actual data. There are the direct block pointer that has an address of the data block, the indirect block pointer that has an address of the direct block pointer (that will point at the data block), and the double indirect block pointer that has an address of the indirect block pointer (that will point at the direct block data pointing to the data block). The actual data stored in the multiple data blocks are linked logically.

 

The structure of the directory:

The directory has the same structure as the file’s, since it is also one of the files. It is expressed as one i-node. The content(data) of the directory will be the directory entry (file name and i-node number). Its structure is as shown below:

zzzzzzzzzzzzzzzzzz

If there are sub-directories under one directory, it will have the hierarchical structure. Now, let’s see how to read the actual data of the file in the hierarchical structure.

If we want to read /usr/hello.txt from the structure as follows, how will it work?

dd

ex

First, we have to go to the i-node #2, to check the actual data of root. It says it is stored in the data block #200. There, we can see that usr’s i-node number is 4. Now, back to i-node #4. Its block pointer is 202 so let’s go to the data block #202. The i-node number of hello.txt is 5. In the i-node #5, it is said that hello.txt’s actual data is stored in the data block #203 and #204.  Its steps are:

  1. Go to the root's i-node (2).
  2. Go to the root's actual data (200).
  3. Go to the usr's i-node (4).
  4. Go to the usr's actual data (202).
  5. Go to the hello.txt's i-node (5).
  6. Go to the hello.txt's actual data (203, 204).

We found it finally! In this way, we can find the data block of the file and read the actual data.

 

The structure of a link:

A link is defined as a file’s another name. There are two types of links, a hard link and a symbolic link.

A hard link can be considered as a new name of the file. It is created by pointing at i-node of the original file. In other words, two file names will be pointed at one i-node, that points at the data block. The number of links shown in the file status (ls -l) is for the hard link.

hardlink → i-node ← file

A symbolic link is a specific file that points at the another file. It is a file where the path of the original file is saved. The path is an indirect pointer of the file.

symboliclink → file → i-node

The commands to create a link are as follows: (-s option is for symbolic link.)

ln [-s] file1 file2 will create a file2 as a new name (link) of file1.

ln [-s] file1 directory will create a link of file1 in the give directory with the same name.

 

Ref:

리눅스 시스템 원리와 실제, 창병모, 생능출판.

Wikipedia