Random Thoughts

Recent content on Random Thoughts

马上订阅 Random Thoughts RSS 更新: https://blog.joway.io/index.xml

Linux I/O 栈浅析

2019年8月11日 08:00

在 Linux 中,所有外部资源都以文件形式作为一个抽象视图,并提供一套统一的接口给应用程序调用。本文将以宏观视角试图阐述 Linux 中关于文件 IO 的整个调用脉络。

VFS

在 Linux 中,所有 IO 都必须先经由 VFS 层进行转发。通过 VFS 将包括磁盘、网络 Socket、打印机、管道等资源全部封装成统一的接口。

基础结构

VFS 自顶向下使用四个数据结构来描述文件:

  • file: 存放一个文件对象的信息。
struct file {
 union {
 struct llist_node fu_llist;
 struct rcu_head fu_rcuhead;
 } f_u;
 struct path f_path;
 struct inode *f_inode; /* cached value */
 const struct file_operations *f_op;

 struct mutex f_pos_lock;
 loff_t f_pos;
}
  • dentry: 存放目录项和其下的文件链接信息。
struct dentry {
 unsigned int d_flags;
 seqcount_t d_seq;
 struct hlist_bl_node d_hash; /* 哈希链表 */
 struct dentry *d_parent; /* 父目录项 */
 struct qstr d_name; /* 目录名 */
 struct inode *d_inode; /* 对应的索引节点 */
 unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */

 struct lockref d_lockref; /* per-dentry lock and refcount */
 const struct dentry_operations *d_op; /* dentry操作 */
 struct super_block *d_sb; /* 文件的超级块对象 */
 unsigned long d_time;
 void *d_fsdata;

 struct list_head d_lru; /* LRU list */
 struct list_head d_child; /* child of parent list */
 struct list_head d_subdirs; /* our children */

 union {
 struct hlist_node d_alias; /* inode alias list */
 struct rcu_head d_rcu;
 } d_u;
}
  • inode: 索引节点对象,存在具体文件的一般信息,文件系统中的文件的唯一标识。...

剩余内容已隐藏

查看完整文章以阅读更多