Tuesday, October 20, 2015

Linux File System - superblock inode dentry



http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file
The superblock is essentially file system metadata and defines the file system type, size, status, and information about other metadata structures (metadata of metadata). The superblock is very critical to the file system and therefore is stored in multiple redundant copies for each file system. The superblock is a very "high level" metadata structure for the file system. For example, if the superblock of a partition, /var, becomes corrupt then the file system in question (/var) cannot be mounted by the operating system. Commonly in this event fsck is run and will automatically select an alternate, backup copy of the superblock and attempt to recover the file system. The backup copies themselves are stored in block groups spread through the file system with the first stored at a 1 block offset from the start of the partition. This is important in the event that a manual recovery is necessary. You may view information about superblock backups with the command dumpe2fs /dev/foo | grep -i superblock which is useful in the event of a manual recovery attempt. Let us suppose that the dumpe2fs command outputs the line Backup superblock at 163840, Group descriptors at 163841-163841. We can use this information, and additional knowledge about the file system structure, to attempt to use this superblock backup: /sbin/fsck.ext3 -b 163840 -B 1024 /dev/foo. Please note that I have assumed a block size of 1024 bytes for this example.
An inode exists in, or on, a file system and represents metadata about a file. For clarity, all objects in a Linux or UNIX system are files; actual files, directories, devices, and so on. Please note that, among the metadata contained in an inode, there is no file name as humans think of it, this will be important later. An inode contains essentially information about ownership (user, group), access mode (read, write, execute permissions) and file type.
dentry is the glue that holds inodes and files together by relating inode numbers to file names. Dentries also play a role in directory caching which, ideally, keeps the most frequently used files on-hand for faster access. File system traversal is another aspect of the dentry as it maintains a relationship between directories and their files.
http://daoluan.net/blog/inode-vnode-dentry/
inode定位文件在磁盘的位置,它的信息本身是存储在磁盘等上的,当打开文件的时候从磁盘上读入内存。 fs.jpginode信息就存储在磁盘的某个分区上。下图是上图的一个扩展:inode指示了文件在数据块中的物理位置。所以仅仅存在inode无法描述一个完整的文件系统,比如:目录与目录的树状结构,这一点在inode上无法体现。

inux 系统下一个文件的存储相关概念包括,文件名,inode 和文件对应的数据。三者的关系可以表示为:
inode 与 数据
                               .---------------> ! data ! ! data ! etc
                              /                  +------+ !------+
        ! permbits, etc ! data addresses !
        +------------inode---------------+
以及
inode 与 文件名:文件名部分包含了文件名和 inode 的位置,通过文件名可以找到 inode
                         .--------------> ! permbits, etc ! addresses !
                        /                 +---------inode-------------+
        ! filename ! inode # !
        +--------------------+
现在将多个文件名指向同一个 inode,这就是硬链接:
        ! filename ! inode # !
        +--------------------+
                        \
                         >--------------> ! permbits, etc ! addresses !
                        /                 +---------inode-------------+
        ! othername ! inode # !
        +---------------------+
而软链接的情况有所不同,软链接的 inode 指向一个特殊文件,访问特殊文件的时候会被重定向到一个新的文件名,其再找到相应的 inode,最后找到最终的数据。
        ! filename ! inode # !
        +--------------------+
                        \
                         .-------> ! permbits, etc ! addresses !
                                   +---------inode-------------+
                                                      /
                                                     /
                                                    /
    .----------------------------------------------'
   ( 
    '-->  !"/path/to/some/other/file"! 
          +---------data-------------+
                  /                      }
    .~ ~ ~ ~ ~ ~ ~                       }-- (redirected at open() time)
   (                                     }
    '~~> ! filename ! inode # !
         +--------------------+
                         \
                          '------------> ! permbits, etc ! addresses !
                                         +---------inode-------------+
                                                            /
                                                           /
     .----------------------------------------------------'
    (
     '->  ! data !  ! data ! etc.
          +------+  +------+
总结如下:
  • 如果多个 inode 指向同一个数据块的时候,是不是就可以实现熟悉的链接了?!这就是软连接的原理,新建一个文件(一个符号链接文件,文件的属性中有明确说明它是一个符号链接文件),为需要链接的文件分配一个新的 inode ,然后指向同一个文件。所以删除软连接文件不会真正删除源文件,而删除源文件过后,软连接文件将失效。
  • 多个文件共用一个 inode ,同样可以实现链接?!这就是硬链接的原理, inode 中有链接计数器,当增加一个文件指向这个 inode 时,计数器增1。特别的,当计数器为 0 时候,即所有的文件都删除,文件才真正从磁盘删除;当然,修改其中任何一个文件,都会作用在其他硬链接文件上。

其他

i_thumb.png
ext3_inode上的数据结构如下:它记录了很多关于文件的信息,比如文件长度,文件所在的设备,文件的物理位置,创建、修改和更新时间等等,特别的,它不包含文件名!
struct ext3_inode {
 __le16 i_mode; File mode
 __le16 i_uid; Low 16 bits of Owner Uid
 __le32 i_size; Size in bytes
 __le32 i_atime; Access time 
 __le32 i_ctime; Creation time
 __le32 i_mtime; Modification time

 __le32 i_dtime; Deletion Time
 __le16 i_gid; Low 16 bits of Group Id
 __le16 i_links_count; Links count
 ......
 __le32 i_block[EXT2_N_BLOCKS]; Pointers to blocks
 ...... 
};
Linux上有dentry,中文的意思就是目录项,它粘合了内存中文件和磁盘中文件,同时它保存是经常访问的目录信息。
http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-fileA dentry is the glue that holds inodes and files together by relating inode numbers to file names. Dentries also play a role in directory caching which, ideally, keeps the most frequently used files on-hand for faster access. File system traversal is another aspect of the dentry as it maintains a relationship between directories and their files.下面是一副很有趣的图片:
inodedentry_thumb.jpg
struct dentry {
 atomic_t d_count; 目录项对象使用计数器
 unsigned int d_flags; 目录项标志
 struct inode * d_inode; 与文件名关联的索引节点
 struct dentry * d_parent; 父目录的目录项对象
 struct list_head d_hash; 散列表表项的指针
 struct list_head d_lru; 未使用链表的指针
 struct list_head d_child; 父目录中目录项对象的链表的指针
 struct list_head d_subdirs;对目录而言,表示子目录目录项对象的链表
 struct list_head d_alias; 相关索引节点(别名)的链表
 int d_mounted; 对于安装点而言,表示被安装文件系统根项
 struct qstr d_name; 文件名
 unsigned long d_time; // used by d_revalidate
 struct dentry_operations *d_op; 目录项方法
 struct super_block * d_sb; 文件的超级块对象
 vunsigned long d_vfs_flags;
 void * d_fsdata;与文件系统相关的数据
 unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名
};
诸如文件名,父目录等。dentry可以描述目录的树状结构。
http://blog.csdn.net/jnu_simba/article/details/8806654

https://www.kernel.org/doc/Documentation/filesystems/vfs.txt

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts