inode and hard link

2 minute read

背景

In computing, an inode (index node) is a data structure found in many Unix file systems.

Each inode stores all the information about a file system object (file, device node, socket, pipe, etc.), except data content and file name.

inode 不包含数据内容和文件名的信息.

POSIX inode包含如下信息 :

POSIX inode description  
The POSIX standard mandates filesystem behavior that is strongly influenced by traditional UNIX filesystems. Regular files must have the following attributes:  
1. The size of the file in bytes.  
2. Device ID (this identifies the device containing the file).  
3. The User ID of the file's owner.  
4. The Group ID of the file.  
5. The file mode which determines the file type and how the file's owner, its group, and others can access the file.  
6. Additional system and user flags to further protect the file (limit its use and modification).  
7. Timestamps telling when the inode itself was last modified (ctime, inode change time), the file content last modified (mtime, modification time), and last accessed (atime, access time).  
8. A link count telling how many hard links point to the inode.  
9. Pointers to the disk blocks that store the file's contents (see inode pointer structure).  
The stat system call retrieves a file's inode number and some of the information in the inode.  

使用stat可以查看文件的inode号.

digoal@digoal-Dell-System-Vostro-3450:~/script$ stat rdp.sh   
  文件:"rdp.sh"  
  大小:464       	块:8          IO 块:4096   普通文件  
设备:801h/2049d	Inode:1447366     硬链接:1  
权限:(0500/-r-x------)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-16 16:23:17.116096958 +0800  
最近更改:2013-03-27 14:09:31.984110512 +0800  
最近改动:2013-03-27 14:09:32.064110510 +0800  
创建时间:-  

使用 ls -i也可以查看inode号.

digoal@digoal-Dell-System-Vostro-3450:~/script$ ls -lai rdp.sh   
1447366 -r-x------ 1 digoal digoal 464  3月 27 14:09 rdp.sh  

目录指的是filename以及对应的inode number的列表.

digoal@digoal-Dell-System-Vostro-3450:~/script$ pwd  
/home/digoal/script  
digoal@digoal-Dell-System-Vostro-3450:~/script$ ls -lai  
总用量 16  
1442642 drwxrwxr-x  2 digoal digoal 4096  3月 29 14:30 .  
 945715 drwxr-xr-x 45 digoal digoal 4096  5月 17 08:32 ..  
1447366 -r-x------  1 digoal digoal  464  3月 27 14:09 rdp.sh  
1442437 -r-x------  1 digoal digoal  339  3月 29 14:30 ssh.sh  

数据块上有一个部分存储了硬链接的个数, 当这个个数变成0时, 这个数据块就可以被删除了.

例如 :

digoal@digoal-Dell-System-Vostro-3450:~/script$ touch abc  
digoal@digoal-Dell-System-Vostro-3450:~/script$ stat abc  
  文件:"abc"  
  大小:0         	块:0          IO 块:4096   普通空文件  
设备:801h/2049d	Inode:1442547     硬链接:1  
权限:(0664/-rw-rw-r--)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-17 14:39:22.020069682 +0800  
最近更改:2013-05-17 14:39:22.020069682 +0800  
最近改动:2013-05-17 14:39:22.020069682 +0800  
创建时间:-  
  
digoal@digoal-Dell-System-Vostro-3450:~/script$ ln ./abc def  
digoal@digoal-Dell-System-Vostro-3450:~/script$ stat abc  
  文件:"abc"  
  大小:0         	块:0          IO 块:4096   普通空文件  
设备:801h/2049d	Inode:1442547     硬链接:2  
权限:(0664/-rw-rw-r--)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-17 14:39:22.020069682 +0800  
最近更改:2013-05-17 14:39:22.020069682 +0800  
最近改动:2013-05-17 14:39:57.656069028 +0800  
创建时间:-  

当删除1个文件时,因为Inode:1442547中还有1个链接, 所以数据块不会被删除.

digoal@digoal-Dell-System-Vostro-3450:~/script$ stat def  
  文件:"def"  
  大小:0         	块:0          IO 块:4096   普通空文件  
设备:801h/2049d	Inode:1442547     硬链接:1  
权限:(0664/-rw-rw-r--)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-17 14:39:22.020069682 +0800  
最近更改:2013-05-17 14:39:22.020069682 +0800  
最近改动:2013-05-17 14:40:18.336068650 +0800  
创建时间:-  

目录上不能创建硬链接.

digoal@digoal-Dell-System-Vostro-3450:~/script$ ln / /abc  
ln: "/": 不允许将硬链接指向目录  

软链接和硬链接的不同之处, 软链接不是指向同一个inode number.

digoal@digoal-Dell-System-Vostro-3450:~/script$ ln -s ./def abc  
digoal@digoal-Dell-System-Vostro-3450:~/script$ stat abc  
  文件:"abc" -> "./def"  
  大小:5         	块:0          IO 块:4096   符号链接  
设备:801h/2049d	Inode:1456078     硬链接:1  
权限:(0777/lrwxrwxrwx)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-17 14:41:39.732067159 +0800  
最近更改:2013-05-17 14:41:39.732067159 +0800  
最近改动:2013-05-17 14:41:39.732067159 +0800  
创建时间:-  
digoal@digoal-Dell-System-Vostro-3450:~/script$ stat def  
  文件:"def"  
  大小:0         	块:0          IO 块:4096   普通空文件  
设备:801h/2049d	Inode:1442547     硬链接:1  
权限:(0664/-rw-rw-r--)  Uid:( 1000/  digoal)   Gid:( 1000/  digoal)  
最近访问:2013-05-17 14:39:22.020069682 +0800  
最近更改:2013-05-17 14:39:22.020069682 +0800  
最近改动:2013-05-17 14:40:18.336068650 +0800  
创建时间:-  

硬链接不能跨文件系统, 但是软链接可以.

digoal@digoal-Dell-System-Vostro-3450:~/script$ ln /home/digoal/script/abc /data01/test  
ln: 无法创建硬链接"/data01/test" => "/home/digoal/script/abc": 无效的跨设备连接  
digoal@digoal-Dell-System-Vostro-3450:~/script$ ln -s /home/digoal/script/abc /data01/test  
digoal@digoal-Dell-System-Vostro-3450:~/script$ ll /data01/test  
lrwxrwxrwx 1 digoal digoal 23  5月 17 14:43 /data01/test -> /home/digoal/script/abc  

参考

1. http://en.wikipedia.org/wiki/Inode_pointer_structure

2. http://en.wikipedia.org/wiki/Ln_(Unix)

3. http://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html

4. http://pubs.opengroup.org/onlinepubs/009695399/utilities/ln.html

5. http://en.wikibooks.org/wiki/Guide_to_Unix/Commands/File_System_Utilities#ln

6. http://www.linfo.org/inode.html

7. http://en.wikipedia.org/wiki/Inode

8. http://en.wikipedia.org/wiki/Hard_link

Flag Counter

digoal’s 大量PostgreSQL文章入口