Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2930640
  • 博文数量: 401
  • 博客积分: 12926
  • 博客等级: 上将
  • 技术积分: 4588
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-22 14:51
文章分类

全部博文(401)

文章存档

2015年(16)

2014年(4)

2013年(12)

2012年(82)

2011年(98)

2010年(112)

2009年(77)

分类: LINUX

2010-11-24 18:09:40

2.3.                    super_block  super_operations


一个存放在磁盘上的文件系统如 EXT2 等,在它的格式中通常包括一个“超级块”或者“控制块”的部分,用于从整体上描述文件系统,例如文件系统的大小、是否可读可写等等。

虚拟文件系统中也通过“超级块”这种概念来描述文件系统整体的信息,对应的结构是 struct super_block

super_block 除了要记录文件大小、访问权限等信息外,更重要的是提供一个操作“接口”super_operations

struct super_operations {

            
struct inode *(*alloc_inode)(struct super_block *sb);
            
void (*destroy_inode)(struct inode *);
            
void (*read_inode) (struct inode *);
            
void (*read_inode2) (struct inode *void *) ;
            
void (*dirty_inode) (struct inode *);
            
void (*write_inode) (struct inode *int);
            
void (*put_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 *);
            
void (*write_super_lockfs) (struct super_block *);
            
void (*unlockfs) (struct super_block *);
            
int (*statfs) (struct super_block *struct statfs *);
            
int (*remount_fs) (struct super_block *int *char *);
            
void (*clear_inode) (struct inode *);
            
void (*umount_begin) (struct super_block *);
            
struct dentry * (*fh_to_dentry)(struct super_block *sb, __u32 *fh, int len, int fhtype, int parent);
            
int (*dentry_to_fh)(struct dentry *, __u32 *fh, int *lenp, int need_parent);
            
int (*show_options)(struct seq_file *struct vfsmount *);
};

我们通过分析“获取一个 inode ”的过程来只理解这个“接口”中两个成员 alloc_inode  read_inode 的作用。

在文件系统的操作中,经常需要获得一个“目录节点”对应的 inode,这个 inode 有可能已经存在于内存中了,也可能还没有,需要创建一个新的 inode,并从磁盘上读取相应的信息来填充。

对应的代码是 iget()   inlcude/linux/fs.h)过程如下:

1、 通过 iget4_locked() 获取 inode。如果 inode 在内存中已经存在,则直接返回;否则创建一个新的 inode

2、 如果是新创建的 inode,通过 super_block->s_op->read_inode() 来填充它。也就是说,如何填充一个新创建的 inode 是由具体文件系统提供的函数实现的。

iget4_locked() 首先在全局的 inode hash table 中寻找,如果找不到,则调用 get_new_inode() ,进而调用 alloc_inode() 来创建一个新的 inode

 alloc_inode() 中可以看到,如果具体文件系统提供了创建 inode 的方法,则由具体文件系统来负责创建,否则采用系统默认的的创建方法。

static struct inode *alloc_inode(struct super_block *sb)
{

            
static struct address_space_operations empty_aops;
            
static struct inode_operations empty_iops;
            
static struct file_operations empty_fops;
            
struct inode *inode;

             
if (sb->s_op->alloc_inode)
                        inode 
= sb->s_op->alloc_inode(sb);
            
else {
                        inode 
= (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
                        if (inode)
                                    memset(
&inode->u, 0sizeof(inode->u));
            }

             
if (inode) {
                        
struct address_space * const mapping = &inode->i_data;
                        inode
->i_sb = sb;
                        inode
->i_dev = sb->s_dev;
                        inode
->i_blkbits = sb->s_blocksize_bits;
                        inode
->i_flags = 0;
                        atomic_set(
&inode->i_count, 1);
                        inode
->i_sock = 0;
                        inode
->i_op = &empty_iops;
                        inode
->i_fop = &empty_fops;
                        inode
->i_nlink = 1;
                        atomic_set(
&inode->i_writecount, 0);
                        inode
->i_size = 0;
                        inode
->i_blocks = 0;
                        inode
->i_bytes = 0;
                        inode
->i_generation = 0;
                        memset(
&inode->i_dquot, 0sizeof(inode->i_dquot));
                        inode
->i_pipe = NULL;
                        inode
->i_bdev = NULL;
                        inode
->i_cdev = NULL;

                        mapping
->a_ops = &empty_aops;
                        mapping
->host = inode;
                        mapping
->gfp_mask = GFP_HIGHUSER;
                        inode
->i_mapping = mapping;
            }
            
return inode;
}

super_block 是在安装文件系统的时候创建的,后面会看到它和其它结构之间的关系

3.   安装文件系统

1、 一个经过格式化的块设备,只有安装后,才能融入 Linux  VFS 之中。

2、 安装一个文件系统,必须指定一个目录作为安装点。

3、 一个设备可以同时被安装到多个目录上。

4、 如果某个目录下原来有一些文件和子目录,一旦将一个设备安装到目录下后,则原有的文件和子目录消失。因为这个目录已经变成了一个安装点。

5、 一个目录节点下可以同时安装多个设备。


3.1.              “根安装点”、“根设备”和“根文件系统”

安装一个文件系统,除了需要“被安装设备”外,还要指定一个“安装点”。“安装点”是已经存在的一个目录节点。例如把 /dev/sda1 安装到 /mnt/win 下,那么/mnt/win 就是“安装点”。

可是文件系统要先安装后使用。因此,要使用 /mnt/win 这个“安装点”,必然要求它所在文件系统已也经被安装。

也就是说,安装一个文件系统,需要另外一个文件系统已经被安装。

这是一个鸡生蛋,蛋生鸡的问题:最顶层的文件系统是如何被安装的?

答案是,最顶层文件系统的时候是被安装在“根安装点”上的,而根安装点不属于任何文件系统,它对应的 dentry inode 是由内核在初始化阶段凭空构造出来的。

最顶层的文件系统叫做“根文件系统”。Linux 在启动的时候,要求用户必须指定一个“根设备”,内核在初始化阶段,将“根设备”安装到“根安装点”上,从而有了根文件系统。这样,文件系统才算准备就绪。此后,用户就可以通过 mount 命令来安装新的设备。

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