Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614927
  • 博文数量: 120
  • 博客积分: 2284
  • 博客等级: 大尉
  • 技术积分: 1330
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-25 10:49
个人简介

http://guliqun1983.blog.163.com/blog/static/501116852011730535314/

文章分类
文章存档

2013年(23)

2012年(23)

2011年(74)

分类: LINUX

2013-04-24 10:29:50

声明:本文为原创
#####请转贴时保留以下内容######
作者GTT
本文档归属http://oldtown.cublog.cn/.转载请注明出处!
请提出宝贵意见Mail:mtloveft@hotmail.com
Linux Version:2.6.33
提示本文是关于file system 实现的介绍
 
先把super block的定义代码传上来,如下

struct super_block {
    struct list_head s_list; /* Keep this first */
    dev_t s_dev; /* search index; _not_ kdev_t */
    unsigned long s_blocksize;
    unsigned char s_blocksize_bits;
    unsigned char s_dirt;
    loff_t s_maxbytes; /* Max file size */
    struct file_system_type *s_type;
    const struct super_operations *s_op;
    const struct dquot_operations *dq_op;
    const struct quotactl_ops *s_qcop;
    const struct export_operations *s_export_op;
    unsigned long s_flags;
    unsigned long s_magic;
    struct dentry *s_root;
    struct rw_semaphore s_umount;
    struct mutex s_lock;
    int s_count;
    int s_need_sync;
    atomic_t s_active;
#ifdef CONFIG_SECURITY
    void *s_security;
#endif
    struct xattr_handler **s_xattr;

    struct list_head s_inodes; /* all inodes */
    struct hlist_head s_anon; /* anonymous dentries for (nfs) exporting */
    struct list_head s_files;
/* s_dentry_lru and s_nr_dentry_unused are protected by dcache_lock */
    struct list_head s_dentry_lru; /* unused dentry lru */
    int s_nr_dentry_unused; /* # of dentry on lru */

    struct block_device *s_bdev;
    struct backing_dev_info *s_bdi;
    struct mtd_info *s_mtd;
    struct list_head s_instances;
    struct quota_info s_dquot; /* Diskquota specific options */

    int s_frozen;
    wait_queue_head_t s_wait_unfrozen;

    char s_id[32]; /* Informational name */

    void *s_fs_info; /* Filesystem private info */
    fmode_t s_mode;

/* The next field is for VFS *only*. No filesystems have any business even looking at it. You had been warned. */
    struct mutex s_vfs_rename_mutex; /* Kludge */

/* Granularity of c/m/atime in ns. Cannot be worse than a second */
    u32 s_time_gran;

/* Filesystem subtype. If non-empty the filesystem type field in /proc/mounts will be "type.subtype" */
    char *s_subtype;

/* Saved mount options for lazy filesystems using generic_show_options() */
    char *s_options;
};

 
super_block描述整个分区的文件系统信息,例如块大小、文件系统版本号、上次mount的时间等等。
 
super_block定义的字段比较多,现在没必要清楚每个字段的具体意义,但其中重要的字段必须了解。
当一个文件系统被mount时,系统将调用文件系统的get_sb,生成一个super block结构,也就是说,每一个
安装的文件系统,都有一个super block 实例与之对应。s_type就是文件系统的定义指针。这样通过s_type
就可以找到文件体统。linux系统利用s_list把所有的super block都链接到super_blocks上。这样安装了多少个文件系统,通过super_blocks链表,遍历一下马上就能知道了。
 
struct super_operations  *s_op是super block 的操作方法,struct super_operations是一个VFT即
virture function table ,虚拟方法表。我是这么翻译的,可能有的书里翻译成虚拟功能表等,一个东西。
直接就是VFT最方便,也不用翻译。因为各个文件系统的super block的操作方法不同,所以定义了这个VFT。
看看他的定义

struct super_operations {
    struct inode *(*alloc_inode)(struct super_block *sb);
    void (*destroy_inode)(struct inode *);

    void (*dirty_inode) (struct inode *);
    int (*write_inode) (struct inode *, int);
    void (*drop_inode) (struct inode *);
    void (*delete_inode) (struct inode *);
    void (*put_super) (struct super_block *);
    void (*write_super) (struct super_block *);
    int (*sync_fs)(struct super_block *sb, int wait);
    int (*freeze_fs) (struct super_block *);
    int (*unfreeze_fs) (struct super_block *);
    int (*statfs) (struct dentry *, struct kstatfs *);
    int (*remount_fs) (struct super_block *, int *, char *);
    void (*clear_inode) (struct inode *);
    void (*umount_begin) (struct super_block *);

    int (*show_options)(struct seq_file *, struct vfsmount *);
    int (*show_stats)(struct seq_file *, struct vfsmount *);
#ifdef CONFIG_QUOTA
    ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
    ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
#endif
    int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
};

 
通过方法名称就基本可以了解要干什么了,
比如alloc_inode,就是分配一个inode 。destroy_inode,就是销毁一个inode等等
super block 要管理inode,所以super block 把所有分配的inode 都链接到s_inodes上。

把所有跟super block 关联的文件都链接到s_files上。

把未使用的dentry链接到s_dentry_lru这个链上。
 
 
 
 
 


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