Hard links and soft links for Linux--Learn about Linux file systems from the Inode __linux

Source: Internet
Author: User
Tags create directory documentation mkdir parent directory file permissions ibm developerworks
1 files and directories of Linux

Modern operating systems introduce files for long-term storage of information that can be stored independently of the process, and the logical units of the files that create information as processes can be used concurrently by multiple processes. In UNIX systems, the operating system designs a common set of APIs for I/O operations such as text and images on disk, input devices such as mouse and keyboard, and network interaction, so that they can be used to uniformly use a byte stream when they are processed. In other words, everything except the process in a UNIX system is a file, and Linux maintains this feature. To facilitate file management, Linux also introduces the concept of directories (sometimes referred to as folders). Directories enable files to be sorted and managed, and the introduction of the directory enables the Linux filesystem to form a hierarchical directory tree. Listing 1. Is the top-level directory structure of an ordinary Linux system, where/dev is the directory where the device-related files are stored.

Listing 1 Top-level directory structure for Linux systems

/           root directory
├──bin         storing user binaries
├──boot        storing kernel boot profiles
├──dev         storing device files
├──etc         storing system configuration files
├──home        User home directory
├──lib         dynamic Shared library
├──lost+found  file system Recovery file
├──media       Removable Storage media mount point
├──mnt         file system temporary mount point
├──opt         Additional application package
├──proc        system Memory Mapping directory that provides kernel and process information
├── Root        root User master directory
├──sbin        storage System binaries
├──srv         storage Service related data
├──sys         sys virtual file system mount point
├──tmp         Store temporary files
├──usr         Store user applications
└──var         store mail, system logs and other change files

Linux, like other UNIX-like systems, does not distinguish between files and directories: A directory is a file that records other file names. When you create a directory by using the command mkdir, you create a failure if you expect the name of the directory you created to duplicate the existing file name (or directory name).

# ls-f/usr/bin/zi* 
/usr/bin/zip*       /usr/bin/zipgrep*  /usr/bin/zipnote* 
/usr/bin/zipcloak*  / usr/bin/zipinfo*  /usr/bin/zipsplit* 

# mkdir-p/usr/bin/zip 
mkdir:cannot Create directory '/usr/bin/zip ' : File exists

Linux treats the device as a file, listing 2 shows how to open the device file/dev/input/event5 and read the contents of the file. File EVENT5 represents an input device, which may be a mouse or keyboard. View file/proc/bus/input/devices know the type of event5 corresponding device. The device file/dev/input/event5 is read as a stream of characters using read (). The structure input_event is defined in the kernel header file linux/input.h.

Listing 2 opens and reads the device file

int FD; 
struct input_event ie; 
FD = open ("/dev/input/event5", o_rdonly); 
Read (FD, &ie, sizeof (struct input_event)); 
printf ("type =%d  code =%d  value =%d\n", Ie.type, Ie.code, ie.value); 
Close (FD);
2 links and differences between hard link and soft link

We know that files have file names and data, which are divided into two parts on Linux: User data and metadata (meta data). User data, that is, file data blocks (data block), which is where the real content of the file is recorded, and metadata is the 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 it does not contain a filename, and the inode number, which is the index node number), is the file's unique identity rather than 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 right file data block. Figure 1 shows the process by which a program obtains the contents of a file through a filename.

Figure 1 Opening a file by file name

Listing 3 Moving or renaming 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

View the inode number in a Linux system using the command stat or ls-i (if an AIX system, use command Istat). Listing 3 uses the command MV to move and rename the file glibc-2.16.0.tar.xz, the result does not affect the file's user data and inode number, before and after the file move inode number is: 2485677.

To solve the shared use of the file, the Linux system introduces two kinds of links: Hard link (hard link) and soft link (also known as symbolic link, that is, soft link or symbolic link). Links for Linux systems to solve the shared use of files, but also brings the hidden file path, increase the security of permissions and save storage, and other benefits. If an inode number corresponds to multiple file names, these files are referred to as hard links. In other words, a hard link is the same file that uses multiple aliases (see Figure 2 Hard link is an alias for file, they have a common inode). Hard links can be created by command link or ln. The following is the creation of a hard link to the file oldfile.

Link oldfile newfile 
ln oldfile newfile

Because a hard link is a file that has the same inode number and only a different file name, there are several characteristics of a hard link:
• files have the same inode and data block;
• Only existing files can be created;
• Cannot cross file system for hard link creation;
• Catalogs cannot be created, only files can be created;
• Deleting a hard link file does not affect other files that have the same inode number.

Listing 4 hard link features show

# Ls-li Total 0//can only create hard connections to existing Files # link old.file hard.link link:cannot create link ' hard.link ' to ' old.file ': No su ch file or directory # echo "This is a original file" > Old.file # Cat Old.file This is a original file # stat O      Ld.file file: ' old.file ' size:25 blocks:8 IO block:4096 Regular file device:807h/2055d Links:2 Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root) ...//files have the same inode number and data block # link OLD.F Ile Hard.link | Ls-li Total 8 660650-rw-r--r--2 root Sep 1 17:44 hard.link 660650-rw-r--r--2 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/ Input/event5 ': Invalid cross-device link//Cannot create hard connection to directory # mkdir-p old.dir/test # ln Old.dir/hardlink.dir ln: ' Old. dir/': Hard link not allowed for directory # ls-if 660650 hard.link 657948 old.dir/660650 old.file

File Old.file and Hard.link have the same inode number: 660650 and file permissions, the Inode is with the existence of the file, so only when the file exists, you can create a hard link, that is, when the inode exists and the link count is not 0 o'clock. The inode number is unique only under each file system, and when Linux mounts multiple file systems There will be a repeat of the inode number (as shown in Listing 5, file t3.jpg, Sync, and 123.txt are not associated with the same inode number), so hard links cannot be created across files System. The file system used by the device file directory/dev is Devtmpfs, and/root (in the root directory/consistent) uses the disk file system Ext4. Listing 5 shows the use of command DF to view the type of file system being mounted on the current system, the usage of the file system Inode, and the file system mount point.

Listing 5 finds 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 
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 used up, but there is still disk space surplus. We create a 5M size EXT4 type of mo.img file and mount it to the directory/mnt. Then we use a shell script to mount the indoe of the Ext4 file system under/MNT (see Listing 6.).

Listing 6 tests the scenario where the file system Inode is depleted 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 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 256 blocks (5.00%) reserved for the Super user ... 
Writing superblocks and filesystem accounting Information:done # Mount-o loop./mo.img/mnt # cat/mnt/inode_test.sh #!/bin/bash for ((i = 1;; i++)) does if [$-eq 0]; Then echo "This is file_$i" > file_$i else Exit 0 fi do #/inode_test.sh./inode_test.s H:line 6:file_1269:no spaces left on device # df-it/mnt/; 
Du-sh/mnt/filesystem Type inodes iused ifree iuse% mounted on/dev/loop0 ext4 1280 1280 0 100%/mnt 1.3m/mnt/

Hard links cannot be created on directories that are limited to the design of the file system (see listing 4 Creating a hard link to the directory will fail). The directory in the Linux file system now hides two special directories: the current directory (.) and the parent directory (...). )。 See the Inode numbers for these two special directories. The two directories are actually two hard links (note the inode number of the directory/mnt/lost+found/). If the system allows the creation of a hard link to the directory, the directory loop will be generated.

# ls-alif/mnt/lost+found Total 
drwx------2 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:-

Soft links and hard links are different, if the file user data block in the contents of another file is the path name point, then the file is a soft connection. Soft link is a common file, but the content of the data block is somewhat special. Soft links have their own inode numbers and user data blocks (see Figure 2). Therefore, the creation and use of soft links do not have many restrictions similar to hard links:

• Soft links have their own file attributes and permissions, etc.;
• Can create soft links to non-existent files or directories;
• Soft link can be cross file system;
• Soft links can be created for files or directories;
• When creating soft links, the link count I_nlink will 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 the path file is recreated, the dead link can revert to a normal soft link).

Figure 2 Access to soft links

Listing 7 Soft link features Show

# Ls-li Total 
 0 

 //can create soft links to nonexistent files
 # ln-s old.file soft.link 
 # ls-lif Total 
 0 
 789467 lrwxrwxrwx 1 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 soft.link is the dead link
 # cat Soft.link 
 CA T:soft.link:no such file or directory 

 //Create files to be pointed old.file,soft.link revert to normal soft link
 # echo ' This is a original fil E_a ' >> old.file 
 # cat Soft.link This 
 is a original file_a 

 //Create soft links to non-existent directories
 # ln-s Old.dir Soft . Link.dir 
 # mkdir-p old.dir/test 
 # tree.-F--inodes 
 . 
├──[789497]  old.dir/ 
│   └──[789498]  test/ 
├──[789495]  old.file 
├──[789495]< C27/>soft.link-> old.file 
└──[789497]  soft.link.dir-> old.dir/

Of course, the user data of the soft link can also be the path of another soft link, its parsing process is recursive. However, note that when the soft link is created, the path to the original file is better to use absolute paths. When a soft link created using a relative path is moved, the soft link file becomes a dead link (soft link A, as shown below, uses a relative path and is therefore not suitable to be moved) because the linked data block is also recorded relative to the path point.

$ ls-li Total 
2136 
656627 lrwxrwxrwx 1 Harris Harris       8 Sep  1 14:37  a->
data.txt 656662 LRWXRW XRWX 1 Harris Harris       1 Sep  1 14:37  b-> a 
656228-rw-------1 Harris Harris 2186738 Sep  1 14:37
  data.txt 6
3 Links Related commands

In Linux, view the file system type that the current system is hanging, in addition to the command DF used above, you can also use the command mount or view file/proc/mounts.

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

Command LS or stat can help us distinguish between soft links and other files and view the file Inode number, but better still use the Find command, which not only finds soft links to a file, but can also be used to find all the hard links to the same inode. (See Listing 8)

Listing 8 finds soft links and hard links using command find

Find soft links for file data.txt under Path/home # Find/home-lname data.txt/home/harris/debug/test2/a//view path/home all hard chains with the same inode Pick Up # find/home-samefile/home/harris/debug/test3/old.file/home/harris/debug/test3/hard.link/home/harris/debug/ Test3/old.file # Find/home-inum 660650/home/harris/debug/test3/hard.link/home/harris/debug/test3/old.file/Row out 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/debug/test2/a-> dat A.txt 789467 0 lrwxrwxrwx 1 root root 8 Sep 1 18:00/home/harris/debug/test/soft.link-> old.file 789496 0 lrwxrwx RWX 1 root 7 Sep 1 18:01/home/harris/debug/test/soft.link.dir-> old.dir system defaults to the value of the inode based on the size of the disk (see listing 9) , you can modify the value before the format file system if necessary.
If you type a command mkfs-t ext4-i 512/dev/sda4, the disk device/DEV/SDA4 formatted as a EXT4 file system with an inode size of 512 bytes. Listing 9 View the system's Inode value//view disk partition/DEV/SDA7 onOde Value # Dumpe2fs-h/DEV/SDA7 | grep "Inode size" dumpe2fs 1.42 (29-nov-2011) inode size:256 # tune2fs-l/dev/sda7 | grep "inode size" Inode size:256
4 Linux VFS

Linux has a very rich file system, can be divided into the following categories: Network file systems, such as NFS, CIFS, disk file systems, such as EXT4, Ext3, and other special file systems, such as Proc, SYSFS, Ramfs, Tmpfs and so on.

The basis for implementing these file systems and living under Linux is the Linux VFS (virtual file system, also known as VM filesystem Switch), a virtualized filesystem. VFS as a common file system, the four basic concepts of file systems are abstracted: files, directory entries (dentry), index nodes (inode), and mount points, which provide a relevant interface for the user space layer's file system in the kernel (see Figure 3. VFS in Linux system architecture). The VFS implements the system of open (), read () and so on, so that user space programs such as CP can cross the file system. The VFS really implements the above: everything except the process in Linux is a file.

Figure 3 The schema of the VFS in the system

The Linux VFS has four basic objects: Super Block Objects (Superblock object), Index node objects (Inode object), directory entry objects (Dentry object), and File Objects (object). A Super Block object represents an installed file system; An index node object represents a file; A directory entry object represents a directory entry, such as a device file event5 in Path/DEV/INPUT/EVENT5, which has four directory entry 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 shown in Figure 4. Shown, where D_inode is a hard link. For fast parsing of file paths, the Linux VFS has designed directory item caching (directory Entry cache, or Dcache).

Figure 4 Handling between objects of the VFS
5 inode in the Linux file system

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

Inode and inode_operations structure in the list of 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 shown in Listing 10, there are two counters per file: I_count and I_nlink, that is, reference count and hard link count. The I_count in the inode of the structure is used to track the number of files accessed, while I_nlink is the number of hard links found in the above commands, such as ls-l. Or I_count trace the file in memory, while 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 Linux system upgrades or program updates easier. The system or program can not be closed in the case (that is, the file I_count is not 0), the new file with the same file name, the new file has its own inode and data block, the old file will be closed after the relevant process is completely deleted.

Listing 11 inode in file system Ext4

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

Listing 11 shows the definition of the Inode in the file system EXT4 (see kernel source fs/ext4/ext4.h). The definitions for three of these times correspond to the three-time view in the command Stat. I_links_count is used not only for hard link counts of files, but also for subdirectories of directories (the directory does not show the number of hard links, and commands ls-ld see the number of subdirectories). The maximum number of file system ext3 to I_links_count is: 32000 (the limit is canceled in Ext4). Try verifying directory subdirectories and the maximum number of hard links on the ext3 file system as shown in Listing 12. The error message. Therefore, there are differences between the inode of the actual file system and that of the VFS inode.

Listing 12 limitations of I_links_count in file system ext3

#./dirtest.sh 
mkdir:cannot Create directory ' dir_31999 ': Too many links 

#/linkcount.sh 
ln:failed to creat e hard link to ' old.file ': Too many links
6 Concluding remarks

This paper initially describes the reasons for the introduction of files and directories in Linux systems and the way Linux processes files, and then by distinguishing between hard links and soft links, we understand the relevant knowledge of the index nodes in Linux, and draw the structure of the inode. The index node structure exists in the Linux VFS and the actual file system, the VFS as a common file model is the Linux "everything is a file" implementation of the foundation. The article does not go deep into the Linux VFS, nor does it involve the implementation of the actual file system, the article only from the inode to understand the Linux file system related content. If you want to drill down to the contents of the file system, it's a good idea to view the kernel documentation documentation/filesystems/.

Original name to understand the hard links and soft links of Linux-Wang Huadong-IBM developerWorks
"URLs" https://www.ibm.com/developerworks/cn/linux/ l-cn-hardandsymb-links/

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.