inode and its structure in linux

Sarath Pillai's picture
Linux inode tutorial

Let me begin this tutorial by a famous UNIX statement which emphasizes the main underlying working model of Linux operating system. The statement is that "Everything Is a File".

If you are one of that person who spends a lot time with Linux or any NIX system, then you might have already understood that statement. Each and everything in Linux is accessed by a file. To make that much more clear, even block devices like Hard disks,and CD/DVD's are nothing other than a file(yeah but a special file..but a file).

Now if we start digging a bit deeper into files, we will come to know that, to the operating system(Linux) files inside a file system(be it ext2,ext3 or ext4) are not really accessed by their name. Names are helpful to humans but, the file system recognizes a file not by its name but by a number. That number through which the operating system reaches the location and other attributes of that file is called as an inode number. Now don't worry smiley, this tutorial is all about Inodes and its structure.

 

What is an INODE in Linux?

 

I must say that its a data structure that keeps track of all the information about a file.

You store your information in a file, and the operating system stores the information about a file in an inode(sometimes called as an inode number).

Information about files(data) are sometimes called metadata. So you can even say it in another way, "An inode is metadata of the data."

Whenever a user or a program needs access to a file, the operating system first searches for the exact and unique inode (inode number), in a table called as an inode table. In fact the program or the user who needs access to a file, reaches the file with the help of the inode number found from the inode table.

To reach a particular file with its "name" needs an inode number corresponding to that file. But to reach an inode number you dont require the file name. Infact with the inode number you can get the data.

 

How does the structure of an inode look like?

 

This is the most important part to understand in terms of an inode. Here we will be discussing the contents of an inode.

Inode Structure of a Directory:

Inode structure of a directory just consists of Name to Inode mapping of files and directories in that directory.

So here in the above shown diagram you can see the first two entries of (.) and (..) dot dot. You might have seen them whenever you list the contents of a directory.(most of the times they are hidden. You will have to use -a option with "ls" command to see them).

And people who are more into Linux or any NIX system, knows that the command "cd ." will change the directory to the current directory itself(which means it does nothing..because you are already in that directory.).

And the command "cd .." will take you to the previous directory or call it the parent directory of the current directory. Now why that happens?

Lets understand why this happens with an example. Imagine am in the directory /var/log on my system.

 

[root@slashroot1 log]# ls -ia
3633723 .                3633786 faillog     3634889 rpmpkgs.3
3633697 ..               3634727 gdm         3634893 rpmpkgs.4
3634833 acpid            3633883 httpd       3633813 samba

Now lets note down inode numbers of .(dot) and ..(dot dot).

.(dot) = 3633723

..(dot dot) = 3633697

Now lets do the directory listing of /var/ directory and see the inodes there.

[root@slashroot1 var]# ls -ia
3633697 .        3634275 cvs    3633698 lib    3633733 nis       3633737 spool
      2 ..       3633724 db     3633729 local  3633734 opt       3633700 tmp
3633844 account  3633725 empty  3633730 lock   3633735 preserve  3634278 tux
3633701 cache    3633726 games  3633723 log    3633838 racoon    3633884 www
3634135 crash    3634624 gdm    3633732 mail   3633736 run       3633740 yp

Now lets note down the inode numbers of log directory and .(dot) from the directory listing of /var

.(dot) = 3633697

log = 3633723

So you can clearly note that inode of .(dot) inside /var/log directory is equal to inode of log directory. And inode of ..(dot dot ) inside /var/log/ is equal to inode of .(dot) inside /var/ directory.

.(dot) always means the current directory just because its inode is same as the directory's inode. And ..(dot dot) means parent directory inode because its inode is same as the previous(parent) directory.

 

Inode Structure of a File

 

Now lets see how the structure of an inode of a file look like.

Mode:

This keeps information about two things, one is the permission information, the other is the type of inode, for example an inode can be of a file, directory or a block device etc.

Owner Info: Access details like owner of the file, group of the file etc.

Size:  This location store the size of the file in terms of bytes.

Time Stamps:  it stores the inode creation time, modification time, etc.

Now comes the important thing to understand about how a file is saved in a partition with the help of an inode.

Block Size: Whenever a partition is formatted with a file system.It normally gets formatted with a default block size. Now block size is the size of chunks in which data
will be spread. So if the block size is 4K, then for a file of 15K it will take 4 blocks(because 4K*4 16), and technically speaking you waste 1 K.

 

Direct Block Pointers:

In an ext2 file system an inode consists of only 15 block pointers. The first 12 block pointers are called as Direct Block pointers. Which means that these pointers point to the address of the blocks containing the data of the file. 12 Block pointers can point to 12 data blocks. So in total the Direct Block pointers can address only 48K(12 * 4K) of data. Which means if the file is only of 48K or below in size, then inode itself can address all the blocks
containing the data of the file.

Now What if the file size is above 48K?

Indirect Block Pointers:

whenever the size of the data goes above 48k(by considering the block size as 4k), the 13th pointer in the inode will point to the very next block after the data(adjacent block after 48k of data), which inturn will point to the next block address where data is to be copied.

Now as we have took our block size as 4K, the indirect block pointer, can point to 1024 blocks containing data(by taking the size of a block pointer as 4bytes, one 4K block can point to 1024 blocks because 4 bytes * 1024 = 4K).

which means an indirect block pointer can address, upto 4MB of data(4bytes of block pointer in 4K block, can point and address 1024 number of 4K blocks which makes the data size of 4M)

Double indirect Block Pointers:

Now if the size of the file is above 4MB + 48K then the inode will start using Double Indirect Block Pointers, to address data blocks. Double Indirect Block pointer in an inode will point to the block that comes just after 4M + 48K data, which intern will point to the blocks where the data is stored.

Double Indirect block pointer also is inside a 4K block as every blocks are 4K, Now block pointers are 4 bytes  in size, as mentioned previously, so Double indirect block pointer can address 1024 Indirect Block pointers(which means 1024 * 4M =4G). So with the help of a double indirect Block Pointer the size of the data can go upto 4G.

Triple Indirect Block Pointers:

Now this triple Indirect Block Pointers can address upto 4G * 1024 = 4TB, of file size. The fifteenth block pointer in the inode will point to the block just after the 4G of data, which intern will point to 1024 Double Indirect Block Pointers.

So after the 12 direct block pointers, 13th block pointer in inode is for Indirect block pointers, and 14th block pointer is for double indirect block pointers, and 15th block pointer is for triple indirect block pointers.

Now this is the main reason why there are limits to the full size of a single file that you can have in a file system.

 

Now an interesting fact to understand is that the total no of inodes are created at the time of creating a file system. Which means there is an upper limit in the number of inodes you can have in a file system. Now after that limit has reached you will not be able to create any more files on the file system, even if you have space left on the partition.

How to check Inode Utilization?

Inode utilization can be checked by using the beow command in linux.

[root@slashroot1 ~]# df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/sda1            10246368  124075 10122293    2% /
tmpfs                  64431       1   64430    1% /dev/shm

 

As you can clearly see from the above output that the max number of inodes that can be created on /dev/sda1 is 10246368.

[root@slashroot1 ~]# tune2fs -l /dev/sda1
tune2fs 1.39 (29-May-2006)
Filesystem volume name:   /1
Last mounted on:          <not available>
Filesystem UUID:          86898399-4550-4b08-8196-7444ea953c96
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              10246368
Block count:              10239421
Reserved block count:     511971

 

 

 

How to find a file using inode?

You can find a file using its inode and do actions on it, like list the file name of the inode, delete the file using its inode etc.
 
[root@slashroot1 log]# find /var/ -inum 3634906 -exec ls -l {} \;
-rw------- 1 root root 1272 Dec  2 10:13 /var/log/maillog.1
[root@slashroot1 log]#
 
You can find and delete a file using its inode using the below command.
 
[root@slashroot1 log]# find /var/ -inum 3634906 -exec rm -f {} \;
[root@slashroot1 log]#
 

How to change directory using an inode?

You can change to a directory using its inode number as shown below.

[root@slashroot1 log]# cd $(find -inum 3633883)
[root@slashroot1 httpd]#
 
you can see that i have changed the directory to httpd by using its inode number.
 
I will posting a couple of another posts, regarding hardlinks in inode, how to deal with inode limit, What happens to the inode when a file gets deleted in ext2 and ext3 filesystem etc.
 
Hope this introductory guide in understanding inode was helpful..
Rate this article: 
Average: 3.5 (2373 votes)

Comments

Thanks for such an great article..

Good explanation :) ..
Thank you

Thanks for this nice article.

Good Job Bro.. seriously very informative ...

Thank you

Nice One..!! Thanks easy to understand..!! :)

Hi,

Could you please clarify below, I have created a test file under /var and I have searched the file using -inum through find. Amazing, I got two files with same Inode number. Anyone comment please.

[root@rhel-prod var]# ls -i test
2660 test

[root@rhel-prod ~]# find / -inum 2660 -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Feb 20 07:45 /var/test
--w------- 1 root root 4096 Feb 20 07:28 /sys/bus/usb/drivers/usb/bind

[root@rhel-prod var]# stat test
File: `test'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 2660 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-02-20 07:45:31.726997893 -0500
Modify: 2015-02-20 07:45:31.726997893 -0500
Change: 2015-02-20 07:45:31.726997893 -0500

[root@rhel-prod var]# stat /sys/bus/usb/drivers/usb/bind
File: `/sys/bus/usb/drivers/usb/bind'
Size: 4096 Blocks: 0 IO Block: 4096 regular file
Device: 0h/0d Inode: 2660 Links: 1
Access: (0200/--w-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-02-20 07:28:47.838998860 -0500
Modify: 2015-02-20 07:28:47.838998860 -0500
Change: 2015-02-20 07:28:47.838998860 -0500
[root@rhel-prod var]#

Device: 805h/2053d
Device: 0h/0d

Two files can have the same inode number if they are stored on different devices.

Nice

Really nice information!!!!!

Very well explained!

good explanation . thanks for posting.

Concept made clear in a single attempt. Very useful article for slow learners.

good explanation.short and sweet.

Thanks

Just wanted to thank you guys

Best explanation about inode. thanks

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.