Thursday, October 18, 2012

Understanding Linux / Unix Filesystem Inode

http://www.geekride.com/understanding-unix-linux-filesystem-inodes


Inode, short form of Index Node is what the whole Linux filesystem is laid on. Anything which resides in the filesystem is represented by Inodes. Just take an example of an old school library which still works with a register having information about their books and their location, like which cabinet and which row, which books resides and who is the author of that book. In this case, the line specific to one book is Inode. In the same way Inodes stores objects, which we will study in detail below.
So, in the linux system, the filesystem mainly consists of two parts, first is the metadata and the second part is the data itself. Metadata, in other words is the data about the data. Inodes takes care of the metadata part in the filesystem.

Inode Basics:

So, as I said, every file or directory in the filesystem is associated with an Inode. An Inode is a data structure, and it stores the following information about the destination:
  • Size of file (In bytes)
  • Device ID (Device containing the file)
  • User ID (of the owner)
  • Group ID
  • File Modes (how owner, group or others could access the file)
  • Extended Attributes (like ACL)
  • Files access, change or modification time stamps
  • Link Count (no of hard links pointing to the inode … remember, no soft links are counted here)
  • Pointer to the disk block that stores the content.
  • File type (whether file, directory or special block device)
  • Block size of the filesystem
  • No. of blocks file us using.
Linux filesystem never stores the file creation time, though lot of people get confused in that. The complete explanation about the various time stamps stored in inode are explained in this article.
A typical inode data will look something like this:
# stat 01
Size: 923383 Blocks: 1816 IO Block: 4096 regular file
Device: 803h/2051d Inode: 12684895 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-09-07 01:46:54.000000000 -0500
Modify: 2012-04-27 06:22:02.000000000 -0500
Change: 2012-04-27 06:22:02.000000000 -0500

How / When Inodes are created ?

Creation of Inodes depends on the filesystem you are using. Some filesystem like ext3 creates the Inodes when the filesystem is created, hence having a limited number of Inodes while others like JFS and XFS creates Inodes at the filesystem creation also, but uses dynamic Inode allocation and can increase the number of Inodes according to the need, hence avoiding the situation where all the Inodes gets used up.

What happen when someone tries to access a File:

When a user tries to access the file or any information related to the file then he/she uses the file name to do so but internally the file-name is first mapped with its Inode number stored in a directory table. Then through that Inode number the corresponding Inode is accessed. There is a table (Inode table) where this mapping of Inode numbers with the respective Inodes is provided.

Inode Pointer Structure:

So, as already explained, Inodes only stores metadata information of the file, including the information of the blocks where the real data of the file is stored. This is where Inode Pointer Structure explained.
As explained in the Wikipedia Article, the structure could have 11 to 13 pointers, but most file system store data structure in 15 pointers. These 15 pointers consists of:
  • Twelve pointers that directly point to blocks of the file’s data, called as direct pointers.
  • One singly indirect pointer, which points to a block of pointers that then point to blocks of the file’s data.
  • One doubly indirect pointer, which points to a block of pointers that point to other blocks of pointers that then point to blocks of the file’s data.
  • One triply indirect pointer, which points to a block of pointers that point to other blocks of pointers that point to other blocks of pointers that then point to blocks of the file’s data.
The above things can be explained in a diagram like this:
Inode Pointer Structure
Inode Pointer Structure (From wikipedia, Wikimedia Commons license)

FAQs:

Q. How do I define inode in one line ?
A. An inode is a data structure on a traditional Unix-style file system such as UFS or ext3. An inode stores basic information about a regular file, directory, or other file system object.
Q. How can I see files or directory Inode number ?
A. You can use “stat” command to see the information or you can use “-i” argument with “ls” command to see the inode number of a file.
Q. How to find the total number of Inodes in a Filesystem and the usage of Inodes ?
A. “df -i” command will tell you the stats about the total number, number used and free Inodes.
Q. Why Inode information doesn’t contain the filename ?
A. Inodes store information which are unique to an Inode. In case of a hard link, an Inode could have 2 different file names pointing to the same Inode. So, it’s better not to store the filename inside an Inode.
Q. What if Inode have no links ?
A. Inode having no links (means 0 links), is removed from the filesystem and the resources are freed for reallocation but deletion must wait until all processes that have opened it finish accessing it.
Q. Does Inode change when we move a file from one location to another ?
A. The Inode number stays the same even when we move the file from one location to another only if it’s on the same filesystem. If it’s across filesystem, then the Inode number changes.
Q. What happens when we create a new file or directory, does it create a new Inode ?
A. No, when we create a File or directory, it will just use a already created Inode space, and update the information, but won’t create a new Inode. Inodes are only created at filesystem creation time (exception about some other filesystems, which is explained above)
Q. Can I find a file from an Inode number?
A. Yes, by using the following command
# find / -inum inode-number -exec ls -l {} \;
Using the same command and replacing “ls” command with “rm” command, you can remove a file also on the basis on inode number
# find / -inum inode-number -exec rm -f {} \;

Reference:

  1. Wikipedia
  2. Linux Magazine

No comments:

Post a Comment