Linux file system soft link hard link

Source: Internet
Author: User

Tags: vi command cat install CLI subdirectory ann Add line errors


There are many implementations of UNIX file systems, such as UFS (BSD-based UNIX file systems), Ext3, Ext4, ZFS, ReiserFS, and so on.

Regardless of the file system, you always need to store the data. the minimum storage unit for a hard disk is a sector , and the smallest unit of data storage is not a sector, because the storage efficiency of the sector is too low. A sector has only 512 bytes, and the head is a sector of a sector to read, that is, if the file is 10MB, then in order to read this file, the head must be read 20,480 times. Such efficiency is extremely low.

Logical Block

In order to improve efficiency, there is the concept of block, or it can be called a data block. The logical block is the "minimum storage unit" specified when the partition is formatted with the file system, and the minimum storage unit is sector-based, so the size of the logical block is always 2 times the N times of the sector. At this point, the head can read one block at a time, so the efficiency is high!

Logical block planning is very learned, not the bigger the better, because a logical block can only hold a file (in Linux ext2), so if the logical block is planned too large, then it will be a waste of disk space. For example, if a logical block is 4KB, and a file has only 0.1KB size, and the small file still occupies a logical block, it will waste 3.9KB of space.

Therefore, you need to consider the purpose of the host when planning your disk. such as BBS host, because the article short, file smaller, then the logical block allocation of a little bit better. If the host is mainly used to store large volumes of files, then consider the use of efficiency, or logical block a little bit better!

the composition of the disk

We can divide a disk into one or more partitions. Each partition can contain a single file system.

Let's describe a hierarchical refinement process below, and ask you to focus on what I think:

1 disks are made up of one partition, that is, disk = partition + partition + partition ...

2 There is a file system within each partition, and there is only one file system within a partition.

3 Each partition contains these contents in sequence: Bootstrap block (also called Bootstrap Block), super block, cylinder group 0, cylinder Group 1, ... Cylinder Group N. That is, partition = Bootstrap block + Super Block + Cylinder Group (several)

4 Each cylinder group also includes these contents: Super Block Copy, configuration information, I node graph (record which I nodes are available), block bitmap (to record which blocks are available), I node (many), data block (also called logical block)

Well, you should be able to construct a hierarchical map in your mind based on 1,2,3,4, and if you draw it out, it will be more beneficial to the concept of memory I node.

Super Block

The function of the Super Block (Superblock) is to store the size of the file system, empty and filled blocks, and their respective totals and other such information. To use a partition for data access, the first one to access is the Super block. So, if the super block is broken, the disk is basically hopeless.

I node

The following is about the I node, you can not mention the security of Linux. Because the Linux operating system is a multi-user, multi-tasking environment, in order to protect the privacy of each user's own data, each file is divided into two parts to store: One is the properties of the file, the other is the contents of the file.

The I node (iinode) is used to store the properties of a file, whereas a block of data (logical block) is used to store the contents of a file!

If you want to format a partition, specify the size of the inode and the size of the block! More generally, a ext2 file system is sure to include the Inode table with the block area of these two parts!

As for the block, as I mentioned earlier, it is also called a logical block, also called a data block, which is the smallest unit of data storage.

And the Inode " record file attributes and where the contents of the file is placed in " the information, more generally, the inode in addition to the attributes containing the file, but also includes a pointer, which points to the location of the file contents of the data block placed, So that the operating system can easily read the contents of the file.

Such file attribute information is generally included in the inode:

    • The owner of the file and the group of users to whom it belongs;
    • The access permission setting of the file;
    • The type of file;
    • Time of file access, modification, etc.
    • The size of the file;
    • Documents of various flags, such as SUID and sgid, etc.;
    • A pointer to the file content data block.

The size of an inode is typically 128 bytes. (This knowledge will be subverted in Ext4, and the inode size in EXT4 will be expanded to 256 bytes)

Well, let's see how we can manage files using Inode.


Let's take a look at the details of the directory operation:

If we set up a directory, then the system assigns an inode and at least one block to the directory. The Inode records the relevant properties of the directory and points the pointer to the allocated data block. In the allocated block, the correlation of the related files (and subdirectories) in this directory is recorded, and more generally, a table containing three columns is stored in the directory block, and the three columns are: inode, file name or directory name, pointer to the data block.

We use the VI command to see what the contents of a directory are: (of course, this is just what the user sees, and the underlying implementation of the filesystem is different.) )

"============================================================================" NETRW Directory Listing (NETRW v109) "/rocrocket/psb/home/git27" Sorted by name "Sort sequence: [\/]$,\.h$,\.c$,\. Cpp$,\. [a-np-z]$,*,\.info$,\.swp$,\.o$\.obj$,\.bak$ "Quick Help: <f1>:help-:go up dir d:delete r:rename s:sort-by x : Exec "============================================================================". /./.git/roc.c

The double quotation marks begin with the comment section followed by four items, the first two being the parent directory and the current directory that are intrinsic to any directory, and then a hidden directory. Git, and finally a file roc.c in the current directory. It can be seen that a directory is actually a file, except that it does not store user data, but instead stores the list of files and subdirectories in the directory.

If you create a new normal file in Linux, the file is assigned at least one inode and the number of blocks relative to that file size. For example, if a block is 4KB and a 100KB file is built, Linux allocates an inode with 25 blocks to store the file.

One thing to be special! Special! Special! The reminder is that the inode itself does not record the file name, but rather the related attributes of the document (those mentioned above), and the file name is recorded in the block area to which the directory belongs. For this reason, if Linux reads the contents of a file, it is necessary to get the inode of the file by the root directory/the inode where the file's upper-level directory is located, and then the file association recorded by that directory. Finally, the final file content is obtained through the block pointers provided within the inode.

Link Count

And when it comes to the number of links, here are some concepts and some regular conclusions:

There is a link count in each I node whose value is the number of directory entries that point to the I node.

The file can be deleted only if the link technology is reduced to 0 o'clock (that is, releasing the data blocks that the file occupies)

Links that can increase the number of links are hard links .

A soft link is also called a symbolic link, and its inode file type is S_iflnk. It just stores the path and name of the other file.

The link count for any one leaf directory (a directory that does not contain any other directories) is always 2, and the value 2 comes from the directory entry that names the directory and the. Item in that directory.

Each subdirectory in the parent directory causes the link count of the parent directory to be increased by 1.

Wonderful citation

Finally gives the CSDN online qxp Netizen's section about the soft link and the hard link comment, very good:

We know that UNIX files can be broadly divided into three parts: directory (file name), Inode, and data area.

For replication, not only the new directory entry (file name), the new inode, but also all the data of the file are copied;

The hard link only creates a new catalog entry , and the corresponding inode number in the catalog entry is linked to the inode number of the corresponding file, and the Inode reference count of the file is added 1;

This way, when you delete the original file, the file data is not deleted because the Inode node reference count is >0, so you can continue to access it through hard links.

In other words, a hard connection causes the file to have another alias, another entry.

By the way, the soft link, the symbolic link, is actually the equivalent of a shortcut under Windows.

Creates a new directory entry, a new inode, except that the referenced file path and name are placed in the data area.

Links and differences between hard links and soft links

We know that files have filenames and data, which are divided into two parts on Linux: User data and metadata (metadata). User data, which is the file block (data block), is where the real content of the file is recorded, while metadata is an attached property of the file, such as file size, creation time, owner, and so on. In Linux, the inode number in the metadata (the Inode is part of the file metadata but does not contain a file name, the inode number is the index node number) is the unique identifier of the file and not the filename. The file name is only for the convenience of people's memory and use, the system or program through the inode number to find the correct file data block. Figure 1 shows the process by which a program obtains the contents of a file by its filename.

Figure 1. Open File by file name

Listing 3. Move or rename a file

# stat/home/harris/source/glibc-2.16.0.tar.xz   File: '/home/harris/source/glibc-2.16.0.tar.xz '  Size: 9990512    blocks:19520      IO block:4096   Regular file  device:807h/2055d  inode:2485677     links:1  Access: (0600/-rw-------)  Uid: (1000/  Harris)   Gid: (1000/  Harris)  ... ...  # mv/home/harris/source/glibc-2.16.0.tar.xz/home/harris/desktop/glibc.tar.xz  # ls-i-f/home/harris/desktop/ GLIBC.TAR.XZ  2485677/home/harris/desktop/glibc.tar.xz

To view the inode number in a Linux system, you can use command stat or ls-i ( use command Istat if AIX system). Listing 3. Use the command MV to move and rename the file glibc-2.16.0.tar.xz, the result does not affect the file user data and inode number , the file before and after the migration of the inode number is: 2485677.

To solve the shared use of files, the Linux system introduces two links: Hard link and soft link (also known as symbolic link, soft link or symbolic link). Link for Linux system to solve the shared use of files, but also bring hidden file paths, increase permissions security and save storage and other good places. If an inode number corresponds to more than one file name, the files are called hard links. In other words, a hard link is the same file using multiple aliases (see Figure 2.hard, Link is an alias of file, they have a common inode). hard links can be created by the command link or ln . as follows, create a hard link to the file oldfile.

Link oldfile newfile  ln oldfile newfile

because a hard link is a file with the same inode number with only a different file name , hard links have the following characteristics:

    • The file has the same inode and data block;
    • Only files that already exist can be created;
    • Cannot cross file system for hard link creation;
    • The directory cannot be created, only the file can be created;
    • Deleting a hard-link file does not affect other files that have the same inode number.
Listing 4. Hard link feature display

 # Ls-li Total 0//can only create hard connections to existing Files # link old.file link:cannot create link ' ' to ' old.file ': No su ch file or directory # echo "This is a original file" > Old.file # Cat Old.file This is an original file # stat OL D.file file: ' old.file ' size:25 blocks:8 IO block:4096 regular file device:807h/2055d inode:6 60650 links:2 Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root) ...//file with the same inode number and DA TA block # link Old.file | Ls-li Total 8 660650-rw-r--r--2 root root Sep 1 17:44 660650-rw-r--r--2 root root Sep 1 17:44 ol D.file//cannot Cross file system # Ln/dev/input/event5/root/bfile.txt ln:failed to create hard link '/root/bfile.txt ' = '/dev/i Nput/event5 ': Invalid cross-device link//Cannot create a hard connection to the directory # mkdir-p old.dir/test # ln old.dir/hardlink.dir ln: ' Old.di r/': Hard link isn't allowed for directory # ls-if 660650 657948 old.dir/660650 old.file

File Old.file and have the same inode number: 660650 and file permissions, the Inode is present as the file exists, so you can create a hard link only if the file exists, that is, when the inode exists and the link count is not 0 o'clock 。 The inode number is unique only under each filesystem, and when Linux mounts multiple file systems, the inode repetition occurs (as shown in Listing 5. File t3.jpg, Sync, and 123.txt are not associated, but have the same inode number), so hard-link creation cannot span files System. The file system used by the device files directory/dev is Devtmpfs, and/root (with root/consistent) uses the disk file system Ext4. Listing 5 shows the use of the command DF to view the file system types mounted on the current system, the file system inode usage, and the file system mount points.

Listing 5. Find files with the same inode number

# df-i--print-type  Filesystem     type       inodes  iused    IFree iuse% mounted on  /dev/sda7      EXT4      3147760 283483  2864277   10%/  udev           devtmpfs   496088 553 495535 1%/    Dev  tmpfs          tmpfs      499006    491   498515    1%/run  None           tmpfs      499006      3   499003    1%/run/lock  None           tmpfs      499006     all   498991  1%/run/shm/dev/ Sda6      fuseblk  74383900   4786 74379114    1%/media/diske  /dev/sda8      fuseblk  29524592  19939 29504653    1%/MEDIA/DISKF  # Find/-inum 1114  /media/diske/pictures/t3.jpg  / Media/diskf/123.txt  /bin/sync

It is worth mentioning that the Linux system has the inode number is exhausted but the disk space still has the remaining situation. We create a 5M size EXT4 type of mo.img file and mount it to the directory/mnt. Then we use a shell script that will mount the Indoe depletion of the Ext4 file system under/MNT (see Listing 6.).

Listing 6. Test a scenario where the file system Inode is exhausted but still has disk space

# dd If=/dev/zero of=mo.img bs=5120k count=1  # ls-lh mo.img  -rw-r--r--1 root root 5.0M Sep  1 17:54 mo.img
   # mkfs-t Ext4-  F./mo.img  ...  OS type:linux  Block size=1024 (log=0)  Fragment size=1024 (log=0)  stride=0 blocks, Stripe width=0 BLOCKS
   1280 inodes, 5120 blocks  blocks (5.00%) reserved for the Super user ...  ...  Writing superblocks and filesystem accounting Information:done  # Mount-o loop./mo.img/mnt  # cat/mnt/inode_t  #!/bin/bash for  ((i = 1;; i++))  does     if [$?-eq 0]; then         echo  "This is file_$i" > File_$i     Else         exit 0     fi  done  #/  ./ 6:file_1269:no Space left on device  # df-it/mnt/, du-sh/mnt/  Filesystem     Type inodes iused IFree iuse% mounted  on /dev/loop0     ext4     0  100%/mnt  1.3M  /mnt/

hard links cannot be created on a directory that is constrained by the design of the file system (see Listing 4. Creating a hard link to a directory will fail). Directories in the Linux file system now have two special directories hidden: the current directory (.) and the parent directory (.. )。 Looking at the inode numbers for these two special directories is the fact that these two directories are two hard links (note the inode number of the directory/mnt/lost+found/). If the system allows hard links to be created on the directory, a directory ring is generated.

# ls-alif/mnt/lost+found Total  drwx------2 root root 12288 Sep  1 17:54./  2 drwxr-xr-x 3 root roo T 31744 Sep  1 17:57.. /  # stat  /mnt/lost+found/   File: '/mnt/lost+found/'  size:12288      blocks:24         IO block:1024   Directory  device:700h/1792d  inode:11          links:2  Access: (0700/drwx------)  Uid: (    0/    root)   Gid: (    0/    root)  access:2012-09-01 17:57:17.000000000 +0800  modify:2012-09-01 17:54:49.000000000 +0800  change:2012-09-01 17:54:49.000000000 +0800  Birth:-

A soft link differs from a hard link in that the file is a soft connection if the contents of the file's user data block are pointing to the path name of another file . Soft link is a normal file, but the content of the data block is a bit special. The soft link has its own inode number and user data block (see Figure 2.). Therefore, the creation and use of soft links does not have many restrictions similar to hard links: soft connections can only be created with the Ln-s command and cannot be created using link

    • Soft links have their own file attributes and permissions, etc.;
    • You can create a soft link to a nonexistent file or directory;
    • Soft link can cross file system;
    • Soft links can be created on files or directories;
    • When you create a soft link, the link count i_nlink not increase;
    • Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, the associated soft connection is called a dead link (that is, dangling link, if it is re-created by pointing to the path file, the dead link can revert to the normal soft link).
Figure 2. Access to Soft links

Listing 7. Soft link Feature Display

# Ls-li  Total 0  //can create soft links to nonexistent files # ln <span style= "color: #FF0000;" >-s</span> old.file  # ls-lif Total  0  789467 lrwxrwxrwx 1 root root 8 Sep  1 18:00 soft. Link-old.file  //Because the file being pointed to does not exist, at this time the soft link is dead link # cat such file or director Y  //Create the file pointed to Old.file, restore to normal soft link # echo "This was an original file_a" >> old.file  # cat
   this is an original file_a  //Create soft links to nonexistent directories # ln-s Old.dir  # mkdir-p old.dir/test  # Tree . -F--inodes  . ├──[789497]  old.dir/│   └──[789498]  test/├──[789495]  old.file├──[789495], old.file└──[789497], old.dir/

Of course, the user data of soft link can also be the path of another soft link, whose parsing process is recursive. However, it is important to note that the path of the original file when the soft link is created is better with absolute path. When a soft link created with a relative path is moved, the soft link file becomes a dead link (the soft link A as shown below uses a relative path and therefore should not be moved) because the linked data block also points to a relative path.

$ ls-li Total  2136  656627 lrwxrwxrwx 1 Harris Harris       8 Sep  1 14:37 A-data.txt 656662 lrwxrwxrwx 1 h Arris Harris       1 Sep  1 14:37 B-a  656228-rw-------1 Harris Harris 2186738 Sep  1 14:37 data.txt 6

Link Related commands

In Linux, view the file system types that are currently hanging on the system, in addition to the command DF used above, you can also use the command mount or view the file/proc/mounts.

# mount  /dev/sda7 on/type ext4 (rw,errors=remount-ro)  proc On/proc type proc (rw,noexec,nosuid,nodev)  sys FS On/sys type SYSFS (Rw,noexec,nosuid,nodev) ...  ...  None On/run/shm type TMPFS (Rw,nosuid,nodev)

The command LS or stat helps us differentiate between soft links and other files and view file Inode numbers, but a better way to use the Find command is not only to find soft links to a file, but also to find all the hard links for the same inode . (see Listing 8.)

Listing 8. Use the command find to find soft links with hard links

//Find a soft link to file data.txt under Path/home # Find/home-lname data.txt/home/harris/debug/test2/a View Path/home with all hard links with the same inode # FIND/HOME-SAMEFILE/HOME/HARRIS/DEBUG/TEST3/OLD.FILE/HOME/HARRIS/DEBUG/TEST3/HARD.L Ink/home/harris/debug/test3/old.file # Find/home-inum 660650/home/harris/debug/test3/  Test3/old.file//List all soft link files under path/home/harris/debug/# find/home/harris/debug/-type l-ls 656662 0 lrwxrwxrwx 1 Harris Harris 1 Sep 1 14:37/home/harris/debug/test2/b A 656627 0 lrwxrwxrwx 1 Harris Harris 8 Sep 1 14:37/home/harris/de bug/test2/a-data.txt 789467 0 lrwxrwxrwx 1 root root 8 Sep 1 18:00/home/harris/debug/test/ E 789496 0 lrwxrwxrwx 1 root root 7 Sep 1 18:01/home/harris/debug/test/ 

The system defaults to the value of the inode based on the size of the disk (see Listing 9), which can be modified before the format file system, if necessary. If you type a commandmkfs -t ext4 -I 512/dev/sda4,将使磁盘设备 /dev/sda4 格式成 inode 大小是 512 字节的 ext4 文件系统。

清单 9. 查看系统的 inode 值

View inode values on disk partition/DEV/SDA7 # dumpe2fs-h/DEV/SDA7 |  grep "Inode size" dumpe2fs 1.42 (29-nov-2011)  inode size:  # tune2fs-l/dev/sda7 | grep "Inode size" inode Size:           256

Linux VFS  文件系统分类

Linux 有着极其丰富的文件系统,大体上可分如下几类:

    1. 网络文件系统,如 nfs、cifs 等;
    2. 磁盘文件系统,如 ext4、ext3 等;
    3. 特殊文件系统,如 proc、sysfs、ramfs、tmpfs 等。

实现以上这些文件系统并在 Linux 下共存的基础就是 Linux VFS(Virtual File System 又称 Virtual Filesystem Switch),即虚拟文件系统。VFS 作为一个通用的文件系统,抽象了文件系统的四个基本概念:文件、目录项 (dentry)、索引节点 (inode) 及挂载点,其在内核中为用户空间层的文件系统提供了相关的接口(见图 3.所示 VFS 在 Linux 系统的架构)。VFS 实现了 open()、read() 等系统调并使得 cp 等用户空间程序可跨文件系统。VFS 真正实现了上述内容中:在Linux中除进程之外一切皆是文件。

图 3. VFS 在系统中的架构

There are four basic objects for the Linux VFS: the Super Block Objects (Superblock object), the Index node objects (Inode object), the Catalog item object (Dentry object), and the file object (the Files objects). The Super Block object represents an installed file system, an index node object represents a file, and a Catalog item object represents a directory entry, such as a device file event5 in Path/DEV/INPUT/EVENT5, which has four directory item objects:/, dev/, input/, and EVENT5. A file object represents a file that is opened by a process. The relationship between these four objects and the process and disk files is 4. Shown, where D_inode is a hard link. For fast parsing of file paths, Linux VFS Designs Directory Item caches (directory Entry cache, or Dcache).

Figure 4. Processing between the objects of the VFS

Inode in a Linux file system

In Linux, the index node structure exists in the system memory and disk, which can be distinguished into the inode of the VFS inode and the actual file system. The VFS Inode, as an abstraction of the inode in the actual file system, defines the structure Inode and its associated operational inode_operations (see Kernel source include/linux/fs.h).

Listing 10. The Inode and inode_operations structure in VFS

struct Inode {     ...     const struct Inode_operations   *i_op;//Index node operation    unsigned long           I_ino;      Index node number    atomic_t                i_count;    Reference counter    unsigned int            i_nlink;    Number of hard links ...  }  struct Inode_operations {     ...     Int (*create) (struct inode *,struct dentry *,int, struct nameidata *);     Int (*link) (struct dentry *,struct inode *,struct dentry *);     Int (*unlink) (struct inode *,struct dentry *);     Int (*symlink) (struct inode *,struct dentry *,const char *);     Int (*mkdir) (struct inode *,struct dentry *,int);     Int (*rmdir) (struct inode *,struct dentry *);     ...  }

As listing 10. See, there are two counters per file: I_count and I_nlink, that is, reference count and hard link count. The i_count in the struct inode is used to track the number of files accessed, while I_nlink is the number of hard links to the file seen using commands such as ls-l. Or I_count trace the file in memory, and I_nlink is the disk counter. When the file is deleted, the I_nlink is set to 0 first. These two counters of the file make the Linux system upgrade or program update easy. The system or program can not be closed (that is, the file I_count is not 0), the new file is replaced with the same file name, the new file has its own inode and data block, the old file will be completely deleted after the relevant process is closed.

Listing 11. Inode in File system Ext4

struct Ext4_inode {     ...     __le32  i_atime;        File content last accessed time    __le32  i_ctime;        Inode modification Time    __le32  i_mtime;        File content Last modified    __le16  i_links_count;  Hard link Count    __le32  I_blocks_lo;    Block count    __le32  i_block[ext4_n_blocks];  Point to a specific block     ...  };

Listing 11. Shows the definition of inode in file system EXT4 (see kernel source fs/ext4/ext4.h). One of the three time definitions can correspond with the command stat to view up to three times. I_links_count is not only used for hard-link counts of files, but also for the number of subdirectories of a directory (the directory does not show the number of hard links, and the command Ls-ld sees the number of subdirectories). Because the file system ext3 has a limit of i_links_count, the maximum number is: 32000 (the limit is canceled in Ext4). Try to verify that the maximum number of directory subdirectories and normal file hard links on the Ext3 file system is visible in Listing 12. Error message. Therefore, there is a difference between the inode of the actual file system and the VFS inode.

Listing 12. Limitations of I_links_count in file system ext3
#./  mkdir:cannot Create directory ' dir_31999 ': Too many links  #./  ln:failed to creat e hard link to ' old.file ': Too many links


Linux file system soft link hard link

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: