1.struct file 结构
文件结构 代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry //该成员是对应的 目录结构 。
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op; //该操作 是定义文件关联的操作的。内核在执行open时对这个 指针赋值。
atomic_long_t f_count;
unsigned int f_flags; //该成员是文件标志。
mode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
struct file_ra_state f_ra;
u64 f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
/* needed for tty driver, and maybe others */
void *private_data;//该成员是系统调用时保存状态信息非常有用的资源。
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};
该结构体中的f_path结构体的结构如下:
struct path f_path;
被定义在linux/include/linux/namei.h中,其原型为:
struct path {
struct vfsmount *mnt;
struct dentry *dentry; //dentry用了查询i_node号
};
struct dentry的结构体如下:
struct dentry {
atomic_t d_count; 目录项对象使用计数器,可以有未使用态,使用态和负状态
unsigned int d_flags; 目录项标志
struct inode * d_inode; 与文件名关联的索引节点
struct dentry * d_parent; 父目录的目录项对象
struct list_head d_hash; 散列表表项的指针
struct list_head d_lru; 未使用链表的指针
struct list_head d_child; 父目录中目录项对象的链表的指针
struct list_head d_subdirs;对目录而言,表示子目录目录项对象的链表
struct list_head d_alias; 相关索引节点(别名)的链表
int d_mounted; 对于安装点而言,表示被安装文件系统根项
struct qstr d_name; 文件名
unsigned long d_time; /* used by d_revalidate /
struct dentry_operations d_op; 目录项方法
struct super_block * d_sb; 文件的超级块对象
vunsigned long d_vfs_flags;
void * d_fsdata;与文件系统相关的数据
unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名
};
file_operations:文件操作函数struct
struct file_operations {
struct module *owner;//拥有该结构的模块的指针,一般为THIS_MODULES
loff_t (*llseek) (struct file *, loff_t, int);//用来修改文件当前的读写位置
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);//从设备中同步读取数据
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);//向设备发送数据
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//初始化一个异步的读取操作
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//初始化一个异步的写入操作
int (*readdir) (struct file *, void *, filldir_t);//仅用于读取目录,对于设备文件,该字段为NULL
unsigned int (*poll) (struct file *, struct poll_table_struct *); //轮询函数,判断目前是否可以进行非阻塞的读写或写入
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); //执行设备I/O控制命令
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); //不使用BLK文件系统,将使用此种函数指针代替ioctl
long (*compat_ioctl) (struct file *, unsigned int, unsigned long); //在64位系统上,32位的ioctl调用将使用此函数指针代替
int (*mmap) (struct file *, struct vm_area_struct *); //用于请求将设备内存映射到进程地址空间
int (*open) (struct inode *, struct file *); //打开
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *); //关闭
int (*fsync) (struct file *, struct dentry *, int datasync); //刷新待处理的数据
int (*aio_fsync) (struct kiocb *, int datasync); //异步刷新待处理的数据
int (*fasync) (int, struct file *, int); //通知设备FASYNC标志发生变化
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
};
i_node的结构体如下:
struct inode {
struct hlist_node i_hash; // 哈希表 */
struct list_head i_list; // 索引节点链表 */
struct list_head i_dentry; // 目录项链表 */
unsigned long i_ino; // 节点号 */
atomic_t i_count; // 引用记数 */
umode_t i_mode; // 访问权限控制 */
unsigned int i_nlink; // 硬链接数 */
uid_t i_uid; // 使用者id */
gid_t i_gid; // 使用者id组 */
kdev_t i_rdev; // 实设备标识符 */
loff_t i_size; // 以字节为单位的文件大小 */
struct timespec i_atime; // 最后访问时间 */
struct timespec i_mtime; // 最后修改(modify)时间 */
struct timespec i_ctime; // 最后改变(change)时间 */
unsigned int i_blkbits; // 以位为单位的块大小 */
unsigned long i_blksize; // 以字节为单位的块大小 */
unsigned long i_version; // 版本号 */
unsigned long i_blocks; // 文件的块数 */
unsigned short i_bytes; // 使用的字节数 */
spinlock_t i_lock; // 自旋锁 */
struct rw_semaphore i_alloc_sem; // 索引节点信号量 */
struct inode_operations *i_op; // 索引节点操作表 */
struct file_operations *i_fop; // 默认的索引节点操作 */
struct super_block *i_sb; // 相关的超级块 */
struct file_lock *i_flock; // 文件锁链表 */
struct address_space *i_mapping; // 相关的地址映射 */
struct address_space i_data; // 设备地址映射 */
struct dquot *i_dquot[MAXQUOTAS]; // 节点的磁盘限额 */
struct list_head i_devices; // 块设备链表 */
struct pipe_inode_info *i_pipe; // 管道信息 */
struct block_device *i_bdev; // 块设备驱动 */
unsigned long i_dnotify_mask; // 目录通知掩码 */
struct dnotify_struct *i_dnotify; // 目录通知 */
unsigned long i_state; // 状态标志 */
unsigned long dirtied_when; // 首次修改时间 */
unsigned int i_flags; // 文件系统标志 */
unsigned char i_sock; // 可能是个套接字吧 */
atomic_t i_writecount; // 写者记数 */
void *i_security; // 安全模块 */
__u32 i_generation; // 索引节点版本号 */
union {
void *generic_ip; // 文件特殊信息 */
} u;
};