Thursday, July 3, 2008

Inodes and Links

INODES
When a filesystem is created in a UNIX OS, a range of inode numbers are assigned to it. Any file/directory created under this filesystem gets an inode number from the list. So, there is an association of a file/directory with an inode number and an inode provides access to an OS datablock.

In general, inodes contain two parts logically.
First: inodes contain information about the file, including its owner, its permissions, and its size.
Second: inodes contain pointers to data blocks associated with the file.

The first field of a 'ls -li' command output denotes the inode for a corresponding file/directory. No two files/directories can have the same inode numbers even it be from different filesystems. One can assume that a single inode number is a unique number in the whole of OS.

LINKS
A link can be thought of as a pointer from one file to another.The third field of a 'ls -li' command output denotes the number of links created for a file/directory.Links are of two types.

Hard links
Syntax : ln source_filename link_name

Hard link always increment the value of the link field for a particular file/directory. The link number for a file/directory is directly proprotional to the number of hard links created for the particular file/directory

UNIX-$> ls -ltri one
121674 -rwxr-xr-x 1 oracle oracle 184 Feb 22 12:07 one

In the above output the third field is equal to 1. Now, a hard link is created for the file 'one'

UNIX-$> ln one one_1UNIX-$> ls -ltri one*

121674 -rwxr-xr-x 2 oracle oracle 184 Feb 22 12:07 one_1
121674 -rwxr-xr-x 2 oracle oracle 184 Feb 22 12:07 one

The above example shows the filename alone differs, but everything else remains unchanged. Both the files have the same inode number and same number of links for them. This is the only reason why a hard link cannot span across different filesystem, since the linked files all share the same inode number and inode numbers come in a specific range for different filesystems. Removing one of the two files does not affect the existence of the other.

UNIX-$> rm one
UNIX-$> ls -ltr one*

121674 -rwxr-xr-x 1 oracle oracle 184 Feb 22 13:19 one_1

Soft links
Syntax : ln -s source_filename link_name

This is also called a symbolic link but more commonly known as soft link in technical jargon(As far as I know). Soft links do not allow the filenames to share the same inode numbers. A point to be noted about a soft link is it has an 'l' variable in the user permissions field of the 'ls -ltri' command output. Soft links can span across file systems. Depending on the location of the soft link, inode number changes.

If the soft link is created on the same filesystem as that of the source file then it gets an inode number from the range of the current filesystem inodes else it gets an inode from the range of the target file system on which it is created.

UNIX-$> ln -s one_1 one_2
UNIX-$> ls -ltri one*

121674 -rwxr-xr-x 1 oracle oracle 184 Feb 22 13:19 one_1
121644 lrwxrwxrwx 1 oracle oracle 3 Feb 22 13:20 one_2 -> one_1

The above soft link is created in the same location as that of the source file. The one shown below is created in a different filesystem, hence a different inode number range.

846 lrwxrwxrwx 1 oracle oracle 30 Feb 22 13:21 one_3 -> /ora1/oracle/dba/one_1

In both the examples above, the link number is unchaged, but the size differs and also the name of the link has a pointer to the original file. So a soft link only stores a pointer to the original file. However commands like vi,cat,more or pg etc. on 'one_3' or 'one_2' run as if it were executed on the original file.

Removing a soft link is a cautious job, one should not use the softlink name along with the pointer to remove a softlink as it has adverse effects.

UNIX-$> rm one_2 -> one_1

The above command will either remove the source file along with the softlink or it could even make the source file empty. The advisable command to remove a soft link is as shown below.

UNIX-$> rm one_2
UNIX-$> ls -ltri one*

121674 -rwxr-xr-x 1 oracle oracle 184 Feb 22 13:19 one_1

It is hoped that this material provides a simple but effective explanation of Inodes and Links.

No comments: