📄 super.c
字号:
/*
* linux/fs/super.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*
* super.c contains code to handle: - mount structures
* - super-block tables
* - filesystem drivers list
* - mount system call
* - umount system call
* - ustat system call
*
* GK 2/5/95 - Changed to support mounting the root fs via NFS
*
* Added kerneld support: Jacques Gelinas and Bjorn Ekwall
* Added change_root: Werner Almesberger & Hans Lermen, Feb '96
* Added options to /proc/mounts:
* Torbj鰎n Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
* Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
* Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
*/
#include <linux/config.h>/*内核配置头文件。定义键盘语言和硬盘类型(HD_TYPE) 可选项*/
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/locks.h>
#include <linux/smp_lock.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/fd.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/quotaops.h>
#include <linux/acct.h>
#include <asm/uaccess.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_fs_sb.h>
#include <linux/nfs_mount.h>
#include <linux/kmod.h>
#define __NO_VERSION__
#include <linux/module.h>
extern void wait_for_keypress(void);/*等待击键函数*/
extern int root_mountflags;/*根模块标记*/
int do_remount_sb(struct super_block *sb, int flags, void * data);/*移除超块*/
/* this is initialized in init/main.c */
kdev_t ROOT_DEV;/*根设备*/
LIST_HEAD(super_blocks);/*系统中所有的super_block都要连接到这个机构后面*/
spinlock_t sb_lock = SPIN_LOCK_UNLOCKED;/*超级块的自旋锁*/
/*
* Handling of filesystem drivers list.
* Rules:
* Inclusion to/removals from/scanning of list are protected by spinlock.
* During the unload module must call unregister_filesystem().
* We can access the fields of list element if:
* 1) spinlock is held or
* 2) we hold the reference to the module.
* The latter can be guaranteed by call of try_inc_mod_count(); if it
* returned 0 we must skip the element, otherwise we got the reference.
* Once the reference is obtained we can drop the spinlock.
*
* 处理文件系统设备链表
* 规则:
* 在表中添加/移除/扫描元素都要有锁来保护。
* 卸载模块时要调用注销文件系统函数unregister_filesystem()
* 当以下条件时我们能够访问链表元素的各个域:
* 1)拥有锁
* 2) 拥有该模块的许可
* 后者需要通过调用try_inc_mod_count()函数来保证。如果该函数
* 返回0, 我们需要跳过这个元素,否则我们能得到这个元素
* 的许可, 一旦我们得到了元素的许可,我们要释放锁。
*
*/
static struct file_system_type *file_systems;/*文件系统类型指针*/
static rwlock_t file_systems_lock = RW_LOCK_UNLOCKED;/*文件系统锁*/
/* WARNING: This can be used only if we _already_ own a reference */
/*警告: 只有在我们已经获得了一个应用时才能使用*/
static void get_filesystem(struct file_system_type *fs)/*文件系统类型结构引用加1*/
{
if (fs->owner)/*如果文件系统所有者存在*/
__MOD_INC_USE_COUNT(fs->owner);/*引用次数加1*/
}
static void put_filesystem(struct file_system_type *fs)/*文件系统类型结构引用减1*/
{
if (fs->owner)/*如果文件系统的所有者存在*/
__MOD_DEC_USE_COUNT(fs->owner);/*引用次数减1*/
}
static struct file_system_type **find_filesystem(const char *name)/*在文件系统类型链表中查找所需文件系统类型*/
{
struct file_system_type **p;/*设置一个文件系统的指针*/
for (p=&file_systems; *p; p=&(*p)->next)/*循环检测文件系统链表*/
if (strcmp((*p)->name,name) == 0)/*如果找到了与给定name同名的文件系统则跳出*/
break;
return p;/*返回指向文件系统的指针,该指针可以为null,即未找到*/
}
/**
* register_filesystem - register a new filesystem
* @fs: the file system structure
*
* Adds the file system passed to the list of file systems the kernel
* is aware of for mount and other syscalls. Returns 0 on success,
* or a negative errno code on an error.
*
* The &struct file_system_type that is passed is linked into the kernel
* structures and must not be freed until the file system has been
* unregistered.
*
* 注册文件系统-注册一个新的文件系统
* @fs: 文件系统结构
*
* 在文件系统链表中添加一个新的文件系统,当加载或其他
* 系统调用时内核从睡眠状态醒来。成功时返回0,或者返回
* 错误信息
*
* 文件系统加入到内核中后只有在文件系统被注销时才会被释放
*
*/
int register_filesystem(struct file_system_type * fs)/*注册文件系统类型*/
{/*顺序搜索链表file_systems,看要注册的文件系统结构是否已存在,存在则错误返回,否则将新的file_system_type结构加入链表的尾部*/
extern int unregister_filesystem(struct file_system_type *);/*注销文件系统*/
int res = 0;/*返回状态*/
struct file_system_type ** p;/*定义一个指向文件系统类型链表的指针*/
if (!fs)/*如果文件系统类型未给定*/
return -EINVAL;/*返回-EINNAL信息, EINVAL:Invalid argument错误数据*/
if (fs->next)/*如果当前文件系统类型的下面还链了一个文件系统类型*/
return -EBUSY;/*返回-EBUSY信息,EBUSY: Device or resource busy设备或资源存在*/
INIT_LIST_HEAD(&fs->fs_supers);/*初始化要注册的文件系统类型的fs_supers链表*/
write_lock(&file_systems_lock);/*写加锁*/
p = find_filesystem(fs->name);/*根据要注册的文件系统类型名在file_systems链表中查找是该文件系统已经注册了*/
if (*p)/*如果指针存在,即表示该文件系统类型已经注册*/
res = -EBUSY;/*返回错误信息-EBUSY*/
else
*p = fs;/*否则p指针指向要注册的文件系统,即把新注册的文件系统类型添加到file_systems链表中*/
write_unlock(&file_systems_lock);/*写解锁*/
return res;/*返回状态*/
}
/**
* unregister_filesystem - unregister a file system
* @fs: filesystem to unregister
*
* Remove a file system that was previously successfully registered
* with the kernel. An error is returned if the file system is not found.
* Zero is returned on a success.
*
* Once this function has returned the &struct file_system_type structure
* may be freed or reused.
*
* 注销文件系统 -注销一个文件系统
* @fs : 文件系统注销
*
* 移出一个曾经成功在内核中注册了的文件系统
* 如果文件系统没有找到将返回错误,成功时返回0
*
* 一旦该函数返回了,文件系统结构将被释放或重新使用
*
*
* ??不知道为什么这样的操作就能注销 ?if (fs == *tmp) { *tmp = fs->next; fs->next = NULL;...}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -