Chinaunix首页 | 论坛 | 博客
  • 博客访问: 371890
  • 博文数量: 47
  • 博客积分: 967
  • 博客等级: 准尉
  • 技术积分: 1290
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-25 16:14
文章分类

全部博文(47)

文章存档

2019年(1)

2014年(1)

2013年(9)

2012年(36)

分类: LINUX

2012-09-07 13:57:49

    前几天我主要把VFS这块的概念理清楚了,现在该是实战的时候了,只有在实战的过程中才能更加清楚的理清他们之间的逻辑关系。这两天写了个代码:打印超级块和索引节点,具体如下:


  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. #define SUPER_BLOCKS 0xffffffff81c41c10
  10. #define SB_LOCK 0xffffffff81f03c20

  11. static int __init my_init(void)
  12. {
  13.     struct super_block *sb;
  14.     struct list_head *pos;
  15.     struct list_head *lnode;
  16.      
  17.     printk("super_blocks:\n");
  18.     spin_lock((spinlock_t *)SB_LOCK);   //加锁
  19.     
  20.     list_for_each(pos,(struct list_head *)SUPER_BLOCKS) {
  21.         sb = list_entry(pos,struct super_block,s_list);
  22.         printk("name---->%s\t\t",sb->s_type->name);
  23.         printk("dev_t %d:%d\n",MAJOR(sb->s_dev),MINOR(sb->s_dev));
  24.     }
  25.     spin_unlock((spinlock_t *)SB_LOCK);  //解锁

  26.     return 0;
  27. }
   大家可能对这个代码有所疑惑:为什么我们不直接使用sb_lock和super_block,原因是这两个变量没有导出所以只能使用他们的地址。在Linux中,并不是每个变量和函数都可以在其他子系统和模块中被引用,只有导出后才能被引用。我们可以通过管道来对此进行取值,这里一定要用root权限,如果不用root权限你将看不到任何地址。具体如下:


  1. lwp@lwp-linux:~$ sudo cat /proc/kallsyms |grep super_blocks
  2. [sudo] password for lwp:
  3. c182928c D super_blocks             
  4. lwp@lwp-linux:~$ sudo cat /proc/kallsyms |grep sb_block
  5. c11be410 t get_sb_block
  6. c11dc750 t get_sb_block
     这里用来查找的List_for_each()和list_entry()这两个函数都在list.h中,用来遍历链表,具体用法在前面的博客中已经提到。这段代码主要打印了文件系统名和文件系统所在设备的主设备号和次设备号。
 我们也可以打印索引结点号,代码如下:


  1. list_for_each(linode,&sb->s_inodes){
  2.         pinode=list_entry(linode,struct inode,i_sb_list);
  3.         count++;
  4.         printk("%lu\t",pinode->i_ino);
  5.         }

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