Let’s think about a simple question today. On Linux you create an empty file with the touch command:

touch empty_file.txt
Copy the code

Will it consume some of our disk space after the operation is complete? How much, if any, can it possibly consume? Well, yes, this question is easier than you think, but I wonder if you can give yourself a satisfactory answer.

My previous articles have covered the physical composition of disks, but they may not be enough to help you understand file-related issues. Starting today, let’s go from the physical layer up to Linux file system principles to find the answer.

Practice is the mother of wisdom

I thought it might be more interesting to leave the kernel behind and experiment with it. You probably know that ls is a command that allows you to check file sizes, so let’s use it.

# touch abcdefghigklmn.txt  
# ls -l  
total 0  
-rw-r--r-- 1 root root 0 Aug 17 17:49 empty.file  
Copy the code

Well, the ls command tells me that the empty file occupies 0. The size of the file is indeed 0 because we haven’t written anything to it yet. But now we need to think about whether an empty file takes up disk space. So our intuition tells us that this is absolutely impossible, how can an extra file on disk cost no space!

To solve this mystery, you also need to use the DF command. Enter df – I

# df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
......
/dev/sdb1            2147361984 12785019 2134576965    1% /search

Copy the code

This output helps us show the inode usage in our file system. Note that IUsed is 12785019. Let’s go ahead and create an empty file

# touch empty_file2.txt
df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
......
/dev/sdb1            2147361984 12785020 2134576964    1% /search
[@bjzw_46_76 temp]#
Copy the code

Notice that IUsed becomes 12785020.

Ha ha, we have a conclusion. Creating an empty file takes up an Inode.

Elaborate the inode

What information about files is stored in inodes? Let’s take a look at the kernel source code again. You can download a copy of the Linux source code. In the case of the ext2 file system, the kernel’s definition of inode structures can be found in the fs/ext2/ext2.h file I downloaded from Linux-2.6. This structure is relatively complex and mainly stores some other data besides the file content. Let’s select some key ones and intercept them:

struct ext2_inode { __le16 i_mode; # file permission __le16i_uid; # file owner ID __le32i_size; # file size __le32i_atime; # the last time the file was accessed __le32i_ctime; # file creation time __le32i_mtime; __le32i_dtime; # time when the file was deleted __le16 i_gid; # group ID __le16i_links_count; # number of times the inode of this file is joined __le32i_blocks; # Number of blocks to file...... __le32 i_block[EXT2_N_BLOCKS]; # point to an array of blocks storing file data......Copy the code

You can see that the user and access time related to the file are stored in the inode. In addition, there is a VFS level inode definition in include/ Linux /fs.h, which we will not spread out here. Using the stat command, you can directly see the data in the file inode.

# stat test
  File: `test'
  Size: 0               Blocks: 0          IO Block: 1024   regular empty file
Device: 801h/2049d      Inode: 26          Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-01 12:14:31.000000000 +0800
Modify: 2020-03-01 12:14:31.000000000 +0800
Change: 2020-03-01 12:14:31.000000000 +0800
Copy the code

How big is each inode? Dumpe2fs can tell you (xfs_info for XFS).

# dumpe2fs -h /dev/mapper/vgroot-lvroot dumpe2fs 1.41.12 (17-May-2010)...... Inode size: 256Copy the code

Inode size Indicates the size of each Inode. On my machine, each inode is 256 bytes. The size of the two inodes is exactly aligned to 512 bytes of the disk sector.

Where is the file name

There is no file name in the inode. So what happened to file names?

In fs/ext2/ext2.h, I found the following folder structure

struct ext2_dir_entry {
         __le32  inode;                  /* Inode number */
         __le16  rec_len;                /* Directory entry length */
         __le16  name_len;               /* Name length */
         char    name[];                 /* File name, up to EXT2_NAME_LEN */
};

Copy the code

This structure is our familiar folder. Yes, the file name is stored in the folder data structure that it belongs to, which is the char name[] field. Along with the file name, the folder also records information such as the inode of the file.

conclusion

    1. Creating an empty file consumes an inode to store metadata such as the user and creation time.
    1. Creating an empty file also consumes a certain amount of space in the block of all its directories, which is used to store the filename, permissions, time, and so on

So, it looks like a new empty file, if you want to dig, can really dig up a lot of knowledge. Finally, I would like to share a fault encountered by a student in our team. One of our offline task machines crashed. After rebooting, we checked the cause because inode was used up. A further trace found that a process had created too many empty log files. Files are empty, but inodes are wasted. Later, I asked the student in charge to modify the logic of creating log files and delete the extra empty files, and the machine returned to normal.



The development of internal training of hard disk album:

  • 1. Disk opening: remove the hard coat of mechanical hard disk!
  • 2. Disk partitioning is also a technical trick
  • 3. How can we solve the problem that mechanical hard disks are slow and easy to break?
  • 4. Disassemble the SSD structure
  • 5. How much disk space does a new empty file occupy?
  • 6. How much disk space does a 1-byte file occupy
  • 7. Why is the ls command stuck when there are too many files?
  • 8. Understand formatting principles
  • 9. Read file How much disk IO actually occurs per byte?
  • 10. When to initiate disk I/O write after one byte of the write file?
  • 11. Mechanical hard drive random IO is slower than you might think
  • 12. How much faster is an SSD server than a mechanical one?

My public account is “developing internal Skills and Practicing”. Here I am not simply introducing technical theories, nor only introducing practical experience. But to combine theory with practice, with practice to deepen the understanding of theory, with theory to improve your technical practice ability. Welcome you to follow my public number, also please share with your friends ~~~