What is a Superblock, Dentry, Inode and a File?

In Linux, the data is organized and managed through a file system that provides space for non-volatile data. More specifically, the combination of different entities like superblocks, inodes, dentries, and files work together to form, manage, and store data in it. Now, you may be wondering what superblocks, inodes, dentries, and files on Linux are and their relationship with the file system.

This guide explains:

  • What is Linux Virtual File System?
  • What are Superblocks in Linux?
    • How to View Superblock Related Information on Linux?
  • What is Inode in Linux?
    • How to View Inodes?
    • What to Expect When There are Too Many Inodes?
  • What are Dentries in Linux?
  • What is a File on Linux?

What is Virtual File System on Linux?

The “VFS” or “Virtual File System” can be defined as an abstraction layer that helps the Linux kernel to allow co-existing of multiple file systems. It is also responsible for providing a unified file system for the user space programs.

Here is an image of the overall working flow of Linux VFS:

Now, let’s discuss the functionality of the different components of VFS:

  • In User Applications, the file operations directly or indirectly) are performed (in User Space).
  • The GNU C Library is responsible for providing the core libraries to the system.
  • The application’s parameters are then passed to the Virtual File System (VFS).
  • Two types of caches are there in the Kernel Space, Inode cache, and Dentry cache in which the recently used system objects are stored for faster access to Inodes and Dentries.
  • NFS stores files on a network.
  • procfs receive signals and trace system calls. It also provides access to each of the active threads and processes in the system
  • The ext4 is the most commonly used Linux file system. However, Linux Kernel is responsible for answering the system calls. Kernel also provides the required data, and manages the file system including the ext4 file system.

What are Superblocks in Linux?

A superblock is a data structure that contains all the information about the Linux file system. It defines the type, status, block size, location of inode tables, and other metadata-related information. These blocks are critical in all file systems to prevent file corruption. 

The primary superblock is checked and verified by the system before the file system can be mounted. Moreover, if the superblock is corrupt, you cannot mount that file system. The superblocks also store the configuration of the file system, such as:

  • File system type (ext4, ext3, etc.)
  • Inodes per block group
  • All the blocks in the file system
  • Write and mount time
  • File system’s UUID 

How to View Superblock Related Information on Linux?

The “dumpe2fs” command of Linux views the superblock-related information. It is used in this format:

$ dumpe2fs <Path-to-Partition>

Before moving on to the practical example, let’s view the partitions using the df command:

$ df -h

The “df -h” command displays the details about the partitions on the system in human-readable form. Let’s view the superblocks on “/dev/sda3” by executing this command:

$ sudo dumpe2fs /dev/sda3

As a result, detailed information about the specified partition has been displayed. Scroll down to find the “Group” portion, where the details of primary and backup superblocks are present:

The primary superblock is found in Group 0, which can get corrupted, so backup superblocks are for the replacement used in such situations.

What is Inode in Linux?

An Inode or index node is a data structure that stores ownership, permissions, file size, and other metadata-related terms. Every file and directory on Linux is represented by a unique inode number used by the system to identify it on the file system. This unique number stores the metadata of each file and directory. An Inode can have the following data:

  • File type and size
  • Owner and group ID
  • Permissions
  • Access and modification time

How to View Inodes?

Multiple commands display the number of Inodes in a file or directory. Some of them are discussed below.

View Inodes Using the “stat” Command

To view the number of inodes of a script along with other detailed information, the “stat” command is used as follows:

$ stat script.sh

In the above image, the number of Inodes for the “script.sh” script file is “274491”. However, the executed command also provides other information.

View Only Inodes Using the “ls” command

To only display the number of inodes on a specific file, the “ls command with the “-i” flag can be utilized:

$ ls -i script.sh

The above image shows that the number of Inodes for the file “script.sh” is “274491”.

View all Inodes on the File System 

To view the total number of inodes of the whole file system, the df command with the “-i” flag is used in this way:

$ df -i /dev/sda3

The above image shows that the total number of Inodes for the file system “/dev/sda3” are “1277952”.

What Happens When There are Too Many Inodes?

When there are too many files and directories in a file system, there is a high probability that you may face:

  • Random crashes
  • Data loss, and corruption
  • Some utilities may fail as temporary files cannot be created

To avoid this, make sure to remove the unnecessary files and directories from the system.

What are Dentries in Linux?

A dentry or directory entry is used by Virtual File Systems (VFS) to represent information about the directories and files inside the memory. It is highly efficient as the system does not have to go through all the directories to get the metadata when it is stored inside the memory. This memory is called the dentry cache.

In the dentries, the following information is stored:

  • Directory’s name
  • Parent directory
  • Inode number

The dentries cannot and should not be viewed by users as it requires kernel-level debugging where a single mistake could lead to severe issues.

What is a File on Linux?

The sequence of bytes that are stored on the disk is a file. It can be any file.. These files have their unique Inode number through which they are identified. Moreover, the unsaved data of the file is erased from the memory when closed.

Conclusion

The superblock is the data structure with all the file system information. Inodes store metadata about a file or directory, while dentries represent information about the file or directory. Lastly, a file is a sequence of bytes in different forms. 

This guide explored the details of the Superblock, Inode, Dentry, and File in Linux.