Thursday, February 24, 2011
7:01 PM

A look into the inodes

Every file that gets created  in the system also has with it various other data associated with it other than the file contents itself. For ex
The modification time
The owner of the file
Permissions : Who is allowed to read/write from the file etc

These information about a file is called the matadata of the file, i.e. it is data about the data in the file.

All the meta data of a file has to be stored somewhere so that the users can access it as and when required . In Linux the inodes are used for this.

Hence Inode is nothing but the data structure that holds the meta data of a file. Every file that gets created in the system gets an inode number assigned to it which is nothing but the address of the first block of the file. The inode in Linux stores the following information of a file

 The type of file type (executable/text etc)
 Read/Write Permissions
 Owner of the file (uid)
 Group to which owner belongs to (gid)
 The size of the file
 Time:  Last time when the contents of the file were modified (written to,mtime)
        Last time when the file was used (Read or executed, atime)
        Last time when the inode itself was modified i.e. changing permissions etc. (ctime)
   
 The number of soft/hard links to the file
 Extended attributes like only root is allowed to delete the file  etc



Note that the inode does not hold the name of the file. The interesting point is the system does not look at the files by their names but by their inode numbers.
Every directory maintains a table of file names and their corresponding inode numbers, so whenever a file is accessed, the inode number is looked up  from this table and the requested operation is done on the file.

To look at the inode numbers of the files you can use the command "ls" with the "-i" option.
For ex:

$ ls -i /etc/fstab

360456 /etc/fstab


The difference between a soft link and a hard link can be easily brought about using the inodes.

A softlink is just a pointer to the file and not the actual file itself, but a hardlink is the same file as the original file that it links to.

Let us create a soft link and hard link for the same file and then have a look at the inode numbers of all three

$ ln -s temp temp_soft  (Creating a softlink for temp)
$ ln temp temp_hard     (Creating hardlink for temp)
$ ls -i temp*
  4195423 temp
  2065399 temp_soft
  4195423 temp_hard


If you observe the output, the inode numbers of the original file "temp" and the hard link "temp_hard" are the same. Indicating that both the file names are actually pointing to the same location in the memory. On the other hand the file "temp_soft" has a different inode number hence indicating that it is a different file which points to the original file "temp".

The number of inodes allowed in a system can be fixed a few file systems, the maximum number is arrived at  by assuming a minimum size for each file that gets created , for e.g every file might be assumed to have a minimum size of 2 KB and a system that has 1 MB of memory will be allowed a maximum of 500 inodes. The modern systems though allow the inode numbers to increase dynamically.






0 comments:

Post a Comment