Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894652
  • 博文数量: 119
  • 博客积分: 2493
  • 博客等级: 大尉
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 14:00
文章分类

全部博文(119)

文章存档

2013年(19)

2012年(100)

分类: LINUX

2012-07-25 20:19:58

         linux上使用的是来自UNIX的文件系统的设计理念,采用的是一种开放式的架构,
该设计思想很是经典,主要就是体现在一种可拓展性和统一接口,VFS也算是linux设计
的一大亮点。该设计理念也可以借鉴到应用软件的设计中。
----------------------------------------------------------------------------------------
一,VFS的示意图。
 
--------------------------------------------------------------------------------------------
二、VFS的主要数据数据结构。
VFS主要有四个数据结构:

超级块对象:struct super_block  相当于一个具体文件系统的描述符。VFS为了管理若干个
具体的文件系统为每个具体的文件系统设置了该数据结构。该数据结构是由双向循环链表连起
来的,链表头是一个全局变量struct head_list super_blocks

索引节点对象:struct inode 该结构是文件的描述符,也就是FCB(file control block),
是对一个文件的具体的描述。

目录项对象:struct dentry 是对一个文件逻辑上的描述。

文件对象:struct file 用来描述进程打开的文件。

-------------------------------------------------------------------------------------------
三、对虚拟文件系统VFS四个对象中的字段进行分析。
      待续...





----------------------------------------------------------------------------------------------
四、实战,使用内核模块遍历struct super_block双向循环链表


---------------------------------------------------------------------------------------------

  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/list.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/kdev_t.h>
  8. #include <linux/fs.h>

  9. //由于这个两个全局变量没有导出所以要直接使用地址
  10. #define SUPER_BLOCKS 0xc07c7814
  11. #define SB_LOCK 0xc0981280

  12. static int __init my_init(void)
  13. {
  14.     struct super_block *sb;
  15.     struct list_head *pos;

  16.     spin_lock((spinlock_t *)SB_LOCK);
  17.     
  18.     list_for_each(pos,(struct list_head *)SUPER_BLOCKS) {
  19.         sb = list_entry(pos,struct super_block,s_list);
  20.         printk("name---->%s\t\t\t",sb->s_type->name);
  21.         printk("dev_t %d:%d\n",MAJOR(sb->s_dev),MINOR(sb->s_dev));
  22.     }
  23.     spin_unlock((spinlock_t *)SB_LOCK);

  24.     return 0;
  25. }
  26. static void __exit my_exit(void)
  27. {
  28.     printk("---------------->exit\n");
  29. }

  30. module_init(my_init);
  31. module_exit(my_exit);
  32. MODULE_LICENSE("GPL");
---------------------------------------------------------------------------------------------



阅读(1227) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~