Chinaunix首页 | 论坛 | 博客
  • 博客访问: 161360
  • 博文数量: 115
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-28 14:16
文章分类

全部博文(115)

文章存档

2017年(36)

2016年(79)

我的朋友

分类: LINUX

2017-03-12 17:08:22

linux 文件系统注册
 
1, 相关数据结构
 
//文件系统类型结构
struct file_system_type {
    const char *name; //文件系统名
    int fs_flags;  //文件系统标志位
    int (*get_sb) (struct file_system_type *, int,
               const char *, void *, struct vfsmount *); //读超级块的方法
    void (*kill_sb) (struct super_block *); //删除超级块的方法
    struct module *owner; //执向实现文件系统的模块
    struct file_system_type * next; //文件系统链表中下一个元素
    struct list_head fs_supers; //具有相同文件系统类型的超级块对象链表的头
};
 
 
2, ext3 文件系统注册
 
ext3文件系统是以模块插入到kernel中的,其注册的过程如下:
 
//定义ext3文件系统类型结构
static struct file_system_type ext3_fs_type = {
    .owner      = THIS_MODULE,
    .name       = "ext3",
    .get_sb     = ext3_get_sb,
    .kill_sb    = kill_block_super,
    .fs_flags   = FS_REQUIRES_DEV,
};     
   
//ext3模块加载时执行该过程函数
static int __init init_ext3_fs(void)
{      
    int err = init_ext3_xattr();
    if (err)
        return err;
    err = init_inodecache();
    if (err)
        goto out1;
        err = register_filesystem(&ext3_fs_type);
    if (err)
        goto out;
    return 0;
out:
    destroy_inodecache();
out1:
    exit_ext3_xattr();
    return err;
}

module_init(init_ext3_fs)
module_exit(exit_ext3_fs)
 
 
 44 static struct file_system_type **find_filesystem(const char *name)
 45 {  
 46     struct file_system_type **p;
 47     for (p=&file_systems; *p; p=&(*p)->next) //在全局链表file_systems中查找
 48         if (strcmp((*p)->name,name) == 0)
 49             break;
 50     return p; //如找到返回文件系统类型指针,若没找到返回NULL
 51 }
 
// 文件系统注册函数流程
 53 /**
 54  *  register_filesystem - register a new filesystem
 55  *  @fs: the file system structure
 56  *
 57  *  Adds the file system passed to the list of file systems the kernel
 58  *  is aware of for mount and other syscalls. Returns 0 on success,
 59  *  or a negative errno code on an error.
 60  *
 61  *  The &struct file_system_type that is passed is linked into the kernel
 62  *  structures and must not be freed until the file system has been
 63  *  unregistered.
 64  */
 65 
 66 int register_filesystem(struct file_system_type * fs)
 67 {
 68     int res = 0;
 69     struct file_system_type ** p;
 70
 71     if (!fs)
 72         return -EINVAL;
 73     if (fs->next)  //fs的next域必须为null以便加入到链表
 74         return -EBUSY;
 75     INIT_LIST_HEAD(&fs->fs_supers); //初始化fs_supers链表结构
 76     write_lock(&file_systems_lock);
 77     p = find_filesystem(fs->name);
 78     if (*p) //若不为空,说明文件系统已经注册
 79         res = -EBUSY;
 80     else
 81         *p = fs; //若为空,把fs文件系统加入到全局文件系统链表中
 82     write_unlock(&file_systems_lock);
 83     return res;
 84 }
 
 
 /**
 89  *  unregister_filesystem - unregister a file system
 90  *  @fs: filesystem to unregister
 91  *
 92  *  Remove a file system that was previously successfully registered
 93  *  with the kernel. An error is returned if the file system is not found.
 94  *  Zero is returned on a success.
 95  * 
 96  *  Once this function has returned the &struct file_system_type structure
 97  *  may be freed or reused.
 98  */
 99
100 int unregister_filesystem(struct file_system_type * fs)
101 {
102     struct file_system_type ** tmp;
103
104     write_lock(&file_systems_lock);
105     tmp = &file_systems;
106     while (*tmp) {
107         if (fs == *tmp) {
108             *tmp = fs->next;
109             fs->next = NULL;
110             write_unlock(&file_systems_lock);
111             return 0;
112         }
113         tmp = &(*tmp)->next;
114     }
115     write_unlock(&file_systems_lock);
116     return -EINVAL;
117 }
 
216
217 struct file_system_type *get_fs_type(const char *name)
218 {
219     struct file_system_type *fs;
220
221     read_lock(&file_systems_lock);
222     fs = *(find_filesystem(name));
223     if (fs && !try_module_get(fs->owner))
224         fs = NULL;
225     read_unlock(&file_systems_lock);
226     if (!fs && (request_module("%s", name) == 0)) {
227         read_lock(&file_systems_lock);
228         fs = *(find_filesystem(name));
229         if (fs && !try_module_get(fs->owner))
230             fs = NULL;
231         read_unlock(&file_systems_lock);
232     }
233     return fs;
234 }
 
 
阅读(1067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~