概述
linux的文件系统是使用面向对象的设计思想实现的,但是使用了C语言实现,然而,C语言不支持面向对象,所以文件系统主要是使用了结构体以及函数指针进行实现的。结构体就是对象,函数指针就是对象的成员变量。
最主要的结构体有四类:
- 超级块对象
- 索引节点对象(inode)
- 目录项对象
- 文件对象
超级块对象
下面是超级块结构体部分代码,在linux源码文件中的include/linux/fs.h中实现,超级块结构体中包含设备描述符,超级块的大小,文件系统类型,文件系统最大文件大小等信息。有一个s_op指针,指向super_operations结构体。
struct super_block {
struct list_head s_list; /* Keep this first */ 指向所有超级块的链表
dev_t s_dev; /* search index; _not_ kdev_t */ 设备标识符
unsigned char s_dirt;
unsigned char s_blocksize_bits; 超级块的大小(以bit为单位)
unsigned long s_blocksize; 超级块的大小(以字节为单位)
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;
atomic_t s_active;
这个super_operations结构体包含了许多函数指针,可以看作super_block的成员函数变量,可以根据指针名称猜测它们的作用,主要是创建一个新的inode,删除inode,修改inode的信息,修改超级块,展示文件路径等。
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *);
void (*dirty_inode) (struct inode *, int flags);
int (*write_inode) (struct inode *, struct writeback_control *wbc);
int (*drop_inode) (struct inode *);
void (*evict_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 (*umount_begin) (struct super_block *);
int (*show_options)(struct seq_file *, struct vfsmount *);
int (*show_devname)(struct seq_file *, struct vfsmount *);
int (*show_path)(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);
};
我们在后面再学习inode,目录项对象以及文件对象。