Showing posts with label inodes. Show all posts
Showing posts with label inodes. Show all posts

2012/12/30

Inodes in Linux

What is a inode, Why we need them ?

Have you ever wondered where is your access permissions located? How does your system know that you are the owner of your home folder? Is it written on the file itself ?

Answer to your questions is the Inodes also known as Index nodes.

Inode is a data structure, inodes stores all data about the file objects, except data content and file name. Unix doesn’t use the file name to refer to the file, it uses the inodes and inodes are pointing to actual block addresses where the data is located. inodes reside in Inode table (see Ext4 Disk Layout)

Windows (NTFS) equivalent is Master file table
Max OSX (HFS) equivalent is Catalog File

POSIX standard description for inode is:


  • File size in bytes
  • Owner and Group of the file
  • Device ID (device where file is contained)
  • file mode (access permissions)
  • Timestamps (Content last modified (mtime), Inode last modified(ctime) and Last accessed (atime))
  • Link count (how many hard links point to the inode)
  • Pointers to actual disk blocks that are containing the data
  • Additional system and user flags (limit its use and modification)


Unix or Linux systems doesn't usually store creation date, but ext4 has an attribute for creation date.

crtime (Create time) and also a dtime (Delete time)

inodes allows us to do linking and gives us significant performance increase, because inodes points us to the right data block when we are querying for files.

You can run out of inodes, what happens then ?

inode limit is decided at file system creation time. Its not a fixed value. You can check your current inodes usage with df -i


 Filesystem      Inodes  IUsed  IFree IUse% Mounted on  
 /dev/sda1      14983168  95016 14888152  1% /  

If you happen to run out of Inodes before you run out of disk space, you simply cannot create any more files or directories that consume Inodes, your things start to get very messy and unstable :)

stat command

stat displays file or file system status.

You can query file or file system with stat, for example for file:

 m@srv:~/symlink_test$ stat file1  
  File: `file1'  
  Size: 65         Blocks: 8     IO Block: 4096  regular file  
 Device: 801h/2049d     Inode: 5506851   Links: 1  
 Access: (0644/-rw-r--r--) Uid: ( 1000/  m)  Gid: ( 1000/  m)  
 Access: 2012-12-30 14:38:23.983590932 +0200  
 Modify: 2012-12-30 14:38:49.112092037 +0200  
 Change: 2012-12-30 14:38:49.112092037 +0200  

and for file system (use -f flag)

 m@srv:~/symlink_test$ stat -f /dev/sda1  
  File: "/dev/sda1"  
   ID: 0    Namelen: 255   Type: tmpfs  
 Block size: 4096    Fundamental block size: 4096  
 Blocks: Total: 191863   Free: 191821   Available: 191821  
 Inodes: Total: 191863   Free: 191262  


Inode data pointers, direct blocks and indirect blocks

One inode can point only to 12 blocks of data.

So if your block size is 4096, that would mean 12x4096 = 49152 bytes (48kb). This limitation would suck so that's why inode also has 3 indirect block pointers.

Indirect block pointer points to a block of block pointers, which.. again can point to indirect blocks that will point to new set of block pointers! This allows us to have very large files in our file system.

I tried to illustrate this with a picture but i ended up with a big messy pile of lines. Please check the wikipedia page for more info.


2012/12/29

Linux Symbolic links: Soft and Hard links

What is a symbolic link? Why its important ?

Symlink (Symbolic link) is a reference to another file or directory. It allows you to point multiple file names to single inode that points to a physical block address on the disk.

Symlink's  are mostly used as a shortcut's, they can make your life a lot easier by making your directories of data available for you where you want it.

How do i make a symlink ?

Use the following option in ln command:

 -s, --symbolic
              make symbolic links instead of hard links

For example:

 ln -s file1 file1_symlink  
 ls -l
 -rw-r--r-- 1 m m 0 2012-12-29 14:57 file1  
 lrwxrwxrwx 1 m m 5 2012-12-29 14:58 file1_symlink -> file1  


How do i make a hard link ?


 ln file1 file1_hardlink  
 ls -l  
 -rw-r--r-- 2 m m 0 2012-12-29 15:03 file1  
 -rw-r--r-- 2 m m 0 2012-12-29 15:03 file1_hardlink  

What is the difference between soft and hard link ?

Most important difference between hard and soft is that soft link depends on the original file.

For example, if i create a soft link and delete the original file that i linked, i cannot access my file trough symlink anymore and it becomes broken link.

 m@srv:~/symlink_test$ cat file1_symlink   
 Hello World!  
 m@srv:~/symlink_test$ rm file1  
 m@srv:~/symlink_test$ cat file1_symlink   
 cat: file1_symlink: No such file or directory  

But in the case of hard link, my file is always accessible if there is one hardlink existing of the file

 m@srv:~/symlink_test$ cat original  
 Hello World!  
 m@srv:~/symlink_test$ ln original hard_linked  
 m@srv:~/symlink_test$ cat original hard_linked   
 Hello World!  
 Hello World!  
 m@srv:~/symlink_test$ rm original   
 m@srv:~/symlink_test$ cat hard_linked   
 Hello World!  

Also hard links do not link paths on different volumes or file systems, soft links do. Soft links also consume inodes but hard links always shares the same inode.

Where is the symbolic link stored ?

Symlinks are just references to a physical location of a file, if you make symlinks, its doesn't meant that you are losing disk space by creating a copies. Symbolic links are stored in the same place as inodes, inodes are the place where disks resides its block addresses.