Wednesday, April 11, 2012

Inode

Inode means index node.
Inode is an internal representation of a file. We identify a file by it's name, while the kernel identifies the file by it's inode number. Every file has one inode, but it may have several names, which map into the inode. Each name is called a link. So if any process calls a file, kernel retrieves the inode for that file. When a new file is created, kernel assigns it an unused inode.

Inodes exist in a static form on disk, and the kernel reads them into an in-core(memory) inode to manipulate them.

Disk inodes consist of following fields
  •  File owner identifier. Ownership is divided between an individual owner and a "group" owner and defines the set of users who have access rights to a file. The superuser has access rights to all files in the system.
  • File type. Files may be of type regular, directory,character or block special, or FIFO(pipes).
  • File type permissions.
  • File access times - when the file was last modified, when it was last accessed.
  • Number of links to file, representing the number of names for the file.
  • File data distribution in the physical disk. Although users treat data in a file as a logical stream of bytes, the kernel saves the data in discontiguous disk blocks.
  • The inode identifies the disk blocks that contain the file data.
  • File size

Distinction between writing the contents of an inode to disk and writing the contents of a file to disk

The contents of a file change only when writing it. The contents of a inode change when changing the contents of a file or when changing it's owner, permission or link settings. Changing the contents of a file automatically implies a change to the inode, but changing the inode does not imply that the contents of a file change.

What does in-core(memory) copy of the inode contain?

The in-core copy has the following details in addition to the fields of the disk inode
  1. Status of in-core inode, indicating if
  • inode is locked
  • process is waiting for inode to get unlocked
  • the in-core representation of file differs from disk copy as a result of change  to file data.
  1. The logical device number of the fiesystem that contains the file
  2. The inode number. Since inodes are stored in a linear array on disk, the kernel identifies the number of a disk inode by it's position in the array. The disk inode does not need this field. 
  3. Pointers to other in-core inodes
  4. A reference count, indicating the number of instances of that file that are active(such as when opened). An inode is active when a process allocates it, such as opening a file. An inode is on the free list only if the reference count is 0, meaning that the kernel can allocate the in-core inode to another disk inode. The free list of inodes thus servers as cache of inactives inodes. If a process attempts to access a file whose inode is not in the in-core inode pool, the kernel allocates an in-core inode from the free list for it's use.

No comments:

Post a Comment