Learn about Linux file systems from Inode
Hard links and soft links are an important concept in the Linux file system, which involves index nodes (index node, also called Inode) in the file system, and the index node object is one of the four basic concepts of the Linux virtual file system (VFS). By dissecting the links and differences between hard links and soft links, we have a better understanding of the common file model of VFS in Linux. And let Linux normal users and system administrators correctly use hard links and soft links to help file system developers get the knowledge of inode.
Files and directories for Linux
The modern operating system introduces files to the solution for information that can be stored independently of the process by long-term storage, and the logical unit that the file creates information as a process can be used concurrently by multiple processes. In UNIX systems, the operating system designs a set of common APIs for I/O operations such as text on disk and input devices such as mouse and keyboard, and network interaction, so that they can be processed uniformly using a byte stream. In other words, everything except the process in a UNIX system is a file, and Linux retains this feature. To facilitate file management, Linux also introduces the concept of catalogs (sometimes referred to as folders). Directories allow files to be managed by classification, and the introduction of directories enables Linux file systems to form a hierarchical directory tree. Listing 1 shows the top-level directory structure of a common 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 store user binaries ├──boot store kernel boot profile ├──dev store device file ├──etc Store system profile ├──home user home directory ├──lib Dynamic Shared library ├──lost+found file system Recovery file ├──media can unload storage media mount point ├──mnt File system temporary mount point ├──opt Additional application package ├──proc Mapping directory of system memory, providing kernel and process information ├──root root user home directory ├──sbin storing system binaries ├──srv storing service related data ├──sys sys virtual file system mount point ├── TMP holds temporary files ├──usr store user application └──var store mail, System log and other change files
Linux, like other UNIX-like systems, does not differentiate between files and directories: Directories are files that record other filenames. When you create a directory using command mkdir, you create a failure if you expect the name of the directory to be 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 processes the device as a file, listing 2 shows how to open the device file/dev/input/event5 and read the file contents. The file event5 represents an input device, which may be a mouse or keyboard, and so on. View the file/proc/bus/input/devices the type of event5 corresponding device. The device file/dev/input/event5 is read in a character stream using read (). The struct input_event is defined in the kernel header file linux/input.h.
Listing 2. Open and read device files
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);
Back to top of page
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 listing 3 by file name. 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 benefits. 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 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 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 Hard.link | Ls-li Total 8 660650-rw-r--r--2 root root Sep 1 17:44 hard.link 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 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 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 file system, 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 links are created with no cross-text 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, 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_test.sh #!/bin/bash for ((i = 1;; i++)) do if [$?-eq 0]; Then echo ' This is file_$i ' > file_$i else Exit 0 fi done #./inode_test.sh./inode_test.sh : Line 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 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 the soft link listing 7. Soft link Feature Display
# Ls-li Total 0 //can create soft links to nonexistent files # ln-s old.file soft.link # ls-lif Total 0 789467 lrwxrwxrwx 1 roo T root 8 Sep 1 18:00 soft.link-old.file //Because the file pointed to does not exist, at this time the soft link soft.link is dead link # cat Soft.link Cat:soft. Link:no such file or directory //Create the files that are pointed to old.file,soft.link revert to normal soft links # echo "This was an original file_a" >> Old.file # cat Soft.link This was an original file_a //Create soft links to nonexistent directories # ln-s Old.dir Soft.link.dir # mk Dir-p old.dir/test # tree.-F--inodes . ├──[789497] old.dir/│ └──[789498] test/├──[789 495] old.file├──[789495] soft.link, old.file└──[789497] soft.link.dir-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
Back to top of page
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 soft links to file Data.txt in Path/home # Find/home-lname data.txt /home/harris/debug/test2/a //view path/home has the same inode All hard links # 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 //List all soft link files under path/home/harris/debug/# find/home/harris/debug/-type l-ls 656662 0 LRWXRW XRWX 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/h ome/harris/debug/test2/a- data.txt 789467 0 lrwxrwxrwx 1 root root 8 Sep 1 18:00/home/harris/debug/test/soft.lin K- old.file 789496 0 lrwxrwxrwx 1 root root 7 Sep 1 18:01 /home/harris /debug/test/soft.link.dir-Old.dir
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 值
// 查看磁盘分区 /dev/sda7 上的 inode 值 # 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
回页首
Linux VFS
Linux 有着极其丰富的文件系统,大体上可分如下几类:
网络文件系统,如 nfs、cifs 等;
磁盘文件系统,如 ext4、ext3 等;
特殊文件系统,如 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
Back to top of page
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
#./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
Back to top of page
Conclusion
This article initially describes the reasons why files and directories are introduced in Linux, and how Linux handles files, and then we understand the knowledge of the index nodes in Linux by distinguishing between hard links and soft links, and we draw the structure of the inode. The index node structure exists in the Linux VFS and the actual file system, and the VFS as a common file model is the basis of "everything is a file" implementation in Linux. The article does not go into the Linux VFS, nor does it involve the implementation of the actual file system, and the article simply understands the contents of the Linux file system from the Inode. If you want to drill down into the contents of the file system, it's a good idea to view the kernel document documentation/filesystems/.
Reference Learning
- View the article "Learning linux,101: File and Directory Management", familiar with Linux files and directories.
- View the article "Learning linux,101: Creating and changing hard links and symbolic links" to learn about commands related to hard links and soft links.
- Check out "Hard link", "Symbolic link" and "inode" on the wiki.
- Check out the man manual for Symlink (7) for soft links in Linux.
- View the article "Q & a:the difference between hard and soft links" to learn about the difference between rigid and soft links.
- Check out the article "in Unix everything is a file" to learn that everything in Unix except for the process is files.
- View the article "Linux Virtual System file Exchanger anatomy" and "Linux File system Anatomy" for a Linux virtual file system (Linux VFS)-related knowledge.
- View the article "parsing the VFS file system mechanism in Linux" for an in-depth understanding of the process of instance a filesystem system.
- View the article "dialog UNIX: About Inode" to learn about inode content.
- See the article, "mechanisms of metadata usage counting in Linux file systems," to learn about Linux count operations on Dentry and Inode.
- View the kernel source file Documentation/filesystems/ext4.txt to learn about the file system Ext4.
- Find out more about our most popular articles and tutorials in the DeveloperWorks Linux zone for more reference materials for Linux developers, including beginners for Linux.
- Check out all Linux tips and Linux tutorials on the developerWorks.
- Stay tuned for DeveloperWorks technical activities and webcasts.
Discuss
- Joining the DeveloperWorks Chinese community, the DeveloperWorks community is a professional social networking community for global IT professionals who can provide community functions such as blogs, bookmarks, wikis, groups, contacts, sharing, and collaboration.
- Join the IBM software download and technology Exchange Group to participate in online communication.
Understanding hard links and soft links for Linux (pending research)