What is Inode
In Unix or Linux file system file internal representation is called inode or index node. An inode structure is an object in the file structure on a file system except the file contents and the file name and used to track the file(s) stored on the disk. The inode entries store metadata about each file, directory or object, but only points to these structures rather than storing the data.
What are the contents of Inode structure
- Inode number
- Access Control List (ACL)
- Extended attribute
- Direct/indirect disk blocks
- Number of blocks
- File access, change and modification time
- File deletion time
- File generation number
- File size
- File type
- Group
- Number of links
- Owner
- Permissions
- Status flags
What is the catch
There is no entry for file name in the Inode, rather the file name is kept as a separate entry parallel to Inode number. The reason for separating out file name from the other information related to same file is for maintaining hard-links to files. This means that once all the other information is separated out from the file name then we can have various file names which point to same Inode.
How to get the inode number of a file
[ec2-user@ip-172-31-42-140 ~]$ ls -il
total 130816
13144 -rw-rw-r-- 1 ec2-user ec2-user 30 May 25 08:49 1.c
13136 -rw-rw-r-- 1 ec2-user ec2-user 294241 Jan 26 14:04 a
352 -rw-rw-r-- 1 ec2-user ec2-user 57295640 Aug 31 2014 abc.tar.gz
13139 -rw-rw-r-- 1 ec2-user ec2-user 801 Feb 26 08:17 a.c
368 -rwxrwxr-x 1 ec2-user ec2-user 6899 Feb 26 08:17 a.out
369 -rw-rw-r-- 1 ec2-user ec2-user 488 Dec 18 2013 apply-button.png
370 -rw-rw-r-- 1 ec2-user ec2-user 137 Dec 23 2013 a.py
371 -rwxrwxr-x 1 ec2-user ec2-user 10140 Mar 8 2014 b
372 drwxr-xr-x 1 ec2-user ec2-user 598 Aug 5 2014 xxx
The number on the far left is the inode number associated with the file. Also notice that there is a directory “xxx” that also has an inode associated with it. Each time a file or directory is created or deleted, an inode is created or deleted.
Inode Pointer Structure
According to the wikipedia, the structure used to have 11 or 13 pointers but most modern file systems use 15 pointers stored in the data structure. From wikipedia, for the case where there are 12 pointers in the data structure, the pointers are:
- Twelve points that directly point to blocks containing the data for the file. These are called direct pointers.
- One single indirect pointer. This pointer points to a block of pointers that point to blocks containing the data for the file.
- One doubly indirect pointer. This pointer points to a block of pointers that point to other blocks of pointers that point to blocks containing the data for the file.
- One triply indirect pointer. This pointer points to a block of pointers that point other blocks of pointers that point to other blocks of pointers that point to blocks containing the data for the file.
To make things easier, Figure below from wikipedia, shows these different types of pointers.
In the figure you can see the direct, indirect, and doubly indirect pointers. A triply indirect pointer is similar to the doubly indirect pointer with another level of pointers between the data blocks and the inode.