linux文件系统的注册,linux文件系统-文件系统注册

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值