Wednesday, May 21, 2014

Index Node for dummies

Inode on UNIX based systems is a data structure used to represent a file system object. The inode contains the information required by the process for accessing the files. Inodes exist in a static form on disk. Whenever any modification is required the kernel reads disk inode into an in-core inode.

The disk inode contains the following fields:

·         File owner identifier: Ownership is divided between an individual and a group owner.
·         File type: Files may be of various types as mentioned below:
o   Regular File
o   Directory
o   Character Device
o   Block Device
o   FIFO (named pipe)
o   Symbolic Link
o   Socket
·         File access permissions: The system protects files according to three classes namely the owner, the group owner & other users. Each class access rights (Read, write & Execute) can be set individually.
·         File access times: This field specifies the time the file was last modified, accessed and when the inode was last modified.
·         Links: Number of hard links.
·         Table of contents for the disk addresses of data in a file. Users treat the data in a file as a logical stream of bytes but the kernel saves the data in discontiguous disk blocks. The inode identifies the disk blocks that contain the file’s data.
·         File size

The in-core copy of the inode contains the following fields in addition to the fields of the disk inode:

·         Status: The status of the in-core inode, indicating whether:
  •    the inode is locked
  •    a process is waiting for the inode
  •    the file is a mount point
  •    the in-core representation of the inode differs from the disk copy as a result of a change to the data in the inode
  •    the in-core representation of the file differs from the disk copy as a result of a change to the file data

·         The logical device number of the file system that contains the file.
·         The inode number. Inodes are stored in a linear array, the kernel identifies the number of a disk inode by its position in the array. The disk inode does not need this field.
·         Pointers to other in-core inodes.
·         A reference count indicating the number of instances of the file that are active.

The VFS inode structure contains lot more fields. Most interesting structures are inode_operations (i_op) & file_operations (i_fop).

How to get inode number for a file under Linux?
We can get the inode numbers using either of the following commands:
·         ls -i
·         stat

Does Windows have an inode number for uniquely identifying the file?
Windows has a structure named as _FILE_INTERNAL_INFORMATION which contains the 8-byte file reference number for the file. This number is assigned by the file system and is file-system-specific.  File reference numbers, also called file IDs, are guaranteed to be unique only within a static file system.