The Linux File System basics

Each file in a Linux file system (eg, ext2, ext3) is described by an Inode, and every inode is identified by a unique number.
An Inode is a datastructure that describes the blocks of data the file occupies, as well as, metadata about the file like, the file’s owner and group owner, the file’s access permissions(eg rwx-r–r–), it’s size in bytes, timestamps telling when the file was last modified and last accessed and when the file’s status (like ownership etc) was last changed and a count telling how many hard links point to the inode.
It should be noted that the filename is not stored in the inode.

A File’s name along with it’s inode number are stored in special files called directories. In other words, a directory is just another file, which is treated specially by the linux, and it contains a list of filenames along with their inode numbers. All these list of files are assumed to be contained within that directory.

Use ls -i to see the inode numbers of files and directorys .

[tony@localhost ~]$ ls -ila
total 820
65538 drwx-----x 57 tony tony    4096 Feb 22 12:18 .
65537 drwxr-xr-x  4 root root    4096 Jul  6  2009 ..
65997 drwx------  3 tony tony    4096 Jul  1  2009  adobe
66027 -rw-rw-r--  1 tony tony     352 Feb 18 21:56 .aspell.en.prepl

All the inodes of a file system are created at the moment we format the disk (eg, when we use mkfs command). Thus, the maximum number of inodes (and so the maximum number of files) that the filesystem can have, is determined during the filesystem’s creation itself.
These inodes are all kept together in inode tables towards the beginning of the partition. The inode tables usually occupy about 1% of the partition’s space when using the default settings.
The decision as to how many inodes to create is made on Linux using an algorithm. The default setting creates an inode for every 2K bytes contained in the filesystem, but the number can be adjusted by the user when creating the filesystem. For example, it can be wise to create fewer inodes when setting up a filesystem that will contain just a few large files.

It should be noted that, there are two ways in which a filesystem can run out of space: it can consume all the space for adding new data (i.e., to existing files or to new files), or it can use up all the available inodes even when space is left for adding data. I tried to check this myself in a test filesystem, see here if you are intersted.
Use the command df -i to see the number of inodes available, used and free.

[tony@localhost ~]$ df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/mapper/CentOsVG-RootLV
4161536  155678 4005858    4% /
/dev/sda9              26104      40   26064    1% /boot

Whenever a file is created, the kernel assigns a free inode to that file. The inode is updated to reflect the details of the file. The file’s name, along with that free inode’s number, is written to the directory, the file is supposed to be in.

Hard links and Symbolic links

Hard links are different filenames that point to same inode number (in essence to the same file).
A soft link or symbolic link is a file whose data contains the ‘path’ (eg, /home/tony/resume.txt) of another file(its target).

Since inode numbers are unique only within a filesystem, hard links cannot span across partitions or different filesystems. Where as softlinks can be to anywhere as long as a valid path is there to its target.

[tony@localhost test]$ echo "this is just some contents" > mytestfile  //create a file
[tony@localhost test]$ ln mytestfile myhardlink              //create a hardlink
[tony@localhost test]$ ln -s mytestfile mysoftlink           //create a softlink
[tony@localhost test]$ ls -li
total 8
2687664 -rw-rw-r-- 2 tony tony 27 Feb 22 14:59 myhardlink
2687665 lrwxrwxrwx 1 tony tony 10 Feb 22 14:58 mysoftlink -> mytestfile
2687664 -rw-rw-r-- 2 tony tony 27 Feb 22 14:59 mytestfile

Note in the above shell dialogue that the hardlink and the original file has the same inode number, and the same size.
The soft link has different inode number and of different size, obviously as it is another file whose data has the path to ‘mytestfile

Virtual File System

On top of the underlying filesystem, the linux maintains a Virtual FileSystem (VFS). The VFS is an interface for the operating system, system services, and programs to the underlying filesystem.
VFS allows Linux to support many, often very different, file systems, each presenting a common software interface to the VFS. All of the details of the underlying file systems are translated by VFS so that all file systems appear identical to the rest of the Linux kernel and to programs running in the system.
Each filesystem type supported by your VFS will have an associated driver routines loaded in to the kernel.
Linux’s Virtual File system layer allows you to transparently mount the many different file systems at the same time.

The /proc File System

The /proc file system really shows the power of the Linux Virtual File System. It does not really exist; neither the /proc directory nor its subdirectories and its files actually exist.
The /proc file system, like a real file system, registers itself with the Virtual File System. However, when the VFS makes calls to it requesting inodes as its files and directories are opened, the /proc file system creates those files and directories from information within the kernel.
For example, the kernel’s /proc/devices file is generated from the kernel’s data structures describing its devices.
The /proc file system presents a user readable window into the kernel’s inner workings.

References
http://tldp.org/LDP/tlk/fs/filesystem.html
http://www.linfo.org/inode.html

Leave a Reply

Your email address will not be published. Required fields are marked *


× 4 = sixteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>