- What is a Superblock, Inode, Dentry and a File?
- 5 Answers 5
- Inodes in Linux: limit, usage and helpful commands
- What is an inode?
- Metadata stored in an inode
- Inode number: creating, copying and modifying files
- Number of inodes in a filesystem
- Inode limit
- Inode usage issues and best practices
- Helpful commands
- Check a file’s inode number
- Using the stat command
- Using the ls command
- Check a directory’s inode number using the ls command
- Check inode usage within a filesystem using the df command
- Check the number of inodes in a directory using the wc command
What is a Superblock, Inode, Dentry and a File?
From the article Anatomy of the Linux file system by M. Tim Jones, I read that Linux views all the file systems from the perspective of a common set of objects and these objects are superblock, inode, dentry and file. Even though the rest of the paragraph explains the above, I was not that comfortable with that explanation. Could somebody explain to me these terms?
5 Answers 5
First and foremost, and I realize that it was not one of the terms from your question, you must understand metadata. Succinctly, and stolen from Wikipedia, metadata is data about data. That is to say that metadata contains information about a piece of data. For example, if I own a car then I have a set of information about the car but which is not part of the car itself. Information such as the registration number, make, model, year of manufacture, insurance information, and so on. All of that information is collectively referred to as the metadata. In Linux and UNIX file systems metadata exists at multiple levels of organization as you will see.
The superblock is essentially file system metadata and defines the file system type, size, status, and information about other metadata structures (metadata of metadata). The superblock is very critical to the file system and therefore is stored in multiple redundant copies for each file system. The superblock is a very «high level» metadata structure for the file system. For example, if the superblock of a partition, /var, becomes corrupt then the file system in question (/var) cannot be mounted by the operating system. Commonly in this event, you need to run fsck which will automatically select an alternate, backup copy of the superblock and attempt to recover the file system. The backup copies themselves are stored in block groups spread through the file system with the first stored at a 1 block offset from the start of the partition. This is important in the event that a manual recovery is necessary. You may view information about ext2/ext3/ext4 superblock backups with the command dumpe2fs /dev/foo | grep -i superblock which is useful in the event of a manual recovery attempt. Let us suppose that the dumpe2fs command outputs the line Backup superblock at 163840, Group descriptors at 163841-163841 . We can use this information, and additional knowledge about the file system structure, to attempt to use this superblock backup: /sbin/fsck.ext3 -b 163840 -B 1024 /dev/foo . Please note that I have assumed a block size of 1024 bytes for this example.
An inode exists in, or on, a file system and represents metadata about a file. For clarity, all objects in a Linux or UNIX system are files; actual files, directories, devices, and so on. Please note that, among the metadata contained in an inode, there is no file name as humans think of it, this will be important later. An inode contains essentially information about ownership (user, group), access mode (read, write, execute permissions), file type, and the data blocks with the file’s content.
A dentry is the glue that holds inodes and files together by relating inode numbers to file names. Dentries also play a role in directory caching which, ideally, keeps the most frequently used files on-hand for faster access. File system traversal is another aspect of the dentry as it maintains a relationship between directories and their files.
A file, in addition to being what humans typically think of when presented with the word, is really just a block of logically related arbitrary data. Comparatively very dull considering all of the work done (above) to keep track of them.
I fully realize that a few sentences do not provide a full explanation of any of these concepts so please feel free to ask for additional details when and where necessary.
Inodes in Linux: limit, usage and helpful commands
Inodes in Linux are unique identifiers that describe files and directories within a filesystem. It is important to keep an eye on them to avoid issues related to inode shortage or excessive usage.
What is an inode?
An inode is a data structure that keeps track of all the files and directories within a Linux or UNIX-based filesystem. So, every file and directory in a filesystem is allocated an inode, which is identified by an integer known as “inode number”. These unique identifiers store metadata about each file and directory.
“Inode” is the abbreviation for “index node”.
All inodes within the same filesystem are unique. However, the same inode number can be used in different filesystems. Because the filesystem ID and each inode number are combined to create unique identification labels.
Metadata stored in an inode
Inodes store metadata such as:
- File type
- File size
- Owner ID
- Group ID
- Read, write and execute permissions
- Last access time
- Last change time
- Last modification time
Inode number: creating, copying and modifying files
As explained above, each inode is identified by an inode number. Therefore, when creating or copying a file, Linux assigns a different inode number to the new file. However, when moving a file, the inode number will only change if the file is moved to a different filesystem. This applies to directories as well.
Number of inodes in a filesystem
The theoretical total number of inodes in a system is approximately 4.3 billion. But the figure you should care about is the number of inodes in your system. The general ratio of inodes is 1:16 KB of system capacity. You can check the number of inodes in a filesystem using the df command with the -i option.
Let’s see why it is important to know the number of inodes in a filesystem.
Inode limit
The total number of inodes in a filesystem is defined when it is created and it cannot be changed dynamically. So, it is important to regularly check inode usage to guarantee it adjusts to the configured limits.
If you have ever got the following error message when trying to create a new file on a server — even though you know there is plenty of space still available —, you might have reached the inode limit of your system:
Although it is unusual to run out of inodes before running out of actual disk space, it is not impossible. It can happen when:
- Using containerization.
- Creating lots of directories, symbolic links and small files.
- Creating ext3 filesystems with smaller block sizes.
Inode usage issues and best practices
An excessive inode usage can lead to issues when creating new files and directories. Some of the issues users may encounter when the server runs out of inodes are:
- Data loss.
- Server restart.
- Application crash.
- Scheduled tasks not running.
So, it is recommended to keep inode usage low by deleting:
- Unnecessary files and directories.
- Cache files.
- Old email files.
- Temporary files.
Helpful commands
Check a file’s inode number
Using the stat command
The stat command gives information about the file and filesystem. So, you can use it to check the inode number of a file.
[root@stackscale ~]$ stat /var/log/lastlog
When executing the command, you will get the following information:
File: /var/log/lastlog Size: 292292 Blocks: 96 IO Block: 4096 regular file Device: fd00h/64768d Inode: 17381397 Links: 1 Access: (0664/rw-rw-r-) Uid: ( 0/ root) Gid: ( 22/ utmp) Context: system_u:object_r:lastlog_t:s0 Access: 2022-01-12 11:28:19.900058928 +0100 Modify: 2022-01-12 11:28:19.900058928 +0100 Change: 2022-01-12 11:28:19.900058928 +0100 Birth: 2021-06-25 17:40:57.254208200 +0200
Using the ls command
You can also use the ls command, together with the -i option, to get a file’s inode number. This command lists files and directories within the filesystem.
[root@stackscale ~]$ ls -i /var/log/lastlog 17381397 /var/log/lastlog
Check a directory’s inode number using the ls command
You can also use the ls command and the -i option to get a directory’s inode number. For doing so, you need to add a few more options.
[root@stackscale ~]$ ls -idl /var/log 16813380 drwxr-xr-x. 18 root root 4096 Jun 6 12:33 /var/log
Check inode usage within a filesystem using the df command
The df command is used to display information related to a filesystem’s total and available space. So, you can use it, together with the -i option, to control inode usage in filesystems.
[root@stackscale~]$ df -i /dev/sda1 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 524288 379 523909 1% /boot
Check the number of inodes in a directory using the wc command
The wc command is used to count the number of characters, words, lines and bytes of files. Together with the -l option, you can use it to count the number of inodes in a directory.
[root@stackscale]# find /var/log | wc -l 120
Need help with system administration?