How UNIX Filesystems Work in 2022
,

How UNIX Filesystems Work in 2022

How UNIX Filesystems Work in 2022

We explain the working principles of file systems on UNIX systems and the meaning of related terms.

EXT2, EXT3, EXT4 and UFS… are just four of the hundreds of file systems you can use on most UNIX-like systems. Of course, these file systems have their own differences, but they have more in common. If you wish, let’s start to examine these common points right away.

What is this “inode” concept?

Let’s start with the most basic point, the inode. The inode is the smallest building block that holds the metadata of the file. This metadata contains:

  • The name of the device where the file is located,
  • file serial number,
  • File type, file modes specifying how the owner and owner group can access it,
  • Number of “hard links” connected to the inode,
  • The user ID of the file owner and the group ID of the file,
  • If the file represents a device, the device ID is
  • The size of the file in bytes,
  • The date the file was last modified, modified and accessed in UNIX time (epoch) format,
  • It contains information such as the preferred number of I/O blocks and the number of blocks allocated to this file.

In most legacy filesystems, the maximum number of inodes was restricted at filesystem creation time. Therefore, the number of files that could be kept on disk was also decreasing. In such file systems, one inode was managed every 2000 bytes. Current file systems can extend themselves by taking advantage of data structures such as a B-tree or inode pointer structure .

Each file has a number next to the filename, which can be called the ID of the inode. We call this the inode number. The inode number is assigned by the file system during the creation of the file. All of these numbers are file specific and cannot be changed.

stat()We can retrieve the inode information of a file from the system with the help of a system call in UNIX systems . This call retrieves the inode number from the file, finds the corresponding inode and returns information about the inode. You can refer to this site for more information about this system call .

 

Windows and inodes

If you are familiar with Windows, we can say that the inode structure is similar to the MFT structure in the NTFS file system. The MFT structure keeps each file in the B-tree and each entry has a file ID. We can say that the only difference between inode and metadata is that the file permissions are obtained through a different API. Also, NTFS system has more complex disk spanning than UNIX file systems, for example, files smaller than ~900 bytes are stored in the same MFT entry as the folder. One of the biggest differences in NTFS compared to FAT is that it can create hard links.

Difference between hard link and symbolic link.

hard link

If you are familiar with the concept of “pointer”, the concept of hard link will not be foreign to you. A hard link is a direct reference to the inode of a file. By using a hard link, we can transfer a file to the folder we want, provided that it is kept in the same partition, or we can change its content. In fact, as a result of these operations, the hard link will still be connected to the file we want, because the inode is still active and the structure we call the hard link is actually the reference of the inode.

We can use the “ln” command to create a hard link. First of all, let’s point out that there is only a file named “test” in our folder.

The first version of our folder.

Let’s create a hard link and see the difference.

Our folder after the hard link is created.

Let’s change our “test_hardlink” file. I wonder if it will affect our “test” file, which is our main file?

We are changing our content on the hard link.

And the result:

Changed!

Bingo! As you can see, the changes we made to the hard link also changed the content of our original file!

Before moving on to symbolic links, let’s note that hard links do not change after copying the file to another location (cp), but after cutting and pasting the file (mv) it changes. Because the inode number does not change after copying, while it does after cutting and pasting.

symbolic link

Symbolic links are a direct reference to the file itself, rather than the inode. Just like a hard link, a change to a symlink changes the file itself. A file referenced with Symlink can be on any partition, device or folder. But if the location of the file referenced by the symlink changes, the symlink will be broken.

We will again use the “ln” command to create the symlink, but this time with the “-s” parameter.

We created a symlink, and our “ls” command showed our symlink in a different color.

As you can see, our “ls” command showed our symlink in a different color. Let’s open our symlink:

Surprise inside our symlink!

As you can see, the content of our original file came out of our symlink, even though we didn’t make any changes to the symlink!

In this article, we have explained the working principles of file systems in UNIX systems and a few helpful concepts. You can express your suggestions and opinions in the comments and ask your questions on Technopat Social .

About Us

Sed gravida lorem eget neque facilisis, sed fringilla nisl eleifend. Nunc finibus pellentesque nisi, at is ipsum ultricies et. Proin at est accumsan tellus.

Featured Posts