Search code examples
linuxfilesystemsinode

How inode number is distributed in a linux root filesystem?


I have several questions on how inode number is used in linux filesystem. These questions may be related but for clearness I will ask them one by one.

Q1: Take EXT2/3 for example, from "Design and Implementation of the Second Extended Filesystem" I know that there are inode tables in block groups, and I know inode presents file, but what if there are too many files but the inode numbers are not enough? My best guess is that filesystem does nothing if all its inode numbers were exhausted. And if some file get deleted later, its inode may be recycled.

Q2: I run "df -i" on my Linux server and I get this:

Filesystem            Inodes       IUsed   IFree      IUse% Mounted on    
    /dev/sda1          1313280    7853 1305427    1%     /
    devtmpfs                   0       0       0              -       /dev
    tmpfs                 525298       4  525294        1%    /dev/shm
    /dev/sda2              65808      50   65758       1%    /boot
    /dev/sda5            1313280     146 1313134    1%   /opt
    /dev/sda6             655776      37  655739      1%   /tmp
    /dev/sda7             655776    5219  650557    1%   /home
    /dev/sda8            1313280     840 1312440    1%  /var
    /dev/sda9             655776   36529  619247    6%  /usr
    /dev/sda10              6432      11    6421        1%  /crash
    /dev/sda12            135488      11  135477     1%  /usr/local/instances
    tmpfs                 525298       3  525295    1% /var/run/xenstored
    tmpfs                 525298       3  525295    1% /var/lib/xenstored
    tmpfs                 525298       3  525295    1% /var/lib/xend/socket
    tmpfs                 517536       3  517533    1% /var/run/libvirt/socket

I see that each filesystem has its own inode counts and these filesystems (like devtmpfs and tmpfs) are all mounted to root file system. I guess each filesystem have their own inode tables, but are inode numbers of different filesystems distributed in different ranges, like root filesystem is [0, N] and tmpfs is [N+1, M]?

If inode numbers of different filesystems are in different ranges (which I don't think so), how filesystems make an agreement on range division?

If inode numbers of different filesystems are generated based on the same rule (like all start from 0), can different filesystems conflict if we mount them together? As I know, inode presents files in running OS, what the OS will do if a file in root filesystem has inode number N and another file in another filesystem (like tmpfs) also has the inode number N?

PS. I'm kind of new to Linux and I'm not coming from a English-speaking country, sorry for if I didnot make clear expression. Thanks in advance.


Solution

  • The full unique identifier for a file is (st_dev, st_ino). This means that inode numbers do not have to be unique across filesystems and every file is still uniquely identified.

    This is why you cannot have hardlinks that cross devices. Directory entries only contain an inode number, not a device number. So directory entries can only refer to files on the same filesystem as the directory is on.

    Even if you added a device field to the directory entry, it would be meaningless and device numbers can change when you add or remove drives and only refer to a particular device while a system is running. Also, how would you uniquely number all of the devices in the entire world. With USB drives, for example, you can take a given device from machine to machine. How would you ensure the device numbers stayed the same so the links still worked?

    Lastly, not all filesystem types have a fixed quantity of inode numbers. reiserfs and (I believe) btrfs dynamically allocate new inodes as needed, and so the total inodes field of the filesystem doesn't really reflect any sort of useful value.