Inode Basics
Inode Basics
An Inode number points to an Inode. An Inode is a data structure that stores the following information about a file or
directory:
Size of file
Device ID
User ID of the file
Group ID of the file
The file mode information and access privileges for owner, group and others
File protection flags
The timestamps for file creation, modification etc
link counter to determine the number of hard links
Pointers to the blocks storing file’s contents
Please note that the above list is not exhaustive. Also, the name of the file is not stored in Inodes (We will come to it later).
When a file is created inside a directory then the file-name and Inode number are assigned to file. These two entries are
associated with every file in a directory. The user might think that the directory contains the complete file and all the extra
information related to it but this might not be the case always. So we see that a directory associates a file name with its Inode
number.
When a user tries to access the file or any information related to the file then he/she uses the file name to do so but internally
the file-name is first mapped with its Inode number stored in a table. Then through that Inode number the corresponding
Inode is accessed. There is a table (Inode table) where this mapping of Inode numbers with the respective Inodes is
provided.
Most input and output devices and directories are treated as files
in Linux. If you have sufficient permissions, you can directly
read all these devices using their file system names. Recent
versions of Unix/Linux have evolved directories into non-
readable (non-file) objects.
Most diagrams showing file systems and links in Unix texts are
wrong and range from confusing to seriously misleading. Here’s
the truth, complete with an ASCII-art file system diagram below.
Names for inodes (names for files, directories, devices, etc.) are
stored on disk in directories. Only thenames and the
associated inode numbers are stored in the directory; the
actual disk space for whatever data is being named is stored in
the inode, not in the directory. The names and numbers are kept
in the directory; the names are not kept with the data.
In the directory, beside each name, is the index number (inode
number) indicating where to find the disk space used to actually
store the thing being named. You can see this name-inode
pairing using ls -i:
$ ls -i /usr/bin/perl*
266327 /usr/bin/perl 266329 /usr/bin/perldoc.stub
266327 /usr/bin/perl5.14.2 266330 /usr/bin/perlivp
266331 /usr/bin/perlbug 266331 /usr/bin/perlthanks
266328 /usr/bin/perldoc
The crucial thing to know is that the names and the actual
storage for the things being named are inseparate places. Most
texts make the error of writing Unix file system diagrams that
put the names righton the things that are being named. That is
misleading and the cause of many misunderstandings about
Unix/Linux files and directories. Names exist one
level above (separate from) the items that they name:
WRONG - names on things RIGHT - names above things
======================= ==========================
The line leading downwards from the name bin in the ROOT
directory indicates that the name bin is paired with an inode
number that is another directory inode containing the list of
names in the bindirectory, including names ls and rm (and
others). The line leading down from ls in the bin directory inode
leads to the data inode for the file /bin/ls. There is no name kept
with the data inode – the name is up in the directory above it.
The actual data for each Unix file or directory stored on disk is
managed by numbered on-disk data structures called “inodes”
(index nodes). One inode is allocated for each file and each
directory. Unix inodes have unique numbers, not names, and it
is these numbers that are kept in directories alongside the
names. The -i option to ls shows these inode numbers.
File system names are stored in directory inodes. The names are
not kept in the same inodes with the things that they name.
The name of a file or directory is not kept in the inode with the
file attributes or pointers to disk blocks; the name is kept in a
directory somewhere else.
Reading a Unix directory tells you only some names and inode
numbers; you know nothing about the types, sizes, owners, or
modify times of those inodes unless you actually go out to the
separate inode on disk and access them to read the attributes.
Without actually accessing the inode, you can’t know most of the
attributes of the file system object; you can’t even know if the
inode is a file inode or a directory inode.
To find out attribute information of some file system object,
which is stored with the inode, not in the directory, you must
first use the inode number associated with the object to find the
inode of the item and look at the item’s attributes. This is
why ls or ls -i are much faster than ls -l:
The name of an item (file, directory, etc.) and its inode number
are kept in a directory. The directory storage for that name and
number is managed by its own inode that is separate from the
inode of each thing in the directory. The name and number are
stored in the directory inode; the data for the item named is
stored in its own inode somewhere else.
Inode 123 may be paired with the name cat in one directory and
the same 123 may be paired with the name dog in the same or a
different directory. Either name leads to the same 123 file inode
and the same data and attributes. Though there appear to be
two different files cat and dog in the directory, the only thing
different between the two is the name – both names lead to the
same inode and therefore to the same data and attributes
(permissions, owner, etc.).
Multiple names for the same inode are called “hard links”.
The ln command can create a new name (a new hard link) in a
directory for an existing inode. The system keeps a “link count”
in each inode that counts the number of names each inode has
been given. The rm command removes a name (a hard link) from
a directory, decreasing the link count. When the link count for
an inode goes to zero, the inode has no names and the inode is
recycled and all the storage and data used by the item is
released.
1. /home/alex/foobar
2. /home/alex/literature/barfoo
The above ROOT directory has the name home in it, paired with
inode #5. The actual disk space of the directory home is not here;
only the name home is here, alongside of its own inode number #5.
To read the actual contents of the home directory, you have to
find the disk space managed by inode #5somewhere else on disk
and look there.
The ROOT directory is the only directory that is its own parent. If
you look at the ROOT directory above, you will see that both the
name . and the name .. in this ROOT directory are paired with
inode #2, the inode number of the ROOT directory. Following
either name . or .. will lead to inode #2 and right back to this
same ROOT inode.
The name home for this inode isn’t in this inode; the name home is
up in the ROOT directory. This homedirectory has the name alex in
it, paired with inode #31. The directory alex is not here; only
the name alex is here. To read the alex directory, you have to
find inode #31 on disk and look there. (In fact, until you look up
inode #31 and find out that it is a directory, you have no way of
even knowing that the name alex is a name of a directory!)
The name alex for this inode isn’t in this inode; the name alex is
up in the home directory. This alexdirectory has the name foobar in
it, paired with inode #12. The file foobar is not here; only
the name foobar is here. To read the data from file foobar, you
have to find inode #12 on disk and look there. (In fact, until you
look up inode #12 and find out that it is a plain file, you have no
way of even knowing that the name foobar is a name of a plain
file!)
Let us move to the storage space for the foobar file at inode #12.
The name foobar for this inode isn’t in this inode; the
name foobar is up in the alex directory. This foobarinode is a file
inode, not a directory inode, and the attributes of this inode will
indicate that.
The inode for a file contains pointers to disk blocks that contain
file data, not directory data. There are no special directory
names . and .. in files. There are no names here at all; the disk
block pointers in this inode point to just file data (whatever is in
the file).
The alex directory inode #31 above says to follow the trail to
the literature name we must go to inode #7. (We won’t know
whether the #7 inode for literature is a file or a directory until we
get there!)
The name literature for this inode isn’t in this inode; the
name literature is up in the alex directory inode #31.
This literature directory inode #7 has the name barfoo in it, paired
with inode #12. The actualdata for the thing that is barfoo is not
here; only the name barfoo is here. You will recall that we have
seen inode #12 in the previous trace.
Above, in the alex directory (inode #31), inode #12 was also paired
with the name foobar. In the literature directory (inode #7),
inode #12 is paired with the name barfoo. Inode #12 has two
different names; names foobar and barfoo are both hard links to
the same inode #12:
$ ls -i /home/alex/foobar /home/alex/literature/barfoo
12 /home/alex/foobar 12 /home/alex/literature/barfoo
Two names means the “link count” of inode #12 is set to “two”.
Both names lead to the same #12 inode and thus to the same
data and same attributes. This is one single file with two names.
A change to the file data using the name foobar changes the data
in inode #12. That changes file data for the name barfoo too;
because, foobar and barfoo are two names for the same #12 inode
storage – they are two names that point to the same storage
inode.
Everything about data inode #12 except its name is kept with the
inode. The only thing different in a long listing
of foobar and barfoo will be the names; everything else (file type,
permissions, owner, group, link count, size, modification times,
etc.) is part of inode #12 and must therefore be identical for the
two names. Neither name is more “original” than the other; both
names have equal status. To release the #12 inode storage, you
have to delete both names (so the link count drops to zero).
9 Path Traversal
Let’s use the above inode data to follow a valid path such as:
/home/alex/literature/barfoo
Start on the left and walk the tree to the right. To be a valid
Unix path, everything to the left of the rightmost slash must be
a directory. (Thus, ROOT, home, alex, and literature must be
directories, if this is a valid pathname.)
Start with the nameless ROOT directory in front of the first slash
(ROOT doesn’t have a name, since it does not appear in any
parent directory) and look for the first pathname component
(home) inside that directory (inside inode #2).
Let’s trace the pathname:
Look in the ROOT directory (located in inode #2) for the name of
the first pathname component: home. We find the
name home inside the ROOT directory, paired with inode #5. Go
back out to the disk to find the home directory that is inode #5.
Note how the names are separate from the things they name. The
actual directory inode of home(#5) is not the same as the inode of the
ROOT directory containing the name home (#2). The name is in a
different place than the thing it names.
In the directory that has the name home (inode #5), look for the
name alex. We find alex paired with inode #31. Go back out to the
disk to find the alex directory that is inode #31. Again, the
name alexcontained in directory home (inode #5) is separate from
the inode that is the actual directory alex (inode #31).
In the directory that has the name alex (inode #31), look for the
name literature. We find literaturepaired with inode #7. Go back
out to the disk to find the literature directory that is inode #7.
(Again, the name literature contained in inode #31 is separate
from the inode #7 that is the actual directory literature.)
You now have the disk node (inode) that is your file data:
inode #12. If that file inode has appropriate permission attributes,
you can read it or write it. It is the permission attributes on the
inode containing the file data that govern what you can do with
the data. The permissions on the directory containing
thename of the file don’t matter, once you have found the inode
containing the file data itself.
You can have no permissions on the inode of the file (it may
even be owned by some other user) and still be able to rename
or remove the name of the file from a directory on which you do
have permissions.
Names are separate from the things that they name. The
permissions are also separate.