Tuesday, June 21, 2011

Inode - All about inodes



  Everything is a file in Unix, we say.  Users deal with files a lot, however, we usually can get most of our work done without needing to know about i-nodes, and their relation with files. However, if we know it, our clarity on looking at the file system will get better.

1. What is an Inode?
     Inodes in Unix are data structures which contain all the properties of a file, metadata. The properties of the file include file size, file owner, the group to which the file belongs to, file access rights, hard link count, the location where the file contents are present and time stamps(last modified time, last accessed time, last changed time). In other words, the inode data structure contains all the information of the file except the file name and its contents.

2. What is an I-node number? Is it same as an inode?
     No, they are not same. inum or I-node number is an integer associated with a file. Whenever a new file is created, a unique integer number is generated in sequence and associated with the file. This number is nothing but the pointer to the inode structure which contains the meta data of the file. In fact, it is the inode number with which the operation system refers to the file during all the processes involving the file. Filename is only for the user to refer it.
Note: No two files in a file system can have the same inode number(except the hard links).

3. If the Unix operating system uses the inode number internally, where does the mapping of inode number with the file name happens?
  At the directory in which the file is present. Any directory structure in Unix maintains a table which contains the list of the file names along with their inode numbers. So, when a process in Unix refers to a file, it is through this mapping at the directory level, the process gets to know the inode number of the file.

4. How can we find the inode number of a file?
      The ls command has an option "-i" which will display the inode number of a given file. For example, to find out the inode number of a file say file1: ls -i file1

5. How can we access the information present in the inode structure for a file?
      In fact, when we execute our favorite command  ls -l for detailed listing of files, most of the output displayed such as the file size, modification times, are being fetched from the inode data structure.

6. Is there any way to find out  all the information of an inode?
     stat command in Unix does exactly this. Example: stat file.  The output contains the inode number, file size, the device to which it belongs to, access/modification/change times of the file, block size, access permissions, etc. In fact, this contains all the information present in the inode data structure.

7. Explain about the i-node number generation?
   When a file system is created, allocation is also done for the number of inodes to be created in the file system. As said earlier, during a file creation, a new sequence number is created and associated with the file. And hence, there is a limit on the maximum number of inodes which can be created, which means there is a limit on the maximum number of files which can be created. 

8. Is it possible to know the inode limit usage for a file system?
    Yes. The df command with the "-i" option does this. Example:  df -i.  The output contains all the file systems along with the total number of inodes in the file system, inodes uesd and inodes remaining.

9. What will happen if the file system runs out of inodes?
     Just like how a system can crash due to lack of storage space, the same is applicable in case of inode numbers as well. If all the available inode numbers are consumed, no more files can be created in the filesystem.

10. As a user, if I know a inode number, can I find out the file associated with it?
    Yes, using the find command. Say, for an inode number 99047, the command to find the file is:
     find . -inum 99047 

3 comments:

  1. On point (6) 'stat'; 'stat' may not be available as a handy stand alone utility (e.g. Sun boxes). ...and you may not have a C compiler to call stat(2). perl provides a stat builtin; This is likely available on many servers.

    ReplyDelete
  2. Nice step by step instruction. Thanks.

    ReplyDelete
  3. how many inodes are associated with one single file. if it is more than one then please explain the same.. what is hardlink and softlink and how they are associated with Inode. if inode is meta data then what is the block called in which actual file is stored...?

    ReplyDelete